DJI-Gimbal-FOC/src/main.cpp

97 lines
2.0 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-05-24 08:32:15 +00:00
BLDCMotor motor = BLDCMotor(4);
BLDCDriver3PWM driver = BLDCDriver3PWM(7, 8, 9);
2022-05-10 14:34:09 +00:00
2022-05-24 08:32:15 +00:00
LinearHallSensor linearSensor = LinearHallSensor(A0, A1, -0.75, 2.35);
2022-05-19 21:32:31 +00:00
void initSensor()
{
2022-05-24 08:32:15 +00:00
linearSensor.init(motor);
2022-05-19 21:32:31 +00:00
}
float callback()
{
return linearSensor.readSensorCallback();
}
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()
{
static String received_chars;
while (Serial.available())
{
char inChar = (char)Serial.read();
received_chars += inChar;
if (inChar == '\n')
{
target = received_chars.toFloat();
Serial.print("Target = ");
Serial.print(target);
received_chars = "";
}
}
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (float)(x - in_min) * (out_max - out_min) / (float)(in_max - in_min) + out_min;
}
2022-05-24 08:32:15 +00:00
void setup()
2022-05-10 14:34:09 +00:00
{
2022-05-24 08:32:15 +00:00
Serial.begin(115200);
_delay(100);
Serial.println("INIT");
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
driver.voltage_power_supply = 6.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;
2022-05-24 18:50:38 +00:00
motor.P_angle.P = 120.0;
motor.P_angle.I = 10.0;
2022-05-24 08:32:15 +00:00
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);
2022-05-10 14:34:09 +00:00
2022-05-24 08:32:15 +00:00
motor.linkSensor(&sensor);
motor.init();
2022-05-24 18:50:38 +00:00
motor.initFOC(5.48, CCW);
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()
{
serialLoop();
motor.move(target);
motor.loopFOC();
motor.monitor();
2022-05-10 14:34:09 +00:00
}