Arduino Forum

Forum 2005-2010 (read only) => Hardware => Interfacing => Topic started by: Aniss on Sep 17, 2009, 03:05 am

Title: How does a servo work (PWM or PPM)?
Post by: Aniss on Sep 17, 2009, 03:05 am
I'm trying to get to the bottom of how a servo works.

First of.. I wish to identify the different components of a standard servos. This is how I see it (please correct me if I'm wrong?):

1) A DC motor
2) Some gears
3) A potmeter
4) A motor controler (also reffered to as a speed controller?)

The motor controller receives a signal (in our case from the Arduino) which tells the servo to move to a specific position. It then reads the current position from the potmeter and determines how much and in which direction the DC motor should be moved and sends an appropiate signal to the DC motor.

Is this correct? Am I missing something? And what kind of signal is sent from the motor controller to the DC motor?


Second..There seems to be a GREAT deal of confusion about whether the signal received by the servo (or rather the motor controller) is PPM or PWM. The following states that it is controlled by PPM signals:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1232572239

Since the author of this comment (Mem) is the guy who wrote the latest Arduino servo library I'm naturally accepting him as an authority on the matter. However I started a topic on another forum because I'm trying to figure out different ways of sensing servo feedback. Here it is:

http://letsmakerobots.com/node/10960

Within that thread several seemingly knowledgable people assured me that a servo is indeed controlled by PWM. Here is a quote and a diagram posted in that thread:

Quote
PWM, with a fixed frequency of ~20ms.  A period of 1.5ms tells the servo circuitry "center".  Period of 0.5ms or 2.5ms sends the servo fully CW or CCW.


(http://bansky.net/blog_stuff/images/servo_pulse_width.png)

Here is a link to that particular comment:

http://letsmakerobots.com/node/10960#comment-32515

So.. now I really don't know WHAT to believe?!  :-/ Hope some of you smart fellas on this forum can help me clear this up once and for all...


Aniss1001

PS: If anyone has comments regarding the other topic (sensing servo feedback) they're more than welcome to post it here :)
Title: Re: How does a servo work (PWM or PPM)?
Post by: westfw on Sep 17, 2009, 06:41 am
I would call it "pulse width modulation", and I think mem's description in the message you reference describes a form of pulse width modulation.   The position of the servo is determined by the width of the "on" pulse.  If it were a tru PPM scheme, the on pulse would always be the same length, but they would be different times between pulses.  (I've read that the actual decoded radio signal is PPM.  I dunno.)

However, the "on" time of the pulse is severely limited compared to the PWM that the arduino hardware will normally generate.  If you consider the servo position to range from 0 to 100%, the pulse widths involved range from 0.5ms/20ms (2.5%) and 2.5/20 (12.5%) of the total pulse period.  (note that the pretty picture of servo pulses you mention is "not to scale."   The full right position (2.5ms) is actually only 1/10th of the total period width...)  All off (0%) and all on (100%) are not valid inputs.  (analogWrite will generate pulses between 0 and 100% for values of 0 to 255)

I don't know what the libraries do.   There are several possibilities:

1) set the period to ~20ms, and map the user-provided values to pulses of 0.5 to 2.5ms.  With an 8 bit counter, each count worth of pulse width is about 0.08s, so that's about 25 steps of resolution for the 2ms range of interest.  That's not TOO bad.  It would work better on the 16-bit counter, if there's a 16bit PWM mode.

2) Fire the timer with the max period set to ~2.5ms (about 0.01s per count) and the count set to between 50 and 250 (200 steps; plenty)  Arrange for this firing to be repeated at appropriate intervals via pushing the responsibility to the user (refresh() function, as I've seen in some servo libraries), or using interrupts tied to some other timer (even the same timer.)
Title: Re: How does a servo work (PWM or PPM)?
Post by: westfw on Sep 17, 2009, 07:40 am
So here are some "scale" pictures.  First, a single servo signal:

(http://farm3.static.flickr.com/2637/3927527975_17112da352.jpg) (http://www.flickr.com/photos/58843278@N00/3927527975/in/set-72157601072004330/)


The large gaps allow multiple servo signals to be multiplexed on a single communications link (eg the radio signal of a radio control set.)  You only have to send one actual pulse at any one time, and you have enough room to send about 10 servo signals.  I'm pretty sure that this is how the libraries that support very large numbers of servos on Arduino (or similar micro) work.  For each timer the chip has, you can easily control up to 10 servos.  Since the ATmega168 (and 328) have three (or even 6, depending on how you count), you can control more servos than there are pins.  (5 channels are drawn here.)

(http://farm3.static.flickr.com/2461/3927527999_23436c6c29.jpg) (http://www.flickr.com/photos/58843278@N00/3927527999/in/set-72157601072004330/)
Title: Re: How does a servo work (PWM or PPM)?
Post by: Aniss on Sep 17, 2009, 08:07 am
Hi..And thanks for the reply :)

I believe I do understand what PWM (pulse width modulation) and PPM (pulse position modulation) means and how they respectively work.

In short...

PWM: ..has a fixed frequency (for servos: ~20 ms) and variable on-pulse (for servos: 0.5-2.5 ms).  The information is contained in the width of the on-pulse.

(http://homepage.ntlworld.com/g.knott/width.gif)

PPM: ..has a fixed length on-pulse that arrives at different times, referenced to a fixed frequency. How far ahead or behind the fixed time is what is important.

(http://homepage.ntlworld.com/g.knott/position.gif)

And you're right..the diagrams posted by Mem does indeed seem like PWM. So it confuses me that he says (directly following the diagrams):

Quote
This is called Pulse Position Modulation and is not the same as PWM as used in the Arduino analogWrite. PWM (Pulse Width Modulation) varies the ratio of on time to off time to vary the overall signal level and are not suitable for driving a hobby servo.


...And that he starts off with:

Quote
You should not use the arduino analogWrite PWM function to drive a hobby servo. They do not use PWM and could damage a servo or speed controller. Its confusing because many references do incorrectly use the term PWM in articles about servos.  But hobby servos and speed controllers expect different pulse timings from that provided by analogWrite.


The latter could mean that the Arduino analogWrite PWM works of a different frequency than the PWM used by servos...but I dunno...
Title: Re: How does a servo work (PWM or PPM)?
Post by: Aniss on Sep 17, 2009, 08:10 am
Ah..it would seem we were writing simultaniously. I was just very slow :)
Title: Re: How does a servo work (PWM or PPM)?
Post by: Aniss on Sep 17, 2009, 08:26 am
And thanks again for the info, westfw

The thing about the timers was all news to me. And it may actually prove to be very useful knowledge...
Title: Re: How does a servo work (PWM or PPM)?
Post by: mem on Sep 17, 2009, 10:50 am
Although it usually doesn't make a lot of  difference what something is called, I have been campaigning for years on this forum to avoid calling the servo pulses PWM because analogWrite is the way Arduino produces PWM and driving a servo using Arduino analogWrite can destroy servos.

PWM involves the modulation of a signals duty cycle.  In controlling a servo, the duty cycle does not control the position of a servo. Servo position is controlled through modulating the on time of the pulse. Indeed it is possible to control a servo over its full range of movement by changing the on time but keeping the duty cycle constant. If servo control works perfectly well without changing the duty cycle then PWM modulation would not be the most appropriate term for the modulation used.

The terminology is confusing because servo control pulses do involve changing (modulating)  the pulse width. And a subset of PWM can be used to emulate the modulation needed to control a servo.  But its no more correct to say that this makes the servo signal PWM than it would be to say that an AC (Alternating Current) signal is PWM because PWM is capable of  alternating (switching on and off)  current. Indeed it is possible to emulate AC using PWM (along with a few passive components) but of course that doesn't mean one should say that AC is PWM modulation.

Choosing the correct terminology for servo signals is complicated by the fact that many smart people (like westfw) do use the term PWM when talking about servos.  But the term PWM for servo modulation can confuse people (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1249178408) into thinking that analogWrite is suitable for driving servos, so what should it be called?

It makes sense to use the same termonology used by the people designing and manufacturing RC electronics. And the major manufactures of RC equipment describe this modulation as PPM:
http://www.futaba-rc.com/faq/product-faq.html#q1
http://www.hitecrcd.com/product/comparison_chart/26/Optic_Radio_Chart.pdf
http://www.spektrumrc.com/DSM/Technology/glossary.aspx

Avoiding references to PWM controlling servos should help reduce confusion with the output from analogWrite, so I will continue to evangelize the use of PPM to refer to servo signals.
Title: Re: How does a servo work (PWM or PPM)?
Post by: Aniss on Sep 18, 2009, 12:23 am
Thanks a lot for the explanation. I was in fact hoping to get a comment from you.

Your explanation is a bit tecnical so I'll need a bit time to digest it. So just one comment:

You said:

Quote
Although it usually doesn't make a lot of difference what something is called..


I must strongly disagree. Establishing a meaningful and COMMON nomenclature is allways a must for being able to communicate properly. Otherwise we end up where: you say tomato - I hear potato :)


Aniss
Title: Re: How does a servo work (PWM or PPM)?
Post by: Aniss on Sep 18, 2009, 12:26 am
So for now let's consider the PPM/PWM matter closed.

However I'd still very much appreciate some input on the other part:

Quote
First of.. I wish to identify the different components of a standard servos. This is how I see it (please correct me if I'm wrong?):

1) A DC motor
2) Some gears
3) A potmeter
4) A motor controler (also reffered to as a speed controller?)

The motor controller receives a signal (in our case from the Arduino) which tells the servo to move to a specific position. It then reads the current position from the potmeter and determines how much and in which direction the DC motor should be moved and sends an appropiate signal to the DC motor.

Is this correct? Am I missing something? And what kind of signal is sent from the motor controller to the DC motor?
Title: Re: How does a servo work (PWM or PPM)?
Post by: retrolefty on Sep 18, 2009, 07:12 am
Quote
Avoiding references to PWM controlling servos should help reduce confusion with the output from analogWrite, so I will continue to evangelize the use of PPM to refer to servo signals.


And after that battle is won (if ever), maybe we can take on my next biggest complaint with Arduino vocabulary. Why in the world did someone chose the word analogWrite for the function that outputs a PWM output signal? Why was it not properly called pwmWrite?

Yes I know if one, using external components, low pass filters a PWM output signal, one can extract a analog voltage, but I'm sure the confusion and misunderstanding for newcomers to the Arduino world, often just learning hardware and software fundamentals, makes for additional road bumps in their learning curve.

No, Joe, it doesn't really output an analog voltage, but it kind of can, if you do some stuff and don't think too hard about it.  ;)

I recommend that for version 18 they announce that they have decided to eliminate the analogWrite command, but also announce that they will then replace it with a new and improved pwmWrite command.  ;)

Lefty
Title: Re: How does a servo work (PWM or PPM)?
Post by: mem on Sep 18, 2009, 07:39 am
Hi Lefty,

I think there is a good case for keeping the name analogWrite even though pwmWrite would be more technically correct.

Arduino is primarily aimed at non technical users - people that want to do things with Arduino but are not engineers and are much more interested in the ends than the means.

For tasks like varying the intensity of an LED or moving the pointer of an analog meter, a function named analogWrite is more expressive than the more technically correct term.
Title: Re: How does a servo work (PWM or PPM)?
Post by: mem on Sep 18, 2009, 07:45 am
Quote
I must strongly disagree. Establishing a meaningful and COMMON nomenclature is always a must for being able to communicate properly

Aniss, my point was that it is usually the case that understanding something is much more important than knowing its correct name.

Quote
However I'd still very much appreciate some input on the other part:

I would amend your summary on servos as follows:
4) A motor controller (typically an H-bridge)
5) a servo feedback circuit that reads the output position from the pot and drives the motor controller to correct deviation from the commanded position.
Title: Re: How does a servo work (PWM or PPM)?
Post by: westfw on Sep 18, 2009, 09:22 am
Mem and I may have to agree to disagree on the PPM vs PWM debate.  I'm pretty sure the PPM references he supplies are talking about the radio side of things, which is not quite the same as the servo side.
I see the servo signal and "duty cycle modulation" as two different sub-cases of PWM, with PPM definitely involving a fixed-width pulse.

Quote
1) A DC motor
2) Some gears
3) A potmeter
4) A motor controler (also reffered to as a speed controller?)

The motor controller receives a signal (in our case from the Arduino) which tells the servo to move to a specific position. It then reads the current position from the potmeter and determines how much and in which direction the DC motor should be moved and sends an appropiate signal to the DC motor.

That sounds pretty close to me.  A "speed controller" in the usual sense is a different beast.  While some servos may vary the speed of the motor based on the amount of "error" from the desired position, most just generate a (nearly digital) "direction" signal (sign bit of the error, sort of.)  that makes the power transistors in the H bridge have an easier job.  (hmm.  The newer "digital" servos may do a better job here, perhaps even generating PWM to the motor terminals to vary speed.  But using transistors in linear analog mode to control motor speed is HARD.)

There was an old linear IC that used to be used in the guts of many servos, and the datasheet had a really good explanation of how the error signal was generated and used to control a motor.  But I don't remember the part number, can't find it even in my historical databooks, and web searches are hopeless polluted by explanations of how to generate/use the PWM/PPM signal we've been talking about.
Title: Re: How does a servo work (PWM or PPM)?
Post by: AWOL on Sep 18, 2009, 09:37 am
Quote
I'm pretty sure the PPM references he supplies are talking about the radio side of things, which is not quite the same as the servo side

They're exactly the same - in the Old Days an R/C receiver's decoder was simply a TTL counter, with the serial pulse stream from the RF connected to the counter's input and an RC circuit attached to the reset pin.
The servo PPM control pulses from the transmitter were sent sequentially in a frame with a long inter-frame gap; the gap was long enough for the RC circuit to trigger the counter's reset and so resync the link.
The output pins of the counter were connected directly to the servo control pins.
Title: Re: How does a servo work (PWM or PPM)?
Post by: retrolefty on Sep 18, 2009, 09:48 am
Quote
There was an old linear IC that used to be used in the guts of many servos, and the datasheet had a really good explanation of how the error signal was generated and used to control a motor.  But I don't remember the part number, can't find it even in my historical databooks, and web searches are hopeless polluted by explanations of how to generate/use the PWM/PPM signal we've been talking about.


I think your talking about the Signetics NE544 chip.

Here is a article that has some of the data sheet info for the NE544 embedded.

http://www.seattlerobotics.org/encoder/200009/Servos.html

Put me down for calling it PPM  8-)

Lefty
Title: Re: How does a servo work (PWM or PPM)?
Post by: Aniss on Sep 18, 2009, 11:00 am
Hi Mem,

Point understood!

However I've been programming for many years (for fun and professionally) but frankly 6-8 weeks ago I barely knew what a circuit, microcontroller or a servo was. And it's been a very slow and painful process learning about these things. Not least because people use different terms for the same concept OR the same term for different concepts.

In that sense I very much agree with lefty that "analogWrite" should be called "pwmWrite". Simply because that's more correct but MAINLY because the term "analog" is allready reserved for an entirely different concept. The current mixup made it a lot harder (for me atleast) to understand the whole ordeal.

Besides even if 2 persons both understand a given concept it's still difficult to talk about it if they don't share a common nomenclature. If working as a programmer/systems developer has taught me anything it's that nomenclature is important, if not a must, for meaningful communication..

Hope you see my point too :)


Thanks again for your input

Aniss
Title: Re: How does a servo work (PWM or PPM)?
Post by: Aniss on Sep 18, 2009, 11:26 am
And just one more noob question...

The "servo feedback circuit" (that Mem mentioned) is that the same as/another term for/related to a PID loop/controller?


PS: Regarding PPM vs. PWM: I think I'll just reffer to it as SERVO SIGNALS from now on.. as a guy on the other forum suggested :D
Title: Re: How does a servo work (PWM or PPM)?
Post by: mem on Sep 18, 2009, 11:36 am
Quote
Hope you see my point too

I do see your point about using correct termonology, and if Arduino was primarily aimed at programmers/system developers then I would wholeheartedly agree.

But arduino is a platform intended for artists, designers and hobbyists. And although it is also highly attractive to trained software engineers like you and me, we should to be tolerant to the bending of some technical terms in the interests of making the technology more accessible to non-technical people.

I also take the point about potential confusion over the differences between analog inputs and analog outputs. But whatever you call the output function, the differences between changing the intensity of an LED and reading the intensity of a light source still needs to be adequately explained - things don't get any clearer to the non-technical person by renaming the function pwmWrite.

I can see many reasons why the analogWrite should not be changed to pwmWrite:
1. (as mentioned in an earlier post) analogWrite is more expressive of the affect when driving things like LEDs. PWM has little or no meaning to non-technical people.

2.  It would increase the likelihood for confusion over its suitability for driving servos (particularly if people persist in referring to servo control signals as PWM).

3. Even if there was a better name for analogWrite, so much material refers to that name (25,000 hits on google) that changing it would create considerable consternation.
Title: Re: How does a servo work (PWM or PPM)?
Post by: westfw on Sep 18, 2009, 11:40 pm
On further thought, I think the main problem with PWM vs PPM is with the "M"; properly speaking, the servo control signal is not "modulated", it is merely "encoded."   Especially as used by microcontrollers.
Title: Re: How does a servo work (PWM or PPM)?
Post by: mem on Sep 19, 2009, 09:58 am
westfw,  if you want the most technically correct term and I want the term that causes the least confusion among Arduino users, can we at least agree not to use PWM in relation to servo encoding signals on the  grounds that the encoding is not based on modulating the proportion of on time to off time and has nothing to do with the PWM output from analogWrite and the current Arduino servo library does not use hardware PWM to produce the servo pulses.
Title: Re: How does a servo work (PWM or PPM)?
Post by: Aniss on Sep 19, 2009, 11:18 am
Hi again Mem,

Just to round of the discussion..

Had some time to research the concepts "duty cycle" and "modulation" and I finally understood your explanation:

www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1253149521/6#6

I see you have a point that PWM may not be the appropriate term for describing servo signals/pulses. However I still don't see that PPM is any less wrong (as I've understood PPM). But let's leave it at that. I learned my lesson and it would seem that most people (on other forums) all agree to call it PWM anyway. Personally I'll call it servo signals/pulses from now on :)

And you're right that it would cause too much confusion changing the name "analogWrite" since it's allready well established. But I still think it was a bad idea to call it that to begin with. And I don't think it's helping non-tecnical users to (mis-)label it "analog". For several reasons:

- On the Arduino I/O's it still says PWM.
- Non-tecnical people mostly don't really know what "analog" means anyway.
- Even non-tecnical users may one day want/need to understand a bit more about how the "black box" (=Arduino) works. I suspect they (like me) may be confused by the (mis-)labeling.

So.. since "analog" is wrong and PWM is too tecnical perhaps "virtualAnalog" (or something like that) would have been the best choice. But it is off course irrelevant cause it's too late to change it.


Thanks for helping me better understand how a servo works (TO ALL). Now I'll move on with my endlessly expanding research todo list and read up on newly added concepts like: servo feedback circuit, H-bridge and PID-loop/controller...


Best wishes

Aniss
Title: Re: How does a servo work (PWM or PPM)?
Post by: retrolefty on Sep 19, 2009, 04:27 pm
I know it's a dead horse and that there is no way the Arduino platform would ever change the wording from analogWrite() to pwmWrite().

However it's just not right and can't be helpful to newcomers to have two commands analogWrite() and analogRead() that have drastically different electrical characteristics. It isn't so difficult to learn for someone just starting to program, as much as if they are also new to electronics fundementals.

Now, where can I obtain a new horse.  ;)

Lefty
Title: Re: How does a servo work (PWM or PPM)?
Post by: clifdweller on Sep 19, 2009, 07:17 pm
To be clear
- PPM (pulse position Modulation) is a method of controlling multiple servos.
- PWM( pulse width modulation) is the method by which a single servo is controlled.

-ppm is used to divide the clock of a timer into chunks that are used for each servo. Within each of those chunks of time you vary the time of an on pulse(pulse width) to tell that servo where to move.


-PWM is an incorrect term because you don't have to send a constant signal to a servo. I can send a 2.0ms pulse and then wait ten seconds before sending the next pulse and it won't hurt the servo. After a certain amount of time a servo just shuts off until i send it another signal. If i want it to stay on i have to send it pulses every "x" seconds which for rc servos happens to be 20 ms. If i do send signals every 20 ms then i am using PWM but if i send the signals at different times it isn't.

-ppm is absolutely incorrect terminology for controlling a single servo. It describes how to control when servo signals are sent when multiple servos are used.

-- The correct terminology then would be a PWM signal at 50HZ(20ms) with pulse widths varying from .5 - 2.0 ms controls a single servo and keeps it turned on. <-- this seems a little complex so i'll stick with PWM and analogwrite for now
Title: Re: How does a servo work (PWM or PPM)?
Post by: retrolefty on Sep 19, 2009, 11:06 pm
One issue with your posting:

Quote
I can send a 2.0ms pulse and then wait ten seconds before sending the next pulse and it won't hurt the servo.


This is not how R/C type servos are designed to operate, they are not designed to be shutoff between normal 20ms updating of pulses. You will find that once you stop sending pulses to a servo it loses all 'holding' torque and if you had a heavy enough torque load on the servo horn the position could change without any correction until another pulse is sent.

Without power or a continuous 50hz signal update the only holding force is the servos gear train resistance relative to the 'live load' force.

Lefty
Title: Re: How does a servo work (PWM or PPM)?
Post by: mem on Sep 19, 2009, 11:55 pm
Quote
To be clear ? PWM( pulse width modulation) is the method by which a single servo is controlled.

"PWM of a signal or power source involves the modulation of its duty cycle" ( http://en.wikipedia.org/wiki/Pulse-width_modulation )
Are you aware that a servo can be operated correctly over its full range without altering the duty cycle of the control signal?
If servo control can be achieved without modulating the duty cycle then PWM would seem a poor choice for describing its encoding.

Perhaps this discussion should be continued in bar sport, I think  this thread is losing its relavance to hardware interfacing  ;)
Title: Re: How does a servo work (PWM or PPM)?
Post by: Aniss on Sep 20, 2009, 01:55 am
He he..this PWM/PPM discussion just refuses to die. As much as I'm glad I posted the topic (I needed to understand it better) I'm kinda regretting that I posted it together with my OTHER general servo questions. These seem to be lost in the PWM/PPM ordeal.

For instance I'm still trying to figure out this:

Quote
what kind of signal is sent from the motor controller to the DC motor?


Is it an analog signal (=the voltage being regulated up and down), PWM or a 3rd option that the servo's motor controller uses to control the DC motor?
Title: Re: How does a servo work (PWM or PPM)?
Post by: mem on Sep 20, 2009, 02:11 am
Most of the low cost (analog) servos I have looked at drive the motor with pulses that are (more or less) proportional to the error (the difference between the current physical position and the commanded position) with the polarity as needed to move the motor in the direction that corrects the error.  Motor pulses are usually only generated immediately following a command pulse.
Title: Re: How does a servo work (PWM or PPM)?
Post by: retrolefty on Sep 20, 2009, 02:50 am
In the prior posting I showed a link that discusses a popular servo controller chip that was used for decades internally for many hobby servos:    http://www.seattlerobotics.org/encoder/200009/Servos.html

While no longer being used, I'm sure most of the inexpensive servos are still using the same general design just with smaller chips.

In figure 1 of the embedded data shows the motor connection on the right hand side. It is being driven by a H-drive bridge. The artical discusses the operation of the servo controller in some detail.

Lefty