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

Stay on the cutting edge of robotics with ARC Pro, guaranteeing that your robot is always ahead of the game.

#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.