FOC Calibration Not Running

Hi all!

I have been having a heck of a time getting my setup to work (see tags for devices). I am doing all my programming in PlatformIO within Visual Studio Code.

I have been able to get the angle off of my AS5048B (the I2C one) following step 1 here. However the velocity is a bit noisy as it bounces between around -1.5 and 1.5 when I don’t spin the shaft. The angle stays constant to the second decimal place.

Step 2 (open loop velocity) works as well, but when I set the motor on my wooden desk, it makes a fast tapping sound as it spins (I assume this is normal for stepper motors is this mode?)

Step 3 ( Closed-loop control - torque using voltage) is where problems really start. I hear the motor click about 2 times after flashing the ESP32 with the code bellow. All that changes when I change my target is the current drawn from my power supply, but the motor does not spin! It stays completely silent and cant be spun by hand. I did notice that it looks like it takes a step when I switch my target from +3 to -3 in a direction in accordance with the sign.

#include <Arduino.h>
#include <SimpleFOC.h>

// Stepper motor instance
StepperMotor motor = StepperMotor(50, 1.5, 20.6);

// Stepper driver instance
StepperDriver4PWM driver = StepperDriver4PWM(33, 32, 25, 26, 27, 14);

// Encoder instance
MagneticSensorI2C encoder = MagneticSensorI2C(AS5048_I2C);

// instantiate the commander
Commander command = Commander(Serial);
void doTarget(char* cmd) { command.scalar(&motor.target, cmd); }

void setup() { 
  
  // initialize encoder sensor hardware
  encoder.init();
  // link the motor to the sensor
  motor.linkSensor(&encoder);

  // driver config
  // power supply voltage [V]
  driver.voltage_power_supply = 12;
  driver.init();
  // link driver
  motor.linkDriver(&driver);

  // aligning voltage
  motor.voltage_sensor_align = 5;

  // set motion control loop to be used
  motor.torque_controller = TorqueControlType::voltage;
  motor.controller = MotionControlType::torque;

  // add current limit
  // motor.phase_resistance = 3.52 // [Ohm]
  // motor.current_limit = 2;   // [Amps] - if phase resistance defined

  // use monitoring with serial 
  Serial.begin(115200);
  // comment out if not needed
  motor.useMonitoring(Serial);

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

  // set the initial motor target
  // motor.target = 0.2; // Amps - if phase resistance defined  
  motor.target = 2; // Volts 

  // add target command T
  // command.add('T', doTarget, "target current"); // - if phase resistance defined
  command.add('T', doTarget, "target voltage");

  Serial.println(F("Motor ready."));
  Serial.println(F("Set the target using serial terminal:"));
  _delay(1000);
}

void loop() {
  // main FOC algorithm function
  motor.loopFOC();

  // Motion control function
  motor.move();

  // user communication
  command.run();
}

This is the beginning of the serial port after flashing followed by some of the targets I set:

MOT: Enable driver.
MOT: Align sensor.
MOT: Skip dir calib.
MOT: Zero elec. angle: 0.00
MOT: Ready.
Motor ready.
Set the target using serial terminal:
T1
1.000
T3
3.000
T-3
-3.000
T3
3.000
T-3
-3.000

Is it possible to get a perfect 0 electric angle? I figured the majority of my problems are coming from an FOC calibration that isn’t being run for some reason.

Any and all help would be appreciated :slight_smile:

Are you using the simpleFOC lib provided via PIO?
There is a bug in main branch for stepper motors now, they don’t initialize correctly. You can use the dev branch (download from github and put in /lib folder of project, remove the simplefoc dependency from platformio.ini)

1 Like

I am indeed! I gave that a shot and it worked! This is the new output from serial monitor:

MOT: sensor_direction==CW
MOT: PP check: OK!
MOT: Zero elec. angle: 2.64
MOT: Ready.
Motor ready.
Set the target using serial terminal:
t1
T1
1.000
T2
2.000
T3
3.000
T4
4.000
T0
0.000
T-1
-1.000
T-2
-2.000

The motor does seem to run a little loud compared to the demo I saw of a backdrivable stepper here

Do you think that’s due to improper tuning and PID values, or are there additional things being worked in the dev branch to improve stepper performance when the main branch got bugged?

If you have not spent some time tuning the PIDs and LPF, then it is definately worth doing so.

No nothing else that I know of.

But my guess is that you have a bandwidth problem - with a motor of 50PP and an AS5048B sensor you probably can run the loop fast enough to drive the motor efficiently. This can also manifest as noise.