Monday, December 9, 2013

The Final Blog

After many classes, hours of blood, sweat, and tears, I present to you my robot.


 


This robot challenged me in every way possible. I started with the mechanical part because I felt that it would be the easiest part for me. I had to think about what the robot would be used for and what terrain it would be used in. I settled on tracks because I could make the robot wide for stability and it would be able to climb over obstacles among other things. I was inspired with the claw by researching other logging equipment and how it works. The SHARP IR sensor provides me with an easy and efficient way to look around for trees without turning the whole robot. I put the battery and speed controller on the bottom platform of the robot for stability and to make it a little more visually appealing.

 
 
I ended up using 8 ports: 2 TX, 5 PWM, 1 Analog. I used the same IR library as last time for the remote and I had to look up a formula for the SHARP IR to display it in inches. The programming was confusing at times because of the nested loops but after many tries (and frustrations) I figured it out. I had one main loop that ran until the robot had cut the required number of trees and the others were nested inside one or more loops to look or move the right way. Here is my code.
 
 
 
 
/*Brian SwansonI used multiple sensors to detect and cut down a specified number of trees. TX
3 LCD screen
2 Speed controller

PWM
11 IR remote
10 SHARP Positional Servo
8 CLAW Postitional Vex
7 Arm Positional Servo
6 Saw Continuous

Analog
9 SHARP IR

Claw Servo
180 = closed
0 = open

SHARP Servo
180 = left
0 = right
45 = straight
35 = look for trees

Saw Servo
2000 = CCW
1500 = stop
1000 = CW

*/
#include <IRremote.h>    //library needed for the ir sensor and remote
#include <Servo.h>      //library needed for servos

Servo sharpServo;
Servo clawServo;
Servo armServo;
Servo sawServo;

int irSensor = 9;          //SHARP is in this port
byte clrscn = 12;          //variable to clear the LCD screen
int tree = 0;              //number of trees to be cut
int remote = 0;            //value received from the remote
int cuttrees = 0;          //number of trees that have been cut
int irValue = 0;            //value the SHARP reads
int pos = 0;                //variable for the position of the servos in the for loop

int RECV_PIN = 11;        //sets RECV_PIN to pin 11
IRrecv irrecv(RECV_PIN);    //sets the IR sensor to pin 11
decode_results results;    //statement to decode the results from the IR sensor

void halt()                //function to stop the robot
{                         
  Serial2.write(0);
  delay(500);
}

void forward()            //function to make the robot move forward
{
  Serial2.write(28);
  Serial2.write(228);
  delay(1000);
}

void inchForward()        //function to slowly move forward
{
  Serial2.write(28);
  Serial2.write(228);
  delay(100);
}

void backwards()          //function to make the robot move backwards
{
  Serial2.write(100);
  Serial2.write(155);
  delay(1000);
}

void turnLeft()            //function to turn left
{
  Serial2.write(100);
  Serial2.write(228);
  delay(250);
}

void turnRight()          //function to turn right
{
  Serial2.write(28);
  Serial2.write(155);
  delay(1500);
}

void openClaw()          //function to open the claw
{
  clawServo.write(0);
}

void closeClaw()          //function to close the claw
{
  clawServo.write(180);
}

void startSaw()          //function to start the saw blade
{
  sawServo.writeMicroseconds(1000);
}

void stopSaw()          //function to stop the saw blade
{
  sawServo.writeMicroseconds(1500);
}

void clearScreen()        //function to clear the LCD screen
{
  Serial3.write(12);
  delay(5);
}

void setup()
{
  Serial.begin(9600);          //makes the Serial monitor, Sabertooth, and the LCD screen on the same level
  Serial2.begin(9600);
  Serial3.begin(9600);
  irrecv.enableIRIn();        //starts the IR receiver
  sharpServo.attach(10);      //Sets the SHARP servo to I/O 10
  clawServo.attach(8);        //Sets the claw servo to I/O 8
  armServo.attach(7);          //Sets the arm servo to I/O 7
  sawServo.attach(6);          //Sets the saw servo to I/O 6
}

void loop()
{ 
  tree = 0;
  remote = 0;
  halt();
 
  clearScreen();                    //this is to tell the user to enter the number of trees they want cut
  Serial3.print("Enter a");
  delay(5000);
  clearScreen();
  Serial3.print("Number 1-9");
  delay(5000);
  clearScreen();
 
  do                            //the program stays in this loop until it receives an input from the remote
  {
    irrecv.decode(&results);   
    remote = results.value;    //puts the signal received by the remote into an integer
    clearScreen();
    Serial3.print("Waiting");
    Serial.println("remote");
    Serial.print(remote);
    delay(100);
    if(remote != 0)            //goes to bailout4 when the number of trees to be cut is specified
    {
      goto bailout4;
    }
  }while(remote == 0);       //exits this loop if the IR sensor detects a signal
 
  bailout4:
  delay(5000);
  //Serial.println(results.value);      //prints to the serial monitor to troubleshoot
     
    do
     { 
      switch(remote)             //switch case structure for the results that the IR sensor receives from the remote
       {
        case 1:                //if 1 is pressed
          clearScreen();
          Serial3.print("One");
          tree = 1;
          break;   
         
        case 2049:                //if 1 is pressed
          clearScreen();
          Serial3.print("One");
          tree = 1;
          break;
     
        case 2:                //if 2 is pressed
          clearScreen();
          Serial3.print("Two");
          tree = 2;
          break;     
         
        case 2050:                //if 2 is pressed
          clearScreen();
          Serial3.print("Two");
          tree = 2;
          break;     
     
        case 3:               //if 3 is pressed
          clearScreen();
          Serial3.print("Three");
          tree = 3;
          break;
         
        case 2051:                //if 3 is pressed
          clearScreen();
          Serial3.print("Three");
          tree = 3;
          break;
        
        case 4:          //if 4 is pressed
          clearScreen();
          Serial3.print("Four");
          tree = 4;
          break;
         
        case 2052:          //if 4 is pressed
          clearScreen();
          Serial3.print("Four");
          tree = 4;
          break;
       
        case 5:                //if 5 is pressed
          clearScreen();
          Serial3.print("Five");
          tree = 5;
          break;
         
        case 2053:                //if 5 is pressed
          clearScreen();
          Serial3.print("Five");
          tree = 5;
          break;
      
         case 6:             //if 6 is pressed
          clearScreen();
          Serial3.print("Six");
          tree = 6;
          break;
         
        case 2054:              //if 6 is pressed
          clearScreen();
          Serial3.print("Six");
          tree = 6;
          break;
       
        case 7:                //if 7 is pressed
          clearScreen();
          Serial3.print("Seven");
          tree = 7;
          break;
         
        case 2055:                //if 7 is pressed
          clearScreen();
          Serial3.print("Seven");
          tree = 7;
          break;
         
        case 8:                    //if 8 is pressed
          clearScreen();
          Serial3.print("Eight");
          tree = 8;
          break;
         
        case 2056:                    //if 8 is pressed
          clearScreen();
          Serial3.print("Eight");
          tree = 8;
          break;
       
        case 9:                //if 9 is pressed
          clearScreen();
          Serial3.print("Nine");
          tree = 9;
          break;
         
        case 2057:                //if 9 is pressed
          clearScreen();
          Serial3.print("Nine");
          tree = 9;
          break;
       
        default:              //if none of the above buttons are pressed
          clearScreen();
          Serial3.print("Default");
          clearScreen();
          Serial3.print("Enter a");
          clearScreen();
          delay(2000);
          Serial3.print("Number 1-9");
          clearScreen();
          halt();
          delay(100);
          irrecv.resume();          // Receive the next value
       }    //end of case
     }while(tree == 0);      //end of while(tree == 0)
 
  delay(5000);
  clearScreen();  
  Serial3.println("Starting Now");
  delay(5000);
  Serial.println("Cut ");
  Serial.println(tree);
  Serial.println("trees.");
 
  while(cuttrees != tree)          //stays in this loop until it has cut the specified number of trees
  {
    delay(500);
    halt();
    openClaw();
    stopSaw();
    sharpServo.write(40);      //puts the SHARP servo at 40 deg
    delay(5000);
   
    while(true)    //moving SHARP to look for trees
    {   
      for(pos = 40; pos < 180; pos += 10)  // goes from 40 degrees to 180 degrees
      {   
        halt();
        clearScreen();
        Serial3.println("Looking");
        sharpServo.write(pos); 
       
        float volts = analogRead(9) * 0.004;      //formula to put the SHARP value in inches
        int irValue = 5*pow(volts, -1.1);  
       
        Serial.println(irValue);
        delay(120);                     
     
        if(irValue < 40 && irValue > 0)        //if a tree is less than 40in away
        {
          clearScreen();
          Serial3.println("Saw Something"); 
          goto bailout; 
        }
      } //end of for loop
      turnRight();
      clearScreen();
      Serial3.println("Looking");
    }//end of while true 
    bailout:
    halt();
    delay(5000);
   
    while(true)      //move robot left until it sees the same tree
    {
      halt();
      sharpServo.write(45);          //puts the SHARP servo at 45 deg
      delay(1000);
     
      float volts = analogRead(9) * 0.004;      //formula to put the SHARP value in inches
      int irValue = 5*pow(volts, -1.1);   
     
      Serial.println(irValue);
      clearScreen();
      Serial3.println("Looking");
     
      if(irValue  < 40 && irValue > 0)          //if it is facing the same tree
      {
        goto bailout2;
      }
      else                                      //if not, turn left
      {
        turnLeft();
      }
    }  //end of while true
    bailout2:
    halt();
    sharpServo.write(10);     //puts the SHARP servo at 10 deg to tell when the tree is within the claw
    delay(5000);
   
    while(true)          //inches forward until the tree is in the claw
    {
      halt();
     
      float volts = analogRead(9) * 0.004;      //formula to put the SHARP value in inches
      int irValue = 5*pow(volts, -1.1);   
     
      Serial.println(irValue);
      clearScreen();
      Serial3.println("Creeping");
     
      if(irValue < 5 && irValue >0)          //if the tree is within the claw
      {
        goto bailout3;
      }
      else                                   //if not, slowly inch forward
      {
        inchForward();
      }
    }      //end of while true
    bailout3:
    halt();
    closeClaw();
    startSaw();
    delay(5000);
    clearScreen();
    Serial3.println("Cutting");
   
    for(pos = 45; pos < 135; pos += 5)      //move arm to cut tree. delay is there to allow for optimal cutting
    {
      armServo.write(pos);
      delay(100);
    }
    armServo.write(0);        //puts the arm back to its original position
    stopSaw();
    delay(5000);
   
    openClaw();              //this is to push the tree over after it has been cut
    forward();
    delay(1000);
   
    halt();                  //this is for the robot to reorient itself for more cutting     
    backwards();
    delay(1000);
    turnRight();
    delay(1000);
   
    halt();
    cuttrees++;
  }         //end of while(cuttrees != tree) 
}          //end of void loop

 
 


No comments:

Post a Comment