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