Pages: [1]   Go Down
Author Topic: Arduino UNO how to set speed of 12 servos  (Read 640 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#include <Timers.h>

Timers timer;

void servoMove()
{
  static byte angle = 0;
  servo.write(angle);
  angle++;
}


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

void loop()
{
  // timer update
  timer.process();
}

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

Code:
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))
    {
     SendERROR();
     return;
    }
    else if (angle[i] == servos[i].read())
    {
      Serial.print("won't write angle number( ");
      Serial.print(i+1);
      Serial.println(") = ");
    }
    else
    {
      servos[i].write(angle[i]);
      Serial.print("written angle number (");
      Serial.print(i+1);
      Serial.print(") = ");
      Serial.println(angle[i], DEC);  
    }
  }

}

« Last Edit: February 19, 2012, 09:39:51 am by rekram » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
« Last Edit: February 19, 2012, 10:58:41 am by rekram » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Tesla Member
***
Karma: 143
Posts: 9599
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you considered using the servo library? Might be ab easier place  to start.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: