Hello, I’m using simplefoc shield 2 with arduino uno clone and as5047p spi mode. I’m trying closed loop mode of operation for angle position but after some undefinied time, the motor suddenly starts moving like crazy for no reason. I first thought that could be sensor noise, then I thought it could be PID tuning error or maybe some register getting overflow, but I haven’t already found the reason. I will post the code I’m currently using:
/**
*
- Position/angle motion control example
- Steps:
-
- Configure the motor and magnetic sensor
-
- Run the code
-
- Set the target angle (in radians) from serial terminal
*/
#include <SimpleFOC.h>
#include <Wire.h>
char comando;
float posin;
float target;
const float rev = 2 * PI;
float pre;
float dato=0;
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0X3FFF);
BLDCMotor motor = BLDCMotor(11);
BLDCDriver3PWM driver = BLDCDriver3PWM(6,9,5,8);
Commander command = Commander(Serial);
void doTarget(char* cmd) {
command.scalar(&pre, cmd);
target= map(pre, 0, rev, 0, 8 * rev);
}
void doDisable(void) {
motor.disable();
}
void doEnable(void) {
motor.enable();
}
void doZero(void) {
posin = motor.shaft_angle;
target = 0;
}
void setup() {
sensor.init();
motor.linkSensor(&sensor);
driver.voltage_power_supply = 24;
driver.init();
motor.linkDriver(&driver);
// motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
motor.foc_modulation=FOCModulationType::SpaceVectorPWM;
motor.controller = MotionControlType::angle;
motor.voltage_limit = 12;
motor.PID_velocity.P = 0.8f;
motor.PID_velocity.I = 3;
motor.PID_velocity.D = 0;
motor.P_angle.P = 15;
motor.P_angle.I = 1;
motor.P_angle.D = 0;
motor.LPF_velocity.Tf = 0.8f;
motor.velocity_limit = 3;
Serial.begin(115200);
motor.useMonitoring(Serial);
motor.init();
motor.initFOC();
command.add(‘T’, doTarget, “target angle”);
command.add(‘D’, doDisable);
command.add(‘E’, doEnable);
command.add(‘Z’, doZero);
Serial.println(F(“Motor ready.”));
Serial.println(F(“Set the target angle using serial terminal:”));
_delay(1000);
posin = motor.shaftAngle();
target =0;
}
void loop() {
motor.loopFOC();
motor.move(target + posin);
command.run();
}