Difference between revisions of "Sumobotsclass"

From LVL1
Jump to navigation Jump to search
Line 1: Line 1:
 
== Motor control code ==
 
== Motor control code ==
 
+
These are the imports, variable decelerations and pin assignments.
 
  <nowiki>#include <PololuQTRSensors.h>          //we're using the pololuQTR sensor library so we must attach it.</nowiki>
 
  <nowiki>#include <PololuQTRSensors.h>          //we're using the pololuQTR sensor library so we must attach it.</nowiki>
 
  <nowiki>#define motor1dir 8                    //direction motor 2</nowiki>
 
  <nowiki>#define motor1dir 8                    //direction motor 2</nowiki>
Line 11: Line 11:
 
  <nowiki>const int linethreshold = 300; </nowiki>
 
  <nowiki>const int linethreshold = 300; </nowiki>
  
void setup() {  // put your setup code here, to run once:
+
void setup() {  // put your setup code here, to run once:
 
  //motor control outputs
 
  //motor control outputs
 
   pinMode(motor1dir, OUTPUT);
 
   pinMode(motor1dir, OUTPUT);
Line 17: Line 17:
 
   pinMode(motor2dir, OUTPUT);
 
   pinMode(motor2dir, OUTPUT);
 
   pinMode(motor2speed, OUTPUT);
 
   pinMode(motor2speed, OUTPUT);
 +
 
  // AV outputs
 
  // AV outputs
 
   pinMode(ledPin, OUTPUT);
 
   pinMode(ledPin, OUTPUT);
 
   pinMode(buzzerPin, OUTPUT);
 
   pinMode(buzzerPin, OUTPUT);
 
   pinMode(irSensorPin, INPUT);
 
   pinMode(irSensorPin, INPUT);
  //Serial.begin(9600); // set up Serial library at 9600 bps for debugging
+
 
  delay (3000); //wait for everything and for the match to start   
+
  Serial.begin(9600); // set up Serial library at 9600 bps for debugging
  blink(ledPin, 3, 100);
+
delay (3000); //wait for everything and for the match to start   
  // blink the LED 3 times. This should happen only once.  
+
blink(ledPin, 3, 100);
  // if you see the LED blink three times, it means that  
+
  // blink the LED 3 times. This should happen only once.  
  // the module reset itself,. probably because the motor  
+
  // if you see the LED blink three times, it means that  
  // caused a brownout or a short.  
+
  // the module reset itself,. probably because the motor  
} //end setup
+
  // caused a brownout or a short.  
 +
} //end setup
  
  
 
void loop() {  // put your main code here, to run repeatedly:  
 
void loop() {  // put your main code here, to run repeatedly:  
qtr.read(sensors); //reads the line sensors
+
 
 
  //set speed max
 
  //set speed max
 
   analogWrite(motor2speed,255);   
 
   analogWrite(motor2speed,255);   
Line 57: Line 59:
 
   delay(3000);
 
   delay(3000);
  
 +
qtr.read(sensors); //reads the line sensors
 
  //DEBUG Don't forget to enable the serial port in setup
 
  //DEBUG Don't forget to enable the serial port in setup
 
   Serial.print("d= ");
 
   Serial.print("d= ");
Line 99: Line 102:
 
   delay(100);
 
   delay(100);
 
  }//end search
 
  }//end search
 
+
}//end the main loop
}
 
  
 
These are functions that make life easy, someone else did the math now we don't have to.
 
These are functions that make life easy, someone else did the math now we don't have to.
  
 
This function blinks an LED  
 
This function blinks an LED  
void blink(int whatPin, int howManyTimes, int milliSecs) {
+
void blink(int whatPin, int howManyTimes, int milliSecs) {
 
   int i = 0;
 
   int i = 0;
 
   <nowiki>for ( i = 0; i < howManyTimes; i++) {</nowiki>
 
   <nowiki>for ( i = 0; i < howManyTimes; i++) {</nowiki>
Line 113: Line 115:
 
   delay(milliSecs/2);
 
   delay(milliSecs/2);
 
   }
 
   }
}// end blink
+
}// end blink
  
 
This is the function that allows us to use the sharp ir sensor for detecting distance
 
This is the function that allows us to use the sharp ir sensor for detecting distance
float irdistance(int pin){
+
float irdistance(int pin){
  //http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/
+
//http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/
  float volts, distance;
+
float volts, distance;
  <nowiki>for (int i=0; i< 5; i++){ </nowiki>
+
<nowiki>for (int i=0; i< 5; i++){ </nowiki>
  volts = analogRead(pin)*0.0048828125;// (5/1024)
+
  volts = analogRead(pin)*0.0048828125;// (5/1024)
  distance += 65* pow(volts, -1.10); //65 = theretical distance / (1/Volts)S
+
  distance += 65* pow(volts, -1.10); //65 = theretical distance / (1/Volts)S
  }
+
}
  return (distance);
+
return (distance);
}//end irdistance
+
}//end irdistance
  
 
This function makes a buzzer make noise or make an ir led pulse at a certain frequency
 
This function makes a buzzer make noise or make an ir led pulse at a certain frequency
void freqout(int freq, int t, int freqoutpin){ // freq in hz, t in ms, pin to use
+
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
+
int hperiod; //calculate 1/2 period in us
  long cycles, i;
+
long cycles, i;
  hperiod = (500000 / freq) - 7; // subtract 7 us to make up for digitalWrite overhead
+
hperiod = (500000 / freq) - 7; // subtract 7 us to make up for digitalWrite overhead
  cycles = ((long)freq * (long)t) / 1000; // calculate cycles
+
cycles = ((long)freq * (long)t) / 1000; // calculate cycles
  <nowiki>for (i=0; i<= cycles; i++){ </nowiki>// play note for t ms  
+
<nowiki>for (i=0; i<= cycles; i++){ </nowiki>// play note for t ms  
  digitalWrite(freqoutpin, HIGH);  
+
  digitalWrite(freqoutpin, HIGH);  
  delayMicroseconds(hperiod);
+
  delayMicroseconds(hperiod);
  digitalWrite(freqoutpin, LOW);  
+
  digitalWrite(freqoutpin, LOW);  
  delayMicroseconds(hperiod);
+
  delayMicroseconds(hperiod);
  }//end loop
+
}//end loop
}// end freqout
+
}// end freqout
  
 
----
 
----

Revision as of 11:57, 23 October 2011

Motor control code

These are the imports, variable decelerations and pin assignments.

#include <PololuQTRSensors.h>           //we're using the pololuQTR sensor library so we must attach it.
#define motor1dir 8                     //direction motor 2
#define motor1speed 9   		//pwm control motor 1
#define motor2dir 11                    //direction motor 2
#define motor2speed 10  		//pwm control motor 2
#define ledpin 13                       //led pin 
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 linethreshold = 300; 

void setup() { // put your setup code here, to run once:

//motor control outputs
 pinMode(motor1dir, OUTPUT);
 pinMode(motor1speed, OUTPUT);
 pinMode(motor2dir, OUTPUT);
 pinMode(motor2speed, OUTPUT);
// AV outputs
 pinMode(ledPin, OUTPUT);
 pinMode(buzzerPin, OUTPUT);
 pinMode(irSensorPin, INPUT);
Serial.begin(9600); // set up Serial library at 9600 bps for debugging
delay (3000); //wait for everything and for the match to start  
blink(ledPin, 3, 100);
 // 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. 

} //end setup


void loop() { // put your main code here, to run repeatedly:

//set speed max
 analogWrite(motor2speed,255);     			
 analogWrite(motor1speed,255);
//go forward
 digitalWrite(motor1dir, HIGH);    			
 digitalWrite(motor2dir, HIGH);
 delay(3000);
//go backwards
 digitalWrite(motor1dir, LOW);     			
 digitalWrite(motor2dir, LOW);
 delay(3000);
//go right 
 digitalWrite(motor1dir, HIGH);     			 
 digitalWrite(motor2dir, LOW);
 delay(3000);
//go left
 digitalWrite(motor1dir, LOW);     			
 digitalWrite(motor2dir, HIGH);
 delay(3000);
//stop 
 analogWrite(motor1speed,0);       			
 analogWrite(motor2speed,0);
 delay(3000);
qtr.read(sensors); //reads the line sensors
//DEBUG Don't forget to enable the serial port in setup
 Serial.print("d= ");
 Serial.print(analogRead(irSensorPin));
 Serial.print(" r= ");
 Serial.print(sensors[0]);   //right
 Serial.print(" l= ");
 Serial.println(sensors[1]);   //left 

//do the robot dance

if (sensors[0] < linethreshold && sensors[1] < linethreshold) 
{ 
 // Serial.println("the edge backup and spin");
 //backup
 delay(500); //do this for half of a second
 //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");
 //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");
 //rotate right
 delay(500); //do this for half of a second
} //end found edge Left
//WITHIN BORDERS
else if (irdistance(irSensorPin) < 450/*max distance */ )
{ //charge
 // Serial.println("CHARGE");
 //go forward 
 delay(100); //a longer delay for the charge
}//end charge
else 
{  //search
 Serial.println("scanning, where are you?");
 //rotate right 
 delay(100);
}//end search

}//end the main loop

These are functions that make life easy, someone else did the math now we don't have to.

This function 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 the function that allows us to use the sharp ir sensor for detecting distance 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;// (5/1024)
 distance += 65* pow(volts, -1.10); //65 = theretical distance / (1/Volts)S
}
return (distance);

}//end irdistance

This function makes a buzzer make noise or make an ir led pulse at a certain frequency 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 loop

}// end freqout



QTR line sensors




Need a Mechapad or some Protoboard and Patience

what type where amount per bot
motor driver h-bridge http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=296-9911-5-ND 1
transistor NPN Transistor http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=2N3904GOS-ND 2
Capacitor Ceramic 0.1uF http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=399-4266-ND 2
diode Diode Rectifier - 1A 50V http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=641-1311-1-ND 8
Crystal 16MHz Crystal 16MHz http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=887-1019-ND 1
Microcontroller ATMega328 http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=ATMEGA328-PU-ND 1
Resistor 10k Ohm http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=CF14JT10K0CT-ND 5
Resistor 1k ohm http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=CF14JT1K00CT-ND 3
Voltage regulator 7805 http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=LM7805CT-ND 1
Capacitor Electrolytic 10uF/25V http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=P975-ND 1
Power switch switch http://search.digikey.com/scripts/dksearch/dksus.dll?pname&WT.z_cid=&name=679-1854-ND 1
reset button button http://search.digikey.com/scripts/dksearch/dksus.dll?pname&WT.z_cid=&name=SW400-ND 1
Capacitor Electrolytic 100uF/25V http://search.digikey.com/scripts/dksearch/dksus.dll?vendor=0&keywords=P10269-ND 1
Motors/wheels GM8 http://www.solarbotics.com/products/gmpw_deal/ 2
connectors female headers > 20 http://www.sparkfun.com/products/115 1
connectors male headers > 20 http://www.sparkfun.com/products/116 1
sensors Infrared Proximity Sensor - Sharp GP2Y0A21YK http://www.sparkfun.com/products/242 1
Capacitor Ceramic 22pF http://www.sparkfun.com/products/8571 2
batteryholder 9vsnap http://www.sparkfun.com/products/91 1
sensors QRE1113 Line Sensor Analog http://www.sparkfun.com/products/9453 2
LED Basic LED - 5mm laying around 1

Mechapad.png


eagle design files