DJI-Gimbal-FOC/src/main.cpp

93 lines
2.1 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-08-30 09:21:03 +00:00
BLDCMotor motor1 = BLDCMotor(4);
BLDCDriver3PWM driver1 = BLDCDriver3PWM(M3_PWM1, M3_PWM2, M3_PWM3);
LinearHallSensor linearSensor1 = LinearHallSensor(M3_Hall1, M3_Hall2);
2022-05-10 14:34:09 +00:00
2022-08-30 09:21:03 +00:00
void initSensor1()
2022-05-19 21:32:31 +00:00
{
2022-08-30 09:21:03 +00:00
linearSensor1.init(motor1);
2022-05-19 21:32:31 +00:00
}
2022-08-30 09:21:03 +00:00
float callback1()
2022-05-19 21:32:31 +00:00
{
2022-08-30 09:21:03 +00:00
return linearSensor1.readSensorCallback();
2022-05-19 21:32:31 +00:00
}
2022-08-30 09:21:03 +00:00
GenericSensor sensor1 = GenericSensor(callback1, initSensor1);
2022-05-19 21:32:31 +00:00
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);
2022-08-30 09:21:03 +00:00
delay(5000);
2022-08-23 11:45:39 +00:00
Serial.println("INIT");
2022-08-30 09:21:03 +00:00
pinMode(LED_BUILTIN, OUTPUT); // Lightup LED
digitalWrite(LED_BUILTIN, LOW);
driver1.voltage_power_supply = 7.1;
driver1.init();
motor1.linkDriver(&driver1);
motor1.useMonitoring(Serial);
motor1.controller = MotionControlType::angle;
motor1.foc_modulation = FOCModulationType::SinePWM;
motor1.voltage_limit = 1.0;
motor1.voltage_sensor_align = 1.0;
motor1.PID_velocity.P = 0.05f;
motor1.PID_velocity.I = 0.01;
motor1.PID_velocity.D = 0.0;
motor1.LPF_velocity.Tf = 0.01f;
motor1.P_angle.P = 150.0;
motor1.P_angle.I = 10.0;
motor1.velocity_limit = 25;
2022-08-23 11:45:39 +00:00
// Init sensor
2022-08-30 09:21:03 +00:00
motor1.init();
2022-08-23 11:45:39 +00:00
Serial.println("calibrating sensor in open loop...");
2022-08-30 09:21:03 +00:00
sensor1.init();
2022-08-23 11:45:39 +00:00
Serial.println("Done");
delay(1000);
2022-08-30 09:21:03 +00:00
motor1.linkSensor(&sensor1);
motor1.init();
2022-08-23 11:45:39 +00:00
// motor.initFOC(5.48, CCW);
2022-08-30 09:21:03 +00:00
motor1.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();
2022-08-30 09:21:03 +00:00
motor1.move(target);
motor1.loopFOC();
motor1.monitor();
2022-05-10 14:34:09 +00:00
}