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