SimpleFOC Theory: timing

Timing is most important for real time systems. Is there any documentation how the PWM-cycle is synchronized with the filters and the control algorithm? Are the encoder values interpolated at the sampling time points? Is the ADC synchronized with the PWM cycle?

Hey @ChrisMicro,

Indeed, timing is very important. As one of the main goals of the simplefoc is to be cross-platform, and given the constraints of many microcontroller architectures, we have implemented an adaptive timing scheme.

This basically means that we do not fix the sampling time or the frequency. Each time our time sensitive code components are called (PID controllers, low pass filters, numerical derivations … ) we measure the time from the last call and use this value in our calculations.
By measuring this varying sample time, we can discretize our equations (PID, low pass filter equation,
velocity calculation) in every execution and make sure that even if you two different mcus with completely different cpu frequencies they can still have the same PID and low pass filter parameters.

For now we are asking the users to call the motion control (motor.move()) and the foc routine (motor.loopFOC()) as much as possible in their arduino loop. Usually the code will have only two of those functions in the arduino loop. This in most casses ends up with very constant sampling time, however if one is using a monitor then things can get a bit tricky, as the monitor will be writing on the serial port and that takes some time. But as the time from the last call is calculated the code does adapt to this as well. However, as you’ve pointed our the more the sampling time is constant the better the performance.

Another thing to note is that if the user sets up his code in a way that the code has a constant execution frequency the code will work as what you’d expect from a fixed frequency control loop, the only difference will be that the simplefoc will measure the sample time itself.
To do that you’d only need to put your motor.move() and motor.loopFOC() in the interrupt based timer callbalcks.
As this is very architecture and microcontroller spacific we are leaving this for the users to implement if they need it.

The PID docs and low pass filter docs are here. The sampling time is something that is measured, and the forms of equations used are quiet standard. The LPF and PIDs are not synched with the PWM cycles. Once the the new pwm duty cycle command is calcualted it will be applied from the next PWM cycyle.

No, there is no interpolation used in the simplefoc at the moment. We are calculating them once per sampling period, typically at the beginning and considering them constant during the execution of the motion control and the foc routines.

The ADC is synched with the middle of the center aligned PWM cycle only if using the low-side current sensing (LowsideCurrentSense). If using inline current sensing (InlineCurrentSense) we are not synching the ADC and the PWM.
But you’re free to use Low-side current sensing code (LowsideCurrentSense) with your inline current sensing hardware. Just make sure that simple foc supports low-side current sense for your mcu architecture.

Here are the docs about the current sensig implemented in simplefoc, there you can also see what techiques are implemented for which architectures current sense docs.


Thank you for the extensive explanation :slight_smile:
The decision to make the loop sampling time independent is interesting. I also designed some real time system with the Arduino environment to run on many MCU architectures (audio). The design decision there is always to have constant frequency because non constant sampling frequency leads to much higher computation effort when implementing more complex DSP. Especially if you want to run it on 8Bit MCUs every additional multiplication increases the load dramatically.