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 13:19:28 2016 +0000
Revision:
21:0b69fada7c5f
Parent:
20:ed954836d028
Child:
22:973b95478663
all the broken things

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