Asked — Edited

Servo Rotation

Does anyone have any code they could share that would turn a servo from side to side. I need it to move the servo from position 50 to position 80 and back again not exceeding those positions but I cant seem to figure out the logic to make it happen. Any examples?


ARC Pro

Upgrade to ARC Pro

Experience early access to the latest features and updates. You'll have everything that is needed to unleash your robot's potential.

#1  

Seriously?


servo(D0,50) #set servo to position 50
sleep(1000)
servo(D0,80) #move servo to position 80
sleep(1000)
servo(D0,50) #move servo back to original position 50

#2  

The important thing in tjat script is the sleep steps. servo positions are absolute, so if you tell it to go to a position it will. However, servos don't give feedback back to ARC to say when they have reached the position. Therefore, as soon as ARC gives the command, it assumes it is complete and moves on to the next step. Without the sleep steps, it will head back to position 50 before ever reaching position 80.

You can enhance this is with the addition of a servospeed command if you want to slow it down (but will need to use a longer sleep).

If you want it to repeat you can use a label and goto, or if you want it to repeat until some condition is met you could use repeat until or repeat while commands.

Alan

PRO
USA
#3  

Ok, what I mean is I want it to slew back and forth not exceeding the position. I can move the servo. I'm trying to set the code up to check the position of the servo, the slew to the side it is further away slowly and then back again. If I use those commands, it very fast and abrubt. something more smooth. I was trying to use a counter and add 1 to the counter to move it that way. But I couldn't get the logic down.

#4  

So then use servospeed(D0,2) command in conjunction with sleep commands to slow things down... Picking the right servo speed and the right sleep commands should get you what you want...

PRO
USA
#5  

I am not at a place I can check the code. So are you suggesting something like this:

Servo(d1,50) sleep (600) Servo(d1,60) sleep (600) servo(d1,70) sleep (600) servo(d1,80)

#6  

More like this... This will execute much smoother and slower (sweeping back and forth like radar)


Servo(d1,50)
servospeed(D1,2) #may need adjusting slows the servo down
:top
servo(d1,50)
sleep(2000) #may need adjusting
servo(d1,80) 
sleep(2000) #may need adjusting
goto(top)

PRO
USA
#7  

Ok, will give it a try tonight. Movement like Radar is what I am looking for not the jerky movement I've experienced. Thanks

#8  

You may need to adjust the sleep commands to maybe sleep(1000) and if it is too slow speed it up using servospeed(D1,1) instead ofservospeed(D1,2)... FYI servospeed(D1,3), servospeed(D1,4) etc.. would be even slower...

#9  

Something like this may give you some help too. I use these two scripts attached to joystick buttons to open and close a claw as long as I am pressing the button. You could adapt the function to your needs but not exceed the min or max values.


#close
RepeatWhile(GetServo(D23) < 90)
  ServoUp(D23, 2)
Sleep(150)
EndRepeatWhile


#open
RepeatWhile(GetServo(D23) > 34)
  ServoDown(D23, 4)
Sleep(150)
EndRepeatWhile

Alan

PRO
USA
#10  

I would like to thank everybody. The example code and explanation was just what I needed. What I've noticed is that by running the code this way I have to way for the slewing to complete. Is there a way to do two things at once? Meaning I would like it to go up/down and side/to side, like tracing a W. Any thoughts or suggestions?

#11  

Do you mean two servos like a pan/tilt?

If you use something like my last example where you are moving a little at a time in a loop, you would just issue two commands, one to each servo before looping. The commands will execute so fast that they will be simultaneous for all intents and purposes.

Alan

United Kingdom
#12  

Another way you could do it using the example in post #7, is to add another servo command before the sleep() (anything before the sleep will run at the same time).

#Assuming you have servos connected to ports D1 and D2. 

#initialize servo speeds
Servospeed(D1,2) 
Servospeed(D2,2)

#start your repeating servo commands
:loop
Servo(D1,50)
Servo(D2, #add your MAX servo position here)
Sleep(2000)
Servo(D1,80)
Servo(D2, #add your MIN servo position here)
Sleep(2000)
Goto(loop)

Obviously, change where you want your max positions to go, and as already mentioned, servo speeds, and length of sleeps where you need to.