Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
#define TRIGGER1 24
#define TRIGGER2 36
mega
# define TRIGGER3 30
# define ECHO1 25
#define ECHO2 37
#define ECHO3 31
// Defines Trigger Pin for sonar sensor left to pin 24 on arduino mega
// Defines Trigger Pin for sonar sensor center to pin 36 on arduino
// Defines Trigger Pin for sonar sensor right to pin 30 on arduino mega
// Defines Echo Pin for sonar sensor left to pin 25 on arduino mega
// Define Echo Pin for sonar sensor center to pin 37 on arduino mega
// Define Echo Pin for sonar sensor right to pin 31 on arduino mega
//Defines each object and its corresponding pins by the new ping library, each pin called is
defined above
NewPing sonarL(TRIGGER1,ECHO1, MaxSonar);
NewPing sonarC(TRIGGER2,ECHO2, MaxSonar);
NewPing sonarR(TRIGGER3,ECHO3, MaxSonar);
//
// values to control EasyDriver ////////////////////////////////////////////////////////
//These pins are used to set step sizes for the motors from full-step to quarter-step to eighthstep
#define MS1R 46 // Defines MS1 Pin for sonar sensor right to pin 46 on arduino mega
#define MS2R 47 // Defines MS2 Pin for sonar sensor right to pin 47 on arduino mega
#define MS1L 48 // Defines MS1 Pin for sonar sensor left to pin 48 on arduino mega
#define MS2L 49 // Defines MS2 Pin for sonar sensor left to pin 49 on arduino mega
#define MS1C 44 // Defines MS1 Pin for sonar sensor center to pin 44 on arduino mega
#define MS2C 45 // Defines MS2 Pin for sonar sensor center to pin 45 on arduino mega
// setup - things that happen only once at start
void setup()
{
Serial.begin(115200);//Sets all serial communication at 115200 baud, fastest speed arduino
can function at
//Sets the enable pin defined above that we can change to HIGH or LOW, HIGH disables the
wheel (coast) LOW enables the wheel (drive)
pinMode(MotorEnableR, OUTPUT); //Right Wheel
pinMode(MotorEnableL, OUTPUT); //Left Wheel
pinMode(MotorEnableC, OUTPUT);//Center Wheel
//Sets the MS pins that are defined above to output pins, enabling us to send HIGH or LOW
//MS1 & MS2 LOW = Full-Step
//MS1 & MS2 HIGH = Eighth-Step
//MS1 HIGH & MS2 LOW = Half-Step
//MS1 LOW & MS2 HIGH = Quarter-Step
pinMode(MS1R, OUTPUT); //MS1 Right
pinMode(MS2R, OUTPUT); //MS2 Right
pinMode(MS1L, OUTPUT); //MS1 Left
//
// pingAve(int num, int sen) - average num pings from sensor
//
int pingAve(int num, int sen) {
int dist=0; //A holding variable for ultrasonic sensor reading inputs
int distTot=0; //Stores the total amount of distance read by the ultrasonic sensor readings
int count=0; //Counts the number of times the ultrasonic reads
int distAve; // Stores the final output that is the average of the ultrasonic readings
//
for (int x=0; x < num; x++) {//num is the integer defined above, the for loop will loop num times
if(sen == 1){ //if left sensor is called
dist = sonarL.ping()/US_ROUNDTRIP_CM; // variable dist = the ultrasonic reading
converted into cm
}
else if(sen == 2){//if right sensor is called
dist = sonarC.ping()/US_ROUNDTRIP_CM; // variable dist = the ultrasonic reading
converted into cm
}
else if(sen == 3){//if center sensor is called
dist = sonarR.ping()/US_ROUNDTRIP_CM; // variable dist = the ultrasonic reading
converted into cm
}
if (dist > 0 && dist < MaxSonar) {// if distance read is less than 300
distTot+=dist; //variable distTot is incremented by dist
count += 1; // add one to the value of count
}
else if (dist <= 0) { //if beyond 300 value returned is -1, so this sets it to 300
distTot+=MaxSonar; //variable distTot is incremented by Maxsonar
count += 1; // add one to the value of count
}
delayMicroseconds(5000); //5 milliseconds of time between pings or they jam each other,
transmits before receives the signal
}
distAve = distTot/count; //distAve is distTot which has been added to divided by count
return(distAve);
//returning the average distance
}
//
//main function///
//
void go(int dist){ //function redirects bluetooth command to proper actions
Serial.println("v - display commands"); // prints on device context of quotations
//resets wheels to quarter-step mode
digitalWrite(MS1L, LOW);
digitalWrite(MS2L, HIGH);
digitalWrite(MS1R, LOW);
digitalWrite(MS2R, HIGH);
while(1){//infinite loop
//resets wheels to quarter-step mode
digitalWrite(MS1L, LOW);
digitalWrite(MS2L, HIGH);
digitalWrite(MS1R, LOW);
digitalWrite(MS2R, HIGH);
int z = Serial.read(); // starts reading integer z for case statements
switch(z){// declares z as switch that enables us to send bluetooth commands
case 'n': //if case n is pressed...
//
// follow lanes a specified distance and direction
//
void lanefollow(int dist, int dir){
int steptime = 1300; // 1400 is slow but reliable
int step = 0; // set int step to 0, this will be altered later
int skip = 1; //set int skip to 1, the variable will determine the frequency a wheel is skipped
int turn = 0; // set int turn to 0, the variable will determines the direction of the turn
int count = 0; //keep track of steps
int sensedist = 26; // determines how often we sense during lanefollow
int pingL, pingR, delta; // declares these variable integers that will be called later
int maxdelta = 10; // sets the maximum difference between left and right sensor before a big
turn
digitalWrite(dirL, dir); // HIGH(1) = forward
digitalWrite(dirR, dir); // HIGH(1) = forward
digitalWrite(MotorEnableL, LOW); //enable wheels
digitalWrite(MotorEnableR, LOW); //enable wheels
for(int x=0;x<dist;x++){//for loop that loops dist times as set above
count++; // add one to count
digitalWrite(MS1L, HIGH);
digitalWrite(MS2L, HIGH);
digitalWrite(MS1R, LOW);
digitalWrite(MS2R, HIGH);
}
if (delta == 0) {// sensor values are equal
turn = 2; // go straight
//reset wheels to quarter-step mode
digitalWrite(MS1R, LOW);
digitalWrite(MS2R, HIGH);
digitalWrite(MS1L, LOW);
digitalWrite(MS2L, HIGH);
}
//pulses high or low for each wheel based on turn direction and when or when not to skip///
if( (step < skip) && (turn == 1) ) {
digitalWrite(motorL, HIGH);
}
if( (step < skip) && (turn == 0) ) {
digitalWrite(motorR, HIGH);
}
if( (turn == 1) || (turn == 2) ) {
digitalWrite(motorR, HIGH);
}
if( (turn == 0) || (turn == 2) ) {
digitalWrite(motorL, HIGH);
}
delayMicroseconds(steptime);
if(step < skip && turn == 0) {
digitalWrite(motorR, LOW);
}
if(step < skip && turn == 1) {
digitalWrite(motorL, LOW);
}
if(step >= skip) {//resets or adds to skip
step = 0;
} else {
step++;
}
if (count == sensedist) {//when count reaches sensedist read ultrasonics and reset count
count = 0;
} else {
delayMicroseconds(steptime);
}
} // end for loop
}
//
// manual control of gripper
//
void getagrip(int dist, int dir) {//grabbing subroutine that goes a passed distance and direction
digitalWrite(dirC,dir);
enableAll(0); //enables all motors
for(int x=0;x<dist;x++) {// Pulse HIGH and LOW dist times, to grab ball
digitalWrite(motorC,LOW);
delayMicroseconds(1000);
digitalWrite(motorC,HIGH);
delayMicroseconds(1000);
}
}
//
// basic move
//
void drive(int dist, int dir) {
digitalWrite(dirL,dir);
digitalWrite(dirR,dir);
for(int x=0;x<dist;x++) {//basic driving pulse that also continual grabs ball for insurance
digitalWrite(motorL,LOW);
digitalWrite(motorR,LOW);
digitalWrite(motorC,LOW);
delayMicroseconds(2500);
digitalWrite(motorL,HIGH);
digitalWrite(motorR,HIGH);
digitalWrite(motorC,HIGH);
delayMicroseconds(2500);
}
}
//
//
//
void spin(int dist, int dir1, int dir2) {//basic turning behavior that also continually grabs ball for
insurance
digitalWrite(dirL,dir1);
digitalWrite(dirR,dir2);
for(int x=0;x<dist;x++) {
digitalWrite(motorL,LOW);
digitalWrite(motorR,LOW);
digitalWrite(motorC,LOW);
delayMicroseconds(2500);
digitalWrite(motorL,HIGH);
digitalWrite(motorR,HIGH);
digitalWrite(motorC,HIGH);
delayMicroseconds(2500);
}
}
//subroutine that does the last bit of navigating to the dropoff box based on distance from center
sensor reading, uses the same logic as lane follow, except it keeps the right wall at a specified
distance away, instead of setting right and left distance equal
void wallfollow(int dir){
int steptime = 1400;
int step = 0;
int skip = 1;
int turn = 0;
int count = 0; //keep track of steps
int sensedist = 26;
int pingL, pingR, delta;
if (count == sensedist) {
count = 0;
digitalWrite(motorC,LOW);
} else {
delayMicroseconds(steptime);
}
} // end for loop
}
void enableAll(int able){// enables or disables motors based on integer passed to it( 1 = disable,
0 = enable)
digitalWrite(MotorEnableL, able);
digitalWrite(MotorEnableR, able);
digitalWrite(MotorEnableC, able);
}
void toBall(){ // subroutine that navigates to the ball based on front sensor reading utilizing the
already existing lanefollow subroutine
int ball; // used to calculate how far ball is
ball = pingAve(3,2);
Serial.print("going forward: ");Serial.println(ball*26.7);
lanefollow(ball*26.7, HIGH);
}
void depart(){ //negotiate the intersection after grabbing the ball utilizing calibrated drive and
spin subroutines distances and directions
drive(400,0);
drive(200,2);
spin(150,1,0);
drive(250,2);
spin(80,1,0);
}
void yuck(){ // utilizes getagrip subroutine to drop ball and drive and spin subroutines to ensure
the ball is in the dropoff box
getagrip(200,0);
spin(10,0,1);
spin(20,1,0);
spin(20,0,1);
spin(10,1,0);
drive(50,1);
spin(10,0,1);
spin(20,1,0);
spin(20,0,1);
spin(10,1,0);
drive(50,1);
spin(10,0,1);
spin(20,1,0);
spin(20,0,1);
spin(10,1,0);
drive(50,1);
enableAll(1);
}
void autonomous(int dist){//calls the series of subroutines to run the entire course
toBall();
toBall();
getagrip(250,1);
depart();
lanefollow(dist, 1);
wallfollow(1);
yuck();
}
//end all