@runger
This is the code that’s currently running on the STM
#include <SimpleFOC.h>
#define LED PC13 // LED pin
// BLDC motor & driver instance
BLDCMotor motor = BLDCMotor(15);
BLDCDriver3PWM driver = BLDCDriver3PWM(PA10,PA9,PA8, PB1,PB14,PB13);
// hall sensor instance
HallSensor sensor = HallSensor(PA_6, PA_7, PB_0, 15);
void doA(){sensor.handleA();}
void doB(){sensor.handleB();}
void doC(){sensor.handleC();}
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
digitalWrite(LED, 1); //off
// initialize encoder sensor hardware
sensor.init();
sensor.enableInterrupts(doA, doB, doC);
motor.linkSensor(&sensor); // link the motor to the sensor
driver.voltage_power_supply = 24;
driver.voltage_limit = 24;
driver.pwm_frequency = 10000;
// driver config
digitalWrite(LED, 0); //on
driver.init();
motor.linkDriver(&driver); // link driver
// set motion control loop to be used
motor.controller = MotionControlType::velocity;
motor.torque_controller = TorqueControlType::voltage;
motor.PID_velocity.P = 0.2f;
motor.PID_velocity.I = 5;
motor.PID_velocity.D = 0;
motor.init();
// align sensor and start FOC
motor.initFOC(0, Direction::CW);
digitalWrite(LED, 1); //off
}
float speed;
unsigned long currMills;
void loop() {
motor.loopFOC();// FOC algorithm function
// Get a random value every 1 second
if(millis() - currMills > 1000){
currMills = millis();
speed = random(-10, 10);
}
motor.target = speed; // Set target velocity
motor.move();
}
And it happens only when the motor is changing the direction.
When using `speed = random(2, 10);` the motor doesn't stop. And I didn't notice any current spikes or increases in current consumption. I am also not using the current sense in the code currently.