Trouble with closed loop

Hi folks,

So i’ve been working on a new setup using the following:

  • MCU: Arduino Nano
  • Motor: Flycat 5010 (I believe has 6 pole pairs, but seems to work best in open loop if I enter 22?)
  • Driver: L298N
  • Magnetic Sensor: AS5600

I can run this thing in open loop no problem, accelerating and decelerating. I can also get a reading from the AS5600 using the tutorial here: Magnetic sensor I2C | Arduino-FOC so I know the driver, motor and sensor can all handle it.

I was getting some really jittery results with the magnetic sensor mounted directly on the motor shaft when I tried closed loop control though - like the motor kept overshooting and trying to correct, and it would just shake itself to death. I am planning a cycloidal gear reduction and was thinking the solution might be to have the sensor read the output after the reduction to avoid jitteriness.

runs fine in open loop: video here

But, when I go to use closed loop, I get some strange results. After calibration, the motor just stays-put.: video here - sorry for the poor quality video. The readout is motor.monitor() and you can see it has a target angle of 20 in the first column, and the read-out of the current angle in the last column - but it won’t budge.

2 things worth mentioning.

  1. I don’t get a confirmation from my serial monitor that my desired angle is sent after I type in something like “12.5” and click send, but also, even if I set a desired angle in the code as I did in the video (e.g. 20 rads), it still stays put.

  2. I’m thinking due to having the encoder after the reduction, when it runs through the setup, it gives an error when trying to determine the pole pairs - it ranges from 70 - 4000, so that’s obviously not correct information - (i’m thinking this is due to the play between the gears) - but how is that affecting things / how do I correct it?

  3. If I separate the sensor from the motor, something seems to happen - if I move the sensor, the motor moves. video here - I think this is more or less what it’s supposed to do? but when i try to command it (either through serial or through the code) to move to an angle - it won’t.

1 Like

Hello there.

  1. Arduino Nano is severely underpowered to perform a proper closed loop control.
  2. The driver L298N is a very old technology and has many limitations, although in your case this should not create a real problem.
  3. The magnetic sensor AS5600 is a really slow sensor, it’s a potentiometer and works at very slow speeds.

The reason you got jittery results is the underperforming Nano is trying to control the motor however the slow AS5600 is confusing it even more and the control loop never converges.

That’s never going to work. The closed loop algorithm is not designed for reduction gear. In fact it will increase the error, not decrease it. It’s not a SimpleFOC problem but intrinsic to that of a closed loop algorithm. In fact, placing the sensor on the motor shaft will increase the accuracy of the reduction gear angular position (that’s a homework for you to find out why).

The obvious solution to your problem, though not the one you would like:

  1. Get a fast MCU. Let’s say, a Blue Pill or better. The faster the better. Nucleo boards are also good, though may be too pricey and these days are hard to find due to the silicon shortage.
  2. Get a fast sensor (AS5047P is my choice).
  3. Mount the sensor on the shaft.
  4. Make sure your magnet is high quality, correctly positioned, and also you got the correct magnet (diametrical).
  5. Great setup by the way, looking at your skill level I don’t think you will have any problems making it work.


1 Like

Thanks a lot for the run-down Valentine! It makes sense what you’re saying.
First up, i’ll change back to having the magnet (I checked it is diametric) mounted on the shaft directly. I’ll order a Bluepill and try to get my hands on a faster sensor. In the meantime i’ll see if I can speed up the loop at all.



You can try to run the motor VERY slowly, like 1 rad/s, this is the same as running it fast with a fast MCU and sensor. You will also have to tune the PID accordingly, at least you will be able to test your setup until you get a fast MCU and sensor. Hopefully the Nano will be able to crunch the numbers. I hope…

As far as the pole pairs, just count the magnets and divide by 2. Your Flycat 5010 should have 14 magnets, ergo, 7 PP. I have the exact same motor, except mine is GARTT.