Motor init procedure questions

Some interesting questions arised recently in our disord server and I thought that it would be good to share the response with the community.

Hi, I was wondering about this piece of code to get the natural direction of the encoder. Is this the part during start-up that your motor moves a bit forward and backward? I have a few questions:

  • Why do you start here from the 3PI/2 (-90 degrees?).
  • The electrical angle ranges from 0-2PI, how does that relate to the physical position of the motor? I can imagine the number of pole pairs or windings have something to do with this?
  • FOC Algorithm | Arduino-FOC the code on this site is a bit out-dated


Second part is about finding the zero electrical angle:

  • I cannot grasp the 3PI/2 (-90 degrees) here and how it becomes 0. Where does the +90 degrees come from?
  • Why do you set the voltage to the Q axis here? I noticed in the cheetah docs (where i’m looking at the magnetic eccentricity calibration) they set the voltage to the D axis. I’m lacking some knowledge here.



Hi Antun, I posted these questions and I think we found the answer to one part of it. The electrical angle is indeed related to the amount of pole pairs. My motor has 11 pole pairs thus the sensor natural moves 2PI/11=0.57 rad=32.72 degrees. Which is seems to be right if I look at the behavior of my motor.

However, what is still not clear to me:

  • why do we need to start at the angle 3PI/2 (-90 deg) for this directional search.

Finding the electric angle is a bit more fuzzy to me:

  • What does physically happen if you only give it 3PI/2 electrical angle and not change it after that.
  • Why do you supply the current to the Q axis, what position of the rotor with respect to the stator do you ‘guarentee’ with this. This will generate a torque right? So will you push the actuator in a certain angle?
  • I also came across different strategies online to find this electrical angle, are there any drawbacks to this approach in SimpleFoc?

Hey @marethyu,

I’ve started to respon few times already and I’ve somehow got distracted every time :smiley:
I really wanted to repsond to this one well, because I really think it is important to many people.

Basic principles of the BLDC

Every BLDC motor has windings in its stator and permanent magnets in its rotor. Here is a quick graphic of a very simple 1 pole pair motor. This motor has three phases and each phase has one pair windings.
Untitled Diagram.drawio (33)

Once the current is passed through the winding coils the motor phases it induces a magnetic field in the windings with a polarity depending on the current polarity. In my figures I am going to simplify this effect and draw the coils in color red/blue - north/south as if they were magnets.

So whenever you apply certain current through the motor coils, they will generate the magntic field and the motor’s rotor will try to align with the coils magnetic filed. Here is an example of the motor behavior for two different initial positions by applying current in the phase a

From the graphic, and whats intuitive, you can see that regardless of the rotor’s intial position, when you set a certain magnetic field in the stator windings the motor will always try to align with that magnetic field, and in a case of this simple motor it will always some to the same position.

Definition of the field vectors

Now when talking about how to align different magnetic fields, what does it mean in practice and how do we characterise the force that actually makes the rotor move it is usual to introduce the field vectors. Basically the magnetic field vector by its length shows the strangth of the magnetic field and its direction will be determined going from the nort to south pole. It is very simple, here is a quick visualisation:

Now when we defined the vectors of the magnetic field based on the motor’s physics the force that turns the rotor of the motor depends of the strength of the two magnetic fields ( B_r - strength of rotor’s permanent magnets and B_s - strength of the field in the coils produced by the current I) and fields and the angle in between them

F ~ B_r*B_s*sin(theta)


F ~ B_r*I*sin(theta)

Basically this relationship means that the motor’s torque will be directly proportional to the two magnetic fields but also to the sine of the angle between them. And in particular as we cannot control the magnetic filed of the rotor’s magnets, we will have to control strength and orientation of the magnetic field vector of the stator windings (field oriented control).

Control of the field vector orientation (FOC)

From the force equation we can see that we can see that in order to use maximally our current and produce the maximal force we need to keep the angle in between the two vectors at 90 degrees, because sin(90) = 1. In that case the force pushing the rotor to move ( creating the motor’s torque) will be proportional only to the current I and B_r

F ~ B_r*I

Now to be able to keep the angle in between two magnetic fields fixed we first find the rotor’s angle in every time-step and then using this value we calculate what are the phase a,b and c currents necessary to produce a the magnetic field vector B_s that is exactly 90 degrees behind the rotor. In order to do that we use Park and Clarke transformations. We could go much deeper in this topic, if you’d be interested I’ll take some time to explain how we go from the vectors B_r and B_s and the angle theta to the d and q coordinate system.

But the main takeaway is the that the d axis is exactly aligned with the B_r and the q axis is 90 degrees from the B_r. If we take the force equation and apply the current I_d in d axis we get the magnetic field vectors B_s and B_r that are aligned

F ~B_r*I_d*sin(0) = 0

And in the q axis, we get the magnetic field vectors that have exactly 90 degree offset:

F~B_r*I_q*sin(90) = B_r*I_q

Which means if the d axis and q axis are defined in this way that any current applied in the d axis will not produce any force to move the rotor or in other words any motor torque.

However, it is possible that in some implementations of the FOC some authors choose to defiend q axis as the one aligned to the B_r and d axis as 90 degrees apart from the B_r and in that case I_q does not generate any torque. But to my knowledge this is not the usual way. :smiley:

6-Step example

One simpler variant of bldc motor control is trapezoidal control (or six-step) that is used very frequently in the ESCs; It can only turn on and off the motor phases and cannot finely tune the orientation of the magnetic field vector of the stator B_s. This is how it works for a half of the electrical rotation.

As you can see the idea is very similar as the FOC (field oriented control) but the angle in between the vectors is not always kept at 90 degrees as we do using the FOC.

SimpleFOClibrary init procedure

In the init procedure we are searching to find the correspondance in between the 0 angle to the position sensor we are using and the electrical 0 angle of out motor which is usually defined by the magnetic field vector of the phase a. As shown on the second image and on the one below we could set the stators magnetic field orientation to -180 degrees and we will be sure that the motor will always align to the 0 electric angle.

Then we can just read the sensor value and add 90 degrees to it and use it as our target position to set out stator magnetic field vector B_s to.

As we will be always adding the 90 degrees to our sensor angles, in literature many approaches (park + clarke) are defined not for the actual rotor position but for the rotor angle+90 degrees. instead of searching for the relationship in between the position sensor angle and the electric angle 0, we often search for electric angle 90. And in order to force the rotor to go to the 90 degrees, you need to apply the 90-180 = -90 or 270 degrees.

Here is an example:


Remembering the offset in between the position sensor angle and the rotor’s electrical 90 degrees angle avoids the need for adding the 90 degrees in the formulas, at least for the sinusoidal modulation. But the approaches of finding the 0 or 90 degree angles are equivalent, its a choice of implementation.

Limitations SimpleFOClibrary init procedure

This is a quiet standard procedure, and probably the most simple ones. But there are certainly limitations. The biggest downside is, as we are only doing one measurement it can happen that the rotor due to some physical imperfections (or sometimes just friction) does not go exactly to 90 degrees but some angle close to it. So in that case the force generated by the motor will not be 100 efficient becuase we will not be able to maintain sin(90) = 1. This could be avoided by taking multiple measurements and doing some averaging or some more fancy statisctics.

The other downside of course is that if we do the calibration only once at the init, we are absolutely unaware if something happens in during the operation of your motor. If the sensor drifts, it could be potentially possible to account for the change in the alignment, but at the moment we do not implement such a feature.


Hi Antun, thanks for the very nice write up! This is amazing! I still need to find some time to write up some questions I still have. For now a few initial questions:

Regarding the electrical angle, have you ever performed a repro measurement on the init sequence? I can imagine it has some dependency on position (friction), Q voltage limit and sensor repro?
I’m going to try some repro measurements once I have time to get a feel of the procedure.

Also, what is the actual effect of an error during alignment besides efficiency? Does it impact the position control loop?
Edit: thinking about this I would think it acts as a disturbance on the plant/controller as the required gain for the same amount of torque can vary?

Hi Antun,

I found some time to do a repro test and get a feel of what you explained. It finally clicked with those pictures and the choice of implementation. It is also quite fun to see the motor ‘be pulled’ into this aligned position. My preliminary conclusion is that the mechanical position where you do this alignment really determines the offset. This has possibly to do with my magnetic encoder eccentricty (which i’m also trying to compensate for, but this is work in progress).

Also fun to see that I indeed got one outlier at some point, which is what you describe as the biggest downside.

See the results / script below. Feedback regarding this test is most welcome if i’m missing something :)!

1 Like