Arduino Forum

Topics => Robotics => Topic started by: williamkoepp on May 17, 2011, 06:01 pm

Title: Beginner's Robotic Arm
Post by: williamkoepp on May 17, 2011, 06:01 pm
I decided to post this for Beginners getting into robotics.
I don't have schematics so i will just have to describe.
This is a simple two servo arm. It uses four push buttons to control the servos. The circuitry  is not complex, but if you don't know how to wire up the buttons or servos to your Arduino you can look up schematic reference that i learned from at http://ardx.org/circ04 for servo(s) and http://ardx.org/circ07 for buttons. *tip* I recommend if using an ARDX kit to "plug" the male wires directly into the female wires of the servo, they seem to keep a better connection that way. As for the construction of the Arm i used cardboard and coat hanger wire to add support. Be creative and design to your liking instead of following exact to some one else's, it helps you learn and build critical/problem solving skills. Now for the code, it is wrote to where it is easy to understand what is going on, not necessarily in the shortest amount of lines. although over all length is short anyway.
Code: [Select]
// control code for simple two servo robotic arm

#include <Servo.h> // servo library, not needed but makes things a whole lot easier

const int buttonAL = 2; //button to make servo A go turn left ie counter clockwise
const int buttonAR = 4; //for servo A right
const int buttonBL = 11; //for servo B left
const int buttonBR = 12; //for servo B right
//to the above direction depends on how you set up your servos

int posA = 90; // sets start postion for servos
int posB = 90;
int valAL = 0; // sets values for button read
int valAR = 0;
int valBL = 0;
int valBR = 0;

Servo servoA; // claims servo use
Servo servoB;

void setup(){
  servoA.attach(5); // selects pin to send servo signal
  servoB.attach(10);
 
  pinMode(buttonAL,INPUT); // sets mode of the buttons as input
  pinMode(buttonAR,INPUT);
  pinMode(buttonBL,INPUT);
  pinMode(buttonBR,INPUT);
 
  Serial.begin(9600);
}

void loop(){
valAL = digitalRead(buttonAL);
valAR = digitalRead(buttonAR);
valBL = digitalRead(buttonBL);
valBR = digitalRead(buttonBR);


if(valAL == LOW){
  servoA.write(posA += 2); //posA can be replaced with a degree i.e. (posA = 25)
  delay(40);
}else if(valAR == LOW){
  servoA.write(posA -= 2); //posA can be replaced with a degree
  delay(40);
}/*else{
  servoA.write(posA = 90);
  delay(40);
}*/

if(valBL == LOW){
  servoB.write(posB += 2); //posB can be replaced with a degree i.e. (posB = 25)
  delay(40);
}else if(valBR == LOW){
  servoB.write(posB -= 2); //posB can be replaced with a degree
  delay(40);
}/*else{
  servoB.write(posB = 90);
  delay(40);
}*/

}


I encourage all beginners to work with or change code. experiment!, and good luck!
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 05, 2011, 07:11 pm
what would be the use of your Serial.begin function???
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Jul 05, 2011, 10:23 pm
Quote
what would be the use of your Serial.begin function?

http://arduino.cc/en/Serial/Begin
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 06, 2011, 08:05 am
i need information on how to control servo using rxtx pins of the arduino in a simple coding for my robotic arm..when i use serial to LED communication, it runs perfectly but when i attach to a servo, it gives out a weird output.
Title: Re: Beginner's Robotic Arm
Post by: retrolefty on Jul 06, 2011, 08:22 am
Quote
it gives out a weird output.


Only if you post your complete sketch that gives the "weird output" can we help you.

Use the # edit button to place your code in proper display format in a posting

Lefty
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 07, 2011, 04:03 pm
this was my sketch for my robotic arm.im using 3 servos and 1 dc motor also for the grip would be a pneumatic cylinder.

Code: [Select]


#include <Servo.h>

Servo sh; //servo for shoulder
Servo el; //servo for elbow
Servo wr; //servo for wrist
int incomingByte = 0;
int shpos = 97;
int elpos = 100;
int wrpos = 180;
char s;
char w;


void setup() {
    pinMode(2,OUTPUT);   //base turn left pulse
    pinMode(4,OUTPUT);  //base turn right pulse
    pinMode(8,OUTPUT); //solenoid output
    pinMode(12,OUTPUT);//buffer output
    sh.attach(9); //shoulder servo
    el.attach(10); //elbow servo
    wr.attach(11); //wrist servo
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
    Serial.println("Ready");
}

void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.println("I received: ");
Serial.println(incomingByte, DEC);
}
switch(incomingByte){
   case '1':
      sh.write(shpos+2);
      incomingByte=0;
      break;     
  case '2':
      el.write(elpos+2);
      incomingByte=0;
      break;
  case '3':
     wr.write(wrpos+2);
     incomingByte=0;
     break;
  case '4':
    digitalWrite(2,HIGH);
    digitalWrite(4,LOW);
    delay(800);
    digitalWrite(2,LOW);
    incomingByte=0;
    break;
  case '5':
    digitalWrite(8,HIGH);
    incomingByte=0;
    break;
  case '6':
    sh.write(shpos-2);
    incomingByte=0;
    break;
case '7':
   el.write(elpos-2);
   incomingByte=0;
   break;
case '8':
   wr.write(wrpos-2);
   incomingByte=0;
   break;
case '9':
    digitalWrite(2,LOW);
    digitalWrite(4,HIGH);
    delay(800);
    digitalWrite(4,LOW);
    incomingByte=0;
    break;
case '10':
   digitalWrite(8,LOW);
    incomingByte=0;
    break;
}   
   if(incomingByte == 0){
      digitalWrite(8,HIGH);
      delay(20);
      digitalWrite(8,LOW);
}

if(shpos <= 95){
   shpos = 97;
   sh.write(shpos);
}
if(elpos <= 95){
  elpos = 100;
el.write(elpos);
}

}


the serial is connected to a UHF transceiver, il be sending 0x01 format over the serial...what would be my problem?
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Jul 07, 2011, 04:19 pm
Code: [Select]
case '10':
That's not going to happen.

You still need to describe the "weird output".

Quote
il be sending 0x01 format over the serial

What does that mean?
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 07, 2011, 05:03 pm
Code: [Select]
case '5':
    digitalWrite(8,HIGH);
    incomingByte=0;
    break;


would this remain high?

Code: [Select]

case '10':
   digitalWrite(8,LOW);
    incomingByte=0;
    break;


case 10 would turn case 5 off.

ive replaced the sending using char 'A'...'J'
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Jul 07, 2011, 07:46 pm
Quote
case 10 would turn case 5 off.

Fitting two ASCII characters into a "int" isn't impossible, but you haven't included a mechanism to do it.
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 08, 2011, 08:29 am
i dont understand what your saying..what do you mean mechanism?
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Jul 08, 2011, 09:50 am
What I mean is '10' is a multicharacter constant, not a string, and it consists of two ASCII values '1' (in hex 0x31) and '0' (0x31). It is a perfectly acceptable construct in C.
These two characters will fit into an Arduino "int" (16 bits) and you can use such a value in comparisons or "case" statements as you did.
However, you have not provided a means to read more than one character and put the result into an "int", so a single character can never trigger
Code: [Select]
case '10':
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 08, 2011, 01:15 pm
ohhh ok tnx! will post update on my work..
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 08, 2011, 10:22 pm
can someone help me recode my code???im not getting any results, even for just one servo port via serial comm. i dont get it, why i can let an LED be high but moving a servo, i cant..help pls! tnx!
Title: Re: Beginner's Robotic Arm
Post by: zoomkat on Jul 09, 2011, 01:10 am
Quote
can someone help me recode my code???im not getting any results, even for just one servo port via serial comm. i dont get it, why i can let an LED be high but moving a servo, i cant..help pls! tnx!


Some servo test code I use for testing two servos:

Code: [Select]
// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550

#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control
  myservo2.attach(7);
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1); 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
     
      // expect a string like 07002100 containing the two servo positions     
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters
     
      Serial.println(servo1);  //print to serial monitor to see results
      Serial.println(servo2);
     
      int n1; //declare as number 
      int n2;
     
      char carray1[6]; //magic needed to convert string to a number
      servo1.toCharArray(carray1, sizeof(carray1));
      n1 = atoi(carray1);
     
      char carray2[6];
      servo2.toCharArray(carray2, sizeof(carray2));
      n2 = atoi(carray2);
     
      myservo1.writeMicroseconds(n1); //set servo position
      myservo2.writeMicroseconds(n2);
    readString="";
  }
}

Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 09, 2011, 02:52 pm
thank you zoomkat! actually i have read these posts of yours, but im bothered on what i am to send at your code in order for me to trigger a specific servo.. im not going to be sending servo positions, il only be sending a specific character then my receiver arduino would process it.
Title: Re: Beginner's Robotic Arm
Post by: zoomkat on Jul 09, 2011, 05:39 pm
Quote
what i am to send at your code in order for me to trigger a specific servo.. im not going to be sending servo positions, il only be sending a specific character then my receiver arduino would process it.


I would think you could use a series of "if" or "case" statements to evaluate the character received by the arduino, and send a value to the appropriate servo based on the received character. 
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 09, 2011, 07:37 pm
ok! will try that..thanks!!
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 12, 2011, 06:12 pm
@zoomkat
im not getting anything out of the serial monitor, also my servos wont run. thanks!

P.S.

im using a PIC16F628A as my transmitter programmed using flowcode v4 and arduino decimilla 168 as my receiver. over 5812A UHF transceiver.http://www.e-gizmo.com/KIT/images/UHF/5812A%20Tech%20Manual.pdf (http://www.e-gizmo.com/KIT/images/UHF/5812A%20Tech%20Manual.pdf)
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 15, 2011, 05:37 pm

Quote
can someone help me recode my code???im not getting any results, even for just one servo port via serial comm. i dont get it, why i can let an LED be high but moving a servo, i cant..help pls! tnx!


Some servo test code I use for testing two servos:

Code: [Select]
// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550

#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control
  myservo2.attach(7);
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1); 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
     
      // expect a string like 07002100 containing the two servo positions     
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters
     
      Serial.println(servo1);  //print to serial monitor to see results
      Serial.println(servo2);
     
      int n1; //declare as number 
      int n2;
     
      char carray1[6]; //magic needed to convert string to a number
      servo1.toCharArray(carray1, sizeof(carray1));
      n1 = atoi(carray1);
     
      char carray2[6];
      servo2.toCharArray(carray2, sizeof(carray2));
      n2 = atoi(carray2);
     
      myservo1.writeMicroseconds(n1); //set servo position
      myservo2.writeMicroseconds(n2);
    readString="";
  }
}




i get your code now...im using 3 servos so  i have to change this char carray1[6]; to char carray1[9]; ???am i right????
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Jul 15, 2011, 06:28 pm
Quote
im using 3 servos so  i have to change this char carray1[6]; to char carray1[9]; ???am i right????

Not sure I follow your logic there - can you post your code?
Title: Re: Beginner's Robotic Arm
Post by: zoomkat on Jul 15, 2011, 06:29 pm
Quote
i get your code now...im using 3 servos so  i have to change this char carray1[6]; to char carray1[9]; ???am i right????


No, you don' get the code. Note that a seperate array is used for each servo. This is just one very basic way of controlling several servos with the servo positions being sent in a single string. There are code improvements once you decide how you are to control the arm.
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 16, 2011, 01:00 pm
why is the array 6???that is whow i dont get it..but i did manage to add a third servo by copying the pervious code for the third..why is carray[6]? the comment just stated that it is where the magic happens..

as i send $130130020 on the serial monitor the servos move to that specific degree..
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Jul 16, 2011, 01:17 pm
Quote
why is the array 6???

Because an "int" can't store more than five decimal digits.
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 16, 2011, 02:59 pm
why cant i use my hyper terminal to this???is this just for arduino serial monitor???

when i type in 90, it sends out

9
0


but in serial monitor i use $090, it sends
$
090

why is this?
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Jul 16, 2011, 04:01 pm
Because the serial monitor sends all the characters you type in only when you hit the send key, but hyperterm sends them as you press the individual keys.

(What is wrong with your question-mark key? Is it sticking down?)
Title: Re: Beginner's Robotic Arm
Post by: zoomkat on Jul 16, 2011, 08:07 pm
Quote
why is the array 6???


Because you will need to store four bytes and a null terminator in the array for a total of 5 spaces required (as I understand it). I made the array capable of holding six places just to ensure sufficient array space while tinkering with the code. You should be able to use 5 instead of 6 as you should not have to send more than four characters for use with the servo library. 
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 17, 2011, 05:59 am

Quote
why is the array 6???


Because you will need to store four bytes and a null terminator in the array for a total of 5 spaces required (as I understand it). I made the array capable of holding six places just to ensure sufficient array space while tinkering with the code. You should be able to use 5 instead of 6 as you should not have to send more than four characters for use with the servo library.   


ohh. now i get it, i didnt get that it was like that. for every substring with 4 integers you allocated 6 spaces, but what is received is only 5 this was a safe precaution to the code. thanks! i'll put it back to 6 and not 9. thanks zoomkat!
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 18, 2011, 01:03 pm
hi zoomkat,

i was able to control and see the results from the serial monitor. but when i connect the UART of the chip to the controller, it simply shows weird outputs. why is that?
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Jul 18, 2011, 01:26 pm
Quote
but when i connect the UART of the chip to the controller,

Because the controller is a RS232 device with different levels and an inversion?
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 18, 2011, 03:52 pm
i get it now...what im receiving are the extended ascii codes, why is this?
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Jul 18, 2011, 03:59 pm
Quote
what im receiving are the extended ascii codes, why is this?

I don't follow you there.
What is "the controller"?
Is it a true RS232 device?
If so, how are you connecting it to the Arduino?
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 18, 2011, 06:18 pm
im using servo angles to determin its position and not microseconds. as i send data from my pic16F628A connected from txport rb3 to arduino rx digital pin 0, and turning the serial monitor i receive unknown characters/symbols (i browsed through the ascii table and saw the decimal values for the extended ascii).
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Jul 18, 2011, 06:21 pm
Are you sure you haven't simply got a line inversion?
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 18, 2011, 06:43 pm
im not exactly sure. how would i remove line inversion? i tapped(very quick) the tx line to the ground and saw numbers upon it, then the symbols appear again.
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 19, 2011, 08:51 am
this is what im receiving in my rx port.. i connected a npn transistor to the tx port of my pic.
why is it that im receiving this?

(http://farm7.static.flickr.com/6009/5953702030_25422a3feb.jpg)
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Jul 19, 2011, 09:17 am
I have no idea why you are receiving that, but it looks like either a speed mismatch, or a line inversion.
You could write a simple sketch to find out what the idle state of the line is.
Or look at it with an oscilloscope.
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 20, 2011, 02:41 pm

I have no idea why you are receiving that, but it looks like either a speed mismatch, or a line inversion.
You could write a simple sketch to find out what the idle state of the line is.
Or look at it with an oscilloscope.


what kind of sketch would be that??? i tried using just a simple Serial.read(); Serial.println(); sketch, but the same happend...
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Jul 20, 2011, 02:42 pm
Quote
what kind of sketch would be that

One that does a simple "digitalRead" on the appropriate input pin, and prints the result repeatedly.
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 20, 2011, 05:37 pm
like a serial to LED sketch? i dont get it...
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Jul 22, 2011, 07:19 pm
thanks for the help AWOL!
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Aug 14, 2011, 08:22 pm
hello once again!

i managed to run everything up..but i have one problem, my DC Motor keeps on moving even if its assigned case value isnt called. why is this so???

thanks!
Title: Re: Beginner's Robotic Arm
Post by: AWOL on Aug 14, 2011, 08:44 pm
Quote
my DC Motor keeps on moving even if its assigned case value isnt called.

I don't know - where is your code?
Title: Re: Beginner's Robotic Arm
Post by: pao_wikhan on Aug 17, 2011, 07:18 am
i found the problem...i placed the break points wrong..hehehehe  :smiley-mr-green: :smiley-mr-green:

thanks again AWOL!