Motor stops spinning for no reason

I have a BLDC motor, 1 pole pair, 12V rated voltage, 1024 pulses/ rev quadrature encoder.

Hardware: SimpleFOC board, V2.0.3, ESP32-WROOM-32 (dual core 240MHz), power supply: 12V DC

The power supply is very clean, there is an 5V LDO for the CPUs and there is no noise.

I don’t use current sense. There is no mechanical load on the motor, just planetary gear. The motor is free to move. I tested it (in open loop velocity mode)

In velocity open loop mode, the motor runs with no problems. I tested with up to 100 rad/sec in both directions.

I checked the encoder, and the signals are very clean and stable. I also wrote a program in open loop velocity mode that counts the encoder pulses for a 30 second movement back and forth in constant speed (100 rad/sec). I got 3000 pulses (±20 pulses) in both directions, which means the encoder counts successfully.

The problem is when I switch to velocity closed loop. If I issue a 20 rad/sec command (M20), the moves, it looks stable, and after a couple of minutes, the velocity start to look jittery and the motor stops: these graphs show the speed at the beginning of the movement at speed 20:

And after several minutes the motor stops, I can’t move the motor anymore. I even tried to disable and enable the motor, but it doesn’t help – no movement command is possible at all. Only resetting the board solves it and the motor can move again.
Here is the picture when the motor stops:

After I reset the board, I try to run at speed of 50 rad/sec (M50): It moves for 20 seconds at stops at position 716. If I reset the board and I try again, now the motor stops at 920. So the final position is random.

Again, when it stops, it is not possible to move the motor. Only the board reset can cancel this error.
If I move the motor back and forth at speed 70 rad/sec for 1-2 seconds in each direction, then there is no problem and it looks good. If I keep it to spin at the same direction for more than 3 seconds at this speed – again the motor stops: (here I spin the motor between +70 and -70):
It seems there is a problem in an overflow of the pulses that cancels when I switch direction.

I also tried the code on Nucleo G431RB (STM32G431RB it is a 180MHz CPU) and the results is the same
What I tried: change all the gains, nothing helped. Change the modulation type, the PWM frequency, the pins, (tried different pinouts), I tried to put motor.loopFOC()line to work only each X microseconds,also motor.move(); I tried the same, Nothing works.(only makes it worse)
here is the code:

#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <SimpleFOC.h>
#include <EEPROM.h>
#include <esp_task_wdt.h>
asm(".global _printf_float");
#define RXD2 16
#define TXD2 17
BLDCMotor motor = BLDCMotor(1);
BLDCDriver3PWM driver = BLDCDriver3PWM(12, 13, 14, 21);
Encoder encoder = Encoder(25, 32, 1024);
long timestamp;
void doA() { encoder.handleA(); }
void doB() { encoder.handleB(); }
Commander command = Commander(Serial2);
void doMotor(char *cmd) { command.motor(&motor, cmd); }
void resetBoard(char *cmd)
{
ESP.restart();
}
void setup()
{
Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
encoder.init();
encoder.enableInterrupts(doA, doB);
motor.linkSensor(&encoder);
driver.voltage_power_supply = 12;
driver.init();
motor.linkDriver(&driver);
motor.voltage_sensor_align = 1;
motor.velocity_index_search = 1;
motor.controller = MotionControlType::velocity;
driver.pwm_frequency = 16000;
motor.PID_velocity.P = 0.03f;
motor.PID_velocity.I = 0.3;
motor.PID_velocity.D = 0;
motor.voltage_limit = 3;
driver.voltage_limit = 3;
motor.PID_velocity.output_ramp = 1000;
motor.LPF_velocity.Tf = 0.02f;
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
motor.velocity_limit = 200;
motor.useMonitoring(Serial2);
motor.init();
motor.initFOC();
command.add(‘M’, doMotor, “my motor”);
command.add(‘X’, resetBoard, “resetBoard”);
Serial2.println(F(“Motor ready.”));
Serial2.println(F(“Set the target angle using serial terminal:”));
_delay(1000);
motor.enable();
}

void loop()
{
motor.loopFOC();
motor.move();
if ((micros() - timestamp) > 200)
{
timestamp = micros();
motor.monitor();
}
command.run();
}

Thats a weird error !

Maybe a ram leak 🫣

I really don’t know, this is very frustrating

Update: with Teensy 4.0 (600MHz NXP IMX1062 CPU)
THE BUG DOESN’T HAPPEN!
Everything works great.
I don’t know why it is.

1 Like