Go Down

Topic: Arduino UNO how to set speed of 12 servos (Read 881 times) previous topic - next topic


Feb 19, 2012, 03:36 pm Last Edit: Feb 19, 2012, 03:39 pm by rekram Reason: 1
Hi, I have problem with setting speed in my 12 servos. I don't want to use delay() function, but timers.
My programs on Arduino using messenger library to read 12 values of angles, and currently, I'm controlling speed of move servos by sending this values at some frequency. It's not reliable, because I have to use pause() function in program on PC who is sending messages, so sometimes something goes wrong.

I want to send example angles 130 20 30 40 50 33 etc. and Arduino set them with some speed (defined by frequency set in timer.attach() ). With one servo function using timers looks like this:

Code: [Select]

#include <Timers.h>

Timers timer;

void servoMove()
 static byte angle = 0;

void setup()
 // connect function running every 0,02s
 timer.attach(0, 20, servoMove);

void loop()
 // timer update

But how to create the same with 12 servos  :~ ? Servos must be set in similar time (they move part of legs in my robot)
My current function looks like this:

Code: [Select]

void setServo()
 int angle [12];
 for (int i=0;i<12;i=i++)
   angle[i] = message.readInt(); // function from Messenger library
   if ((angle[i]>180) || (angle[i] < 0))
   else if (angle[i] == servos[i].read())
     Serial.print("won't write angle number( ");
     Serial.println(") = ");
     Serial.print("written angle number (");
     Serial.print(") = ");
     Serial.println(angle[i], DEC);  



Are all 12 servos supposed to complete a move at the same time?


Feb 19, 2012, 04:31 pm Last Edit: Feb 19, 2012, 04:58 pm by rekram Reason: 1
I think the parts of servos (6 even and 6 odd) should complete their move at the same time, because it's hexapod robot, so one part of servos is responsible for moving leg up and the second is rotating leg.

But the angles for this part will be the same (sometimes in the opposite direction) because all legs are moving in the same way (only sometimes different when the body rotate)


I don't see that a timer is necessary, then. You have a state machine, with two states - waiting for step data and stepping.

If you are stepping, there is a time that the last increment was made and a time between increments. Use millis() to record when the last increment was made and to determine if it is time to increment again. Use floats to determine the angle that each servo rotates at each increment (based on the current angle and new angle for each servo, determine the largest change required. One servo rotates this amount; all the others rotate smaller amounts).

Each servo needs to advance some amount at each iteration. If one servo is to rotate 20 degrees, and another rotates 10 degrees, there will be 20 iterations. One servo rotates 1.0 degrees per iteration, while the other rotates 0.5 degrees per iteration. For each iteration, you need to determine if it is time to perform the iteration. If it is, you need to determine the new value for each servo for the iteration, based on the current position and the increment per iteration. Apply the new angle to each servo when it is time, and record the time.


Have you considered using the servo library? Might be ab easier place  to start.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131