Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat May 27 21:29:55 2017 +0000
Revision:
38:fe05f93009a2
Parent:
37:3dcc95e9321c
Child:
39:058fb32c24e0
Playing around with constants

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 0:a03c771ab78e 1 #include "mbed.h"
sahilmgandhi 31:9b71b44e0867 2
kyleliangus 6:3d68fedd6fd9 3 #include "irpair.h"
kyleliangus 4:b5b7836ca2b0 4 #include "main.h"
kyleliangus 4:b5b7836ca2b0 5 #include "motor.h"
sahilmgandhi 31:9b71b44e0867 6
sahilmgandhi 26:d20f1adac2d3 7 #include <stdlib.h>
sahilmgandhi 26:d20f1adac2d3 8 #include <stack> // std::stack
sahilmgandhi 26:d20f1adac2d3 9 #include <utility> // std::pair, std::make_pair
christine222 25:f827a8b7880e 10
sahilmgandhi 1:8a4b2f573923 11 #include "ITG3200.h"
sahilmgandhi 14:9e7bb03ddccb 12 #include "stm32f4xx.h"
sahilmgandhi 7:6f5cb6377bd4 13 #include "QEI.h"
christine222 25:f827a8b7880e 14
sahilmgandhi 33:68ce1f74ab5f 15 /*LEFT/RIGHT BOTH WALLS
sahilmgandhi 33:68ce1f74ab5f 16 0.34 0.12
sahilmgandhi 33:68ce1f74ab5f 17
sahilmgandhi 33:68ce1f74ab5f 18 LEFT/RIGHT LEFT WALL GONE
sahilmgandhi 33:68ce1f74ab5f 19 0.02 0.11
sahilmgandhi 31:9b71b44e0867 20
sahilmgandhi 33:68ce1f74ab5f 21 LEFT/RIGHT RIGTH WALL GONE
sahilmgandhi 33:68ce1f74ab5f 22 0.33 0.008
sahilmgandhi 31:9b71b44e0867 23
sahilmgandhi 33:68ce1f74ab5f 24 LEFT/RIGHT BOTH WALLS GONE
sahilmgandhi 33:68ce1f74ab5f 25 0.02 0.008
sahilmgandhi 33:68ce1f74ab5f 26
sahilmgandhi 33:68ce1f74ab5f 27 LEFT/RIGHT CLOSE TO RIGHT WALL
sahilmgandhi 33:68ce1f74ab5f 28 0.14 0.47
sahilmgandhi 31:9b71b44e0867 29
sahilmgandhi 33:68ce1f74ab5f 30 LEFT/RIGHT CLOSE TO LEFT WALL
sahilmgandhi 33:68ce1f74ab5f 31 0.89 0.05
sahilmgandhi 31:9b71b44e0867 32
sahilmgandhi 33:68ce1f74ab5f 33 FRONT IRS NEAR WALL (STOPPING DISTANCE)
sahilmgandhi 33:68ce1f74ab5f 34 0.41 0.49
sahilmgandhi 31:9b71b44e0867 35
sahilmgandhi 33:68ce1f74ab5f 36 FRONT IRS NO WALL AHEAD (FOR ATLEAST 1 FULL CELL)
sahilmgandhi 33:68ce1f74ab5f 37 0.07 0.06
sahilmgandhi 31:9b71b44e0867 38
sahilmgandhi 33:68ce1f74ab5f 39 */
sahilmgandhi 34:69342782fb68 40
sahilmgandhi 37:3dcc95e9321c 41 #define IP_CONSTANT 2.00
sahilmgandhi 37:3dcc95e9321c 42 #define II_CONSTANT 0.00001
sahilmgandhi 37:3dcc95e9321c 43 #define ID_CONSTANT 0.190
sahilmgandhi 29:ec2c5a69acd6 44
sahilmgandhi 16:d9252437bd92 45 void pidOnEncoders();
sahilmgandhi 29:ec2c5a69acd6 46
sahilmgandhi 31:9b71b44e0867 47 void moveForwardCellEncoder(double cellNum)
sahilmgandhi 31:9b71b44e0867 48 {
sahilmgandhi 17:f713758f6238 49 int desiredCount0 = encoder0.getPulses() + oneCellCountMomentum*cellNum;
sahilmgandhi 17:f713758f6238 50 int desiredCount1 = encoder1.getPulses() + oneCellCountMomentum*cellNum;
sahilmgandhi 31:9b71b44e0867 51
sahilmgandhi 37:3dcc95e9321c 52 left_motor.forward(WHEEL_SPEED);
sahilmgandhi 37:3dcc95e9321c 53 right_motor.forward(WHEEL_SPEED);
sahilmgandhi 19:7b66a518b6f8 54 wait_ms(1);
sahilmgandhi 31:9b71b44e0867 55 while (encoder0.getPulses() <= desiredCount0 && encoder1.getPulses() <= desiredCount1) {
sahilmgandhi 20:82836745332e 56 receiverTwoReading = IRP_2.getSamples(100);
sahilmgandhi 20:82836745332e 57 receiverThreeReading = IRP_3.getSamples(100);
sahilmgandhi 20:82836745332e 58 // serial.printf("Average 2: %f Average 3: %f Sensor 2: %f Sensor 3: %f\n", IRP_2.sensorAvg, IRP_3.sensorAvg, receiverTwoReading, receiverThreeReading);
sahilmgandhi 31:9b71b44e0867 59 if (receiverThreeReading < ir3base) {
sahilmgandhi 20:82836745332e 60 // redLed.write(1);
sahilmgandhi 20:82836745332e 61 // blueLed.write(0);
sahilmgandhi 19:7b66a518b6f8 62 turnFlag |= RIGHT_FLAG;
sahilmgandhi 31:9b71b44e0867 63 } else if (receiverTwoReading < ir2base) {
sahilmgandhi 20:82836745332e 64 // redLed.write(0);
sahilmgandhi 20:82836745332e 65 // blueLed.write(1);
sahilmgandhi 17:f713758f6238 66 turnFlag |= LEFT_FLAG;
sahilmgandhi 20:82836745332e 67 }
sahilmgandhi 16:d9252437bd92 68 pidOnEncoders();
sahilmgandhi 16:d9252437bd92 69 }
sahilmgandhi 31:9b71b44e0867 70
sahilmgandhi 16:d9252437bd92 71 left_motor.brake();
sahilmgandhi 14:9e7bb03ddccb 72 right_motor.brake();
sahilmgandhi 14:9e7bb03ddccb 73 }
sahilmgandhi 31:9b71b44e0867 74
sahilmgandhi 31:9b71b44e0867 75
sahilmgandhi 31:9b71b44e0867 76 void moveForwardEncoder(double num)
sahilmgandhi 31:9b71b44e0867 77 {
christine222 21:9a6cb07bdcb6 78 int count0;
christine222 21:9a6cb07bdcb6 79 int count1;
christine222 21:9a6cb07bdcb6 80 count0 = encoder0.getPulses();
christine222 21:9a6cb07bdcb6 81 count1 = encoder1.getPulses();
christine222 21:9a6cb07bdcb6 82 int initial1 = count1;
christine222 21:9a6cb07bdcb6 83 int initial0 = count0;
christine222 21:9a6cb07bdcb6 84 int diff = count0 - count1;
christine222 21:9a6cb07bdcb6 85 double kp = 0.00015;
christine222 21:9a6cb07bdcb6 86 double kd = 0.00019;
christine222 21:9a6cb07bdcb6 87 int prev = 0;
christine222 25:f827a8b7880e 88
sahilmgandhi 37:3dcc95e9321c 89 double speed0 = WHEEL_SPEED;
sahilmgandhi 37:3dcc95e9321c 90 double speed1 = WHEEL_SPEED;
christine222 21:9a6cb07bdcb6 91 right_motor.move(speed0);
christine222 21:9a6cb07bdcb6 92 left_motor.move(speed1);
sahilmgandhi 38:fe05f93009a2 93 receiverOneReading = IRP_1.getSamples(100);
sahilmgandhi 38:fe05f93009a2 94 receiverTwoReading = IRP_4.getSamples(100);
sahilmgandhi 31:9b71b44e0867 95
sahilmgandhi 38:fe05f93009a2 96 while( ((encoder0.getPulses() - initial0) <= (oneCellCountMomentum-200)*num && (encoder1.getPulses() - initial1) <= (oneCellCountMomentum-200)*num) && receiverOneReading < IRP_1.sensorAvg*4 && receiverFourReading < IRP_4.sensorAvg*4) {
sahilmgandhi 31:9b71b44e0867 97 //while( (IRP_1.getSamples(50) + IRP_4.getSamples(50))/2 < ((IRP_1.sensorAvg+IRP_2.sensorAvg)/2)*0.4 ){
christine222 21:9a6cb07bdcb6 98 //serial.printf("IRS= >: %f, %f \r\n", IRP_2.getSamples( 100 ), IRP_3.getSamples( 100 ));
sahilmgandhi 31:9b71b44e0867 99
christine222 21:9a6cb07bdcb6 100 count0 = encoder0.getPulses() - initial0;
christine222 21:9a6cb07bdcb6 101 count1 = encoder1.getPulses() - initial1;
christine222 21:9a6cb07bdcb6 102 int x = count0 - count1;
christine222 21:9a6cb07bdcb6 103 //double d = kp * x + kd * ( x - prev );
christine222 21:9a6cb07bdcb6 104 double kppart = kp * x;
christine222 21:9a6cb07bdcb6 105 double kdpart = kd * (x-prev);
christine222 21:9a6cb07bdcb6 106 double d = kppart + kdpart;
sahilmgandhi 31:9b71b44e0867 107
christine222 21:9a6cb07bdcb6 108 //serial.printf( "x: %d,\t prev: %d,\t d: %f,\t kppart: %f,\t kdpart: %f\n", x, prev, d, kppart, kdpart );
sahilmgandhi 31:9b71b44e0867 109 if( x < diff - 40 ) { // count1 is bigger, right wheel pushed forward
christine222 21:9a6cb07bdcb6 110 left_motor.move( speed1-0.8*d );
christine222 21:9a6cb07bdcb6 111 right_motor.move( speed0+d );
sahilmgandhi 31:9b71b44e0867 112 } else if( x > diff + 40 ) {
christine222 21:9a6cb07bdcb6 113 left_motor.move( speed1-0.8*d );
christine222 21:9a6cb07bdcb6 114 right_motor.move( speed0+d );
christine222 21:9a6cb07bdcb6 115 }
sahilmgandhi 38:fe05f93009a2 116 prev = x;
sahilmgandhi 38:fe05f93009a2 117 receiverOneReading = IRP_1.getSamples(100);
sahilmgandhi 38:fe05f93009a2 118 receiverFourReading = IRP_4.getSamples(100);
sahilmgandhi 38:fe05f93009a2 119 }
sahilmgandhi 38:fe05f93009a2 120
sahilmgandhi 38:fe05f93009a2 121 //pidOnEncoders();
sahilmgandhi 38:fe05f93009a2 122 //pidBrake();
sahilmgandhi 38:fe05f93009a2 123 right_motor.brake();
sahilmgandhi 38:fe05f93009a2 124 left_motor.brake();
sahilmgandhi 38:fe05f93009a2 125 return;
sahilmgandhi 38:fe05f93009a2 126 }
sahilmgandhi 38:fe05f93009a2 127
sahilmgandhi 38:fe05f93009a2 128
sahilmgandhi 38:fe05f93009a2 129 void moveForwardWallEncoder()
sahilmgandhi 38:fe05f93009a2 130 {
sahilmgandhi 38:fe05f93009a2 131 int count0;
sahilmgandhi 38:fe05f93009a2 132 int count1;
sahilmgandhi 38:fe05f93009a2 133 count0 = encoder0.getPulses();
sahilmgandhi 38:fe05f93009a2 134 count1 = encoder1.getPulses();
sahilmgandhi 38:fe05f93009a2 135 int initial1 = count1;
sahilmgandhi 38:fe05f93009a2 136 int initial0 = count0;
sahilmgandhi 38:fe05f93009a2 137 int diff = count0 - count1;
sahilmgandhi 38:fe05f93009a2 138 double kp = 0.00015;
sahilmgandhi 38:fe05f93009a2 139 double kd = 0.00019;
sahilmgandhi 38:fe05f93009a2 140 int prev = 0;
sahilmgandhi 38:fe05f93009a2 141
sahilmgandhi 38:fe05f93009a2 142 double speed0 = WHEEL_SPEED;
sahilmgandhi 38:fe05f93009a2 143 double speed1 = WHEEL_SPEED;
sahilmgandhi 38:fe05f93009a2 144 right_motor.move(speed0);
sahilmgandhi 38:fe05f93009a2 145 left_motor.move(speed1);
sahilmgandhi 38:fe05f93009a2 146
sahilmgandhi 38:fe05f93009a2 147 double ir1 = IRP_1.getSamples(50);
sahilmgandhi 38:fe05f93009a2 148 double ir4 = IRP_4.getSamples(50);
sahilmgandhi 38:fe05f93009a2 149
sahilmgandhi 38:fe05f93009a2 150 if(ir1 > IRP_1.sensorAvg*4 || ir4 > IRP_2.sensorAvg*4){
sahilmgandhi 38:fe05f93009a2 151 return;
sahilmgandhi 38:fe05f93009a2 152 }
sahilmgandhi 38:fe05f93009a2 153
sahilmgandhi 38:fe05f93009a2 154 //while((encoder0.getPulses() - initial0) <= (oneCellCountMomentum-200) && (encoder1.getPulses() - initial1) <= (oneCellCountMomentum-200)) {
sahilmgandhi 38:fe05f93009a2 155 //while( (ir1 + ir4)/2 < ((IRP_1.sensorAvg+IRP_4.sensorAvg)/2)*0.4 ){
sahilmgandhi 38:fe05f93009a2 156 while( ir1 < IRP_1.sensorAvg*0.7 || ir4 < IRP_4.sensorAvg*0.7 ) {
sahilmgandhi 38:fe05f93009a2 157 //serial.printf("IRS= >: %f, %f \r\n", IRP_2.getSamples( 100 ), IRP_3.getSamples( 100 ));
sahilmgandhi 38:fe05f93009a2 158
sahilmgandhi 38:fe05f93009a2 159 count0 = encoder0.getPulses() - initial0;
sahilmgandhi 38:fe05f93009a2 160 count1 = encoder1.getPulses() - initial1;
sahilmgandhi 38:fe05f93009a2 161 int x = count0 - count1;
sahilmgandhi 38:fe05f93009a2 162 //double d = kp * x + kd * ( x - prev );
sahilmgandhi 38:fe05f93009a2 163 double kppart = kp * x;
sahilmgandhi 38:fe05f93009a2 164 double kdpart = kd * (x-prev);
sahilmgandhi 38:fe05f93009a2 165 double d = kppart + kdpart;
sahilmgandhi 38:fe05f93009a2 166
sahilmgandhi 38:fe05f93009a2 167 //serial.printf( "x: %d,\t prev: %d,\t d: %f,\t kppart: %f,\t kdpart: %f\n", x, prev, d, kppart, kdpart );
sahilmgandhi 38:fe05f93009a2 168 if( x < diff - 40 ) { // count1 is bigger, right wheel pushed forward
sahilmgandhi 38:fe05f93009a2 169 left_motor.move( speed1-0.8*d );
sahilmgandhi 38:fe05f93009a2 170 right_motor.move( speed0+d );
sahilmgandhi 38:fe05f93009a2 171 } else if( x > diff + 40 ) {
sahilmgandhi 38:fe05f93009a2 172 left_motor.move( speed1-0.8*d );
sahilmgandhi 38:fe05f93009a2 173 right_motor.move( speed0+d );
sahilmgandhi 38:fe05f93009a2 174 }
christine222 21:9a6cb07bdcb6 175 // else
christine222 21:9a6cb07bdcb6 176 // {
christine222 21:9a6cb07bdcb6 177 // left_motor.brake();
sahilmgandhi 31:9b71b44e0867 178 // right_motor.brake();
christine222 21:9a6cb07bdcb6 179 // }
sahilmgandhi 31:9b71b44e0867 180 prev = x;
christine222 21:9a6cb07bdcb6 181 }
sahilmgandhi 31:9b71b44e0867 182
christine222 21:9a6cb07bdcb6 183 //pidOnEncoders();
christine222 23:690b0ca34ee9 184 //pidBrake();
christine222 23:690b0ca34ee9 185 right_motor.brake();
christine222 23:690b0ca34ee9 186 left_motor.brake();
christine222 23:690b0ca34ee9 187 return;
christine222 23:690b0ca34ee9 188 }
sahilmgandhi 31:9b71b44e0867 189
sahilmgandhi 14:9e7bb03ddccb 190 void printDipFlag()
sahilmgandhi 14:9e7bb03ddccb 191 {
vanshg 11:8fc2b703086b 192 if (DEBUGGING) serial.printf("Flag value is %d", dipFlags);
vanshg 11:8fc2b703086b 193 }
sahilmgandhi 31:9b71b44e0867 194
sahilmgandhi 14:9e7bb03ddccb 195 void enableButton1()
sahilmgandhi 14:9e7bb03ddccb 196 {
vanshg 10:810d1849da9d 197 dipFlags |= BUTTON1_FLAG;
vanshg 11:8fc2b703086b 198 printDipFlag();
vanshg 10:810d1849da9d 199 }
sahilmgandhi 14:9e7bb03ddccb 200 void enableButton2()
sahilmgandhi 14:9e7bb03ddccb 201 {
vanshg 10:810d1849da9d 202 dipFlags |= BUTTON2_FLAG;
vanshg 11:8fc2b703086b 203 printDipFlag();
vanshg 10:810d1849da9d 204 }
sahilmgandhi 14:9e7bb03ddccb 205 void enableButton3()
sahilmgandhi 14:9e7bb03ddccb 206 {
vanshg 10:810d1849da9d 207 dipFlags |= BUTTON3_FLAG;
vanshg 11:8fc2b703086b 208 printDipFlag();
vanshg 10:810d1849da9d 209 }
sahilmgandhi 14:9e7bb03ddccb 210 void enableButton4()
sahilmgandhi 14:9e7bb03ddccb 211 {
vanshg 10:810d1849da9d 212 dipFlags |= BUTTON4_FLAG;
vanshg 11:8fc2b703086b 213 printDipFlag();
vanshg 10:810d1849da9d 214 }
sahilmgandhi 14:9e7bb03ddccb 215 void disableButton1()
sahilmgandhi 14:9e7bb03ddccb 216 {
vanshg 10:810d1849da9d 217 dipFlags &= ~BUTTON1_FLAG;
vanshg 11:8fc2b703086b 218 printDipFlag();
vanshg 10:810d1849da9d 219 }
sahilmgandhi 14:9e7bb03ddccb 220 void disableButton2()
sahilmgandhi 14:9e7bb03ddccb 221 {
vanshg 10:810d1849da9d 222 dipFlags &= ~BUTTON2_FLAG;
vanshg 11:8fc2b703086b 223 printDipFlag();
vanshg 10:810d1849da9d 224 }
sahilmgandhi 14:9e7bb03ddccb 225 void disableButton3()
sahilmgandhi 14:9e7bb03ddccb 226 {
vanshg 10:810d1849da9d 227 dipFlags &= ~BUTTON3_FLAG;
vanshg 11:8fc2b703086b 228 printDipFlag();
vanshg 10:810d1849da9d 229 }
sahilmgandhi 14:9e7bb03ddccb 230 void disableButton4()
sahilmgandhi 14:9e7bb03ddccb 231 {
vanshg 10:810d1849da9d 232 dipFlags &= ~BUTTON4_FLAG;
vanshg 11:8fc2b703086b 233 printDipFlag();
kyleliangus 9:1d8e4da058cd 234 }
sahilmgandhi 31:9b71b44e0867 235
kyleliangus 15:b80555a4a8b9 236 void pidOnEncoders()
kyleliangus 15:b80555a4a8b9 237 {
kyleliangus 15:b80555a4a8b9 238 int count0;
kyleliangus 15:b80555a4a8b9 239 int count1;
kyleliangus 15:b80555a4a8b9 240 count0 = encoder0.getPulses();
kyleliangus 15:b80555a4a8b9 241 count1 = encoder1.getPulses();
kyleliangus 15:b80555a4a8b9 242 int diff = count0 - count1;
sahilmgandhi 33:68ce1f74ab5f 243 double kp = 0.00016;
sahilmgandhi 28:8126a4d620e8 244 double kd = 0.00016;
kyleliangus 15:b80555a4a8b9 245 int prev = 0;
sahilmgandhi 31:9b71b44e0867 246
sahilmgandhi 16:d9252437bd92 247 int counter = 0;
sahilmgandhi 31:9b71b44e0867 248 while(1) {
kyleliangus 15:b80555a4a8b9 249 count0 = encoder0.getPulses();
kyleliangus 15:b80555a4a8b9 250 count1 = encoder1.getPulses();
kyleliangus 15:b80555a4a8b9 251 int x = count0 - count1;
kyleliangus 15:b80555a4a8b9 252 //double d = kp * x + kd * ( x - prev );
kyleliangus 15:b80555a4a8b9 253 double kppart = kp * x;
kyleliangus 15:b80555a4a8b9 254 double kdpart = kd * (x-prev);
kyleliangus 15:b80555a4a8b9 255 double d = kppart + kdpart;
sahilmgandhi 31:9b71b44e0867 256
kyleliangus 15:b80555a4a8b9 257 //serial.printf( "x: %d,\t prev: %d,\t d: %f,\t kppart: %f,\t kdpart: %f\n", x, prev, d, kppart, kdpart );
sahilmgandhi 31:9b71b44e0867 258 if( x < diff - 60 ) { // count1 is bigger, right wheel pushed forward
kyleliangus 15:b80555a4a8b9 259 left_motor.move( -d );
kyleliangus 15:b80555a4a8b9 260 right_motor.move( d );
sahilmgandhi 31:9b71b44e0867 261 } else if( x > diff + 60 ) {
kyleliangus 15:b80555a4a8b9 262 left_motor.move( -d );
kyleliangus 15:b80555a4a8b9 263 right_motor.move( d );
kyleliangus 15:b80555a4a8b9 264 }
sahilmgandhi 16:d9252437bd92 265 // else
sahilmgandhi 16:d9252437bd92 266 // {
sahilmgandhi 16:d9252437bd92 267 // left_motor.brake();
sahilmgandhi 31:9b71b44e0867 268 // right_motor.brake();
sahilmgandhi 16:d9252437bd92 269 // }
kyleliangus 15:b80555a4a8b9 270 prev = x;
sahilmgandhi 16:d9252437bd92 271 counter++;
sahilmgandhi 28:8126a4d620e8 272 if (counter == 10)
sahilmgandhi 16:d9252437bd92 273 break;
kyleliangus 15:b80555a4a8b9 274 }
kyleliangus 15:b80555a4a8b9 275 }
sahilmgandhi 31:9b71b44e0867 276
sahilmgandhi 31:9b71b44e0867 277 void nCellEncoderAndIR(double cellCount)
sahilmgandhi 31:9b71b44e0867 278 {
sahilmgandhi 19:7b66a518b6f8 279 double currentError = 0;
sahilmgandhi 19:7b66a518b6f8 280 double previousError = 0;
sahilmgandhi 19:7b66a518b6f8 281 double derivError = 0;
sahilmgandhi 19:7b66a518b6f8 282 double sumError = 0;
christine222 25:f827a8b7880e 283
sahilmgandhi 37:3dcc95e9321c 284 double HIGH_PWM_VOLTAGE_R = WHEEL_SPEED;
sahilmgandhi 37:3dcc95e9321c 285 double HIGH_PWM_VOLTAGE_L = WHEEL_SPEED;
sahilmgandhi 29:ec2c5a69acd6 286
sahilmgandhi 37:3dcc95e9321c 287 double rightSpeed = WHEEL_SPEED;
sahilmgandhi 37:3dcc95e9321c 288 double leftSpeed = WHEEL_SPEED;
christine222 25:f827a8b7880e 289
christine222 25:f827a8b7880e 290 int desiredCount0 = encoder0.getPulses() + oneCellCountMomentum*cellCount;
christine222 25:f827a8b7880e 291 int desiredCount1 = encoder1.getPulses() + oneCellCountMomentum*cellCount;
kyleliangus 32:69acb14778ea 292 serial.printf("%d, %d\n", desiredCount0, desiredCount1 );
christine222 25:f827a8b7880e 293
sahilmgandhi 37:3dcc95e9321c 294 left_motor.forward(WHEEL_SPEED);
sahilmgandhi 37:3dcc95e9321c 295 right_motor.forward(WHEEL_SPEED);
christine222 25:f827a8b7880e 296
sahilmgandhi 19:7b66a518b6f8 297 float ir2 = IRP_2.getSamples( SAMPLE_NUM );
sahilmgandhi 19:7b66a518b6f8 298 float ir3 = IRP_3.getSamples( SAMPLE_NUM );
christine222 25:f827a8b7880e 299
christine222 23:690b0ca34ee9 300 int state = 0;
christine222 23:690b0ca34ee9 301
sahilmgandhi 31:9b71b44e0867 302 while (encoder0.getPulses() <= desiredCount0 && encoder1.getPulses() <= desiredCount1) {
sahilmgandhi 33:68ce1f74ab5f 303 // serial.printf("The desiredCount0 is: %d \t The desiredCount1 is: %d\t the 0encoderval is :%d\t the 1encoderval is : %d\t\n", desiredCount0, desiredCount1, encoder0.getPulses(), encoder1.getPulses());
kyleliangus 32:69acb14778ea 304 receiverTwoReading = IRP_2.getSamples( SAMPLE_NUM );
kyleliangus 32:69acb14778ea 305 receiverThreeReading = IRP_3.getSamples( SAMPLE_NUM );
kyleliangus 32:69acb14778ea 306 receiverOneReading = IRP_1.getSamples( SAMPLE_NUM );
kyleliangus 32:69acb14778ea 307 receiverFourReading = IRP_4.getSamples( SAMPLE_NUM );
sahilmgandhi 33:68ce1f74ab5f 308 // serial.printf("IR2 = %f, IR2AVE = %f, IR3 = %f, IR3_AVE = %f\n", receiverTwoReading, IRP_2.sensorAvg, receiverThreeReading, IRP_3.sensorAvg);
sahilmgandhi 38:fe05f93009a2 309 if( receiverOneReading > IRP_1.sensorAvg * 5.5 || receiverFourReading > IRP_4.sensorAvg * 5.5) {
sahilmgandhi 26:d20f1adac2d3 310 break;
sahilmgandhi 26:d20f1adac2d3 311 }
christine222 25:f827a8b7880e 312
sahilmgandhi 33:68ce1f74ab5f 313 if((receiverThreeReading < IRP_3.sensorAvg/5) && (receiverTwoReading < IRP_2.sensorAvg/5)) {
christine222 25:f827a8b7880e 314 // both sides gone
sahilmgandhi 33:68ce1f74ab5f 315 double prev0 = encoder0.getPulses();
sahilmgandhi 33:68ce1f74ab5f 316 double prev1 = encoder1.getPulses();
sahilmgandhi 33:68ce1f74ab5f 317 double diff0 = desiredCount0 - prev0;
sahilmgandhi 33:68ce1f74ab5f 318 double diff1 = desiredCount1 - prev1;
sahilmgandhi 33:68ce1f74ab5f 319 double valToPass = ((diff0 + diff1)/2)/(oneCellCountMomentum);
sahilmgandhi 33:68ce1f74ab5f 320 // serial.printf("Going to go over to move forward with encoder, and passing %f\n", valToPass);
sahilmgandhi 26:d20f1adac2d3 321 moveForwardEncoder(valToPass);
sahilmgandhi 33:68ce1f74ab5f 322 continue;
sahilmgandhi 33:68ce1f74ab5f 323 } else if (receiverThreeReading < IRP_3.sensorAvg/3.5) { // right wall gone RED
christine222 25:f827a8b7880e 324 state = 1;
christine222 25:f827a8b7880e 325 redLed.write(0);
christine222 25:f827a8b7880e 326 greenLed.write(1);
christine222 25:f827a8b7880e 327 blueLed.write(1);
sahilmgandhi 33:68ce1f74ab5f 328 } else if (receiverTwoReading < IRP_2.sensorAvg/3.5) { // left wall gone
christine222 25:f827a8b7880e 329 // BLUE BLUE BLUE BLUE
christine222 25:f827a8b7880e 330 state = 2;
christine222 25:f827a8b7880e 331 redLed.write(1);
christine222 25:f827a8b7880e 332 greenLed.write(1);
christine222 25:f827a8b7880e 333 blueLed.write(0);
sahilmgandhi 33:68ce1f74ab5f 334 } else if ((receiverTwoReading > ((IRP_2.sensorAvg)*averageDivUpper)) && (receiverThreeReading > ((IRP_3.sensorAvg)*averageDivUpper))) {
christine222 25:f827a8b7880e 335 // both walls there
christine222 25:f827a8b7880e 336 state = 0;
christine222 25:f827a8b7880e 337 redLed.write(1);
christine222 25:f827a8b7880e 338 greenLed.write(0);
christine222 25:f827a8b7880e 339 blueLed.write(1);
christine222 25:f827a8b7880e 340 }
christine222 25:f827a8b7880e 341
kyleliangus 32:69acb14778ea 342 //serial.printf("Entering switch\n");
sahilmgandhi 31:9b71b44e0867 343 switch(state) {
sahilmgandhi 31:9b71b44e0867 344 case(0): { // both walls there
sahilmgandhi 33:68ce1f74ab5f 345 currentError = ( receiverTwoReading - IRP_2.sensorAvg) - ( receiverThreeReading - IRP_3.sensorAvg);
christine222 25:f827a8b7880e 346 break;
christine222 25:f827a8b7880e 347 }
sahilmgandhi 31:9b71b44e0867 348 case(1): { // RED RED RED RED RED
sahilmgandhi 33:68ce1f74ab5f 349 currentError = (receiverTwoReading - IRP_2.sensorAvg) - (IRP_3.sensorAvg*0.001);
sahilmgandhi 31:9b71b44e0867 350 break;
christine222 25:f827a8b7880e 351 }
sahilmgandhi 31:9b71b44e0867 352 case(2): { // blue
sahilmgandhi 33:68ce1f74ab5f 353 currentError = (IRP_2.sensorAvg*0.001) - (receiverThreeReading - IRP_3.sensorAvg);
christine222 25:f827a8b7880e 354 break;
christine222 25:f827a8b7880e 355 }
sahilmgandhi 31:9b71b44e0867 356 default: {
sahilmgandhi 33:68ce1f74ab5f 357 currentError = ( receiverTwoReading - IRP_2.sensorAvg) - ( receiverThreeReading - IRP_3.sensorAvg);
christine222 25:f827a8b7880e 358 break;
christine222 25:f827a8b7880e 359 }
christine222 25:f827a8b7880e 360 }
kyleliangus 32:69acb14778ea 361 //serial.printf("Exiting switch");
christine222 25:f827a8b7880e 362
christine222 25:f827a8b7880e 363 sumError += currentError;
christine222 25:f827a8b7880e 364 derivError = currentError - previousError;
christine222 25:f827a8b7880e 365 double PIDSum = IP_CONSTANT*currentError + II_CONSTANT*sumError + ID_CONSTANT*derivError;
christine222 25:f827a8b7880e 366 if (PIDSum > 0) { // this means the leftWheel is faster than the right. So right speeds up, left slows down
sahilmgandhi 29:ec2c5a69acd6 367 rightSpeed = HIGH_PWM_VOLTAGE_R - abs(PIDSum*HIGH_PWM_VOLTAGE_R);
sahilmgandhi 29:ec2c5a69acd6 368 leftSpeed = HIGH_PWM_VOLTAGE_L + abs(PIDSum*HIGH_PWM_VOLTAGE_L);
christine222 25:f827a8b7880e 369 } else { // r is faster than L. speed up l, slow down r
sahilmgandhi 29:ec2c5a69acd6 370 rightSpeed = HIGH_PWM_VOLTAGE_R + abs(PIDSum*HIGH_PWM_VOLTAGE_R);
sahilmgandhi 29:ec2c5a69acd6 371 leftSpeed = HIGH_PWM_VOLTAGE_L - abs(PIDSum*HIGH_PWM_VOLTAGE_L);
sahilmgandhi 31:9b71b44e0867 372 }
sahilmgandhi 34:69342782fb68 373
kyleliangus 32:69acb14778ea 374 //serial.printf("%f, %f\n", leftSpeed, rightSpeed);
christine222 25:f827a8b7880e 375 if (leftSpeed > 0.30) leftSpeed = 0.30;
christine222 25:f827a8b7880e 376 if (leftSpeed < 0) leftSpeed = 0;
christine222 25:f827a8b7880e 377 if (rightSpeed > 0.30) rightSpeed = 0.30;
christine222 25:f827a8b7880e 378 if (rightSpeed < 0) rightSpeed = 0;
sahilmgandhi 31:9b71b44e0867 379
christine222 25:f827a8b7880e 380 right_motor.forward(rightSpeed);
christine222 25:f827a8b7880e 381 left_motor.forward(leftSpeed);
sahilmgandhi 33:68ce1f74ab5f 382 pidOnEncoders();
sahilmgandhi 31:9b71b44e0867 383
christine222 25:f827a8b7880e 384 previousError = currentError;
sahilmgandhi 26:d20f1adac2d3 385 }
sahilmgandhi 34:69342782fb68 386
sahilmgandhi 34:69342782fb68 387 // GO BACK A BIT BEFORE BRAKING??
sahilmgandhi 34:69342782fb68 388 left_motor.backward(0.01);
sahilmgandhi 34:69342782fb68 389 right_motor.backward(0.01);
sahilmgandhi 37:3dcc95e9321c 390 wait_us(150);
sahilmgandhi 34:69342782fb68 391 // DELETE THIS IF IT DOES NOT WORK!!
sahilmgandhi 34:69342782fb68 392
sahilmgandhi 34:69342782fb68 393 left_motor.brake();
sahilmgandhi 34:69342782fb68 394 right_motor.brake();
sahilmgandhi 31:9b71b44e0867 395 if (encoder0.getPulses() >= 0.6*desiredCount0 && encoder1.getPulses() >= 0.6*desiredCount1) {
sahilmgandhi 31:9b71b44e0867 396 if (currDir % 4 == 0) {
sahilmgandhi 31:9b71b44e0867 397 mouseY += 1;
sahilmgandhi 31:9b71b44e0867 398 } else if (currDir % 4 == 1) {
kyleliangus 32:69acb14778ea 399 mouseX += 1;
sahilmgandhi 31:9b71b44e0867 400 } else if (currDir % 4 == 2) {
sahilmgandhi 28:8126a4d620e8 401 mouseY -= 1;
sahilmgandhi 31:9b71b44e0867 402 } else if (currDir % 4 == 3) {
sahilmgandhi 28:8126a4d620e8 403 mouseX -= 1;
sahilmgandhi 28:8126a4d620e8 404 }
sahilmgandhi 31:9b71b44e0867 405
sahilmgandhi 31:9b71b44e0867 406 // the mouse has moved forward, we need to update the wall map now
sahilmgandhi 33:68ce1f74ab5f 407 receiverOneReading = IRP_1.getSamples(75);
sahilmgandhi 33:68ce1f74ab5f 408 receiverTwoReading = IRP_2.getSamples(75);
sahilmgandhi 33:68ce1f74ab5f 409 receiverThreeReading = IRP_3.getSamples(75);
sahilmgandhi 33:68ce1f74ab5f 410 receiverFourReading = IRP_4.getSamples(75);
sahilmgandhi 31:9b71b44e0867 411
sahilmgandhi 33:68ce1f74ab5f 412 if (receiverOneReading >= 0.3f && receiverFourReading >= 0.3f) {
sahilmgandhi 31:9b71b44e0867 413 if (currDir % 4 == 0) {
sahilmgandhi 31:9b71b44e0867 414 wallArray[MAZE_LEN - 1 - (mouseY)][mouseX] |= F_WALL;
sahilmgandhi 31:9b71b44e0867 415 } else if (currDir % 4 == 1) {
sahilmgandhi 31:9b71b44e0867 416 wallArray[MAZE_LEN - 1 - (mouseY)][mouseX] |= R_WALL;
sahilmgandhi 31:9b71b44e0867 417 } else if (currDir % 4 == 2) {
sahilmgandhi 31:9b71b44e0867 418 wallArray[MAZE_LEN - 1 - (mouseY)][mouseX] |= L_WALL;
sahilmgandhi 31:9b71b44e0867 419 } else if (currDir % 4 == 3) {
sahilmgandhi 31:9b71b44e0867 420 wallArray[MAZE_LEN - 1 - (mouseY)][mouseX] |= B_WALL;
sahilmgandhi 31:9b71b44e0867 421 }
sahilmgandhi 31:9b71b44e0867 422 }
sahilmgandhi 33:68ce1f74ab5f 423 if (receiverThreeReading >= IRP_3.sensorAvg*0.6) {
sahilmgandhi 31:9b71b44e0867 424 if (currDir % 4 == 0) {
sahilmgandhi 31:9b71b44e0867 425 wallArray[MAZE_LEN - 1 - (mouseY + 1)][mouseX] |= R_WALL;
sahilmgandhi 31:9b71b44e0867 426 } else if (currDir % 4 == 1) {
sahilmgandhi 31:9b71b44e0867 427 wallArray[MAZE_LEN - 1 - (mouseY)][mouseX+1] |= B_WALL;
sahilmgandhi 31:9b71b44e0867 428 } else if (currDir % 4 == 2) {
sahilmgandhi 31:9b71b44e0867 429 wallArray[MAZE_LEN - 1 - (mouseY - 1)][mouseX] |= L_WALL;
sahilmgandhi 31:9b71b44e0867 430 } else if (currDir % 4 == 3) {
sahilmgandhi 31:9b71b44e0867 431 wallArray[MAZE_LEN - 1 - (mouseY)][mouseX-1] |= F_WALL;
sahilmgandhi 31:9b71b44e0867 432 }
sahilmgandhi 31:9b71b44e0867 433 }
sahilmgandhi 33:68ce1f74ab5f 434 if (receiverTwoReading >= IRP_2.sensorAvg*0.6) {
sahilmgandhi 31:9b71b44e0867 435 if (currDir % 4 == 0) {
sahilmgandhi 31:9b71b44e0867 436 wallArray[MAZE_LEN - 1 - (mouseY + 1)][mouseX] |= L_WALL;
sahilmgandhi 31:9b71b44e0867 437 } else if (currDir % 4 == 1) {
sahilmgandhi 31:9b71b44e0867 438 wallArray[MAZE_LEN - 1 - (mouseY)][mouseX+1] |= F_WALL;
sahilmgandhi 31:9b71b44e0867 439 } else if (currDir % 4 == 2) {
sahilmgandhi 31:9b71b44e0867 440 wallArray[MAZE_LEN - 1 - (mouseY - 1)][mouseX] |= R_WALL;
sahilmgandhi 31:9b71b44e0867 441 } else if (currDir % 4 == 3) {
sahilmgandhi 31:9b71b44e0867 442 wallArray[MAZE_LEN - 1 - (mouseY)][mouseX-1] |= B_WALL;
sahilmgandhi 31:9b71b44e0867 443 }
sahilmgandhi 31:9b71b44e0867 444 }
sahilmgandhi 26:d20f1adac2d3 445 }
sahilmgandhi 17:f713758f6238 446 }
christine222 25:f827a8b7880e 447
sahilmgandhi 34:69342782fb68 448 bool isWallInFront(int x, int y)
sahilmgandhi 34:69342782fb68 449 {
sahilmgandhi 27:02ce1040f331 450 return (wallArray[MAZE_LEN - y - 1][x] & F_WALL);
sahilmgandhi 26:d20f1adac2d3 451 }
sahilmgandhi 34:69342782fb68 452 bool isWallInBack(int x, int y)
sahilmgandhi 34:69342782fb68 453 {
sahilmgandhi 27:02ce1040f331 454 return (wallArray[MAZE_LEN - y - 1][x] & B_WALL);
sahilmgandhi 26:d20f1adac2d3 455 }
sahilmgandhi 34:69342782fb68 456 bool isWallOnRight(int x, int y)
sahilmgandhi 34:69342782fb68 457 {
sahilmgandhi 27:02ce1040f331 458 return (wallArray[MAZE_LEN - y - 1][x] & R_WALL);
sahilmgandhi 26:d20f1adac2d3 459 }
sahilmgandhi 34:69342782fb68 460 bool isWallOnLeft(int x, int y)
sahilmgandhi 34:69342782fb68 461 {
sahilmgandhi 27:02ce1040f331 462 return (wallArray[MAZE_LEN - y - 1][x] & L_WALL);
sahilmgandhi 26:d20f1adac2d3 463 }
sahilmgandhi 26:d20f1adac2d3 464
sahilmgandhi 31:9b71b44e0867 465 int chooseNextMovement()
sahilmgandhi 31:9b71b44e0867 466 {
sahilmgandhi 26:d20f1adac2d3 467 int currentDistance = manhattanDistances[MAZE_LEN - 1 - mouseY][mouseX];
sahilmgandhi 31:9b71b44e0867 468 if (goingToCenter && (currentDistance == 0)) {
sahilmgandhi 26:d20f1adac2d3 469 goingToCenter = false;
sahilmgandhi 26:d20f1adac2d3 470 changeManhattanDistance(goingToCenter);
sahilmgandhi 31:9b71b44e0867 471 } else if (!goingToCenter && (currentDistance == 0)) {
kyleliangus 32:69acb14778ea 472 goingToCenter = true;
sahilmgandhi 26:d20f1adac2d3 473 changeManhattanDistance(goingToCenter);
sahilmgandhi 26:d20f1adac2d3 474 }
sahilmgandhi 26:d20f1adac2d3 475
sahilmgandhi 26:d20f1adac2d3 476 visitedCells[MAZE_LEN - 1 - mouseY][mouseX] = 1;
sahilmgandhi 26:d20f1adac2d3 477 int currX = 0;
sahilmgandhi 26:d20f1adac2d3 478 int currY = 0;
sahilmgandhi 26:d20f1adac2d3 479 int currDist = 0;
sahilmgandhi 26:d20f1adac2d3 480 int dirToGo = 0;
sahilmgandhi 31:9b71b44e0867 481 if (!justTurned) {
sahilmgandhi 26:d20f1adac2d3 482 cellsToVisit.push(make_pair(mouseX, mouseY));
sahilmgandhi 26:d20f1adac2d3 483 while (!cellsToVisit.empty()) {
sahilmgandhi 26:d20f1adac2d3 484 pair<int, int> curr = cellsToVisit.top();
sahilmgandhi 26:d20f1adac2d3 485 cellsToVisit.pop();
sahilmgandhi 26:d20f1adac2d3 486 currX = curr.first;
sahilmgandhi 26:d20f1adac2d3 487 currY = curr.second;
sahilmgandhi 26:d20f1adac2d3 488 currDist = manhattanDistances[(MAZE_LEN - 1) - currY][currX];
sahilmgandhi 26:d20f1adac2d3 489 int minDist = INT_MAX;
sahilmgandhi 26:d20f1adac2d3 490
sahilmgandhi 31:9b71b44e0867 491 if (hasVisited(currX, currY)) { // then we want to actually see where the walls are, else we treat it as if there are no walls!
sahilmgandhi 26:d20f1adac2d3 492 if (currX + 1 < MAZE_LEN && !isWallOnRight(currX, currY)) {
sahilmgandhi 26:d20f1adac2d3 493 if (manhattanDistances[MAZE_LEN - 1 - currY][currX + 1] < minDist) {
sahilmgandhi 26:d20f1adac2d3 494 minDist = manhattanDistances[MAZE_LEN - 1 - currY][currX + 1];
sahilmgandhi 26:d20f1adac2d3 495 }
sahilmgandhi 26:d20f1adac2d3 496 }
sahilmgandhi 27:02ce1040f331 497 if (currX - 1 >= 0 && !isWallOnLeft(currX, currY)) {
sahilmgandhi 26:d20f1adac2d3 498 if (manhattanDistances[MAZE_LEN - 1 - currY][currX - 1] < minDist) {
sahilmgandhi 26:d20f1adac2d3 499 minDist = manhattanDistances[MAZE_LEN - 1 - currY][currX - 1];
sahilmgandhi 26:d20f1adac2d3 500 }
sahilmgandhi 26:d20f1adac2d3 501 }
sahilmgandhi 27:02ce1040f331 502 if (currY + 1 < MAZE_LEN && !isWallInFront( currX, currY)) {
sahilmgandhi 26:d20f1adac2d3 503 if (manhattanDistances[MAZE_LEN - 1 - (currY + 1)][currX] < minDist) {
sahilmgandhi 26:d20f1adac2d3 504 minDist = manhattanDistances[MAZE_LEN - 1 - (currY + 1)][currX];
sahilmgandhi 26:d20f1adac2d3 505 }
sahilmgandhi 26:d20f1adac2d3 506 }
sahilmgandhi 27:02ce1040f331 507 if (currY - 1 >= 0 && !isWallInBack(currX, currY)) {
sahilmgandhi 26:d20f1adac2d3 508 if (manhattanDistances[MAZE_LEN - 1 - (currY - 1)][currX] < minDist) {
sahilmgandhi 26:d20f1adac2d3 509 minDist = manhattanDistances[MAZE_LEN - 1 - (currY - 1)][currX];
sahilmgandhi 26:d20f1adac2d3 510 }
sahilmgandhi 26:d20f1adac2d3 511 }
sahilmgandhi 26:d20f1adac2d3 512 } else {
sahilmgandhi 26:d20f1adac2d3 513 if (currX + 1 < MAZE_LEN) {
sahilmgandhi 26:d20f1adac2d3 514 if (manhattanDistances[MAZE_LEN - 1 - currY][currX + 1] < minDist) {
sahilmgandhi 26:d20f1adac2d3 515 minDist = manhattanDistances[MAZE_LEN - 1 - currY][currX + 1];
sahilmgandhi 26:d20f1adac2d3 516 }
sahilmgandhi 26:d20f1adac2d3 517 }
sahilmgandhi 26:d20f1adac2d3 518 if (currX - 1 >= 0) {
sahilmgandhi 26:d20f1adac2d3 519 if (manhattanDistances[MAZE_LEN - 1 - currY][currX - 1] < minDist) {
sahilmgandhi 26:d20f1adac2d3 520 minDist = manhattanDistances[MAZE_LEN - 1 - currY][currX - 1];
sahilmgandhi 26:d20f1adac2d3 521 }
sahilmgandhi 26:d20f1adac2d3 522 }
sahilmgandhi 26:d20f1adac2d3 523 if (currY + 1 < MAZE_LEN) {
sahilmgandhi 26:d20f1adac2d3 524 if (manhattanDistances[MAZE_LEN - 1 - (currY + 1)][currX] < minDist) {
sahilmgandhi 26:d20f1adac2d3 525 minDist = manhattanDistances[MAZE_LEN - 1 - (currY + 1)][currX];
sahilmgandhi 26:d20f1adac2d3 526 }
sahilmgandhi 26:d20f1adac2d3 527 }
sahilmgandhi 26:d20f1adac2d3 528 if (currY - 1 >= 0) {
sahilmgandhi 26:d20f1adac2d3 529 if (manhattanDistances[MAZE_LEN - 1 - (currY - 1)][currX] < minDist) {
sahilmgandhi 26:d20f1adac2d3 530 minDist = manhattanDistances[MAZE_LEN - 1 - (currY - 1)][currX];
sahilmgandhi 26:d20f1adac2d3 531 }
sahilmgandhi 26:d20f1adac2d3 532 }
sahilmgandhi 26:d20f1adac2d3 533 }
sahilmgandhi 26:d20f1adac2d3 534
sahilmgandhi 26:d20f1adac2d3 535 if (minDist == INT_MAX)
sahilmgandhi 26:d20f1adac2d3 536 continue;
sahilmgandhi 26:d20f1adac2d3 537 if (currDist > minDist)
sahilmgandhi 26:d20f1adac2d3 538 continue;
sahilmgandhi 26:d20f1adac2d3 539 if (currDist <= minDist) {
sahilmgandhi 26:d20f1adac2d3 540 manhattanDistances[MAZE_LEN - 1 - currY][currX] = minDist + 1;
sahilmgandhi 26:d20f1adac2d3 541 }
sahilmgandhi 26:d20f1adac2d3 542 if (hasVisited(currX, currY)) {
sahilmgandhi 26:d20f1adac2d3 543 if (currY + 1 < MAZE_LEN && !isWallInFront(currX, currY)) {
sahilmgandhi 26:d20f1adac2d3 544 cellsToVisit.push(make_pair(currX, currY + 1));
sahilmgandhi 26:d20f1adac2d3 545 }
sahilmgandhi 26:d20f1adac2d3 546 if (currX + 1 < MAZE_LEN && !isWallOnRight(currX, currY)) {
sahilmgandhi 26:d20f1adac2d3 547 cellsToVisit.push(make_pair(currX + 1, currY));
sahilmgandhi 26:d20f1adac2d3 548 }
sahilmgandhi 26:d20f1adac2d3 549 if (currY - 1 >= 0 && !isWallInBack(currX, currY)) {
sahilmgandhi 26:d20f1adac2d3 550 cellsToVisit.push(make_pair(currX, currY - 1));
sahilmgandhi 26:d20f1adac2d3 551 }
sahilmgandhi 26:d20f1adac2d3 552 if (currX - 1 >= 0 && !isWallOnLeft( currX, currY)) {
sahilmgandhi 26:d20f1adac2d3 553 cellsToVisit.push(make_pair(currX - 1, currY));
sahilmgandhi 26:d20f1adac2d3 554 }
sahilmgandhi 26:d20f1adac2d3 555 } else {
sahilmgandhi 26:d20f1adac2d3 556 if (currY + 1 < MAZE_LEN) {
sahilmgandhi 26:d20f1adac2d3 557 cellsToVisit.push(make_pair(currX, currY + 1));
sahilmgandhi 26:d20f1adac2d3 558 }
sahilmgandhi 26:d20f1adac2d3 559 if (currX + 1 < MAZE_LEN) {
sahilmgandhi 26:d20f1adac2d3 560 cellsToVisit.push(make_pair(currX + 1, currY));
sahilmgandhi 26:d20f1adac2d3 561 }
sahilmgandhi 26:d20f1adac2d3 562 if (currY - 1 >= 0) {
sahilmgandhi 26:d20f1adac2d3 563 cellsToVisit.push(make_pair(currX, currY - 1));
sahilmgandhi 26:d20f1adac2d3 564 }
sahilmgandhi 26:d20f1adac2d3 565 if (currX - 1 >= 0) {
sahilmgandhi 26:d20f1adac2d3 566 cellsToVisit.push(make_pair(currX - 1, currY));
sahilmgandhi 26:d20f1adac2d3 567 }
sahilmgandhi 26:d20f1adac2d3 568 }
sahilmgandhi 26:d20f1adac2d3 569 }
sahilmgandhi 26:d20f1adac2d3 570
sahilmgandhi 26:d20f1adac2d3 571 int minDistance = INT_MAX;
sahilmgandhi 26:d20f1adac2d3 572 if (currDir % 4 == 0) {
sahilmgandhi 26:d20f1adac2d3 573 if (mouseX + 1 < MAZE_LEN && !isWallOnRight(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 574 if (manhattanDistances[MAZE_LEN - 1 - mouseY][mouseX + 1] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 575 minDistance = manhattanDistances[MAZE_LEN - 1 - mouseY][mouseX + 1];
sahilmgandhi 26:d20f1adac2d3 576 dirToGo = 1;
sahilmgandhi 26:d20f1adac2d3 577 }
sahilmgandhi 26:d20f1adac2d3 578 }
sahilmgandhi 26:d20f1adac2d3 579 if (mouseX - 1 >= 0 && !isWallOnLeft(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 580 if (manhattanDistances[MAZE_LEN - 1 - mouseY][mouseX - 1] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 581 minDistance = manhattanDistances[MAZE_LEN - 1 - mouseY][mouseX - 1];
sahilmgandhi 26:d20f1adac2d3 582 dirToGo = 2;
sahilmgandhi 26:d20f1adac2d3 583 }
sahilmgandhi 26:d20f1adac2d3 584 }
sahilmgandhi 26:d20f1adac2d3 585 if (mouseY + 1 < MAZE_LEN && !isWallInFront(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 586 if (manhattanDistances[MAZE_LEN - 1 - (mouseY + 1)][mouseX] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 587 minDistance = manhattanDistances[MAZE_LEN - 1 - (mouseY + 1)][mouseX];
sahilmgandhi 26:d20f1adac2d3 588 dirToGo = 3;
sahilmgandhi 26:d20f1adac2d3 589 }
sahilmgandhi 26:d20f1adac2d3 590 }
sahilmgandhi 26:d20f1adac2d3 591 if (mouseY - 1 >= 0 && !isWallInBack(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 592 if (manhattanDistances[MAZE_LEN - 1 - (mouseY - 1)][mouseX] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 593 minDistance = manhattanDistances[MAZE_LEN - 1 - (mouseY - 1)][mouseX];
sahilmgandhi 26:d20f1adac2d3 594 dirToGo = 4;
sahilmgandhi 26:d20f1adac2d3 595 }
sahilmgandhi 26:d20f1adac2d3 596 }
sahilmgandhi 26:d20f1adac2d3 597 } else if (currDir % 4 == 2) {
sahilmgandhi 26:d20f1adac2d3 598 if (mouseX - 1 >= 0 && !isWallOnRight(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 599 if (manhattanDistances[MAZE_LEN - 1 - mouseY][mouseX - 1] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 600 minDistance = manhattanDistances[MAZE_LEN - 1 - mouseY][mouseX - 1];
sahilmgandhi 26:d20f1adac2d3 601 dirToGo = 1;
sahilmgandhi 26:d20f1adac2d3 602 }
sahilmgandhi 26:d20f1adac2d3 603 }
sahilmgandhi 26:d20f1adac2d3 604 if (mouseX + 1 < MAZE_LEN && !isWallOnLeft(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 605 if (manhattanDistances[MAZE_LEN - 1 - mouseY][mouseX + 1] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 606 minDistance = manhattanDistances[MAZE_LEN - 1 - mouseY][mouseX + 1];
sahilmgandhi 26:d20f1adac2d3 607 dirToGo = 2;
sahilmgandhi 26:d20f1adac2d3 608 }
sahilmgandhi 26:d20f1adac2d3 609 }
sahilmgandhi 26:d20f1adac2d3 610 if (mouseY - 1 >= 0 && !isWallInFront(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 611 if (manhattanDistances[MAZE_LEN - 1 - (mouseY - 1)][mouseX] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 612 minDistance = manhattanDistances[MAZE_LEN - 1 - (mouseY - 1)][mouseX];
sahilmgandhi 26:d20f1adac2d3 613 dirToGo = 3;
sahilmgandhi 26:d20f1adac2d3 614 }
sahilmgandhi 26:d20f1adac2d3 615 }
sahilmgandhi 26:d20f1adac2d3 616 if (mouseY + 1 < MAZE_LEN && !isWallInBack(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 617 if (manhattanDistances[MAZE_LEN - 1 - (mouseY + 1)][mouseX] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 618 minDistance = manhattanDistances[MAZE_LEN - 1 - (mouseY + 1)][mouseX];
sahilmgandhi 26:d20f1adac2d3 619 dirToGo = 4;
sahilmgandhi 26:d20f1adac2d3 620 }
sahilmgandhi 26:d20f1adac2d3 621 }
sahilmgandhi 26:d20f1adac2d3 622 } else if (currDir % 4 == 1) {
sahilmgandhi 26:d20f1adac2d3 623 if (mouseY - 1 >= 0 && !isWallOnRight(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 624 if (manhattanDistances[MAZE_LEN - 1 - (mouseY - 1)][mouseX] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 625 minDistance = manhattanDistances[MAZE_LEN - 1 - (mouseY - 1)][mouseX];
sahilmgandhi 26:d20f1adac2d3 626 dirToGo = 1;
sahilmgandhi 26:d20f1adac2d3 627 }
sahilmgandhi 26:d20f1adac2d3 628 }
sahilmgandhi 26:d20f1adac2d3 629 if (mouseY + 1 < MAZE_LEN && !isWallOnLeft(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 630 if (manhattanDistances[MAZE_LEN - 1 - (mouseY + 1)][mouseX] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 631 minDistance = manhattanDistances[MAZE_LEN - 1 - (mouseY + 1)][mouseX];
sahilmgandhi 26:d20f1adac2d3 632 dirToGo = 2;
sahilmgandhi 26:d20f1adac2d3 633 }
sahilmgandhi 26:d20f1adac2d3 634 }
sahilmgandhi 26:d20f1adac2d3 635 if (mouseX + 1 < MAZE_LEN && !isWallInFront(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 636 if (manhattanDistances[MAZE_LEN - 1 - mouseY][mouseX + 1] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 637 minDistance = manhattanDistances[MAZE_LEN - 1 - (mouseY)][mouseX + 1];
sahilmgandhi 26:d20f1adac2d3 638 dirToGo = 3;
sahilmgandhi 26:d20f1adac2d3 639 }
sahilmgandhi 26:d20f1adac2d3 640 }
sahilmgandhi 26:d20f1adac2d3 641 if (mouseX - 1 >= 0 && !isWallInBack(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 642 if (manhattanDistances[MAZE_LEN - 1 - (mouseY)][mouseX - 1] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 643 minDistance = manhattanDistances[MAZE_LEN - 1 - (mouseY)][mouseX - 1];
sahilmgandhi 26:d20f1adac2d3 644 dirToGo = 4;
sahilmgandhi 26:d20f1adac2d3 645 }
sahilmgandhi 26:d20f1adac2d3 646 }
sahilmgandhi 26:d20f1adac2d3 647 } else if (currDir % 4 == 3) {
sahilmgandhi 26:d20f1adac2d3 648 if (mouseY + 1 < MAZE_LEN && !isWallOnRight(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 649 if (manhattanDistances[MAZE_LEN - 1 - (mouseY + 1)][mouseX] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 650 minDistance = manhattanDistances[MAZE_LEN - 1 - (mouseY + 1)][mouseX];
sahilmgandhi 26:d20f1adac2d3 651 dirToGo = 1;
sahilmgandhi 26:d20f1adac2d3 652 }
sahilmgandhi 26:d20f1adac2d3 653 }
sahilmgandhi 26:d20f1adac2d3 654 if (mouseY - 1 >= 0 && !isWallOnLeft(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 655 if (manhattanDistances[MAZE_LEN - 1 - (mouseY - 1)][mouseX] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 656 minDistance = manhattanDistances[MAZE_LEN - 1 - (mouseY - 1)][mouseX];
sahilmgandhi 26:d20f1adac2d3 657 dirToGo = 2;
sahilmgandhi 26:d20f1adac2d3 658 }
sahilmgandhi 26:d20f1adac2d3 659 }
sahilmgandhi 26:d20f1adac2d3 660 if (mouseX - 1 >= 0 && !isWallInFront(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 661 if (manhattanDistances[MAZE_LEN - 1 - mouseY][mouseX - 1] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 662 minDistance = manhattanDistances[MAZE_LEN - 1 - (mouseY)][mouseX - 1];
sahilmgandhi 26:d20f1adac2d3 663 dirToGo = 3;
sahilmgandhi 26:d20f1adac2d3 664 }
sahilmgandhi 26:d20f1adac2d3 665 }
sahilmgandhi 26:d20f1adac2d3 666 if (mouseX + 1 < MAZE_LEN && !isWallInBack(mouseX, mouseY)) {
sahilmgandhi 26:d20f1adac2d3 667 if (manhattanDistances[MAZE_LEN - 1 - (mouseY)][mouseX + 1] <= minDistance) {
sahilmgandhi 26:d20f1adac2d3 668 minDistance = manhattanDistances[MAZE_LEN - 1 - (mouseY)][mouseX + 1];
sahilmgandhi 26:d20f1adac2d3 669 dirToGo = 4;
sahilmgandhi 26:d20f1adac2d3 670 }
sahilmgandhi 26:d20f1adac2d3 671 }
sahilmgandhi 26:d20f1adac2d3 672 }
sahilmgandhi 31:9b71b44e0867 673 } else {
sahilmgandhi 26:d20f1adac2d3 674 justTurned = false;
sahilmgandhi 26:d20f1adac2d3 675 dirToGo = 0;
sahilmgandhi 26:d20f1adac2d3 676 }
sahilmgandhi 26:d20f1adac2d3 677
sahilmgandhi 26:d20f1adac2d3 678 return dirToGo;
sahilmgandhi 26:d20f1adac2d3 679 }
sahilmgandhi 26:d20f1adac2d3 680
sahilmgandhi 34:69342782fb68 681 bool hasVisited(int x, int y)
sahilmgandhi 34:69342782fb68 682 {
sahilmgandhi 26:d20f1adac2d3 683 return visitedCells[MAZE_LEN - 1 - y][x];
sahilmgandhi 26:d20f1adac2d3 684 }
sahilmgandhi 26:d20f1adac2d3 685
sahilmgandhi 31:9b71b44e0867 686 void changeManhattanDistance(bool headCenter)
sahilmgandhi 31:9b71b44e0867 687 {
sahilmgandhi 31:9b71b44e0867 688 if (headCenter) {
sahilmgandhi 29:ec2c5a69acd6 689 for (int i = 0; i < MAZE_LEN; i++) {
sahilmgandhi 29:ec2c5a69acd6 690 for (int j = 0; j < MAZE_LEN; j++) {
sahilmgandhi 29:ec2c5a69acd6 691 distanceToCenter[i][j] = manhattanDistances[i][j];
sahilmgandhi 26:d20f1adac2d3 692 }
sahilmgandhi 26:d20f1adac2d3 693 }
sahilmgandhi 29:ec2c5a69acd6 694 for (int i = 0; i < MAZE_LEN; i++) {
sahilmgandhi 29:ec2c5a69acd6 695 for (int j = 0; j < MAZE_LEN; j++) {
sahilmgandhi 29:ec2c5a69acd6 696 manhattanDistances[i][j] = distanceToStart[i][j];
sahilmgandhi 26:d20f1adac2d3 697 }
sahilmgandhi 26:d20f1adac2d3 698 }
sahilmgandhi 31:9b71b44e0867 699 } else {
sahilmgandhi 31:9b71b44e0867 700 for (int i = 0; i < MAZE_LEN; i++) {
sahilmgandhi 29:ec2c5a69acd6 701 for (int j = 0; j < MAZE_LEN; j++) {
sahilmgandhi 29:ec2c5a69acd6 702 distanceToStart[i][j] = manhattanDistances[i][j];
sahilmgandhi 29:ec2c5a69acd6 703 }
sahilmgandhi 29:ec2c5a69acd6 704 }
sahilmgandhi 29:ec2c5a69acd6 705 for (int i = 0; i < MAZE_LEN; i++) {
sahilmgandhi 29:ec2c5a69acd6 706 for (int j = 0; j < MAZE_LEN; j++) {
sahilmgandhi 29:ec2c5a69acd6 707 manhattanDistances[i][j] = distanceToCenter[i][j];
sahilmgandhi 29:ec2c5a69acd6 708 }
sahilmgandhi 29:ec2c5a69acd6 709 }
sahilmgandhi 29:ec2c5a69acd6 710 }
sahilmgandhi 29:ec2c5a69acd6 711 }
sahilmgandhi 29:ec2c5a69acd6 712
sahilmgandhi 31:9b71b44e0867 713 void initializeDistances()
sahilmgandhi 31:9b71b44e0867 714 {
sahilmgandhi 29:ec2c5a69acd6 715 for (int i = 0; i < MAZE_LEN / 2; i++) {
sahilmgandhi 31:9b71b44e0867 716 for (int j = 0; j < MAZE_LEN / 2; j++) {
sahilmgandhi 31:9b71b44e0867 717 distanceToStart[MAZE_LEN - 1 - j][i] = abs(0 - j) + abs(0 - i);
sahilmgandhi 26:d20f1adac2d3 718 }
sahilmgandhi 31:9b71b44e0867 719 }
sahilmgandhi 31:9b71b44e0867 720 for (int i = MAZE_LEN / 2; i < MAZE_LEN; i++) {
sahilmgandhi 31:9b71b44e0867 721 for (int j = 0; j < MAZE_LEN / 2; j++) {
sahilmgandhi 31:9b71b44e0867 722 distanceToStart[MAZE_LEN - 1 - j][i] = abs(0 - j) + abs(0 - i);
sahilmgandhi 26:d20f1adac2d3 723 }
sahilmgandhi 31:9b71b44e0867 724 }
sahilmgandhi 31:9b71b44e0867 725 for (int i = 0; i < MAZE_LEN / 2; i++) {
sahilmgandhi 31:9b71b44e0867 726 for (int j = MAZE_LEN / 2; j < MAZE_LEN; j++) {
sahilmgandhi 31:9b71b44e0867 727 distanceToStart[MAZE_LEN - 1 - j][i] = abs(0 - j) + abs(0 - i);
sahilmgandhi 26:d20f1adac2d3 728 }
sahilmgandhi 31:9b71b44e0867 729 }
sahilmgandhi 31:9b71b44e0867 730 for (int i = MAZE_LEN / 2; i < MAZE_LEN; i++) {
sahilmgandhi 31:9b71b44e0867 731 for (int j = MAZE_LEN / 2; j < MAZE_LEN; j++) {
sahilmgandhi 31:9b71b44e0867 732 distanceToStart[MAZE_LEN - 1 - j][i] = abs(0 - j) + abs(0 - i);
sahilmgandhi 26:d20f1adac2d3 733 }
sahilmgandhi 31:9b71b44e0867 734 }
sahilmgandhi 26:d20f1adac2d3 735 }
sahilmgandhi 31:9b71b44e0867 736
kyleliangus 36:9c4cc9944b69 737 void waitButton4()
kyleliangus 36:9c4cc9944b69 738 {
kyleliangus 36:9c4cc9944b69 739 //serial.printf("%d, %d, %d\n", dipFlags, BUTTON1_FLAG, dipFlags & BUTTON1_FLAG);
kyleliangus 36:9c4cc9944b69 740 int init_val = dipFlags & BUTTON4_FLAG;
kyleliangus 36:9c4cc9944b69 741 while( (dipFlags & BUTTON4_FLAG) == init_val )
kyleliangus 36:9c4cc9944b69 742 {
kyleliangus 36:9c4cc9944b69 743 // do nothing until ready
kyleliangus 36:9c4cc9944b69 744 }
kyleliangus 36:9c4cc9944b69 745 //serial.printf("%d, %d, %d\n", dipFlags, BUTTON1_FLAG, dipFlags & BUTTON1_FLAG);
kyleliangus 36:9c4cc9944b69 746 wait( 2 );
kyleliangus 36:9c4cc9944b69 747 }
kyleliangus 36:9c4cc9944b69 748
sahilmgandhi 0:a03c771ab78e 749 int main()
sahilmgandhi 0:a03c771ab78e 750 {
christine222 3:880f15be8c72 751 //Set highest bandwidth.
christine222 23:690b0ca34ee9 752 //gyro.setLpBandwidth(LPFBW_42HZ);
sahilmgandhi 29:ec2c5a69acd6 753 initializeDistances();
christine222 3:880f15be8c72 754 serial.baud(9600);
christine222 23:690b0ca34ee9 755 //serial.printf("The gyro's address is %s", gyro.getWhoAmI());
sahilmgandhi 31:9b71b44e0867 756
sahilmgandhi 1:8a4b2f573923 757 wait (0.1);
sahilmgandhi 31:9b71b44e0867 758
sahilmgandhi 2:771db996cee0 759 redLed.write(1);
sahilmgandhi 14:9e7bb03ddccb 760 greenLed.write(0);
sahilmgandhi 2:771db996cee0 761 blueLed.write(1);
sahilmgandhi 31:9b71b44e0867 762
kyleliangus 9:1d8e4da058cd 763 //left_motor.forward(0.1);
kyleliangus 9:1d8e4da058cd 764 //right_motor.forward(0.1);
sahilmgandhi 31:9b71b44e0867 765
kyleliangus 8:a0760acdc59e 766 // PA_1 is A of right
kyleliangus 8:a0760acdc59e 767 // PA_0 is B of right
kyleliangus 8:a0760acdc59e 768 // PA_5 is A of left
kyleliangus 8:a0760acdc59e 769 // PB_3 is B of left
vanshg 11:8fc2b703086b 770 //QEI encoder0( PA_5, PB_3, NC, PULSES, QEI::X4_ENCODING );
sahilmgandhi 26:d20f1adac2d3 771 // QEI encoder1( PA_1, PA_0, NC, PULSES, QEI::X4_ENCODING );
sahilmgandhi 31:9b71b44e0867 772
vanshg 10:810d1849da9d 773 // TODO: Setting all the registers and what not for Quadrature Encoders
sahilmgandhi 14:9e7bb03ddccb 774 /* RCC->APB1ENR |= 0x1001; // Enable clock for Tim2 (Bit 0) and Tim5 (Bit 3)
sahilmgandhi 14:9e7bb03ddccb 775 RCC->AHB1ENR |= 0x11; // Enable GPIO port clock enables for Tim2(A) and Tim5(B)
sahilmgandhi 14:9e7bb03ddccb 776 GPIOA->AFR[0] |= 0x10; // Set GPIO alternate function modes for Tim2
sahilmgandhi 14:9e7bb03ddccb 777 GPIOB->AFR[0] |= 0x100; // Set GPIO alternate function modes for Tim5
sahilmgandhi 14:9e7bb03ddccb 778 */
sahilmgandhi 31:9b71b44e0867 779
kyleliangus 12:5790e56a056f 780 // set GPIO pins to alternate for the pins corresponding to A/B for eacah encoder, and 2 alternate function registers need to be selected for each type
kyleliangus 12:5790e56a056f 781 // of alternate function specified
kyleliangus 12:5790e56a056f 782 // 4 modes sets AHB1ENR
kyleliangus 12:5790e56a056f 783 // Now TMR: enable clock with timer, APB1ENR
kyleliangus 12:5790e56a056f 784 // set period, autoreload value, ARR value 2^32-1, CR1 - TMR resets itself, ARPE and EN
kyleliangus 12:5790e56a056f 785 //
kyleliangus 12:5790e56a056f 786 // Encoder mode: disable timer before changing timer to encoder
kyleliangus 12:5790e56a056f 787 // CCMR1/2 1/2 depends on channel 1/2 or 3/4, depends on upper bits, depending which channels you use
kyleliangus 12:5790e56a056f 788 // CCMR sets sample rate and set the channel to input
kyleliangus 12:5790e56a056f 789 // CCER, which edge to trigger on, cannot be 11(not allowed for encoder mode), CCER for both channels
kyleliangus 12:5790e56a056f 790 // SMCR - encoder mode
kyleliangus 12:5790e56a056f 791 // CR1 reenabales
kyleliangus 12:5790e56a056f 792 // then read CNT reg of timer
sahilmgandhi 31:9b71b44e0867 793
vanshg 10:810d1849da9d 794 dipButton1.rise(&enableButton1);
vanshg 10:810d1849da9d 795 dipButton2.rise(&enableButton2);
vanshg 10:810d1849da9d 796 dipButton3.rise(&enableButton3);
vanshg 10:810d1849da9d 797 dipButton4.rise(&enableButton4);
sahilmgandhi 31:9b71b44e0867 798
vanshg 10:810d1849da9d 799 dipButton1.fall(&disableButton1);
vanshg 10:810d1849da9d 800 dipButton2.fall(&disableButton2);
vanshg 10:810d1849da9d 801 dipButton3.fall(&disableButton3);
vanshg 10:810d1849da9d 802 dipButton4.fall(&disableButton4);
sahilmgandhi 7:6f5cb6377bd4 803
kyleliangus 36:9c4cc9944b69 804 //waitButton4();
kyleliangus 35:a5bd9ef82210 805
sahilmgandhi 26:d20f1adac2d3 806 // init the wall, and mouse loc arrays:
sahilmgandhi 26:d20f1adac2d3 807 wallArray[MAZE_LEN - 1 - mouseY][mouseX] = 0xE;
sahilmgandhi 26:d20f1adac2d3 808 visitedCells[MAZE_LEN - 1 - mouseY][mouseX] = 1;
sahilmgandhi 26:d20f1adac2d3 809
kyleliangus 32:69acb14778ea 810 //int prevEncoder0Count = 0;
kyleliangus 32:69acb14778ea 811 //int prevEncoder1Count = 0;
kyleliangus 32:69acb14778ea 812 //int currEncoder0Count = 0;
kyleliangus 32:69acb14778ea 813 //int currEncoder1Count = 0;
sahilmgandhi 28:8126a4d620e8 814
kyleliangus 32:69acb14778ea 815 //bool overrideFloodFill = false;
sahilmgandhi 37:3dcc95e9321c 816 right_motor.forward( WHEEL_SPEED );
sahilmgandhi 37:3dcc95e9321c 817 left_motor.forward( WHEEL_SPEED );
kyleliangus 36:9c4cc9944b69 818 //turn180();
sahilmgandhi 26:d20f1adac2d3 819 //wait_ms(1500);
kyleliangus 32:69acb14778ea 820 //int nextMovement = 0;
sahilmgandhi 26:d20f1adac2d3 821 while (1) {
sahilmgandhi 31:9b71b44e0867 822 // prevEncoder0Count = encoder0.getPulses();
sahilmgandhi 29:ec2c5a69acd6 823 // prevEncoder1Count = encoder1.getPulses();
sahilmgandhi 29:ec2c5a69acd6 824 //
sahilmgandhi 29:ec2c5a69acd6 825 // if (!overrideFloodFill){
sahilmgandhi 29:ec2c5a69acd6 826 // nextMovement = chooseNextMovement();
sahilmgandhi 29:ec2c5a69acd6 827 // if (nextMovement == 0){
sahilmgandhi 29:ec2c5a69acd6 828 // nCellEncoderAndIR(1);
sahilmgandhi 29:ec2c5a69acd6 829 // }
sahilmgandhi 29:ec2c5a69acd6 830 // else if (nextMovement == 1){
sahilmgandhi 29:ec2c5a69acd6 831 // justTurned = true;
sahilmgandhi 29:ec2c5a69acd6 832 // turnRight();
sahilmgandhi 29:ec2c5a69acd6 833 // }
sahilmgandhi 29:ec2c5a69acd6 834 // else if (nextMovement == 2){
sahilmgandhi 29:ec2c5a69acd6 835 // justTurned = true;
sahilmgandhi 29:ec2c5a69acd6 836 // turnLeft();
sahilmgandhi 29:ec2c5a69acd6 837 // }
sahilmgandhi 29:ec2c5a69acd6 838 // else if (nextMovement == 3){
sahilmgandhi 29:ec2c5a69acd6 839 // nCellEncoderAndIR(1);
sahilmgandhi 29:ec2c5a69acd6 840 // }
sahilmgandhi 29:ec2c5a69acd6 841 // else if (nextMovement == 4){
sahilmgandhi 29:ec2c5a69acd6 842 // justTurned = true;
kyleliangus 36:9c4cc9944b69 843 // turn180();
sahilmgandhi 29:ec2c5a69acd6 844 // }
sahilmgandhi 29:ec2c5a69acd6 845 // }
sahilmgandhi 29:ec2c5a69acd6 846 // else{
sahilmgandhi 29:ec2c5a69acd6 847 // overrideFloodFill = false;
sahilmgandhi 29:ec2c5a69acd6 848 // if ((rand()%2 + 1) == 1){
sahilmgandhi 29:ec2c5a69acd6 849 // justTurned = true;
sahilmgandhi 29:ec2c5a69acd6 850 // turnLeft();
sahilmgandhi 29:ec2c5a69acd6 851 // }
sahilmgandhi 29:ec2c5a69acd6 852 // else{
sahilmgandhi 29:ec2c5a69acd6 853 // justTurned = true;
sahilmgandhi 29:ec2c5a69acd6 854 // turnRight();
sahilmgandhi 29:ec2c5a69acd6 855 // }
sahilmgandhi 29:ec2c5a69acd6 856 // }
sahilmgandhi 29:ec2c5a69acd6 857 // currEncoder0Count = encoder0.getPulses();
sahilmgandhi 29:ec2c5a69acd6 858 // currEncoder1Count = encoder1.getPulses();
sahilmgandhi 31:9b71b44e0867 859 //
sahilmgandhi 29:ec2c5a69acd6 860 // if (!justTurned && (currEncoder0Count <= prevEncoder0Count + 100) && (currEncoder1Count <= prevEncoder1Count + 100) && !overrideFloodFill){
sahilmgandhi 29:ec2c5a69acd6 861 // overrideFloodFill = true;
sahilmgandhi 29:ec2c5a69acd6 862 // }
sahilmgandhi 29:ec2c5a69acd6 863 //
sahilmgandhi 29:ec2c5a69acd6 864 // wait_ms(300); // reduce this once we fine tune this!
christine222 24:e7063765d6f0 865
sahilmgandhi 31:9b71b44e0867 866
sahilmgandhi 34:69342782fb68 867 //////////////////////// TESTING CODE GOES BELOW ///////////////////////////
kyleliangus 36:9c4cc9944b69 868
sahilmgandhi 37:3dcc95e9321c 869 nCellEncoderAndIR(4);
kyleliangus 36:9c4cc9944b69 870 wait_ms(300);
sahilmgandhi 37:3dcc95e9321c 871 turnRight();
sahilmgandhi 37:3dcc95e9321c 872 nCellEncoderAndIR(1);
kyleliangus 36:9c4cc9944b69 873 wait_ms(300);
sahilmgandhi 37:3dcc95e9321c 874 turnRight();
sahilmgandhi 37:3dcc95e9321c 875 nCellEncoderAndIR(4);
sahilmgandhi 38:fe05f93009a2 876 wait_ms(300);
sahilmgandhi 38:fe05f93009a2 877 turnLeft();
sahilmgandhi 38:fe05f93009a2 878 wait_ms(300);
sahilmgandhi 38:fe05f93009a2 879 nCellEncoderAndIR(1);
sahilmgandhi 38:fe05f93009a2 880 wait_ms(300);
sahilmgandhi 38:fe05f93009a2 881 turnLeft();
sahilmgandhi 38:fe05f93009a2 882 wait_ms(300);
sahilmgandhi 38:fe05f93009a2 883 nCellEncoderAndIR(3);
sahilmgandhi 38:fe05f93009a2 884 wait_ms(300);
sahilmgandhi 38:fe05f93009a2 885 turnRight();
sahilmgandhi 38:fe05f93009a2 886 wait_ms(300);
sahilmgandhi 38:fe05f93009a2 887 nCellEncoderAndIR(3);
sahilmgandhi 38:fe05f93009a2 888 wait_ms(300);
sahilmgandhi 38:fe05f93009a2 889 turnRight();
sahilmgandhi 38:fe05f93009a2 890 wait_ms(300);
sahilmgandhi 38:fe05f93009a2 891 nCellEncoderAndIR(3);
kyleliangus 36:9c4cc9944b69 892 waitButton4();
sahilmgandhi 33:68ce1f74ab5f 893 // serial.printf("Encoder 0 is : %d \t Encoder 1 is %d\n",encoder0.getPulses(), encoder1.getPulses() );
sahilmgandhi 33:68ce1f74ab5f 894 // double currentError = ( (IRP_2.getSamples( SAMPLE_NUM ) - IRP_2.sensorAvg) ) - ( (IRP_3.getSamples( SAMPLE_NUM ) - IRP_3.sensorAvg) ) ;
sahilmgandhi 33:68ce1f74ab5f 895 // serial.printf("IRS= >: %f, %f, %f, %f, %f \r\n", IRP_1.getSamples( 100 ), IRP_2.getSamples( 100 ), IRP_3.getSamples( 100 ), IRP_4.getSamples(100), currentError );
christine222 3:880f15be8c72 896 }
sahilmgandhi 26:d20f1adac2d3 897 }