Stm32l476rg velocity control example + GM6208

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
  // hardware interrupt enable
  encoder.enableInterrupts(doA, doB);

  // power supply voltage
  // default 12V
  driver.voltage_power_supply = 12;
  // link the motor to the 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

  // initialize motor
  // align encoder and start FOC

  // add target command T
  command.add('T', onTarget, "target velocity");

  // monitoring port
  Serial.println("Motor ready.");
  Serial.println("Set the target velocity using serial terminal:");

void loop() {
  // iterative foc function 

  // iterative function setting and calculating the velocity loop
  // this function can be run at much lower frequency than loopFOC function

  // user communication;

It looks like you didn’t send the command. It has to be capital T followed by velocity value - ie T1.

I did send the command but I didn’t receive any indication that the target has changed. I tried using the Position Control example and send the command. I did receive a reply indicatiing that the command is sent. But the motor itself doesn’t move after I’ve set the target position.

Welcome to SimpleFOC, @mike_a,

Did you solve your problem in the end?

Which IDE are you using, Arduino or PlatformIO? Which driver are you using? Is it the SimpleFOC shield?

You code looks fine at first glance, could you tell us a little more about your electrical setup?

I have not. I am currently using Arduino IDE with the SimpleFOCShield v2. STM32duino and SimpleFOCLibrary have been installed.
My current setup is a rotary inverted pendulum (or reaction wheel pendulum):

  • STM32-L476RG
  • iPower Motor GM6208 150T
  • SimpleFOCShield v2
  • AMT103-v (PB4, PB5, 2048) - 2048PPR
  • Pin assigment (PA8, PA9, PA10, PA11)

The whole setup is currently powered with 12V and 0.5A and it can be adjusted. I am currently still trying to move the motor before going further and adding another encoder.

I also got this notice when I tried running the PositionControl example.

MOT: Align sensor.
MOT: Failed to notice movement
MOT: Init FOC failed.

I have also tried using Arduino Uno R3, and it works for the Position Control.