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...")
 
Line 34: Line 34:
  
 
<pre>
 
<pre>
//#include <AFMotor.h>
+
/*************************************************************
#include <PololuQTRSensors.h> //we're using the pololuQTR sensor library so we must attach it.
+
Motor test by Brian Wagner
 +
10/21/10
 +
*
 +
  *************************************************************/
  
PololuQTRSensorsRC qtr((unsigned char[]) {18,19}, 2, 2000, 255); //declares two line sensors on pins 18 and 19
+
#include <PololuSerial.h>
                                                                  //this corresponds to analog pins 4 and 5
+
#include <NewSoftSerial.h>
unsigned int sensors[2];
 
  
  const int motor1Pin = 3;    // right motor (directional pin 1)
+
#define LineSensorLeftPin 3
  const int motor2Pin = 2;    // right motor (directional pin 2)
+
#define LineSensorRightPin 2
  const int motor1Pin2 = 4;    //left motor (directional pin 1)
+
#define RangeSensorPin 0
  const int motor2Pin2 = 5;    //left motor (directional pin 2)
 
  const int enablePin1 = 9;    //right speed control
 
  const int enablePin2 = 10;  //left speed control
 
 
 
  const int linethreshold = 300;
 
  const int rangethreshold = 30;
 
  
int rangepin = 0; //distance sensor 1
+
int LineLeft = 0;
 +
int LineRight = 0;
 +
int Range = 0;
  
void setup() {
+
int LineThreshold = 7;
  Serial.begin(9600);          // set up Serial library at 9600 bps for debugging
+
int RangeThreshold = 100;
 
 
    // 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:
+
#define rxPin 0
    analogWrite(enablePin1, 255); //255 is full speed, 0 is stopped
+
#define txPin 4
    analogWrite(enablePin2, 255); //255 is full speed, 0 is stopped
+
#define rstPin 5
 +
 
 +
NewSoftSerial mySerial =  NewSoftSerial(rxPin, txPin);
 +
PololuSerial motor = PololuSerial(&mySerial,rstPin);
 +
byte fwVersion = -1;
 +
 
 +
void setup() 
 +
{
 +
  mySerial.begin(9600);
 +
  motor.begin();
 +
  motor.stopBothMotors();
 
}
 
}
  
void loop() {
+
void loop()  
//THE MAIN PROGRAM LOOP
+
{
   qtr.read(sensors);
+
   //check line sensors
  delay(20); //a possibly redundant delay
+
   //1 = white, 15 = black
 
+
  //left Sensor
//DEBUG STATEMENTS
+
   pinMode( LineSensorLeftPin, OUTPUT );
//if things aren't working right try uncommenting the lines below and checking the serial monitor
+
   digitalWrite( LineSensorLeftPin, HIGH );
//Serial.println(read_gp2d12_range(rangepin));
+
   delayMicroseconds( 50 );
Serial.print(sensors[0]);
+
   pinMode( LineSensorLeftPin, INPUT );
Serial.print("          ");
+
   long LineLeft = 0;
Serial.println(sensors[1]);
+
  while ( digitalRead( LineSensorLeftPin ) == HIGH ) {
+
     LineLeft++;
//********** MOTOR TEST CODDE ********************
+
     if ( LineLeft == 15 ) { break; }
//*******************************************
+
     delay( 20 );
//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  *********************
 
//*********************************************
 
 
//CHECK THE LINE SENSORS
 
if (sensors[0] < linethreshold && sensors[1] < linethreshold) {
 
  //UH-OH... BOTH SENSORS DETECT THE BORDER, LET'S BACKUP AND SPIN
 
    digitalWrite(13, HIGH); //turn on light to indicate it senses the border
 
    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(500); //do this for half of a second
 
   
 
    digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low  **right motor stopped
 
    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(500); //do this for half of a second
 
 
   }
 
   }
   else if (sensors[0] < linethreshold && sensors[1] > linethreshold) {
+
 
    //RIGHT SENSOR DETECTS BORDER, TURN LEFT
+
   //right sensor
    digitalWrite(13, HIGH); //turn on light to indicate it senses the border
+
  pinMode( LineSensorRightPin, OUTPUT );
    digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low  **right motor forward
+
  digitalWrite( LineSensorRightPin, HIGH );
    digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
+
   delayMicroseconds( 50 );
    digitalWrite(motor1Pin2, LOW);  // set leg 1 of the H-bridge low   **left motor stopped
+
  pinMode( LineSensorRightPin, INPUT );
     digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge high
+
   long LineRight = 0;
     delay(10); //slight delay to keep the program running smoothly
+
   while ( digitalRead( LineSensorRightPin ) == HIGH ) {
 +
     LineRight++;
 +
    if ( LineRight == 15 ) { break; }
 +
     delay( 20 );
 
   }
 
   }
  else if (sensors[0] > linethreshold && sensors[1] < linethreshold) {
+
     //LEFT SENSOR DETECTS BORDER, TURN RIGHT
+
if (LineLeft < LineThreshold and LineRight < LineThreshold) {
     digitalWrite(13, HIGH); //turn on light to indicate it senses the border
+
     motor.stopBothMotors();
     digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low  **right motor stopped
+
    motor.motor0Reverse(127);
     digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge high
+
     motor.motor1Reverse(127);
     digitalWrite(motor1Pin2, LOW);  // set leg 1 of the H-bridge low  **left motor forward
+
     delay(500);
     digitalWrite(motor2Pin2, HIGH); // set leg 2 of the H-bridge high
+
    motor.motor1Forward(127);
     delay(10); //slight delay to keep the program running smoothly
+
     delay(300);
 +
     motor.stopBothMotors();
 +
  }
 +
   else if (LineLeft < LineThreshold) {
 +
    motor.stopBothMotors();
 +
     motor.motor1Forward(127);
 +
     delay(10);
 
   }
 
   }
  else {
+
else if (LineRight < LineThreshold) {
    //WITHIN BORDERS
+
  motor.stopBothMotors();
    if (read_gp2d12_range(rangepin) < rangethreshold) {
+
    motor.motor0Forward(127);
    //IT SEES AN OPPONENT --> CHARGE!
+
    delay(10);
    digitalWrite(13, HIGH); //turn on a light to indicate it sees an opponent
+
}
    analogWrite(enablePin1, 255);
+
else {
    analogWrite(enablePin2, 255);
+
       
    digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low **right motor forward
+
    Range = analogRead(RangeSensorPin);
    digitalWrite(motor2Pin, HIGH)// set leg 2 of the H-bridge high
+
    if ( Range < RangeThreshold ) {
    digitalWrite(motor1Pin2, LOW);   // set leg 1 of the H-bridge low **left motor forward
+
        //Charge!!!
    digitalWrite(motor2Pin2, HIGH); // set leg 2 of the H-bridge high
+
        motor.motor0Forward(127);
    delay(100); //a longer delay for the charge
+
        motor.motor1Forward(127);
 +
        delay(1000);
 
     }
 
     }
 
     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
+
        motor.motor0Forward(64);
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low  **right motor forward
+
        motor.motor1Forward(0);  
      digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
+
        delay(10);    
      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(10); //slight delay to keep the program running smoothly
 
 
     }
 
     }
  }
+
   
 
+
}
 
+
 
 
}
 
}
 
 
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>

Revision as of 10:13, 16 February 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.

/*************************************************************
Motor test by Brian Wagner
10/21/10
 * 
 *************************************************************/

#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 = 7;
int RangeThreshold = 100;

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

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

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

void loop() 
{
  //check line sensors
  //1 = white, 15 = black
  //left Sensor
  pinMode( LineSensorLeftPin, OUTPUT );
  digitalWrite( LineSensorLeftPin, HIGH );
  delayMicroseconds( 50 );
  pinMode( LineSensorLeftPin, INPUT );
  long LineLeft	= 0;
  while ( digitalRead( LineSensorLeftPin ) == HIGH ) {
    LineLeft++;
    if ( LineLeft == 15 ) { break; }
    delay( 20 );
  }

  //right sensor
  pinMode( LineSensorRightPin, OUTPUT );
  digitalWrite( LineSensorRightPin, HIGH );
  delayMicroseconds( 50 );
  pinMode( LineSensorRightPin, INPUT );
  long LineRight = 0;
  while ( digitalRead( LineSensorRightPin ) == HIGH ) {
    LineRight++;
    if ( LineRight == 15 ) { break; }
    delay( 20 );
  }
 
 if (LineLeft < LineThreshold and LineRight < LineThreshold) {
    motor.stopBothMotors();
    motor.motor0Reverse(127);
    motor.motor1Reverse(127);
    delay(500);
    motor.motor1Forward(127);
    delay(300);
    motor.stopBothMotors();
  } 
  else if (LineLeft < LineThreshold) {
    motor.stopBothMotors();
    motor.motor1Forward(127);
    delay(10);
  }
 else if (LineRight < LineThreshold) {
   motor.stopBothMotors();
    motor.motor0Forward(127);
    delay(10);
 }
 else {
        
    Range = analogRead(RangeSensorPin);
    if ( Range < RangeThreshold ) {
        //Charge!!!
        motor.motor0Forward(127);
        motor.motor1Forward(127);
        delay(1000);
    }
    else {
    //let's look around
        motor.motor0Forward(64);
        motor.motor1Forward(0);    
        delay(10);     
    }
    
 }

}