This ILC code kinda works
Dependencies: FatFileSystem MSCFileSystem btbee m3pi_ng mbed ILCRobot
Fork of RobotB by
main.cpp@21:df402d79b2ad, 2015-06-12 (annotated)
- Committer:
- charwhit
- Date:
- Fri Jun 12 09:52:22 2015 +0000
- Revision:
- 21:df402d79b2ad
- Parent:
- 20:262739736935
- Child:
- 22:1288bd38d682
This works!
Who changed what in which revision?
User | Revision | Line number | New 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 | 20:262739736935 | 11 | #include <algorithm> |
charwhit | 8:bd2f012e2f57 | 12 | |
charwhit | 8:bd2f012e2f57 | 13 | |
charwhit | 8:bd2f012e2f57 | 14 | MSCFileSystem msc(FSNAME); // Mount flash drive under the name "msc" |
charwhit | 9:030b7e4ff7be | 15 | //Serial pc(USBTX,USBRX); |
charwhit | 8:bd2f012e2f57 | 16 | |
nbtavis | 2:80a1ed62c307 | 17 | m3pi robot; |
charwhit | 4:acd0f86ed832 | 18 | btbee btbee; |
nbtavis | 3:bae8eb81a9d7 | 19 | DigitalIn m3pi_IN[]= {(p12),(p21)}; // IR sensor and Knopf |
charwhit | 20:262739736935 | 20 | DigitalIn m3pi_pb(p21); |
nbtavis | 2:80a1ed62c307 | 21 | Timer timer; |
nbtavis | 2:80a1ed62c307 | 22 | Timer time_wait; |
charwhit | 21:df402d79b2ad | 23 | #define MAX 1 |
charwhit | 5:c38929c0fd95 | 24 | #define MIN 0 |
charwhit | 12:0422156f83f6 | 25 | #define PI 3.14159265 |
bbabbs | 0:17669460c6b1 | 26 | |
charwhit | 4:acd0f86ed832 | 27 | //#define P_TERM 5 |
charwhit | 4:acd0f86ed832 | 28 | //#define I_TERM 0 |
charwhit | 5:c38929c0fd95 | 29 | //#define D_TERM 20 |
nbtavis | 2:80a1ed62c307 | 30 | |
nbtavis | 2:80a1ed62c307 | 31 | |
charwhit | 20:262739736935 | 32 | struct testPoint { |
charwhit | 20:262739736935 | 33 | testPoint():Pvalue(0), Dvalue(0), time(1000) {} |
charwhit | 20:262739736935 | 34 | testPoint(float a, float b, float c): Pvalue(a), Dvalue(b), time(c) {} |
charwhit | 20:262739736935 | 35 | |
charwhit | 20:262739736935 | 36 | friend testPoint operator-(const testPoint& lhs, const testPoint& rhs) { |
charwhit | 20:262739736935 | 37 | testPoint dummy; |
charwhit | 20:262739736935 | 38 | dummy.Pvalue = lhs.Pvalue - rhs.Pvalue; |
charwhit | 20:262739736935 | 39 | dummy.Dvalue = lhs.Dvalue - rhs.Dvalue; |
charwhit | 20:262739736935 | 40 | dummy.time = 1000; |
charwhit | 20:262739736935 | 41 | return dummy; |
charwhit | 20:262739736935 | 42 | } |
charwhit | 20:262739736935 | 43 | |
charwhit | 20:262739736935 | 44 | friend testPoint operator+(const testPoint& lhs, const testPoint& rhs) { |
charwhit | 20:262739736935 | 45 | testPoint dummy; |
charwhit | 20:262739736935 | 46 | dummy.Pvalue = lhs.Pvalue + rhs.Pvalue; |
charwhit | 20:262739736935 | 47 | dummy.Dvalue = lhs.Dvalue + rhs.Dvalue; |
charwhit | 20:262739736935 | 48 | dummy.time = 1000; |
charwhit | 20:262739736935 | 49 | return dummy; |
charwhit | 20:262739736935 | 50 | } |
charwhit | 20:262739736935 | 51 | |
charwhit | 20:262739736935 | 52 | friend testPoint operator*(const float a, const testPoint& rhs) { |
charwhit | 20:262739736935 | 53 | testPoint dummy; |
charwhit | 20:262739736935 | 54 | dummy.Pvalue = a * rhs.Pvalue; |
charwhit | 20:262739736935 | 55 | dummy.Dvalue = a * rhs.Dvalue; |
charwhit | 20:262739736935 | 56 | dummy.time = 1000; |
charwhit | 20:262739736935 | 57 | return dummy; |
charwhit | 20:262739736935 | 58 | |
charwhit | 20:262739736935 | 59 | } |
charwhit | 20:262739736935 | 60 | |
charwhit | 20:262739736935 | 61 | |
charwhit | 20:262739736935 | 62 | |
charwhit | 20:262739736935 | 63 | float Pvalue; |
charwhit | 20:262739736935 | 64 | float Dvalue; |
charwhit | 20:262739736935 | 65 | float time; |
charwhit | 20:262739736935 | 66 | }; |
charwhit | 20:262739736935 | 67 | |
charwhit | 20:262739736935 | 68 | |
charwhit | 20:262739736935 | 69 | bool myComp (testPoint i,testPoint j) |
charwhit | 20:262739736935 | 70 | { |
charwhit | 20:262739736935 | 71 | return (i.time<j.time); |
charwhit | 20:262739736935 | 72 | } |
charwhit | 20:262739736935 | 73 | |
charwhit | 20:262739736935 | 74 | |
charwhit | 20:262739736935 | 75 | |
bbabbs | 0:17669460c6b1 | 76 | |
charwhit | 5:c38929c0fd95 | 77 | int main() |
charwhit | 5:c38929c0fd95 | 78 | { |
charwhit | 16:caa77287cc25 | 79 | float P_TERM = 3; |
charwhit | 16:caa77287cc25 | 80 | float I_TERM = 0; |
bbabbs | 17:5eb563904361 | 81 | float D_TERM = 27; |
charwhit | 20:262739736935 | 82 | //int last_lap = 0; |
charwhit | 20:262739736935 | 83 | //int position = 0; |
charwhit | 20:262739736935 | 84 | |
charwhit | 20:262739736935 | 85 | m3pi_pb.mode(PullUp); |
charwhit | 5:c38929c0fd95 | 86 | btbee.reset(); |
charwhit | 20:262739736935 | 87 | |
charwhit | 20:262739736935 | 88 | |
charwhit | 5:c38929c0fd95 | 89 | robot.sensor_auto_calibrate(); |
charwhit | 20:262739736935 | 90 | while(m3pi_pb) { |
charwhit | 20:262739736935 | 91 | |
charwhit | 20:262739736935 | 92 | } |
charwhit | 5:c38929c0fd95 | 93 | wait(2.0); |
charwhit | 5:c38929c0fd95 | 94 | float right; |
charwhit | 5:c38929c0fd95 | 95 | float left; |
charwhit | 5:c38929c0fd95 | 96 | //float current_pos[5]; |
charwhit | 5:c38929c0fd95 | 97 | float current_pos = 0.0; |
charwhit | 5:c38929c0fd95 | 98 | float previous_pos =0.0; |
charwhit | 5:c38929c0fd95 | 99 | float derivative, proportional, integral = 0; |
charwhit | 5:c38929c0fd95 | 100 | float power; |
charwhit | 5:c38929c0fd95 | 101 | float speed = MAX; |
charwhit | 5:c38929c0fd95 | 102 | |
charwhit | 5:c38929c0fd95 | 103 | int lap = 0; |
charwhit | 5:c38929c0fd95 | 104 | float lap_time = 0.0; |
charwhit | 5:c38929c0fd95 | 105 | int y =1; |
charwhit | 5:c38929c0fd95 | 106 | int count = 0; |
charwhit | 20:262739736935 | 107 | |
charwhit | 8:bd2f012e2f57 | 108 | bool passed = false; |
charwhit | 20:262739736935 | 109 | |
charwhit | 20:262739736935 | 110 | Timer crossProtect; |
charwhit | 20:262739736935 | 111 | bool poop = false; //Random variable so that it doesn't print 50 gagillion times |
charwhit | 20:262739736935 | 112 | |
charwhit | 20:262739736935 | 113 | |
charwhit | 20:262739736935 | 114 | //Simplex algorithm stuff |
charwhit | 20:262739736935 | 115 | vector< testPoint > triangle; |
charwhit | 20:262739736935 | 116 | triangle.push_back(testPoint(20,2,1000)); |
charwhit | 20:262739736935 | 117 | triangle.push_back(testPoint(2,40,1000)); |
charwhit | 20:262739736935 | 118 | triangle.push_back(testPoint(3,27,1000)); |
charwhit | 20:262739736935 | 119 | int corners = 0; |
charwhit | 20:262739736935 | 120 | P_TERM = triangle[0].Pvalue; |
charwhit | 20:262739736935 | 121 | D_TERM = triangle[0].Dvalue; |
charwhit | 20:262739736935 | 122 | testPoint centroid; |
charwhit | 20:262739736935 | 123 | testPoint reflection; |
charwhit | 20:262739736935 | 124 | testPoint expansion; |
charwhit | 20:262739736935 | 125 | testPoint contraction; |
charwhit | 20:262739736935 | 126 | testPoint reduction; |
charwhit | 20:262739736935 | 127 | int step = 1; |
charwhit | 20:262739736935 | 128 | bool contracted = false; |
charwhit | 20:262739736935 | 129 | float reflecFactor = 1; |
charwhit | 20:262739736935 | 130 | float expanFactor = 2; |
charwhit | 20:262739736935 | 131 | float contrFactor = -1/2; |
charwhit | 20:262739736935 | 132 | float reducFactor = 1/2; |
charwhit | 20:262739736935 | 133 | Timer backup; |
charwhit | 21:df402d79b2ad | 134 | int MINPVAL = 2; |
charwhit | 21:df402d79b2ad | 135 | int MAXPVAL = 30; |
charwhit | 21:df402d79b2ad | 136 | int MAXDVAL= 50; |
charwhit | 16:caa77287cc25 | 137 | |
charwhit | 16:caa77287cc25 | 138 | |
charwhit | 5:c38929c0fd95 | 139 | |
charwhit | 4:acd0f86ed832 | 140 | char arr_read[30]; // this should be long enough to store any reply coming in over bt. |
charwhit | 4:acd0f86ed832 | 141 | int chars_read; |
bbabbs | 17:5eb563904361 | 142 | vector<float> Uvalue, lineposval, setPointVals, Currentvec; |
charwhit | 12:0422156f83f6 | 143 | vector<float> sweepData; |
charwhit | 16:caa77287cc25 | 144 | FILE *fp = fopen( "/" FSNAME "/exper1.txt", "w"); |
charwhit | 16:caa77287cc25 | 145 | |
charwhit | 16:caa77287cc25 | 146 | |
charwhit | 16:caa77287cc25 | 147 | |
charwhit | 20:262739736935 | 148 | //wait(8); |
charwhit | 20:262739736935 | 149 | // btbee.printf("Battery: %f\n", robot.battery()); |
charwhit | 9:030b7e4ff7be | 150 | |
nbtavis | 2:80a1ed62c307 | 151 | |
charwhit | 16:caa77287cc25 | 152 | |
charwhit | 16:caa77287cc25 | 153 | |
charwhit | 5:c38929c0fd95 | 154 | |
charwhit | 5:c38929c0fd95 | 155 | time_wait.start(); |
charwhit | 20:262739736935 | 156 | backup.start(); |
charwhit | 16:caa77287cc25 | 157 | int x [5]; |
charwhit | 16:caa77287cc25 | 158 | //timer.start(); |
charwhit | 5:c38929c0fd95 | 159 | while(y) { |
charwhit | 5:c38929c0fd95 | 160 | time_wait.reset(); |
charwhit | 16:caa77287cc25 | 161 | //timer.reset(); |
charwhit | 5:c38929c0fd95 | 162 | //Get raw sensor values |
charwhit | 16:caa77287cc25 | 163 | |
charwhit | 5:c38929c0fd95 | 164 | robot.calibrated_sensor(x); |
charwhit | 5:c38929c0fd95 | 165 | |
charwhit | 5:c38929c0fd95 | 166 | |
charwhit | 5:c38929c0fd95 | 167 | |
charwhit | 5:c38929c0fd95 | 168 | //Check to make sure battery isn't low |
charwhit | 5:c38929c0fd95 | 169 | if (robot.battery() < 2.4) { |
charwhit | 5:c38929c0fd95 | 170 | timer.stop(); |
charwhit | 16:caa77287cc25 | 171 | robot.printf("LowBatt"); |
charwhit | 6:99d09e88924b | 172 | btbee.printf("Battery too low\n"); |
charwhit | 5:c38929c0fd95 | 173 | break; |
charwhit | 5:c38929c0fd95 | 174 | } |
charwhit | 5:c38929c0fd95 | 175 | |
charwhit | 5:c38929c0fd95 | 176 | //else if (m3pi_IN [0] == 0) |
charwhit | 5:c38929c0fd95 | 177 | //{break;} |
charwhit | 16:caa77287cc25 | 178 | |
charwhit | 20:262739736935 | 179 | //running the four corners |
charwhit | 20:262739736935 | 180 | if(x[0] > 300 && x[2]>300 && x[4]>300 && !passed && (backup.read() > 2 || lap == 0)) { |
charwhit | 20:262739736935 | 181 | btbee.printf("Crossing\n"); |
charwhit | 5:c38929c0fd95 | 182 | if (lap == 0) { |
charwhit | 5:c38929c0fd95 | 183 | timer.start(); |
charwhit | 20:262739736935 | 184 | //crossProtect.start(); |
charwhit | 5:c38929c0fd95 | 185 | lap= lap +1; |
charwhit | 20:262739736935 | 186 | } else if(corners < 2) { |
charwhit | 5:c38929c0fd95 | 187 | lap_time = timer.read(); |
charwhit | 20:262739736935 | 188 | triangle[corners].time = lap_time; |
charwhit | 20:262739736935 | 189 | //if (btbee.writeable()) { |
charwhit | 20:262739736935 | 190 | btbee.printf("Lap %d time: %f\n", lap, lap_time); |
charwhit | 20:262739736935 | 191 | //} |
charwhit | 20:262739736935 | 192 | |
charwhit | 20:262739736935 | 193 | ++corners; |
bbabbs | 17:5eb563904361 | 194 | robot.cls(); |
charwhit | 20:262739736935 | 195 | robot.printf("%f", lap_time); |
charwhit | 20:262739736935 | 196 | |
charwhit | 20:262739736935 | 197 | ++lap; |
charwhit | 20:262739736935 | 198 | P_TERM = triangle[corners].Pvalue; |
charwhit | 20:262739736935 | 199 | D_TERM = triangle[corners].Dvalue; |
charwhit | 18:dcdf28fa4c9e | 200 | timer.reset(); |
charwhit | 20:262739736935 | 201 | } else { |
charwhit | 20:262739736935 | 202 | if (corners == 2){ |
charwhit | 20:262739736935 | 203 | triangle[corners].time = timer.read(); |
charwhit | 20:262739736935 | 204 | ++corners; |
charwhit | 20:262739736935 | 205 | } |
charwhit | 20:262739736935 | 206 | switch(step) { |
charwhit | 20:262739736935 | 207 | case 1: |
charwhit | 20:262739736935 | 208 | if (triangle[1].time == 1000 || triangle[2].time == 1000){ |
charwhit | 20:262739736935 | 209 | if (triangle[1].time == 1000){ |
charwhit | 20:262739736935 | 210 | triangle[1].time = timer.read(); |
charwhit | 20:262739736935 | 211 | P_TERM = triangle[2].Pvalue; |
charwhit | 20:262739736935 | 212 | D_TERM = triangle[2].Dvalue; |
charwhit | 20:262739736935 | 213 | timer.reset(); |
charwhit | 20:262739736935 | 214 | break; |
charwhit | 20:262739736935 | 215 | } |
charwhit | 20:262739736935 | 216 | else |
charwhit | 20:262739736935 | 217 | triangle[2].time = timer.read(); |
charwhit | 20:262739736935 | 218 | } |
charwhit | 20:262739736935 | 219 | sort(triangle.begin(), triangle.end(), myComp); |
charwhit | 20:262739736935 | 220 | for (int i = 0; i < triangle.size(); ++i) |
charwhit | 20:262739736935 | 221 | btbee.printf("Time %d: %f\n",i,triangle[i].time); |
charwhit | 21:df402d79b2ad | 222 | btbee.printf("Best so far: P: %f, D: %f\n", triangle[0].Pvalue, triangle[0].Dvalue); |
charwhit | 20:262739736935 | 223 | centroid.Pvalue = (triangle[0].Pvalue + triangle[1].Pvalue) / 2; |
charwhit | 20:262739736935 | 224 | centroid.Dvalue = (triangle[0].Dvalue + triangle[1].Dvalue) / 2; |
charwhit | 20:262739736935 | 225 | reflection = centroid + (reflecFactor*(centroid - triangle[2])); |
charwhit | 20:262739736935 | 226 | if (reflection.Pvalue < 0) |
charwhit | 21:df402d79b2ad | 227 | reflection.Pvalue = MINPVAL; |
charwhit | 20:262739736935 | 228 | else if (reflection.Pvalue > 30) |
charwhit | 21:df402d79b2ad | 229 | reflection.Pvalue = MAXPVAL; |
charwhit | 20:262739736935 | 230 | |
charwhit | 20:262739736935 | 231 | if (reflection.Dvalue < 0) |
charwhit | 20:262739736935 | 232 | reflection.Dvalue = 1; |
charwhit | 21:df402d79b2ad | 233 | else if (reflection.Dvalue > 40) |
charwhit | 21:df402d79b2ad | 234 | reflection.Dvalue = MAXDVAL; |
charwhit | 20:262739736935 | 235 | P_TERM = reflection.Pvalue; |
charwhit | 20:262739736935 | 236 | D_TERM = reflection.Dvalue; |
charwhit | 20:262739736935 | 237 | ++step; |
charwhit | 20:262739736935 | 238 | btbee.printf("Reflecting\n"); |
charwhit | 20:262739736935 | 239 | btbee.printf("P: %f, D: %f\n", P_TERM, D_TERM); |
charwhit | 20:262739736935 | 240 | poop = true; |
charwhit | 20:262739736935 | 241 | timer.reset(); |
charwhit | 20:262739736935 | 242 | break; |
charwhit | 20:262739736935 | 243 | case 2: //finished running reflection |
charwhit | 20:262739736935 | 244 | reflection.time = timer.read(); |
charwhit | 20:262739736935 | 245 | btbee.printf("Reflection Time: %f\n", reflection.time); |
charwhit | 20:262739736935 | 246 | if (reflection.time < triangle[0].time) { |
charwhit | 20:262739736935 | 247 | expansion = centroid + expanFactor*(centroid - triangle[2]); |
charwhit | 20:262739736935 | 248 | if (expansion.Pvalue < 0) |
charwhit | 21:df402d79b2ad | 249 | expansion.Pvalue = MINPVAL; |
charwhit | 20:262739736935 | 250 | else if (expansion.Pvalue > 30) |
charwhit | 21:df402d79b2ad | 251 | expansion.Pvalue = MAXPVAL; |
charwhit | 20:262739736935 | 252 | |
charwhit | 20:262739736935 | 253 | if (expansion.Dvalue < 0) |
charwhit | 20:262739736935 | 254 | expansion.Dvalue = 1; |
charwhit | 21:df402d79b2ad | 255 | else if (expansion.Dvalue > 40) |
charwhit | 21:df402d79b2ad | 256 | expansion.Dvalue = MAXDVAL; |
charwhit | 20:262739736935 | 257 | P_TERM = expansion.Pvalue; |
charwhit | 20:262739736935 | 258 | D_TERM = expansion.Dvalue; |
charwhit | 20:262739736935 | 259 | ++step; |
charwhit | 20:262739736935 | 260 | btbee.printf("Expanding\n"); |
charwhit | 20:262739736935 | 261 | btbee.printf("P: %f, D: %f\n", P_TERM, D_TERM); |
charwhit | 20:262739736935 | 262 | timer.reset(); |
charwhit | 20:262739736935 | 263 | } else if (reflection.time < triangle[1].time) { |
charwhit | 20:262739736935 | 264 | triangle[2] = reflection; |
charwhit | 20:262739736935 | 265 | step = 1; |
charwhit | 20:262739736935 | 266 | btbee.printf("Going back to 1\n"); |
charwhit | 20:262739736935 | 267 | P_TERM = 3; |
charwhit | 20:262739736935 | 268 | D_TERM = 27; |
charwhit | 20:262739736935 | 269 | } else { |
charwhit | 20:262739736935 | 270 | |
charwhit | 20:262739736935 | 271 | contraction = centroid + contrFactor*(centroid - triangle[2]); |
charwhit | 20:262739736935 | 272 | if (contraction.Pvalue < 0) |
charwhit | 21:df402d79b2ad | 273 | contraction.Pvalue = MINPVAL; |
charwhit | 20:262739736935 | 274 | else if (contraction.Pvalue > 30) |
charwhit | 21:df402d79b2ad | 275 | contraction.Pvalue = MAXPVAL; |
charwhit | 20:262739736935 | 276 | |
charwhit | 20:262739736935 | 277 | if (contraction.Dvalue < 0) |
charwhit | 20:262739736935 | 278 | contraction.Dvalue = 1; |
charwhit | 21:df402d79b2ad | 279 | else if (contraction.Dvalue > 40) |
charwhit | 21:df402d79b2ad | 280 | contraction.Dvalue = MAXDVAL; |
charwhit | 20:262739736935 | 281 | P_TERM = contraction.Pvalue; |
charwhit | 20:262739736935 | 282 | D_TERM = contraction.Dvalue; |
charwhit | 20:262739736935 | 283 | ++step; |
charwhit | 20:262739736935 | 284 | contracted = true; |
charwhit | 20:262739736935 | 285 | btbee.printf("Contracting\n"); |
charwhit | 20:262739736935 | 286 | btbee.printf("P: %f, D: %f\n", P_TERM, D_TERM); |
charwhit | 20:262739736935 | 287 | timer.reset(); |
charwhit | 20:262739736935 | 288 | } |
charwhit | 20:262739736935 | 289 | break; |
charwhit | 20:262739736935 | 290 | case 3: //Finished running Expansion/Contraction |
charwhit | 20:262739736935 | 291 | if (!contracted) { |
charwhit | 20:262739736935 | 292 | expansion.time = timer.read(); |
charwhit | 20:262739736935 | 293 | btbee.printf("Expansion Time: %f\n", expansion.time); |
charwhit | 20:262739736935 | 294 | if (expansion.time < reflection.time) |
charwhit | 20:262739736935 | 295 | triangle[2] = expansion; |
charwhit | 20:262739736935 | 296 | else |
charwhit | 20:262739736935 | 297 | triangle[2] = reflection; |
charwhit | 20:262739736935 | 298 | P_TERM = 3; |
charwhit | 20:262739736935 | 299 | D_TERM = 27; |
charwhit | 20:262739736935 | 300 | } else { |
charwhit | 20:262739736935 | 301 | contraction.time = timer.read(); |
charwhit | 20:262739736935 | 302 | btbee.printf("Contraction Time: %f\n", contraction.time); |
charwhit | 20:262739736935 | 303 | if (contraction.time < triangle[2].time){ |
charwhit | 20:262739736935 | 304 | triangle[2] = contraction; |
charwhit | 20:262739736935 | 305 | P_TERM = 3; |
charwhit | 20:262739736935 | 306 | D_TERM = 27; |
charwhit | 20:262739736935 | 307 | } |
charwhit | 20:262739736935 | 308 | else { |
charwhit | 20:262739736935 | 309 | triangle[1] = triangle[0] + (reducFactor*(triangle[1] - triangle[0])); |
charwhit | 20:262739736935 | 310 | triangle[2] = triangle[0] + (reducFactor*(triangle[2] - triangle[0])); |
charwhit | 20:262739736935 | 311 | P_TERM = triangle[1].Pvalue; |
charwhit | 20:262739736935 | 312 | D_TERM = triangle[1].Dvalue; |
charwhit | 20:262739736935 | 313 | timer.reset(); |
charwhit | 20:262739736935 | 314 | } |
charwhit | 20:262739736935 | 315 | } |
charwhit | 20:262739736935 | 316 | step = 1; |
charwhit | 20:262739736935 | 317 | contracted = false; |
charwhit | 20:262739736935 | 318 | btbee.printf("Going back to 1\n"); |
charwhit | 20:262739736935 | 319 | break; |
charwhit | 20:262739736935 | 320 | }//end of switch |
charwhit | 20:262739736935 | 321 | btbee.printf("Step: %d\n", step); |
charwhit | 5:c38929c0fd95 | 322 | } |
charwhit | 8:bd2f012e2f57 | 323 | passed = true; |
charwhit | 20:262739736935 | 324 | backup.reset(); |
charwhit | 16:caa77287cc25 | 325 | } else if (x[0] > 300 && x[2]>300 && x[4]>300) |
charwhit | 8:bd2f012e2f57 | 326 | passed = true; |
charwhit | 8:bd2f012e2f57 | 327 | else |
charwhit | 8:bd2f012e2f57 | 328 | passed = false; |
charwhit | 16:caa77287cc25 | 329 | |
charwhit | 5:c38929c0fd95 | 330 | |
nbtavis | 2:80a1ed62c307 | 331 | |
charwhit | 5:c38929c0fd95 | 332 | current_pos = robot.line_position(); |
charwhit | 16:caa77287cc25 | 333 | |
charwhit | 16:caa77287cc25 | 334 | |
charwhit | 16:caa77287cc25 | 335 | proportional = current_pos; |
charwhit | 16:caa77287cc25 | 336 | |
charwhit | 16:caa77287cc25 | 337 | |
charwhit | 5:c38929c0fd95 | 338 | derivative = current_pos - previous_pos; |
charwhit | 5:c38929c0fd95 | 339 | |
charwhit | 5:c38929c0fd95 | 340 | |
charwhit | 5:c38929c0fd95 | 341 | //compute the integral |
charwhit | 5:c38929c0fd95 | 342 | integral =+ proportional; |
charwhit | 5:c38929c0fd95 | 343 | |
charwhit | 5:c38929c0fd95 | 344 | //remember the last position. |
charwhit | 5:c38929c0fd95 | 345 | previous_pos = current_pos; |
charwhit | 20:262739736935 | 346 | |
charwhit | 5:c38929c0fd95 | 347 | |
charwhit | 5:c38929c0fd95 | 348 | // compute the power |
charwhit | 20:262739736935 | 349 | power = (proportional*(P_TERM)) + (integral*(I_TERM)) + (derivative*(D_TERM)); |
charwhit | 21:df402d79b2ad | 350 | /*if (poop){ |
charwhit | 20:262739736935 | 351 | btbee.printf("Check: P: %f D:%f Power: %f\n", P_TERM, D_TERM, power); |
charwhit | 20:262739736935 | 352 | poop = false; |
charwhit | 21:df402d79b2ad | 353 | }*/ |
charwhit | 20:262739736935 | 354 | |
charwhit | 5:c38929c0fd95 | 355 | //computer new speeds |
charwhit | 5:c38929c0fd95 | 356 | right = speed+power; |
charwhit | 5:c38929c0fd95 | 357 | left = speed-power; |
charwhit | 5:c38929c0fd95 | 358 | |
charwhit | 5:c38929c0fd95 | 359 | //limit checks |
charwhit | 5:c38929c0fd95 | 360 | if(right<MIN) |
charwhit | 5:c38929c0fd95 | 361 | right = MIN; |
charwhit | 5:c38929c0fd95 | 362 | else if (right > MAX) |
charwhit | 5:c38929c0fd95 | 363 | right = MAX; |
charwhit | 5:c38929c0fd95 | 364 | |
charwhit | 5:c38929c0fd95 | 365 | if(left<MIN) |
charwhit | 5:c38929c0fd95 | 366 | left = MIN; |
charwhit | 5:c38929c0fd95 | 367 | else if (left>MIN) |
charwhit | 5:c38929c0fd95 | 368 | left = MAX; |
charwhit | 5:c38929c0fd95 | 369 | |
charwhit | 5:c38929c0fd95 | 370 | //set speed |
charwhit | 5:c38929c0fd95 | 371 | |
charwhit | 5:c38929c0fd95 | 372 | robot.left_motor(left); |
charwhit | 5:c38929c0fd95 | 373 | robot.right_motor(right); |
charwhit | 16:caa77287cc25 | 374 | |
charwhit | 20:262739736935 | 375 | //traveled += ((right + left) / 2) * .005; |
charwhit | 16:caa77287cc25 | 376 | |
charwhit | 16:caa77287cc25 | 377 | |
charwhit | 5:c38929c0fd95 | 378 | wait((5-time_wait.read_ms())/1000); |
bbabbs | 0:17669460c6b1 | 379 | } |
charwhit | 5:c38929c0fd95 | 380 | |
charwhit | 5:c38929c0fd95 | 381 | |
charwhit | 5:c38929c0fd95 | 382 | |
charwhit | 5:c38929c0fd95 | 383 | robot.stop(); |
charwhit | 5:c38929c0fd95 | 384 | |
charwhit | 16:caa77287cc25 | 385 | /*char hail[]= {'V','1','5','O','4','E','4','C','8','D','8','E','8','C','8','D','8' |
charwhit | 5:c38929c0fd95 | 386 | ,'E','8','F','4','D','8','E','8','F','8','D','8','E','8','F','8','G' |
charwhit | 5:c38929c0fd95 | 387 | ,'4','A','4','E','1','6','E','1','6','F','8','C','8','D','8','E','8' |
charwhit | 5:c38929c0fd95 | 388 | ,'G','8','E','8','D','8','C','4' |
charwhit | 5:c38929c0fd95 | 389 | }; |
nbtavis | 2:80a1ed62c307 | 390 | int numb = 59; |
charwhit | 5:c38929c0fd95 | 391 | |
charwhit | 16:caa77287cc25 | 392 | robot.playtune(hail,numb);*/ |
charwhit | 5:c38929c0fd95 | 393 | |
charwhit | 5:c38929c0fd95 | 394 | |
charwhit | 5:c38929c0fd95 | 395 | |
charwhit | 5:c38929c0fd95 | 396 | |
charwhit | 5:c38929c0fd95 | 397 | } |