DJI-Gimbal-FOC/src/main.cpp

118 lines
2.7 KiB
C++
Raw Normal View History

2022-05-10 14:34:09 +00:00
#include <Arduino.h>
#include <SimpleFOC.h>
2022-05-19 21:32:31 +00:00
#include <linearHallSensor.h>
2022-08-23 11:45:39 +00:00
#include <pinout.h>
2022-05-19 21:32:31 +00:00
2022-05-24 08:32:15 +00:00
BLDCMotor motor = BLDCMotor(4);
2022-08-23 11:45:39 +00:00
BLDCDriver3PWM driver = BLDCDriver3PWM(M2_PWM1, M2_PWM2, M2_PWM3);
2022-05-10 14:34:09 +00:00
2022-08-23 11:45:39 +00:00
LinearHallSensor linearSensor = LinearHallSensor(M2_Hall1, M2_Hall2, -0.75, 2.35);
2022-05-19 21:32:31 +00:00
void initSensor()
{
2022-08-23 11:45:39 +00:00
linearSensor.init(motor);
2022-05-19 21:32:31 +00:00
}
float callback()
{
2022-08-23 11:45:39 +00:00
return linearSensor.readSensorCallback();
2022-05-19 21:32:31 +00:00
}
GenericSensor sensor = GenericSensor(callback, initSensor);
2022-05-10 14:34:09 +00:00
float targetX = 0.0;
float targetY = 0.0;
2022-05-24 08:32:15 +00:00
float target = 0.0;
2022-05-10 14:34:09 +00:00
void serialLoop()
{
2022-08-23 11:45:39 +00:00
static String received_chars;
while (Serial.available())
2022-05-10 14:34:09 +00:00
{
2022-08-23 11:45:39 +00:00
char inChar = (char)Serial.read();
received_chars += inChar;
if (inChar == '\n')
{
target = received_chars.toFloat();
Serial.print("Target = ");
Serial.print(target);
received_chars = "";
}
2022-05-10 14:34:09 +00:00
}
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
2022-08-23 11:45:39 +00:00
return (float)(x - in_min) * (out_max - out_min) / (float)(in_max - in_min) + out_min;
2022-05-10 14:34:09 +00:00
}
2022-05-24 08:32:15 +00:00
void setup()
2022-05-10 14:34:09 +00:00
{
2022-08-23 11:45:39 +00:00
Serial.begin(115200);
delay(2000);
Serial.println("INIT");
// pinMode(M1_PWM1, OUTPUT);
// pinMode(M1_PWM2, OUTPUT);
// pinMode(M1_PWM3, OUTPUT);
// digitalWrite(M1_PWM1, LOW);
// digitalWrite(M1_PWM2, LOW);
// digitalWrite(M1_PWM3, LOW);
// pinMode(M2_PWM1, OUTPUT);
// pinMode(M2_PWM2, OUTPUT);
// pinMode(M2_PWM3, OUTPUT);
// digitalWrite(M2_PWM1, LOW);
// digitalWrite(M2_PWM2, LOW);
// digitalWrite(M2_PWM3, LOW);
// pinMode(M3_PWM1, OUTPUT);
// pinMode(M3_PWM2, OUTPUT);
// pinMode(M3_PWM3, OUTPUT);
// digitalWrite(M3_PWM1, LOW);
// digitalWrite(M3_PWM2, LOW);
// digitalWrite(M3_PWM3, LOW);
pinMode(LED_BUILTIN, OUTPUT); // Lightup LED
digitalWrite(LED_BUILTIN, HIGH);
driver.voltage_power_supply = 5.0;
driver.init();
motor.linkDriver(&driver);
motor.useMonitoring(Serial);
motor.controller = MotionControlType::angle;
motor.foc_modulation = FOCModulationType::SinePWM;
motor.voltage_limit = 1.5;
motor.voltage_sensor_align = 0.8;
motor.PID_velocity.P = 0.075f;
motor.PID_velocity.I = 0.01;
motor.PID_velocity.D = 0.0;
motor.LPF_velocity.Tf = 0.01f;
motor.P_angle.P = 120.0;
motor.P_angle.I = 10.0;
motor.velocity_limit = 50;
motor.useMonitoring(Serial);
// Init sensor
motor.init();
Serial.println("calibrating sensor in open loop...");
sensor.init();
Serial.println("Done");
delay(1000);
motor.linkSensor(&sensor);
motor.init();
// motor.initFOC(5.48, CCW);
motor.initFOC();
2022-05-24 08:32:15 +00:00
}
2022-05-10 14:34:09 +00:00
2022-05-24 08:32:15 +00:00
void loop()
{
2022-08-23 11:45:39 +00:00
serialLoop();
motor.move(target);
motor.loopFOC();
motor.monitor();
2022-05-10 14:34:09 +00:00
}