I've been trying to get my head around how the motor control in EUCs (or in general with 3-phase brushless direct current motors, BLDC's) works, and been meaning to write this post for a good while (a "raw" -version of this has actually sat on my hard drive for months). @Henrik Olsen's question about the mosfets sparked me to finally get around to quickly read this through, make a few small additions and corrections and finally "publish" it here in the forums... So here goes, hopefully I corrected most of the typos etc, and it reads fairly easily, it's been written in parts every now and then, and sometimes I might jump from one topic to another a bit abruptly.
I'm an amateur when it comes to this stuff, so I might use some "unorthodox" terminology and there could be many mistakes, factual errors and misunderstandings on my part, so I wish that the people who really know this stuff could chime in and correct if and when I've gone wrong. There are loads and loads of sources around the internet with more precise (and complex) articles and documents about how the motors work and how they're driven (or can be driven), but hopefully this could give a fairly good (albeit simplified) explanation of the basics of the motor drives.
I'll try to keep it simple (most of the time) and skip over a lot of things like inductance, magnetic fields, different motor builds, mathematics, and most of the details on electronics as I'd likely just get those parts wrong anyway , and mostly concentrate on a basic overview of how the motor is being driven (or at least what I understand of it) Probably other people and sources can explain these parts better and in much more detail, if anyone's more interested.
To start off, I'm not going to talk about 3-phase brushless motors, but a basic brushed DC motors ("1-phase") at first, as it's easier to understand the basics of the half-bridges through such an example. I've also seen (small) 3-phase motors sold with "1-phase" connections (just power + and -, and maybe PWM-input or not even that), where the controlling circuitry is inside the motor, so this could be such a case too.
In a brushed motor, as the name suggests, there are parts called "brushes" that actually have mechanical connection to the coils in the rotor (the moving part of the motor). The coils actually act as electromagnets, and if you've played with magnets some time in your life, you know that opposite poles (north and south) attract each other and similar poles (north and north or south and south) repel each other.
Simplified, as the brushes slide across conductors in the turning rotor, they cause current to flow through the winding coils ("energizing the coils"), which creates a magnetic field ("north"-pole in one and "south"-pole in the other) that keeps the motor turning in one direction, as the coils attract or repulse the (usually permanent) magnets in the stator (the stationary part of the motor). There are splits in the "ring" where the brushes touch, so the brushes will alternate between touching different "ends" of the coils of the motor, causing the magnetic field of the coils turn on and off and to change polarity (depending which brush they're touching or no brush at all) to keep the rotor moving as the poles of the permanent magnets attract / repel them.
If the motor would need to turn in opposite direction, the polarities of the wires connected to the brushes could be changed to the other way around, ie. you'd change the + and - wires going into the motor to the opposite connections. No complex electronics are needed to drive the motor (at least in one direction), simply plugging it into a power source will do. The downside is that the mechanical brushing causes friction, noise and the brushes wear down, so they need to be changed every now and then when they wear out. But how we (slowly) get from here to BLDC-motor control is a fairly simple circuit called H-bridge (not a half-bridge, a H-bridge).
Wikipedia explains an H-bridge simply as:
A H bridge is an electronic circuit that enables a voltage to be applied across a load in either direction. These circuits are often used in robotics and other applications to allow DC motors to run forwards and backwards.
A H-bridge could be built using just four (mechanical) switches:
So when switches S1 and S4 are closed, the VCC (+) and GND (-) of the power source are connected across the motor, running it in one direction. Open those switches and close S2 and S3, and the polarities will be opposite, running the motor in the other direction. Making the upper ("high-side") and lower ("low-side") switch (be it a mosfet or BJT or whatever) on the same side of the H-bridge conduct at the same time (like S1 & S3 or S2 and S4 above) would cause what is called a "shoot-through", ie. the current won't flow through the motor, but instead directly passes through the switches, causing a short circuit (only resistance in the current path is that of the switch-elements and the wires).
Now, using mechanical switches isn't that much more handy than just switching the wires in the opposite connections. That's why the H-bridge is usually built from transistors. Transistors can be controlled electronically and used as high-speed (non-mechanical) switches, so you basically use just two states for them: fully conducting (open) and not conducting (closed, although sometimes you could see these terms used vice versa, ie open = not conducting and closed = conducting, I'll just use terms "conducting" or "on" and "not conducting" or "off" to prevent any misunderstandings). Actually, transistors can be used in-between these two extreme states to make them conduct only partially, for things like (audio-)signal amplifying, but that's outside the scope of this, as only switching is needed for motor driving. Depending on the type of transistor, the conductivity over it can be controlled either by current or voltage.
In the below animation, bipolar junction transistors (BJTs) are used for and H-bridge, with signals coming from INA and INB to switch the different transistors between conducting and not conducting. Our wheels use what are called MOSFETs (metal-oxide-semiconductor field-effect transistor, a certain type of transistors) instead of BJTs. Mosfets are voltage-controlled, unlike bipolar junction transistors, which are current-controlled (but still need a certain amount of voltage to overcome the PN-junction... Yeah, I won't go into that here :P). There are more different types of transistors (J-Fets, IGBT's...), but I won't get to them here, as I only have some experience with BJTs and mosfets, and the wheels use mosfets anyway.
So, now we could have electronical directional control over a (brushed) motor by turning on two transistor in opposite sides of the bridge, one on the high-side and one on the low-side. The next step is to control the speed of the motor (don't worry, I'll get to BLDC's and how this all relates to them in a while).
A motor also acts as a generator, it actually produces it's own voltage when it's turning. This is called back-EMF (back electromotive force) or BEMF in some sources. When the motor is not turning, the back-EMF is 0 volts, and when it's running, it produces a voltage that raises linearly with the rotational speed of the motor. When the motor's turning in the "correct" direction caused by the voltage from the batteries, back-EMF polarity will be opposite to the voltage from the battery direction.
So, if you connect a battery with some voltage X to the brushed motor, if will start turning and reach some speed. If you take a battery with twice the voltage, 2 * X, it should rotate at twice that speed (assuming it can overcome the increasing friction and won't burn due to overvoltage/current etc etc ).
This is probably a good point to mention about the current in the motor. When the battery is first connected to a non-moving motor, the motor isn't turning and the back-EMF is thus 0V. There's a voltage difference between the battery and the back-EMF, and it's this difference that causes the current to flow. Simplified, there's always some resistance in the circuit (for example the motors' internal resistance from the coil-wires, other wires, connectors etc), and current equals voltage divided by resistance.
So still keeping it simple, as the resistance stays (or here is assumed to stay) the same, the bigger the difference between the back-EMF voltage and the voltage from the battery, the higher the current. If there's a big difference, the current will be higher, if there's a small difference, the current will be lower, and if there's no difference (battery voltage = back-EMF), there's no current. Current is the thing causing torque in the motor, so when the battery with a constant voltage is first connected and the motor is not turning, the difference between these two voltages is at its greatest. This will cause high current to flow, causing high torque and getting the motor moving. As the motor speeds up, the current drops as the difference between the voltages becomes smaller (back-EMF goes up), and the current (and thus the torque) of the motor drop. Ideally, once the motor reaches the speed where the back-EMF equals the battery voltage, the current and the torque would drop to 0 and the motor would be "free wheeling". In reality, the back-EMF is probably slightly below the battery voltage, as the motor has to overcome friction from bearings etc, and there's some current always flowing when running at steady speed.
Some motors state constant factors called k-factors that can be used to calculate speed from voltage and torque from current, as they're both linear.
Back to the speed control. As the speed of the motor is relational to the back-EMF and the motor changes speed when back-EMF and applied voltage do not "match", we can actually control the speed of the motor by controlling the voltage applied to it. You could use a potentiometer (a variable resistor) to drop the voltage before the H-bridge, but that just wastes power by burning it off as heat in the potentiometer (which itself might burn if it needs to drop a lot of voltage), and you'd have to control it by hand.
This is where the fast switching ability of the transistors steps in. You might first think that "partially" switching on the transistor could be used to control voltage, but it's not a very precise method (to my knowledge) with motors and also wastes power in the transistor (again, actually heating up the transistor, which could lead to it burning). Instead of trying to control the voltage by allowing the transistor to conduct only partially, a scheme known as pulse width modulation (PWM) is used.
I'll try to explain this as the best I can. PWM is a technique to produce a square wave signal (like those seen above) where the relation between the "on" time (full voltage) and "off" time (zero voltage) can be controlled. During one period, for some part of the period the full voltage is applied, and for the rest of the period, no voltage is applied. Controlling the "width" of the on-partion (full voltage pulse) of the period you get different "duty cycles". 100% duty cycle means that the voltage is at the full value throughout the period and 0% means there's no voltage in the entire course of the period. A single period is usually very short, and the frequency of the PWM is measured in hertz (1/s), meaning how many periods per second there are. Motor controls usually use "low" frequencies for PWM up to some tens of kilohertz (for example, 20kHz = 20000 hertz = 20000 times per second, about the upper limit of very good human hearing range). 20000 times a second might not sound "low", but in electronics-side, the "ITU"-specification for frequencies calls 3kHz-30kHz -range VLF = "very low frequency"
When "sufficient" voltage is applied at the "gate" of the mosfet (that's one of the three pins in the mosfets, the other two are "drain" and "source", the functions of these are similar to base, emitter and collector in bipolar junction transistors), the mosfet will allow current to flow between it's drain and source. Depending on the voltage difference between the gate and source, the mosfet will either be fully off (not conducting), then start to partially conduct, and once the voltage difference is "large enough", it will fully conduct. I won't go into gate charges and how to go with driving high-side N-channels here...
Now, if we "drive" the gate of the mosfet with a PWM pulse (let's just assume that the on-part of the pulse is of "sufficient voltage", ok? ), the mosfet will conduct during the on-part (full voltage) of the PWM-period, and stop conducting during the off-part of the period. In reality, there is also some delay ("rise and fall times") between the transistor starting to fully conduct after the higher voltage is applied and before it stops conducting after the voltage drops to zero, but for the sake of simplicity, just assume it turns on and off pretty much instantly.
So, as the mosfet is switching between fully conducting and not conducting, the motor will get pulses of voltage applied to it. Again simplified, with the switching happening at a high frequency, what the motor "sees" as the incoming voltage is the "average voltage" between the "on"-part and "off"-part of the PWM-period. If the full voltage would be for example 5V, and the on-part of the period would last half of the period, and the off-part would last the second half of the period, the voltage applied to the motor would be half of the full voltage, 2.5V. Using the duty-cycle, it's then easy to calculate the voltages, using a fraction percent-value (100% = 1.0, 50% = 0.5, 10% = 0.1 etc):
100% duty cycle: 5V * 1.0 = 5V
80% duty cycle: 5V * 0.8 = 4V
60% duty cycle: 5V * 0.6 = 3V
40% duty cycle: 5V * 0.4 = 2V
20% duty cycle: 5V * 0.2 = 1V
0% duty cycle: 5V * 0.0 = 0V
Being able to change the duty-cycle, and thinking of the average voltage, you can create more complex waveforms than simple square wave. The below image shows a sine-wave -like voltage applied by changing the pulse width between the periods:
The thick black line is the voltage applied to the motor, the thinner lines show the PWM-pulse going up and down, at different duty cycles in each period. Nevermind the horizontal line in the middle, it's not related to this.
So, I hope from this you can see how using an H-bridge with 4 mosfets can be used to control the direction of the motor (by using "high-side" and "low-side" -mosfets from two opposite sides of the H-bridge) and how the speed of the motor in said direction can be controlled by quickly switching the transistors on and off, to apply different voltages to the motor. Actually, there's current flowing only when the transistors are conducting, the motor connections are "floating" during the off-state, and thus, no current (or very little) is flowing. So actually the motor's alternating between (almost) free-wheeling and motoring, but this happens fast, and "mass is slow", so it has very little effect (you won't notice it).
And now, to the "real deal", the 3-phase BLDC motor, which is what (at least most if not all) our wheels use.
Both the above animations show an "inrunner" -BLDC motor. It means that the "inside" of the motor is rotor (the part that turns) with the permanent magnets. In our wheels, the structure is actually the opposite: the tire on the outer rim of the motor turns, while the stator is in the middle (an "outrunner"-motor). AFAIK, same principles of control apply to both, although the motor characteristics might be slightly different.
The basic idea is still the same as in the brushed motor driving: magnetic fields of the coils are used to attract / repel the permanent magnets.
In the brushed motor, the brushes handled energizing the coils and it was simply enough to plug a power source into the two connectors of the motor. Here we have three connectors, that then are connected together inside the motor, there are two common patterns, the "wye" (or "y" or "star")-connection and the "delta" (or "triangle")-connection:
Personally, I must admit that I don't know the pros or cons of either configuration. To my knowledge, both can be run with similar principles, although the commutation order might differ(?)
The motors in EUCs have much more coils and poles than in the usual pictures, I believe this is to give a more precise control over the motor position, ie. finer control.
Basically, the upside of a brushless motor is less friction (only mechanical connections are in the bearings), and thus less audible noise (unless you can hear the PWM-frequency like in some wheels ), longer lifetime and (I believe) higher efficiency, although this may vary. The downside is that a much more complex method of driving the motor is needed compared to brushed motors.
You can't just plug a battery over two phases and expect the motor to turn. It could maybe twitch a little if the magnetic fields of the coils happen to attract to/repulse from the nearest permanent magnets, and then stay there stationary, all the while heating up the coils and, if the battery/other power supply can supply enough current, probably sooner or later burning them or the power source. So don't do that.
To drive a 3-phase motor, you need to energize the phases (and thus the coils) in correct order and at correct time (well, at least some motors should start eventually turning and "catch up" just by energizing the phases in correct order at stable frequency, but that's not really "controlled" way of driving it). For controlling three phases, three half-bridges (not H-bridges) are used:
Looks pretty similar to the H-bridge before? That's because a half-bridge is also known as "half-H-bridge". Each of those two mosfets on the high- and low-side form one half-bridge (half of a H-bridge). With three phases, you need three half-bridges (or "one and a half H-bridges"? )
The reason I started with the brushed motors and the H-bridge is that if you understood the simpler H-bridge (how it can be used to control the direction and the speed of the motor), the same principles work here: the speed/current of the motor can be controlled with PWM and direction of the motor can be controlled based on which two phases are conducting at a time (high-side from one bridge, low-side from the other). The added complexity is that the phases need to be energized in correct order as the motor turns. For this, the controller needs to know the position of the rotor.
Typically, EUCs seem to use Hall-effect sensors (Hall-sensors) for detecting the rotor position (there are other ways, like rotary encoders, but I don't think they're used in EUCs). Wikipedia states that:
A Hall effect sensor is a transducer that varies its output voltage in response to a magnetic field. Hall effect sensors are used for proximity switching, positioning, speed detection, and current sensing applications.
A three sensor setup seems to be typical for our wheels and BLDCs in general. Many BLDC-driving documents / articles give out the commutation order of the phases either as a table or as a graph showing the sensor on/off -states and the high/low/floating -states for the phases (for one direction):
So there are 6 different "states" (or combinations) of the bridges. The order of states can be different for different motors, so if you try to build your own motor controller, it might take some trial and error to find the correct sequence (and then reverse to run it in the other direction). Also, a brief period of both mosfet "off" (not conducting) is needed when the conducting mosfet on the bridge is changed from high to low (or vice versa), as the mosfet gates need to be "discharged" before the voltage drops below the conducting threshold. This is called "dead time insertion" (DTI), ie. a brief period is waited before continuing operation, otherwise there's a risk of bridge shoot-through (the other mosfet hasn't fully stopped conducting, and the other starts conducting -> potential short circuit through the bridge between battery plus and minus).
In addition to the position, the hall-sensors can be used to measure the speed (how fast the sensor states are switching from one position to another) and direction of the motor (the order in which the sensors switch on and off). Somebody also suggested that after certain speed, the EUCs could actually switch to sensorless speed/position-detection (maybe the hall-sensors don't switch up and down fast enough after certain speed?), using the back-EMF voltage from the floating phase to induce the rotation speed. I haven't studied the subject much, so I won't go into it, but I did learn that this requires fairly precise timing (otherwise you'll get false readings).
Earlier I mentioned that the voltage difference between the batteries (well, actually the average from the PWM-pulses, when using that for control) and the back EMF is what causes the current to flow. Now, as current causes the torque of the motor, and higher torque means higher acceleration (faster change in speed), the applied voltage can also be used to control torque. Larger voltage difference, larger current, larger torque. So in addition to controlling the speed, the wheels must control the torque to keep us from falling down.
The final piece of the puzzle for self-balancing is not actually in the motor, but of course the gyroscope/accelerometer in the mainboard (to my knowledge, the accelerometer is there only to make reading the gyroscope more accurate, I've understood it tends to wander and the acceleration info can be used to correct this in the filters). In principle, the idea is simple: the gyroscope is used detect the pitch (tilting back and forth for acceleration/deceleration) and roll (shutting down after falling on its side) of the wheel:
When the wheel starts tilting forwards, the motor must be accelerated to a faster forward speed (or get moving if stationary, or decelerate if moving backwards) to keep it balanced. If tilting backwards, the motor must be slowed down to prevent falling backwards (when moving forwards). There might be some form of "dead-zone" allowing the wheel to tilt a little bit backwards or forwards before the motor reacts (one way of doing "soft"-riding mode), also some wheels allow the pedals tilt more when turning in slow speed (so probably they use the speed-, pitch- & roll-information for this).
Of course, the real deal is the balancing logics and fine tuning of the acceleration/speed control/dead-zone/allowing tilts when the wheel is turning (ie. the roll is different from zero) etc. That's what makes different wheels different to ride, and probably is one of the most, if not the most important asset of any single wheel-company, as that's what really distinguishes it from other similar wheels and gives the riding "feel" of the wheel.
There are a ton of more technical details I've skipped over above, like different and more complicated ways the PWM-drive can be done (sine-wave driving, space-vectors, field-oriented control, whathaveyou...), motor inductances, inductive spikes, etc etc., mostly because it goes beyond the scope of "basics", but also because I'd probably get a ton of details plain wrong
But, were not really finished yet with the basic motor control: there is (at least) one more thing: braking (yeah, that's nice to have), namely, regenerative braking.
There are multiple ways to brake an electric motor: you could use mechanical brakes (yeah, well, not really with self-balancing wheels ), let it free-wheel to a stop (with self-balancing wheels, not really an option either), use resistive braking (which causes tons of heat that must be dissipated), "plugging-type" braking, where the motor control actually tries to drive the motor backwards (causing what is called "slipping"), dynamic/"rheostatic" braking (of which there are actually multiple different types of) and finally, regenerative braking. I haven't actually delved that deep into braking the motor, especially the dynamic/rheostatic -types, but I do know a little bit about the basics of regenerative braking.
The best fairly brief explanation of regenerative braking I've found is this:
Although it talks of a single-phase DC motor and H-bridge, I believe the same principle can be (and is being) used for 3-phase motors also, just that the phases must be switched in "correct order" and correct timing. For a much better explanation than mine, you probably should read through the accepted answer in that question.
As said before, the motor also acts as a generator/dynamo due to the back-EMF induced in it while it's turning. When you get moving with the wheel, you actually gain "kinetic energy" (E = mv2 or something along those lines?); that is, the chemical energy in the battery packs converts into electric energy, which then converts into heat (thermal energy) and motion (kinetic energy). Although we often talk about "energy losses" or "power losses", energy never actually disappears. The "losses" just mean that part of the energy turns into something we don't want (like heat instead of motion). But anyway, some part of the energy dissipated from the batteries has turned into motion (and some part has been "lost" to other forms of energy), but now you need to slow down or stop. Again, energy never disappears completely, so when you brake, the motor actually has to do "dump" the extra kinetic energy somewhere, it can't just disappear into thin air (well, technically, it CAN disappear into thin air, in the form of heat ). Purely resistive braking is a bit troublesome due to high power that must be dissipated, so to do braking with resistive load, huge power resistors with large heatsinks would be needed.
The basic idea of regenerative braking is (or at least seems to be, from what I've understood ;)) that low-sides of the half-bridges (probably two bridges at a time in a 3-phase motor) are conducting simultaneously, causing the current from motor to flow through them and back to the motor to brake and "charge" the magnetic fields of the motor coils, then one high-side is switched open (there could be some leakage through the high-side mosfet body-diodes?) to "shoot" off the charge (discharge the coil magnetic fields) into the battery (during which time the motor is not braking). Basically the back-EMF voltage must get (at least momentarily) higher than the battery voltage for the current to flow in "reverse" and charge up the packs. "Discharging" that into the packs, the voltage drops again, and then the low sides are opened again to brake more and charge up again. Repeating this fast, the motor is "switching" between braking and dumping the energy into the battery packs. As this is done fast (probably at the same PWM-frequency as when normally driving the motor), you feel it as constant braking (same as you do when you accelerate, you could say that the motor is actually switching "on" and "off" real fast, but the frequency is high and mass is slow, so you won't notice it). Probably (a large?) part of the energy is burned off as heat in different parts (motor coils, wiring, mosfets, even the battery cells), so it's nowhere near 100% efficient, more like "convenient" (and there is the upside that it does charge your packs, at least a little), as no extra parts are needed.
The rate of deceleration (or "negative acceleration", if you will) plays a role in the amount of (momentary) power generated during braking, and there have been reports of the mosfets actually burning on mainboards during strong braking. On Vee's MCM2s, I tested "not-that-aggressive-yet-strong" -braking, and got peaks above 2kW (if memory serves). Even if Gotway has that huge 1.5-2x error on the current measurements, it's still above 1kW at peak (but only for a moment). I don't know how high it could go with lots of mass and really strong braking, but I guess "pretty high" Apparently high enough to fry something, if you look at some experiences here.
Well, that's pretty much my short(ish) explanation of the motor-drive... I was going to write about the details of things much more, but decided against it, as it would probably make this too long and there are many, many things I'm not that sure about