Difference between revisions of "Home Depot Special"

From LVL1
Jump to navigation Jump to search
(Created page with "__NOTOC__ Home depot special is a 'bot Brian Wagner built for the fall 2010 LVL1 Sumobot competition. == Components == I will get pictures up soon, but until then here are t...")
 
 
(One intermediate revision by the same user not shown)
Line 34: Line 34:
  
 
<pre>
 
<pre>
//#include <AFMotor.h>
 
#include <PololuQTRSensors.h>  //we're using the pololuQTR sensor library so we must attach it.
 
  
PololuQTRSensorsRC qtr((unsigned char[]) {18,19}, 2, 2000, 255); //declares two line sensors on pins 18 and 19
 
                                                                  //this corresponds to analog pins 4 and 5
 
unsigned int sensors[2];
 
  
  const int motor1Pin = 3;    // right motor (directional pin 1)
+
/*************************************************************
  const int motor2Pin = 2;   // right motor (directional pin 2)
+
Sumo Competition program by Brian Wagner
  const int motor1Pin2 = 4;   //left motor (directional pin 1)
+
10/24/10
  const int motor2Pin2 = 5;   //left motor (directional pin 2)
+
*
  const int enablePin1 = 9;   //right speed control
+
*************************************************************/
  const int enablePin2 = 10; //left speed control
+
 
 
+
#include <PololuQTRSensors.h>
  const int linethreshold = 300;
+
#include <PololuSerial.h>
  const int rangethreshold = 30;
+
#include <NewSoftSerial.h>
 +
 
 +
#define LineSensorLeftPin 3
 +
#define LineSensorRightPin 2
 +
#define RangeSensorPin 0
 +
 
 +
int LineLeft = 0;
 +
int LineRight = 0;
 +
int Range = 0;
 +
 
 +
int LineThreshold = 800;
 +
int RangeThreshold = 100;
 +
 
 +
int SpinDir = 0;
 +
int spiral = 20;
 +
 
 +
#define rxPin 0
 +
#define txPin 4
 +
#define rstPin 5
  
int rangepin = 0; //distance sensor 1
+
NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);
 +
PololuSerial motor = PololuSerial(&mySerial,rstPin);
 +
byte fwVersion = -1;
  
void setup() {
+
unsigned int sensors[2];
  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging
+
PololuQTRSensorsRC qtr((unsigned char[]) {LineSensorLeftPin,LineSensorRightPin}, 2, 2000, 255); //declares two line sensors on pins 2,3
 
 
    // set all the other pins you're using as outputs:
 
    pinMode(motor1Pin, OUTPUT);
 
    pinMode(motor2Pin, OUTPUT);
 
    pinMode(motor1Pin2, OUTPUT);
 
    pinMode(motor2Pin2, OUTPUT);
 
    pinMode(enablePin1, OUTPUT);
 
    pinMode(enablePin2, OUTPUT);
 
    pinMode(13, OUTPUT);
 
  
    // set enablePin high so that motor can turn on:
+
void setup() 
    analogWrite(enablePin1, 255); //255 is full speed, 0 is stopped
+
{
    analogWrite(enablePin2, 255); //255 is full speed, 0 is stopped
+
  mySerial.begin(9600);
 +
  motor.begin();
 +
  motor.stopBothMotors();
 +
  delay(5000);
 
}
 
}
  
void loop() {
+
void loop()  
//THE MAIN PROGRAM LOOP
+
{
 +
  //check line sensors
 
   qtr.read(sensors);
 
   qtr.read(sensors);
  delay(20); //a possibly redundant delay
 
 
 
//DEBUG STATEMENTS
 
//if things aren't working right try uncommenting the lines below and checking the serial monitor
 
//Serial.println(read_gp2d12_range(rangepin));
 
Serial.print(sensors[0]);
 
Serial.print("          ");
 
Serial.println(sensors[1]);
 
 
//********** MOTOR TEST CODDE ********************
 
//*******************************************
 
//left motor forward (turn right)
 
//digitalWrite(motor1Pin2, LOW);  // set leg 1 of the H-bridge low **left motor forward
 
//digitalWrite(motor2Pin2, HIGH);  // set leg 2 of the H-bridge high
 
//delay(1000);
 
//right motor forward (turn left)
 
//digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low  **right motor forward
 
//digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
 
//delay(1000);
 
//both forward
 
//digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low  **right motor forward
 
//digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
 
//digitalWrite(motor1Pin2, LOW);  // set leg 1 of the H-bridge low **left motor forward
 
//digitalWrite(motor2Pin2, HIGH);  // set leg 2 of the H-bridge high
 
//delay(1000);
 
//digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge low  **right motor reverse
 
//digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
 
//digitalWrite(motor1Pin2, HIGH);  // set leg 1 of the H-bridge low  **left motor reverse
 
//digitalWrite(motor2Pin2, LOW);  // set leg 2 of the H-bridge high
 
//delay(1000);
 
 
 
   
 
   
//**********  MAIN CODE  *********************
+
  if (sensors[0] < LineThreshold && sensors[1] < LineThreshold) {
//*********************************************
+
    motor.stopBothMotors();
+
     motor.motor0Reverse(127);
//CHECK THE LINE SENSORS
+
     motor.motor1Reverse(127);
  if (sensors[0] < linethreshold && sensors[1] < linethreshold) {
+
     delay(500);
  //UH-OH... BOTH SENSORS DETECT THE BORDER, LET'S BACKUP AND SPIN
+
     motor.motor1Forward(127);
    digitalWrite(13, HIGH); //turn on light to indicate it senses the border
+
     delay(300);
     digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge low  **right motor reverse
+
     motor.stopBothMotors();
     digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge high
+
  }
     digitalWrite(motor1Pin2, HIGH);   // set leg 1 of the H-bridge low  **left motor reverse
+
   else if (sensors[0] < LineThreshold && sensors[1] > LineThreshold) {
     digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge high
+
     motor.stopMotor0();
     delay(500); //do this for half of a second
+
     motor.motor1Forward(127);
      
+
     delay(40);
    digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low  **right motor stopped
+
     motor.motor0Forward(127);
     digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge high
+
    SpinDir = 1;
     digitalWrite(motor1Pin2, HIGH);   // set leg 1 of the H-bridge low  **left motor reverse
+
    spiral = 20;
     digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge high
 
     delay(500); //do this for half of a second
 
 
   }
 
   }
  else if (sensors[0] < linethreshold && sensors[1] > linethreshold) {
+
else if (sensors[0] > LineThreshold && sensors[1] < LineThreshold) {
    //RIGHT SENSOR DETECTS BORDER, TURN LEFT
+
  motor.stopMotor1();
    digitalWrite(13, HIGH); //turn on light to indicate it senses the border
+
     motor.motor0Forward(127);
     digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low  **right motor forward
+
     delay(40);
     digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
+
     motor.motor1Forward(127);
     digitalWrite(motor1Pin2, LOW);  // set leg 1 of the H-bridge low  **left motor stopped
+
     SpinDir = 0;
    digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge high
+
     spiral = 20;
     delay(10); //slight delay to keep the program running smoothly
+
  }
  }
+
  else {
  else if (sensors[0] > linethreshold && sensors[1] < linethreshold) {
+
       
    //LEFT SENSOR DETECTS BORDER, TURN RIGHT
+
     Range = analogRead(RangeSensorPin);
    digitalWrite(13, HIGH); //turn on light to indicate it senses the border
+
     if ( Range > RangeThreshold ) {
     digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low  **right motor stopped
+
        //Charge!!!
    digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge high
+
        motor.motor0Forward(127);
    digitalWrite(motor1Pin2, LOW);  // set leg 1 of the H-bridge low **left motor forward
+
        motor.motor1Forward(127);
    digitalWrite(motor2Pin2, HIGH);  // set leg 2 of the H-bridge high
+
        delay(100);
     delay(10); //slight delay to keep the program running smoothly
 
  }
 
  else {
 
    //WITHIN BORDERS
 
     if (read_gp2d12_range(rangepin) < rangethreshold) {
 
    //IT SEES AN OPPONENT --> CHARGE!
 
    digitalWrite(13, HIGH);  //turn on a light to indicate it sees an opponent
 
    analogWrite(enablePin1, 255);
 
    analogWrite(enablePin2, 255);
 
    digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low **right motor forward
 
    digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
 
    digitalWrite(motor1Pin2, LOW);  // set leg 1 of the H-bridge low **left motor forward
 
    digitalWrite(motor2Pin2, HIGH); // set leg 2 of the H-bridge high
 
    delay(100); //a longer delay for the charge
 
 
     }
 
     }
 
     else {
 
     else {
      //LOOKING FOR OPPONENT BY SPINNING IN PLACE
+
    //let's look around
       digitalWrite(13, LOW); //turn off the light to indicate it doesn't see an opponent
+
       if (SpinDir == 1 ) {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low  **right motor forward
+
        motor.motor0Forward(80);
       digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
+
        motor.motor1Forward(80);
      digitalWrite(motor1Pin2, HIGH);   // set leg 1 of the H-bridge low **left motor reverse
+
       } else {
       digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge high
+
        motor.motor1Forward(80);
      delay(10); //slight delay to keep the program running smoothly
+
        motor.motor0Forward(80);  
 +
       }
 +
        delay(20);  
 +
        //spiral = spiral +20;   
 +
      // if (spiral > 100) { spiral = 20; }
 +
        //motor.stopBothMotors();
 
     }
 
     }
  }
+
   
 
+
}
 
+
 
 
}
 
}
 
 
float read_gp2d12_range(byte pin) {  //method for reading the range sensor(s)
 
//probably shouldn't mess with this stuff... I didn't do it
 
int tmp;
 
tmp = analogRead(pin);
 
if (tmp < 3)
 
return -1; // invalid value
 
return (6787.0 /((float)tmp - 3.0)) - 4.0;
 
}
 
 
 
</pre>
 
</pre>

Latest revision as of 10:14, 24 March 2011


Home depot special is a 'bot Brian Wagner built for the fall 2010 LVL1 Sumobot competition.


Components

I will get pictures up soon, but until then here are the components I used...

Motor and wheels http://www.solarbotics.com/products/gmpw_deal/

Casters to maintain balance http://www.pololu.com/catalog/product/950

Motor Driver http://www.pololu.com/catalog/product/110 (discontinued)

Bare Bones Board Arduino clone http://shop.moderndevice.com/products/bbb-kit

New work Box - makes a nice chassis. I cut holes in this and hot-melt glued the motors in it. http://www.homedepot.com/h_d1/N-5yc1v/R-100404058/h_d2/ProductDisplay?langId=-1&storeId=10051&catalogId=10053

digital line sensor http://www.pololu.com/catalog/product/959

distance sensor http://www.pololu.com/catalog/product/1136

battery Holder for Arduino power https://www.jameco.com/webapp/wcs/stores/servlet/Product_10001_10001_2111329_-1

battery holder for motor power (9v) https://www.jameco.com/webapp/wcs/stores/servlet/Product_10001_10001_109154_-1

Misc switches and other stuff


Code

This is the code I used. It worked OK, but not great. I need to tweak it more.


/*************************************************************
Sumo Competition program by Brian Wagner
10/24/10
 * 
 *************************************************************/

#include <PololuQTRSensors.h>
#include <PololuSerial.h>
#include <NewSoftSerial.h>

#define LineSensorLeftPin 3
#define LineSensorRightPin 2
#define RangeSensorPin 0

int LineLeft = 0;
int LineRight = 0;
int Range = 0;

int LineThreshold = 800;
int RangeThreshold = 100;

int SpinDir = 0;
int spiral = 20;

#define rxPin 0
#define txPin 4
#define rstPin 5

NewSoftSerial mySerial =  NewSoftSerial(rxPin, txPin);
PololuSerial motor = PololuSerial(&mySerial,rstPin);
byte fwVersion = -1;

unsigned int sensors[2];
PololuQTRSensorsRC qtr((unsigned char[]) {LineSensorLeftPin,LineSensorRightPin}, 2, 2000, 255); //declares two line sensors on pins 2,3

void setup()  
{
  mySerial.begin(9600);
  motor.begin();
  motor.stopBothMotors();
  delay(5000);
}

void loop() 
{
  //check line sensors
  qtr.read(sensors);
 
 if (sensors[0] < LineThreshold && sensors[1] < LineThreshold) {
    motor.stopBothMotors();
    motor.motor0Reverse(127);
    motor.motor1Reverse(127);
    delay(500);
    motor.motor1Forward(127);
    delay(300);
    motor.stopBothMotors();
  } 
  else if (sensors[0] < LineThreshold && sensors[1] > LineThreshold) {
    motor.stopMotor0();
    motor.motor1Forward(127);
    delay(40);
    motor.motor0Forward(127);
    SpinDir = 1;
    spiral = 20;
  }
 else if (sensors[0] > LineThreshold && sensors[1] < LineThreshold) {
   motor.stopMotor1();
    motor.motor0Forward(127);
    delay(40);
    motor.motor1Forward(127);
    SpinDir = 0;
    spiral = 20;
 }
 else {
        
    Range = analogRead(RangeSensorPin);
    if ( Range > RangeThreshold ) {
        //Charge!!!
        motor.motor0Forward(127);
        motor.motor1Forward(127);
        delay(100);
    }
    else {
    //let's look around
      if (SpinDir == 1 ) {
        motor.motor0Forward(80);
        motor.motor1Forward(80);
      } else {
        motor.motor1Forward(80);
        motor.motor0Forward(80);    
      }
        delay(20); 
        //spiral = spiral +20;    
       // if (spiral > 100) { spiral = 20; }
        //motor.stopBothMotors();
    }
    
 }

}