Designing a Motor Control
The Motor

Fig. 1: The motor
After reading a book on electric motors it struck me as somewhat obvious what sort of motor I should use: the asynchronous
three phase induction motor. All the complexity is moved from the actual motor to the computer program controlling it.
It gives you the greatest simplicity and flexibility. Essentially the only component limiting its power output is the insulation
of the windings of wire. When it gets too hot power degredation occurs and the motor even goes up in smoke and requires rewinding. So if you manage to decently
cool your motor you can squeeze unparalleled amounts of power out of a relatively small unit.
So how does it work? Well, we have all put a paper clip on top of a table and then moved it about by moving
a magnet underneath the table. And that sums it up really. Only that in a motor we don't want a paper clip
to move about but we want a metal shaft to spin. So we replace the clip by the shaft and wind the table around it.
Now we can still pull the same trick: as we move the magnet the shaft will follow that movement by spinning.
Doing this by hand isn't exactly what you would call a motor. So have someone else spin the magnet for you.
Oh, and there is no piece of table in a motor obviously, but an air gap between the shaft (rotor) and
the magnet (stator).
Magnetic Force

Fig. 2: Motor Schematic
The coils are connected to the phases with the same colour. The green, magenta and cyan coloured dots denote the centre of the magnetic field at the respective point in time as marked in the diagram.
Let's look at the magnet a bit closer: it attracts metal objects because it has a magnetic field. Now there is
something else that has a magnetic field: a wire that has electric current running through it. Ever used jump start
cables and watched them jump (maybe this could be attributed to the name...) around when you started the car? The magnetic field caused that. On scrap yards tons
of metal can be lifted by an electric magnet. An electric magnet is a metal core with some wire wound
around it. And there are exactly three of those in a 2 pole 3 phase induction motor. Now if you switch them
on and off in the right order they replace the magnet you spun around earlier.
Now hopefully you have observed that simply switching the magnets on and off would only allow three positions
for your magnet, causing a jerky movement of the shaft.
But you want it to spin smoothly. So instead, you vary the force of the magnets to gradually
hand off the magnetic field to the neighbouring magnet. And that's exactly what happens when you connect those
electric magnets to a three phase power supply. In Figure 2 you can literally see the magnetic field move around as the
currents through the wires of the electric magnet get weaker and stronger. The direction at which the current flows
through the coils also determines the direction of the magnetic field. Thus, on one side you have a magnetic south pole
and on the opposite side a magnetic north pole, as denoted by the "-" and "+" signs. The shaft in-between follows
this field just like a compass needle.
Let's have a glance at how I came up with the dots. If you look at the sine waves in the diagram at time "green",
you will see, that L1 is at a high potential (let's say 100V), whereas L2 and L3 are both at a potential of about -50V.
Thus, the voltage between L2 and L1 equals -50V-100V=-150V. This creates a magnetic south pole at the centre of the coil,
so actually where the magenta coloured dot is. BUT: let's look at the voltage between L1 and L3: 100V-(-50V)=150V.
It's a positive voltage, creating a magnetic north pole. Since those two coils are right next to each other, their magnetic
fields overlap. They are being pushed away from each other, and being exactly opposite of each other is the furthest they
can get apart. Note, that the bottom coil has no impact at all, since L2=L3, making the coil voltage equal to 0V.
So what does "asynchronous" mean? Well, if we return to the concept of the paper clip moving about on a table you will observe
that it has a little lag over the movement of the magnet underneath the table. The faster you accelerate your magnet
the more obvious this gets. If you accelerate too fast, you could even loose the paper clip. If you use something heavier
than a paper clip you will observe that at some point when the magnet is a bit ahead of the object the force is strongest.
If your magnet isn't strong enough, the object won't move at all. It's like attaching a heavy object to a rubber band
and moving it around. The force is strongest shortly before the rubber band snaps. Fortunately the magnetic field
doesn't snap, it just gets gradually weaker over the distance.
So this is exactly what happens inside the motor when producing torque. The field will always be a bit ahead of the
actual mechanical movement. This is called slip. If it is too far ahead it will "snap", if it's not ahead far enough there won't be enough torque.
Basically there is nothing wrong with the field snapping. But let's look at the extreme case: if our shaft weren't
spinning at all and the field at the same time is spinning all the magnetic energy is used to heat
up our shaft. And heat we don't want, because it decreases efficiency. So, the greater the slip, the lower the efficiency.
Well, not exactly. If the slip drops below a certain value it means that our field is unnecessarily strong and thus
we heat up the wire of our coils by means of their ohmic resistance.
The job of the motor control is to keep the slip at the level shortly before the magnetic field connection snaps.
Therefore it controls the strength of the field and the speed at which it rotates.
The Motor Control

Fig. 3: The motor control system
Since the motor itself is just a chunk of metal with some coils inside it, all its dynamic characteristics are defined
by the motor control. The motor control, in essence, produces the same sine waves as come from normal three phase
power supplies. The more the motor control knows about the situation inside the motor, the better it can optimize
the slip.
A basic motor control with no feedback increases torque by increasing the amplitude of the sine wave and increases
speed by increasing the frequency of the sine wave. That's ok for powering a fan or a water pump but it will
do very badly when torque demands vary quickly as they usually do in a car. A small improvement to this is
feeding back the motors actual speed. That way the control can try to keep the slip at a certain value.
This does actually work quite well, but there are certain drawbacks. One of them being the need for a speed
sensor on the motor shaft.
Pulse Width Modulation (PWM)

Fig. 4: The PWM module
It translated decimal values from 0..255 to an according pulse width or "valve open time". It has three independent channels.
To control the amount of electricity that flows through the motor's coils, transistors are used. Now, you could simply take
an analog sine wave generator and have it control the transistors. Thats a bad idea. Let me explain:
When you pump up your bike's tires you will realize that if you pump fast enough the pump gets warm on certain
spots. Namely those spots that are a bit narrow and slow down the air as it passes through. For my pump
that was the bit where the tube is attached to the pumps body. So slowing something down creates heat, ok?
No let's say for some reason we wanted to limit the amount of air that flows from the pump into the bikes tire.
We could simply place a valve somewhere in between and by those means regulate how much the air is being slowed down.
And also how much heat is generated in the process of slowing it down. But remember: we don't want heat.
So we need to think of something else to regulate the speed of our airflow. Note: if the air flows through
our system freely, no heat will be generated, if no air flows at all, no heat will be generated.
So we could simply use the same valve as before and open and close it at a certain rate. So there will
be times when there is no airflow at all and there will be times when there is full air flow. Added
up over a certain amount of time the same amount of air will have passed our valve as if it had been opened just a bit.
If we open and close the valve fast enough, we won't even notice that the air flow isn't continuous.
But again, there is a limit to this: while changing the valve's position from opened to closed or from closed
to opened it will be some-opened for a small amount of time, again causing heat to be generated. Thus, our
switching interval should be larger than the valves maximum switching speed.
Personally, I can picture the air example better than imagining what is going on with the electrons inside
a transistor. The relations are exactly the same: a half-open transistor gets hot, a transistor has
a certain switching speed (dv/dt) and the frequency of the pulse width modulation should be high enough
for the motor not to notice i.e. to run smoothly.
Another advantage of PWM is that the system stays completely digital from the controlling
software to the motor. Thus you can control very precisly at software level what currents are applied to your motor's coils.
My Try at Sine Wave Modulation
Right now the essence of my controlling program looks like this:
void doPwm(void *arg)
{
float x;
unsigned char dutyCycle[3]={0,0,0};
rt_task_set_periodic(NULL, TM_NOW, 128000);
while(1) {
if (frq>0.0) {
x+=2*M_PI*frq*(float)128/1000000.0;
if (x>=2.0*M_PI) x-=2.0*M_PI;
dutyCycle[0]=amp*(sin(x))+128;
dutyCycle[1]=amp*(sin(x+2*M_PI/3.0))+128;
dutyCycle[2]=amp*(sin(x+4*M_PI/3.0))+128;
}
writeLptVal(PWM1_E, dutyCycle[0]);
writeLptVal(PWM2_E, dutyCycle[1]);
writeLptVal(PWM3_E, dutyCycle[2]);
rt_task_wait_period(NULL);
}
}
It is the most simple approach: calculate 3 sine waves with a variable amplitude and frequency
and a fixed phase shift of 120 degrees. The values are then output over the PCs parallel port
using the four control lines (strobe, linefeed, init and select) as address outputs and the
data lines D0..D7 as data outputs. The task is executed every 128 microseconds, supported
by the Xenomai real time linux kernel. The variables frq and amp are set by another thread which
reads from the console input.
Download the complete source code
Direct Torque Control (DTC)
Now I have to admit that I haven't implemented DTC on my controller yet. So my explanations might not quite
have the quality you would expect or they could even be wrong. But I'll give it a try:
Let's go back to the paper clip and magnet example again: obviously, when you try to stop your paper clip
from moving as the magnet moves, you will feel this stopping force on your magnet, too. The same happens
inside the motor: when you try to stop the shaft from spinning the slip increases and the electric magnet
has to work harder to keep the shaft spinning at the same speed. This "working harder" causes the
current through the coil to increase. Thus, by measuring that current we can estimate how far ahead of
the mechanical movement our magnetic field is. If we figure that it's not ahead far enough, we decrease the
current through the coil, if we figure that it is too far ahead, we increase the current. This is called
a dynamic switching pattern as opposed to the pre-determined switching pattern of PWM. Depending on the
conditions, a lot of unecessary switching (causing heat!) can be prevented.
DTC requires very frequent and accurate measurement of the motor currents, typically 40000 times per second.
The Full Bridge

Fig. 5: The full bridge
We are working high currents here, as demonstrated by the black spot at the lower right half: it was caused
by transistors that caught fire.
Lets go back to our pumps and air example. Imagine the upper transistors were connected to a compressor
and the lower transistors to a vacuum pump. So now each of the three outputs in the middle can either
be connected to the vacuum pump or to the compressor. If an output is connected to the compressor
longer than to the vacuum pump it will overall carry on over-pressure. Otherwise it carries an under-
pressure. The difference in pressure between our three outputs translates to the coil voltages. If there
is no difference, there is no coil voltage.
Radiation Considerations
The fast switching patterns of PWM cause a so called impulse response from the motor. The impulse response
contains harmonics of the PWM base frequency and the actual motor frequency. Whatever it is, it will be
sent into the wild by the cables that lead from the inverter to the motor. For one reason or the other
there are very strict regulations on how much radiation can be tolerated inside a car. Therefor the
radiation should be reduced as much as possible. The inventors of DTC claim that the real time switching
pattern prevents excessive impulse responses from the motor in the first place. I will have to see
about that. It is also recommended to use shielded cables from the controller to the motor.