Go Down

Topic: Controlling servos through serial, with letters/words (Read 986 times) previous topic - next topic

tomflah

hi guys im new to the arduino world and im getting to grips with all the different code out there.

ive done a good few examples and i think im getting the hang of it.

im working on a robotic gripper that i got off arduino and i can get it to move using numbers through String commands - which i got off this forum
but i also want to be able to type in the command " close claw"  and have the of the servos slowly close the gripper to grip the object

this is the code i have so far. i got it off another topic here, theres an explanation for the bits i added which didnt work. i posted the error messages at the bottom it it helps

Quote

#include <Servo.h>

String readString, servo1, servo2, servo3;

Servo myservo1;   
Servo myservo2;
Servo myservo3;
Servo closeclaw;


int pos =0;


void setup() {
  Serial.begin(9600);
  myservo1.attach(6); 
  myservo2.attach(7);
  myservo3.attach(8);
  closeclaw.attach(8); // i want the third servo to control the gripper

  Serial.println("I am ready");
}

void loop() {

 
 
  while (Serial.available()) {
    delay(3);   
    if (Serial.available() >0) {
      char c = Serial.read(); 
      readString += c;
    }
  }

  if (readString.length() >0) {
      Serial.println(readString);
     
         
      servo1 = readString.substring(0, 3);
      servo2 = readString.substring(3, 6); 
      servo3 = readString.substring(6, 9);
      servo3 = readString.substring(closeclaw); // my attempt at getting the
      // arduino to recognise "close claw" command
     
      Serial.println(servo1); 
      Serial.println(servo2);
      Serial.println(servo3);
     

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();
      int n3 = servo3.toInt();
      int closeclaw = servo3.toInt();// connecting the close claw command to
      //the third serv
     
     
      Serial.println("move to :");
      Serial.println(n1); 
      Serial.println(n2);
      Serial.println(n3);
       
     
       
      myservo1.write(n1); 
      myservo2.write(n2);
      myservo3.write(n3);
     
      if(closeclaw){// the arm gipper can bend twist and close with the commands
      // but if i type in close claw this is to make sure it closes on the object at a spped i like
      { for(pos=0; pos<180; +=1) }
      myservo.write(pos)
      delay (40);
   
  }
   
    readString="";
  }

/quote]

thanks to anyone who helps out

serial_slow_trial.cpp: In function 'void loop()':
serial_slow_trial:44: error: no matching function for call to 'String::substring(Servo&)'
/Users/thomasoflaherty/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/WString.h:160: note: candidates are: String String::substring(unsigned int) const
/Users/thomasoflaherty/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/WString.h:161: note:                 String String::substring(unsigned int, unsigned int) const
serial_slow_trial:72: error: expected primary-expression before '+=' token
serial_slow_trial:72: error: expected primary-expression before '}' token
serial_slow_trial:72: error: expected `;' before '}' token
serial_slow_trial:73: error: 'myservo' was not declared in this scope
serial_slow_trial:79: error: expected `}' at end of input

johnwasser

Code: [Select]

#include <Servo.h>

String readString, servo1, servo2, servo3;

Servo myservo1;   
Servo myservo2;
Servo myservo3;
Servo closeclaw;

int pos =0;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6); 
  myservo2.attach(7);
  myservo3.attach(8);
  closeclaw.attach(8); /////  MAKES NO SENSE TO HAVE TWO Servo OBJECTS CONTROL THE SAME PIN

  Serial.println("I am ready");
}

void loop() {
 
  while (Serial.available()) {
    delay(3);   
    if (Serial.available() >0) {  //// NO NEED TO CHECK AGAIN.  THE while LOOP CHECED ALREADY
      char c = Serial.read(); 
      readString += c;
    }
  }

  if (readString.length() >0) {
      Serial.println(readString);
         
      servo1 = readString.substring(0, 3);
      servo2 = readString.substring(3, 6); 
      servo3 = readString.substring(6, 9);
      servo3 = readString.substring(closeclaw); //// closeclaw IS A Servo OBJECT, NOT THE TWO NUMBERS THAT
//// REPRESENT THE FIRST AND LAST CHARACTERS OF THE DESIRED SUBSTRING
     
      Serial.println(servo1); 
      Serial.println(servo2);
      Serial.println(servo3);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();
      int n3 = servo3.toInt();
      int closeclaw = servo3.toInt();  // YOU CAN'T HAVE AN int AND A Servo both named 'closeclaw'     
     
      Serial.println("move to :");
      Serial.println(n1); 
      Serial.println(n2);
      Serial.println(n3);

      myservo1.write(n1); 
      myservo2.write(n2);
      myservo3.write(n3);
     
      if(closeclaw){// the arm gipper can bend twist and close with the commands
      // but if i type in close claw this is to make sure it closes on the object at a spped i like
      { for(pos=0; pos<180; +=1) }    //////// BRACKETS HERE ARE MISPLACED. for LOOP HAS NO CONTENTS.
      myservo.write(pos)       ///////// MISSING A SEMICOLON TO END THIS STATEMENT
      delay (40);
   
  }
   
    readString="";
  }

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

tomflah

i could tell from the error messages my code wasn't correct. i put them in with an explanation to help give someone an idea of what im trying to do
i fixed the bracket and semi colon mistakes. have you got any suggestions as to how i can control servo3 to move when i type in "close claw"

johnwasser

Currently the only input allowed is three consecutive three-digit numbers controlling the position of three servos.  If I understand your comments correctly you don't want to add a fourth servo for the claw, you want myservo3 to control the claw.

Can't you just tell myservo3 to move to 000 to close the claw and 180 to open the claw?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

tomflah

that would be a lot easier john, and its what im doing now. this is for an assignment in college. the lecturer gave the class a few different programming assignments to choose from with their own criteria. i choose one for hand movement. and the criteria states that i have to be able to move the claw components independently; servo1 for wrist bending, 2 for twisting and 3 for opening and closing the claw. im aiming for extra points though and im trying to add grip control - a separate command that will move the claw slowly in increments with a pressure sensor measure the force until the grip is strong enough. i know the code for that - there is another guy in my class doing the same thing and he's on this forum too somewhere-.

problem is i dont know how to input a command like " closeclaw" and have the arduino, skip the n3 command and use the grip code instead. how do i get the controller to recognise word commands?

zoomkat

Some servo test code that might be of interest that uses a comma to delimit the individual servo commands (not yet actually servo tested). You might send a command to the gripper (claw) servo using a command like "600closeclaw," and "2200openclaw" to enter a gripper operating loop. If the grip/release loop knows the desired servo positions, then "closeclaw," and "openclaw," could probably be used.


Code: [Select]
if(readString.indexOf("closeclaw") >0) //checks for closeclaw
if(readString.indexOf("openclaw") >0 //checks for openclaw


Code: [Select]

//zoomkat 11-22-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added

String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        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);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

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

Go Up