
Team Design project 3 main file
Dependencies: LocalPositionSystem MMA8451Q Motor_Driver Sensors mbed
Fork of TDP_main by
main.cpp@29:307b45a52401, 2015-03-23 (annotated)
- Committer:
- Reckstyle
- Date:
- Mon Mar 23 16:42:27 2015 +0000
- Revision:
- 29:307b45a52401
- Parent:
- 28:d1e7daeb240e
- Child:
- 31:fb355508481f
Added sensor values;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Reckstyle | 2:e2adb7ab2947 | 1 | /* |
Reckstyle | 2:e2adb7ab2947 | 2 | ****** MAIN PROGRAM ****** |
Reckstyle | 2:e2adb7ab2947 | 3 | |
Reckstyle | 2:e2adb7ab2947 | 4 | Please consider that although it is an embedded envrionment we are NOT creating a HARD-TIME real time system - delays can be dealt with |
Reckstyle | 11:9e56d52485d1 | 5 | |
Reckstyle | 11:9e56d52485d1 | 6 | Sensors are mapped on the global variable sensorsCheckSum, |
Reckstyle | 11:9e56d52485d1 | 7 | which multiplies the sensor number by itself to then decode, |
Reckstyle | 11:9e56d52485d1 | 8 | which sensors are off and which are on |
Reckstyle | 11:9e56d52485d1 | 9 | ie. if sensor rightright - sensorChecksum = 1*1 = 1 |
Reckstyle | 11:9e56d52485d1 | 10 | if rightright and rightcentre - sensorChecksum = 1*1 + 2*2 = 5 |
Reckstyle | 11:9e56d52485d1 | 11 | ... |
Reckstyle | 2:e2adb7ab2947 | 12 | */ |
Reckstyle | 2:e2adb7ab2947 | 13 | |
Reckstyle | 0:5ca0450111f3 | 14 | #include "mbed.h" |
Reckstyle | 11:9e56d52485d1 | 15 | #include "sensor_measure.h" |
Joseph_Penikis | 5:2fc7bf0135d4 | 16 | #include "Motor_Driver.h" |
orsharp | 12:bb21b76b6375 | 17 | #include "shooter.h" |
Reckstyle | 0:5ca0450111f3 | 18 | |
Joseph_Penikis | 5:2fc7bf0135d4 | 19 | #define PWM_PERIOD_US 10000 |
Reckstyle | 0:5ca0450111f3 | 20 | |
Reckstyle | 28:d1e7daeb240e | 21 | DigitalOut led(LED_RED); |
Bartas | 24:ecc3fbaf2824 | 22 | Serial HC06(PTE0,PTE1); |
Reckstyle | 9:718987b106a8 | 23 | |
Reckstyle | 18:d277614084bc | 24 | Timer measureTimer; //Timer used for measurement |
Reckstyle | 29:307b45a52401 | 25 | Motor_Driver motors(PTA5, PTC9, PTC8,PTD4, PTA12, PTA4, PWM_PERIOD_US); //motors object |
Reckstyle | 9:718987b106a8 | 26 | |
Reckstyle | 11:9e56d52485d1 | 27 | typedef enum mode {REGULAR,SQUARE} mode; //enumeration for different states |
Reckstyle | 11:9e56d52485d1 | 28 | mode driveMode; //declaring the variable for the states |
Reckstyle | 11:9e56d52485d1 | 29 | int sensorsCheckSum; //varibale used for sensors mapping access |
Reckstyle | 18:d277614084bc | 30 | int passedTime1,passedTime2; |
Reckstyle | 25:bec5dc4c9f5e | 31 | int oldValues[5] = {0}; |
Bartas | 27:fc0fd2c0eebb | 32 | int cursor = 0, k = 0; |
Reckstyle | 11:9e56d52485d1 | 33 | |
Reckstyle | 11:9e56d52485d1 | 34 | |
Reckstyle | 11:9e56d52485d1 | 35 | void measureSensors () { |
Bartas | 24:ecc3fbaf2824 | 36 | sensorsCheckSum = 0; //zero it when first going into the routine |
Bartas | 24:ecc3fbaf2824 | 37 | int iterationNumber = NUMBER_SENSORS_REGULAR; |
Bartas | 24:ecc3fbaf2824 | 38 | if (driveMode == SQUARE) { |
orsharp | 12:bb21b76b6375 | 39 | iterationNumber += NUMBER_SENSORS_SQUARE; |
Bartas | 24:ecc3fbaf2824 | 40 | } |
Bartas | 24:ecc3fbaf2824 | 41 | for (int i = 0; i < iterationNumber;i++){ |
Bartas | 24:ecc3fbaf2824 | 42 | //pc.printf("%i iteration%i ",i,iterationNumber); |
Bartas | 24:ecc3fbaf2824 | 43 | if (measure(sensorArray[i]) == 1) {//if sensor is white |
Reckstyle | 11:9e56d52485d1 | 44 | sensorsCheckSum += (i+1)*(i+1); |
Reckstyle | 11:9e56d52485d1 | 45 | } |
Bartas | 24:ecc3fbaf2824 | 46 | } |
Reckstyle | 25:bec5dc4c9f5e | 47 | if (oldValues[cursor] != sensorsCheckSum) { //why only if different ?? |
Reckstyle | 25:bec5dc4c9f5e | 48 | oldValues[cursor] = sensorsCheckSum; |
Reckstyle | 23:9b53c72fcd38 | 49 | cursor = (cursor+1)%5; |
Bartas | 24:ecc3fbaf2824 | 50 | } |
Bartas | 24:ecc3fbaf2824 | 51 | //pc.printf("sensorsCheckSum is %i",sensorsCheckSum); |
Reckstyle | 11:9e56d52485d1 | 52 | } |
Reckstyle | 11:9e56d52485d1 | 53 | |
Reckstyle | 15:6453cd351452 | 54 | void printBluetooth() { //for debugging |
Reckstyle | 29:307b45a52401 | 55 | HC06.printf("LLU%i LRU%i rlu%i rru%i\n",sensorArray[7]->state,sensorArray[6]->state,sensorArray[1]->state,sensorArray[0]->state); |
Reckstyle | 29:307b45a52401 | 56 | HC06.printf("LLD%i LRD%i rld%i rrd%i\n\n",sensorArray[5]->state,sensorArray[4]->state,sensorArray[3]->state,sensorArray[2]->state); |
Reckstyle | 16:3649eb1a056d | 57 | //HC06.printf("%i %i %i %i",sensorArray[NUMBER_SENSORS_REGULAR-3]->state,sensorArray[NUMBER_SENSORS_REGULAR-4]->state,sensorArray[3]->state,sensorArray[2]->state); |
Reckstyle | 15:6453cd351452 | 58 | //HC06.printf("%i %i/n%i %i,sensorArray[NUMBER_SENSORS_REGULAR]->state,sensorArray[NUMBER_SENSORS_REGULAR+1]->state,sensorArray[NUMBER_SENSORS_REGULAR+2]->state,sensorArray[NUMBER_SENSORS_REGULAR+3]->state); |
Reckstyle | 15:6453cd351452 | 59 | //HC06.printf("%f %f",motor.getLeftSpeed(),motor.getRightSpeed()); |
Reckstyle | 29:307b45a52401 | 60 | HC06.printf("sensorCheckSum%i\n\n",sensorsCheckSum); |
Reckstyle | 18:d277614084bc | 61 | //HC06.printf("passedTime1 %i passedTime2 \n\n",passedTime1,passedTime2); |
Reckstyle | 15:6453cd351452 | 62 | } |
Reckstyle | 15:6453cd351452 | 63 | |
Reckstyle | 11:9e56d52485d1 | 64 | int main() { |
Reckstyle | 21:9cf274ffe1de | 65 | |
Reckstyle | 28:d1e7daeb240e | 66 | |
Reckstyle | 29:307b45a52401 | 67 | //setup_counter(1000, 0); |
Reckstyle | 22:902c3086394e | 68 | // float frequency = measure_frequency(CHANNEL_1); |
Reckstyle | 29:307b45a52401 | 69 | led = 0;//start LED with beginning of main program |
Reckstyle | 29:307b45a52401 | 70 | |
Reckstyle | 22:902c3086394e | 71 | measureTimer.start(); |
Reckstyle | 22:902c3086394e | 72 | driveMode = REGULAR; //initialise drivemoder |
Reckstyle | 22:902c3086394e | 73 | sensor_initialise(); // initialise sensor values |
Reckstyle | 22:902c3086394e | 74 | wait(1); //give time to set up the system |
Reckstyle | 22:902c3086394e | 75 | |
Reckstyle | 22:902c3086394e | 76 | sensorTimer.start(); // start timer for sensors |
Reckstyle | 29:307b45a52401 | 77 | float normalSpeed = 0.1f; |
Reckstyle | 29:307b45a52401 | 78 | // motors.setSteeringMode (NORMAL); |
Reckstyle | 29:307b45a52401 | 79 | motors.forward (); |
Reckstyle | 29:307b45a52401 | 80 | motors.setSpeed (normalSpeed); |
Reckstyle | 29:307b45a52401 | 81 | motors.start (); |
Reckstyle | 29:307b45a52401 | 82 | wait(5); |
Reckstyle | 29:307b45a52401 | 83 | motors.stop(); |
Reckstyle | 29:307b45a52401 | 84 | HC06.printf("heello"); |
Reckstyle | 16:3649eb1a056d | 85 | while(1){ |
Reckstyle | 29:307b45a52401 | 86 | |
Reckstyle | 29:307b45a52401 | 87 | /* if (HC06.getc() == 'r') { |
Reckstyle | 28:d1e7daeb240e | 88 | measureSensors(); |
Reckstyle | 28:d1e7daeb240e | 89 | printBluetooth(); |
Reckstyle | 28:d1e7daeb240e | 90 | } |
Reckstyle | 29:307b45a52401 | 91 | if (sensorsChecksum == 94) { |
Reckstyle | 29:307b45a52401 | 92 | } |
Reckstyle | 29:307b45a52401 | 93 | else if (sensorsCheckSum < 96) { |
Reckstyle | 29:307b45a52401 | 94 | motors.setSpeed (0.0); |
Reckstyle | 29:307b45a52401 | 95 | motors.setRightSpeed(normalSpeed*2); |
Reckstyle | 29:307b45a52401 | 96 | wait(0.0002); |
Reckstyle | 28:d1e7daeb240e | 97 | } |
Reckstyle | 29:307b45a52401 | 98 | else if (sensorsCheckSum > 96) { |
Reckstyle | 29:307b45a52401 | 99 | motors.setSpeed (0.0); |
Reckstyle | 29:307b45a52401 | 100 | motors.setLeftSpeed(normalSpeed*2); |
Reckstyle | 29:307b45a52401 | 101 | wait(0.0002); |
Reckstyle | 28:d1e7daeb240e | 102 | } |
Reckstyle | 29:307b45a52401 | 103 | motors.setSpeed (normalSpeed);*/ |
Reckstyle | 25:bec5dc4c9f5e | 104 | } |
Reckstyle | 28:d1e7daeb240e | 105 | |
Reckstyle | 28:d1e7daeb240e | 106 | /* |
Reckstyle | 28:d1e7daeb240e | 107 | int testOtherCases = 0; //needed for control statements |
Reckstyle | 19:198dc13777eb | 108 | while (1) { |
Reckstyle | 19:198dc13777eb | 109 | |
Reckstyle | 25:bec5dc4c9f5e | 110 | if (driveMode == REGULAR) { |
Bartas | 27:fc0fd2c0eebb | 111 | measureSensors(); |
Bartas | 27:fc0fd2c0eebb | 112 | switch (sensorsCheckSum) { |
Reckstyle | 25:bec5dc4c9f5e | 113 | case 0: // all black, turn around by 180 degrees or a possible turn on the left |
Bartas | 27:fc0fd2c0eebb | 114 | for (k=0;k<5;k++) { //left turn situation >> stop, go backwards |
Bartas | 27:fc0fd2c0eebb | 115 | if (oldValues[k] == 194) { |
Bartas | 27:fc0fd2c0eebb | 116 | motors.stop(); |
Bartas | 27:fc0fd2c0eebb | 117 | motors.setSteeringMode(NORMAL); |
Bartas | 27:fc0fd2c0eebb | 118 | motors.reverse(); |
Bartas | 27:fc0fd2c0eebb | 119 | motors.setSpeed(0.1f); |
Bartas | 27:fc0fd2c0eebb | 120 | motors.start(); |
Bartas | 27:fc0fd2c0eebb | 121 | wait(2); |
Bartas | 27:fc0fd2c0eebb | 122 | motors.stop(); |
Bartas | 27:fc0fd2c0eebb | 123 | } else { //dead end situation >> stop, go a bit backwards so there is space for turning and turn CCW |
Bartas | 27:fc0fd2c0eebb | 124 | motors.stop(); |
Bartas | 27:fc0fd2c0eebb | 125 | motors.setSteeringMode(NORMAL); |
Bartas | 27:fc0fd2c0eebb | 126 | motors.reverse(); |
Bartas | 27:fc0fd2c0eebb | 127 | motors.setSpeed(0.1f); |
Bartas | 27:fc0fd2c0eebb | 128 | motors.start(); |
Bartas | 27:fc0fd2c0eebb | 129 | wait(2); |
Bartas | 27:fc0fd2c0eebb | 130 | motors.stop(); |
Bartas | 27:fc0fd2c0eebb | 131 | motors.setSteeringMode(PIVOT_CCW); |
Bartas | 27:fc0fd2c0eebb | 132 | motors.setSpeed(0.1f); |
Reckstyle | 25:bec5dc4c9f5e | 133 | do |
Bartas | 27:fc0fd2c0eebb | 134 | { |
Reckstyle | 25:bec5dc4c9f5e | 135 | motors.start(); |
Reckstyle | 25:bec5dc4c9f5e | 136 | measureSensors(); |
Bartas | 27:fc0fd2c0eebb | 137 | } while (sensorsCheckSum != 96); |
Reckstyle | 25:bec5dc4c9f5e | 138 | motors.stop(); |
Bartas | 27:fc0fd2c0eebb | 139 | } |
Bartas | 27:fc0fd2c0eebb | 140 | } |
Bartas | 27:fc0fd2c0eebb | 141 | break; |
Bartas | 27:fc0fd2c0eebb | 142 | case 30: //all right are white, left all black >> turn right(move left wheel faster) |
Bartas | 27:fc0fd2c0eebb | 143 | motors.setSteeringMode(NORMAL); |
Bartas | 27:fc0fd2c0eebb | 144 | motors.forward(); |
Bartas | 27:fc0fd2c0eebb | 145 | motors.setRightSpeed(0.1f); |
Bartas | 27:fc0fd2c0eebb | 146 | motors.setLeftSpeed(0.05f); |
Bartas | 27:fc0fd2c0eebb | 147 | do |
Bartas | 27:fc0fd2c0eebb | 148 | { |
Bartas | 27:fc0fd2c0eebb | 149 | motors.start(); |
Bartas | 27:fc0fd2c0eebb | 150 | measureSensors(); |
Bartas | 27:fc0fd2c0eebb | 151 | } while (sensorsCheckSum == 30); |
Bartas | 27:fc0fd2c0eebb | 152 | motors.stop(); |
Bartas | 27:fc0fd2c0eebb | 153 | break; |
Bartas | 27:fc0fd2c0eebb | 154 | |
Bartas | 27:fc0fd2c0eebb | 155 | case 94: //normal <<STARTING POSITION>>, half of right and half of left are white |
Bartas | 27:fc0fd2c0eebb | 156 | motors.setSteeringMode(NORMAL); |
Bartas | 27:fc0fd2c0eebb | 157 | motors.forward(); |
Bartas | 27:fc0fd2c0eebb | 158 | motors.setSpeed(0.1f); |
Bartas | 27:fc0fd2c0eebb | 159 | do |
Bartas | 27:fc0fd2c0eebb | 160 | { |
Bartas | 27:fc0fd2c0eebb | 161 | motors.start(); |
Bartas | 27:fc0fd2c0eebb | 162 | measureSensors(); |
Bartas | 27:fc0fd2c0eebb | 163 | } while (sensorsCheckSum == 94); |
Bartas | 27:fc0fd2c0eebb | 164 | motors.start(); |
Bartas | 27:fc0fd2c0eebb | 165 | break; |
Bartas | 27:fc0fd2c0eebb | 166 | case 104: //right all white, left half white >> turn right |
Bartas | 27:fc0fd2c0eebb | 167 | motors.setSteeringMode(NORMAL); |
Bartas | 27:fc0fd2c0eebb | 168 | motors.forward(); |
Bartas | 27:fc0fd2c0eebb | 169 | motors.setSpeed(0.1f); |
Bartas | 27:fc0fd2c0eebb | 170 | motors.start(); |
Bartas | 27:fc0fd2c0eebb | 171 | wait(1); |
Bartas | 27:fc0fd2c0eebb | 172 | motors.stop(); |
Bartas | 27:fc0fd2c0eebb | 173 | motors.setRightSpeed(0.2f); |
Bartas | 27:fc0fd2c0eebb | 174 | motors.setLeftSpeed(0.0f); |
Bartas | 27:fc0fd2c0eebb | 175 | do |
Bartas | 27:fc0fd2c0eebb | 176 | { |
Bartas | 27:fc0fd2c0eebb | 177 | motors.start(); |
Bartas | 27:fc0fd2c0eebb | 178 | measureSensors(); |
Bartas | 27:fc0fd2c0eebb | 179 | } while (sensorsCheckSum <= 94); |
Bartas | 27:fc0fd2c0eebb | 180 | motors.stop(); |
Bartas | 27:fc0fd2c0eebb | 181 | break; |
Bartas | 27:fc0fd2c0eebb | 182 | case 174: //left all white, right all black >> turn left (move right wheel) |
Bartas | 27:fc0fd2c0eebb | 183 | motors.setSteeringMode(NORMAL); |
Bartas | 27:fc0fd2c0eebb | 184 | motors.forward(); |
Bartas | 27:fc0fd2c0eebb | 185 | motors.setRightSpeed(0.05f); |
Bartas | 27:fc0fd2c0eebb | 186 | motors.setLeftSpeed(0.1f); |
Bartas | 27:fc0fd2c0eebb | 187 | do |
Bartas | 27:fc0fd2c0eebb | 188 | { |
Bartas | 27:fc0fd2c0eebb | 189 | motors.start(); |
Bartas | 27:fc0fd2c0eebb | 190 | measureSensors(); |
Bartas | 27:fc0fd2c0eebb | 191 | } while (sensorsCheckSum == 174); |
Bartas | 27:fc0fd2c0eebb | 192 | break; |
Bartas | 27:fc0fd2c0eebb | 193 | case 194 : //left all white, right half white >> go straight, turn right if 194 goes to 204 |
Bartas | 27:fc0fd2c0eebb | 194 | motors.setSteeringMode(NORMAL); |
Bartas | 27:fc0fd2c0eebb | 195 | motors.forward(); |
Bartas | 27:fc0fd2c0eebb | 196 | motors.setRightSpeed(0.05f); |
Bartas | 27:fc0fd2c0eebb | 197 | motors.setLeftSpeed(0.1f); |
Bartas | 27:fc0fd2c0eebb | 198 | do |
Bartas | 27:fc0fd2c0eebb | 199 | { |
Bartas | 27:fc0fd2c0eebb | 200 | motors.start(); |
Bartas | 27:fc0fd2c0eebb | 201 | measureSensors(); |
Bartas | 27:fc0fd2c0eebb | 202 | } while (sensorsCheckSum == 194); |
Bartas | 27:fc0fd2c0eebb | 203 | motors.stop(); //do while left is white |
Bartas | 27:fc0fd2c0eebb | 204 | break; |
Bartas | 27:fc0fd2c0eebb | 205 | case 204 : //all sensors are white |
Bartas | 27:fc0fd2c0eebb | 206 | for (k=0;k<6;k++) { //situation when a square is entered, need to follow right line |
Bartas | 27:fc0fd2c0eebb | 207 | if (oldValues[k] == 194) { //checks whether black line on the right was present before |
Bartas | 27:fc0fd2c0eebb | 208 | motors.setSteeringMode(NORMAL); |
Bartas | 27:fc0fd2c0eebb | 209 | motors.setRightSpeed(0.05f); |
Bartas | 27:fc0fd2c0eebb | 210 | motors.setLeftSpeed(0.1f); |
Bartas | 27:fc0fd2c0eebb | 211 | do |
Bartas | 27:fc0fd2c0eebb | 212 | { |
Bartas | 27:fc0fd2c0eebb | 213 | motors.start(); |
Bartas | 27:fc0fd2c0eebb | 214 | measureSensors(); |
Bartas | 27:fc0fd2c0eebb | 215 | } while (sensorsCheckSum == 204); |
Bartas | 27:fc0fd2c0eebb | 216 | motors.stop(); |
Reckstyle | 25:bec5dc4c9f5e | 217 | } |
Bartas | 27:fc0fd2c0eebb | 218 | if (oldValues[k] == 104) { //right turn 90 situation |
Bartas | 27:fc0fd2c0eebb | 219 | motors.setSteeringMode(PIVOT_CW); |
Bartas | 27:fc0fd2c0eebb | 220 | motors.setRightSpeed(0.1f); |
Bartas | 27:fc0fd2c0eebb | 221 | motors.setLeftSpeed(0.1f); |
Bartas | 27:fc0fd2c0eebb | 222 | do |
Bartas | 27:fc0fd2c0eebb | 223 | { |
Bartas | 27:fc0fd2c0eebb | 224 | motors.start(); |
Bartas | 27:fc0fd2c0eebb | 225 | measureSensors(); |
Bartas | 27:fc0fd2c0eebb | 226 | } while (sensorsCheckSum != 94); |
Bartas | 27:fc0fd2c0eebb | 227 | motors.stop(); |
Reckstyle | 28:d1e7daeb240e | 228 | } |
Reckstyle | 28:d1e7daeb240e | 229 | } |
Bartas | 27:fc0fd2c0eebb | 230 | break; |
Bartas | 27:fc0fd2c0eebb | 231 | default: |
Bartas | 27:fc0fd2c0eebb | 232 | measureSensors(); |
Bartas | 27:fc0fd2c0eebb | 233 | break; |
Reckstyle | 28:d1e7daeb240e | 234 | } |
Reckstyle | 28:d1e7daeb240e | 235 | } // if statement |
Reckstyle | 28:d1e7daeb240e | 236 | else { |
Reckstyle | 28:d1e7daeb240e | 237 | testOtherCases = 1; |
Reckstyle | 28:d1e7daeb240e | 238 | }//if driveMode == SQUARE |
Reckstyle | 28:d1e7daeb240e | 239 | |
Reckstyle | 28:d1e7daeb240e | 240 | }//while loop |
Reckstyle | 28:d1e7daeb240e | 241 | */ |
Reckstyle | 28:d1e7daeb240e | 242 | } //int main |
Reckstyle | 11:9e56d52485d1 | 243 | |
Reckstyle | 28:d1e7daeb240e | 244 |