Build a DIY brushless motor controller in a day (ish)
(This is part one. Part two is here)
(Also make sure to check out the updated, ridiculously simple commutation scheme, in the form of an infographic, here)
Hey folks, this post is about my approach to building a pretty basic dual brushless motor controller. I'm going to focus on the power section in this post, and as soon as I have time I'll discuss the code/FroBoard side of things in another post. I got started on the power section last night after a copious amount of coffee, and wrapped it up this morning- it's a pretty simple build if you're used to building stuff on perf board. I firmly believe that the best way to learn how something works is to build it yourself- that's how I learned about electric cars, and brushless motor control is no exception. So if you want to learn how brushless motor controllers work, build one! A controller this size is a great place to start if you're just getting involved in brushless motor control- I would definitely recommend starting out with something like this if you plan on building a car-sized controller. In fact, my first brushless controller was essentially half of this controller. It's good up to around 24 volts and 1 or 2 amps per motor, but with proper heatsinking it could probably take a bit more current.
This post is written with the electronics hobbyist/motor enthusiast in mind. It's also turned out to be a bit longer than expected; ah well. I'll try harder next time! Most of this won't make any sense if you don't have a rough understanding of what brushless motors are and how they work- if you're in that group, no worries. There are lots of great sites out there explaining those details; unfortunately, I don't have the space to go through it here. I'm really just hoping to take a little bit of the mystery out of brushless motor control and show that it isn't all the black magic it can seem like- hopefully it helps a few hobbyists out there get a little further.
This controller is currently driven by two FroBoards, which is definitely more processing power than is really needed- it's cool because it leaves room to read encoders, do serial stuff, and anything else you want to do in addition to running the motors. I'm running a whole robot off of these two FroBoards- usually, the motor controller is a separate unit, and another processor is used to handle the higher level decisions. Right now, for instance, I have a serial routine set up that lets me use a terminal emulator (MacWise in my case) to drive the robot around using the number pad on my keyboard, and there is still tons of room for add-ons. With a storebought setup, this would probably take two separate motor controllers and a processor to control them. So anyway, that's pretty cool. That said, this power section could just as easily be driven off of a different processor or board; it isn't specific to the FroBoard.
Likewise, it would be really easy to build a single motor controller for use with one processor or FroBoard in this style. It would just be a matter of getting rid of three of the half bridges. Anyway, without much ado, here's a schematic for y'all:
This is the schematic for my power section. It is seriously just a pile of transistors and resistors- no magic here. Of course, if you're new to brushless motor control, this probably looks like a bowl of spaghetti- so I'll explain a little bit about how it works.
A 3-phase brushless DC motor- which is what we want to drive with a brushless motor controller about 98% of the time- has three wires coming out of it. Wikipedia, at the time of this writing, has a pretty solid explanation of what these are connected to inside the motor. We're worried about what's outside the motor. In order to get the motor to spin, we need to apply three sine waves to these wires, each 120 degrees away from each other. Here's a graphical illustration of what we're talking about:
"But wait," you ask, "what the hell?" It's a bit a of jump to go from three sine waves on a computer screen to a spinning motor. I'll leave the theory to those who feel like writing about theory; not me at the moment. I'm worried about how to actually build one of these things! We have some power source, probably a battery or other DC source, that needs to be turned into all that crap.
The way we do this is with half bridges. Those who have dabbled in motor control probably know about H-bridges. Well, fittingly, a half bridge is half of an H-bridge. This involves two transistors connected to each other, with their other halves connected to power and ground. The output is in between the transistors. The transistor connected to power is often referred to as the "high-side" transistor (generally a PNP, in the case of smaller controllers), and the one connected to ground can be called a "low-side" transistor (generally an NPN). Clear as mud, no? If you look at the schematic, there are six of these- but we're building a dual motor controller. Three of them make up a single 3-phase motor controller. When the high-side transistor is active, the signal at the output high. When the low-side transistor is on, the signal at the output is low. Using two complementary (when one is high, the other is low) PWM (pulse-width modulation) signals, we can create (or pretend we're creating, for the purists out there) any voltage between ground and power. Sooo, with three of these, we can make 3 sine waves. It takes a little while to wrap your head around this stuff; I still confuse myself with it on a regular basis.
The only thing to worry about here, which is pretty obvious when looking at the schematic, is that you really, really don't want to turn on both the high and the low side transistors at the same time. This would result in a blatant power-to-ground short. Since it takes a few microseconds for the transistor to actually turn on or off, it's also important not to switch them at exactly the same time- this where "dead time" comes in. It's just a short delay between turning one transistor off, and another one on- but it will save you piles of transistors. It's also important to account for this in your reset code- even if it works fine while the processor is running, it is *very* necessary to account for what happens when the processor gets reset. Attention to detail here really pays off.
This all probably sounds like a bit of a pain in the rear; it definitely can be. What they don't tell you when you get into motor control is that, especially with smaller motors, the shape of the sine wave isn't very critical. In fact, it doesn't reeeaaaally have to be a sine wave at all. Things are more efficient if you get closer to a sine wave, but most smaller motors will run just fine on a trapezoidal signal. Or even a square wave. What this means is that everywhere the sine wave is greater than zero, the high-side transistor is active; everywhere the sine wave is less than zero, the low-side transistor is active. This is called block commutation, and it's one of the easiest ways to control a brushless motor. Once you get this far, full sine commutation comes more easily, too. So that's what we're going to do with this thing. It can be used for sine commutation too; it's all in the code.
So how do we know when the "high" parts happen, and when the "low" parts happen (insert corny joke about life here)? Most motors have some sort of sensor that will tell you where the rotor is at. In the case of many smaller, robot-sized permanent magnet motors, this comes in the form of digital hall sensors. These things are really super easy to use. When a magnetic field is passing through the sensor in one direction the output is high, and in the opposite direction the output is low. So, the output of the controller more or less matches the output of the hall sensors, plus a few tweaks to make life better. Dead time, throttling, and reversing the direction of rotation, for instance.
Model airplane motors (and a few others like them) are a different story. They don't use sensors, and instead use the back EMF of the motor as it's spinning to determine the position of the motor. Other commutation types are encoders or analog hall sensors. All it takes to interface with any of these is a little ingenuity, so don't be scared if your project doesn't use digital halls.
Anyway, that's enough thought experiments. The schematic shows 6 half bridges and some other crap. All of this is available at Radioshack for about three times what you'd pay if you ordered it from mouser.com, so sources are plentiful, if slightly unfair. The half bridges use TIP120 darlington NPN transistors on the low side (emitter goes to ground, collector goes to the output/high side), with a 470 ohm current limiting resistor between the base and the input to the board. Bring the corresponding input up to 5 volts or so, and the transistor turns on. Not much to that one. The high side is slightly more complicated. It uses TIP125 PNP transistors. The emitter of these is connected to the controller's drive voltage, and the collector is again connected to the output/low side. To get these guys to turn on, we need to pull the base below the emitter voltage. This is done with a trusty 2N3904. The base of the TIP125 is connected to the collector of the 3904 through a current limiting resistor, and then the emitter of the 3904 is connected to ground. So when the 3904 is active, the base of the TIP125 is pulled low, and it turns "on." Of course, don't forget the 2k-ish current limiting resistor on the base of the 3904. This is then connected to the input to the board. So, looking purely at driving the inputs, 5V on a high-side input means the corresponding transistor turns on, just like the low side would if you applied 5V to a low-side input. Cool how that works, huh?
There's also that giant, electrolytic, 1500uF cap on the board. This can really be anything roughly in that size range. I used a 50V cap; you might be able to get away with a 35V cap on a 24V system, but on a controller like this that's likely to have lots of spikey stuff going on, 50V is probably a safer bet. This cap is there to absorb the voltage spikes involved in switching coils on and off really fast. There's a 10k resistor in parallel with it, so that it bleeds down after power is removed. In the final application, it's important to think about precharging this cap, depending on your power source. Connecting an empty cap directly to 24V produces a reasonably sized spark, which is hard on relays and stuff like that. A circuit can be made to connect the cap to the power supply through a resistor before connecting it directly, allowing it to charge slowly without hurting anything. I was lazy; this isn't too big of a deal for testing and casual use, but be prepared for the spark.
There's also a power indicator LED and a current limiter. It's up to you to pick these out, I used whatever was in my parts bin.
I wired it all up on a piece of perf board, in classic spaghetti fashion. The only stuff that you might *not* want to use 30 AWG hookup wire for involves the power traces... the outputs from the half bridges and the supply power. I believe I used 20 or 22 AWG for this. Here's what the bottom of the board looks like:
So, to use this thing, one connects the pins (J2 on the schematic) to your processor of choice, or in my case to the PWM outputs on a couple of piggybacked FroBoards. The big orange connector (J1) takes power, ground, and the 6 wires coming from the two motors. Then it's just a matter of generating the correct signals, and voila- spinning motors! Here's what it looks like on the robot, hooked up to the FroBoards, with a bunch of my crap in the background and a really, really messy wiring job:
Hopefully I'll have time to go through the code and stuff necessary to make this work very soon. If I don't get to it right away, it is really pretty simple code- the only difficulty is in getting past the logical hurdles involving what it actually has to do. I wrote the whole thing in PICBASIC, and the meat of it is just a few lines long. So don't be afraid to take a crack at it!I'd also like to talk about a big, car-sized power section in a future post- so stay tuned!
//AGA



