back emf and adc

Asimov

Member
What i was planning on doing was driving a DC motor via a L293D with PWM from a picaxe chip. Then, using a bridge rectifier with 4 1N4001 diodes to ensure polarity is fixed and two resistors forming a voltage divider connected across the motor terminals I could obtain a value 0-255 via an ADC pin. Using a lookup table I could then vary the motor output to compensate for load. Does that sound plausible?
 

BeanieBots

Moderator
If you know the motor DC resistance and the current through it, then the back emf is the voltage across it minus IR.
Much easier to measure and implement. (just requires a current sense resistor).
Look up "IR speed compensation".

All you need to do for load compensation is add a bit more drive proportional to the current drawn.
If you have the gain over unity, it will oscillate violently!
 

Ravenous

Member
What i was planning on doing was driving a DC motor via a L293D with PWM from a picaxe chip. Then, using a bridge rectifier with 4 1N4001 diodes to ensure polarity is fixed and two resistors forming a voltage divider connected across the motor terminals I could obtain a value 0-255 via an ADC pin. Using a lookup table I could then vary the motor output to compensate for load. Does that sound plausible?
To answer your question, I've never attempted it but a few years ago I did find some pages on the web where people tried it. I was always planning to have a go (I have a lot of old motors that can't be fitted with proper encoders.)

One was looking for spikes caused by the motor's commutator rings, but I guess that won't work at all if you're driving with PWM.

The other was doing more what you describe. For a small fraction of the time, the motor drive was turned off and a diode (and possibly capacitor) arrangement was used to measure the DC being generated by the dynamo effect. Required pretty precise timing and I'm not sure if it works with a motor that can be run in either direction. Probably worth having a big search...
 

Asimov

Member
Many thanks for all the replies.

I rigged up what I proposed today and had some success. However, the value at ADC Varied too widely to be useful. I put a 10k resistor from the voltage divider to ADC with a 470uF cap across the 10k resistor. Might try using a greater value cap. Failing that, back to the drawing board.
 

Ravenous

Member
I'd beware of trying to use too large a capacitor. I think it may retain a charge from the motor driver circuits, meaning a misleading reading.

When I googled around about this yesterday I noticed a comment or two about this being the old way to regulate speed of tape players. Makes me think it should be easier than I'd previously thought...
 

BeanieBots

Moderator
... I noticed a comment or two about this being the old way to regulate speed of tape players. Makes me think it should be easier than I'd previously thought...
It's been used for years and still is, as described post #4.
Measure the current and then add a bit more drive proportional to that current (to compensate for the armature IR loss). It really is that simple.

No need to sync your ADC with PWM and filter out brush noise/spikes or any of that nonsense. Just measure the current (by whatever method you prefer, series resistor is simple) and act on what you read. The rest is good old Ohm's Law.

A perfect motor will run at a speed proportional to the voltage no matter what the load is.
An imperfect motor has IR loss. Compensate for that loss and you get back to a pefect motor.
 

Asimov

Member
BeanieBots, that sounds like a straight forward approach. However, I can't get my head around how the picaxe chip can read the voltage difference in order to compensate the PWM duty..
 

BeanieBots

Moderator
You don't measure the back emf, you calculate it from the voltage across the motor and the current through it.
The voltage is the supply times the duty%.
If the DC resistance of the motor is 0.5 Ohms, then for every 2 Amps, you "lose" 1 volt.
So, for every Amp of measured current, if you increase the duty to give an extra 1/2 volt, the speed of your motor will remain the same.
If you want to know the speed then reverse the equation. (you know the duty and the current so you can calculate back emf)
If you are using an H-Bridge, then measure the current going in the bridge. It will be the same as the motor but always positive.
 

Asimov

Member
BB, I understand how one can calculate the voltage using a current sense resistor and how to obtain the resistance of the motor etc, but how do incorporate all of this into the picaxe so that it can vary the PWM duty accordingly ?
 

BeanieBots

Moderator
As I described in post #12.
Add the amount of PWM duty required to compensate for the voltage lost due to IR.
To be any more specific would require the exact nature of the motor, power supply and desired speed.
In practice, you can get there very quickly by experimentation.
Measure the current multiply by a factor (K) and add that value to the PWM duty.
Keep increasing K until you get oscillations, then back it off a small amount. Job done!
(in practice, if you have a decent current range for the ADC, K will actually be less than 1 ie you will need to DIVIDE the current by a value to get the correct compensation).

Duty = duty required for desired off load speed.
I = value read from current ADC
Duty = Duty + (I*K)
 

Asimov

Member
BB, its not a case of a one off compensation. I have built and model train decoder operated by remote control via ERF module. It works a treat!! However, the only component I am having trouble incorporating is speed compensation. The speed situation is quite dynamic, compensation might be for instance, train under load climbing an incline, or even just overcoming internal friction within the train workings themselves. Supply to the train is via the rails at 16V, this is regulated to 5v for the picaxe, ERF module and L293D. Around 30% duty would be tops running the train at realistic speeds.
 

BeanieBots

Moderator
Not sure I follow your concern/confusion....
Set a PWM duty and the motor will go at that speed.
Load the motor and it will slow down.
Add in the amount of extra duty to maintain speed and the train will run at constant speed.
The amount to add is proportional to the current being pulled by the motor.

If the train goes up hill, the current will increase and it will slow down (if not compensated)
As the current increases, so does the compensation.....
Net result, CONSTANT SPEED irrespective of load.

Maybe it's me missing what you are really trying to do?
IR comp is so that you can tell the train what speed you want and it will go at that speed irrespective of how many coaches it is pulling or what incline it is going up.
Is that not what you are trying to do?
 

Asimov

Member
Precisely. I understand the theory behind IR comp, but cant get my head around implementing it. Lets say I include a resistor after the L293D, I can see from that how I might determine the voltage drop across the motor to determine the load. I am stuck from incorporating the resistor....

Am I correct in saying that the PWM duty will vary dependent on the ADC value, if so, the ADC will not be stable due to PWM.

I'm either missing something very fundamental or I'm at the limits of my electrical knowledge.

P.S. I am very grateful for your assistance.
 

BeanieBots

Moderator
You are correct about the ADC and PWM issue.
You would need to filter the current measurement to give a steady ADC value.
Also, have a look at this thread for more ideas on measuring current.
http://www.picaxeforum.co.uk/showthread.php?25633-Displaying-volts-and-amps-via-18M2-and-LCD

The exact filter will depend on what PWM frequency you go for but do not over-do it or there will be a delay between measurment and compensation which can also lead to oscaillation. I tend to use a second order filter:-
10k - 100nF - 10k - 100nF works well for most PICAXE/motor PWM designs.

You say 30% gives about the right speed.
If you are using 255 for period that would be about 300 for duty.
Let's say running along the flat it pulls about 1A and for that current you get an ADC reading of 100.
When it goes up hill, let's say the current increases to 1.5A (so the ADC will now read 150).
Also, the speed has gone down a little.

If we just add the ADC value, it will be far too much.
We want to get roughly the same (namely 300) for the 1A load but a bit more for the 1.5A load.
This will require a fair bit of trial and error but a good starting point would be about 10% compensation.

So, to get the same speed but now with compensation.
Duty = 270 (300 - 10%)
ADC returns a value close to 100
Scale the ADC reading to get roughly the 10% we took away.
ADC = ADC/3 (gives about 33)
Call this Comp
Duty = Duty + Comp = 303 (back to original duty for original speed)
Now, the load increases to 1.5A and the speed drops a little.
ADC = ADC/3 now gives about 50
Duty = Duty + Comp now gives about 350 (instead of 303) because the current (load) has increased.
Hope that helps.
 

Asimov

Member
Cheers BB.

I was using Livewire like the user in the thread you directed me too. It's great for knocking up circuits in theory, but it was getting a little frustrating not getting the results I wanted. I will take your advice and take a practical approach and see what I can achieve through trial and error. If I can just achieve a stable ADC value that relates to the motor load I can find my way home.

The beauty in making my own controllers is that I can get the best out of the models from a realistic running point of view. No two motors will ever perform entirely the same and being able to tweak them through hardware and software is a real bonus over commercially available controllers. Apart from the ERF module they are really cheep to put together, inertia start and stop is easily programmed as are most features commercially available. Just got to get over speed comp.... :)

Thanks again.
 

Asimov

Member
Thanks BB.

Out of curiosity, what do you consider low frequency? I was using 5KHz and the noise from the motors was acceptable. But as I am aiming for realistic running, if you say low speed is better at lower frequencies, then I will experiment with lower frequencies.

BTW, that article you referred me to is quite interesting.
 

BeanieBots

Moderator
... what do you consider low frequency? ....
One of the problems when running at low speed (and hence low torque) is sticktion.
ie a certain amount of torque must be applied before anything moves at all and then suddenly shoots off once it does start to move.

To overcome this, apply a large enough torque to make it move but only for a short time. The result is a gauranteed movement but only for a short distance. Do this in small steps frequently and the result (on average) is a net slow speed.
This is a similar principle to that of regular PWM drive for voltage but at a low enough frequency such that each pulse actually causes movement in its own right.
What frequency depends on the entire mechanics being driven but for something like a model train I would expect something in the region of several hundred Herts would do the job. Again, experimentation is required. A good way to determine a value is to use pulsout with a long delay between (maybe 1 second) to find the smallest pulse that will result in net movement. Then use that to determine the frequency that will give net movement for the smallest duty.

The downside (there always is one) is that it puts a lot of mechanical strain on the gear set. Another choice you need to make.
 

Asimov

Member
Am I correct in saying that once I have established the shortest pulse, taking the reciprocal of that value will give me the frequency?
 

BeanieBots

Moderator
Am I correct in saying that once I have established the shortest pulse, taking the reciprocal of that value will give me the frequency?
No, it depends on what your delay between pulses was and what resolution duty you will be running at.
If you draw it out on paper it should become obvious what you need to do.
Off hand, I'm not sure what frequency PWM will go down to at 'standard' clock speeds but you might need to investigate under clocking as well. Do a few tests and see what sort of values you come with and also if the 'pulsing' (and associated noise) is acceptable or not.
 

Asimov

Member
Incredible!!!

At 63Hz I got the locomotive motor turning without hesitation at less than 2% duty. It took 3 minutes and 52 seconds for the wheels to do a complete revolution (not that you would run that slow in reality) but the scope is remarkable.

As for noise, stand two meters away and you can just detect some gearbox noise, motor noise is fine.

Going to experiment with some different motors.

BB, glad we had this chat ;)
 

BeanieBots

Moderator
Glad I could help.
Please bear in mind that you are now effectively starting and stopping 63 times a second.
This could have serious consequences on the life of the gearbox. I'd hate to be responsible for premature failure of your gears!
Perhaps leave an old unit moving at that pace for a few days and review the effect on backlash etc before subjecting your prized stock to such punishment.
You can minimise the jerk by adding a small cap. (SMALL, we don't want to smooth out the PWM, only reduce dv/dt).
It will also help to reduce RFI which will help increase the range of the radio.
Did you get the IR comp stuff working?
 

Asimov

Member
I haven't taken the IR comp any further. When a particular loco runs in one direction it hesitated a little at a certain point, running at the low frequency seemed to have ironed that out. It was that hesitation I was trying to solve with IR comp. As for inclines etc, I guess that's going to be down to the driver :), adds a bit more realism I guess. The two locos are experimental ones I purchased on eBay. They both have different motors, one (the one that's runs nice and slow) has a worm gear drive setup and still has the factory grease. I had a Hornby decoder fitted to the same chassis last year and the gear noise was horrendous, certainly quitter with my decoder. There is already a small cap fitted across the motor, for RF suppression I think. I have no interference issues with the ERF module, in fact, I can control the motor through three floors of the house!! Would be ideal with someone who has a garden railway.

Just got to keep up the R&D now until I am happy with the running :)
 

BeanieBots

Moderator
I have several friends who are into model trains.
One of the first things they do (even with brand new locos) is to swap out the 3-pole motor for a 5 or even 7-pole unit.
This gives much improved slow speed running.
With improved PWM and good motors, IR comp probably won't gain much.
Something else I forgot to mention, IR comp becomes less effective the slower you go.
I suspect that low frequency PWM will be all you need for reliable slow running. (plus good clean rails and pickups).
Keep us updated on progress.
 
Top