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

 
 


Monday, November 25, 2013

The Progression of the Tree-Cutter

So far, the Tree-Cutter is coming along fairly well. I have nearly completed the physical robot along with the wiring. I still need to do some programming but I am feeling optimistic about it as a whole.

 
The mechanical part is nearly done except for one part. I am a few parts short of making the arm that will be used to cut the tree. I will mount it on the other side of the claw where I have room. I was able to put the Sabertooth and battery underneath the top platform to free up some space and conceal some of the wiring. I put the Sabertooth and LCD screen into TX ports like I did before and the servos and IR sensors to PWM ports. Everything turns on so I just need to complete the programming part to bring it all together.


 
 
For the programming, I am trying to work in a linear fashion instead of jumping around from line to line. This makes it much easier to follow and write as I progress through this. I have the beginning part programmed so far. I am still working on the setup for the sensors but once I have that it will hopefully go smoothly.



/*
TX
3 LCD screen
2 Speed controller

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

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

byte clrscn = 12;          //variable to clear the LCD screen
int tree = 0;              //number of trees to be cut
long int remote = 0;          //value received from the remote

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(100);
  Serial2.write(228);
  delay(500);
}

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

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

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

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
}


void loop()
{ 
  halt();
 
  Serial3.write(clrscn);
  Serial3.print("Enter a");
  delay(2000);
  Serial3.write(clrscn);
  Serial3.print("Number 1-9");
  delay(2000);
  Serial3.write(clrscn);
 
  while(remote == 0)     //enters this loop if the IR sensor detects a signal
  {
     irrecv.decode(&results);
     long int remote = results.value;    //puts the signal received by the remote into an integer type long
  }   
     Serial.println(results.value);      //prints to the serial monitor to troubleshoot
     
   switch(remote)             //switch case structure for the results that the IR sensor receives from the remote
    {
      case 1 || case 2049:                //if 1 is pressed
        Serial3.write(12);        //clears the screen
        delay(5);
        Serial3.print("One");
        int tree = 1;
        //irrecv.resume();          // Receive the next value
        break;   
     
      case 2 || case 2050:                //if 2 is pressed
        Serial3.write(12);        //clears the screen
        delay(5);
        Serial3.print("Two");
        int tree = 2;
        //irrecv.resume();          // Receive the next value
        break;     
     
      case 3 || case 2051:                //if 3 is pressed
        Serial3.write(12);        //clears the screen
        delay(5);
        Serial3.print("Three");
        int tree = 3;
        //irrecv.resume();          // Receive the next value
        break;
       
      case 4 || case 2052:          //if 4 is pressed
        Serial3.write(12);
        delay(5);
        Serial3.print("Four");
        int tree = 4;
        //irrecv.resume();          // Receive the next value
        break;
       
      case 5 || case 2053:                //if 5 is pressed
        Serial3.write(12);        //clears the screen
        delay(5);
        Serial3.print("Five");
        int tree = 5;
        //irrecv.resume();          // Receive the next value
        break;
     
       case 6 || case 2054:              //if 6 is pressed
        Serial3.write(12);
        delay(5);
        Serial3.print("Six");
        int tree = 6;
        //irrecv.resume();          // Receive the next value
        break;
       
      case 7 || case 2055:                //if 7 is pressed
        Serial3.write(12);        //clears the screen
        delay(5);
        Serial3.print("Seven");
        int tree = 7;
        //irrecv.resume();          // Receive the next value
        break;
       
      case 8 || case 2056:                    //if 8 is pressed
        Serial3.write(12);
        delay(5);
        Serial3.print("Eight");
        int tree = 8;
        //irrecv.resume();          // Receive the next value
        break;
       
      case 9 || case 2057:                //if 9 is pressed
        Serial3.write(12);        //clears the screen
        delay(5);
        Serial3.print("Nine");
        int tree = 9;
        //irrecv.resume();          // Receive the next value
        break;
       
      default:              //if none of the above buttons are pressed
        Serial3.write(12);
        delay(5);
        Serial3.print("Enter a");
        delay(2000);
        Serial3.print("Number 1-9");
        halt();
        delay(100);
        irrecv.resume();          // Receive the next value
    }
   
  /*
    rightServo.attach(12);      //Sets the right servo to I/O 12
    leftServo.attach(13);
  */
}

Tuesday, November 12, 2013

Starting The Tree-Cutter

We have to make a robot for our final project. We were able to choose from any robotic application as long as it was agile and could be constructed in a reasonable amount of time. After doing some research, I decided on a robot that would cut down trees(in theory).

I thought up a design and I came up with a fairly wide robot on tracks. I will need a ping or IR sensor on a positional servo to sense for trees. Then it will drive towards the tree until it is close enough. It will look for a color painted on a tree. If it is there, then it will start cutting. if it is not there, it will turn around and keep looking. It will continue to do this until it has cut a specified number of trees. Here is a flowchart of what I hope it will do.

 
 
My first challenge was finding a suitable chassis. I looked at several that I found that were already built but I decided that it would be easier to start from the beginning to get exactly what I want. I started with the tracks so I could get a good base. After working out some minor bugs, I think I know how I need to configure it for the tracks to fit. I don't have much yet but it's a work in progress.
 
 

  

Monday, October 28, 2013

Using a PIR and an IR sensor

This assignment we were given two different sensors and we needed to make them work together. I chose the PIR sensor and the IR sensor and Remote.

 
 
 
I was able to keep the wiring for the Sabertooth and the LCD screen. I had to look up data sheets for the PIR and IR sensors to see which pins needed connected. Both the PIR and IR sensors went to power and ground and they each had a signal wire. I was able to use a PWM for both of these sensors. I did not use the encoders because I thought that they would cause too many problems while the program was running. (Sorry the pictures are upside-down. I flipped them but they still uploaded this way.)
 


 
 
The programming wasn't as bad as I thought it would be. I split the programming up into two parts to make it easier. First I worked with the PIR Sensor. I read the data sheet to familiarize myself with it and then found some basic code. I needed to set the pin as an Input because I was using a PWM port. That part was easy but the more difficult part was setting the PIR variable to whatever the PIR sensor reads. After some thinking, I figured out that this could be done by putting that line in a loop that would kick out once it changed to HIGH. The IR and remote was much harder. I had to download a library to make the IR sensor receive signals from the remote and then decode those signals. It made it a lot easier to print those values to the Serial Monitor and write them down to determine the code that each button sent to the IR sensor. Once I had all of the codes, I used a switch case structure inside of a loop to decide what the robot would do if a certain button was pressed. I used 2, 4, 6, and 8 for forward, left turn, right turn, and backwards respectfully.
 
 
 
/*
Brian Swanson

I used a PIR sensor to first detect movement anad then take movement
commands from a remote.

Parts:
Arduino Mega
Sabertooth Speed Controller
2 Motors
1 Breadboard
1 LCD screen
1 PIR sensor
1 IR sensor with remote

Connections:
Both motors to the Sabertooth
Sabertooth to TX2
LCD to TX3
IR sensor to PIN 11
PIR to PIN 2


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

int PIR = 0;             //sets PIR sensor to 0
byte clrscn = 12;          //variable to clear the LCD screen
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(100);
  Serial2.write(228);
  delay(500);
}
void backwards()          //function to make the robot move backwards
{
  Serial2.write(28);
  Serial2.write(155);
  delay(500);
}
void turnLeft()            //function to turn left
{
  Serial2.write(100);
  Serial2.write(154);
  delay(250);
}
void turnRight()          //function to turn right
{
  Serial2.write(28);
  Serial2.write(228);
  delay(250);
}
void setup()
{
  pinMode(2, INPUT);          //makes the PIR sensor an input
  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
}

void loop()
{
  halt();
 
  PIR = 0;          //sets the PIR sensor to 0. should already be at 0 but to double-check
   
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("DO NOT MOVE");
  delay(30000);                  //setup time for PIR sensor between 10-60s. mine is 30
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("Setup Ready");
  delay(1000);
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("Looking");
 
  while (PIR == LOW)            //loops until the PIR sensor sees something
  {
    delay(100);
    PIR = digitalRead(2);      //sets PIR equal to whatever the PIR sensor reads
  }
 
    Serial3.write(clrscn);
    delay(5);
    Serial3.print("Movement");
    delay(2000);
   
  while(true)          //loops forever to enter the remote phase
  {
    if (irrecv.decode(&results))     //enters this loop if the IR sensor detects a signal
    {
      long int code = results.value;    //puts the signal received by the remote into an integer type long
     
      Serial.println(results.value);      //prints to the serial monitor to troubleshoot
     
      switch(code)             //switch case structure for the results that the IR sensor receives from the remote
      {
      case 2050:                //if 2 is pressed
        Serial3.write(12);        //clears the screen
        delay(5);
        Serial3.print("two");
        forward();
        delay(50);
        halt();
        irrecv.resume();          // Receive the next value
        break;     
     
      case 2:                  //if 2 is pressed
        Serial3.write(12);
        delay(5);
        Serial3.print("two");
        forward();
        delay(50);
        halt();
        irrecv.resume();          // Receive the next value
        break;     
     
      case 8:                    //if 8 is pressed
        Serial3.write(12);
        delay(5);
        Serial3.print("eight");
        backwards();
        delay(50);
        halt();
        irrecv.resume();          // Receive the next value
        break;
       
      case 2056:                //if 8 is pressed
        Serial3.write(12);
        delay(5);
        Serial3.print("eight");
        backwards();
        delay(50);
        halt();
        irrecv.resume();          // Receive the next value
        break;
       
      case 2054:              //if 6 is pressed
        Serial3.write(12);
        delay(5);
        Serial3.print("six");
        turnRight();
        delay(50);
        halt();
        irrecv.resume();          // Receive the next value
        break;
       
      case 6:              //if 6 is pressed
        Serial3.write(12);
        delay(5);
        Serial3.print("six");
        turnRight();
        delay(50);
        halt();
        irrecv.resume();          // Receive the next value
        break;
       
      case 2052:          //if 4 is pressed
        Serial3.write(12);
        delay(5);
        Serial3.print("four");
        turnLeft();
        delay(50);
        halt();
        irrecv.resume();          // Receive the next value
        break;
       
      case 4:              //if 4 is pressed
        Serial3.write(12);
        delay(5);
        Serial3.print("four");
        turnLeft();
        delay(50);
        halt();
        irrecv.resume();          // Receive the next value
        break;
       
      default:              //if none of the above buttons are pressed
        Serial3.write(12);
        delay(5);
        Serial3.print("nothing");
        halt();
        delay(100);
        irrecv.resume();          // Receive the next value
   }
  }
 }
}




 
 
 

Tuesday, October 22, 2013

Using Encoders to go move

This assignment required us to use encoders to have our robot move straight and then make a 90 deg turn to the left and then the right.

 
The wiring was the same for the Sabertooth as last time. I was still using the Sabertooth in Simplified Serial mode so I only needed the one signal wire to TX2. Each encoder had 4 wires that needed to be connected. Two of those wires are power and ground and the other two are signal wires. One of these signal wires need to be connected to an interrupt port and the other could be connected to any other port.
 

 
 
 
The programming was by far the hardest part. First we had to include the encoder library and some prewritten code to count the encoder values. I had the program print the encoder values in each function to see where my wheels are. I used while and if loops to decide what the robot would do according to where the wheels are.  I also found that I needed to reset my encoder values for the when the robot stops so it is ready to count for the next function. I had to slow the robot down to make the driving more precise. I had problems with my robot veering slightly at the end of its straight run but I was able to solve this by decreasing the encoder count that it traveled.
 
 
 
 

#include <digitalWriteFast.h>
#include <Servo.h>

/*
Brian Swanson
We used encoders attached to our motors to program our robot to go straight and
then make two 90 deg turns.

Parts:
Arduino Mega
Sabertooth speed controller
2 Motors with encoders
1 Breadboard

Connections:
Both motors connected to the Sabertooth
Sabertooth to TX2

*/
//this part is not me
// It turns out that the regular digitalRead() calls are too slow and bring the arduino down when
// I use them in the interrupt routines while the motor runs at full speed creating more than
// 40000 encoder ticks per second per motor.

// Quadrature encoders
// Left encoder
#define c_LeftEncoderInterrupt 0  //This is the number of the interrupt NOT the pin
#define c_LeftEncoderPinA 2       //This is the PIN on the board and the one for the interrupt above
#define c_LeftEncoderPinB 4      // This is any other pin
#define LeftEncoderIsReversed
volatile bool _LeftEncoderBSet;
volatile long _LeftEncoderTicks = 0;

// Right encoder
#define c_RightEncoderInterrupt 1  // This is the number of the interrupt NOT the pin
#define c_RightEncoderPinA 3        // This is the PIN on the board and the one for the interrupt above
#define c_RightEncoderPinB 5        // This is any other pin
volatile bool _RightEncoderBSet;
volatile long _RightEncoderTicks = 0;

Servo _RightServo;  // create servo object to control right motor
Servo _LeftServo;  // create servo object to control left motor

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

 //_RightServo.attach(2);  // attaches the servo on specified pin to the servo object
  //_LeftServo.attach(3);  // attaches the servo on specified pin to the servo object

  // Quadrature encoders
  // Left encoder
  pinMode(c_LeftEncoderPinA, INPUT);      // sets pin A as input
  digitalWrite(c_LeftEncoderPinA, LOW);  // turn on pullup resistors
  pinMode(c_LeftEncoderPinB, INPUT);      // sets pin B as input
  digitalWrite(c_LeftEncoderPinB, LOW);  // turn on pullup resistors
  attachInterrupt(c_LeftEncoderInterrupt, HandleLeftMotorInterruptA, RISING);

  // Right encoder
  pinMode(c_RightEncoderPinA, INPUT);      // sets pin A as input
  digitalWrite(c_RightEncoderPinA, LOW);  // turn on pullup resistors
  pinMode(c_RightEncoderPinB, INPUT);      // sets pin B as input
  digitalWrite(c_RightEncoderPinB, LOW);  // turn on pullup resistors
  attachInterrupt(c_RightEncoderInterrupt, HandleRightMotorInterruptA, RISING);
}

// Interrupt service routines for the left motor's quadrature encoder
void HandleLeftMotorInterruptA()
{
  // Test transition; since the interrupt will only fire on 'rising' we don't need to read pin A
  _LeftEncoderBSet = digitalReadFast(c_LeftEncoderPinB);   // read the input pin

  // and adjust counter + if A leads B
  #ifdef LeftEncoderIsReversed
    _LeftEncoderTicks -= _LeftEncoderBSet ? -1 : +1;
  #else
    _LeftEncoderTicks += _LeftEncoderBSet ? -1 : +1;
  #endif
}

// Interrupt service routines for the right motor's quadrature encoder
void HandleRightMotorInterruptA()
{
  // Test transition; since the interrupt will only fire on 'rising' we don't need to read pin A
  _RightEncoderBSet = digitalReadFast(c_RightEncoderPinB);   // read the input pin

  // and adjust counter + if A leads B
  #ifdef RightEncoderIsReversed
    _RightEncoderTicks -= _RightEncoderBSet ? -1 : +1;
  #else
    _RightEncoderTicks += _RightEncoderBSet ? -1 : +1;
  #endif
}

//This is me

void forward()                      //This function prints the encoder values to the Serial Monitor
{
  Serial.print(_LeftEncoderTicks);
  Serial.print("                       ");
  Serial.print(_RightEncoderTicks);
  Serial.println();
  delay(10);
 
  if(_RightEncoderTicks == _LeftEncoderTicks)  //if the encoder values are the same, the robot moves straight
  {
    Serial2.write(110);
    Serial2.write(238);
    delay(100);
  } 
   
  if(_RightEncoderTicks < _LeftEncoderTicks)  //if the right wheel is slower, it slows down the left wheel
  {
    Serial2.write(110);
    Serial2.write(221);
    delay(10);
  }
  
  if(_RightEncoderTicks > _LeftEncoderTicks)  //if the left wheel is slower, it slows the right wheel
  {
    Serial2.write(90);
    Serial2.write(238);
    delay(10);
  }
}

void halt()    //this function resets the encoder values, prints the values, anad stops the robot
{
  Serial2.write(0);
  _RightEncoderTicks = 0;
  _LeftEncoderTicks = 0;
  Serial.print(_LeftEncoderTicks);
  Serial.print("                       ");
  Serial.print(_RightEncoderTicks);
  Serial.println();
  delay(10);
  delay(2000);
}

void turnRight()
{
  while(_LeftEncoderTicks < 7000)    //continues until the left encoder is 7000
  {
    Serial.print(_LeftEncoderTicks);    //prints the encoder values
    Serial.print("                       ");
    Serial.print(_RightEncoderTicks);
    Serial.println();
    delay(10);
    Serial2.write(230);
    Serial2.write(16);
    delay(10);
  }
}

void turnLeft()
{
  while(_RightEncoderTicks < 7000)    //continues until the right encoder is 7000
  {
    Serial.print(_LeftEncoderTicks);    //prints the encoder values
    Serial.print("                       ");
    Serial.print(_RightEncoderTicks);
    Serial.println();
    delay(10);
    Serial2.write(112);
    Serial2.write(143);
    delay(10);
  }
}

void loop()
{ 
  halt();
 
  while(_RightEncoderTicks < 20000)    //the robot will go straight until the right encoder reaches 20000
  {
    forward();
  }
 
  halt();
 
  turnLeft();
  halt();
  turnRight();
  halt(); 

  delay(20);
}
 
 



Tuesday, October 15, 2013

Sabertooth with Serial commands

This assignment was similar to the last one but with simplified programming. This was very similar to the last assignment with the same moves and displays on the screen.

 
 
The wiring for this assignment was almost identical to the last one. The Sabertooth had less wires going to the Arduino because we switched to the Simplified Serial mode. This made it easier to control the motors and less cluttered. We also did not need power going to the Arduino from the Sabertooth because the power was a little redundant. The Simplified Serial mode also required us to move the signal wire to a TX port so it was able to control both motors.
 
 
 
The programming was simple and much easier for this assignment for several reasons. I didn't have to include any libraries, assign any variables, or define any servos. This made reading the program much easier and more straight-forward. I did have some trouble with one of my motors not moving at full speed but I found that it was a programming error and I was able to fix it (after switching many other things only to find that they weren't my problem). 
 
 
 
/*
Brian Swanson
AR II

This program is the first to use Serial.write to control the speed controllers. 
Parts:
Arduino Mega
2 Motors
1 Sabertooth Dual Speed Controller
1 Breadboard
1 Switch

Connections:
Sabertooth connected to TX2
LCD Screen connected to TX3
Battery, Arduino, Sabertooth to ground
Switch, Arduino, Sabertooth to power

0-both motors stop
1-left full reverse
64-left stop
127-left full forward
128-right full reverse
191-right stop
255-right full forward

*/
void halt()
{
  Serial3.write(12);          //12 is the command to clear the LCD screen
  delay(5);                   //mandatory delay
  Serial3.print("stop");      //prints "stop" to the screen
  Serial2.write(0);          
  delay(1000);
}

void forward()
{
  Serial3.write(12);
  delay(5);
  Serial3.print("forward");
  Serial2.write(127);
  Serial2.write(255);
  delay(1000);
}

void slowForward()
{
  Serial3.write(12);
  delay(5);
  Serial3.print("slowForward");
  Serial2.write(110);
  Serial2.write(238);
  delay(1000);
}

void backwards()
{
  Serial3.write(12);
  delay(5);
  Serial3.print("backwards");
  Serial2.write(1);
  Serial2.write(128);
  delay(1000);
}

void slowBackwards()
{
  Serial3.write(12);
  delay(5);
  Serial3.print("slowBackwards");
  Serial2.write(30);
  Serial2.write(157);
  delay(1000);
}

void turnLeft()
{
  Serial3.write(12);
  delay(5);
  Serial3.print("turnLeft");
  Serial2.write(16);
  Serial2.write(230);
  delay(1000);
}

void turnRight()
{
  Serial3.write(12);
  delay(5);
  Serial3.print("turnRight");
  Serial2.write(112);
  Serial2.write(143);
  delay(1000);
}

void veerRight()
{
  Serial3.write(12);
  delay(5);
  Serial3.print("veerLeft");
  Serial2.write(127);
  Serial2.write(225);
  delay(1000);
  Serial3.write(12);
}

void veerLeft()
{
  Serial3.write(12);
  delay(5);
  Serial3.print("veerLeft");
  Serial2.write(97);
  Serial2.write(255);
  delay(1000);
}

void pause()
{
  Serial2.write(0);
  delay(250);
}
void setup()
{
  Serial2.begin(9600);      //motors and screen on same baud
  Serial3.begin(9600);
}
void loop()
{
  halt();
  forward();
  pause();
  slowForward();
  pause();
  backwards();
  pause();
  slowBackwards();
  pause();
  turnLeft();
  pause();
  turnRight();
  pause();
  veerLeft();
  pause();
  veerRight();
  pause(); 
}

 
 
 
 



Monday, October 7, 2013

Using Different Speed Controllers for the Same Thing

There were two parts to this assignment. In the first part, we used two Parallax speed controllers and then switched to one dual Sabertooth speed controller. We had to do a set of basic maneuvers and display messages on a screen while it went through the process.

 
It took me about 4 or 5 hours to complete the whole assignment and I found out that there was more to be learned than I thought. The wiring for the Parallax speed controllers was similar to the previous assignment except for the Parallax also needed 3-pin connectors that needed to go to the Arduino. The Parallax screen also had a 3-pin connector that I connected to the Arduino with the TX3 port.
 

 
The programming was the part where I learned the most. I had trouble at first with printing to the screen but after looking online, I was able to find out one way to print to the screen. I later revised this due to some advisement and better programming techniques. I watched several videos made by "drbrono" on YouTube and found them very helpful with programming the screen. 
 
 
 
/*
Brian Swanson
AR II

The assignment was to use the Arduino to control the two Parallax speed controllers.
Parts:
Arduino Mega
2 Motors
2 Parallax Speed Controllers
1 Breadboard
1 Switch

Connections:

Left speed controller to PWM 13
Right speed controller to PWM 12
Battery, Arduino, Right and Left Speed controller to ground
Switch, Arduino, Right and Left Speed controller to power

*/
#include <Servo.h>          //Adds Library for servos
Servo left;
Servo right;

int leftMotor = 13;        //sets motors for PWM ports
int rightMotor = 12;
byte clrscn = 12;          //sends a byte instead of a bit for the clear screen command

void forward()
{
  Serial3.write(clrscn);          //writes to TX3 and RX3 to clear the screen
  delay(5);                        //mandatory delay after command
  Serial3.print("forward");        //prints "forward" on the screen
  left.writeMicroseconds(1900);
  right.writeMicroseconds(1800);
  delay(1000);
}

void slowForward()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("slowForward");
  left.writeMicroseconds(1650);
  right.writeMicroseconds(1650);
  delay(1000);
}

void backward()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("backward");
  left.writeMicroseconds(1000);
  right.writeMicroseconds(1200);
  delay(1000);
}

void slowBackward()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("slowBackward");
  left.writeMicroseconds(1000);
  right.writeMicroseconds(1300);
  delay(1000);
}

void rightTurn()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("rightTurn");
  left.writeMicroseconds(2000);
  right.writeMicroseconds(1000);
  delay(1000);
}

void leftTurn()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("leftTurn");
  left.writeMicroseconds(1000);
  right.writeMicroseconds(2000);
  delay(1000);
}

void veerRight()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("veerRight");
  left.writeMicroseconds(1900);
  right.writeMicroseconds(1700);
  delay(2000);
}

void veerLeft()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("veerLeft");
  left.writeMicroseconds(1700);
  right.writeMicroseconds(1900);
  delay(1000);
  Serial3.write(clrscn);
}

void motorStop()
{
  left.writeMicroseconds(1500);
  right.writeMicroseconds(1500);
  delay(1000);
}

void motorPause()
{
  left.writeMicroseconds(1500);
  right.writeMicroseconds(1500);
  delay(250);
}

void setup()
{
  left.attach(leftMotor);
  right.attach(rightMotor);
  Serial3.begin(9600);          //sets the baud of the motors and the screen the same
  Serial.begin(9600);
}

void loop()
{
  Serial3.write(17);      //command to turn the backlight on
  motorStop();
  forward();
  motorPause();
  slowForward();
  motorPause();
  backward();
  motorPause();
  slowBackward();
  motorPause();
  leftTurn();
  motorPause();
  rightTurn();
  motorPause();
  veerRight();
  motorPause();
  veerLeft(); 
  Serial3.write(18);      //command to turn the backlight off
  delay(1000);
}

 

  

 

 


 

 
 

The wiring for the Sabertooth was the biggest difference between it and the Parallax speed controllers. It was much less cluttered with just one speed controller in my robot. I had to take four wires from the Sabertooth to the Arduino to control each motor, power, and ground. The other side of the Sabertooth had connections for two wires from each motor and a spot for power and ground. This is where I tied together power from the Arduino and the switch with the Sabertooth and ground from the Arduino and battery with the Sabertooth.



The programming was identical to the first but I still had to work out some bugs. I had to switch the wires going into the Sabertooth to change the direction that the wheels were spinning. after solving all of these problems, I was able to complete this project.

Robot with Sabertooth dual speed controller


Sorry the video is upside down and there is a spot where it loses traction fro a second.


/*
Brian Swanson
AR II

The assignment was to use the Arduino to control the Sabertooth dual speed controller.
Parts:
Arduino Mega
2 Motors
1 Sabertooth Dual Speed Controller
1 Breadboard
1 Switch

Connections:
Left speed controller to PWM 13
Right speed controller to PWM 12
Battery, Arduino, and Sabertooth to ground
Switch, Arduino, and Sabertooth to power

*/
#include <Servo.h>          //Adds Library for servos
Servo left;
Servo right;

int leftMotor = 13;        //sets motors for PWM ports
int rightMotor = 12;
byte clrscn = 12;          //sends a byte instead of a bit for the clear screen command

void forward()
{
  Serial3.write(clrscn);          //writes to TX3 and RX3 to clear the screen
  delay(5);                        //mandatory delay after command
  Serial3.print("forward");        //prints "forward" on the screen
  left.writeMicroseconds(1900);
  right.writeMicroseconds(1800);
  delay(1000);
}

void slowForward()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("slowForward");
  left.writeMicroseconds(1650);
  right.writeMicroseconds(1650);
  delay(1000);
}

void backward()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("backward");
  left.writeMicroseconds(1000);
  right.writeMicroseconds(1200);
  delay(1000);
}

void slowBackward()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("slowBackward");
  left.writeMicroseconds(1000);
  right.writeMicroseconds(1300);
  delay(1000);
}

void rightTurn()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("rightTurn");
  left.writeMicroseconds(2000);
  right.writeMicroseconds(1000);
  delay(1000);
}

void leftTurn()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("leftTurn");
  left.writeMicroseconds(1000);
  right.writeMicroseconds(2000);
  delay(1000);
}

void veerRight()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("veerRight");
  left.writeMicroseconds(1900);
  right.writeMicroseconds(1700);
  delay(2000);
}

void veerLeft()
{
  Serial3.write(clrscn);
  delay(5);
  Serial3.print("veerLeft");
  left.writeMicroseconds(1700);
  right.writeMicroseconds(1900);
  delay(1000);
  Serial3.write(clrscn);
}

void motorStop()
{
  left.writeMicroseconds(1500);
  right.writeMicroseconds(1500);
  delay(1000);
}

void motorPause()
{
  left.writeMicroseconds(1500);
  right.writeMicroseconds(1500);
  delay(250);
}

void setup()
{
  left.attach(leftMotor);
  right.attach(rightMotor);
  Serial3.begin(9600);          //sets the baud of the motors and the screen the same
  Serial.begin(9600);
}

void loop()
{
  Serial3.write(17);      //command to turn the backlight on
  motorStop();
  forward();
  motorPause();
  slowForward();
  motorPause();
  backward();
  motorPause();
  slowBackward();
  motorPause();
  leftTurn();
  motorPause();
  rightTurn();
  motorPause();
  veerRight();
  motorPause();
  veerLeft(); 
  Serial3.write(18);      //command to turn the backlight off
  delay(1000);
}