Go Down

Topic: how to program servo motor for robotic arm (Read 29600 times) previous topic - next topic

fatah

I am working on a robotics project. I have designed an arm which lifts the objects up and puts them down.
I want to create a function/procedure in arduino (say fork()) which when called should take the arm up in order to pick some object and then I want to move forward, and then pick the thing.

Right now, I am trying <servo>.write(deg) function available in servo library, but it does not seem to fulfill my requirements. I want to lift the arm for some while and move forward and them make the arm down, but I am seeing the arm repeatedly move up and down, even I have inserted a single command to fork, but the arm contnuously rotates moves up and down.

Please help me.

AWOL

#1
Sep 13, 2013, 09:37 pm Last Edit: Sep 13, 2013, 10:16 pm by AWOL Reason: 1
Please post code.
Please don't post multiple posts on the same topic.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

fatah

code is given below:


#include <Servo.h>

Servo myServo;

void setup(){
  myServo.attach(9);
  Serial.begin(9600);
 
}

void loop(){

 
  if(Serial.available()){
    char val = (char) Serial.read();
   
    if(val == 'l'){
      lift(); //lift the object
    }
   
    else if (val == 'p'){
      putDown(); //put the object down
    }
  }
}

void lift(){
  myServo.write(45);
}

void putDown(){
  myServo.write(0);
 
}

I want to lift the object when I send 'l' to the arduino and put the object when I send 'p' to it. But, the servo arm continuously lifts and puts the object down. It's totally unexpected.
Please help me, how can I achieve my goal.

PaulS

The art of getting good answers lies in asking good questions.

fatah


PaulS

Quote
I am sending this via Serial monitor

First, I find your code very difficult to read. Putting the { on the same line as the function/statement made sense when monitors could only display 24 lines at a time. That ceased to be a problem years ago. Now, it makes more sense to put each on it's own line. When it did make sense to put the { on the same line as the {, you can bet that extraneous blank lines were not used.

It makes sense to use the Tools + Auto Format tool to properly indent your code.

It makes sense to follow the rules and post code properly.

I don't see anything in that code that would cause the servo to move unless a 'l' or 'p' arrived on the serial port.

By the way, Serial.available() doesn't return a boolean. You shouldn't treat the return value as a boolean. It is far clearer that you understand what is supposed to happen if you explicitly test for a reasonable value.

Where are your Serial.print() statements?
The art of getting good answers lies in asking good questions.

aarondc

NB: keep in mind most posts here are simply people's opinions. Whether you put the { on the same line or not is irrelevant.

If you do if (Serial.available()) it's immediately apparent what is happening, and is the same as the official example provide by Arduino here: http://arduino.cc/en/Serial/Available

Code: [Select]
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);

}

void loop() {
  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.print(inByte, BYTE);

  }
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.print(inByte, BYTE);
  }
}


So again, don't feel you need to change anything there.

I have no problem reading your code whatsoever.

Beyond that, I cannot see why the sketch as you have posted would continuously operate. Can you give us a bit more detail as to how you are sending commands? Something from your description is missing if it is behaving as you say.

PaulS

Quote
So again, don't feel you need to change anything there.

Since the code is not working as-is, I guess I would feel that I needed to make a change. YMMV.

Quote
Beyond that, I cannot see why the sketch as you have posted would continuously operate.

I believe I said the same thing. I also suggested adding Serial.print() statements to help understand why the code is not behaving as expected. Since you don't seem to feel the need for any more information, I'll leave it to you to help OP find a solution.
The art of getting good answers lies in asking good questions.

zoomkat

Some servo test code (may or may not work, no way to currently test) that provides for sending commands to more than one servo at a time. You can test using the serial monitor (make an arm position control string like 30c,180b,70a,120d, in notepad, copy the control string, paste in the serial monitor text box using keyboard ctrl-v, then click send). Note that servos need a large 6v power supply to function properly.

Code: [Select]

//zoomkat 11-22-12 simple delimited ',' string parse
//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 combined like 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 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

vivek16_

sir i actually doing a project on robotic arm with two sensors on the gripper(inductive and capacitive) ,so that it can sense a metallic and non metallic parts and keep the robotic arm to the different position,
sir i am finding difficulty to find out the codes , so sir please  send me the codes for that and if possible please post on my comment??????? please sir


PaulS

please help me
What, EXACTLY, do you need help with? Reading the sensors? Making the gripper, about which you have provided NO details, squash the object you are trying to pick up?
The art of getting good answers lies in asking good questions.

JimboZA

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

MarkT

95% of all issues people have with servos is inadequate power supply.  How are you powering
your servo?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

zoomkat

Quote
sir i am finding difficulty to find out the codes , so sir please  send me the codes for that and if possible please post on my comment??????? please sir
I posted code that you should be able to use to test your arm and determine its mechanical limits.
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