Pages: [1]   Go Down
Author Topic: Controlling 30 servos..  (Read 1646 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

I'm new here ...
I have a small project in which I need control up to 30 servos using
an application, through the serial port (USB).

I have a Mega Arduino, I know that you can control up to 48 servos, but do not know
how to make an optimal code for that .. can control up to 6 servos (by PWM) without
problem using the instruction "case" to get the handle of the servo
and then the value of the position servo ..
But he liked to take it more simply, get a string with 30
identifiers over the respective values ​​of position ..

Any ideas'?

Thanks,
Rafael.
Logged

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

Quote
can control up to 6 servos (by PWM) without problem
No, you can't. Servos are not controlled using PWM. They are controlled using PPM.

Quote
I have a Mega Arduino, I know that you can control up to 48 servos, but do not know
how to make an optimal code for that
Optimal code depends on what you want to do. Arrays will almost certainly come into play.

Quote
But he liked to take it more simply, get a string with 30
identifiers over the respective values ​​of position
He who? If you want speed, you almost certainly don't want a string of anything. An array of bytes, now, that's a different story.

What kind of servos are you using? The standard 0 to 180 type? If so, you can send a start of data marker (a byte whose value is greater than 180, and less than 255), the 30 serial positions, and and end of data marker (a byte whose value is greater than 180, and less than 255, and different from the start marker).

On the Arduino, read the data from the serial port, discarding data until the start marker arrives. Keep reading, storing and counting the values until the end marker arrives. If you received 30 values, use them. If not, the packet was corrupt, and should be discarded.
Logged

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

Hi PaulS,

I'm using standards servos, 0 to 180. I just need to control 30 servos, by USB. The speed is not to important...

I have this code working with 6 servos, "a" to "f" is the identifier servo and "0" to "180" is the position value.
But I don't know how to "scale" this to up 30 servos..

#include <Servo.h>
#define SERVO_2_PIN 2
#define SERVO_3_PIN 3
#define SERVO_4_PIN 4
#define SERVO_5_PIN 5
#define SERVO_6_PIN 6
#define SERVO_7_PIN 7

Servo g_servo2,g_servo3,g_servo4,g_servo5,g_servo6,g_servo7;

void setup()
{
  pinMode(SERVO_2_PIN, OUTPUT);
  pinMode(SERVO_3_PIN, OUTPUT);
  pinMode(SERVO_4_PIN, OUTPUT);
  pinMode(SERVO_5_PIN, OUTPUT);
  pinMode(SERVO_6_PIN, OUTPUT);
  pinMode(SERVO_7_PIN, OUTPUT);
  g_servo2.attach(SERVO_2_PIN);
  g_servo3.attach(SERVO_3_PIN);
  g_servo4.attach(SERVO_4_PIN);
  g_servo5.attach(SERVO_5_PIN);
  g_servo6.attach(SERVO_6_PIN);
  g_servo7.attach(SERVO_7_PIN);
  Serial.begin(9600);
 }

void loop()
{
  static int val = 0;

  if (Serial.available())
  {
    char ch = Serial.read();

    switch(ch)
    {
      case '0'...'9':
        val = val * 10 + ch - '0';
        break;
        case 'a':
        g_servo2.write(val);
        val = 0;
        break;
        case 'b':
        g_servo3.write(val);
        val = 0;
        break;
        case 'c':
        g_servo4.write(val);
        val = 0;
        break;
        case 'd':
        g_servo5.write(val);
        val = 0;
        break;
        case 'e':
        g_servo6.write(val);
        val = 0;
        break;
        case 'f':
        g_servo7.write(val);
     
       val = 0;
        break;
    }
  }
  //Servo::refresh();  // Should try to call this every 20 ms to insure servo stays set
}

Thank you!
Rafael.
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8894
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are going to purchase 30 servos, you might want to consider getting a seperate servo controller to be controlled by your arduino. An ssc-32 ($40) can control 32 servos.
Logged

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

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

I am using the Mega to control 18 servos. Here is some of the code:

Code:
  const int LEG_COUNT = 6;
  const int servos_per_leg = 3;

  enum leg_id { R1, R2, R3, L3, L2, L1 };

  // Servo pins.
  const int servo_pins[][servos_per_leg] = {{42, 43, 44},  // R1
    {34, 35, 37},  // R2
    {26, 27, 28},  // R3
    {22, 23, 24},  // L3
    {30, 32, 33},  // L2
    {38, 39, 40}}; // L1

      // create servo objects to control servos.
      for (int i = 0; i < (LEG_COUNT); i++)
      {
        Servo sx;       // New servo object.
        servos[i].coxa = sx; // Add to array.

        Servo sy;       // New servo object.
        servos[i].femur = sy; // Add to array.

        Servo sz;       // New servo object.
        servos[i].tibia = sz; // Add to array.

        servos[i].coxa.attach(servo_pins[i][x]); // Attach servo to pin.
        servos[i].femur.attach(servo_pins[i][y]); // Attach servo to pin.
        servos[i].tibia.attach(servo_pins[i][z]); // Attach servo to pin.
      }
Logged

denver
Offline Offline
Newbie
*
Karma: 0
Posts: 35
...no matter where you go, there you are
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know this is an old post but, just in case dbiltcliffe observes this reply, I must ask:

 Would you post the rest of the code your sample came from?  I would love to see it. 

Thanx
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I bought the Lynxmotion SSC-32 board. How should I connect the SSC-32 board to the Arduino board? Should I use Arduino Mega board instead of Arduino Uno board?
Logged

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

At 136 posts, you should know that taking a question onto an unrelated post is not a good idea. You should also know that posting questions about hardware without posting a link to the hardware wastes everybody's time.
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8894
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I bought the Lynxmotion SSC-32 board. How should I connect the SSC-32 board to the Arduino board? Should I use Arduino Mega board instead of Arduino Uno board?

I'd go ahead and get the mega as the price difference between the mega and uno is now only a couple of $. You connect the arduino tx and rx to the ssc-32 TTL rx and tx, and connect the grounds between the two boards. make sure both are set for the same baud rate.   
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: