Home Depot Special

From LVL1
Revision as of 09:52, 16 February 2011 by Bpwagner (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


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.

//#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)
  const int motor1Pin2 = 4;    //left motor (directional pin 1)
  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

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging
  
    // 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: 
    analogWrite(enablePin1, 255); //255 is full speed, 0 is stopped
    analogWrite(enablePin2, 255); //255 is full speed, 0 is stopped
}

void loop() {
//THE MAIN PROGRAM LOOP
  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  *********************
 //*********************************************
 
 //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
    digitalWrite(13, HIGH);  //turn on light to indicate it senses the border
    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 stopped
    digitalWrite(motor2Pin2, LOW);  // set leg 2 of the H-bridge high
    delay(10); //slight delay to keep the program running smoothly
  }
  else if (sensors[0] > linethreshold && sensors[1] < linethreshold) {
    //LEFT SENSOR DETECTS BORDER, TURN RIGHT
    digitalWrite(13, HIGH);  //turn on light to indicate it senses the border
    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, LOW);   // set leg 1 of the H-bridge low  **left motor forward
    digitalWrite(motor2Pin2, HIGH);  // set leg 2 of the H-bridge high
    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 {
      //LOOKING FOR OPPONENT BY SPINNING IN PLACE
      digitalWrite(13, LOW);  //turn off the light to indicate it doesn't see an opponent
      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, 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;
}