one lap kind of works

Dependencies:   FatFileSystem MSCFileSystem btbee m3pi_ng mbed

Fork of Robot by IESS

Committer:
charwhit
Date:
Wed Jun 10 12:01:21 2015 +0000
Revision:
16:caa77287cc25
Parent:
15:3e3f1b42cc23
Child:
17:5eb563904361
Course code without bluetooth

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charwhit 12:0422156f83f6 1 #include "mbed.h"
charwhit 8:bd2f012e2f57 2 #include "MSCFileSystem.h"
bbabbs 0:17669460c6b1 3 #include "btbee.h"
bbabbs 0:17669460c6b1 4 #include "m3pi_ng.h"
charwhit 8:bd2f012e2f57 5 #include <fstream>
charwhit 8:bd2f012e2f57 6 #define FSNAME "msc"
charwhit 9:030b7e4ff7be 7 #include <string>
charwhit 9:030b7e4ff7be 8 #include <sstream>
charwhit 9:030b7e4ff7be 9 #include <vector>
charwhit 12:0422156f83f6 10 #include "math.h"
charwhit 8:bd2f012e2f57 11
charwhit 8:bd2f012e2f57 12
charwhit 8:bd2f012e2f57 13 MSCFileSystem msc(FSNAME); // Mount flash drive under the name "msc"
charwhit 9:030b7e4ff7be 14 //Serial pc(USBTX,USBRX);
charwhit 8:bd2f012e2f57 15
nbtavis 2:80a1ed62c307 16 m3pi robot;
charwhit 4:acd0f86ed832 17 btbee btbee;
nbtavis 3:bae8eb81a9d7 18 DigitalIn m3pi_IN[]= {(p12),(p21)}; // IR sensor and Knopf
nbtavis 2:80a1ed62c307 19 Timer timer;
nbtavis 2:80a1ed62c307 20 Timer time_wait;
charwhit 16:caa77287cc25 21 #define MAX .9
charwhit 5:c38929c0fd95 22 #define MIN 0
charwhit 12:0422156f83f6 23 #define PI 3.14159265
bbabbs 0:17669460c6b1 24
charwhit 4:acd0f86ed832 25 //#define P_TERM 5
charwhit 4:acd0f86ed832 26 //#define I_TERM 0
charwhit 5:c38929c0fd95 27 //#define D_TERM 20
nbtavis 2:80a1ed62c307 28
nbtavis 2:80a1ed62c307 29
bbabbs 0:17669460c6b1 30
charwhit 5:c38929c0fd95 31 int main()
charwhit 5:c38929c0fd95 32 {
charwhit 16:caa77287cc25 33 float P_TERM = 3;
charwhit 16:caa77287cc25 34 float I_TERM = 0;
charwhit 16:caa77287cc25 35 float D_TERM = 1;
charwhit 5:c38929c0fd95 36
charwhit 5:c38929c0fd95 37 btbee.reset();
charwhit 5:c38929c0fd95 38 robot.sensor_auto_calibrate();
charwhit 5:c38929c0fd95 39 wait(2.0);
charwhit 5:c38929c0fd95 40 float right;
charwhit 5:c38929c0fd95 41 float left;
charwhit 5:c38929c0fd95 42 //float current_pos[5];
charwhit 5:c38929c0fd95 43 float current_pos = 0.0;
charwhit 5:c38929c0fd95 44 float previous_pos =0.0;
charwhit 5:c38929c0fd95 45 float derivative, proportional, integral = 0;
charwhit 5:c38929c0fd95 46 float power;
charwhit 5:c38929c0fd95 47 float speed = MAX;
charwhit 5:c38929c0fd95 48
charwhit 5:c38929c0fd95 49 int lap = 0;
charwhit 5:c38929c0fd95 50 float lap_time = 0.0;
charwhit 5:c38929c0fd95 51 float total_time = 0.0;
charwhit 5:c38929c0fd95 52 float average_time = 0.0;
charwhit 5:c38929c0fd95 53 int y =1;
charwhit 5:c38929c0fd95 54 int count = 0;
charwhit 7:7d491b51665e 55 float paramChange[3];
charwhit 8:bd2f012e2f57 56 bool passed = false;
charwhit 12:0422156f83f6 57 char sweepValue[100];
charwhit 16:caa77287cc25 58
charwhit 16:caa77287cc25 59
charwhit 5:c38929c0fd95 60
charwhit 4:acd0f86ed832 61 char arr_read[30]; // this should be long enough to store any reply coming in over bt.
charwhit 4:acd0f86ed832 62 int chars_read;
charwhit 14:5b3cc7741bdf 63 vector<float> Uvalue, lineposval, setPointVals;
charwhit 12:0422156f83f6 64 vector<float> sweepData;
charwhit 16:caa77287cc25 65 FILE *fp = fopen( "/" FSNAME "/exper1.txt", "w");
charwhit 16:caa77287cc25 66 //FILE *sweep = fopen("/" FSNAME "/sweep15.txt","r");
charwhit 16:caa77287cc25 67
charwhit 16:caa77287cc25 68
charwhit 16:caa77287cc25 69
charwhit 16:caa77287cc25 70 /*if (sweep == NULL) {
charwhit 12:0422156f83f6 71 robot.printf("Nope.");
charwhit 12:0422156f83f6 72 y = 0;
charwhit 15:3e3f1b42cc23 73 }*/
charwhit 9:030b7e4ff7be 74
nbtavis 2:80a1ed62c307 75
charwhit 5:c38929c0fd95 76 /* for (int i = 0; i <5; ++i)
charwhit 5:c38929c0fd95 77 current_pos[i] = 0.0; */
charwhit 16:caa77287cc25 78
charwhit 16:caa77287cc25 79
charwhit 9:030b7e4ff7be 80 //wait(8);
charwhit 6:99d09e88924b 81 btbee.printf("Battery: %f\n", robot.battery());
charwhit 16:caa77287cc25 82
charwhit 16:caa77287cc25 83
charwhit 16:caa77287cc25 84 //btbee.printf("C: %d", sweepData.size());
charwhit 5:c38929c0fd95 85
charwhit 5:c38929c0fd95 86
charwhit 5:c38929c0fd95 87 time_wait.start();
charwhit 16:caa77287cc25 88 int x [5];
charwhit 16:caa77287cc25 89 //timer.start();
charwhit 5:c38929c0fd95 90 while(y) {
charwhit 5:c38929c0fd95 91 time_wait.reset();
charwhit 16:caa77287cc25 92 //timer.reset();
charwhit 5:c38929c0fd95 93 //Get raw sensor values
charwhit 16:caa77287cc25 94
charwhit 5:c38929c0fd95 95 robot.calibrated_sensor(x);
charwhit 5:c38929c0fd95 96
charwhit 5:c38929c0fd95 97
charwhit 5:c38929c0fd95 98
charwhit 5:c38929c0fd95 99 //Check to make sure battery isn't low
charwhit 5:c38929c0fd95 100 if (robot.battery() < 2.4) {
charwhit 5:c38929c0fd95 101 timer.stop();
charwhit 16:caa77287cc25 102 robot.printf("LowBatt");
charwhit 6:99d09e88924b 103 btbee.printf("Battery too low\n");
charwhit 5:c38929c0fd95 104 break;
charwhit 5:c38929c0fd95 105 }
charwhit 5:c38929c0fd95 106
charwhit 5:c38929c0fd95 107 //else if (m3pi_IN [0] == 0)
charwhit 5:c38929c0fd95 108 //{break;}
charwhit 16:caa77287cc25 109
charwhit 14:5b3cc7741bdf 110 if( (x[0] > 300 && x[2]>300 && x[4]>300 & !passed)) {
charwhit 5:c38929c0fd95 111 if (lap == 0) {
charwhit 8:bd2f012e2f57 112 /*while( x[0]> 300 && x[4] > 300) {
charwhit 5:c38929c0fd95 113 robot.calibrated_sensor(x);
charwhit 8:bd2f012e2f57 114 }*/
charwhit 5:c38929c0fd95 115 timer.start();
charwhit 5:c38929c0fd95 116 lap= lap +1;
charwhit 5:c38929c0fd95 117 }
charwhit 5:c38929c0fd95 118
charwhit 14:5b3cc7741bdf 119 else if (lap == 1) {
charwhit 16:caa77287cc25 120
charwhit 6:99d09e88924b 121 robot.stop();
charwhit 16:caa77287cc25 122
charwhit 16:caa77287cc25 123 //Print to file stuff
charwhit 16:caa77287cc25 124 /*if(fp != NULL) {
charwhit 12:0422156f83f6 125 for (int i = 0; i < Uvalue.size(); ++i)
charwhit 14:5b3cc7741bdf 126 fprintf(fp,"%f %f %f\n", setPointVals[i], Uvalue[i], lineposval[i]);
charwhit 10:83dfbc5e93ed 127 fclose(fp);
charwhit 15:3e3f1b42cc23 128 //fclose(sweep);
charwhit 10:83dfbc5e93ed 129 robot.cls();
charwhit 10:83dfbc5e93ed 130 robot.printf("Doner");
charwhit 11:bb90691e5166 131 }
charwhit 16:caa77287cc25 132 break;*/
charwhit 5:c38929c0fd95 133 lap_time = timer.read();
charwhit 5:c38929c0fd95 134 total_time += lap_time;
charwhit 5:c38929c0fd95 135 average_time = total_time/lap;
charwhit 5:c38929c0fd95 136 robot.printf("%f",average_time);
charwhit 5:c38929c0fd95 137 if (btbee.writeable()) {
charwhit 5:c38929c0fd95 138 btbee.printf("Lap %d time: %f\n", lap, lap_time);
charwhit 5:c38929c0fd95 139 btbee.printf("Avg Lap time: %f\n", average_time);
charwhit 5:c38929c0fd95 140 }
charwhit 16:caa77287cc25 141
charwhit 16:caa77287cc25 142 /*while (count < 3) {
charwhit 5:c38929c0fd95 143 btbee.read_line(arr_read, 30, &chars_read);
charwhit 7:7d491b51665e 144 paramChange[count] = atof(arr_read);
charwhit 5:c38929c0fd95 145 count++;
charwhit 5:c38929c0fd95 146 }
charwhit 5:c38929c0fd95 147 P_TERM = paramChange[0];
charwhit 5:c38929c0fd95 148 I_TERM = paramChange[1];
charwhit 5:c38929c0fd95 149 D_TERM = paramChange[2];
charwhit 6:99d09e88924b 150 btbee.printf("PTERM %f\n", P_TERM);
charwhit 6:99d09e88924b 151 btbee.printf("ITERM %f\n", I_TERM);
charwhit 6:99d09e88924b 152 btbee.printf("DTERM %f\n", D_TERM);
charwhit 5:c38929c0fd95 153 lap = 0;
charwhit 5:c38929c0fd95 154 total_time = 0;
charwhit 5:c38929c0fd95 155 count = 0;
charwhit 6:99d09e88924b 156 timer.stop();
charwhit 6:99d09e88924b 157 timer.reset();
charwhit 16:caa77287cc25 158 continue;*/
charwhit 16:caa77287cc25 159 break;
charwhit 16:caa77287cc25 160
charwhit 5:c38929c0fd95 161 } else {
charwhit 8:bd2f012e2f57 162 /*while( x[0]> 300 && x[4] > 300) {
charwhit 5:c38929c0fd95 163 robot.calibrated_sensor(x);
charwhit 8:bd2f012e2f57 164 }*/
charwhit 5:c38929c0fd95 165 lap_time = timer.read();
charwhit 5:c38929c0fd95 166 if (btbee.writeable()) {
charwhit 5:c38929c0fd95 167 btbee.printf("Lap %d time: %f\n", lap, lap_time);
charwhit 5:c38929c0fd95 168 }
charwhit 5:c38929c0fd95 169 total_time += lap_time;
charwhit 5:c38929c0fd95 170 average_time = total_time/lap;
charwhit 5:c38929c0fd95 171 lap = lap +1;
charwhit 16:caa77287cc25 172 timer.reset();
charwhit 5:c38929c0fd95 173 }
charwhit 8:bd2f012e2f57 174 passed = true;
charwhit 16:caa77287cc25 175 } else if (x[0] > 300 && x[2]>300 && x[4]>300)
charwhit 8:bd2f012e2f57 176 passed = true;
charwhit 8:bd2f012e2f57 177 else
charwhit 8:bd2f012e2f57 178 passed = false;
charwhit 16:caa77287cc25 179
charwhit 5:c38929c0fd95 180
nbtavis 2:80a1ed62c307 181
charwhit 5:c38929c0fd95 182 // Get the position of the line.
charwhit 5:c38929c0fd95 183 /* for (int i =0; i < 4; ++i)
charwhit 5:c38929c0fd95 184 current_pos[i] = current_pos[i+1];
charwhit 5:c38929c0fd95 185 current_pos[4] = robot.line_position();
charwhit 5:c38929c0fd95 186 proportional = current_pos[4];
charwhit 5:c38929c0fd95 187
charwhit 5:c38929c0fd95 188 // compute the derivative
charwhit 5:c38929c0fd95 189 derivative = 0;
charwhit 5:c38929c0fd95 190 for (int i =1; i<5;++i) {
charwhit 5:c38929c0fd95 191 if (i ==1)
charwhit 5:c38929c0fd95 192 derivative += 0*(current_pos[i] - current_pos[i-1]);
charwhit 5:c38929c0fd95 193 else if (i == 2)
charwhit 5:c38929c0fd95 194 derivative += 0*(current_pos[i] - current_pos[i-1]);
charwhit 5:c38929c0fd95 195 else if (i==3)
charwhit 5:c38929c0fd95 196 derivative += 0*(current_pos[i] - current_pos[i-1]);
charwhit 5:c38929c0fd95 197 else
charwhit 5:c38929c0fd95 198 derivative += (current_pos[i] - current_pos[i-1]);
charwhit 5:c38929c0fd95 199 }
charwhit 5:c38929c0fd95 200
charwhit 5:c38929c0fd95 201 derivative = derivative; */
charwhit 5:c38929c0fd95 202
charwhit 5:c38929c0fd95 203
charwhit 5:c38929c0fd95 204 current_pos = robot.line_position();
charwhit 16:caa77287cc25 205
charwhit 16:caa77287cc25 206 //Performs the sweep
charwhit 16:caa77287cc25 207 /*if (fgets(sweepValue, 100, sweep) != NULL){
charwhit 16:caa77287cc25 208 proportional = atof(sweepValue) + current_pos;
charwhit 16:caa77287cc25 209 }
charwhit 16:caa77287cc25 210 else {
charwhit 16:caa77287cc25 211 robot.stop();
charwhit 16:caa77287cc25 212 //fclose(sweep);
charwhit 16:caa77287cc25 213 if(fp != NULL) {
charwhit 16:caa77287cc25 214 for (int i = 0; i < Uvalue.size(); ++i)
charwhit 16:caa77287cc25 215 fprintf(fp,"%f %f %f\n", setPointVals[i], Uvalue[i], lineposval[i]);
charwhit 16:caa77287cc25 216 fclose(fp);
charwhit 16:caa77287cc25 217 fclose(sweep);
charwhit 16:caa77287cc25 218 robot.cls();
charwhit 16:caa77287cc25 219 robot.printf("Doner");
charwhit 16:caa77287cc25 220 }
charwhit 16:caa77287cc25 221 break;
charwhit 16:caa77287cc25 222 //FILE *sweep = fopen("/" FSNAME "/sweep15.txt","r");
charwhit 16:caa77287cc25 223 //robot.cls();
charwhit 16:caa77287cc25 224 //robot.printf("Done.");
charwhit 16:caa77287cc25 225 // break;
charwhit 15:3e3f1b42cc23 226 }*/
charwhit 16:caa77287cc25 227
charwhit 16:caa77287cc25 228 proportional = current_pos;
charwhit 16:caa77287cc25 229
charwhit 16:caa77287cc25 230
charwhit 5:c38929c0fd95 231
charwhit 5:c38929c0fd95 232 derivative = current_pos - previous_pos;
charwhit 5:c38929c0fd95 233
charwhit 5:c38929c0fd95 234
charwhit 5:c38929c0fd95 235 //compute the integral
charwhit 5:c38929c0fd95 236 integral =+ proportional;
charwhit 5:c38929c0fd95 237
charwhit 5:c38929c0fd95 238 //remember the last position.
charwhit 5:c38929c0fd95 239 previous_pos = current_pos;
charwhit 5:c38929c0fd95 240
charwhit 5:c38929c0fd95 241 // compute the power
charwhit 5:c38929c0fd95 242 power = (proportional*(P_TERM)) + (integral*(I_TERM)) + (derivative*(D_TERM));
charwhit 16:caa77287cc25 243 /*if (fgets(sweepValue, 100, sweep) != NULL){
charwhit 16:caa77287cc25 244 power = atof(sweepValue) + power;
charwhit 16:caa77287cc25 245 }
charwhit 16:caa77287cc25 246 else {
charwhit 16:caa77287cc25 247 robot.stop();
charwhit 16:caa77287cc25 248 fclose(sweep);
charwhit 16:caa77287cc25 249 if(fp != NULL) {
charwhit 16:caa77287cc25 250 for (int i = 0; i < Uvalue.size(); ++i)
charwhit 16:caa77287cc25 251 fprintf(fp,"%f %f %f\n", setPointVals[i], Uvalue[i], lineposval[i]);
charwhit 16:caa77287cc25 252 fclose(fp);
charwhit 16:caa77287cc25 253 fclose(sweep);
charwhit 16:caa77287cc25 254 robot.cls();
charwhit 16:caa77287cc25 255 robot.printf("Doner");
charwhit 16:caa77287cc25 256 }
charwhit 16:caa77287cc25 257 break;
charwhit 16:caa77287cc25 258 }*/
charwhit 5:c38929c0fd95 259 //computer new speeds
charwhit 5:c38929c0fd95 260 right = speed+power;
charwhit 5:c38929c0fd95 261 left = speed-power;
charwhit 5:c38929c0fd95 262
charwhit 5:c38929c0fd95 263 //limit checks
charwhit 5:c38929c0fd95 264 if(right<MIN)
charwhit 5:c38929c0fd95 265 right = MIN;
charwhit 5:c38929c0fd95 266 else if (right > MAX)
charwhit 5:c38929c0fd95 267 right = MAX;
charwhit 5:c38929c0fd95 268
charwhit 5:c38929c0fd95 269 if(left<MIN)
charwhit 5:c38929c0fd95 270 left = MIN;
charwhit 5:c38929c0fd95 271 else if (left>MIN)
charwhit 5:c38929c0fd95 272 left = MAX;
charwhit 5:c38929c0fd95 273
charwhit 5:c38929c0fd95 274 //set speed
charwhit 5:c38929c0fd95 275
charwhit 5:c38929c0fd95 276 robot.left_motor(left);
charwhit 5:c38929c0fd95 277 robot.right_motor(right);
charwhit 16:caa77287cc25 278
charwhit 16:caa77287cc25 279
charwhit 16:caa77287cc25 280
charwhit 16:caa77287cc25 281 Uvalue.push_back(power);
charwhit 16:caa77287cc25 282 lineposval.push_back(robot.line_position());
charwhit 16:caa77287cc25 283 setPointVals.push_back(0);
charwhit 16:caa77287cc25 284 //setPointVals.push_back(0.5);
charwhit 16:caa77287cc25 285
charwhit 16:caa77287cc25 286
charwhit 16:caa77287cc25 287
charwhit 5:c38929c0fd95 288 wait((5-time_wait.read_ms())/1000);
bbabbs 0:17669460c6b1 289 }
charwhit 5:c38929c0fd95 290
charwhit 5:c38929c0fd95 291
charwhit 5:c38929c0fd95 292
charwhit 5:c38929c0fd95 293 robot.stop();
charwhit 5:c38929c0fd95 294
charwhit 16:caa77287cc25 295 /*char hail[]= {'V','1','5','O','4','E','4','C','8','D','8','E','8','C','8','D','8'
charwhit 5:c38929c0fd95 296 ,'E','8','F','4','D','8','E','8','F','8','D','8','E','8','F','8','G'
charwhit 5:c38929c0fd95 297 ,'4','A','4','E','1','6','E','1','6','F','8','C','8','D','8','E','8'
charwhit 5:c38929c0fd95 298 ,'G','8','E','8','D','8','C','4'
charwhit 5:c38929c0fd95 299 };
nbtavis 2:80a1ed62c307 300 int numb = 59;
charwhit 5:c38929c0fd95 301
charwhit 16:caa77287cc25 302 robot.playtune(hail,numb);*/
charwhit 5:c38929c0fd95 303
charwhit 5:c38929c0fd95 304
charwhit 5:c38929c0fd95 305
charwhit 5:c38929c0fd95 306
charwhit 5:c38929c0fd95 307 }