Team Design project 3 main file

Dependencies:   mbed Motor_Driver Sensors MMA8451Q LocalPositionSystem

Fork of TDP_main by Ivelin Kozarev

Committer:
Bartas
Date:
Mon Mar 23 14:04:24 2015 +0000
Revision:
26:7b67bcbddde3
Parent:
24:ecc3fbaf2824
Before updating;

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;
Bartas 26:7b67bcbddde3 31 int oldValues[5] = {0};
Bartas 26:7b67bcbddde3 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 }
Bartas 26:7b67bcbddde3 47 if (oldValues[cursor] != sensorsCheckSum) { //why only if different ??
Bartas 26:7b67bcbddde3 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 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
Bartas 26:7b67bcbddde3 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
Bartas 26:7b67bcbddde3 130 // int testOtherCases = 0; //needed for control statements
Reckstyle 19:198dc13777eb 131 while (1) {
Bartas 26:7b67bcbddde3 132 // if (driveMode == REGULAR) {
Bartas 26:7b67bcbddde3 133 // measureSensors();
Bartas 26:7b67bcbddde3 134 switch (sensorsCheckSum) {
Bartas 26:7b67bcbddde3 135 case 0: // all black, turn around by 180 degrees or a possible turn on the left
Bartas 26:7b67bcbddde3 136 for (k=0;k<6;k++) { //left turn situation >> stop, go backwards
Bartas 26:7b67bcbddde3 137 if (oldValues[k] == 194) {
Bartas 26:7b67bcbddde3 138 motors.stop();
Bartas 26:7b67bcbddde3 139 motors.setSteeringMode(NORMAL);
Bartas 26:7b67bcbddde3 140 motors.reverse();
Bartas 26:7b67bcbddde3 141 motors.setSpeed(0.1f);
Bartas 26:7b67bcbddde3 142 motors.start();
Bartas 26:7b67bcbddde3 143 wait(2);
Bartas 26:7b67bcbddde3 144 motors.stop();
Bartas 26:7b67bcbddde3 145 } else { //dead end situation >> stop, go a bit backwards so there is space for turning and turn CCW
Bartas 26:7b67bcbddde3 146 motors.stop();
Bartas 26:7b67bcbddde3 147 motors.setSteeringMode(NORMAL);
Bartas 26:7b67bcbddde3 148 motors.reverse();
Bartas 26:7b67bcbddde3 149 motors.setSpeed(0.1f);
Bartas 26:7b67bcbddde3 150 motors.start();
Bartas 26:7b67bcbddde3 151 wait(2);
Bartas 26:7b67bcbddde3 152 motors.stop();
Bartas 26:7b67bcbddde3 153 motors.setSteeringMode(PIVOT_CCW);
Bartas 26:7b67bcbddde3 154 motors.setSpeed(0.1f);
Bartas 26:7b67bcbddde3 155 do
Bartas 26:7b67bcbddde3 156 {
Bartas 26:7b67bcbddde3 157 motors.start();
Bartas 26:7b67bcbddde3 158 measureSensors();
Bartas 26:7b67bcbddde3 159 } while (sensorsCheckSum != 96);
Bartas 26:7b67bcbddde3 160 motors.stop();
Bartas 26:7b67bcbddde3 161 }
Bartas 26:7b67bcbddde3 162 }
Reckstyle 20:e8da3b351cd0 163 break;
Bartas 26:7b67bcbddde3 164 case 30: //all right are white, left all black >> turn right(move left wheel faster)
Bartas 24:ecc3fbaf2824 165 motors.setSteeringMode(NORMAL);
Bartas 24:ecc3fbaf2824 166 motors.forward();
Bartas 26:7b67bcbddde3 167 motors.setRightSpeed(0.1f);
Bartas 26:7b67bcbddde3 168 motors.setLeftSpeed(0.05f);
Bartas 26:7b67bcbddde3 169 do
Bartas 26:7b67bcbddde3 170 {
Bartas 26:7b67bcbddde3 171 motors.start();
Bartas 26:7b67bcbddde3 172 measureSensors();
Bartas 26:7b67bcbddde3 173 } while (sensorsCheckSum == 30);
Bartas 26:7b67bcbddde3 174 motors.stop();
Reckstyle 20:e8da3b351cd0 175 break;
Bartas 26:7b67bcbddde3 176
Bartas 24:ecc3fbaf2824 177 case 94: //normal <<STARTING POSITION>>, half of right and half of left are white
Bartas 24:ecc3fbaf2824 178 motors.setSteeringMode(NORMAL);
Bartas 24:ecc3fbaf2824 179 motors.forward();
Bartas 24:ecc3fbaf2824 180 motors.setSpeed(0.1f);
Bartas 26:7b67bcbddde3 181 do
Bartas 26:7b67bcbddde3 182 {
Bartas 26:7b67bcbddde3 183 motors.start();
Bartas 26:7b67bcbddde3 184 measureSensors();
Bartas 26:7b67bcbddde3 185 } while (sensorsCheckSum == 94);
Bartas 26:7b67bcbddde3 186 motors.start();
Bartas 26:7b67bcbddde3 187 break;
Bartas 26:7b67bcbddde3 188 case 104: //right all white, left half white >> turn right
Bartas 26:7b67bcbddde3 189 motors.setSteeringMode(NORMAL);
Bartas 26:7b67bcbddde3 190 motors.forward();
Bartas 26:7b67bcbddde3 191 motors.setSpeed(0.1f);
Bartas 24:ecc3fbaf2824 192 motors.start();
Bartas 26:7b67bcbddde3 193 wait(1);
Bartas 26:7b67bcbddde3 194 motors.stop();
Bartas 26:7b67bcbddde3 195 motors.setRightSpeed(0.2f);
Bartas 26:7b67bcbddde3 196 motors.setLeftSpeed(0.0f);
Bartas 26:7b67bcbddde3 197 do
Bartas 26:7b67bcbddde3 198 {
Bartas 26:7b67bcbddde3 199 motors.start();
Bartas 26:7b67bcbddde3 200 measureSensors();
Bartas 26:7b67bcbddde3 201 } while (sensorsCheckSum <= 94);
Bartas 26:7b67bcbddde3 202 motors.stop();
Bartas 24:ecc3fbaf2824 203 break;
Reckstyle 20:e8da3b351cd0 204 case 174: //left all white, right all black >> turn left (move right wheel)
Bartas 26:7b67bcbddde3 205 motors.setSteeringMode(NORMAL);
Bartas 26:7b67bcbddde3 206 motors.forward();
Bartas 24:ecc3fbaf2824 207 motors.setRightSpeed(0.05f);
Bartas 26:7b67bcbddde3 208 motors.setLeftSpeed(0.1f);
Bartas 26:7b67bcbddde3 209 do
Bartas 26:7b67bcbddde3 210 {
Bartas 26:7b67bcbddde3 211 motors.start();
Bartas 26:7b67bcbddde3 212 measureSensors();
Bartas 26:7b67bcbddde3 213 } while (sensorsCheckSum == 174);
Reckstyle 20:e8da3b351cd0 214 break;
Bartas 26:7b67bcbddde3 215 case 194 : //left all white, right half white >> go straight, turn right if 194 goes to 204
Bartas 26:7b67bcbddde3 216 motors.setSteeringMode(NORMAL);
Bartas 26:7b67bcbddde3 217 motors.forward();
Bartas 26:7b67bcbddde3 218 motors.setRightSpeed(0.05f);
Bartas 26:7b67bcbddde3 219 motors.setLeftSpeed(0.1f)
Bartas 26:7b67bcbddde3 220 do
Bartas 26:7b67bcbddde3 221 {
Bartas 26:7b67bcbddde3 222 motors.start();
Bartas 26:7b67bcbddde3 223 measureSensors();
Bartas 26:7b67bcbddde3 224 } while (sensorsCheckSum == 194);
Bartas 26:7b67bcbddde3 225 motors.stop(); //do while left is white
Bartas 26:7b67bcbddde3 226 break;
Bartas 26:7b67bcbddde3 227 case 204 : //all sensors are white
Bartas 26:7b67bcbddde3 228 for (k=0;k<6;k++) { //situation when a square is entered, need to follow right line
Bartas 26:7b67bcbddde3 229 if (oldValues[k] == 194) { //checks whether black line on the right was present before
Bartas 26:7b67bcbddde3 230 motors.setSteeringMode(NORMAL);
Bartas 26:7b67bcbddde3 231 motors.setRightSpeed(0.05f);
Bartas 26:7b67bcbddde3 232 motors.setLeftSpeed(0.1f);
Bartas 26:7b67bcbddde3 233 do
Bartas 26:7b67bcbddde3 234 {
Bartas 26:7b67bcbddde3 235 motors.start();
Bartas 26:7b67bcbddde3 236 measureSensors();
Bartas 26:7b67bcbddde3 237 } while (sensorsCheckSum == 204);
Bartas 26:7b67bcbddde3 238 motors.stop();
Bartas 26:7b67bcbddde3 239 }
Bartas 26:7b67bcbddde3 240 if (oldValues[k] == 104) { //right turn 90 situation
Bartas 26:7b67bcbddde3 241 motors.setSteeringMode(PIVOT_CW);
Bartas 26:7b67bcbddde3 242 motors.setRightSpeed(0.1f);
Bartas 26:7b67bcbddde3 243 motors.setLeftSpeed(0.1f);
Bartas 26:7b67bcbddde3 244 do
Bartas 26:7b67bcbddde3 245 {
Bartas 26:7b67bcbddde3 246 motors.start();
Bartas 26:7b67bcbddde3 247 measureSensors();
Bartas 26:7b67bcbddde3 248 } while (sensorsCheckSum != 94);
Bartas 26:7b67bcbddde3 249 motors.stop();
Bartas 26:7b67bcbddde3 250 }
Bartas 24:ecc3fbaf2824 251 break;
Bartas 26:7b67bcbddde3 252 default:
Bartas 24:ecc3fbaf2824 253 measureSensors();
Reckstyle 19:198dc13777eb 254 break;
Bartas 24:ecc3fbaf2824 255 }
Bartas 24:ecc3fbaf2824 256 }
Bartas 26:7b67bcbddde3 257 }
Bartas 26:7b67bcbddde3 258 }
Bartas 26:7b67bcbddde3 259 }
Bartas 26:7b67bcbddde3 260 }