initFOC failing while using find_sensor_offset_and_direction

Hello!

I am using a NUCLEO-64 STM32F446RE board, SimpleFOC Mini, a Hacker X-BL 52S motor and a MA782 magnetic sensor. I’ve been trying to implement position control, but the SimpleFOCDebug is telling me that InitFOC is failing. Additionally, I’ve been able to get sensor.getAngle() working but motor.shaft_angle is always 0.00. I have the SimpleFOC Mini hooked up to a power supply set at 11 V / 1.5 A. I’ve been able to get the motor working and turning normally with this setup in open loop, so I’ve just been at a loss for why transitioning to closed-loop with initFOC() has been so difficult. Here is the code I’ve been using:

/**
 * Simple example intended to help users find the zero offset and natural direction of the sensor. 
 * 
 * These values can further be used to avoid motor and sensor alignment procedure. 
 * To use these values add them to the code:");
 *    motor.sensor_direction=Direction::CW; // or Direction::CCW
 *    motor.zero_electric_angle=1.2345;     // use the real value!
 * 
 * This will only work for abosolute value sensors - magnetic sensors. 
 * Bypassing the alignment procedure is not possible for the encoders and for the current implementation of the Hall sensors. 
 * library version 1.4.2.
 * 
 */
#include <SimpleFOC.h>
MagneticSensorSPIConfig_s ma782config = {
  .spi_mode=SPI_MODE0, 
  .clock_speed=100000, 
  .bit_resolution=12, 
  .angle_register=0x0000, 
  .data_start_bit=15,
  .command_rw_bit=0, // not required
  .command_parity_bit=16
}; 
// magnetic sensor instance - SPI
//MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
// magnetic sensor instance - I2C
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0X0C, 4);
// magnetic sensor instance - analog output
MagneticSensorSPI sensor = MagneticSensorSPI(ma782config, PB6); // reference ma730 config setup in the github

// BLDC motor instance
BLDCMotor motor = BLDCMotor(7);
BLDCDriver3PWM driver = BLDCDriver3PWM(PA15, PB3, PB10, PC13);
// Stepper motor instance
//StepperMotor motor = StepperMotor(50);
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6,  8);

void setup() {

  // use monitoring with serial 
  Serial.begin(115200);
  // enable more verbose output for debugging
  // comment out if not needed
  SimpleFOCDebug::enable(&Serial);
  
  // power supply voltage
  driver.voltage_power_supply = 12;
  driver.init();
  motor.linkDriver(&driver);

  // initialise magnetic sensor hardware
  sensor.init();
  // link the motor to the sensor
  motor.linkSensor(&sensor);

  // aligning voltage 
  motor.voltage_sensor_align = 1;
  
  // set motion control loop to be used
  motor.controller = MotionControlType::torque;

  // force direction search - because default is CW
  motor.sensor_direction = Direction::UNKNOWN;

  // initialize motor
  motor.init();
  // align sensor and start FOC
  motor.initFOC();

  
  Serial.println("Sensor zero offset is:");
  Serial.println(motor.zero_electric_angle, 4);
  Serial.println("Sensor natural direction is: ");
  Serial.println(motor.sensor_direction == Direction::CW ? "Direction::CW" : "Direction::CCW");

  Serial.println("To use these values add them to the code:");
  Serial.print("   motor.sensor_direction=");
  Serial.print(motor.sensor_direction == Direction::CW ? "Direction::CW" : "Direction::CCW");
  Serial.println(";");
  Serial.print("   motor.zero_electric_angle=");
  Serial.print(motor.zero_electric_angle, 4);
  Serial.println(";");

  _delay(1000);
  Serial.println("If motor is not moving the alignment procedure was not successfull!!");
}


void loop() {
  // Motion control function
  motor.move(2);
    
  // main FOC algorithm function
  motor.loopFOC();


}


Hi @zoochini , welcome to SimpleFOC!

Did you get it to work in the meantime?

With the 0.6Ohm motor please stick to very low voltage limits… 1V alignment voltage sounds enough.

You could try open loop mode, with a low voltage limit of 0.25 or 0.5 V and see if this works. If open loop is working and closed loop is not, then it is likely to be a sensor problem.

Could you share the debug output from the serial console?