SimpleFOC v2 - understanding sensor align

Hey Nat,

The initFOC function has three main functions:

  1. Align sensor direction with motor direction
    • turn the motor one electrical revolution and save this direction as the good one (fails initFOC if the sensor does not move)
    • check if the pole pairs number is right when you are there (does’t fail if initFOC if it does but it tells you the estimated value)
    • You can skip it by setting motor.sensor_direction before initFOC
  2. Align position sensor angle with the motor electrical angle
    • Set voltage in direction -90 degree electrically and let the motor settle
    • Find the sensor angle on which motor electrical zero angle is zero (the position of the sensor when the motor settled) and store it
    • You can skip it by setting motor.zero_electrical_angle before initFOC
  3. Align the current sensing to the motor direction (Only if using current sensing)
    • This part of the code will put voltage on the phases A and B of the motor and see if the currents align with those voltages. In general if it puts a voltage to phase A then the current sense A channel should measure current with twice magnitude than the B and C.
    • This part of the code can invert gains of the particular current sense channel if necessary and it can switch pinouts of the current sense channels A and B for example. And it will let you know what did it do if you use monitoring : see in docs
    • And finally it will fail if it could not figure out what to do to align the current sense with the motor phases.
    • You can skip it by setting current_sense.skip_align = true before initFOC

The reason why current alignment sometimes returns different value is because of the noise of current sensing. It sets the voltage to the motor phases and in then measures the currents. It will only pass the alignment if the measured current ratio is correct.

The procedure goes as follows:
For example, current sense alignment procedure puts motor.voltage_sensor_align voltage to the motor phase A and the phases B and C to 0. It should be measuring Ca = voltage_sensor_align/(1.5*phase_resistance) Amps on current sense channel A and exactly half of it Ca/2 on the other channels.

  • If you measure Ca on A and 2*Ca on B, the procedure will switch the A and B current sense channels
  • If you measure -Ca instead of Ca the procedure will invert the gain of the phase A
  • If you measure Ca on A ~Ca on B then it will will fail the alignment because it could not deduce what to do to make it aligned.

So for the motors with lower resistance the current measurement noise is higher and it sometimes triggers misbehavior. To avoid this from happening there are really three things you can do:

  • Use the highest reasonable value of the motor.voltage_sensor_align to magnify the current magnitude to be measure and minimize the noise influence
  • Go to the code and use longer period of time and increase filtering of current acquisition, I’ve fixed it to 100 points and averaged it with a simple exponential filter: see in the code
  • Don’t use this feature, just skip it. It does not do anything else but checks if your specified pinout and gains are well set. If they are it will not change anything. This check is here to prevent mistakes really nothing more :smiley:
2 Likes