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 14:07:35 2015 +0000
Revision:
27:fc0fd2c0eebb
Parent:
25:bec5dc4c9f5e
Child:
28:d1e7daeb240e
ALl the cases atm

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 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 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 22:902c3086394e 68 // setup_counter(1000, 0);
Reckstyle 22:902c3086394e 69 // float frequency = measure_frequency(CHANNEL_1);
Reckstyle 22:902c3086394e 70 measureTimer.start();
Reckstyle 22:902c3086394e 71 driveMode = REGULAR; //initialise drivemoder
Reckstyle 22:902c3086394e 72 sensor_initialise(); // initialise sensor values
Reckstyle 22:902c3086394e 73 wait(1); //give time to set up the system
Reckstyle 22:902c3086394e 74
Reckstyle 22:902c3086394e 75 sensorTimer.start(); // start timer for sensors
Bartas 27:fc0fd2c0eebb 76 // float normalSpeed = 0.01f;
Reckstyle 25:bec5dc4c9f5e 77 /*
Reckstyle 16:3649eb1a056d 78 while(1){
Reckstyle 22:902c3086394e 79 if (pc.getc() == 'r') {
Reckstyle 16:3649eb1a056d 80 measureSensors();
Reckstyle 18:d277614084bc 81 //measureTimer.reset();
Reckstyle 16:3649eb1a056d 82 printBluetooth();
Reckstyle 18:d277614084bc 83 //passedTime1 = measureTimer.read_ms();
Reckstyle 22:902c3086394e 84 //if (sensorsCheckSum == 0) {
Reckstyle 22:902c3086394e 85 // motors.setSpeed(normalSpeed);
Reckstyle 22:902c3086394e 86 // }
Reckstyle 22:902c3086394e 87 // else if (sensorsCheckSum == 1 || sensorsCheckSum == 9 || sensorsCheckSum == 10 || sensorsCheckSum == 14 || sensorsCheckSum==26){
Reckstyle 22:902c3086394e 88 // motors.setLeftSpeed(normalSpeed/2);
Reckstyle 22:902c3086394e 89 //
Reckstyle 22:902c3086394e 90 // motors.setRightSpeed(normalSpeed*6);
Reckstyle 22:902c3086394e 91 // }
Reckstyle 22:902c3086394e 92 // else if (sensorsCheckSum == 4 && sensorsCheckSum ==16 || sensorsCheckSum == 20 || sensorsCheckSum ==21 ||sensorsCheckSum== 29) {
Reckstyle 22:902c3086394e 93 // motors.setRightSpeed(normalSpeed/2);
Reckstyle 22:902c3086394e 94 // motors.setLeftSpeed(normalSpeed*9);
Reckstyle 22:902c3086394e 95 // }
Reckstyle 22:902c3086394e 96 // else {
Reckstyle 22:902c3086394e 97 // motors.setSpeed(normalSpeed);
Reckstyle 22:902c3086394e 98 // }
Reckstyle 25:bec5dc4c9f5e 99 }
Reckstyle 25:bec5dc4c9f5e 100 */
Bartas 27:fc0fd2c0eebb 101 // int testOtherCases = 0; //needed for control statements
Reckstyle 19:198dc13777eb 102 while (1) {
Reckstyle 19:198dc13777eb 103
Reckstyle 25:bec5dc4c9f5e 104 if (driveMode == REGULAR) {
Bartas 27:fc0fd2c0eebb 105 measureSensors();
Bartas 27:fc0fd2c0eebb 106 switch (sensorsCheckSum) {
Reckstyle 25:bec5dc4c9f5e 107 case 0: // all black, turn around by 180 degrees or a possible turn on the left
Bartas 27:fc0fd2c0eebb 108 for (k=0;k<5;k++) { //left turn situation >> stop, go backwards
Bartas 27:fc0fd2c0eebb 109 if (oldValues[k] == 194) {
Bartas 27:fc0fd2c0eebb 110 motors.stop();
Bartas 27:fc0fd2c0eebb 111 motors.setSteeringMode(NORMAL);
Bartas 27:fc0fd2c0eebb 112 motors.reverse();
Bartas 27:fc0fd2c0eebb 113 motors.setSpeed(0.1f);
Bartas 27:fc0fd2c0eebb 114 motors.start();
Bartas 27:fc0fd2c0eebb 115 wait(2);
Bartas 27:fc0fd2c0eebb 116 motors.stop();
Bartas 27:fc0fd2c0eebb 117 } else { //dead end situation >> stop, go a bit backwards so there is space for turning and turn CCW
Bartas 27:fc0fd2c0eebb 118 motors.stop();
Bartas 27:fc0fd2c0eebb 119 motors.setSteeringMode(NORMAL);
Bartas 27:fc0fd2c0eebb 120 motors.reverse();
Bartas 27:fc0fd2c0eebb 121 motors.setSpeed(0.1f);
Bartas 27:fc0fd2c0eebb 122 motors.start();
Bartas 27:fc0fd2c0eebb 123 wait(2);
Bartas 27:fc0fd2c0eebb 124 motors.stop();
Bartas 27:fc0fd2c0eebb 125 motors.setSteeringMode(PIVOT_CCW);
Bartas 27:fc0fd2c0eebb 126 motors.setSpeed(0.1f);
Reckstyle 25:bec5dc4c9f5e 127 do
Bartas 27:fc0fd2c0eebb 128 {
Reckstyle 25:bec5dc4c9f5e 129 motors.start();
Reckstyle 25:bec5dc4c9f5e 130 measureSensors();
Bartas 27:fc0fd2c0eebb 131 } while (sensorsCheckSum != 96);
Reckstyle 25:bec5dc4c9f5e 132 motors.stop();
Bartas 27:fc0fd2c0eebb 133 }
Bartas 27:fc0fd2c0eebb 134 }
Bartas 27:fc0fd2c0eebb 135 break;
Bartas 27:fc0fd2c0eebb 136 case 30: //all right are white, left all black >> turn right(move left wheel faster)
Bartas 27:fc0fd2c0eebb 137 motors.setSteeringMode(NORMAL);
Bartas 27:fc0fd2c0eebb 138 motors.forward();
Bartas 27:fc0fd2c0eebb 139 motors.setRightSpeed(0.1f);
Bartas 27:fc0fd2c0eebb 140 motors.setLeftSpeed(0.05f);
Bartas 27:fc0fd2c0eebb 141 do
Bartas 27:fc0fd2c0eebb 142 {
Bartas 27:fc0fd2c0eebb 143 motors.start();
Bartas 27:fc0fd2c0eebb 144 measureSensors();
Bartas 27:fc0fd2c0eebb 145 } while (sensorsCheckSum == 30);
Bartas 27:fc0fd2c0eebb 146 motors.stop();
Bartas 27:fc0fd2c0eebb 147 break;
Bartas 27:fc0fd2c0eebb 148
Bartas 27:fc0fd2c0eebb 149 case 94: //normal <<STARTING POSITION>>, half of right and half of left are white
Bartas 27:fc0fd2c0eebb 150 motors.setSteeringMode(NORMAL);
Bartas 27:fc0fd2c0eebb 151 motors.forward();
Bartas 27:fc0fd2c0eebb 152 motors.setSpeed(0.1f);
Bartas 27:fc0fd2c0eebb 153 do
Bartas 27:fc0fd2c0eebb 154 {
Bartas 27:fc0fd2c0eebb 155 motors.start();
Bartas 27:fc0fd2c0eebb 156 measureSensors();
Bartas 27:fc0fd2c0eebb 157 } while (sensorsCheckSum == 94);
Bartas 27:fc0fd2c0eebb 158 motors.start();
Bartas 27:fc0fd2c0eebb 159 break;
Bartas 27:fc0fd2c0eebb 160 case 104: //right all white, left half white >> turn right
Bartas 27:fc0fd2c0eebb 161 motors.setSteeringMode(NORMAL);
Bartas 27:fc0fd2c0eebb 162 motors.forward();
Bartas 27:fc0fd2c0eebb 163 motors.setSpeed(0.1f);
Bartas 27:fc0fd2c0eebb 164 motors.start();
Bartas 27:fc0fd2c0eebb 165 wait(1);
Bartas 27:fc0fd2c0eebb 166 motors.stop();
Bartas 27:fc0fd2c0eebb 167 motors.setRightSpeed(0.2f);
Bartas 27:fc0fd2c0eebb 168 motors.setLeftSpeed(0.0f);
Bartas 27:fc0fd2c0eebb 169 do
Bartas 27:fc0fd2c0eebb 170 {
Bartas 27:fc0fd2c0eebb 171 motors.start();
Bartas 27:fc0fd2c0eebb 172 measureSensors();
Bartas 27:fc0fd2c0eebb 173 } while (sensorsCheckSum <= 94);
Bartas 27:fc0fd2c0eebb 174 motors.stop();
Bartas 27:fc0fd2c0eebb 175 break;
Bartas 27:fc0fd2c0eebb 176 case 174: //left all white, right all black >> turn left (move right wheel)
Bartas 27:fc0fd2c0eebb 177 motors.setSteeringMode(NORMAL);
Bartas 27:fc0fd2c0eebb 178 motors.forward();
Bartas 27:fc0fd2c0eebb 179 motors.setRightSpeed(0.05f);
Bartas 27:fc0fd2c0eebb 180 motors.setLeftSpeed(0.1f);
Bartas 27:fc0fd2c0eebb 181 do
Bartas 27:fc0fd2c0eebb 182 {
Bartas 27:fc0fd2c0eebb 183 motors.start();
Bartas 27:fc0fd2c0eebb 184 measureSensors();
Bartas 27:fc0fd2c0eebb 185 } while (sensorsCheckSum == 174);
Bartas 27:fc0fd2c0eebb 186 break;
Bartas 27:fc0fd2c0eebb 187 case 194 : //left all white, right half white >> go straight, turn right if 194 goes to 204
Bartas 27:fc0fd2c0eebb 188 motors.setSteeringMode(NORMAL);
Bartas 27:fc0fd2c0eebb 189 motors.forward();
Bartas 27:fc0fd2c0eebb 190 motors.setRightSpeed(0.05f);
Bartas 27:fc0fd2c0eebb 191 motors.setLeftSpeed(0.1f);
Bartas 27:fc0fd2c0eebb 192 do
Bartas 27:fc0fd2c0eebb 193 {
Bartas 27:fc0fd2c0eebb 194 motors.start();
Bartas 27:fc0fd2c0eebb 195 measureSensors();
Bartas 27:fc0fd2c0eebb 196 } while (sensorsCheckSum == 194);
Bartas 27:fc0fd2c0eebb 197 motors.stop(); //do while left is white
Bartas 27:fc0fd2c0eebb 198 break;
Bartas 27:fc0fd2c0eebb 199 case 204 : //all sensors are white
Bartas 27:fc0fd2c0eebb 200 for (k=0;k<6;k++) { //situation when a square is entered, need to follow right line
Bartas 27:fc0fd2c0eebb 201 if (oldValues[k] == 194) { //checks whether black line on the right was present before
Bartas 27:fc0fd2c0eebb 202 motors.setSteeringMode(NORMAL);
Bartas 27:fc0fd2c0eebb 203 motors.setRightSpeed(0.05f);
Bartas 27:fc0fd2c0eebb 204 motors.setLeftSpeed(0.1f);
Bartas 27:fc0fd2c0eebb 205 do
Bartas 27:fc0fd2c0eebb 206 {
Bartas 27:fc0fd2c0eebb 207 motors.start();
Bartas 27:fc0fd2c0eebb 208 measureSensors();
Bartas 27:fc0fd2c0eebb 209 } while (sensorsCheckSum == 204);
Bartas 27:fc0fd2c0eebb 210 motors.stop();
Reckstyle 25:bec5dc4c9f5e 211 }
Bartas 27:fc0fd2c0eebb 212 if (oldValues[k] == 104) { //right turn 90 situation
Bartas 27:fc0fd2c0eebb 213 motors.setSteeringMode(PIVOT_CW);
Bartas 27:fc0fd2c0eebb 214 motors.setRightSpeed(0.1f);
Bartas 27:fc0fd2c0eebb 215 motors.setLeftSpeed(0.1f);
Bartas 27:fc0fd2c0eebb 216 do
Bartas 27:fc0fd2c0eebb 217 {
Bartas 27:fc0fd2c0eebb 218 motors.start();
Bartas 27:fc0fd2c0eebb 219 measureSensors();
Bartas 27:fc0fd2c0eebb 220 } while (sensorsCheckSum != 94);
Bartas 27:fc0fd2c0eebb 221 motors.stop();
Bartas 27:fc0fd2c0eebb 222 }
Bartas 27:fc0fd2c0eebb 223 break;
Bartas 27:fc0fd2c0eebb 224 default:
Bartas 27:fc0fd2c0eebb 225 measureSensors();
Bartas 27:fc0fd2c0eebb 226 break;
Bartas 24:ecc3fbaf2824 227 }
Bartas 24:ecc3fbaf2824 228 }
Reckstyle 25:bec5dc4c9f5e 229 }
Bartas 27:fc0fd2c0eebb 230 }
Bartas 27:fc0fd2c0eebb 231 }
Reckstyle 11:9e56d52485d1 232