PIDs and fun
Dependencies: btbee m3pi_ng mbed FatFileSystemCpp
main.cpp@1:936e329e840a, 2015-05-22 (annotated)
- Committer:
- crmackey
- Date:
- Fri May 22 14:06:50 2015 +0000
- Revision:
- 1:936e329e840a
- Parent:
- 0:4d1ae7eb0070
- Child:
- 2:1e947dcc40cb
Stuff
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
crmackey | 0:4d1ae7eb0070 | 1 | #include "mbed.h" |
crmackey | 0:4d1ae7eb0070 | 2 | #include "m3pi_ng.h" |
crmackey | 0:4d1ae7eb0070 | 3 | #include "time.h" |
crmackey | 0:4d1ae7eb0070 | 4 | #include "btbee.h" |
crmackey | 0:4d1ae7eb0070 | 5 | |
crmackey | 0:4d1ae7eb0070 | 6 | m3pi m3pi; |
crmackey | 0:4d1ae7eb0070 | 7 | btbee btbee; |
crmackey | 0:4d1ae7eb0070 | 8 | DigitalIn m3pi_pb(p21); |
crmackey | 1:936e329e840a | 9 | DigitalIn m3pi_IN[] = {(p12)}; |
crmackey | 0:4d1ae7eb0070 | 10 | DigitalOut mbed_led[] = {(LED1), (LED2),(LED3), (LED4)}; |
crmackey | 0:4d1ae7eb0070 | 11 | DigitalOut m3pi_led[] = {(p13), (p14), (p15), (p16), (p17), (p18), (p19), (p20)}; |
crmackey | 0:4d1ae7eb0070 | 12 | |
crmackey | 0:4d1ae7eb0070 | 13 | // Minimum and maximum motor speeds |
crmackey | 0:4d1ae7eb0070 | 14 | #define MAX 1.0 |
crmackey | 0:4d1ae7eb0070 | 15 | #define MIN 0 |
crmackey | 0:4d1ae7eb0070 | 16 | |
crmackey | 0:4d1ae7eb0070 | 17 | // PID terms |
crmackey | 0:4d1ae7eb0070 | 18 | #define P_TERM 1.5 |
crmackey | 0:4d1ae7eb0070 | 19 | #define I_TERM 0 |
crmackey | 0:4d1ae7eb0070 | 20 | #define D_TERM 25 |
crmackey | 0:4d1ae7eb0070 | 21 | |
crmackey | 0:4d1ae7eb0070 | 22 | int main() { |
crmackey | 0:4d1ae7eb0070 | 23 | |
crmackey | 1:936e329e840a | 24 | double IR; |
crmackey | 1:936e329e840a | 25 | |
crmackey | 1:936e329e840a | 26 | while(1) { |
crmackey | 0:4d1ae7eb0070 | 27 | |
crmackey | 0:4d1ae7eb0070 | 28 | btbee.reset(); |
crmackey | 0:4d1ae7eb0070 | 29 | m3pi_pb.mode(PullUp); |
crmackey | 0:4d1ae7eb0070 | 30 | |
crmackey | 0:4d1ae7eb0070 | 31 | m3pi.printf("Wait 4"); |
crmackey | 0:4d1ae7eb0070 | 32 | m3pi.locate(0,1); |
crmackey | 0:4d1ae7eb0070 | 33 | m3pi.printf("PC"); |
crmackey | 0:4d1ae7eb0070 | 34 | |
crmackey | 0:4d1ae7eb0070 | 35 | while(m3pi_pb) { |
crmackey | 0:4d1ae7eb0070 | 36 | m3pi_led[0]=!m3pi_led[0]; |
crmackey | 0:4d1ae7eb0070 | 37 | wait(3); |
crmackey | 0:4d1ae7eb0070 | 38 | btbee.printf("\n"); |
crmackey | 0:4d1ae7eb0070 | 39 | btbee.printf("PC connected. Press the button \n"); |
crmackey | 0:4d1ae7eb0070 | 40 | } |
crmackey | 0:4d1ae7eb0070 | 41 | |
crmackey | 1:936e329e840a | 42 | m3pi.cls(); |
crmackey | 0:4d1ae7eb0070 | 43 | |
crmackey | 0:4d1ae7eb0070 | 44 | Timer LapTimer; |
crmackey | 0:4d1ae7eb0070 | 45 | char Bat[] = {'V','o','l','t',' ','i','s'}; |
crmackey | 0:4d1ae7eb0070 | 46 | m3pi.print(Bat,7); |
crmackey | 0:4d1ae7eb0070 | 47 | wait(0.75); |
crmackey | 0:4d1ae7eb0070 | 48 | m3pi.cls(); |
crmackey | 0:4d1ae7eb0070 | 49 | float batteryvoltage = m3pi.battery(); |
crmackey | 0:4d1ae7eb0070 | 50 | char* str = new char[30]; |
crmackey | 0:4d1ae7eb0070 | 51 | sprintf(str, "%.4g", batteryvoltage); |
crmackey | 0:4d1ae7eb0070 | 52 | m3pi.print(str,6); |
crmackey | 0:4d1ae7eb0070 | 53 | btbee.printf("Battery voltage is %f \n", batteryvoltage); |
crmackey | 0:4d1ae7eb0070 | 54 | wait(0.75); |
crmackey | 0:4d1ae7eb0070 | 55 | m3pi.cls(); |
crmackey | 0:4d1ae7eb0070 | 56 | |
crmackey | 0:4d1ae7eb0070 | 57 | if (batteryvoltage < 4.2) |
crmackey | 0:4d1ae7eb0070 | 58 | { |
crmackey | 0:4d1ae7eb0070 | 59 | char low[] = {'L','o','w',' ','b','a','t'}; |
crmackey | 0:4d1ae7eb0070 | 60 | m3pi.print(low,7); |
crmackey | 0:4d1ae7eb0070 | 61 | char ExitSound[]={'V','1','5','O','6','E','4','O','5','E','4'}; |
crmackey | 0:4d1ae7eb0070 | 62 | m3pi.playtune(ExitSound,11); |
crmackey | 0:4d1ae7eb0070 | 63 | btbee.printf("Battery voltage is too low. Stopping program"); |
crmackey | 0:4d1ae7eb0070 | 64 | exit(1); |
crmackey | 0:4d1ae7eb0070 | 65 | } |
crmackey | 0:4d1ae7eb0070 | 66 | |
crmackey | 0:4d1ae7eb0070 | 67 | m3pi.locate(0,1); |
crmackey | 0:4d1ae7eb0070 | 68 | m3pi.printf("Line PID"); |
crmackey | 0:4d1ae7eb0070 | 69 | |
crmackey | 0:4d1ae7eb0070 | 70 | wait(2.0); |
crmackey | 0:4d1ae7eb0070 | 71 | |
crmackey | 0:4d1ae7eb0070 | 72 | btbee.printf("Now calibrating \n"); |
crmackey | 0:4d1ae7eb0070 | 73 | |
crmackey | 0:4d1ae7eb0070 | 74 | m3pi.sensor_auto_calibrate(); |
crmackey | 0:4d1ae7eb0070 | 75 | |
crmackey | 0:4d1ae7eb0070 | 76 | btbee.printf("Finished calibrating \n"); |
crmackey | 0:4d1ae7eb0070 | 77 | |
crmackey | 1:936e329e840a | 78 | while (m3pi_IN [0] == 0) { |
crmackey | 1:936e329e840a | 79 | btbee.printf("Obstacle detected. Please remove to continue"); |
crmackey | 1:936e329e840a | 80 | m3pi.stop(); |
crmackey | 1:936e329e840a | 81 | wait(3.0); |
crmackey | 0:4d1ae7eb0070 | 82 | } |
crmackey | 0:4d1ae7eb0070 | 83 | |
crmackey | 0:4d1ae7eb0070 | 84 | float right; |
crmackey | 0:4d1ae7eb0070 | 85 | float left; |
crmackey | 0:4d1ae7eb0070 | 86 | float current_pos_of_line = 0.0; |
crmackey | 0:4d1ae7eb0070 | 87 | float previous_pos_of_line = 0.0; |
crmackey | 0:4d1ae7eb0070 | 88 | float derivative,proportional,integral = 0; |
crmackey | 0:4d1ae7eb0070 | 89 | float power; |
crmackey | 0:4d1ae7eb0070 | 90 | float speed = MAX; |
crmackey | 0:4d1ae7eb0070 | 91 | int LapTest[5]; |
crmackey | 0:4d1ae7eb0070 | 92 | int s1, s2, s3, s4, s5; |
crmackey | 0:4d1ae7eb0070 | 93 | int counter = -1; |
crmackey | 0:4d1ae7eb0070 | 94 | int checkvar = 0; |
crmackey | 0:4d1ae7eb0070 | 95 | double Time1, Time2, Time3, Time4, Time5, TimeAve; |
crmackey | 0:4d1ae7eb0070 | 96 | btbee.printf("\n"); |
crmackey | 0:4d1ae7eb0070 | 97 | btbee.printf("Now starting \n"); |
crmackey | 0:4d1ae7eb0070 | 98 | |
crmackey | 0:4d1ae7eb0070 | 99 | |
crmackey | 1:936e329e840a | 100 | |
crmackey | 0:4d1ae7eb0070 | 101 | |
crmackey | 1:936e329e840a | 102 | while (1) { |
crmackey | 1:936e329e840a | 103 | |
crmackey | 1:936e329e840a | 104 | |
crmackey | 0:4d1ae7eb0070 | 105 | // Get the position of the line. |
crmackey | 0:4d1ae7eb0070 | 106 | current_pos_of_line = m3pi.line_position(); |
crmackey | 0:4d1ae7eb0070 | 107 | proportional = current_pos_of_line; |
crmackey | 0:4d1ae7eb0070 | 108 | |
crmackey | 0:4d1ae7eb0070 | 109 | // Compute the derivative |
crmackey | 0:4d1ae7eb0070 | 110 | derivative = current_pos_of_line - previous_pos_of_line; |
crmackey | 0:4d1ae7eb0070 | 111 | |
crmackey | 0:4d1ae7eb0070 | 112 | // Compute the integral |
crmackey | 0:4d1ae7eb0070 | 113 | integral += proportional; |
crmackey | 0:4d1ae7eb0070 | 114 | |
crmackey | 0:4d1ae7eb0070 | 115 | // Remember the last position. |
crmackey | 0:4d1ae7eb0070 | 116 | previous_pos_of_line = current_pos_of_line; |
crmackey | 0:4d1ae7eb0070 | 117 | |
crmackey | 0:4d1ae7eb0070 | 118 | // Compute the power |
crmackey | 0:4d1ae7eb0070 | 119 | power = (proportional * (P_TERM) ) + (integral*(I_TERM)) + (derivative*(D_TERM)) ; |
crmackey | 0:4d1ae7eb0070 | 120 | |
crmackey | 0:4d1ae7eb0070 | 121 | // Compute new speeds |
crmackey | 0:4d1ae7eb0070 | 122 | right = speed+power; |
crmackey | 0:4d1ae7eb0070 | 123 | left = speed-power; |
crmackey | 0:4d1ae7eb0070 | 124 | |
crmackey | 0:4d1ae7eb0070 | 125 | // limit checks |
crmackey | 0:4d1ae7eb0070 | 126 | if (right < MIN) |
crmackey | 0:4d1ae7eb0070 | 127 | right = MIN; |
crmackey | 0:4d1ae7eb0070 | 128 | else if (right > MAX) |
crmackey | 0:4d1ae7eb0070 | 129 | right = MAX; |
crmackey | 0:4d1ae7eb0070 | 130 | |
crmackey | 0:4d1ae7eb0070 | 131 | if (left < MIN) |
crmackey | 0:4d1ae7eb0070 | 132 | left = MIN; |
crmackey | 0:4d1ae7eb0070 | 133 | else if (left > MAX) |
crmackey | 0:4d1ae7eb0070 | 134 | left = MAX; |
crmackey | 0:4d1ae7eb0070 | 135 | |
crmackey | 0:4d1ae7eb0070 | 136 | // set speed |
crmackey | 0:4d1ae7eb0070 | 137 | m3pi.left_motor(left); |
crmackey | 0:4d1ae7eb0070 | 138 | m3pi.right_motor(right); |
crmackey | 0:4d1ae7eb0070 | 139 | |
crmackey | 0:4d1ae7eb0070 | 140 | m3pi.calibrated_sensor(LapTest); |
crmackey | 0:4d1ae7eb0070 | 141 | |
crmackey | 0:4d1ae7eb0070 | 142 | s1 = LapTest[0]; |
crmackey | 0:4d1ae7eb0070 | 143 | s2 = LapTest[1]; |
crmackey | 0:4d1ae7eb0070 | 144 | s3 = LapTest[2]; |
crmackey | 0:4d1ae7eb0070 | 145 | s4 = LapTest[3]; |
crmackey | 0:4d1ae7eb0070 | 146 | s5 = LapTest[4]; |
crmackey | 0:4d1ae7eb0070 | 147 | |
crmackey | 0:4d1ae7eb0070 | 148 | if (s1 > 500 and s5 > 500) { |
crmackey | 0:4d1ae7eb0070 | 149 | checkvar = 1; |
crmackey | 0:4d1ae7eb0070 | 150 | } |
crmackey | 0:4d1ae7eb0070 | 151 | |
crmackey | 0:4d1ae7eb0070 | 152 | if (s1 < 500 and s5 < 500 and checkvar == 1 and counter == -1) { |
crmackey | 0:4d1ae7eb0070 | 153 | LapTimer.start(); |
crmackey | 0:4d1ae7eb0070 | 154 | } |
crmackey | 0:4d1ae7eb0070 | 155 | |
crmackey | 0:4d1ae7eb0070 | 156 | if (s1 < 500 and s5 < 500 and checkvar == 1) { |
crmackey | 0:4d1ae7eb0070 | 157 | counter = counter + 1; |
crmackey | 0:4d1ae7eb0070 | 158 | m3pi.cls(); |
crmackey | 0:4d1ae7eb0070 | 159 | m3pi.printf("%d", counter); |
crmackey | 0:4d1ae7eb0070 | 160 | checkvar = 0; |
crmackey | 0:4d1ae7eb0070 | 161 | switch (counter) { |
crmackey | 0:4d1ae7eb0070 | 162 | case 1: { |
crmackey | 0:4d1ae7eb0070 | 163 | Time1 = LapTimer.read(); |
crmackey | 0:4d1ae7eb0070 | 164 | LapTimer.reset(); |
crmackey | 0:4d1ae7eb0070 | 165 | LapTimer.start(); |
crmackey | 0:4d1ae7eb0070 | 166 | btbee.printf("Lap 1: %f \n", Time1); |
crmackey | 0:4d1ae7eb0070 | 167 | break; |
crmackey | 0:4d1ae7eb0070 | 168 | } |
crmackey | 0:4d1ae7eb0070 | 169 | case 2: { |
crmackey | 0:4d1ae7eb0070 | 170 | Time2 = LapTimer.read(); |
crmackey | 0:4d1ae7eb0070 | 171 | LapTimer.reset(); |
crmackey | 0:4d1ae7eb0070 | 172 | LapTimer.start(); |
crmackey | 0:4d1ae7eb0070 | 173 | btbee.printf("Lap 2: %f \n", Time2); |
crmackey | 0:4d1ae7eb0070 | 174 | break; |
crmackey | 0:4d1ae7eb0070 | 175 | } |
crmackey | 0:4d1ae7eb0070 | 176 | case 3: { |
crmackey | 0:4d1ae7eb0070 | 177 | Time3 = LapTimer.read(); |
crmackey | 0:4d1ae7eb0070 | 178 | LapTimer.reset(); |
crmackey | 0:4d1ae7eb0070 | 179 | LapTimer.start(); |
crmackey | 0:4d1ae7eb0070 | 180 | btbee.printf("Lap 3: %f \n", Time3); |
crmackey | 0:4d1ae7eb0070 | 181 | break; |
crmackey | 0:4d1ae7eb0070 | 182 | } |
crmackey | 0:4d1ae7eb0070 | 183 | case 4: { |
crmackey | 0:4d1ae7eb0070 | 184 | Time4 = LapTimer.read(); |
crmackey | 0:4d1ae7eb0070 | 185 | LapTimer.reset(); |
crmackey | 0:4d1ae7eb0070 | 186 | LapTimer.start(); |
crmackey | 0:4d1ae7eb0070 | 187 | btbee.printf("Lap 4: %f \n", Time4); |
crmackey | 0:4d1ae7eb0070 | 188 | break; |
crmackey | 0:4d1ae7eb0070 | 189 | } |
crmackey | 0:4d1ae7eb0070 | 190 | case 5: { |
crmackey | 0:4d1ae7eb0070 | 191 | Time5 = LapTimer.read(); |
crmackey | 0:4d1ae7eb0070 | 192 | LapTimer.reset(); |
crmackey | 0:4d1ae7eb0070 | 193 | LapTimer.start(); |
crmackey | 0:4d1ae7eb0070 | 194 | btbee.printf("Lap 5: %f \n", Time5); |
crmackey | 0:4d1ae7eb0070 | 195 | break; |
crmackey | 0:4d1ae7eb0070 | 196 | } |
crmackey | 0:4d1ae7eb0070 | 197 | } |
crmackey | 0:4d1ae7eb0070 | 198 | |
crmackey | 0:4d1ae7eb0070 | 199 | |
crmackey | 0:4d1ae7eb0070 | 200 | } |
crmackey | 0:4d1ae7eb0070 | 201 | |
crmackey | 0:4d1ae7eb0070 | 202 | if (counter == 5) { |
crmackey | 0:4d1ae7eb0070 | 203 | m3pi.stop(); |
crmackey | 0:4d1ae7eb0070 | 204 | m3pi.cls(); |
crmackey | 0:4d1ae7eb0070 | 205 | m3pi.locate(0,0); |
crmackey | 0:4d1ae7eb0070 | 206 | m3pi.printf("Ave time"); |
crmackey | 0:4d1ae7eb0070 | 207 | TimeAve = (Time1 + Time2 + Time3 + Time4 + Time5)/5.0; |
crmackey | 0:4d1ae7eb0070 | 208 | m3pi.locate(0,1); |
crmackey | 0:4d1ae7eb0070 | 209 | m3pi.printf("%f", TimeAve); |
crmackey | 0:4d1ae7eb0070 | 210 | btbee.printf("Average time per lap: %f ", TimeAve); |
crmackey | 0:4d1ae7eb0070 | 211 | exit(1); |
crmackey | 0:4d1ae7eb0070 | 212 | } |
crmackey | 0:4d1ae7eb0070 | 213 | |
crmackey | 0:4d1ae7eb0070 | 214 | |
crmackey | 0:4d1ae7eb0070 | 215 | } |
crmackey | 0:4d1ae7eb0070 | 216 | |
crmackey | 1:936e329e840a | 217 | } |
crmackey | 1:936e329e840a | 218 | |
crmackey | 1:936e329e840a | 219 | } |