SimpleFOC Tuning help (Velocity OpenLoop working, sensor working, but closed loop control not)

Hi there,

I’m trying to tune the parameters for controlling a small (35cm diameter ~100kv) gimbal motor.

Open loop velocity works. Sensor (PWM) is noisy but works and gives reasonable values.

But Closed loop controls are very weird.

I’m very new to simpleFOC / FOC and electronics in general. So any help / tip would be appreciated.

Velocity OpenLoop (Working) :
Angle Closed Loop (Not Working :frowning: ) :
Torque Closed Loop (Not working :cry: ) :

Thanks so much in advance!

Edit: Just noticed that none of the parameters are actually visible in the video due to poor quality recording.

Essentially for torque control I’ve set the PID parameters to all 0, (I think in the video the I gain is actually 3 but I’ve tried many combinations including the default).

For the other PID values I’ve also tried many combinations. Starting with setting all to 0 (which I would expect that angle control etc will just not do anything? But the large oscillation still exists)

I’ve tried to eliminate the error being the weird new UI I’m using by writing the arduino code:

Problem is still the same though.

void setup_torque_test(){
  // initialize encoder sensor hardware
    // link the motor to the sensor

  // driver config
  // power supply voltage [V]
  driver.voltage_power_supply = 10;
  // link 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 = 0.5;   // [Amps] - if phase resistance defined
  motor.PID_current_d.P = 0;
  motor.PID_current_d.I = 0;
  motor.PID_current_d.D = 0;

  motor.PID_current_q.P = 0;
  motor.PID_current_q.I = 0;
  motor.PID_current_q.D = 0;
  // use monitoring with serial 
  // comment out if not needed

  // initialize motor
  // align sensor and start FOC

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

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

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


Torque-voltage is the right closed loop mode to test at first. It does not need any PIDs to be tuned.

The PID_current is used only when using current sensing and current control so you don’t need it for voltage control and setting it to zero doesn’t matter.

For torque-voltage mode the only thing that is important is that the sensor is working well, and the pole pairs are set correctly on the motor.

Since I see in the video that open loop mode is working well with your nice web-UI :-), it means the general setup with the pins for motor PWM is working.

Using PWM mode on the sensor is very much not recommended. You can get it working in theory, but especially if you’re less experienced with simplefoc I can’t recommend using PWM sensors. Not only are they very slow, which makes it hard to establish effective control, but usually they are also quite inaccurate and noisy, and often exhibit some discontinuity around the zero.
Use I2C if its a AS5600, or use ABZ or SPI sensors which will work much better!

Thank you!

I think for a start my pole pair is probably not correct.

I’ll try using not PWM with my sensor tomorrow.
Just checked the actual sensor I’m using is AS5048A which I think supports SPI?

Yes, it does, that will be a great sensor to use. It’s well tested and works well, but you do have to use the SPI mode… it will be plenty fast for the gimbal motor in SPI. The only downside is that SPI needs 4 pins while PWM only needed one…

So it seems to be an issue with sensor.

If I run torque control but with the sensor away from the motor it does not do crazy oscillations.
Checked that the pole pair setting is correct.

I think the issue with interference?

Here is a video of the motor being torque controlled, but the sensor is sensing a different motor’s rotation.

@runger Thanks again.

Yes, it could be interference, either magnetic or electrical…

Let me ask this: how confident are you that the magnet is of the correct type? Cheap offers on AliExpress have sometimes come with the wrong kind of magnet in the past… it has to be a high strength diametrically magnetised magnet. If the magnet is axially magnetised, or too weak, then it can work at slow speeds or when turning the motor by hand, but completely fail when the motor is turning itself…

For interference, question #1 is if your I2C lines have pull-ups on them, and the sensor’s DIR pin is tied either to ground or VDD, but not left floating.
If this is the case, the next thing to check is the cables and their connections, and shorten and neaten them if possible. Keep them well away from the motor phase wires…