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:
Fri Dec 09 11:10:14 2016 +0000
Revision:
20:ed954836d028
Parent:
19:65f0b6febc23
Child:
21:0b69fada7c5f
Child:
24:15264aee54d1
add slip detection and changed pid sending to floats

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"
maximusismax 8:7c5e6b1e7aa5 18
FatCookies 17:6ae90788cc2b 19 // Serial
FatCookies 17:6ae90788cc2b 20 #if USE_COMMS
FatCookies 17:6ae90788cc2b 21 Serial pc(PTD3,PTD2);
FatCookies 17:6ae90788cc2b 22 XBEE xb(&pc);
FatCookies 17:6ae90788cc2b 23 #endif
FatCookies 3:87a5122682fa 24
FatCookies 17:6ae90788cc2b 25 // PID Timer
FatCookies 3:87a5122682fa 26 Timer t;
FatCookies 4:4afa448c9cce 27
FatCookies 17:6ae90788cc2b 28 //Speed Sensors interupts and timers
FatCookies 9:aa2ce38dec6b 29 InterruptIn leftHallSensor(D0);
FatCookies 9:aa2ce38dec6b 30 InterruptIn rightHallSensor(D2);
FatCookies 9:aa2ce38dec6b 31 Timer t1;
FatCookies 9:aa2ce38dec6b 32 Timer t2;
FatCookies 9:aa2ce38dec6b 33
lh14g13 18:0095a3a8f8e4 34 //testing timer
lh14g13 18:0095a3a8f8e4 35 Timer test;
FatCookies 16:81cdffd8c5d5 36
maximusismax 0:566127ca8048 37 int main() {
maximusismax 8:7c5e6b1e7aa5 38 //Set up TFC driver stuff
maximusismax 0:566127ca8048 39 TFC_Init();
FatCookies 17:6ae90788cc2b 40 ALIGN_SERVO;
FatCookies 13:4e77264f254a 41
FatCookies 17:6ae90788cc2b 42 #if USE_COMMS
FatCookies 17:6ae90788cc2b 43 //Setup baud rate for serial link, do not change!
FatCookies 17:6ae90788cc2b 44 pc.baud(BAUD_RATE);
FatCookies 17:6ae90788cc2b 45 #endif
maximusismax 0:566127ca8048 46
maximusismax 8:7c5e6b1e7aa5 47 //Initialise/reset PID variables
maximusismax 8:7c5e6b1e7aa5 48 initVariables();
FatCookies 9:aa2ce38dec6b 49 initSpeedSensors();
FatCookies 12:da96e2f87465 50
FatCookies 13:4e77264f254a 51
maximusismax 0:566127ca8048 52 while(1) {
FatCookies 3:87a5122682fa 53
FatCookies 17:6ae90788cc2b 54 #if USE_COMMS
FatCookies 17:6ae90788cc2b 55 handleComms();
FatCookies 17:6ae90788cc2b 56 #endif
maximusismax 8:7c5e6b1e7aa5 57
maximusismax 8:7c5e6b1e7aa5 58 //If we have an image ready
FatCookies 13:4e77264f254a 59 if(TFC_LineScanImageReady>0) {
FatCookies 13:4e77264f254a 60 /* Find the bounds of the track and calculate how close we are to
FatCookies 13:4e77264f254a 61 * the centre */
FatCookies 17:6ae90788cc2b 62 servo_pid.measured_value = findCentreValue(CLOSE_CAMERA, left, right);
maximusismax 8:7c5e6b1e7aa5 63
lh14g13 19:65f0b6febc23 64
FatCookies 17:6ae90788cc2b 65 // Slow down, adjust PID values and enable differential before corners.
FatCookies 17:6ae90788cc2b 66 handleCornering();
FatCookies 17:6ae90788cc2b 67
FatCookies 13:4e77264f254a 68 // Run the PID controllers and adjust steering/motor accordingly
maximusismax 8:7c5e6b1e7aa5 69 PIDController();
maximusismax 8:7c5e6b1e7aa5 70
FatCookies 17:6ae90788cc2b 71 #if USE_COMMS
FatCookies 17:6ae90788cc2b 72 // Send the line scan image over serial
FatCookies 17:6ae90788cc2b 73 sendImage();
FatCookies 17:6ae90788cc2b 74
FatCookies 17:6ae90788cc2b 75 // Send the wheel speeds over serial
FatCookies 17:6ae90788cc2b 76 sendSpeeds();
FatCookies 17:6ae90788cc2b 77 #endif
FatCookies 13:4e77264f254a 78
FatCookies 13:4e77264f254a 79 // Check if car is at the stop line
FatCookies 20:ed954836d028 80 handleStartStop();
maximusismax 8:7c5e6b1e7aa5 81
FatCookies 15:ccde02f96449 82
maximusismax 8:7c5e6b1e7aa5 83 //Reset image ready flag
maximusismax 8:7c5e6b1e7aa5 84 TFC_LineScanImageReady=0;
maximusismax 8:7c5e6b1e7aa5 85 }
maximusismax 8:7c5e6b1e7aa5 86 }
maximusismax 8:7c5e6b1e7aa5 87 }
maximusismax 8:7c5e6b1e7aa5 88
FatCookies 17:6ae90788cc2b 89 void initVariables() {
FatCookies 17:6ae90788cc2b 90 // Initialise three PID controllers for the servo and each wheel.
FatCookies 17:6ae90788cc2b 91 initPID(&servo_pid, 2.2f, 0.6f, 0.f);
FatCookies 20:ed954836d028 92 initPID(&left_motor_pid, 0.02f, 0.f, 0.f);
FatCookies 20:ed954836d028 93 initPID(&right_motor_pid, 0.02f, 0.f, 0.f);
FatCookies 17:6ae90788cc2b 94
lh14g13 19:65f0b6febc23 95 right_motor_pid.desired_value=0;
lh14g13 19:65f0b6febc23 96 left_motor_pid.desired_value=0;
lh14g13 19:65f0b6febc23 97
lh14g13 19:65f0b6febc23 98 // intialise the maximum speed interms of the angular speed.
FatCookies 17:6ae90788cc2b 99 valBufferIndex = 0;
lh14g13 19:65f0b6febc23 100 speed = 50;
FatCookies 13:4e77264f254a 101
FatCookies 17:6ae90788cc2b 102 //Start stop
FatCookies 17:6ae90788cc2b 103 startstop = 0;
FatCookies 17:6ae90788cc2b 104 seen = false;
FatCookies 17:6ae90788cc2b 105
FatCookies 17:6ae90788cc2b 106 // Turning
FatCookies 17:6ae90788cc2b 107 turning = 0;
FatCookies 17:6ae90788cc2b 108 keepTurning = 0;
FatCookies 17:6ae90788cc2b 109 slow = false;
FatCookies 20:ed954836d028 110
FatCookies 20:ed954836d028 111 wL = 0;
FatCookies 20:ed954836d028 112 wR = 0;
FatCookies 20:ed954836d028 113 prevL = 0;
FatCookies 20:ed954836d028 114 prevR = 0;
lh14g13 19:65f0b6febc23 115
maximusismax 8:7c5e6b1e7aa5 116 }
maximusismax 8:7c5e6b1e7aa5 117
FatCookies 13:4e77264f254a 118 void initPID(pid_instance* pid, float Kp, float Ki, float Kd) {
FatCookies 17:6ae90788cc2b 119 pid->Kp = Kp;
FatCookies 13:4e77264f254a 120 pid->Ki = Ki;
FatCookies 13:4e77264f254a 121 pid->Kd = Kd;
FatCookies 13:4e77264f254a 122 pid->dt = 0;
FatCookies 13:4e77264f254a 123 pid->p_error = 0;
FatCookies 13:4e77264f254a 124 pid->pid_error = 0;
FatCookies 13:4e77264f254a 125 pid->integral = 0;
FatCookies 13:4e77264f254a 126 pid->measured_value = 0;
FatCookies 13:4e77264f254a 127 pid->desired_value = 0;
FatCookies 13:4e77264f254a 128 pid->derivative = 0;
FatCookies 13:4e77264f254a 129 }
FatCookies 13:4e77264f254a 130
FatCookies 17:6ae90788cc2b 131 inline float findCentreValue(volatile uint16_t * cam_data, uint8_t &l, uint8_t &r) {
FatCookies 17:6ae90788cc2b 132
FatCookies 17:6ae90788cc2b 133 diff = 0;
FatCookies 17:6ae90788cc2b 134 prev = -1;
FatCookies 17:6ae90788cc2b 135 for(i = 63; i > 0; i--) {
FatCookies 17:6ae90788cc2b 136 curr_left = (int8_t)(cam_data[i] >> 4) & 0xFF;
FatCookies 17:6ae90788cc2b 137 diff = prev - curr_left;
FatCookies 17:6ae90788cc2b 138 if(abs(diff) >= CAM_DIFF && curr_left <= CAM_THRESHOLD && prev != -1) {
FatCookies 17:6ae90788cc2b 139 l = i;
FatCookies 17:6ae90788cc2b 140 break;
FatCookies 17:6ae90788cc2b 141 }
FatCookies 17:6ae90788cc2b 142 prev = curr_left;
FatCookies 17:6ae90788cc2b 143 }
maximusismax 8:7c5e6b1e7aa5 144
FatCookies 17:6ae90788cc2b 145 prev = -1;
FatCookies 17:6ae90788cc2b 146 for(i = 64; i < 128; i++) {
FatCookies 17:6ae90788cc2b 147 curr_right = (int8_t)(cam_data[i] >> 4) & 0xFF;
FatCookies 17:6ae90788cc2b 148 int diff = prev - curr_right;
FatCookies 17:6ae90788cc2b 149 if(abs(diff) >= CAM_DIFF && curr_right <= CAM_THRESHOLD && prev != -1) {
FatCookies 17:6ae90788cc2b 150 r = i;
FatCookies 17:6ae90788cc2b 151 break;
FatCookies 17:6ae90788cc2b 152 }
FatCookies 17:6ae90788cc2b 153 prev = curr_right;
FatCookies 17:6ae90788cc2b 154 }
FatCookies 17:6ae90788cc2b 155
FatCookies 17:6ae90788cc2b 156 //Calculate how left/right we are
FatCookies 17:6ae90788cc2b 157 return (64 - ((l+r)/2))/64.f;
FatCookies 13:4e77264f254a 158 }
FatCookies 13:4e77264f254a 159
FatCookies 17:6ae90788cc2b 160 inline void handleCornering() {
FatCookies 12:da96e2f87465 161
FatCookies 17:6ae90788cc2b 162 float lookaheadMeasuredValue = findCentreValue(LOOKAHEAD_CAMERA, farLeft, farRight);
FatCookies 15:ccde02f96449 163
FatCookies 13:4e77264f254a 164 measuredValBuffer[frame_counter % 64] = servo_pid.measured_value;
FatCookies 13:4e77264f254a 165
FatCookies 13:4e77264f254a 166 int count = 0;
FatCookies 13:4e77264f254a 167 for(i = 0; i < 10; i++) {
FatCookies 13:4e77264f254a 168 float val = abs(measuredValBuffer[(frame_counter - i) % 64]);
FatCookies 13:4e77264f254a 169 if(val > 0.09) {
FatCookies 13:4e77264f254a 170 count++;
FatCookies 13:4e77264f254a 171 }
FatCookies 13:4e77264f254a 172 }
FatCookies 12:da96e2f87465 173
FatCookies 17:6ae90788cc2b 174 if(!turning && abs(lookaheadMeasuredValue) > 0.11f){
FatCookies 15:ccde02f96449 175 TFC_SetMotorPWM(0.4,0.4);
FatCookies 15:ccde02f96449 176 }
FatCookies 15:ccde02f96449 177
FatCookies 14:13085e161dd1 178 if(turning) {
lh14g13 18:0095a3a8f8e4 179
lh14g13 18:0095a3a8f8e4 180 //default
lh14g13 18:0095a3a8f8e4 181 //TFC_SetMotorPWM(0.4,0.4);
lh14g13 18:0095a3a8f8e4 182
lh14g13 19:65f0b6febc23 183 //dutyCycleCorner(speed,servo_pid.output);
lh14g13 18:0095a3a8f8e4 184
lh14g13 18:0095a3a8f8e4 185 //may want to have just to set cornering speed at different if going to be slowing down for cornering.
lh14g13 18:0095a3a8f8e4 186 //dutyCycleCorner(float cornerspeed, servo_pid.output);
lh14g13 18:0095a3a8f8e4 187 //dutyCycleCorner(speed, servo_pid.output);
lh14g13 18:0095a3a8f8e4 188
FatCookies 14:13085e161dd1 189 //sensorCorner(left_motor_pid.desired_value, right_motor_pid.desired_value , servo_pid.output, 50);
FatCookies 14:13085e161dd1 190 }
FatCookies 14:13085e161dd1 191
FatCookies 13:4e77264f254a 192 if(abs(servo_pid.measured_value) > 0.11f){
FatCookies 15:ccde02f96449 193 if(!turning) {
FatCookies 13:4e77264f254a 194 turning = 1;
FatCookies 13:4e77264f254a 195 } else {
FatCookies 13:4e77264f254a 196 turning++;
FatCookies 13:4e77264f254a 197 }
FatCookies 13:4e77264f254a 198
FatCookies 13:4e77264f254a 199 } else {
FatCookies 13:4e77264f254a 200 if(turning) {
FatCookies 13:4e77264f254a 201 if(keepTurning == 0 || count > 6) {
FatCookies 13:4e77264f254a 202 keepTurning++;
FatCookies 13:4e77264f254a 203 } else {
FatCookies 15:ccde02f96449 204 //sendString("stop turning turned=%d",turning);
FatCookies 13:4e77264f254a 205 keepTurning = 0;
FatCookies 13:4e77264f254a 206 turning = 0;
FatCookies 13:4e77264f254a 207 TFC_SetMotorPWM(speed,speed);
FatCookies 13:4e77264f254a 208 }
FatCookies 13:4e77264f254a 209
FatCookies 13:4e77264f254a 210 }
FatCookies 13:4e77264f254a 211 }
FatCookies 13:4e77264f254a 212
maximusismax 8:7c5e6b1e7aa5 213 }
maximusismax 8:7c5e6b1e7aa5 214
FatCookies 17:6ae90788cc2b 215 inline float getLineEntropy() {
FatCookies 17:6ae90788cc2b 216 float entropy = 0;
FatCookies 17:6ae90788cc2b 217 float last = (int8_t)(CLOSE_CAMERA[0] >> 4) & 0xFF;
FatCookies 17:6ae90788cc2b 218 for(int i = 1; i < 128; i++) {
FatCookies 17:6ae90788cc2b 219 entropy += abs(last - ((int8_t)(CLOSE_CAMERA[i] >> 4) & 0xFF));
FatCookies 17:6ae90788cc2b 220 }
FatCookies 17:6ae90788cc2b 221 return entropy;
FatCookies 17:6ae90788cc2b 222 }
FatCookies 17:6ae90788cc2b 223
FatCookies 17:6ae90788cc2b 224 void handlePID(pid_instance *pid) {
FatCookies 17:6ae90788cc2b 225 pid->dt = t.read();
FatCookies 17:6ae90788cc2b 226 pid->pid_error = pid->desired_value - pid->measured_value;
FatCookies 17:6ae90788cc2b 227 pid->integral = pid->integral + pid->pid_error * pid->dt;
FatCookies 17:6ae90788cc2b 228 pid->derivative = (pid->pid_error - pid->p_error) / pid->dt;
FatCookies 17:6ae90788cc2b 229 pid->output = pid->Kp * pid->pid_error + pid->Ki * pid->integral + pid->Kd * pid->derivative;
FatCookies 17:6ae90788cc2b 230 pid->p_error = pid->pid_error;
FatCookies 17:6ae90788cc2b 231
FatCookies 17:6ae90788cc2b 232 if(pid->integral > 1.0f) {
FatCookies 17:6ae90788cc2b 233 pid->integral = 1.0f;
FatCookies 17:6ae90788cc2b 234 }
FatCookies 17:6ae90788cc2b 235 if(pid->integral < -1.0f) {
FatCookies 17:6ae90788cc2b 236 pid->integral = -1.0f;
FatCookies 17:6ae90788cc2b 237 }
FatCookies 17:6ae90788cc2b 238 }
FatCookies 17:6ae90788cc2b 239
lh14g13 19:65f0b6febc23 240
lh14g13 19:65f0b6febc23 241
FatCookies 17:6ae90788cc2b 242 inline void PIDController() {
FatCookies 17:6ae90788cc2b 243 // update motor measurements
lh14g13 19:65f0b6febc23 244 // Read the angular velocity of both wheels
lh14g13 19:65f0b6febc23 245 wL=Get_Speed(Time_L);
lh14g13 19:65f0b6febc23 246 wR=Get_Speed(Time_R);
FatCookies 20:ed954836d028 247
FatCookies 20:ed954836d028 248 // Check if left wheel is slipping/giving an abnormal reading and ignore reading
FatCookies 20:ed954836d028 249 if(wL - prevL < 1.2/0.025) {
FatCookies 20:ed954836d028 250 left_motor_pid.measured_value = wL;
FatCookies 20:ed954836d028 251 prevL = wL;
FatCookies 20:ed954836d028 252 } else {
FatCookies 20:ed954836d028 253 wL = prevL;
FatCookies 20:ed954836d028 254 }
FatCookies 20:ed954836d028 255
FatCookies 20:ed954836d028 256 // Same as above for right
FatCookies 20:ed954836d028 257 if(wR - prevR < 1.2/0.025) {
FatCookies 20:ed954836d028 258 right_motor_pid.measured_value = wR;
FatCookies 20:ed954836d028 259 prevR = wR;
FatCookies 20:ed954836d028 260 } else {
FatCookies 20:ed954836d028 261 wR = prevR;
FatCookies 20:ed954836d028 262 }
FatCookies 17:6ae90788cc2b 263
FatCookies 17:6ae90788cc2b 264 //PID Stuff!
FatCookies 17:6ae90788cc2b 265 t.start();
FatCookies 17:6ae90788cc2b 266 handlePID(&servo_pid);
FatCookies 17:6ae90788cc2b 267 handlePID(&left_motor_pid);
FatCookies 17:6ae90788cc2b 268 handlePID(&right_motor_pid);
FatCookies 17:6ae90788cc2b 269
FatCookies 17:6ae90788cc2b 270 if((-1.0 <= servo_pid.output) && (servo_pid.output <= 1.0))
FatCookies 17:6ae90788cc2b 271 {
FatCookies 17:6ae90788cc2b 272 TFC_SetServo(0, servo_pid.output);
FatCookies 17:6ae90788cc2b 273 }
FatCookies 17:6ae90788cc2b 274 else //Unhappy PID state
FatCookies 17:6ae90788cc2b 275 {
FatCookies 17:6ae90788cc2b 276 //sendString("out = %f p_err = %f", servo_pid.output, servo_pid.p_error);
FatCookies 17:6ae90788cc2b 277 ALIGN_SERVO;
FatCookies 17:6ae90788cc2b 278 if(servo_pid.output >= 1.0f) {
FatCookies 17:6ae90788cc2b 279 TFC_SetServo(0, 0.9f);
FatCookies 17:6ae90788cc2b 280 servo_pid.output = 1.0f;
FatCookies 17:6ae90788cc2b 281 } else {
FatCookies 17:6ae90788cc2b 282 TFC_SetServo(0, -0.9f);
FatCookies 17:6ae90788cc2b 283 servo_pid.output = -1.0f;
FatCookies 17:6ae90788cc2b 284 }
FatCookies 17:6ae90788cc2b 285 }
FatCookies 17:6ae90788cc2b 286
FatCookies 17:6ae90788cc2b 287
FatCookies 20:ed954836d028 288 if(left_motor_pid.output > 1.0f) {
FatCookies 20:ed954836d028 289 left_motor_pid.output = 1.0f;
lh14g13 19:65f0b6febc23 290 }
FatCookies 20:ed954836d028 291 if(left_motor_pid.output < -1.0f) {
FatCookies 20:ed954836d028 292 left_motor_pid.output = 0.f;
lh14g13 19:65f0b6febc23 293 }
lh14g13 19:65f0b6febc23 294
FatCookies 20:ed954836d028 295 if(right_motor_pid.output > 1.0f) {
FatCookies 20:ed954836d028 296 right_motor_pid.output = 1.0f;
FatCookies 20:ed954836d028 297 }
FatCookies 20:ed954836d028 298 if(right_motor_pid.output < -1.0f) {
FatCookies 20:ed954836d028 299 right_motor_pid.output = 0.f;
FatCookies 20:ed954836d028 300 }
lh14g13 19:65f0b6febc23 301
FatCookies 20:ed954836d028 302 TFC_SetMotorPWM(left_motor_pid.output,right_motor_pid.output);
lh14g13 19:65f0b6febc23 303
FatCookies 17:6ae90788cc2b 304 t.stop();
FatCookies 17:6ae90788cc2b 305 t.reset();
FatCookies 17:6ae90788cc2b 306 t.start();
FatCookies 17:6ae90788cc2b 307 }
FatCookies 17:6ae90788cc2b 308
FatCookies 17:6ae90788cc2b 309 inline void handleStartStop() {
FatCookies 17:6ae90788cc2b 310 //Hacky way to detect the start/stop signal
FatCookies 17:6ae90788cc2b 311 if(right - left < 60) {
lh14g13 18:0095a3a8f8e4 312 sendString("START STOP!! &d",startstop);//do you mean %d?
lh14g13 18:0095a3a8f8e4 313
FatCookies 20:ed954836d028 314 //lapTime();
lh14g13 18:0095a3a8f8e4 315 //testSpeed(speed) HOLY SHIT ITS DAT BOI!!!!!!!!
FatCookies 17:6ae90788cc2b 316 if(seen) {
FatCookies 17:6ae90788cc2b 317 seen = false;
FatCookies 17:6ae90788cc2b 318 } else {
FatCookies 17:6ae90788cc2b 319 startstop++;
FatCookies 17:6ae90788cc2b 320 seen = true;
FatCookies 17:6ae90788cc2b 321 }
FatCookies 17:6ae90788cc2b 322 //If we've done 5 laps, stop the car
FatCookies 17:6ae90788cc2b 323 if(startstop >= 1) {
FatCookies 17:6ae90788cc2b 324 TFC_SetMotorPWM(0.f,0.f);
FatCookies 17:6ae90788cc2b 325 TFC_HBRIDGE_DISABLE;
FatCookies 17:6ae90788cc2b 326 startstop = 0;
FatCookies 17:6ae90788cc2b 327 }
FatCookies 20:ed954836d028 328 }
FatCookies 17:6ae90788cc2b 329 }
FatCookies 17:6ae90788cc2b 330
FatCookies 17:6ae90788cc2b 331
FatCookies 17:6ae90788cc2b 332 inline void initSpeedSensors() {
FatCookies 17:6ae90788cc2b 333 t1.start();
FatCookies 17:6ae90788cc2b 334 t2.start();
FatCookies 17:6ae90788cc2b 335
FatCookies 17:6ae90788cc2b 336 //Left and Right are defined looking at the rear of the car, in the direction the camera points at.
FatCookies 17:6ae90788cc2b 337 leftHallSensor.rise(&GetTime_L);
FatCookies 17:6ae90788cc2b 338 rightHallSensor.rise(&GetTime_R);
FatCookies 17:6ae90788cc2b 339 }
FatCookies 17:6ae90788cc2b 340
FatCookies 17:6ae90788cc2b 341 void GetTime_L(){
FatCookies 17:6ae90788cc2b 342 Time_L=t1.read_us();
FatCookies 17:6ae90788cc2b 343 t1.reset();
FatCookies 17:6ae90788cc2b 344 }
FatCookies 17:6ae90788cc2b 345
FatCookies 17:6ae90788cc2b 346 void GetTime_R(){
FatCookies 17:6ae90788cc2b 347 Time_R=t2.read_us();
FatCookies 17:6ae90788cc2b 348 t2.reset();
FatCookies 17:6ae90788cc2b 349 }
FatCookies 17:6ae90788cc2b 350
FatCookies 17:6ae90788cc2b 351 #if USE_COMMS
FatCookies 17:6ae90788cc2b 352 void sendBattery() {
FatCookies 17:6ae90788cc2b 353
FatCookies 17:6ae90788cc2b 354 if(frame_counter % 256 == 0) {
FatCookies 17:6ae90788cc2b 355 float level = TFC_ReadBatteryVoltage() * 6.25;
FatCookies 17:6ae90788cc2b 356 pc.putc('J');
FatCookies 17:6ae90788cc2b 357 thing.a = level;
FatCookies 17:6ae90788cc2b 358 pc.putc(thing.bytes[0]);
FatCookies 17:6ae90788cc2b 359 pc.putc(thing.bytes[1]);
FatCookies 17:6ae90788cc2b 360 pc.putc(thing.bytes[2]);
FatCookies 17:6ae90788cc2b 361 pc.putc(thing.bytes[3]);
FatCookies 17:6ae90788cc2b 362 }
FatCookies 17:6ae90788cc2b 363 }
FatCookies 17:6ae90788cc2b 364
FatCookies 17:6ae90788cc2b 365 void sendString(const char *format, ...) {
FatCookies 17:6ae90788cc2b 366 va_list arg;
FatCookies 17:6ae90788cc2b 367
FatCookies 17:6ae90788cc2b 368 pc.putc('E');
FatCookies 17:6ae90788cc2b 369 va_start (arg, format);
FatCookies 17:6ae90788cc2b 370 pc.vprintf(format,arg);
FatCookies 17:6ae90788cc2b 371 va_end (arg);
FatCookies 17:6ae90788cc2b 372 pc.putc(0);
FatCookies 17:6ae90788cc2b 373 }
FatCookies 17:6ae90788cc2b 374
maximusismax 8:7c5e6b1e7aa5 375 inline void sendImage() {
maximusismax 8:7c5e6b1e7aa5 376 //Only send 1/3 of camera frames to GUI program
maximusismax 8:7c5e6b1e7aa5 377 if((frame_counter % 3) == 0) {
maximusismax 8:7c5e6b1e7aa5 378 pc.putc('H');
FatCookies 15:ccde02f96449 379 if(sendCam == 0) {
FatCookies 15:ccde02f96449 380 for(i = 0; i < 128; i++) {
FatCookies 17:6ae90788cc2b 381 pc.putc((int8_t)(CLOSE_CAMERA[i] >> 4) & 0xFF);
FatCookies 15:ccde02f96449 382 }
FatCookies 15:ccde02f96449 383 } else {
FatCookies 15:ccde02f96449 384 for(i = 0; i < 128; i++) {
FatCookies 17:6ae90788cc2b 385 pc.putc((int8_t)(LOOKAHEAD_CAMERA[i] >> 4) & 0xFF);
FatCookies 15:ccde02f96449 386 }
FatCookies 15:ccde02f96449 387 }
FatCookies 13:4e77264f254a 388 sendBattery();
FatCookies 13:4e77264f254a 389 }
FatCookies 13:4e77264f254a 390
FatCookies 13:4e77264f254a 391 frame_counter++;
FatCookies 13:4e77264f254a 392 }
FatCookies 13:4e77264f254a 393
FatCookies 13:4e77264f254a 394 inline void sendSpeeds() {
FatCookies 17:6ae90788cc2b 395
lh14g13 19:65f0b6febc23 396 /*float en = getLineEntropy();
FatCookies 15:ccde02f96449 397
FatCookies 15:ccde02f96449 398 if(onTrack) {
FatCookies 15:ccde02f96449 399 if(en <= 14000) {
FatCookies 15:ccde02f96449 400 onTrack = false;
FatCookies 15:ccde02f96449 401 sendString("offfffffffffffff");
FatCookies 15:ccde02f96449 402 TFC_SetMotorPWM(0.0,0.0);
FatCookies 15:ccde02f96449 403 TFC_HBRIDGE_DISABLE;
FatCookies 15:ccde02f96449 404 }
FatCookies 15:ccde02f96449 405 } else {
FatCookies 15:ccde02f96449 406 if(en > 14000) {
FatCookies 15:ccde02f96449 407 onTrack = true;
FatCookies 15:ccde02f96449 408 sendString("ON TRACK");
FatCookies 15:ccde02f96449 409 }
lh14g13 19:65f0b6febc23 410 }*/
FatCookies 15:ccde02f96449 411
FatCookies 14:13085e161dd1 412
FatCookies 13:4e77264f254a 413 pc.putc('B');
FatCookies 20:ed954836d028 414 thing.a = wL * WHEEL_RADIUS;//left_motor_pid.output; //
FatCookies 12:da96e2f87465 415 pc.putc(thing.bytes[0]);
FatCookies 12:da96e2f87465 416 pc.putc(thing.bytes[1]);
FatCookies 12:da96e2f87465 417 pc.putc(thing.bytes[2]);
FatCookies 12:da96e2f87465 418 pc.putc(thing.bytes[3]);
FatCookies 20:ed954836d028 419 thing.a = wR * WHEEL_RADIUS; // right_motor_pid.output; //
FatCookies 13:4e77264f254a 420 pc.putc(thing.bytes[0]);
FatCookies 13:4e77264f254a 421 pc.putc(thing.bytes[1]);
FatCookies 13:4e77264f254a 422 pc.putc(thing.bytes[2]);
FatCookies 13:4e77264f254a 423 pc.putc(thing.bytes[3]);
maximusismax 8:7c5e6b1e7aa5 424 }
maximusismax 8:7c5e6b1e7aa5 425
FatCookies 13:4e77264f254a 426
maximusismax 8:7c5e6b1e7aa5 427 inline void handleComms() {
maximusismax 8:7c5e6b1e7aa5 428 if(curr_cmd != 0) {
FatCookies 4:4afa448c9cce 429 switch(curr_cmd) {
FatCookies 4:4afa448c9cce 430 case 'A':
FatCookies 20:ed954836d028 431 if(xb.cBuffer->available() >= 12) {
FatCookies 20:ed954836d028 432 /*
FatCookies 4:4afa448c9cce 433 char p = xb.cBuffer->read();
FatCookies 4:4afa448c9cce 434 char i = xb.cBuffer->read();
FatCookies 4:4afa448c9cce 435 char d = xb.cBuffer->read();
FatCookies 12:da96e2f87465 436 servo_pid.Kp = p/25.0f;
FatCookies 12:da96e2f87465 437 servo_pid.Ki = i/25.0f;
FatCookies 12:da96e2f87465 438 servo_pid.Kd = d/25.0f;
FatCookies 13:4e77264f254a 439 sendString("pid= Kp: %f, Ki: %f, Kd: %f, p: %u, i: %u, d: %u", servo_pid.Kp, servo_pid.Ki, servo_pid.Kd, p, i, d);
FatCookies 20:ed954836d028 440 */
FatCookies 20:ed954836d028 441
FatCookies 20:ed954836d028 442 thing.bytes[0] = xb.cBuffer->read();
FatCookies 20:ed954836d028 443 thing.bytes[1] = xb.cBuffer->read();
FatCookies 20:ed954836d028 444 thing.bytes[2] = xb.cBuffer->read();
FatCookies 20:ed954836d028 445 thing.bytes[3] = xb.cBuffer->read();
FatCookies 20:ed954836d028 446 servo_pid.Kp = thing.a;
FatCookies 20:ed954836d028 447
FatCookies 20:ed954836d028 448 thing.bytes[0] = xb.cBuffer->read();
FatCookies 20:ed954836d028 449 thing.bytes[1] = xb.cBuffer->read();
FatCookies 20:ed954836d028 450 thing.bytes[2] = xb.cBuffer->read();
FatCookies 20:ed954836d028 451 thing.bytes[3] = xb.cBuffer->read();
FatCookies 20:ed954836d028 452 servo_pid.Ki = thing.a;
FatCookies 20:ed954836d028 453
FatCookies 20:ed954836d028 454 thing.bytes[0] = xb.cBuffer->read();
FatCookies 20:ed954836d028 455 thing.bytes[1] = xb.cBuffer->read();
FatCookies 20:ed954836d028 456 thing.bytes[2] = xb.cBuffer->read();
FatCookies 20:ed954836d028 457 thing.bytes[3] = xb.cBuffer->read();
FatCookies 20:ed954836d028 458 servo_pid.Kd = thing.a;
FatCookies 20:ed954836d028 459
FatCookies 20:ed954836d028 460 sendString("pid= Kp: %f, Ki: %f, Kd: %f", servo_pid.Kp, servo_pid.Ki, servo_pid.Kd);
maximusismax 8:7c5e6b1e7aa5 461
FatCookies 4:4afa448c9cce 462 curr_cmd = 0;
FatCookies 4:4afa448c9cce 463 }
FatCookies 4:4afa448c9cce 464 break;
FatCookies 4:4afa448c9cce 465
FatCookies 4:4afa448c9cce 466 case 'F':
FatCookies 6:b0e160c51013 467 if(xb.cBuffer->available() >= 1) {
FatCookies 4:4afa448c9cce 468 char a = xb.cBuffer->read();
lh14g13 19:65f0b6febc23 469 //speed = a/255;
lh14g13 19:65f0b6febc23 470 speed = (a/0.025f)/50.f;
lh14g13 19:65f0b6febc23 471 //TFC_SetMotorPWM(speed,speed);
FatCookies 13:4e77264f254a 472 sendString("s = %u %f",a, speed);
FatCookies 4:4afa448c9cce 473 curr_cmd = 0;
FatCookies 4:4afa448c9cce 474 }
FatCookies 4:4afa448c9cce 475 break;
FatCookies 4:4afa448c9cce 476
FatCookies 4:4afa448c9cce 477 default:
FatCookies 13:4e77264f254a 478 // Unrecognised command
FatCookies 13:4e77264f254a 479 curr_cmd = 0;
FatCookies 4:4afa448c9cce 480 break;
FatCookies 4:4afa448c9cce 481 }
FatCookies 4:4afa448c9cce 482 }
FatCookies 4:4afa448c9cce 483
FatCookies 6:b0e160c51013 484 if(xb.cBuffer->available() > 0 && curr_cmd == 0) {
lh14g13 18:0095a3a8f8e4 485 //Start car
FatCookies 4:4afa448c9cce 486 char cmd = xb.cBuffer->read();
FatCookies 4:4afa448c9cce 487 if(cmd == 'D') {
FatCookies 17:6ae90788cc2b 488 ALIGN_SERVO;
FatCookies 4:4afa448c9cce 489 TFC_HBRIDGE_ENABLE;
lh14g13 19:65f0b6febc23 490 right_motor_pid.desired_value=speed;
lh14g13 19:65f0b6febc23 491 left_motor_pid.desired_value=speed;
FatCookies 12:da96e2f87465 492 servo_pid.integral = 0;
lh14g13 18:0095a3a8f8e4 493 test.start();
lh14g13 18:0095a3a8f8e4 494 lapNo =0;
FatCookies 6:b0e160c51013 495
FatCookies 4:4afa448c9cce 496 } else if (cmd == 'C') {
FatCookies 4:4afa448c9cce 497 TFC_SetMotorPWM(0.0,0.0);
lh14g13 19:65f0b6febc23 498 right_motor_pid.desired_value=0;
lh14g13 19:65f0b6febc23 499 left_motor_pid.desired_value=0;
FatCookies 4:4afa448c9cce 500 TFC_HBRIDGE_DISABLE;
lh14g13 18:0095a3a8f8e4 501 endTest();
FatCookies 4:4afa448c9cce 502 } else if(cmd == 'A') {
FatCookies 4:4afa448c9cce 503 curr_cmd = 'A';
FatCookies 4:4afa448c9cce 504 } else if(cmd == 'F') {
FatCookies 4:4afa448c9cce 505 curr_cmd = 'F';
FatCookies 15:ccde02f96449 506 } else if(cmd == 'K') {
FatCookies 15:ccde02f96449 507 sendCam = ~sendCam;
FatCookies 4:4afa448c9cce 508 }
FatCookies 4:4afa448c9cce 509
FatCookies 4:4afa448c9cce 510 }
maximusismax 8:7c5e6b1e7aa5 511 }
lh14g13 18:0095a3a8f8e4 512
lh14g13 18:0095a3a8f8e4 513 float testSpeed(float speed)
lh14g13 18:0095a3a8f8e4 514 {
lh14g13 18:0095a3a8f8e4 515 // search: Speed Increase
lh14g13 18:0095a3a8f8e4 516 // every time the car sees the stop start the speed of the car will increase
lh14g13 18:0095a3a8f8e4 517 // this can occur on stop start trigger.
lh14g13 18:0095a3a8f8e4 518 // may need to send the speed back to the telemetry.
lh14g13 18:0095a3a8f8e4 519 if (speed>0.4)
lh14g13 18:0095a3a8f8e4 520 {
lh14g13 18:0095a3a8f8e4 521 speed+=0.05;
lh14g13 18:0095a3a8f8e4 522 }
lh14g13 18:0095a3a8f8e4 523
lh14g13 18:0095a3a8f8e4 524 else
lh14g13 18:0095a3a8f8e4 525 {
lh14g13 18:0095a3a8f8e4 526 speed+=0.1;
lh14g13 18:0095a3a8f8e4 527
lh14g13 18:0095a3a8f8e4 528 }
lh14g13 18:0095a3a8f8e4 529
lh14g13 18:0095a3a8f8e4 530
lh14g13 18:0095a3a8f8e4 531 sendString("s = %f", speed);
lh14g13 18:0095a3a8f8e4 532 return speed;
lh14g13 18:0095a3a8f8e4 533
lh14g13 18:0095a3a8f8e4 534 }
lh14g13 18:0095a3a8f8e4 535
lh14g13 18:0095a3a8f8e4 536
lh14g13 18:0095a3a8f8e4 537
lh14g13 18:0095a3a8f8e4 538 int lapTime()
lh14g13 19:65f0b6febc23 539 {
lh14g13 18:0095a3a8f8e4 540 // function which sends the lap time back to the telemetry.
lh14g13 18:0095a3a8f8e4 541 float newTime= test.read();
lh14g13 18:0095a3a8f8e4 542 lapNo += 1;
lh14g13 18:0095a3a8f8e4 543 float lapTime= newTime-oldTime;
lh14g13 18:0095a3a8f8e4 544 float avgTime= newTime/lapNo;
lh14g13 18:0095a3a8f8e4 545
lh14g13 19:65f0b6febc23 546 sendString("For lap number: %d Lap Time: %f Avergae time: %f \n\r", lapNo,lapTime,avgTime);
lh14g13 18:0095a3a8f8e4 547
lh14g13 18:0095a3a8f8e4 548 // OH WHAT UP IT'S DAT BOI!!!!
lh14g13 18:0095a3a8f8e4 549 return 0;
lh14g13 18:0095a3a8f8e4 550 }
lh14g13 18:0095a3a8f8e4 551
lh14g13 18:0095a3a8f8e4 552
lh14g13 18:0095a3a8f8e4 553 void endTest()
lh14g13 18:0095a3a8f8e4 554 {// 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 555
lh14g13 18:0095a3a8f8e4 556 float time= test.read();
lh14g13 18:0095a3a8f8e4 557
lh14g13 19:65f0b6febc23 558 sendString("Laps done: %d Time elapsed: %f Average time: %f \n\r",lapNo, time,float (time/lapNo));
lh14g13 18:0095a3a8f8e4 559 test.stop();
lh14g13 18:0095a3a8f8e4 560
lh14g13 18:0095a3a8f8e4 561
lh14g13 19:65f0b6febc23 562 }
lh14g13 18:0095a3a8f8e4 563
lh14g13 18:0095a3a8f8e4 564
lh14g13 18:0095a3a8f8e4 565
lh14g13 18:0095a3a8f8e4 566
lh14g13 18:0095a3a8f8e4 567
FatCookies 17:6ae90788cc2b 568 #endif