Hi there,
I am trying to apply the Velocity Control example code with STM32L476RG. I am new to this so what I’ve done is that I’ve only changed the pins, to adapt the STM32L476RG pins. The motor used in my setup is GM6208. My goal right now is to spin the motor in one direction and perhaps control the speed and position later on. I tried to flash it into the microcontroller and control it using the SimpleFOCStudio. But when I set the target velocity, nothing happened.
Did I do something wrong?
#include <SimpleFOC.h>
// define BLDC motor
BLDCMotor motor = BLDCMotor( 11 );
BLDCDriver3PWM driver = BLDCDriver3PWM(PA8, PA9, PA10, PA11);
// define Encoder
Encoder encoder = Encoder(PB4, PB5, 600);
// channel A and B callbacks
void doA(){encoder.handleA();}
void doB(){encoder.handleB();}
// velocity set point variable
float target_velocity = 0;
// commander interface
Commander command = Commander(Serial);
void onTarget(char* cmd){ command.scalar(&target_velocity, cmd); }
void setup() {
// initialize encoder hardware
encoder.init();
// hardware interrupt enable
encoder.enableInterrupts(doA, doB);
// power supply voltage
// default 12V
driver.voltage_power_supply = 12;
driver.init();
// link the motor to the driver
motor.linkDriver(&driver);
// set control loop type to be used
motor.controller = MotionControlType::velocity;
// velocity PI controller parameters
// default P=0.5 I = 10
motor.PID_velocity.P = 0.2;
motor.PID_velocity.I = 20;
//default voltage_power_supply
motor.voltage_limit = 6;
// velocity low pass filtering
// default 5ms - try different values to see what is the best.
// the lower the less filtered
motor.LPF_velocity.Tf = 0.01;
// link the motor to the sensor
motor.linkSensor(&encoder);
// initialize motor
motor.init();
// align encoder and start FOC
motor.initFOC();
// add target command T
command.add('T', onTarget, "target velocity");
// monitoring port
Serial.begin(115200);
Serial.println("Motor ready.");
Serial.println("Set the target velocity using serial terminal:");
_delay(1000);
}
void loop() {
// iterative foc function
motor.loopFOC();
// iterative function setting and calculating the velocity loop
// this function can be run at much lower frequency than loopFOC function
motor.move(target_velocity);
// user communication
command.run();
}