Need help with FOC control of GM3506, using AS5600, SimpleFOCmini V1.0 and STM32G474RE

Hello, I am quite new to BLDC control. I have some kind of problem when trying to set up position control on a GM3506 BLDC motor. I have a 12V supply to the driver, I use SimpleFOC Mini V1.0,AS5600 Magnetic encoder, STM32 Nucleo G474RE board and GM3506 bldc motor.

I have tested all of the components separately; however, when I try to do position control, after the initialisation, the motor gets stuck at random positions, sometimes it tries to move when I set a new target, but in general it does not move at all and does not follow the target. The encoder seems to give correct values. I was able to run Open Loop control, and I also found poles using a simpleFOC script, which matched the documentation.

The code I use for Position control:

#include <Wire.h>

#include <SimpleFOC.h>

BLDCMotor motor = BLDCMotor(11);

BLDCDriver3PWM driver = BLDCDriver3PWM(PA8, PA9, PA10, PA6);

TwoWire myWire(PB9, PB8);   // AS56000 sensor

MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);

Commander command = Commander(Serial);

void onAngleA(char\* cmd) { command.scalar(&motor.target, cmd); }

void setup() {

Serial.begin(115200);

while(!Serial);

myWire.begin();

Wire.setClock(400000);

sensor.init(&myWire);

motor.linkSensor(&sensor);

driver.voltage_power_supply = 12;

driver.init();

driver.enable();

motor.linkDriver(&driver);

motor.controller = MotionControlType::angle;

motor.PID_velocity.P = 0.5;

motor.PID_velocity.I = 5;

motor.LPF_velocity.Tf = 0.02;

motor.P_angle.P = 20;

motor.velocity_limit = 4;

motor.voltage_limit = 10;

motor.voltage_sensor_align = 2;

motor.init();

motor.initFOC();

motor.target = sensor.getAngle();

command.add(‘A’, onAngleA, “set motor target \[rad\]”);

Serial.println(“Motor ready. Use command ‘A’ to set a new target angle.”);

}

void loop() {

sensor.update();

motor.loopFOC();

motor.move();

command.run();

static unsigned long t = 0;

if (millis() - t > 200) {

t = millis();

float currentAngle = sensor.getAngle();

Serial.print("Angle: ");

Serial.print(currentAngle, 3);

Serial.print("  Target: ");

Serial.println(motor.target, 3);

}