RC Servo Basics
Autor: lolpes | Publicado: 2011-03-07T17:14:00-08:00

In this post I'll talk about Servos.What is a servo? It is an electric motor with some gears and a control circuit, whose position you can control, within a certain amplitude. Here i'll use a RC Servo, this one: LINK.This is a nice post about RC Servos (very nice images): LINK.The electric motor, in this case a DC motor, is what gives the thing the ability to actually move. But generally these motors are weak, that is to say they give you little torque. Torque is force times distance, Wikipedia: LINK. But they are fast, they rotate fast. That is where the gears comes in action. Power is equal to torque times angular speed. So, the gears diminish the angular speed and increases the torque of the output, Wikipedia: LINK.The ability to move alone can't do much. If you want position control, you need to know where you are. This is usually done with a potentiometer connected to the output shaft of the gearbox. Its resistance, and therefore the voltage level seem by the controller will be proportional to the actual angular position of the output shaft.Having the ability to move and the position feedback leaves you needing only a control algorithm that will tell the motor to move the output shaft to the desired position. As this is the complex part, I'll make a post about it in the future.So, basically, the servo has these three ingredients. To use one, you need only to power it and tell it the desired position. So, how do you command it?To command a RC Servo you send it a PWM signal, whose duty cycle is proportional to the position you want (the difference between a analog and a digital servo is that the later has a much higher frequency of the input pwm.PWM stands for Pulse Width Modulation, Wikipedia: LINK. It's a digital signal (it has high or low value) that takes the form of a train of pulses, with a determined period and a duty cycle, that is the percentage of the period in which the signal is high.In the case of our rc analog servo, the period is around 20ms (which gives us a 50Hz frequency), with a useful duty cycle between 525 us and 2325us, more or less ( a pulse of 525us would give a 0° angular position on the output shaft whereas one of 2325us, a 180°, with values between these two would give a proportional angular position. You can play with these values to see what is the better combination.So, the program for this post simply consists of generating this PWM signal using the timer0 and two buttons to increase or decrease the position.As you can see in the code, it is made to control only one servo, but can easily be modified to control more servos (see the ~70 instruction cycles delay, that can be partially substituted by code to check for the pulse duration of others servos). One thing to note is that the duty cycle resolution that the servo sees is around 5us, but the (in my case) amplitude of the duty cycle is near 2ms, so i need to correctly check the timing of the duty cycle control algorithm to give the result that i want.And a detail for the Proteus simulation. It's "motor-pwmservo" is controlled by a duty cycle between 1ms and 2ms.Also, another thing about the Proteus simulation. I just noticed i had not configured the frequency of the pic18 to 40mhz. It's 20mMhz there. When I tried to change to 40Mhz, the LCD stopped working :S . Well, I couldn't fix it, but what matters is that in the real life it works.There is a video for the working thing here: LINK. Again, sorry for the poor quality.The files are here: LINK.One word about the so called full rotation servos: they in fact offer speed control, not position. But of course, we are here talking about hobby servos. Industrial level ones are much more precise and robust. See this servo controller for a robust modification of a hobby servo: LINK.