Society of Robots - Robot Forum

Software => Software => Topic started by: maverick monk on June 26, 2008, 03:47:43 PM

Title: Servo control?
Post by: maverick monk on June 26, 2008, 03:47:43 PM
Hey guys, im back, and more confused than before. I still dont understand how to do servo control, Ive read the tutorials, and looked at the source code, but Im not good enough with c to be able to understand what does what. Can anyone help me?
Title: Re: Servo control?
Post by: Admin on July 01, 2008, 08:38:13 PM
What do you have coded so far? I mean, I'm sure you attempted to reverse engineer the $50 Robot code, no? :P
Title: Re: Servo control?
Post by: maverick monk on September 09, 2008, 03:30:04 PM
Sorry for the late reply, my computer went on the frits and ive just recentlystarted taking a look at this stuff again, yes i have tried, and failed, I just dont understand how it works
Title: Re: Servo control?
Post by: izua on September 10, 2008, 02:35:32 AM
You have a pulse. This pulse is repetead 50 times per second. The width of the pulse determines the servo's horn angle.
Check this (http://www.izua.ro/tools/servo_calculator) for an animation.
Title: Re: Servo control?
Post by: maverick monk on September 14, 2008, 01:20:25 PM
so in code it would be like this for forward?

bring pin high
wait(250)
bring pin low
wait(1)
bring pin high
wait(250)
bring pin low
wait (1)
Title: Re: Servo control?
Post by: Ro-Bot-X on September 14, 2008, 06:50:22 PM
Ok, a servo pulse has a high time and a low time, totalling 20 miliseconds. The high time duration controlls the servo horn position. A 1 ms high time will make the horn go to 0 degrees (left), a 1.5 ms high time will make it go to 90 degrees (middle) and a 2 ms high time will make it go to 180 degrees (right). The low time will make the difference to the 20 ms period. In practice, we just set the high time for all servos and wait for about 15-20 ms at the end of the main loop. A servo modified for continuous rotation will rotate one way for a 1 ms pulse and the other way for a 2 ms pulse. A 1.5 ms pulse should make it stop.

So, your code will look like this:
do
 bring servo1_pin high
 delay_us(1000)
 bring servo1_pin low
 bring servo2_pin high
 delay_us(2000)
 bring servo2_pin low
  delay_ms(20)
loop

However, Admin uses cycles to specify the high time, and that is different for each microcontroller clock (1MHz to 16 or more MHz). Read this thread to see how are the cycle numbers determined for your setup:
http://www.societyofrobots.com/robotforum/index.php?topic=2724.0 (http://www.societyofrobots.com/robotforum/index.php?topic=2724.0)
Title: Re: Servo control?
Post by: MadMax on September 15, 2008, 09:44:10 AM
If you want to this perfectly:

A servo checks the signal after 20ms, it checks the signal at 50Hz.
Lets say you want to create a squarewave with a pulsewidth of 1ms (center servo).
Your actual code should look like this:

Bring servo pin high
Wait (pulsewidth)
Bring servo pin low
Wait (20ms - pulsewidth)