car using PID from centre line

Dependencies:   FRDM-TFC mbed CBuffer XBEE mbed_angular_speed motor2 MMA8451Q

Fork of KL25Z_Camera_Test by GDP 4

Committer:
FatCookies
Date:
Thu Mar 23 09:36:39 2017 +0000
Revision:
45:3435bdd2d487
Parent:
44:1884ffec9a57
Child:
46:6806ea59ffed
testing out accelerometer data for slope detection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maximusismax 8:7c5e6b1e7aa5 1 //Autonomous Car GDP controller
maximusismax 8:7c5e6b1e7aa5 2 //Written by various group members
maximusismax 8:7c5e6b1e7aa5 3 //Commented & cleaned up by Max/Adam
maximusismax 8:7c5e6b1e7aa5 4
maximusismax 8:7c5e6b1e7aa5 5 //To-do
maximusismax 8:7c5e6b1e7aa5 6 // -Change xbee transmission to non-blocking
maximusismax 8:7c5e6b1e7aa5 7 // -Improve start/stop detection and resultant action (setting PID values?)
maximusismax 8:7c5e6b1e7aa5 8
maximusismax 8:7c5e6b1e7aa5 9 #include <stdarg.h>
maximusismax 8:7c5e6b1e7aa5 10 #include <stdio.h>
maximusismax 8:7c5e6b1e7aa5 11
maximusismax 0:566127ca8048 12 #include "mbed.h"
maximusismax 0:566127ca8048 13 #include "TFC.h"
FatCookies 4:4afa448c9cce 14 #include "XBEE.h"
FatCookies 9:aa2ce38dec6b 15 #include "angular_speed.h"
FatCookies 14:13085e161dd1 16 #include "main.h"
FatCookies 12:da96e2f87465 17 #include "motor.h"
FatCookies 45:3435bdd2d487 18 #include "MMA8451Q.h"
maximusismax 8:7c5e6b1e7aa5 19
FatCookies 17:6ae90788cc2b 20 // Serial
FatCookies 17:6ae90788cc2b 21 #if USE_COMMS
FatCookies 17:6ae90788cc2b 22 Serial pc(PTD3,PTD2);
FatCookies 17:6ae90788cc2b 23 XBEE xb(&pc);
FatCookies 17:6ae90788cc2b 24 #endif
FatCookies 3:87a5122682fa 25
FatCookies 17:6ae90788cc2b 26 // PID Timer
FatCookies 3:87a5122682fa 27 Timer t;
FatCookies 4:4afa448c9cce 28
FatCookies 17:6ae90788cc2b 29 //Speed Sensors interupts and timers
FatCookies 9:aa2ce38dec6b 30 InterruptIn leftHallSensor(D0);
FatCookies 9:aa2ce38dec6b 31 InterruptIn rightHallSensor(D2);
FatCookies 9:aa2ce38dec6b 32 Timer t1;
FatCookies 9:aa2ce38dec6b 33 Timer t2;
FatCookies 9:aa2ce38dec6b 34
lh14g13 32:6829684f8c4d 35 //testing timer for laptimes
FatCookies 27:627d67e3b9b0 36 Timer lapTimer;
FatCookies 16:81cdffd8c5d5 37
FatCookies 45:3435bdd2d487 38 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
FatCookies 45:3435bdd2d487 39
FatCookies 45:3435bdd2d487 40
FatCookies 45:3435bdd2d487 41
FatCookies 45:3435bdd2d487 42 int loop = 0;
FatCookies 45:3435bdd2d487 43 float accc = 0.f;
FatCookies 21:0b69fada7c5f 44
maximusismax 0:566127ca8048 45 int main() {
FatCookies 45:3435bdd2d487 46
maximusismax 8:7c5e6b1e7aa5 47 //Set up TFC driver stuff
maximusismax 0:566127ca8048 48 TFC_Init();
FatCookies 17:6ae90788cc2b 49 ALIGN_SERVO;
FatCookies 13:4e77264f254a 50
FatCookies 17:6ae90788cc2b 51 #if USE_COMMS
FatCookies 17:6ae90788cc2b 52 //Setup baud rate for serial link, do not change!
FatCookies 17:6ae90788cc2b 53 pc.baud(BAUD_RATE);
FatCookies 17:6ae90788cc2b 54 #endif
maximusismax 0:566127ca8048 55
maximusismax 8:7c5e6b1e7aa5 56 //Initialise/reset PID variables
maximusismax 8:7c5e6b1e7aa5 57 initVariables();
FatCookies 9:aa2ce38dec6b 58 initSpeedSensors();
FatCookies 40:10e8e80af7da 59
FatCookies 43:649473c5a12b 60 #if !(USE_COMMS)
FatCookies 43:649473c5a12b 61 buttonStartup();
FatCookies 43:649473c5a12b 62 #endif
FatCookies 45:3435bdd2d487 63
FatCookies 12:da96e2f87465 64
maximusismax 0:566127ca8048 65 while(1) {
FatCookies 3:87a5122682fa 66
FatCookies 17:6ae90788cc2b 67 #if USE_COMMS
FatCookies 17:6ae90788cc2b 68 handleComms();
FatCookies 17:6ae90788cc2b 69 #endif
maximusismax 8:7c5e6b1e7aa5 70
maximusismax 8:7c5e6b1e7aa5 71 //If we have an image ready
FatCookies 13:4e77264f254a 72 if(TFC_LineScanImageReady>0) {
FatCookies 13:4e77264f254a 73 /* Find the bounds of the track and calculate how close we are to
FatCookies 13:4e77264f254a 74 * the centre */
FatCookies 17:6ae90788cc2b 75 servo_pid.measured_value = findCentreValue(CLOSE_CAMERA, left, right);
maximusismax 8:7c5e6b1e7aa5 76
FatCookies 21:0b69fada7c5f 77 // Check if car is at the stop line
FatCookies 45:3435bdd2d487 78
FatCookies 45:3435bdd2d487 79 if(loop % 10 == 0) {
FatCookies 45:3435bdd2d487 80 accc = checkAcc();
FatCookies 45:3435bdd2d487 81 }
FatCookies 45:3435bdd2d487 82
FatCookies 45:3435bdd2d487 83 if(accc < 0.18f) {
FatCookies 45:3435bdd2d487 84 handleStartStop();
FatCookies 45:3435bdd2d487 85 } else {
FatCookies 45:3435bdd2d487 86 sendString("up %f", accc);
FatCookies 45:3435bdd2d487 87 }
FatCookies 45:3435bdd2d487 88
FatCookies 45:3435bdd2d487 89
FatCookies 45:3435bdd2d487 90
FatCookies 21:0b69fada7c5f 91
FatCookies 27:627d67e3b9b0 92 #if USE_COMMS
FatCookies 27:627d67e3b9b0 93 // Send the line scan image over serial
FatCookies 27:627d67e3b9b0 94 sendImage();
FatCookies 27:627d67e3b9b0 95 #endif
FatCookies 21:0b69fada7c5f 96
FatCookies 21:0b69fada7c5f 97
FatCookies 21:0b69fada7c5f 98 //Reset image ready flag
FatCookies 21:0b69fada7c5f 99 TFC_LineScanImageReady=0;
lh14g13 19:65f0b6febc23 100
FatCookies 17:6ae90788cc2b 101 // Slow down, adjust PID values and enable differential before corners.
FatCookies 21:0b69fada7c5f 102 //handleCornering();
FatCookies 17:6ae90788cc2b 103
FatCookies 13:4e77264f254a 104 // Run the PID controllers and adjust steering/motor accordingly
maximusismax 8:7c5e6b1e7aa5 105 PIDController();
maximusismax 8:7c5e6b1e7aa5 106
FatCookies 21:0b69fada7c5f 107
FatCookies 21:0b69fada7c5f 108
FatCookies 21:0b69fada7c5f 109
FatCookies 17:6ae90788cc2b 110 #if USE_COMMS
FatCookies 17:6ae90788cc2b 111 // Send the wheel speeds over serial
FatCookies 17:6ae90788cc2b 112 sendSpeeds();
FatCookies 17:6ae90788cc2b 113 #endif
FatCookies 13:4e77264f254a 114
FatCookies 21:0b69fada7c5f 115
maximusismax 8:7c5e6b1e7aa5 116
FatCookies 45:3435bdd2d487 117 //wait_ms(25);
FatCookies 21:0b69fada7c5f 118
maximusismax 8:7c5e6b1e7aa5 119 }
maximusismax 8:7c5e6b1e7aa5 120 }
maximusismax 8:7c5e6b1e7aa5 121 }
maximusismax 8:7c5e6b1e7aa5 122
FatCookies 40:10e8e80af7da 123 void buttonStartup() {
FatCookies 45:3435bdd2d487 124 TFC_SetBatteryLED(led_values[b_pressed % 4]);
FatCookies 40:10e8e80af7da 125 while(1) {
FatCookies 45:3435bdd2d487 126 //handleComms();
FatCookies 45:3435bdd2d487 127
FatCookies 40:10e8e80af7da 128 // 2 bit is broken = max value is 13
FatCookies 40:10e8e80af7da 129 uint8_t dip = TFC_GetDIP_Switch();
FatCookies 40:10e8e80af7da 130
FatCookies 40:10e8e80af7da 131 // 1 on press, 0 otherwise
FatCookies 40:10e8e80af7da 132 uint8_t button_a = TFC_ReadPushButton(0);
FatCookies 40:10e8e80af7da 133 uint8_t button_b = TFC_ReadPushButton(1);
FatCookies 40:10e8e80af7da 134
FatCookies 40:10e8e80af7da 135 // -1 to 1, turning clockwise INCREASES value
FatCookies 40:10e8e80af7da 136 float pot_a = TFC_ReadPot(0);
FatCookies 40:10e8e80af7da 137 float pot_b = TFC_ReadPot(1);
FatCookies 40:10e8e80af7da 138
FatCookies 45:3435bdd2d487 139 if(button_b && !bDown) {
FatCookies 45:3435bdd2d487 140 bDown = true;
FatCookies 45:3435bdd2d487 141 continue;
FatCookies 45:3435bdd2d487 142 }
FatCookies 45:3435bdd2d487 143 if(!button_b && bDown) {
FatCookies 45:3435bdd2d487 144 b_pressed++;
FatCookies 45:3435bdd2d487 145 TFC_SetBatteryLED(led_values[b_pressed % 4]);
FatCookies 45:3435bdd2d487 146 bDown = false;
FatCookies 45:3435bdd2d487 147 continue;
FatCookies 45:3435bdd2d487 148 }
FatCookies 45:3435bdd2d487 149
FatCookies 45:3435bdd2d487 150 if(button_a && !aDown) {
FatCookies 45:3435bdd2d487 151 aDown = true;
FatCookies 45:3435bdd2d487 152 continue;
FatCookies 45:3435bdd2d487 153 }
FatCookies 45:3435bdd2d487 154 if(!button_a && aDown) {
FatCookies 45:3435bdd2d487 155
FatCookies 45:3435bdd2d487 156 TFC_SetBatteryLED(0);
FatCookies 43:649473c5a12b 157 aDown = false;
FatCookies 43:649473c5a12b 158
FatCookies 43:649473c5a12b 159 int choice = b_pressed % 4;
FatCookies 43:649473c5a12b 160 switch(choice) {
FatCookies 45:3435bdd2d487 161 case 0:
FatCookies 43:649473c5a12b 162 initPID(&servo_pid, 2.2f, 0.6f, 0.f);
FatCookies 45:3435bdd2d487 163 speed = 40;
FatCookies 45:3435bdd2d487 164 ed_tune = 1.0f;
FatCookies 43:649473c5a12b 165 break;
FatCookies 43:649473c5a12b 166 case 1:
FatCookies 43:649473c5a12b 167 initPID(&servo_pid, 2.2f, 0.6f, 0.f);
FatCookies 43:649473c5a12b 168 speed = 120;
FatCookies 45:3435bdd2d487 169 ed_tune = 1.0f;
FatCookies 43:649473c5a12b 170 break;
FatCookies 43:649473c5a12b 171 case 2:
FatCookies 43:649473c5a12b 172 initPID(&servo_pid, 2.2f, 0.6f, 0.f);
FatCookies 43:649473c5a12b 173 speed = 130;
FatCookies 45:3435bdd2d487 174 ed_tune = 1.0f;
FatCookies 43:649473c5a12b 175 break;
FatCookies 43:649473c5a12b 176 case 3:
FatCookies 43:649473c5a12b 177 initPID(&servo_pid, 2.2f, 0.6f, 0.f);
FatCookies 43:649473c5a12b 178 speed = 140;
FatCookies 45:3435bdd2d487 179 ed_tune = 1.0f;
FatCookies 43:649473c5a12b 180 break;
FatCookies 45:3435bdd2d487 181 }
FatCookies 45:3435bdd2d487 182
FatCookies 45:3435bdd2d487 183 wait(2.f);
FatCookies 43:649473c5a12b 184
FatCookies 45:3435bdd2d487 185 ALIGN_SERVO;
FatCookies 45:3435bdd2d487 186 right_motor_pid.desired_value=speed;
FatCookies 45:3435bdd2d487 187 left_motor_pid.desired_value=speed;
FatCookies 45:3435bdd2d487 188 TFC_HBRIDGE_ENABLE;
FatCookies 45:3435bdd2d487 189 servo_pid.integral = 0;
FatCookies 45:3435bdd2d487 190 //sendString("PID: %f %f %f speed:%f ed:%f",servo_pid.Kp, servo_pid.Ki, servo_pid.Kd, speed, ed_tune);
FatCookies 45:3435bdd2d487 191 return;
FatCookies 43:649473c5a12b 192 }
FatCookies 43:649473c5a12b 193
FatCookies 45:3435bdd2d487 194
FatCookies 40:10e8e80af7da 195 }
FatCookies 45:3435bdd2d487 196
FatCookies 45:3435bdd2d487 197 }
FatCookies 40:10e8e80af7da 198
FatCookies 45:3435bdd2d487 199
FatCookies 40:10e8e80af7da 200
FatCookies 17:6ae90788cc2b 201 void initVariables() {
FatCookies 17:6ae90788cc2b 202 // Initialise three PID controllers for the servo and each wheel.
oj3g13 34:3de7a19ccea3 203 initPID(&servo_pid, 0.f, 0.f, 0.f);
FatCookies 45:3435bdd2d487 204 initPID(&left_motor_pid, 0.007f, 0.f, 0.f);
FatCookies 45:3435bdd2d487 205 initPID(&right_motor_pid, 0.007f, 0.f, 0.f);
FatCookies 17:6ae90788cc2b 206
lh14g13 19:65f0b6febc23 207 right_motor_pid.desired_value=0;
lh14g13 19:65f0b6febc23 208 left_motor_pid.desired_value=0;
lh14g13 19:65f0b6febc23 209
lh14g13 19:65f0b6febc23 210 // intialise the maximum speed interms of the angular speed.
FatCookies 17:6ae90788cc2b 211 valBufferIndex = 0;
lh14g13 19:65f0b6febc23 212 speed = 50;
FatCookies 13:4e77264f254a 213
FatCookies 17:6ae90788cc2b 214 //Start stop
FatCookies 17:6ae90788cc2b 215 startstop = 0;
FatCookies 17:6ae90788cc2b 216 seen = false;
FatCookies 17:6ae90788cc2b 217
FatCookies 17:6ae90788cc2b 218 // Turning
FatCookies 17:6ae90788cc2b 219 turning = 0;
FatCookies 17:6ae90788cc2b 220 keepTurning = 0;
FatCookies 17:6ae90788cc2b 221 slow = false;
FatCookies 20:ed954836d028 222
FatCookies 20:ed954836d028 223 wL = 0;
FatCookies 20:ed954836d028 224 wR = 0;
FatCookies 20:ed954836d028 225 prevL = 0;
FatCookies 20:ed954836d028 226 prevR = 0;
maximusismax 8:7c5e6b1e7aa5 227 }
maximusismax 8:7c5e6b1e7aa5 228
FatCookies 13:4e77264f254a 229 void initPID(pid_instance* pid, float Kp, float Ki, float Kd) {
FatCookies 17:6ae90788cc2b 230 pid->Kp = Kp;
FatCookies 13:4e77264f254a 231 pid->Ki = Ki;
FatCookies 13:4e77264f254a 232 pid->Kd = Kd;
FatCookies 13:4e77264f254a 233 pid->dt = 0;
FatCookies 13:4e77264f254a 234 pid->p_error = 0;
FatCookies 13:4e77264f254a 235 pid->pid_error = 0;
FatCookies 13:4e77264f254a 236 pid->integral = 0;
FatCookies 13:4e77264f254a 237 pid->measured_value = 0;
FatCookies 13:4e77264f254a 238 pid->desired_value = 0;
FatCookies 13:4e77264f254a 239 pid->derivative = 0;
FatCookies 13:4e77264f254a 240 }
FatCookies 13:4e77264f254a 241
FatCookies 29:b5b31256572b 242 bool leftSeen;
FatCookies 29:b5b31256572b 243 bool rightSeen;
maximusismax 31:1a06c9e1985e 244
maximusismax 31:1a06c9e1985e 245 //Function which calcuates how far to the left/right of the centre of the track the car is
maximusismax 31:1a06c9e1985e 246 //Takes data from either camera, and passes some variables by reference, which will hold
maximusismax 31:1a06c9e1985e 247 //the indices holding the locations of the left and right edges of the track
FatCookies 17:6ae90788cc2b 248 inline float findCentreValue(volatile uint16_t * cam_data, uint8_t &l, uint8_t &r) {
FatCookies 17:6ae90788cc2b 249
maximusismax 31:1a06c9e1985e 250 diff = 0; //Holds difference in intensity between consecutive pixels
maximusismax 31:1a06c9e1985e 251 prev = -1; //Holds index of last inspected pixel
FatCookies 29:b5b31256572b 252
maximusismax 31:1a06c9e1985e 253 //Used for crossroads navigation, holds info on which edges of the track are observed
FatCookies 29:b5b31256572b 254 leftSeen = false;
FatCookies 29:b5b31256572b 255 rightSeen = false;
maximusismax 31:1a06c9e1985e 256
maximusismax 31:1a06c9e1985e 257 //Starting in the middle index, step left, inspecting the the edge of the track
oj3g13 34:3de7a19ccea3 258 for(i = 63; i > 2; i--) {
oj3g13 26:f3d770f3eda1 259 curr_left = (uint8_t)(cam_data[i] >> 4) & 0xFF;
FatCookies 17:6ae90788cc2b 260 diff = prev - curr_left;
maximusismax 31:1a06c9e1985e 261 //Check incorporates a combination of looking at the difference in intensities
maximusismax 31:1a06c9e1985e 262 //and whether the pixels intensity is less than a threshold, corresponding to the black edge
FatCookies 17:6ae90788cc2b 263 if(abs(diff) >= CAM_DIFF && curr_left <= CAM_THRESHOLD && prev != -1) {
maximusismax 31:1a06c9e1985e 264 l = i; //Record the index where the edge is observed
FatCookies 29:b5b31256572b 265 leftSeen = true;
FatCookies 17:6ae90788cc2b 266 break;
FatCookies 17:6ae90788cc2b 267 }
maximusismax 31:1a06c9e1985e 268 prev = curr_left; //Update previous value for the loop
FatCookies 17:6ae90788cc2b 269 }
maximusismax 8:7c5e6b1e7aa5 270
FatCookies 17:6ae90788cc2b 271 prev = -1;
maximusismax 31:1a06c9e1985e 272 //As before, start in the middle but this time step rightwards in the image
oj3g13 34:3de7a19ccea3 273 for(i = 64; i < 126; i++) {
oj3g13 26:f3d770f3eda1 274 curr_right = (uint8_t)(cam_data[i] >> 4) & 0xFF;
FatCookies 17:6ae90788cc2b 275 int diff = prev - curr_right;
FatCookies 17:6ae90788cc2b 276 if(abs(diff) >= CAM_DIFF && curr_right <= CAM_THRESHOLD && prev != -1) {
FatCookies 17:6ae90788cc2b 277 r = i;
FatCookies 29:b5b31256572b 278 rightSeen = true;
FatCookies 17:6ae90788cc2b 279 break;
FatCookies 17:6ae90788cc2b 280 }
FatCookies 17:6ae90788cc2b 281 prev = curr_right;
FatCookies 17:6ae90788cc2b 282 }
FatCookies 17:6ae90788cc2b 283
maximusismax 31:1a06c9e1985e 284 //If both edges are not visible, we are likely in a crossroads
FatCookies 29:b5b31256572b 285 if(!rightSeen && !leftSeen) {
FatCookies 43:649473c5a12b 286 #if USE_COMMS
FatCookies 43:649473c5a12b 287 sendString("lost edges");
FatCookies 43:649473c5a12b 288 #endif
maximusismax 31:1a06c9e1985e 289 ALIGN_SERVO; //Straighten wheels so we go straight through the crossroads
maximusismax 31:1a06c9e1985e 290 servo_pid.integral = 0;
oj3g13 34:3de7a19ccea3 291 l=0;r=128;
FatCookies 29:b5b31256572b 292 }
FatCookies 29:b5b31256572b 293
maximusismax 31:1a06c9e1985e 294 //Calculate how left/right from the centre line we are
FatCookies 17:6ae90788cc2b 295 return (64 - ((l+r)/2))/64.f;
FatCookies 13:4e77264f254a 296 }
FatCookies 13:4e77264f254a 297
maximusismax 31:1a06c9e1985e 298 //Unused function currently
maximusismax 31:1a06c9e1985e 299 //Was used to establish whether we are in a corner, by inspecting a buffer of
maximusismax 31:1a06c9e1985e 300 //centre line values
FatCookies 17:6ae90788cc2b 301 inline void handleCornering() {
FatCookies 12:da96e2f87465 302
maximusismax 31:1a06c9e1985e 303 //Get current value of how left/right of centre line we are on the track
FatCookies 17:6ae90788cc2b 304 float lookaheadMeasuredValue = findCentreValue(LOOKAHEAD_CAMERA, farLeft, farRight);
FatCookies 15:ccde02f96449 305
FatCookies 13:4e77264f254a 306 measuredValBuffer[frame_counter % 64] = servo_pid.measured_value;
FatCookies 13:4e77264f254a 307
FatCookies 13:4e77264f254a 308 int count = 0;
FatCookies 13:4e77264f254a 309 for(i = 0; i < 10; i++) {
maximusismax 31:1a06c9e1985e 310 //Step through the buffer, using modulus operator
FatCookies 13:4e77264f254a 311 float val = abs(measuredValBuffer[(frame_counter - i) % 64]);
maximusismax 31:1a06c9e1985e 312 if(val > 0.09) { //If the value exceeds a certain value (obtained experimentally), we are in a corner
FatCookies 13:4e77264f254a 313 count++;
FatCookies 13:4e77264f254a 314 }
FatCookies 13:4e77264f254a 315 }
FatCookies 12:da96e2f87465 316
FatCookies 29:b5b31256572b 317 /*if(!turning && abs(lookaheadMeasuredValue) > 0.11f){
FatCookies 15:ccde02f96449 318 TFC_SetMotorPWM(0.4,0.4);
FatCookies 15:ccde02f96449 319 }
FatCookies 29:b5b31256572b 320 */
FatCookies 15:ccde02f96449 321
FatCookies 29:b5b31256572b 322 if(false) {
lh14g13 18:0095a3a8f8e4 323
lh14g13 18:0095a3a8f8e4 324 //default
lh14g13 18:0095a3a8f8e4 325 //TFC_SetMotorPWM(0.4,0.4);
lh14g13 18:0095a3a8f8e4 326
lh14g13 19:65f0b6febc23 327 //dutyCycleCorner(speed,servo_pid.output);
lh14g13 18:0095a3a8f8e4 328
lh14g13 32:6829684f8c4d 329
lh14g13 18:0095a3a8f8e4 330 //dutyCycleCorner(float cornerspeed, servo_pid.output);
lh14g13 18:0095a3a8f8e4 331 //dutyCycleCorner(speed, servo_pid.output);
lh14g13 32:6829684f8c4d 332 // This activates the electronic differential so that it runs whilst cornering.
lh14g13 32:6829684f8c4d 333 // this changes the desired desired speed of each of the wheels according to the angle of the servo.
lh14g13 39:7b28ee39185d 334 sensorCorner(left_motor_pid.desired_value, right_motor_pid.desired_value , servo_pid.output, speed,ed_tune);
FatCookies 14:13085e161dd1 335 }
FatCookies 14:13085e161dd1 336
FatCookies 29:b5b31256572b 337 /*
FatCookies 13:4e77264f254a 338 if(abs(servo_pid.measured_value) > 0.11f){
FatCookies 15:ccde02f96449 339 if(!turning) {
FatCookies 13:4e77264f254a 340 turning = 1;
FatCookies 13:4e77264f254a 341 } else {
FatCookies 13:4e77264f254a 342 turning++;
FatCookies 13:4e77264f254a 343 }
FatCookies 13:4e77264f254a 344
FatCookies 13:4e77264f254a 345 } else {
FatCookies 13:4e77264f254a 346 if(turning) {
FatCookies 13:4e77264f254a 347 if(keepTurning == 0 || count > 6) {
FatCookies 13:4e77264f254a 348 keepTurning++;
FatCookies 13:4e77264f254a 349 } else {
FatCookies 15:ccde02f96449 350 //sendString("stop turning turned=%d",turning);
FatCookies 13:4e77264f254a 351 keepTurning = 0;
lh14g13 33:0fc789c09694 352 turning = 0;
lh14g13 33:0fc789c09694 353 left_motor_pid.desired_value=speed;
lh14g13 33:0fc789c09694 354 right_motot_pid.desired_value=speed;
FatCookies 13:4e77264f254a 355 TFC_SetMotorPWM(speed,speed);
FatCookies 13:4e77264f254a 356 }
FatCookies 13:4e77264f254a 357
FatCookies 13:4e77264f254a 358 }
FatCookies 13:4e77264f254a 359 }
FatCookies 29:b5b31256572b 360 */
FatCookies 13:4e77264f254a 361
maximusismax 8:7c5e6b1e7aa5 362 }
maximusismax 8:7c5e6b1e7aa5 363
maximusismax 31:1a06c9e1985e 364 //Unused function currently
maximusismax 31:1a06c9e1985e 365 //Was used to estimate whether the stop marker was seen
FatCookies 17:6ae90788cc2b 366 inline float getLineEntropy() {
FatCookies 17:6ae90788cc2b 367 float entropy = 0;
FatCookies 17:6ae90788cc2b 368 float last = (int8_t)(CLOSE_CAMERA[0] >> 4) & 0xFF;
FatCookies 17:6ae90788cc2b 369 for(int i = 1; i < 128; i++) {
FatCookies 17:6ae90788cc2b 370 entropy += abs(last - ((int8_t)(CLOSE_CAMERA[i] >> 4) & 0xFF));
FatCookies 17:6ae90788cc2b 371 }
FatCookies 17:6ae90788cc2b 372 return entropy;
FatCookies 17:6ae90788cc2b 373 }
FatCookies 17:6ae90788cc2b 374
FatCookies 17:6ae90788cc2b 375 void handlePID(pid_instance *pid) {
FatCookies 17:6ae90788cc2b 376 pid->dt = t.read();
FatCookies 17:6ae90788cc2b 377 pid->pid_error = pid->desired_value - pid->measured_value;
FatCookies 17:6ae90788cc2b 378 pid->integral = pid->integral + pid->pid_error * pid->dt;
FatCookies 17:6ae90788cc2b 379 pid->derivative = (pid->pid_error - pid->p_error) / pid->dt;
FatCookies 17:6ae90788cc2b 380 pid->output = pid->Kp * pid->pid_error + pid->Ki * pid->integral + pid->Kd * pid->derivative;
FatCookies 17:6ae90788cc2b 381 pid->p_error = pid->pid_error;
FatCookies 17:6ae90788cc2b 382
FatCookies 17:6ae90788cc2b 383 if(pid->integral > 1.0f) {
maximusismax 31:1a06c9e1985e 384 pid->integral = 1.0f;
FatCookies 17:6ae90788cc2b 385 }
FatCookies 27:627d67e3b9b0 386 if(pid->integral < -1.0f ) {
FatCookies 17:6ae90788cc2b 387 pid->integral = -1.0f;
FatCookies 17:6ae90788cc2b 388 }
FatCookies 17:6ae90788cc2b 389 }
FatCookies 17:6ae90788cc2b 390
lh14g13 19:65f0b6febc23 391
FatCookies 17:6ae90788cc2b 392 inline void PIDController() {
FatCookies 27:627d67e3b9b0 393 // update motor measurements
lh14g13 19:65f0b6febc23 394 // Read the angular velocity of both wheels
FatCookies 27:627d67e3b9b0 395
FatCookies 27:627d67e3b9b0 396 prevL = wL;
FatCookies 27:627d67e3b9b0 397 prevR = wR;
FatCookies 27:627d67e3b9b0 398
lh14g13 19:65f0b6febc23 399 wL=Get_Speed(Time_L);
lh14g13 19:65f0b6febc23 400 wR=Get_Speed(Time_R);
FatCookies 20:ed954836d028 401
FatCookies 20:ed954836d028 402 // Check if left wheel is slipping/giving an abnormal reading and ignore reading
maximusismax 31:1a06c9e1985e 403 if(wL - prevL < 1.2/0.025) { //<3 magic numbers: 48....?
FatCookies 20:ed954836d028 404 left_motor_pid.measured_value = wL;
FatCookies 20:ed954836d028 405 }
FatCookies 20:ed954836d028 406 if(wR - prevR < 1.2/0.025) {
FatCookies 20:ed954836d028 407 right_motor_pid.measured_value = wR;
FatCookies 20:ed954836d028 408 }
FatCookies 17:6ae90788cc2b 409
FatCookies 21:0b69fada7c5f 410
FatCookies 17:6ae90788cc2b 411 //PID Stuff!
FatCookies 17:6ae90788cc2b 412 t.start();
FatCookies 17:6ae90788cc2b 413 handlePID(&servo_pid);
oj3g13 35:e23354abf352 414 //enables the ED
lh14g13 39:7b28ee39185d 415 sensorCorner(left_motor_pid.desired_value,right_motor_pid.desired_value, servo_pid.output,speed,ed_tune);
FatCookies 17:6ae90788cc2b 416 handlePID(&left_motor_pid);
FatCookies 17:6ae90788cc2b 417 handlePID(&right_motor_pid);
FatCookies 17:6ae90788cc2b 418
FatCookies 17:6ae90788cc2b 419 if((-1.0 <= servo_pid.output) && (servo_pid.output <= 1.0))
FatCookies 17:6ae90788cc2b 420 {
FatCookies 17:6ae90788cc2b 421 TFC_SetServo(0, servo_pid.output);
FatCookies 17:6ae90788cc2b 422 }
FatCookies 17:6ae90788cc2b 423 else //Unhappy PID state
FatCookies 17:6ae90788cc2b 424 {
FatCookies 17:6ae90788cc2b 425 //sendString("out = %f p_err = %f", servo_pid.output, servo_pid.p_error);
maximusismax 31:1a06c9e1985e 426 //ALIGN_SERVO;
maximusismax 31:1a06c9e1985e 427 //Could cause the car to be travelling along one side of the track rather than in the middle
FatCookies 17:6ae90788cc2b 428 if(servo_pid.output >= 1.0f) {
FatCookies 17:6ae90788cc2b 429 TFC_SetServo(0, 0.9f);
FatCookies 17:6ae90788cc2b 430 servo_pid.output = 1.0f;
FatCookies 17:6ae90788cc2b 431 } else {
FatCookies 17:6ae90788cc2b 432 TFC_SetServo(0, -0.9f);
FatCookies 17:6ae90788cc2b 433 servo_pid.output = -1.0f;
FatCookies 17:6ae90788cc2b 434 }
FatCookies 17:6ae90788cc2b 435 }
FatCookies 17:6ae90788cc2b 436
lh14g13 42:4395ede5781e 437 // stops the motors from going into reverse and from being set to a value higher than 1.
FatCookies 20:ed954836d028 438 if(left_motor_pid.output > 1.0f) {
FatCookies 20:ed954836d028 439 left_motor_pid.output = 1.0f;
lh14g13 19:65f0b6febc23 440 }
FatCookies 29:b5b31256572b 441 if(left_motor_pid.output < 0.0f) {
FatCookies 27:627d67e3b9b0 442 left_motor_pid.output = 0.0f;
lh14g13 19:65f0b6febc23 443 }
lh14g13 19:65f0b6febc23 444
FatCookies 20:ed954836d028 445 if(right_motor_pid.output > 1.0f) {
FatCookies 20:ed954836d028 446 right_motor_pid.output = 1.0f;
FatCookies 20:ed954836d028 447 }
FatCookies 29:b5b31256572b 448 if(right_motor_pid.output < 0.0f) {
FatCookies 27:627d67e3b9b0 449 right_motor_pid.output = 0.0f;
FatCookies 20:ed954836d028 450 }
lh14g13 19:65f0b6febc23 451
FatCookies 20:ed954836d028 452 TFC_SetMotorPWM(left_motor_pid.output,right_motor_pid.output);
lh14g13 19:65f0b6febc23 453
FatCookies 17:6ae90788cc2b 454 t.stop();
FatCookies 17:6ae90788cc2b 455 t.reset();
FatCookies 17:6ae90788cc2b 456 t.start();
FatCookies 17:6ae90788cc2b 457 }
FatCookies 17:6ae90788cc2b 458
FatCookies 17:6ae90788cc2b 459 inline void handleStartStop() {
maximusismax 31:1a06c9e1985e 460
maximusismax 31:1a06c9e1985e 461 //Function to detect the NXP cup stop marker
maximusismax 22:973b95478663 462
maximusismax 31:1a06c9e1985e 463 int slower = 0; //Only send a string every few frames
maximusismax 31:1a06c9e1985e 464 int difference = 0; //Holds the difference between intensities of consecutive pixels
oj3g13 26:f3d770f3eda1 465 int lastPixel, currentPixel, transitionsSeen;
maximusismax 22:973b95478663 466 lastPixel = -1;
maximusismax 22:973b95478663 467 transitionsSeen = 0;
maximusismax 31:1a06c9e1985e 468 //Starting near the left edge, step right, counting transitions.
maximusismax 31:1a06c9e1985e 469 //If there are several (exact value varies, best established experimentally), it is the marker
maximusismax 22:973b95478663 470 for(int i = 30; i < 98; i++) {
oj3g13 26:f3d770f3eda1 471 currentPixel = (int)(CLOSE_CAMERA[i] >> 4) & 0xFF;
maximusismax 22:973b95478663 472 difference = lastPixel - currentPixel;
FatCookies 27:627d67e3b9b0 473 if(abs(difference) > 10 && lastPixel != -1){ //transition seen, increment counter
maximusismax 22:973b95478663 474 transitionsSeen++;
oj3g13 26:f3d770f3eda1 475 i+=5;
maximusismax 22:973b95478663 476 }
maximusismax 22:973b95478663 477 lastPixel = currentPixel;
maximusismax 22:973b95478663 478 }
maximusismax 31:1a06c9e1985e 479 //Was used to send an indication that the marker was seen, useful for debugging
FatCookies 45:3435bdd2d487 480 //if (slower % 1000 == `0) {
oj3g13 26:f3d770f3eda1 481 //sendString("Transitions seen: %d", transitionsSeen);
oj3g13 26:f3d770f3eda1 482 //}
maximusismax 31:1a06c9e1985e 483 //slower++;
oj3g13 26:f3d770f3eda1 484 if(transitionsSeen >= 5) {
maximusismax 22:973b95478663 485 //Stop the car!
FatCookies 43:649473c5a12b 486 #if USE_COMMS
FatCookies 45:3435bdd2d487 487 //sendString("Start/stop seen");
FatCookies 45:3435bdd2d487 488 //lapTime();
FatCookies 43:649473c5a12b 489 #endif
oj3g13 26:f3d770f3eda1 490 TFC_SetMotorPWM(0.f,0.f);
oj3g13 26:f3d770f3eda1 491 TFC_HBRIDGE_DISABLE;
FatCookies 43:649473c5a12b 492
oj3g13 26:f3d770f3eda1 493 }
FatCookies 17:6ae90788cc2b 494 }
FatCookies 17:6ae90788cc2b 495
FatCookies 45:3435bdd2d487 496 float checkAcc() {
FatCookies 45:3435bdd2d487 497 return abs(acc.getAccY());
FatCookies 45:3435bdd2d487 498 }
FatCookies 45:3435bdd2d487 499
FatCookies 17:6ae90788cc2b 500
FatCookies 17:6ae90788cc2b 501 inline void initSpeedSensors() {
FatCookies 17:6ae90788cc2b 502 t1.start();
FatCookies 17:6ae90788cc2b 503 t2.start();
FatCookies 17:6ae90788cc2b 504
FatCookies 17:6ae90788cc2b 505 //Left and Right are defined looking at the rear of the car, in the direction the camera points at.
FatCookies 17:6ae90788cc2b 506 leftHallSensor.rise(&GetTime_L);
FatCookies 17:6ae90788cc2b 507 rightHallSensor.rise(&GetTime_R);
FatCookies 17:6ae90788cc2b 508 }
FatCookies 17:6ae90788cc2b 509
FatCookies 17:6ae90788cc2b 510 void GetTime_L(){
FatCookies 17:6ae90788cc2b 511 Time_L=t1.read_us();
FatCookies 17:6ae90788cc2b 512 t1.reset();
FatCookies 17:6ae90788cc2b 513 }
FatCookies 17:6ae90788cc2b 514
FatCookies 17:6ae90788cc2b 515 void GetTime_R(){
FatCookies 17:6ae90788cc2b 516 Time_R=t2.read_us();
FatCookies 17:6ae90788cc2b 517 t2.reset();
FatCookies 17:6ae90788cc2b 518 }
FatCookies 17:6ae90788cc2b 519
FatCookies 17:6ae90788cc2b 520 #if USE_COMMS
FatCookies 17:6ae90788cc2b 521 void sendBattery() {
FatCookies 17:6ae90788cc2b 522
FatCookies 17:6ae90788cc2b 523 if(frame_counter % 256 == 0) {
FatCookies 17:6ae90788cc2b 524 float level = TFC_ReadBatteryVoltage() * 6.25;
FatCookies 40:10e8e80af7da 525 pc.putc(BATTERY_LEVEL);
FatCookies 43:649473c5a12b 526 writeFloat(level);
FatCookies 17:6ae90788cc2b 527 }
FatCookies 17:6ae90788cc2b 528 }
FatCookies 17:6ae90788cc2b 529
FatCookies 17:6ae90788cc2b 530 void sendString(const char *format, ...) {
FatCookies 17:6ae90788cc2b 531 va_list arg;
FatCookies 17:6ae90788cc2b 532
FatCookies 40:10e8e80af7da 533 pc.putc(STATUS_STRING);
FatCookies 17:6ae90788cc2b 534 va_start (arg, format);
FatCookies 17:6ae90788cc2b 535 pc.vprintf(format,arg);
FatCookies 17:6ae90788cc2b 536 va_end (arg);
FatCookies 17:6ae90788cc2b 537 pc.putc(0);
FatCookies 17:6ae90788cc2b 538 }
FatCookies 17:6ae90788cc2b 539
maximusismax 8:7c5e6b1e7aa5 540 inline void sendImage() {
maximusismax 8:7c5e6b1e7aa5 541 //Only send 1/3 of camera frames to GUI program
maximusismax 8:7c5e6b1e7aa5 542 if((frame_counter % 3) == 0) {
FatCookies 40:10e8e80af7da 543 pc.putc(SEND_LINE);
FatCookies 15:ccde02f96449 544 if(sendCam == 0) {
FatCookies 15:ccde02f96449 545 for(i = 0; i < 128; i++) {
FatCookies 17:6ae90788cc2b 546 pc.putc((int8_t)(CLOSE_CAMERA[i] >> 4) & 0xFF);
FatCookies 15:ccde02f96449 547 }
FatCookies 15:ccde02f96449 548 } else {
FatCookies 15:ccde02f96449 549 for(i = 0; i < 128; i++) {
FatCookies 17:6ae90788cc2b 550 pc.putc((int8_t)(LOOKAHEAD_CAMERA[i] >> 4) & 0xFF);
FatCookies 15:ccde02f96449 551 }
FatCookies 15:ccde02f96449 552 }
FatCookies 13:4e77264f254a 553 sendBattery();
FatCookies 13:4e77264f254a 554 }
FatCookies 13:4e77264f254a 555
FatCookies 13:4e77264f254a 556 frame_counter++;
FatCookies 13:4e77264f254a 557 }
FatCookies 13:4e77264f254a 558
FatCookies 13:4e77264f254a 559 inline void sendSpeeds() {
FatCookies 43:649473c5a12b 560 pc.putc(SEND_SPEEDS);
FatCookies 43:649473c5a12b 561 writeFloat(wL * WHEEL_RADIUS);
FatCookies 43:649473c5a12b 562 writeFloat(wR * WHEEL_RADIUS);
FatCookies 43:649473c5a12b 563 }
FatCookies 14:13085e161dd1 564
FatCookies 43:649473c5a12b 565 void writeFloat(float f) {
FatCookies 43:649473c5a12b 566 byte_float_union._float = f;
FatCookies 27:627d67e3b9b0 567 pc.putc(byte_float_union.bytes[0]);
FatCookies 27:627d67e3b9b0 568 pc.putc(byte_float_union.bytes[1]);
FatCookies 27:627d67e3b9b0 569 pc.putc(byte_float_union.bytes[2]);
FatCookies 27:627d67e3b9b0 570 pc.putc(byte_float_union.bytes[3]);
maximusismax 8:7c5e6b1e7aa5 571 }
maximusismax 8:7c5e6b1e7aa5 572
FatCookies 41:d74878640739 573 float readFloat() {
FatCookies 41:d74878640739 574 byte_float_union.bytes[0] = xb.cBuffer->read();
FatCookies 41:d74878640739 575 byte_float_union.bytes[1] = xb.cBuffer->read();
FatCookies 41:d74878640739 576 byte_float_union.bytes[2] = xb.cBuffer->read();
FatCookies 41:d74878640739 577 byte_float_union.bytes[3] = xb.cBuffer->read();
FatCookies 41:d74878640739 578 return byte_float_union._float;
FatCookies 41:d74878640739 579 }
FatCookies 41:d74878640739 580
FatCookies 40:10e8e80af7da 581 // Listen for incoming commands from telemetry program and change car variables
maximusismax 8:7c5e6b1e7aa5 582 inline void handleComms() {
maximusismax 8:7c5e6b1e7aa5 583 if(curr_cmd != 0) {
FatCookies 4:4afa448c9cce 584 switch(curr_cmd) {
FatCookies 40:10e8e80af7da 585 // Change the PID values of the servo controller
FatCookies 40:10e8e80af7da 586 case CHANGE_PID:
FatCookies 20:ed954836d028 587 if(xb.cBuffer->available() >= 12) {
FatCookies 20:ed954836d028 588
FatCookies 41:d74878640739 589 servo_pid.Kp = readFloat();
FatCookies 41:d74878640739 590 servo_pid.Ki = readFloat();
FatCookies 41:d74878640739 591 servo_pid.Kd = readFloat();
FatCookies 20:ed954836d028 592
FatCookies 20:ed954836d028 593 sendString("pid= Kp: %f, Ki: %f, Kd: %f", servo_pid.Kp, servo_pid.Ki, servo_pid.Kd);
maximusismax 8:7c5e6b1e7aa5 594
FatCookies 4:4afa448c9cce 595 curr_cmd = 0;
FatCookies 4:4afa448c9cce 596 }
FatCookies 4:4afa448c9cce 597 break;
FatCookies 4:4afa448c9cce 598
FatCookies 40:10e8e80af7da 599 // Set the maximum speed that the motor controllers should attempt to drive the car to
FatCookies 40:10e8e80af7da 600 case MAX_SPEED:
FatCookies 6:b0e160c51013 601 if(xb.cBuffer->available() >= 1) {
FatCookies 4:4afa448c9cce 602 char a = xb.cBuffer->read();
FatCookies 27:627d67e3b9b0 603 speed = a;
FatCookies 13:4e77264f254a 604 sendString("s = %u %f",a, speed);
FatCookies 4:4afa448c9cce 605 curr_cmd = 0;
FatCookies 29:b5b31256572b 606 right_motor_pid.desired_value=speed;
FatCookies 29:b5b31256572b 607 left_motor_pid.desired_value=speed;
FatCookies 4:4afa448c9cce 608 }
FatCookies 4:4afa448c9cce 609 break;
lh14g13 39:7b28ee39185d 610
FatCookies 40:10e8e80af7da 611 // Change the electronic differential coefficient
FatCookies 40:10e8e80af7da 612 case CHANGE_ED:
lh14g13 39:7b28ee39185d 613 if(xb.cBuffer->available() >= 4) {
FatCookies 41:d74878640739 614 ed_tune = readFloat();
lh14g13 39:7b28ee39185d 615 curr_cmd = 0;
lh14g13 39:7b28ee39185d 616 }
lh14g13 39:7b28ee39185d 617 break;
FatCookies 4:4afa448c9cce 618
FatCookies 4:4afa448c9cce 619 default:
FatCookies 13:4e77264f254a 620 // Unrecognised command
FatCookies 13:4e77264f254a 621 curr_cmd = 0;
FatCookies 4:4afa448c9cce 622 break;
FatCookies 4:4afa448c9cce 623 }
FatCookies 4:4afa448c9cce 624 }
FatCookies 4:4afa448c9cce 625
FatCookies 6:b0e160c51013 626 if(xb.cBuffer->available() > 0 && curr_cmd == 0) {
FatCookies 40:10e8e80af7da 627
FatCookies 4:4afa448c9cce 628 char cmd = xb.cBuffer->read();
FatCookies 40:10e8e80af7da 629
FatCookies 40:10e8e80af7da 630 //Start car the car motors
FatCookies 40:10e8e80af7da 631 if(cmd == START) {
FatCookies 17:6ae90788cc2b 632 ALIGN_SERVO;
FatCookies 21:0b69fada7c5f 633 right_motor_pid.desired_value=speed;
FatCookies 21:0b69fada7c5f 634 left_motor_pid.desired_value=speed;
FatCookies 4:4afa448c9cce 635 TFC_HBRIDGE_ENABLE;
FatCookies 29:b5b31256572b 636
FatCookies 21:0b69fada7c5f 637
FatCookies 12:da96e2f87465 638 servo_pid.integral = 0;
FatCookies 27:627d67e3b9b0 639 lapTimer.start();
lh14g13 18:0095a3a8f8e4 640 lapNo =0;
FatCookies 6:b0e160c51013 641
FatCookies 40:10e8e80af7da 642 // Stop the car motors
FatCookies 40:10e8e80af7da 643 } else if (cmd == STOP) {
FatCookies 4:4afa448c9cce 644 TFC_SetMotorPWM(0.0,0.0);
lh14g13 19:65f0b6febc23 645 right_motor_pid.desired_value=0;
FatCookies 21:0b69fada7c5f 646 right_motor_pid.measured_value = 0;
FatCookies 21:0b69fada7c5f 647 wR = 0;
FatCookies 21:0b69fada7c5f 648 prevR = 0;
FatCookies 21:0b69fada7c5f 649
lh14g13 19:65f0b6febc23 650 left_motor_pid.desired_value=0;
FatCookies 21:0b69fada7c5f 651 left_motor_pid.measured_value = 0;
FatCookies 21:0b69fada7c5f 652 wL = 0;
FatCookies 21:0b69fada7c5f 653 prevL = 0;
FatCookies 21:0b69fada7c5f 654
FatCookies 4:4afa448c9cce 655 TFC_HBRIDGE_DISABLE;
lh14g13 18:0095a3a8f8e4 656 endTest();
FatCookies 40:10e8e80af7da 657
FatCookies 40:10e8e80af7da 658 // Change the PID values of the servo controller
FatCookies 40:10e8e80af7da 659 } else if(cmd == CHANGE_PID) {
FatCookies 40:10e8e80af7da 660 curr_cmd = CHANGE_PID;
FatCookies 40:10e8e80af7da 661
FatCookies 40:10e8e80af7da 662 // Set the maximum speed of the motor controllers
FatCookies 40:10e8e80af7da 663 } else if(cmd == MAX_SPEED) {
FatCookies 40:10e8e80af7da 664 curr_cmd = MAX_SPEED;
FatCookies 40:10e8e80af7da 665
FatCookies 40:10e8e80af7da 666 // Switch the camera data that is sent over telemetry
FatCookies 40:10e8e80af7da 667 } else if(cmd == SWITCH_CAM) {
FatCookies 15:ccde02f96449 668 sendCam = ~sendCam;
FatCookies 40:10e8e80af7da 669
FatCookies 40:10e8e80af7da 670 // Modify the electronic differential coefficient
FatCookies 40:10e8e80af7da 671 } else if(cmd == CHANGE_ED) {
FatCookies 40:10e8e80af7da 672 curr_cmd = CHANGE_ED;
FatCookies 4:4afa448c9cce 673 }
FatCookies 4:4afa448c9cce 674
FatCookies 4:4afa448c9cce 675 }
maximusismax 8:7c5e6b1e7aa5 676 }
lh14g13 18:0095a3a8f8e4 677
lh14g13 18:0095a3a8f8e4 678 float testSpeed(float speed)
lh14g13 18:0095a3a8f8e4 679 {
lh14g13 18:0095a3a8f8e4 680 // search: Speed Increase
lh14g13 18:0095a3a8f8e4 681 // every time the car sees the stop start the speed of the car will increase
lh14g13 18:0095a3a8f8e4 682 // this can occur on stop start trigger.
lh14g13 18:0095a3a8f8e4 683 // may need to send the speed back to the telemetry.
lh14g13 18:0095a3a8f8e4 684 if (speed>0.4)
lh14g13 18:0095a3a8f8e4 685 {
lh14g13 18:0095a3a8f8e4 686 speed+=0.05;
lh14g13 18:0095a3a8f8e4 687 }
lh14g13 18:0095a3a8f8e4 688
lh14g13 18:0095a3a8f8e4 689 else
lh14g13 18:0095a3a8f8e4 690 {
lh14g13 18:0095a3a8f8e4 691 speed+=0.1;
lh14g13 18:0095a3a8f8e4 692
lh14g13 18:0095a3a8f8e4 693 }
lh14g13 18:0095a3a8f8e4 694
lh14g13 18:0095a3a8f8e4 695
lh14g13 18:0095a3a8f8e4 696 sendString("s = %f", speed);
lh14g13 18:0095a3a8f8e4 697 return speed;
lh14g13 18:0095a3a8f8e4 698
lh14g13 18:0095a3a8f8e4 699 }
lh14g13 18:0095a3a8f8e4 700
lh14g13 18:0095a3a8f8e4 701
lh14g13 18:0095a3a8f8e4 702
lh14g13 18:0095a3a8f8e4 703 int lapTime()
lh14g13 19:65f0b6febc23 704 {
lh14g13 18:0095a3a8f8e4 705 // function which sends the lap time back to the telemetry.
lh14g13 32:6829684f8c4d 706 //reads the timer
FatCookies 27:627d67e3b9b0 707 float newTime= lapTimer.read();
lh14g13 18:0095a3a8f8e4 708 lapNo += 1;
lh14g13 18:0095a3a8f8e4 709 float lapTime= newTime-oldTime;
lh14g13 18:0095a3a8f8e4 710 float avgTime= newTime/lapNo;
lh14g13 32:6829684f8c4d 711 // this calulates the average lap time and the lap time.
lh14g13 32:6829684f8c4d 712 //then sends the information back to the UI.
lh14g13 19:65f0b6febc23 713 sendString("For lap number: %d Lap Time: %f Avergae time: %f \n\r", lapNo,lapTime,avgTime);
lh14g13 18:0095a3a8f8e4 714
lh14g13 32:6829684f8c4d 715
lh14g13 18:0095a3a8f8e4 716 return 0;
lh14g13 18:0095a3a8f8e4 717 }
lh14g13 18:0095a3a8f8e4 718
lh14g13 18:0095a3a8f8e4 719
lh14g13 18:0095a3a8f8e4 720 void endTest()
lh14g13 18:0095a3a8f8e4 721 {// This runs when the car has stopped, this should give the final elapsed time and othere things. this also stops the timer
lh14g13 18:0095a3a8f8e4 722
FatCookies 27:627d67e3b9b0 723 float time= lapTimer.read();
lh14g13 18:0095a3a8f8e4 724
lh14g13 19:65f0b6febc23 725 sendString("Laps done: %d Time elapsed: %f Average time: %f \n\r",lapNo, time,float (time/lapNo));
FatCookies 27:627d67e3b9b0 726 lapTimer.stop();
lh14g13 18:0095a3a8f8e4 727
lh14g13 18:0095a3a8f8e4 728
lh14g13 19:65f0b6febc23 729 }
lh14g13 33:0fc789c09694 730 //motor controll specific(newfunction)
lh14g13 18:0095a3a8f8e4 731
lh14g13 18:0095a3a8f8e4 732
lh14g13 18:0095a3a8f8e4 733
lh14g13 18:0095a3a8f8e4 734
FatCookies 17:6ae90788cc2b 735 #endif