Wednesday, May 21, 2014

Braț robotic #4 (final)

Proiectul a ajuns la final. Au fost câteva săptămâni intense, pentru care mă bucur cu adevărat. La sesiunea de comunicări a obținut locul 2 și a energizat și audiența. Lucruri bune.

Inițial proiectul includea un senzor de culoare care să citească culoarea obiectului preluat de robot. Pentru că senzorul pe care îl aveam era problematic, am decis să includ din nou elementul uman în ecuație, și am înlocuit senzorul cu o tastatură.

De asemenea, am trecut de la IDE-ul Arduino, la Atmel Studio 6.1 cu pluginul de Arduino. Mi-a oferit câteva funcții în plus, ca cea de autocompletare, sau utilizarea mai multor fișiere sursă în paralel.


Mai sus puteți vizualiza un clip care exemplifică funcționalitatea robotului. 

Codul este mai jos:

#include <Servo.h> 
#include <interrupt.h>
#include "Keypad/Keypad.h" 

Servo hand, wrist, elbow, shoulder, waist;  // create servo object to control a servo a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 
int key = 12; // joystick variables
int automatic, manual;
boolean keyFlag = false;
volatile int hor, vert;
int waistPos = 90, shoulderPos = 90, elbowPos = 90, wristPos = 90, handPos = 90; 


//keypad
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 7, 13, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {16, 17, 18, 19}; //connect to the column pinouts of the keypad

Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char customKey;
void setup() 
  // attaches the servos on pins 
  hand.attach(3);
  wrist.attach(11);
  elbow.attach(6);
  shoulder.attach(9);
  waist.attach(10);  

  //joystick pins
  pinMode(key, INPUT);
  pinMode(A0, INPUT); //horizontal movement
  pinMode(A1, INPUT); //vertical movement
  
  //automatic/manual pins
  pinMode(2, INPUT); //automatic
  pinMode(4, INPUT); //manual

  Serial.begin(9600);
   
  pinMode(12, INPUT);
  digitalWrite(12, HIGH); //pull-up
  
  homePos(); //get robot to start position

void getBack()
{
 while(waistPos > 90)
{
  waist.write(waistPos--);
delay(30);
}
 while(wristPos > 90)
{
wrist.write(wristPos--);
delay(30);
}
 while(elbowPos > 90)
{
elbow.write(elbowPos--);
  delay(30);
}
 while(shoulderPos > 90)
{
shoulder.write(shoulderPos--);
     delay(30);
}

}
void getToLoad() //gets load to sensor
{
  waist.write(130);
  hand.write(140);
  
  for (wristPos; wristPos < 135; wristPos++)
  { 
   wrist.write(wristPos);              // tell servo to go to position in variable 'pos' 
   delay(30);   
  }

  for (shoulderPos; shoulderPos < 140; shoulderPos++)
  {
 shoulder.write(shoulderPos);              // tell servo to go to position in variable 'pos'
 delay(30);
  }
  
  for (elbowPos; elbowPos < 115; elbowPos++)
  {
 elbow.write(elbowPos);              // tell servo to go to position in variable 'pos'
 delay(30);
  }
   
  delay(30);
  getLoad();
  
  for (wristPos; wristPos > 125; wristPos--)
  {
 wrist.write(wristPos);              // tell servo to go to position in variable 'pos'
 delay(30);
  }
}

void receiveLoad() //opens and closes hand/claw
{
 hand.write(120);
 delay(1000);
 hand.write(50);
 delay(1000);
}

void getLoad()
{
 hand.write(50);
 delay(1000);
}

void runAutomatic()
{
Serial.println("automatic");
 getToLoad();
 delay(1000);
 getLoad();
 getBack();
 checkCommand();
}

void homePos() //gets to start position
{
  elbow.write(90);
  shoulder.write(90);
  wrist.write(90);
}

void runManual()
{
  hor = analogRead(A0);            // reads the value of the potentiometer (value between 0 and 1023) 
  vert = analogRead(A1);
  hor = map(hor, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  vert = map(vert, 0, 1023, 0, 179);
  
  waist.write(179 - hor);          // sets the servo position according to the scaled value 
  delay(15);
  
  elbow.write(vert);
  shoulder.write(vert);
  wrist.write(vert);
  
  int keyState = digitalRead(key);

  if(keyState == LOW && keyFlag == true)
  {
    receiveLoad();
  }
  keyFlag = !keyFlag;
  Serial.println(pos);
}

void checkCommand()
{
   customKey = customKeypad.getKey();

   while(customKey == '0')
{
Serial.println("no command");
customKey = customKeypad.getKey();
}
   if (customKey == '1')
   {
  Serial.print(customKey);
  Serial.println(" out");
 
  for (wristPos; wristPos < 179; wristPos++)
  {
  wrist.write(wristPos);              // tell servo to go to position in variable 'pos'
  delay(30);
  }
  for (waistPos; waistPos < 179; waistPos++)
  {
  waist.write(waistPos);              // tell servo to go to position in variable 'pos'
  delay(30);
  }
  receiveLoad();
  getBack();
   }
   
   if (customKey == '2')
   {
  Serial.print(customKey);
  Serial.println(" in");
  shoulder.write(130);
  for (shoulderPos; shoulderPos < 160; shoulderPos++)
  {
   shoulder.write(shoulderPos);              // tell servo to go to position in variable 'pos'
delay(30);
  }     
  Serial.println(waistPos);
  for (waistPos ; waistPos > 85; waistPos--)
  {
  waist.write(waistPos);              // tell servo to go to position in variable 'pos'
  delay(30);
  }
    
  receiveLoad();
  getBack();
   }

}

void loop() 
  int keyOn = digitalRead(key);

  automatic = digitalRead(2);  
  manual = digitalRead(4);
  
  
  if(manual) //manual
  {
    runManual();
getBack();
  }
  
  if(!manual && (keyOn == LOW))//automatic
  {
    runAutomatic();   
  }
 } 

All is well :)

No comments:

Post a Comment