Low side current sensing for stepper motors

Let’s figure out what’s wrong with the current SimpleFOC low side current sensing implementation for the stepper motors. There are two issues:

  1. The sign of the measured currents is not always correct
  2. Current sensors ADC sampling and PWM not synchronized correctly

Let’s look into these issues one by one

The sign of the measured currents is not always correct

Let’s look at an example. Let’s say, at some point of time, the current flows along the red path, top to bottom:

  1. The current flows through the motor coil top to bottom, let’s consider this direction to be positive for the motor coil, i.e.:
I_a^{coil} > 0
  1. The current flows through the current sensor top to bottom, let’s consider this direction to be positive for the current sensor, i.e.:
I_a^{sensor} > 0

Let’s say that after that the current starts flowing along the blue path, but the direction of current through the coil A is the same, i.e.:

I_a^{coil} > 0

It can be seen that the direction of current through the current sensor has changed because the active pair of transistors has changed ([Q1, Q4] → [Q2, Q3]):

I_a^{sensor} < 0

So it can be said that depending on which pair of transistors is active, either [Q1, Q4] or [Q2, Q3]:

  1. The current flowing through the motor coil is the same as the current measured by the current sensor
  2. The current flowing through the motor coil coincides in absolute value with the current measured by the current sensor, but their signs are opposite

Fortunately, there is a simple formula that relates the current flowing through the motor coil and the current seen by the current sensor:

I_a^{coil} = sign(V_a) * I_a^{sensor}

The problem is that this formula is not currently accounted for in SimpleFOC, instead this one is used:

I_a^{coil} = I_a^{sensor}

The PR fixing this issue is here

To be continued…