Estimating speed from back-EMF on a cheap brushed motor
Early on I wrote a section in the OpenServoCore control doc called “Back-EMF, a Velocity Sensor for Free”. A spinning motor is also a generator, so if you take the voltage across its terminals and subtract the part the winding resistance eats, what’s left is proportional to speed. The board already measures both. No encoder, no extra part, and a fresh reading every PWM period. I was pretty excited about it.
A couple of months and a lot of notebooks later, it turned out to be a really good sensor for how the speed changes, and a pretty bad one for what the speed actually is. This post is the story of how I got from one belief to the other, and what the firmware does about it.
This is the second of three posts where I ask whether a cheap servo can reliably observe one thing about its own motor. The first one is about brush contact resistance and motor identification. This one is speed from back-EMF. The third is winding temperature without a sensor on the rotor.
If you’re new here, OpenServoCore is my effort to turn cheap MG90S-class servos into networked smart actuators with sensor feedback, cascade control, and its own single-wire bus protocol. Everything below ran on one MG90 clone servo on the rev 2A dev board, which carries a CH32V006 (WCH’s sub-20-cent RISC-V microcontroller).
TL;DR
If you’d rather not read the whole saga, here is where it landed.
- Speed from back-EMF is a subtraction, the terminal voltage minus the current times the winding resistance. A servo spends its life at slow speeds, where those two are about the same size, so any small error in the subtraction becomes a big error in speed.
- The brushes add a drop that wanders over the day, and the servo has no way to sense which state they’re in. No fixed correction can follow that.
- So I stopped trusting the back-EMF’s level. What it’s really good at is the fast part, seeing a change in speed almost right away, long before the pot does.
- The firmware now takes its mean speed from the pot and only the fast part from back-EMF. With the old switch between the two, Velocity mode ran 6 to 9% slow. With the blend it lands within about 2% of the commanded speed.
The Idea
The motor’s electrical equation, turned around, gives the speed.
speed = (V - I·R) / Ke
V is the voltage across the motor terminals, I is the winding current, R is the winding resistance, and Ke is the back-EMF constant, volts per unit of speed. I covered what Ke is and how I measured it in the motor teardown post.
V and I get sampled inside the PWM drive window, which is the whole subject of the duty floor post, and averaged over a short boxcar (a plain moving average over a fixed window). That boxcar is the back-EMF speed everything below talks about.
The R in there isn’t a live measurement. It’s the identified resistance, scaled to whatever temperature the winding heat model thinks the copper is at. That model is what the thermometer post is about.
Ke Is the Easy Part
Going in, I figured Ke would be the hard constant. For a while it even looked that way, when the back-EMF read way low in October, but that turned out to be a bad fit of the constants. Measured cleanly in coasts, Ke is boring in the best way. It’s set by the magnets and the winding, and it repeats really tightly.
So the constant on the bottom of the equation is fine. Everything that goes wrong below is about the I·R part on top, and the stuff that isn’t in the equation at all.
Where a Servo Actually Lives
This is the part I underestimated the most.
On the bare motor, spinning freely in the middle of its range, back-EMF speed works pretty well. The back-EMF is a few volts there, and I·R is a small correction on top. I assumed that would carry over to the servo.
It doesn’t, because of the gearbox. The MG90’s motor goes through a reduction of a few hundred to one, so a slow crawl at the horn is barely turning the motor at all. At a crawl the back-EMF is a tiny fraction of a volt, and the crawl’s current through the winding drops about the same. The thing I subtract is as big as the thing I want! And when the servo holds still, which is where a position servo spends a lot of its life, the back-EMF is zero and I·R is all of it.
It’s like weighing your cat by stepping on the bathroom scale with it and subtracting your own weight. If you’re off by a pound on yourself, the cat is off by a pound, and for a cat that’s a lot. At a crawl the back-EMF is the cat, and any error in R, or anything else that drops volts in the winding path, lands on it in full.
What I took from this is to grade an estimate where the device actually lives, not where the motor is happiest. A method that looks great on a free-spinning motor can be a coin flip at the bottom of a servo’s range.
Standing Still and Under the Floor
I assumed that at rest the estimate would just read about zero, maybe a little noisy. But at a hold the cat weighs nothing, so the estimate is purely the small difference of two big numbers. If the winding warms up and the firmware’s R falls behind, the estimate invents a speed out of nothing.
The stage 4 notebooks showed exactly that with the shaft stopped under current. It reads a forward speed, and the harder the motor pushes, the bigger the phantom speed, as if there’s extra resistance nobody told the estimate about. I don’t know what it is yet.
Very low duty is worse. The terminal samples sit at fixed points inside the drive window, so below some duty the window closes before the samples land, and they read the brake instead of the motor.
The duty floor post has all the detail on where the samples sit, and it pushed that floor down a lot, but there’s still a floor. I expected the estimate to fade into noise down there. Instead it reads a wrong number with a straight face, and on one stage 3 ladder a stopped rotor under the floor read a big negative speed, confident as anything.
The way I think about it now, an estimate that can’t measure should say so instead of guessing. So any boxcar with a sample under a floor is void, and the estimate is either reported or absent, never quietly wrong. The catch is that a slow servo lives under that floor a lot, and down there the speed has to come from the pot.
Commutation Ripple
I had assumed the sample-to-sample scatter was ADC (analog-to-digital converter) noise, the random kind that averages away. Then I looked at its spectrum.
The motor has three commutator segments and two brushes, so the current ripples several times every revolution, and that ripple goes straight through R into the estimate. Once the motor is moving at a decent clip, the vast majority of the scatter is that ripple. In the spectrum the biggest peak at every speed sits right on the commutation rate, and it walks up as the motor speeds up. The plots are in the speed notebook.
The annoying part is that the ripple’s period moves with speed, so a window that cancels it at one speed doesn’t at another. The firmware just uses the short boxcar and lives with what’s left. Next time I will look at the spectrum before picking a filter.
Transients
The equation above leaves out the inductance term, the voltage it takes to change the current. On a steady shaft the boxcar averages it away, and putting it back made things worse because the ripple’s slope comes in with it. Where it bites is right after torque-on or a direction flip, when the current hasn’t built yet and the estimate reads the applied voltage as a huge speed. The firmware just voids the first few samples after either one. I’m fine leaving a term out as long as I know exactly where it bites.
There’s also one I can’t explain yet. After a hard deceleration, with the current running backwards, the back-EMF reads well under the pot for a short while, and a commutation count says the pot is the one telling the truth there.
Fusion, Then a Switch
Meanwhile the firmware had already been through two ways of using the back-EMF.
The servo has a second speed source, the pot observer. Over a stretch of travel its speed averages out to what the shaft really did, but moment to moment a pot is a lousy speedometer.
The first version fused the two with inverse-variance weighting, the textbook way to combine two noisy measurements. That went badly. The back-EMF is so much quieter than a pot derivative that the weighting basically ignored the pot, so it was really the back-EMF with a rounding error stapled to it, and with the servo sitting still its speed swung all over the place. Inverse variance also assumes each sensor’s error is random with zero mean. The pot’s error is mostly locked to position, and the back-EMF’s is mostly an offset in level, so the math was answering a question my sensors weren’t asking.
The second version switched. The loop read the back-EMF when it was valid and the pot otherwise. At rest it went quiet, and I moved on happy for a while. But it was trusting the back-EMF’s level, and that came back to bite me.
The Brushes Wander
Up to this point I still believed the level error was a calibration problem. Find the right R, maybe add a brush term, and the level would line up with the pot.
In mid November I put the closed-loop captures next to the open-loop ones, and the back-EMF was reading over the pot in every single closed-loop row. The excess looks like an extra drop that follows the current, and it only shows up when the rotor turns. Hold the shaft still and the motor reads its normal resistance.
What really got me was the time of day. The same cruise, scored the same way, read an excess that drifted up through the night, peaked the next midday at a few times its lowest reading, and came back down by the evening. The best explanation I have is the state of the brushes on the commutator, and part 1 goes into why that contact is so hard to pin down. The drift is bigger than the whole speed of a slow crawl.
That changed how I think about the whole problem. If an error depends on a state the servo can’t sense, I don’t see how any constant can fix it. A correction tuned in the morning is wrong by the afternoon.
Reading in the Off Time
If I·R is the problem, the obvious way around it is to measure when there’s no current, like a coast does. So I looked at fast decay, where the bridge opens in every PWM off time, and reading the back-EMF there on every period.
It has no working point at this PWM rate on this motor. After the bridge opens, the current drains back through the diodes, the terminals ring, and the motor’s iron holds them a little off zero. They only show the true back-EMF a few PWM periods later, so a sample inside one off time never gets there. Fast decay got deleted from the firmware.
So the clean reading does exist. It’s just only available when you stop driving the motor for a while, and a servo in the middle of a move can’t afford that.
The Blend
By then the picture was pretty clear. There are two sensors that are good at opposite things. The pot observer is honest in the mean and slow. The back-EMF is quiet and fast, and its level drifts.
The switch had it backwards. It trusted the back-EMF’s level, so Velocity mode (where the host asks for a speed and the servo holds it) ran noticeably slow, and moves were full of drive reversals, the drive flipping direction over and over, basically buzzing.
But you can’t just use the pot either. At short delays the back-EMF is far quieter than the pot’s slope, and the pot only catches up after a long delay. A speed loop can’t wait that long. The velocity notebook scores both sources against delay if you want to see the crossing.
If you’ve used a complementary filter on an IMU (inertial measurement unit), you already know the answer. The gyro is fast and drifts, the accelerometer is slow and honest about the mean, and you take the fast part of one and the slow part of the other. Here the back-EMF is the gyro and the pot is the accelerometer.
So the blend splits the job. On every period with a valid boxcar, it takes the difference between the boxcar and the pot observer. A slow filter tracks the standing part of that difference, which is the back-EMF’s level error, and throws it away. What’s left is the change in speed the observer hasn’t caught up with yet, and that goes through a short lead and gets added on top of the observer’s speed.
The nice part is that the loop’s mean speed is always the observer’s, whatever the back-EMF’s level is doing that day, and with no boxcar at all the loop just reads the pot. Nothing has to know the brush state. The thing I couldn’t measure stopped mattering, which I like a lot better than trying to measure it.
A pot-only control image held speed about as well as the blend, but overshot way more on landings. That’s the fast part earning its keep.
Trying to Fix the Level Anyway
Of course I didn’t give up on the level that easily. Over the next few days I tried a bunch of things.
- A brush term in the back-EMF close. The brushes add volts that follow the current, so the speed loop partly closes on its own current. Subtracting that helps on a worked motor, but a size tuned on a worked motor collapsed the speed loop on a rested one. So the term is in the code and switched off, and a learner to adapt it went with it.
- Dither and a lock-in. It needed a per-motor offset, which defeats the point.
- Ripple counting at runtime. Great for identification on the host, but I don’t think it fits in the servo’s loop.
- A Kalman filter with the level error as a state. Way too heavy for a chip with no divide and no float. For the V006 it just needs to be good enough.
Funny enough, industrial DC drives have a knob for this called “IR compensation”, and it’s known to oscillate when set too high. That’s basically what mine did on a rested motor.
The rule I ended up with is to grade any fixed value in every state the servo can’t tell apart, rested and worked, and let the worst state decide. A rested servo may hold speed a little less tightly, and I can live with that. A collapsed loop I can’t.
Where It Landed
One last piece was the floor. The back-EMF estimate still had its old, higher floor, so a servo holding near its goal ran on the pot’s lagging speed alone and hunted back and forth. Lowering it to the taps’ own floor made hunting basically go away on the free horn.
So, can the servo observe its motor’s speed from back-EMF? As an absolute speed, not reliably, at least not on this motor. As a sensor for how the speed is changing right now, it’s really good, and that’s the job it has now.
The end result shows up best in Velocity mode. On the old switch, which trusted the back-EMF’s level, it ran 6 to 9% slow. On the blend it lands within about 2% of the commanded speed, and the buzzing on moves mostly went away with it.
Caveats
- One servo, one board, one bench. The brush wander belongs to this motor, and another MG90 could be better or worse.
- All the open-loop rows came from one week and all the closed-loop rows from the next, on different firmware images. So I can’t fully rule out something about closed-loop drive I didn’t find. A short capture with open-loop spins on both sides of one closed-loop cruise would settle it, and I haven’t run it yet.
- The blend isn’t free. The follow error on a slow ramp got a bit worse, and at top speed it overshoots a little more than the switch did.
Open Questions
- What the stopped rotor’s extra resistance is.
- Why the back-EMF reads low for a short while after a hard deceleration.
- Whether the commutation pattern comes from Ke or R changing with rotor angle. Under drive the two look the same.
- Whether a settled coast holds its level on a day the drive-window reading doesn’t. If it does, an occasional natural coast could pin the level. I’d still rather not inject coasts at runtime.
What’s Next
Part 3 covers the winding temperature that the firmware’s R comes from.
All the estimators and their scores are in the stage 4 notebooks, and the blend is written up in the “Which Velocity the Loop Closes On” section of the control doc. If you’ve made back-EMF speed work on a small brushed motor at low speed, or you know a trick for brush drop that holds from one day to the next, I’d love to hear it. Let me know your results / opinions / rants!