Society of Robots - Robot Forum

General Misc => Misc => Topic started by: LXE on October 07, 2010, 06:12:39 AM

Title: Arduino - Servo Speed Control Question
Post by: LXE on October 07, 2010, 06:12:39 AM
How do you control the rotational speed of the servo with the Arduino?

I have a very vague algorithm that might not work.

while (i <> degree)
{
i = i + 1 // new servo position
servo.write(i); // rotate servo to position "i"
delay(x); // wait "x" amount of milliseconds
}
Title: Re: Arduino - Servo Speed Control Question
Post by: knossos on October 07, 2010, 07:23:00 AM
That looks like it would work.  But i would add some code to keep it from trying to go beyond the angle limits of your servo.  Not sure how necessary it would be, but I like to add error trapping in all my code.  Of course I'm used to programming PCs which the added memory usage is insignificant compared to potential problems.
Title: Re: Arduino - Servo Speed Control Question
Post by: KurtEck on October 07, 2010, 07:55:44 AM
I did something similar to this with an Arduino Pro.  But I wrote a new library that did it for multiple servos and I had a set of functions that said move the following servos from their current location to a new location (angle) in this amount of time.

This library I did was write an ISR for a timer that overflowed every 16ms.  I then calculated for each servo how much change in the angle (in ms) should happen per interrupt.  The interrupt then updated all of the servos that needed updating.  If all of the servos are at their desired location the timer is disabled.  There are also functions to query to find out which servos are still moving. 

This code was not tested much but I did have a Lynxmotion Biped Brat walking using it.  I did the servo update code in the background as this allows me to go off and query the XBEE or PS2 for additional information and for example rotated the turret while it is still in the middle of a step.  I would share this if desired but remember it is simply a work in progress...

Kurt
Title: Re: Arduino - Servo Speed Control Question
Post by: LXE on October 07, 2010, 04:48:25 PM
That looks like it would work
I think I don't need the delay because servos tend to slow down when they are told to move to a position close to it.

Title: Re: Arduino - Servo Speed Control Question
Post by: knossos on October 07, 2010, 05:16:33 PM
I would start with a delay and reduce it until you have the desired speed.  Besides if you start with a larger delay it gives you more of an idea if it is doing what you want since it's going slower.  If you find the delay is unnecessary you can always comment it out later.