### Author Topic: How to change servo speed  (Read 11397 times)

0 Members and 1 Guest are viewing this topic.

#### Razor Concepts

• Supreme Robot
• Posts: 1,856
##### How to change servo speed
« on: September 24, 2008, 07:43:11 PM »
I'm using an Axon, the line of code is "servo_name(700)" to move the servo. I noticed this gets the servo to that position as fast as it can. How can you adjust that so you can control how fast the servo moves? I'm thinking adjusting the servo moving method to accept three variables. First is position, second is the speed, and third would be either 1 or 0. Say if the third value was 1, it would just move the servo to the specified position at the specified speed. If the value was 0, then it would gradually slow down the specified speed as it reached the position.

Is this possible? Also if someone could point me towards the c file that has the code for the move servo method, that would be great. Thanks

#### izua

• Supreme Robot
• Posts: 682
##### Re: How to change servo speed
« Reply #1 on: September 24, 2008, 08:39:24 PM »
The servo gradually speeds down as it reaches its position. Onlythat it happens at higher speed you may not see. On some servos, it's quite visible though.

To slow it down, just send it several intermediate positions, delaying each command by a time factor.
Check out my homepage for in depth tutorials on microcontrollers and electronics.

#### Ro-Bot-X

• Contest Winner
• Supreme Robot
• Posts: 1,431
• Store: RoBotXDesigns.ca
##### Re: How to change servo speed
« Reply #2 on: September 24, 2008, 09:08:04 PM »
I think you will have to make up your own function that will be able to accelerate and decelerate by giving in between positions to the servo untill it reaches the desired position.

Say the current position is at 600 delay cycles and you want it to go to 900. You will start the function to ramp up the delay cycles adding a number to the starting position like 630, 690, then jump over to almost the end and decrease the added number in the same way like 810, 870, 900.

To make up a formula, substract the end position from the start position and divide it by 10, then add one 10th to the starting position, then 2 10ths, then 4 10ths, then 2 10ths and finally the last 10th.

Now this is just an example, you need to come up with the numbers that will work out the best.
Check out the uBotino robot controller!

#### Webbot

• Expert Roboticist
• Supreme Robot
• Posts: 2,165
##### Re: How to change servo speed
« Reply #3 on: September 24, 2008, 09:24:54 PM »
Isn't this an example of PID control where the various constants can be used to move smoothly from A to B?
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

• Supreme Robot
• Posts: 11,702
##### Re: How to change servo speed
« Reply #4 on: September 24, 2008, 10:53:31 PM »
Here are two very quick examples for slow servo motion:

Code: [Select]
`//change angles in about 12 secondsfor(i=300;i<900;i++){servo(i);delay_ms(20);}//change angles 5 times faster using 5 as the multiplieri=300;while(i<900){servo(i);i=i+5;delay_ms(20);}`

#### benji

• Supreme Robot
• Posts: 830
##### Re: How to change servo speed
« Reply #5 on: September 25, 2008, 03:54:10 PM »
Quote
Isn't this an example of PID control where the various constants can be used to move smoothly from A to B?

i dont think soo,,,, anyways,, can u explain ?
good ol' BeNNy

#### Razor Concepts

• Supreme Robot
• Posts: 1,856
##### Re: How to change servo speed
« Reply #6 on: September 25, 2008, 04:07:39 PM »
I tried the for loop. The servo does move more slowly, but it seems to vibrate a lot. It's like as its moving slowly, its rapidly moving back and forth, making it vibrate. Is this how it should move?

• Supreme Robot
• Posts: 11,702
##### Re: How to change servo speed
« Reply #7 on: September 25, 2008, 04:16:47 PM »
What servo you using? Have a load on the servo or just a wheel?

Increase delay_ms(20); to something like delay_ms(40);

I'm assuming your servo can't handle it. Vibration like this is bad, causing overheating . . .

There are a dozen other ways you can slow the servo down in software. But yea, you get the idea.

#### izua

• Supreme Robot
• Posts: 682
##### Re: How to change servo speed
« Reply #8 on: September 25, 2008, 04:18:38 PM »
Increase the distance between the intermediate points. The points you choose now are in the deadband, and that's what it's making the servo oscillate.
Check out my homepage for in depth tutorials on microcontrollers and electronics.

#### Razor Concepts

• Supreme Robot
• Posts: 1,856
##### Re: How to change servo speed
« Reply #9 on: September 25, 2008, 05:15:29 PM »
Thanks, I thought you had to decrease the delay time
I experimented a little and found a delay of 28 was good for my servos.

#### Razor Concepts

• Supreme Robot
• Posts: 1,856
##### Re: How to change servo speed
« Reply #10 on: September 26, 2008, 05:28:56 PM »
I just noticed that the servos dont move the same distance from the center... say if I center the servo at 700. If I tell it to go to 400, it spins -40 degrees from center. From 700, if I tell it to go to 1000, it spins 60 degrees (from center). Those are estimations, but why does this happen? I tried 3 types of servos and I noticed it one two, and could barely tell on another.

Also, I made a moveservo method that uses the current position, the position you want it to go, and the speed you want it to travel to move the servo. The problem is, I cant control two servos at the same time, since it will wait for the first moveservo method to complete, and then start the next moveservo method. Is there an easy way to run two methods at the same time without writing a new moveservo method that moves two servos?
« Last Edit: September 26, 2008, 06:24:58 PM by Razor Concepts »

#### pomprocker

• Supreme Robot
• Posts: 1,431
• Sorry miss, I was giving myself an oil-job.
##### Re: How to change servo speed
« Reply #11 on: September 26, 2008, 06:13:49 PM »
Try creating a recursive function that will call itself adding some small change like +2 to the servo position every time only exiting the function when the desired position is reached

#### Razor Concepts

• Supreme Robot
• Posts: 1,856
##### Re: How to change servo speed
« Reply #12 on: September 26, 2008, 06:27:08 PM »
I found out that it isnt anything wrong with the programming, its just that the momentum of the servo causes it to overshoot the desired position. Is there a way to find out the position of the servo, so that it will "lock on" unto that position, or do you just keep telling the servo to stay at that position for a secord or two to make sure its where its supposed to be?

#### izua

• Supreme Robot
• Posts: 682
##### Re: How to change servo speed
« Reply #13 on: September 26, 2008, 07:04:44 PM »
I'll tell you how to find out the position of the servo if you can tell me how to make a mechanical system freeze instantly or lock on
Check out my homepage for in depth tutorials on microcontrollers and electronics.

• Supreme Robot
• Posts: 11,702
##### Re: How to change servo speed
« Reply #14 on: September 28, 2008, 08:21:54 AM »
Quote
Try creating a recursive function
Not allowed on microcontrollers . . .

Quote
Is there a way to find out the position of the servo, so that it will "lock on" unto that position
Nope. But you can hack the pot signal in the servo and with a wire connect the output to an ADC

Quote
Also, I made a moveservo method that uses the current position, the position you want it to go, and the speed you want it to travel to move the servo. The problem is, I cant control two servos at the same time, since it will wait for the first moveservo method to complete, and then start the next moveservo method. Is there an easy way to run two methods at the same time without writing a new moveservo method that moves two servos?
There are plenty of ways around this, such as including them in the same function:

Code: [Select]
`[blah blah blah]//your code herei=300//calculated required speedconstant=3;//increase this number to slow servo speedi=(i+i_old*constant)/(constant+1);//smoothing functionservo1(i);i=i_old;j=300//calculated required speedconstant=3;//increase this number to slow servo speedj=(j+j_old*constant)/(constant+1);//smoothing functionservo2(j);j=j_old;delay_ms(28);`

#### Razor Concepts

• Supreme Robot
• Posts: 1,856
##### Re: How to change servo speed
« Reply #15 on: September 29, 2008, 06:29:46 PM »
Thanks for all the help    I was hoping there would be "easy" ways to program multiple servos and such, but I guess programming is harder than I thought

I made a simple arm from 3 servos today, and used the for-loop style code to slow the servos down.
Code: [Select]
`for(i=700;i<900;i++){servo_1(i);servo_2(700);servo_3(700);delay_ms(28);}`
Here is a small video showing the arm and the movements... nothing amazing but I might as well document my first steps into (Axon) robotics
http://vimeo.com/1845644
(note 175 minute wait time from the time of this posting)

• Supreme Robot
• Posts: 11,702
##### Re: How to change servo speed
« Reply #16 on: September 29, 2008, 08:33:07 PM »
You can also do it based on a timer, where it checks to see if a certain amount of time has passed before it changes the servo command.

It says 65 minutes for me . . . one more hour! [cough cough youtube cough ]

Feel free to send me links to anything cool you do with the Axon in the future and I'll put it up on the Axon Examples page.

#### Razor Concepts

• Supreme Robot
• Posts: 1,856
##### Re: How to change servo speed
« Reply #17 on: September 30, 2008, 08:23:40 PM »
Thanks! I have a feeling I will like the timer method better. I'm sure that's been done a lot so I will look that up. However, can I still use timers to control servos, even if I'm using timers to interpret data from a receiver (from my other radio control thread)?

• Supreme Robot
• Posts: 11,702
##### Re: How to change servo speed
« Reply #18 on: October 02, 2008, 10:32:57 PM »
Quote
However, can I still use timers to control servos, even if I'm using timers to interpret data from a receiver (from my other radio control thread)?
Yeap you can no problem. If you use timer interrupts though it might get tricky to debug if you don't do it just right.

#### sr26

• Beginner
• Posts: 2
##### Re: How to change servo speed
« Reply #19 on: February 27, 2009, 09:43:23 AM »
Quote
Try creating a recursive function
Not allowed on microcontrollers . . .

hello! I'm just wondering why recursive function is not allowed on microcontrollers... I'm using an axon and was planning to do a recursive function for a maze solving algorithm. But since I read this part of the thread, I'm now curious why microcontrollers cannot handle recursion.. Why is this so?

Any idea will be greatly appreciated. Thank you.

#### Razor Concepts

• Supreme Robot
• Posts: 1,856
##### Re: How to change servo speed
« Reply #20 on: February 27, 2009, 11:13:04 AM »
From what I've read, recursion is doable but you have to be careful, recursion takes up a lot of "stack space". So if you don't do it in just the right way, it won't work at all.

#### Webbot

• Expert Roboticist
• Supreme Robot
• Posts: 2,165
##### Re: How to change servo speed
« Reply #21 on: February 27, 2009, 01:01:23 PM »
Everytime you call a routine then it takes up stack space to store the return address (ie where to go back to) and also to store the local variables of the method you are calling. So the deeper you go then the more stack space is required. Any interrupts will also temporarily require extra space on the stack.

Dont forget the stack is based in flash RAM along with your global variables.

So if the stack gets too big then you get a 'Stack Overflow' - which on a microprocessor will either start writing all over your data or reboot. It will certainly cause chaos.

So - as long as you keep the level of recursion small then you are okay. But most recursive routines can be unwrapped into non-recursive routines - normally by maintaining your own sort of stack in arrays. Your program can then check if the array has run out of space and signal an error in a more friendly way such as turning the LED on.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

#### paulstreats

• Guest
##### Re: How to change servo speed
« Reply #22 on: February 27, 2009, 01:31:58 PM »
An easy way to make sure that you dont run out of stack space is to make the 1st line of the recursive function pop itself off the top of the stack. It does mean that any interrupts wont return back into the function though.

A different way is to have an calling function which starts of the recursive function, the interrupt return would return back to the calling function. If you modify the stack pointer with -1 or -2 (on the pointer of the calling function)of its value it will return to the calling function a command before calling the original recursion which means it will start off the recursion again(it sounds complicated and can be tricky to get right but it does work).

overflowing the stack causes a reset on PIC's aswell as having null pointers or empty spaces in the stack also causes a reset(yep my threading system ran into all sorts of problems )

maybe a function that looks through the stack and pops any multiple pointers off but leaves just 1 occurence would work best.

This is probably going to be quite difficult unless you can actually see the stack and observe its behaviour without any experience of it(with PIC's and mplab you can animate your program and see things like the return stack during simulation, its really satifying to be able to see it do what you want it to and really annoying to see gaps appearing )

Dynamically called variables in the function would probably never be released from the heap so you would need to use static variables created globally at the beginning of the program.

You would be far better off using a while loop. a while loop can be used like a recursive function and will exit once the conditional is satisfied without you having to get too complex about it. (While and For loops were created to solve the problem of recursion - the only real reason to use recursion now is with languages that dont support while and for loops. - yes they do exist)
« Last Edit: February 27, 2009, 01:47:28 PM by paulstreats »