AKUL check if this has the correct right and left turning, if not replace it Code with lots of comments
Fork of Working_on_Left_and_Right by
Diff: main.cpp
- Revision:
- 10:e2267771f7d4
- Parent:
- 9:accfae3aaf72
diff -r accfae3aaf72 -r e2267771f7d4 main.cpp --- a/main.cpp Wed Jun 04 11:38:06 2014 +0000 +++ b/main.cpp Thu Jun 05 08:49:03 2014 +0000 @@ -10,57 +10,78 @@ using namespace std; +//EFFECTS: looks at a number of sensors to determine if the robot has reached a cross. + // If the robot has reached a cross, it returns TRUE. If not, it returns FALSE bool cross_detection(int sensor[5], int black_thresh, int white_thresh); + +// REQUIRES: Startpoint and endpoint must be between 1 and 6, inclusive +// EFFECTS: returns a string of directions fo either L (left), R (right), or +// F (forward) to tell the robot how to get from Startpoint to endpoints +string directions(int startpoint, int endpoint); + + void mapping(string directions, float speed, int turns); -string directions(int startpoint, int endpoint); + void turn_right(); void turn_left(); - -int end(); -int start(); - m3pi thinggy; - int black_thresh = 300; - int white_thresh = 240; - float speed = 0.25; - float turn_speed = 0.2; - float correction; - float k = -0.3; - int sensor[5]; - int returned; +//black and white thresholds chose after testing +int black_thresh = 300; +int white_thresh = 240; + +//normal speed and turn speed to slow +float speed = 0.25; +float turn_speed = 0.2; + +//used to change the direction of the car +float correction; + +//k was chosen after testing +float k = -0.3; + +int sensor[5]; +int returned; int main() { bool cross = 0; wait(1.0); - + + //calibrate the sensors thinggy.sensor_auto_calibrate(); - thinggy.calibrated_sensor(sensor); //find the average of the three sensors returned = (sensor[1] + sensor[2] + sensor[3])/3; - //finds the directions of the robot - int startpt = 2; //start(); - int endpt = 1; //end(); + //enter in the endpoint and startpoint here + int startpt = 2; + int endpt = 1; string d = directions(startpt, endpt); + + //declares the number of turns so that it can be incremented after each turn int turns = 0; + //performs movement while(1) { - //check if it needs to turn + + //checks if it needs to turn while(returned <= 240){ + //turns right while(sensor[0] < sensor[4] && thinggy.line_position() != 0){ cross = cross_detection(sensor, black_thresh, white_thresh); + + //checks if there is a cross if(cross){ mapping(d, speed, turns); ++turns; cross = 0; } + //corrects turn else { thinggy.left_motor(turn_speed); thinggy.right_motor(-turn_speed); @@ -72,11 +93,13 @@ //turns left while(sensor[4] > sensor[0] && thinggy.line_position() != 0){ cross = cross_detection(sensor, black_thresh, white_thresh); + //checks for cross if(cross){ mapping(d, speed, turns); ++turns; cross = 0; } + //corrects pathway else{ thinggy.left_motor(-turn_speed); thinggy.right_motor(turn_speed); @@ -94,6 +117,7 @@ float position = thinggy.line_position(); correction = k*(position); cross = cross_detection(sensor, black_thresh, white_thresh); + //checks for cross if(cross){ mapping(d, speed, turns); ++turns; @@ -116,12 +140,14 @@ thinggy.left_motor(speed-correction); thinggy.right_motor(speed+correction); - //Infared: stop if obstructed + //Infared: will stop if obstructed m3pi_IN[0].mode(PullUp); while (m3pi_IN[0]==0){ thinggy.stop(); } } + + //recalibrate thinggy.calibrated_sensor(sensor); returned = (sensor[1] + sensor[2]*2 + sensor[3])/4; }//while returned > 220 @@ -133,10 +159,8 @@ } -//DONE - //REQUIRES: array of 5 ints - //EFFECTS: stops the robot if it comes to any interesection where a decision has to be made - // and returns true if there is a cross + //EFFECTS: looks at a number of sensors to determine if the robot has reached a cross. + // If the robot has reached a cross, it returns TRUE. If not, it returns FALSE bool cross_detection(int sensor[5], int black_thresh, int white_thresh){ //three directions to choose from NOT WORKING if(sensor[0] > black_thresh and sensor[2] > black_thresh and sensor[4] > black_thresh){ @@ -158,18 +182,10 @@ return 0; } -//take in the starting point of the robot from bluetooth -int start(){ - return 0; -} -//take in the ending point of the robot from bluetooth -int end(){ - return 0; -} - -//DONE -//gives the string to use for the map +// REQUIRES: Startpoint and endpoint must be between 1 and 6, inclusive +// EFFECTS: returns a string of directions fo either L (left), R (right), or +// F (forward) to tell the robot how to get from Startpoint to endpoints string directions(int startpoint, int endpoint){ string charmap[6][6] = { {"", "RLRLLR", "RLRLFF", "RLRRRLLF", "RLRRLFLRRRL", "RLRRLR"}, @@ -179,31 +195,35 @@ {"RLLLRFRLL", "RLLRLFR", "RLFLR", "RLFF", "", "RLLLRL"}, {"LRLL", "LFLR", "LRFFF", "RLRLF", "RLRRRL", ""} }; + return charmap[startpoint - 1][endpoint - 1]; } -//DONE -//takes in the string of directions and the number of turns -//completed and then tells it whether to do left, right, forward +//MODIFIES: Direction and speed +//EFFECTS: Looks at the turn in the string of directions and tells it whether +// whether to go left, right, forward void mapping(string directions, float speed, int turns){ - char x = directions[turns]; //something in the string + //char x is L, R, F, or NULL to perform at the specified turn + char x = directions[turns]; - //tells it which direction to go + //left turn if(x == 'L'){ turn_left(); return; } + //right turn else if(x == 'R'){ turn_right(); return; } + //move forward else if(x == 'F'){ - thinggy.printf("F"); thinggy.forward(speed); wait(.1); return; } + //if it reaches the last element in the string else if (x == NULL){ thinggy.stop(); wait(300); @@ -211,6 +231,7 @@ } } +//EFFECTS: turns the robot left void turn_left(){ int n=0; wait(.08); @@ -221,9 +242,9 @@ } thinggy.forward(0.2); wait(0.15); - } +//EFFECTS: turns the robot right void turn_right(){ int n=0; wait(.08); @@ -234,5 +255,4 @@ } thinggy.forward(0.2); wait(0.15); - } \ No newline at end of file