Motor Start Spinning Nosense

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:
    1. Configure the motor and magnetic sensor
    1. Run the code
    1. 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();
}

How do you have the encoder attached to the motor? It seems a few times on this forum where things are working initially, but then stop working after things warm up, it was a loose or damaged 3d-printed mount which was deforming.

Hi, it’s firmly attached to the motor with screws. the magnet is always in place, so it must be related with the code. im working on a 6dof robot arm and this phenomenon is happening in all of the joints. first, the motor follows the order properly, but then, it starts spinning in the same direction with constant speed

Hi @Federico_Lopez , welcome to SimpleFOC!

How much time passes before this happens? Is it a long time?

It depends, between 4 and 10 mins aprox