Difference between revisions of "Barney Fife"
Line 1: | Line 1: | ||
2x Solarbotics GM8 Motor with wheels | 2x Solarbotics GM8 Motor with wheels | ||
− | [http://www.solarbotics.com/products/gmpw_deal/] | + | |
+ | [http://www.solarbotics.com/products/gmpw_deal/ http://www.solarbotics.com/products/gmpw_deal/] | ||
+ | |||
1x SN754410 Quad Half H-Bridge | 1x SN754410 Quad Half H-Bridge | ||
− | [http://www.sparkfun.com/commerce/product_info.php?products_id=315] | + | |
+ | [http://www.sparkfun.com/commerce/product_info.php?products_id=315 http://www.sparkfun.com/commerce/product_info.php?products_id=315] | ||
+ | |||
+ | |||
2x QRE1113 Line Sensor Breakout - Analog | 2x QRE1113 Line Sensor Breakout - Analog | ||
− | [http://www.sparkfun.com/commerce/product_info.php?products_id=9453] | + | |
+ | [http://www.sparkfun.com/commerce/product_info.php?products_id=9453 http://www.sparkfun.com/commerce/product_info.php?products_id=9453] | ||
+ | |||
+ | |||
1x Infrared Proximity Sensor Short Range - Sharp GP2D120XJ00F | 1x Infrared Proximity Sensor Short Range - Sharp GP2D120XJ00F | ||
− | [http://www.sparkfun.com/commerce/product_info.php?products_id=8959] | + | |
+ | [http://www.sparkfun.com/commerce/product_info.php?products_id=8959 http://www.sparkfun.com/commerce/product_info.php?products_id=8959] | ||
+ | |||
+ | |||
1x cheap arduino clone (Diavolino) | 1x cheap arduino clone (Diavolino) | ||
− | [http://evilmadscience.com/tinykitlist/180] | + | |
+ | |||
+ | [http://evilmadscience.com/tinykitlist/180 http://evilmadscience.com/tinykitlist/180] | ||
+ | |||
+ | |||
1x small sheet of 1/8" lexan | 1x small sheet of 1/8" lexan | ||
+ | |||
1x 9v battery holder | 1x 9v battery holder | ||
+ | |||
1x 4xAA battery holder | 1x 4xAA battery holder | ||
+ | |||
20x Header pins (3,5,2) | 20x Header pins (3,5,2) | ||
+ | |||
hot glue | hot glue | ||
+ | |||
Solder | Solder | ||
+ | |||
assorted bits of wire | assorted bits of wire | ||
+ | |||
+ | Sources | ||
+ | |||
+ | [http://groups.google.com/group/tuftsroboticsclub/web/anatomy-of-a-sumobot http://groups.google.com/group/tuftsroboticsclub/web/anatomy-of-a-sumobot] | ||
+ | |||
+ | [http://groups.google.com/group/tuftsroboticsclub/web/how-to-program-your-sumobot http://groups.google.com/group/tuftsroboticsclub/web/how-to-program-your-sumobot] | ||
− | + | My code as it ran at the competition | |
− | [ | + | |
− | [http:// | + | |
+ | <nowiki>#include <PololuQTRSensors.h> </nowiki>//we're using the pololuQTR sensor library so we must attach it. | ||
+ | |||
+ | |||
+ | <nowiki>PololuQTRSensorsRC qtr((unsigned char[]) {19,18}, 2, 2000, 255); //declares two line sensors on pins 18 and 19 this corresponds to analog pins 4 and 5</nowiki> | ||
+ | |||
+ | <nowiki>unsigned int sensors[2];</nowiki> | ||
+ | |||
+ | |||
+ | const int motor1APin = 10; // H-bridge leg 1 (pin 2) bundle black wire | ||
+ | |||
+ | const int motor2APin = 9; // H-bridge leg 2 (pin 7) bundle white wire | ||
+ | |||
+ | const int motor4APin = 11; // H-bridge leg 3 (pin 10) bundle red wire | ||
+ | |||
+ | const int motor3APin = 12; // H-bridge leg 4 (pin 15) bundle green wire | ||
+ | |||
+ | const int enmotorPin = 8; // H-bridge enable pin speed control (1 and 9) green wire | ||
+ | |||
+ | const int buzzerPin = 2; //buzzer pin | ||
+ | |||
+ | const int ledPin = 13; // LED (onboard) | ||
+ | |||
+ | const int irSensorPin = 3; // IR sensor on this pin distance sensor | ||
+ | |||
+ | |||
+ | const int linethreshold = 300; | ||
+ | |||
+ | |||
+ | /* This is notes on what my motors will do | ||
+ | |||
+ | PORTB = B00010011;//Forward | ||
+ | |||
+ | |||
+ | PORTB = B00001101; //backwards | ||
+ | |||
+ | |||
+ | PORTB = B00001011;//rotate left | ||
+ | |||
+ | |||
+ | PORTB = B00010101;//rotate right | ||
+ | |||
+ | |||
+ | PORTB = B00000001; //all stop | ||
+ | |||
+ | <nowiki>*/</nowiki> | ||
+ | |||
+ | |||
+ | void setup() { | ||
+ | |||
+ | |||
+ | //Serial.begin(9600); // set up Serial library at 9600 bps for debugging | ||
+ | |||
+ | // set all outputs: | ||
+ | |||
+ | // motors | ||
+ | |||
+ | pinMode(motor1APin, OUTPUT); | ||
+ | |||
+ | pinMode(motor2APin, OUTPUT); | ||
+ | |||
+ | pinMode(motor3APin, OUTPUT); | ||
+ | |||
+ | pinMode(motor4APin, OUTPUT); | ||
+ | |||
+ | pinMode(enmotorPin, OUTPUT); | ||
+ | |||
+ | // AV outputs | ||
+ | |||
+ | pinMode(ledPin, OUTPUT); | ||
+ | |||
+ | pinMode(buzzerPin, OUTPUT); | ||
+ | |||
+ | pinMode(irSensorPin, INPUT); | ||
+ | |||
+ | delay (3000); | ||
+ | |||
+ | |||
+ | /* blink the LED 3 times. This should happen only once. | ||
+ | |||
+ | if you see the LED blink three times, it means that | ||
+ | |||
+ | the module reset itself,. probably because the motor | ||
+ | |||
+ | caused a brownout or a short. */ | ||
+ | |||
+ | blink(ledPin, 3, 100); | ||
+ | |||
+ | |||
+ | } //end setup | ||
+ | |||
+ | |||
+ | void loop() { | ||
+ | |||
+ | qtr.read(sensors); | ||
+ | |||
+ | |||
+ | //Don't forget to enable the serial port in setup | ||
+ | |||
+ | /* | ||
+ | |||
+ | Serial.print(analogRead(irSensorPin)); | ||
+ | |||
+ | Serial.print(" r= "); | ||
+ | |||
+ | <nowiki>Serial.print(sensors[0]);//right</nowiki> | ||
+ | |||
+ | Serial.print(" l= "); | ||
+ | |||
+ | <nowiki>Serial.println(sensors[1]);//left </nowiki> | ||
+ | |||
+ | // <nowiki>*/</nowiki> | ||
+ | |||
+ | |||
+ | <nowiki>if (sensors[0] < linethreshold && sensors[1] < linethreshold) </nowiki> | ||
+ | |||
+ | { | ||
+ | |||
+ | // Serial.println("the edge backup and spin"); | ||
+ | |||
+ | PORTB = B00001101; //backup | ||
+ | |||
+ | delay(500); //do this for half of a second | ||
+ | |||
+ | PORTB = B00001011;//rotate left | ||
+ | |||
+ | delay(500); //do this for half of a second | ||
+ | |||
+ | } //end found edge front | ||
+ | |||
+ | <nowiki>else if (sensors[1] < linethreshold) </nowiki> | ||
+ | |||
+ | { | ||
+ | |||
+ | // Serial.println("right sensor, turn left"); | ||
+ | |||
+ | PORTB = B00001011;//rotate left | ||
+ | |||
+ | delay(1000); //do this for half of a second | ||
+ | |||
+ | } //end found edge right | ||
+ | |||
+ | <nowiki>else if (sensors[0] < linethreshold) </nowiki> | ||
+ | |||
+ | { | ||
+ | |||
+ | // Serial.println("left sensor, turn right"); | ||
+ | |||
+ | PORTB = B00010101;//rotate right | ||
+ | |||
+ | delay(500); //do this for half of a second | ||
+ | |||
+ | } //end found edge Left | ||
+ | |||
+ | //WITHIN BORDERS | ||
+ | |||
+ | <nowiki>else if (irdistance(irSensorPin) < 450/*max distance */ )</nowiki> | ||
+ | |||
+ | { | ||
+ | |||
+ | // Serial.println("CHARGE"); | ||
+ | |||
+ | PORTB = B00010011; | ||
+ | |||
+ | freqout(900,20,buzzerPin); //Battlecry to indicate it sees an opponent | ||
+ | |||
+ | delay(100); //a longer delay for the charge | ||
+ | |||
+ | }//end charge | ||
+ | |||
+ | else | ||
+ | |||
+ | { | ||
+ | |||
+ | Serial.println("scanning, where are you?"); | ||
+ | |||
+ | PORTB = B00010101;//rotate right | ||
+ | |||
+ | delay(100); | ||
+ | |||
+ | }//end search | ||
+ | |||
+ | }//end Loop | ||
+ | |||
+ | |||
+ | //blinks an LED | ||
+ | |||
+ | void blink(int whatPin, int howManyTimes, int milliSecs) { | ||
+ | |||
+ | int i = 0; | ||
+ | |||
+ | <nowiki>for ( i = 0; i < howManyTimes; i++) {</nowiki> | ||
+ | |||
+ | digitalWrite(whatPin, HIGH); | ||
+ | |||
+ | delay(milliSecs/2); | ||
+ | |||
+ | digitalWrite(whatPin, LOW); | ||
+ | |||
+ | delay(milliSecs/2); | ||
+ | |||
+ | } | ||
+ | |||
+ | }// end blink | ||
+ | |||
+ | |||
+ | //this is for the piezo buzzer | ||
+ | |||
+ | void freqout(int freq, int t, int freqoutpin) // freq in hz, t in ms, pin to use | ||
+ | |||
+ | { | ||
+ | |||
+ | int hperiod; //calculate 1/2 period in us | ||
+ | |||
+ | long cycles, i; | ||
+ | |||
+ | hperiod = (500000 / freq) - 7; // subtract 7 us to make up for digitalWrite overhead | ||
+ | |||
+ | cycles = ((long)freq * (long)t) / 1000; // calculate cycles | ||
+ | |||
+ | <nowiki>for (i=0; i<= cycles; i++){ </nowiki>// play note for t ms | ||
+ | |||
+ | digitalWrite(freqoutpin, HIGH); | ||
+ | |||
+ | delayMicroseconds(hperiod); | ||
+ | |||
+ | digitalWrite(freqoutpin, LOW); | ||
+ | |||
+ | delayMicroseconds(hperiod); | ||
+ | |||
+ | }//end loopbu | ||
+ | |||
+ | }// end freqout | ||
+ | |||
+ | |||
+ | float irdistance(int pin){ | ||
+ | |||
+ | //http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/ | ||
+ | |||
+ | float volts, distance; | ||
+ | |||
+ | <nowiki>for (int i=0; i< 5; i++){ </nowiki> | ||
+ | |||
+ | volts = analogRead(pin)*0.0048828125; | ||
+ | |||
+ | distance += 65* pow(volts, -1.10); | ||
+ | |||
+ | } | ||
+ | |||
+ | distance = distance/10; | ||
+ | |||
+ | // float volts = analogRead(pin)*0.0048828125; // (5/1024) | ||
+ | |||
+ | // float distance = 65* pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk | ||
+ | |||
+ | return (distance); | ||
+ | |||
+ | }//end irdistance |
Revision as of 10:36, 30 October 2010
2x Solarbotics GM8 Motor with wheels
http://www.solarbotics.com/products/gmpw_deal/
1x SN754410 Quad Half H-Bridge
http://www.sparkfun.com/commerce/product_info.php?products_id=315
2x QRE1113 Line Sensor Breakout - Analog
http://www.sparkfun.com/commerce/product_info.php?products_id=9453
1x Infrared Proximity Sensor Short Range - Sharp GP2D120XJ00F
http://www.sparkfun.com/commerce/product_info.php?products_id=8959
1x cheap arduino clone (Diavolino)
http://evilmadscience.com/tinykitlist/180
1x small sheet of 1/8" lexan
1x 9v battery holder
1x 4xAA battery holder
20x Header pins (3,5,2)
hot glue
Solder
assorted bits of wire
Sources
http://groups.google.com/group/tuftsroboticsclub/web/anatomy-of-a-sumobot
http://groups.google.com/group/tuftsroboticsclub/web/how-to-program-your-sumobot
My code as it ran at the competition
#include <PololuQTRSensors.h> //we're using the pololuQTR sensor library so we must attach it.
PololuQTRSensorsRC qtr((unsigned char[]) {19,18}, 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 motor1APin = 10; // H-bridge leg 1 (pin 2) bundle black wire
const int motor2APin = 9; // H-bridge leg 2 (pin 7) bundle white wire
const int motor4APin = 11; // H-bridge leg 3 (pin 10) bundle red wire
const int motor3APin = 12; // H-bridge leg 4 (pin 15) bundle green wire
const int enmotorPin = 8; // H-bridge enable pin speed control (1 and 9) green wire
const int buzzerPin = 2; //buzzer pin
const int ledPin = 13; // LED (onboard)
const int irSensorPin = 3; // IR sensor on this pin distance sensor
const int linethreshold = 300;
/* This is notes on what my motors will do
PORTB = B00010011;//Forward
PORTB = B00001101; //backwards
PORTB = B00001011;//rotate left
PORTB = B00010101;//rotate right
PORTB = B00000001; //all stop
*/
void setup() {
//Serial.begin(9600); // set up Serial library at 9600 bps for debugging
// set all outputs:
// motors
pinMode(motor1APin, OUTPUT);
pinMode(motor2APin, OUTPUT);
pinMode(motor3APin, OUTPUT);
pinMode(motor4APin, OUTPUT);
pinMode(enmotorPin, OUTPUT);
// AV outputs
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(irSensorPin, INPUT);
delay (3000);
/* blink the LED 3 times. This should happen only once.
if you see the LED blink three times, it means that
the module reset itself,. probably because the motor
caused a brownout or a short. */
blink(ledPin, 3, 100);
} //end setup
void loop() {
qtr.read(sensors);
//Don't forget to enable the serial port in setup
/*
Serial.print(analogRead(irSensorPin));
Serial.print(" r= ");
Serial.print(sensors[0]);//right
Serial.print(" l= ");
Serial.println(sensors[1]);//left
// */
if (sensors[0] < linethreshold && sensors[1] < linethreshold)
{
// Serial.println("the edge backup and spin");
PORTB = B00001101; //backup
delay(500); //do this for half of a second
PORTB = B00001011;//rotate left
delay(500); //do this for half of a second
} //end found edge front
else if (sensors[1] < linethreshold)
{
// Serial.println("right sensor, turn left");
PORTB = B00001011;//rotate left
delay(1000); //do this for half of a second
} //end found edge right
else if (sensors[0] < linethreshold)
{
// Serial.println("left sensor, turn right");
PORTB = B00010101;//rotate right
delay(500); //do this for half of a second
} //end found edge Left
//WITHIN BORDERS
else if (irdistance(irSensorPin) < 450/*max distance */ )
{
// Serial.println("CHARGE");
PORTB = B00010011;
freqout(900,20,buzzerPin); //Battlecry to indicate it sees an opponent
delay(100); //a longer delay for the charge
}//end charge
else
{
Serial.println("scanning, where are you?");
PORTB = B00010101;//rotate right
delay(100);
}//end search
}//end Loop
//blinks an LED
void blink(int whatPin, int howManyTimes, int milliSecs) {
int i = 0;
for ( i = 0; i < howManyTimes; i++) {
digitalWrite(whatPin, HIGH);
delay(milliSecs/2);
digitalWrite(whatPin, LOW);
delay(milliSecs/2);
}
}// end blink
//this is for the piezo buzzer
void freqout(int freq, int t, int freqoutpin) // freq in hz, t in ms, pin to use
{
int hperiod; //calculate 1/2 period in us
long cycles, i;
hperiod = (500000 / freq) - 7; // subtract 7 us to make up for digitalWrite overhead
cycles = ((long)freq * (long)t) / 1000; // calculate cycles
for (i=0; i<= cycles; i++){ // play note for t ms
digitalWrite(freqoutpin, HIGH);
delayMicroseconds(hperiod);
digitalWrite(freqoutpin, LOW);
delayMicroseconds(hperiod);
}//end loopbu
}// end freqout
float irdistance(int pin){
//http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/
float volts, distance;
for (int i=0; i< 5; i++){
volts = analogRead(pin)*0.0048828125;
distance += 65* pow(volts, -1.10);
}
distance = distance/10;
// float volts = analogRead(pin)*0.0048828125; // (5/1024)
// float distance = 65* pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
return (distance);
}//end irdistance