|
bitsim
Model, simulate, estimate and control robots in C++
|
This example accompanies the fifth lesson of the robotics course. In Forward kinematics and Inverse kinematics the joints of the arm were ideal: they went exactly where we told them. Here we look at what actually turns a joint, an electric motor, and we put one into the shoulder of the arm. You need bitsim configured with Chrono support to run it.
Most robot joints, drones and electric vehicles use brushless DC motors (BLDC). The rotor carries permanent magnets, and the stator carries three coils, the phases \(a\), \(b\) and \(c\). A current in the coils makes a magnetic field that pulls the magnets around. To keep the rotor turning, the field has to move ahead of the rotor, so the current has to be switched from one pair of phases to the next as the rotor turns. This is called commutation.
A brushed DC motor does this mechanically, with brushes sliding over a segmented ring on the shaft. A brushless motor has no brushes: the controller measures the rotor angle and switches the phases electronically. There is nothing to wear out, and the coils, which get hot, sit on the outside where they can be cooled.
Two equations describe the motor. A current \(i\) produces a torque, and a turning rotor produces a voltage, the back-EMF, that opposes the supply:
\[T = K_t \, i \qquad e = K_e \, \omega \]
In SI units \(K_t\) in N m/A and \(K_e\) in V s/rad are the same number. This is energy conservation: the electrical power \(e \, i\) that goes into the back-EMF comes out as the mechanical power \(T \omega\).
The example uses a small motor of the size found in robot joints:
| parameter | symbol | value |
|---|---|---|
| resistance between two terminals | \(R\) | 0.6 \(\Omega\) |
| inductance between two terminals | \(L\) | 0.4 mH |
| torque and back-EMF constant | \(K_t = K_e\) | 0.05 N m/A |
| rotor inertia | \(J\) | \(2 \cdot 10^{-5}\) kg m \(^2\) |
| viscous friction | \(b\) | \(10^{-5}\) N m s/rad |
| pole pairs | \(p\) | 7 |
| supply voltage | \(V\) | 24 V |
Averaged over the commutation, the motor behaves like a brushed DC motor, and we start with that model. It has an electrical and a mechanical equation:
\[L \frac{di}{dt} = V - R \, i - K_e \, \omega \qquad J \frac{d\omega}{dt} = K_t \, i - b \, \omega - T_{\text{load}} \]
Without simulating anything, these give the numbers on a datasheet. At standstill there is no back-EMF and the current is only limited by the resistance: the stall current \(V / R\) and the stall torque \(K_t V / R\). Without a load the motor speeds up until the back-EMF almost cancels the supply, at the no-load speed \(\approx V / K_e\). In between, the torque falls linearly with the speed, and the mechanical power peaks at half the no-load speed.
The two time constants tell us how fast the motor responds. The current settles in about \(\tau_e = L / R\), and the speed in about \(\tau_m = J R / (K_t K_e)\). Here the mechanical one is seven times slower. Notice that \(\tau_m\) depends on the electrical constants: the back-EMF acts as a brake that grows with the speed.
The models of this example are in bitsim: BLDCMotorParameters, dc_equivalent_step, BLDCMotor, BLDCRotor and the speed controller, PIController.
We integrate the DC model with the explicit Euler method. The step of 1 \(\mu\)s is more than 600 times smaller than the fastest time constant, which is what makes such a simple method accurate here.
The current jumps to over 30 A, almost the stall current, and falls as the motor speeds up and the back-EMF grows. The inductance only delays the jump by a fraction of a millisecond. The speed reaches 63% of its final value after about \(\tau_m\), and ends where the constants predict. A real motor controller limits the current, since 32 A would quickly overheat a motor like this one.
Now we open up the commutation. The back-EMF of each phase depends on the rotor angle. Here it is a sine wave, shifted by 120 degrees from phase to phase. What matters is the electrical angle \(\theta_e = p \, \theta\): with 7 pole pairs the pattern repeats 7 times per mechanical turn.
The simplest scheme is six-step commutation. At any time the current flows in through one phase and out through another, and the third is disconnected. The controller picks the pair whose back-EMF difference is the largest at the current rotor angle. That divides an electrical turn into six sectors of 60 degrees.
To know the sector, three Hall sensors on the stator detect the magnets of the rotor. Each one is on for half an electrical turn, and they are 120 degrees apart, so together they give a 3-bit code that changes every 60 degrees. The controller looks the code up in a table:
The codes 0 and 7 never happen with working sensors. A real controller treats them as a fault.
In each sector the two active phases are in series, so the circuit has the resistance and the inductance between two terminals, and the difference of the two back-EMFs:
\[L \frac{di}{dt} = V - R \, i - k(\theta_e) \, \omega \qquad T = k(\theta_e) \, i \]
where \(k(\theta_e)\) is the back-EMF difference of the active pair per unit of speed. This is the DC model with a constant that changes with the rotor angle. Within a sector it goes through its peak \(k_{\text{line}}\), and at the edges it has dropped to \(k_{\text{line}} \cos 30^\circ\). Its average over a sector is \(\frac{3}{\pi} k_{\text{line}}\), and this average is the \(K_e\) of the DC model.
The class BLDCMotor implements this. It does not integrate the motion of the rotor: it takes the angle and the speed as inputs and returns the torque. That way the same class works with our own model of the rotor here, and with Chrono in section 6.
With a load of 0.5 N m the three-phase model runs at the same mean speed and draws the same mean current as the DC model, which is why the DC model is so widely used. The difference is the torque ripple: the torque dips at the edges of every sector, by about \(1 - \cos 30^\circ \approx 13\%\). That is felt as vibration and heard as noise, at a frequency of six times the electrical speed.
The second table shows why the Hall sensors must be in the right place. With an error, the controller switches too early or too late, and every sector is shifted away from the peak of the back-EMF. The torque per ampere drops as the cosine of the error: at 90 degrees the motor makes no torque at all, and at 180 it runs backwards. The torque is measured with the rotor turned slowly at a constant speed, so that the current hardly changes. Notice that the error is in electrical degrees. With 7 pole pairs, 15 electrical degrees are only about 2 mechanical degrees.
To run at a given speed the controller adjusts the voltage. In a real drive the transistors switch the supply on and off thousands of times per second, and the motor sees the average. This is called pulse width modulation (PWM). We simulate the average voltage directly, limited to \(\pm 24\) V. A negative voltage swaps the high and low side of the pair, and drives the motor the other way.
A PI controller computes the voltage from the speed error. It is tuned on the DC model, where the response of the speed to the voltage is \(\frac{1/K_e}{\tau_m s + 1}\). Choosing \(k_i = k_p / \tau_m\) cancels the mechanical time constant, and \(k_p = K_e \tau_m / \tau_c\) gives a closed loop with time constant \(\tau_c = 1\) ms. We also feed forward the voltage that balances the back-EMF at the desired speed, \(K_e \omega_{\text{ref}}\). The PI controller then only has to correct the remaining error, and does not have to build up 15 V in its integral after every change of the setpoint.
The controller runs at 10 kHz and the motor model at 1 MHz. The motor first runs at 300 rad/s, then a load of 0.3 N m is applied from 40 to 80 ms, and then the motor reverses to -300 rad/s:
Under load the controller raises the voltage by \(R \, i\) to push the extra current through the coils, and the speed returns to the setpoint. The current printed is the average over the last millisecond, since the instantaneous current ripples from sector to sector. At the start and at the reversal the voltage is at its limit for a few milliseconds, and the response is slower than \(\tau_c\). The controller can not ask for more than the supply can give, and it stops integrating while the output is limited so that it does not overshoot afterwards. This is called anti-windup.
Finally we put the motor into the shoulder of the arm of Forward kinematics, with the upper arm, 0.4 m long and 1 kg, and no forearm. A motor turns much faster than a joint and makes much less torque than it needs, so it drives the joint through a gearbox with ratio \(N = 20\). The motor turns \(N\) times faster than the joint and the joint gets \(N\) times the torque:
\[\theta_{\text{motor}} = N q \qquad T_{\text{joint}} = N \, T_{\text{motor}} \]
The gearbox also multiplies the inertia of the rotor by \(N^2\), as seen from the joint. That is 15% of the inertia of the link here, so the example adds it to the link.
The joint is now a ChLinkMotorRotationTorque. Chrono simulates the arm, and our motor model provides the torque. This is a small co-simulation: the two models run side by side and exchange values at every step. Chrono steps every millisecond, and the motor runs 100 steps in between with the speed of the rotor held constant:
The arm starts horizontal and has to go to 45 degrees. A PI controller computes the voltage from the angle error. We need no derivative term because the back-EMF already damps the motion: seen from the joint it acts as a damper of \(N^2 K_t K_e / R \approx 1.7\) N m s/rad.
The arm overshoots by more than 10 degrees and then settles at 45. To hold it there, the motor has to balance the weight of the link, \(m g \frac{l}{2} \cos q\), with a constant current. A motor at standstill makes no back-EMF, so the voltage is just \(R \, i\), and only 0.8 V are needed.
Without the integral term the arm stops 2.5 degrees short. The proportional term only produces a voltage when there is an error, and here it needs about 0.9 V to hold the arm up. The integral term builds up that voltage and removes the error.
At standstill the torque per ampere depends on where in its sector the rotor stopped, between \(k_{\text{line}} \cos 30^\circ\) and \(k_{\text{line}}\). So the holding current can be anywhere between the two values printed. The simulated current is inside that range, near the lower end, because the rotor stopped near the middle of a sector.
The ideal joints of the previous lessons were a good start. With a motor the arm takes time to move, overshoots and needs a controller. Those are the effects a controller design has to deal with.
With --render the arm of section 6 is given a new target every 2.5 s: 45, 90, 0, -45 and 135 degrees. The white line points at the target, and the panel shows the voltage, the current and the torque at the joint. The simulation runs four times slower than real time, since the motor is too fast to follow otherwise.
On the left is a schematic of the motor over one electrical turn. The yellow arrow is the electrical angle of the rotor, \(p N q\). The phase the current goes into is red, the phase it comes out of is blue, and the disconnected phase is grey, exactly as the commutation table of section 3 says. When the arm moves the colours switch every 60 degrees of the arrow. When it holds still they stop, and the current has to keep flowing to hold the arm up. Try to see how much harder the motor works at 0 degrees, where the arm is horizontal, than at 90 degrees, where it hangs on the joint.