STM32 - open loop test - Driver init failed

Hello everybody! I am developing my own bldc motor controller. Sadly i’ve ran into problems with my microcontroller. I am trying to program an STM32L432kc nucleo32 microcontroller using Arduino IDE. There are 2 major problems.

  1. The code uploads properly but after the code uploads i get a “Driver init failed” information on serial monitor. There is also no response to any commander commands that i am trying to send.
  2. When uploading code, Arduino IDE shows wrong numbers of avialiable Flash memory and SRAM on the microcontroller (i am talking max values, not the memory i have avialiable after the upload). Arduino IDE shows i have 65kB of flash memory avialiable, even though i should have 256kB on this particular model. Same situation with SRAM, it shows 41kB avialiable and i should have 64kB.

I cant find any problem with the code. I tested the code with a standard Arduino Nano (with a different pin config ofc) and it was working flawlessly. In the beggining i wanted to use a basic Nano for the whole project but i ran into problems with storage space, when uploading any code more complicated than the open loop velocity control. So i choose to use the stm32 instead.

I dont know if this problem can even be solved, or if a have to get a different microcontroller.
If someone has any idea what might be the problem it would be a life saver.

My code:

// Open loop motor control example
#include <SimpleFOC.h>

// BLDC motor & driver instance
// BLDCMotor motor = BLDCMotor(pole pair number, phase resistance (optional) );
BLDCMotor motor = BLDCMotor(15);
//  BLDCDriver6PWM( int phA_h, int phA_l, int phB_h, int phB_l, int phC_h, int phC_l, int en)

BLDCDriver6PWM driver = BLDCDriver6PWM(PA8, PA7, PA9, PB0, PA10, PB1);
//BLDCDriver6PWM driver = BLDCDriver6PWM(11, 3, 10, 9, 6, 5);
// instantiate the commander
Commander commander = Commander(Serial);
void onTarget(char* cmd) { commander.target(&motor, cmd);}
void onMotor(char* cmd)  { commander.motor(&motor,cmd); }

void setup() {

  // driver config
  // power supply voltage [V]
  driver.voltage_power_supply =14;
  driver.init();
  
  driver.pwm_frequency = 20000;
 
  // link the motor and the driver
  motor.linkDriver(&driver);

  // limiting motor movements
  //motor.phase_resistance = 1.6; // [Ohm]
  //motor.current_limit = 4;   // [Amps] - if phase resistance defined
  
  motor.voltage_limit = 12;  // [V] - if phase resistance not defined
  motor.velocity_limit = 50;  // [rad/s] cca 50rpm

  // open loop control config
  motor.controller = MotionControlType::velocity_openloop;

  // init motor hardware
  motor.init();

  // add target command T
  commander.add('T', onTarget, "target velocity");
  commander.add('M',onMotor,"full motor config");

  Serial.begin(2000000);
   
  Serial.print("Driver init "); // init driver
      if (driver.init())  Serial.println("success!");
        else{
        Serial.println("failed!");
        return;}

  Serial.println("Motor ready!");
  Serial.println("Set target velocity [rad/s]");
  _delay(1000);

}
void loop() {

  // open loop velocity movement
  // using motor.voltage_limit and motor.velocity_limit
  motor.move();

  // user communication
  commander.run();
}

And a screenshot of Arduino IDE:

Wrong.

You are calling driver.init() twice here.

Load this:

#include <SimpleFOC.h>
BLDCMotor motor = BLDCMotor(1);
BLDCDriver6PWM driver = BLDCDriver6PWM(PA8, PA7, PA9, PB0, PA10, PB1);
void setup() {
  driver.voltage_power_supply = 12;
  driver.pwm_frequency = 20000;
  driver.init();
  motor.linkDriver(&driver);
  motor.voltage_limit = 2;   // [V]
  motor.velocity_limit = 10; // [rad/s]
  motor.controller = MotionControlType::velocity_openloop;
  motor.init();
  _delay(1000); }
void loop() {motor.move(2);}

and after you make it work start building your code from there.

For open loop the poles don’t matter.

Let me know how it goes.

Also, I very strongly recommend you look into this:

Cheers,
Valentine

Hi @Valentine thank you very much for the response

I dont really understand why this way of checking if the driver is working is wrong, i copied the code almost directly from the web site

Also
I upploaded the test program you send me, and when the motor.move() function has a predeclared value (motor.move(2)), the program uploads correctly and the stm32 starts sending pwm signals on the declared pins.
But i cant make the commander interface work at all. Even with a simple function of setting speed. The code uploads but there is no response to the commands that i send, the output of the declared pwm pins is constant. Im trying to do it like in the code below:

#include <SimpleFOC.h>
BLDCMotor motor = BLDCMotor(1);
BLDCDriver6PWM driver = BLDCDriver6PWM(PA8, PA7, PA9, PB0, PA10, PB1);

Commander commander = Commander(Serial);
void onTarget(char* cmd) { commander.target(&motor, cmd);}


void setup() 
{
  driver.voltage_power_supply = 12;
  driver.pwm_frequency = 20000;
  driver.init();
  motor.linkDriver(&driver);
  motor.voltage_limit = 12;   // [V]
  motor.velocity_limit = 10; // [rad/s]
  motor.controller = MotionControlType::velocity_openloop;
  motor.init();

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

  Serial.begin(115200);

  _delay(1000); 

}


void loop() 
{
  commander.run();
  motor.move(); 
 
}

No idea.

That’s awesome. You can make 2 as a varible.

You need to make sure you call the correct Serial interface. The Serial you call may not sit on the Serial you send/receive from. Please check the Serial for that MCU. There is some discrepancy in the hardware serial on the board you use and the Serial you call?

Cheers,
Valentine