Go Down

Topic: Serial control of servos realtime (Read 1 time) previous topic - next topic

thecheatscalc

Here's the basic idea, I need to control a robot with around 17 servo motors (which is quite a bit I know)

The process looks something like this:

computer feeds simulation data through serial
Arduino reads the data (let's say CSV values for each stepper)
Arduino moves Servos
Arduino has gyro sensors that feed back to the computer (or even a second board if one isn't adequate)

Data transfer is completed by WiFly adaptor

The main issue is I need low latency, and so far in my small endeavors into this issue, my uno (which will be replaced by a Mega after proof of concept with 3-6 steppers)

here's my fairly simple code that lets me adjust 3 servos through the serial monitor:
Code: [Select]

#include <Servo.h>

  int srv1;
  int srv2;
  int srv3;
  Servo servo1;
  Servo servo2;
  Servo servo3;

void setup() {
  servo1.attach(2);
  servo2.attach(8);
  servo3.attach(6);
  Serial.begin(38400);

  }

void loop() {
  if (Serial.available() > 0){
    srv1 = Serial.parseInt();
    srv2 = Serial.parseInt();
    srv3 = Serial.parseInt();
    servo1.writeMicroseconds(srv1);
    servo2.writeMicroseconds(srv2);
    servo3.writeMicroseconds(srv3);
  }
}


The biggest issue I have right now is that once I send a line such as 1500,1500,1500 (for the uS values) it takes around .5 to 1 second before anything "happens" after which the servos move quickly to their positions without any noticeable delay (so far)

Any help on what's causing the latency issues, or if if this is even feasible? Most the processing will be done by a computer, the arduino just needs to read values, apply, and send a few pieces of data.

Nick Gammon

I think I would rework how you get your integers. Here are some ideas:

http://www.gammon.com.au/serial
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

zoomkat

Quote
The biggest issue I have right now is that once I send a line such as 1500,1500,1500 (for the uS values) it takes around .5 to 1 second before anything "happens" after which the servos move quickly to their positions without any noticeable delay (so far)


Have you considered it may be a problem with the computer application sending the serial data? do you get the same delay with the below servo test code?

Code: [Select]

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

PaulS

Quote
Arduino reads the data (let's say CSV values for each stepper)

Let's say binary data, to eliminate the need to send useless commas and the need to convert the strings back to ints to be used. The Arduino program will be at least an order of magnitude more responsive. Less data, faster processing, what's not to like?

Nick Gammon


The biggest issue I have right now is that once I send a line such as 1500,1500,1500 (for the uS values) it takes around .5 to 1 second before anything "happens"


It shouldn't take a second to receive that at 38400 baud. Something else is wrong. It should take 3.65 mS.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

thecheatscalc

zoomkat, I appreciate the help, I'll give your code a shot as soon as I get the chance, I've been busy for the memorial day holiday but it's back to work for me now. :)

Nick, I agree with what you're saying, I don't very well understand serial communication, but the time delay is MUCH too long for anything reasonable. I'll be looking at that link and see if that can help me more, I'll try to keep you updated!


Go Up