Team Design project 3 main file

Dependencies:   LocalPositionSystem MMA8451Q Motor_Driver Sensors mbed

Fork of TDP_main by Yelfie

Committer:
Bartas
Date:
Mon Mar 23 12:38:53 2015 +0000
Revision:
24:ecc3fbaf2824
Parent:
23:9b53c72fcd38
Child:
25:bec5dc4c9f5e
Child:
26:7b67bcbddde3
Case statements updated

Who changed what in which revision?

UserRevisionLine numberNew 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
Bartas 24:ecc3fbaf2824 21 Serial HC06(PTE0,PTE1);
Reckstyle 9:718987b106a8 22
Reckstyle 18:d277614084bc 23 Timer measureTimer; //Timer used for measurement
Joseph_Penikis 10:c9212bbfeae6 24
orsharp 12:bb21b76b6375 25 //Motor_Driver motors(PTD4, PTA12, PTA4, PTA5, PTC9, PTC8, PWM_PERIOD_US);
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 21:9cf274ffe1de 31 int values_old[5] = {0};
Reckstyle 23:9b53c72fcd38 32 int cursor = 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 }
Bartas 24:ecc3fbaf2824 47 if (values_old[cursor] != sensorsCheckSum) { //why only if different ??
Reckstyle 23:9b53c72fcd38 48 values_old[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 20:e8da3b351cd0 55 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 56 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 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 18:d277614084bc 60 pc.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 18:d277614084bc 66 Motor_Driver motors(PTA5, PTC9, PTC8,PTD4, PTA12, PTA4, PWM_PERIOD_US);
Reckstyle 11:9e56d52485d1 67
Reckstyle 17:de8b3111ddc5 68 // motors.setSpeed(0.1f);
Reckstyle 17:de8b3111ddc5 69 // motors.forward();
Reckstyle 17:de8b3111ddc5 70 // motors.start();
Reckstyle 17:de8b3111ddc5 71 // wait(2);
Reckstyle 17:de8b3111ddc5 72 // float x=0.1f;
Reckstyle 17:de8b3111ddc5 73 // while (1) {
Reckstyle 17:de8b3111ddc5 74 // motors.setLeftSpeed(x);
Reckstyle 17:de8b3111ddc5 75 // x = x+0.05;
Reckstyle 17:de8b3111ddc5 76 // wait(3);
Reckstyle 17:de8b3111ddc5 77 // }
Reckstyle 17:de8b3111ddc5 78 // motors.setLeftSpeed(0.1f);
Reckstyle 17:de8b3111ddc5 79 // wait(5);
Reckstyle 17:de8b3111ddc5 80 // motors.setLeftSpeed(0.2f);
Reckstyle 17:de8b3111ddc5 81 // motors.setRightSpeed (0.2f);
Reckstyle 17:de8b3111ddc5 82 // wait(3);
Reckstyle 17:de8b3111ddc5 83 // motors.setRightSpeed (0.1f);
Reckstyle 17:de8b3111ddc5 84 // wait(5);
Reckstyle 17:de8b3111ddc5 85 // motors.stop();
Reckstyle 11:9e56d52485d1 86
Reckstyle 17:de8b3111ddc5 87 //wait(1);
Reckstyle 17:de8b3111ddc5 88 // motors.reverse();
Reckstyle 17:de8b3111ddc5 89 // wait(5);
Reckstyle 17:de8b3111ddc5 90 // motors.stop();
Reckstyle 17:de8b3111ddc5 91 // motors.setSpeed(0.5f);
Reckstyle 17:de8b3111ddc5 92 // motors.start();
Reckstyle 17:de8b3111ddc5 93 // wait(5);
Reckstyle 17:de8b3111ddc5 94 // motors.stop();
Reckstyle 17:de8b3111ddc5 95 // wait(1);
Reckstyle 17:de8b3111ddc5 96 // motors.reverse();
Reckstyle 11:9e56d52485d1 97
Reckstyle 22:902c3086394e 98 // setup_counter(1000, 0);
Reckstyle 22:902c3086394e 99 // float frequency = measure_frequency(CHANNEL_1);
Reckstyle 22:902c3086394e 100 measureTimer.start();
Reckstyle 22:902c3086394e 101 driveMode = REGULAR; //initialise drivemoder
Reckstyle 22:902c3086394e 102 sensor_initialise(); // initialise sensor values
Reckstyle 22:902c3086394e 103 wait(1); //give time to set up the system
Reckstyle 22:902c3086394e 104
Reckstyle 22:902c3086394e 105 sensorTimer.start(); // start timer for sensors
Reckstyle 18:d277614084bc 106 float normalSpeed = 0.01f;
Bartas 24:ecc3fbaf2824 107
Reckstyle 16:3649eb1a056d 108 while(1){
Reckstyle 22:902c3086394e 109 if (pc.getc() == 'r') {
Reckstyle 16:3649eb1a056d 110 measureSensors();
Reckstyle 18:d277614084bc 111 //measureTimer.reset();
Reckstyle 16:3649eb1a056d 112 printBluetooth();
Reckstyle 18:d277614084bc 113 //passedTime1 = measureTimer.read_ms();
Reckstyle 22:902c3086394e 114 //if (sensorsCheckSum == 0) {
Reckstyle 22:902c3086394e 115 // motors.setSpeed(normalSpeed);
Reckstyle 22:902c3086394e 116 // }
Reckstyle 22:902c3086394e 117 // else if (sensorsCheckSum == 1 || sensorsCheckSum == 9 || sensorsCheckSum == 10 || sensorsCheckSum == 14 || sensorsCheckSum==26){
Reckstyle 22:902c3086394e 118 // motors.setLeftSpeed(normalSpeed/2);
Reckstyle 22:902c3086394e 119 //
Reckstyle 22:902c3086394e 120 // motors.setRightSpeed(normalSpeed*6);
Reckstyle 22:902c3086394e 121 // }
Reckstyle 22:902c3086394e 122 // else if (sensorsCheckSum == 4 && sensorsCheckSum ==16 || sensorsCheckSum == 20 || sensorsCheckSum ==21 ||sensorsCheckSum== 29) {
Reckstyle 22:902c3086394e 123 // motors.setRightSpeed(normalSpeed/2);
Reckstyle 22:902c3086394e 124 // motors.setLeftSpeed(normalSpeed*9);
Reckstyle 22:902c3086394e 125 // }
Reckstyle 22:902c3086394e 126 // else {
Reckstyle 22:902c3086394e 127 // motors.setSpeed(normalSpeed);
Reckstyle 22:902c3086394e 128 // }
Reckstyle 15:6453cd351452 129
Reckstyle 19:198dc13777eb 130 int testOtherCases = 0; //needed for control statements
Reckstyle 19:198dc13777eb 131 while (1) {
Reckstyle 19:198dc13777eb 132
Bartas 24:ecc3fbaf2824 133 if (driveMode == REGULAR) {
Reckstyle 19:198dc13777eb 134 measureSensors();
Reckstyle 19:198dc13777eb 135 switch (sensorsCheckSum) {
Bartas 24:ecc3fbaf2824 136 case 0: // all black, turn around by 180 degrees or a possible turn on the left
Bartas 24:ecc3fbaf2824 137 for (k=0;k<6;k++) { //left turn situation
Bartas 24:ecc3fbaf2824 138 if (oldValues[k] == 194) {
Bartas 24:ecc3fbaf2824 139 motors.stop();
Bartas 24:ecc3fbaf2824 140 motors.setSteeringMode(NORMAL);
Bartas 24:ecc3fbaf2824 141 motors.reverse();
Bartas 24:ecc3fbaf2824 142 motors.setSpeed(0.1f);
Bartas 24:ecc3fbaf2824 143 motors.start();
Bartas 24:ecc3fbaf2824 144 wait(1);
Bartas 24:ecc3fbaf2824 145 motors.stop();
Bartas 24:ecc3fbaf2824 146 } else {
Bartas 24:ecc3fbaf2824 147 motors.stop();
Bartas 24:ecc3fbaf2824 148 motors.setSteeringMode(NORMAL);
Bartas 24:ecc3fbaf2824 149 motors.reverse();
Bartas 24:ecc3fbaf2824 150 motors.setSpeed(0.1f);
Bartas 24:ecc3fbaf2824 151 motors.start();
Bartas 24:ecc3fbaf2824 152 wait(2);
Bartas 24:ecc3fbaf2824 153 motors.stop();
Bartas 24:ecc3fbaf2824 154 motors.setSteeringMode(PIVOT_CCW);
Bartas 24:ecc3fbaf2824 155 motors.setSpeed(0.1f);
Bartas 24:ecc3fbaf2824 156 do
Bartas 24:ecc3fbaf2824 157 {
Bartas 24:ecc3fbaf2824 158 motors.start();
Bartas 24:ecc3fbaf2824 159 measureSensors();
Bartas 24:ecc3fbaf2824 160 } while (sensorsCheckSum != 96);
Bartas 24:ecc3fbaf2824 161 motors.stop();
Bartas 24:ecc3fbaf2824 162 motors.setSteeringMode(NORMAL);
Bartas 24:ecc3fbaf2824 163 }
Reckstyle 20:e8da3b351cd0 164 break;
Reckstyle 20:e8da3b351cd0 165 case 30: //all right are white, left all black >> turn right(move left wheel)
Bartas 24:ecc3fbaf2824 166 motors.setSteeringMode(NORMAL);
Bartas 24:ecc3fbaf2824 167 motors.forward();
Bartas 24:ecc3fbaf2824 168 motors.setRightSpeed(0.15f);
Bartas 24:ecc3fbaf2824 169 motors.setLeftSpeed(0.5f);
Reckstyle 20:e8da3b351cd0 170 break;
Bartas 24:ecc3fbaf2824 171 // case 46: //left 5 white, right only 3 black >> turn right
Bartas 24:ecc3fbaf2824 172 // motors.setRightSpeed(0.15f);
Bartas 24:ecc3fbaf2824 173 // motors.setLeftSpeed(0.1f);
Bartas 24:ecc3fbaf2824 174 // break;
Bartas 24:ecc3fbaf2824 175 case 94: //normal <<STARTING POSITION>>, half of right and half of left are white
Bartas 24:ecc3fbaf2824 176 motors.setSteeringMode(NORMAL);
Bartas 24:ecc3fbaf2824 177 motors.forward();
Bartas 24:ecc3fbaf2824 178 motors.setSpeed(0.1f);
Bartas 24:ecc3fbaf2824 179 motors.start();
Reckstyle 20:e8da3b351cd0 180 break;
Bartas 24:ecc3fbaf2824 181 case 104: //right all white, left half white >> turn right
Bartas 24:ecc3fbaf2824 182 motors.setRightSpeed(0.1f);
Bartas 24:ecc3fbaf2824 183 motors.setLeftSpeed(0.15f);
Bartas 24:ecc3fbaf2824 184 break;
Bartas 24:ecc3fbaf2824 185 // case 154: //right 4 white, left only 6 black >> turn left
Bartas 24:ecc3fbaf2824 186 // break;
Reckstyle 20:e8da3b351cd0 187 case 174: //left all white, right all black >> turn left (move right wheel)
Bartas 24:ecc3fbaf2824 188 motors.setRightSpeed(0.05f);
Bartas 24:ecc3fbaf2824 189 motors.setLeftSpeed(0.15f);
Reckstyle 20:e8da3b351cd0 190 break;
Bartas 24:ecc3fbaf2824 191 case 194 : //left all white, right half white >> go straight, turn right if 194 goes to 204
Reckstyle 20:e8da3b351cd0 192 break;
Reckstyle 20:e8da3b351cd0 193 case 204 : //all sensors are white
Bartas 24:ecc3fbaf2824 194
Bartas 24:ecc3fbaf2824 195 for (k=0;k<6;k++) { //situation when a square is entered, need to follow right line
Bartas 24:ecc3fbaf2824 196 if (oldValues[k] == 194) { //checks whether black line on the right was present before
Bartas 24:ecc3fbaf2824 197 motors.setRightSpeed(0.15f);
Bartas 24:ecc3fbaf2824 198 motors.setLeftSpeed(0.05f);
Bartas 24:ecc3fbaf2824 199 }
Bartas 24:ecc3fbaf2824 200
Bartas 24:ecc3fbaf2824 201 if (oldValues[k] == 104) { //right turn 90 situation
Bartas 24:ecc3fbaf2824 202 motors.stop();
Bartas 24:ecc3fbaf2824 203 motors.setSteeringMode(PIVOT_CW);
Bartas 24:ecc3fbaf2824 204 motors.setRightSpeed(0.1f);
Bartas 24:ecc3fbaf2824 205 motors.setLeftSpeed(0.1f);
Bartas 24:ecc3fbaf2824 206 do
Bartas 24:ecc3fbaf2824 207 {
Bartas 24:ecc3fbaf2824 208 motors.start();
Bartas 24:ecc3fbaf2824 209 measureSensors();
Bartas 24:ecc3fbaf2824 210 } while (sensorsCheckSum != 94);
Bartas 24:ecc3fbaf2824 211 motors.stop();
Bartas 24:ecc3fbaf2824 212 motors.setSteeringMode(NORMAL);
Bartas 24:ecc3fbaf2824 213 }
Bartas 24:ecc3fbaf2824 214 break;
Bartas 24:ecc3fbaf2824 215
Bartas 24:ecc3fbaf2824 216 default: //checksum is zero , all are black
Bartas 24:ecc3fbaf2824 217 measureSensors();
Reckstyle 19:198dc13777eb 218 break;
Bartas 24:ecc3fbaf2824 219 }
Bartas 24:ecc3fbaf2824 220 }
Bartas 24:ecc3fbaf2824 221 // if (testOtherCases == 1) {
Bartas 24:ecc3fbaf2824 222 // if (sensorsCheckSum < 96){ // adjust right
Bartas 24:ecc3fbaf2824 223 // } else {//adjust left
Bartas 24:ecc3fbaf2824 224 // }
Bartas 24:ecc3fbaf2824 225 // testOtherCases = 0;
Bartas 24:ecc3fbaf2824 226 // } else { //if (driveMode == SQUARE} //implement the square searching thing.
Bartas 24:ecc3fbaf2824 227 }
Bartas 24:ecc3fbaf2824 228 }
Bartas 24:ecc3fbaf2824 229
Reckstyle 19:198dc13777eb 230 if (testOtherCases == 1) {
Reckstyle 19:198dc13777eb 231 if (sensorsCheckSum < 96){ // adjust right
Reckstyle 19:198dc13777eb 232 }
Reckstyle 19:198dc13777eb 233 else {//adjust left
Reckstyle 19:198dc13777eb 234 }
Reckstyle 19:198dc13777eb 235 testOtherCases = 0;
Reckstyle 19:198dc13777eb 236 }
Reckstyle 14:3844d1dacece 237 // }
Reckstyle 14:3844d1dacece 238 // else { //if (driveMode == SQUARE}
Reckstyle 14:3844d1dacece 239 // //implement the square searching thing..
Reckstyle 11:9e56d52485d1 240
Reckstyle 11:9e56d52485d1 241
Reckstyle 11:9e56d52485d1 242
Reckstyle 11:9e56d52485d1 243
Reckstyle 11:9e56d52485d1 244