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 Dec 15 16:42:16 2016 +0000
Revision:
28:613239f10ba4
Parent:
27:627d67e3b9b0
Child:
29:b5b31256572b
actually got rid of thing.a

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
FatCookies 27:627d67e3b9b0 35 Timer lapTimer;
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 27:627d67e3b9b0 67 #if USE_COMMS
FatCookies 27:627d67e3b9b0 68 // Send the line scan image over serial
FatCookies 27:627d67e3b9b0 69 sendImage();
FatCookies 27:627d67e3b9b0 70 #endif
FatCookies 21:0b69fada7c5f 71
FatCookies 21:0b69fada7c5f 72
FatCookies 21:0b69fada7c5f 73 //Reset image ready flag
FatCookies 21:0b69fada7c5f 74 TFC_LineScanImageReady=0;
lh14g13 19:65f0b6febc23 75
FatCookies 17:6ae90788cc2b 76 // Slow down, adjust PID values and enable differential before corners.
FatCookies 21:0b69fada7c5f 77 //handleCornering();
FatCookies 17:6ae90788cc2b 78
FatCookies 13:4e77264f254a 79 // Run the PID controllers and adjust steering/motor accordingly
maximusismax 8:7c5e6b1e7aa5 80 PIDController();
maximusismax 8:7c5e6b1e7aa5 81
FatCookies 21:0b69fada7c5f 82
FatCookies 21:0b69fada7c5f 83
FatCookies 21:0b69fada7c5f 84
FatCookies 17:6ae90788cc2b 85 #if USE_COMMS
FatCookies 17:6ae90788cc2b 86 // Send the wheel speeds over serial
FatCookies 17:6ae90788cc2b 87 sendSpeeds();
FatCookies 17:6ae90788cc2b 88 #endif
FatCookies 13:4e77264f254a 89
FatCookies 21:0b69fada7c5f 90
maximusismax 8:7c5e6b1e7aa5 91
FatCookies 15:ccde02f96449 92
FatCookies 21:0b69fada7c5f 93
maximusismax 8:7c5e6b1e7aa5 94 }
maximusismax 8:7c5e6b1e7aa5 95 }
maximusismax 8:7c5e6b1e7aa5 96 }
maximusismax 8:7c5e6b1e7aa5 97
FatCookies 17:6ae90788cc2b 98 void initVariables() {
FatCookies 17:6ae90788cc2b 99 // Initialise three PID controllers for the servo and each wheel.
FatCookies 17:6ae90788cc2b 100 initPID(&servo_pid, 2.2f, 0.6f, 0.f);
FatCookies 21:0b69fada7c5f 101 initPID(&left_motor_pid, 0.01f, 0.f, 0.f);
FatCookies 21:0b69fada7c5f 102 initPID(&right_motor_pid, 0.01f, 0.f, 0.f);
FatCookies 17:6ae90788cc2b 103
lh14g13 19:65f0b6febc23 104 right_motor_pid.desired_value=0;
lh14g13 19:65f0b6febc23 105 left_motor_pid.desired_value=0;
lh14g13 19:65f0b6febc23 106
lh14g13 19:65f0b6febc23 107 // intialise the maximum speed interms of the angular speed.
FatCookies 17:6ae90788cc2b 108 valBufferIndex = 0;
lh14g13 19:65f0b6febc23 109 speed = 50;
FatCookies 13:4e77264f254a 110
FatCookies 17:6ae90788cc2b 111 //Start stop
FatCookies 17:6ae90788cc2b 112 startstop = 0;
FatCookies 17:6ae90788cc2b 113 seen = false;
FatCookies 17:6ae90788cc2b 114
FatCookies 17:6ae90788cc2b 115 // Turning
FatCookies 17:6ae90788cc2b 116 turning = 0;
FatCookies 17:6ae90788cc2b 117 keepTurning = 0;
FatCookies 17:6ae90788cc2b 118 slow = false;
FatCookies 20:ed954836d028 119
FatCookies 20:ed954836d028 120 wL = 0;
FatCookies 20:ed954836d028 121 wR = 0;
FatCookies 20:ed954836d028 122 prevL = 0;
FatCookies 20:ed954836d028 123 prevR = 0;
lh14g13 19:65f0b6febc23 124
maximusismax 8:7c5e6b1e7aa5 125 }
maximusismax 8:7c5e6b1e7aa5 126
FatCookies 13:4e77264f254a 127 void initPID(pid_instance* pid, float Kp, float Ki, float Kd) {
FatCookies 17:6ae90788cc2b 128 pid->Kp = Kp;
FatCookies 13:4e77264f254a 129 pid->Ki = Ki;
FatCookies 13:4e77264f254a 130 pid->Kd = Kd;
FatCookies 13:4e77264f254a 131 pid->dt = 0;
FatCookies 13:4e77264f254a 132 pid->p_error = 0;
FatCookies 13:4e77264f254a 133 pid->pid_error = 0;
FatCookies 13:4e77264f254a 134 pid->integral = 0;
FatCookies 13:4e77264f254a 135 pid->measured_value = 0;
FatCookies 13:4e77264f254a 136 pid->desired_value = 0;
FatCookies 13:4e77264f254a 137 pid->derivative = 0;
FatCookies 13:4e77264f254a 138 }
FatCookies 13:4e77264f254a 139
FatCookies 17:6ae90788cc2b 140 inline float findCentreValue(volatile uint16_t * cam_data, uint8_t &l, uint8_t &r) {
FatCookies 17:6ae90788cc2b 141
FatCookies 17:6ae90788cc2b 142 diff = 0;
FatCookies 17:6ae90788cc2b 143 prev = -1;
FatCookies 17:6ae90788cc2b 144 for(i = 63; i > 0; i--) {
oj3g13 26:f3d770f3eda1 145 curr_left = (uint8_t)(cam_data[i] >> 4) & 0xFF;
FatCookies 17:6ae90788cc2b 146 diff = prev - curr_left;
FatCookies 17:6ae90788cc2b 147 if(abs(diff) >= CAM_DIFF && curr_left <= CAM_THRESHOLD && prev != -1) {
FatCookies 17:6ae90788cc2b 148 l = i;
FatCookies 17:6ae90788cc2b 149 break;
FatCookies 17:6ae90788cc2b 150 }
FatCookies 17:6ae90788cc2b 151 prev = curr_left;
FatCookies 17:6ae90788cc2b 152 }
maximusismax 8:7c5e6b1e7aa5 153
FatCookies 17:6ae90788cc2b 154 prev = -1;
FatCookies 17:6ae90788cc2b 155 for(i = 64; i < 128; i++) {
oj3g13 26:f3d770f3eda1 156 curr_right = (uint8_t)(cam_data[i] >> 4) & 0xFF;
FatCookies 17:6ae90788cc2b 157 int diff = prev - curr_right;
FatCookies 17:6ae90788cc2b 158 if(abs(diff) >= CAM_DIFF && curr_right <= CAM_THRESHOLD && prev != -1) {
FatCookies 17:6ae90788cc2b 159 r = i;
FatCookies 17:6ae90788cc2b 160 break;
FatCookies 17:6ae90788cc2b 161 }
FatCookies 17:6ae90788cc2b 162 prev = curr_right;
FatCookies 17:6ae90788cc2b 163 }
FatCookies 17:6ae90788cc2b 164
FatCookies 17:6ae90788cc2b 165 //Calculate how left/right we are
FatCookies 17:6ae90788cc2b 166 return (64 - ((l+r)/2))/64.f;
FatCookies 13:4e77264f254a 167 }
FatCookies 13:4e77264f254a 168
FatCookies 17:6ae90788cc2b 169 inline void handleCornering() {
FatCookies 12:da96e2f87465 170
FatCookies 17:6ae90788cc2b 171 float lookaheadMeasuredValue = findCentreValue(LOOKAHEAD_CAMERA, farLeft, farRight);
FatCookies 15:ccde02f96449 172
FatCookies 13:4e77264f254a 173 measuredValBuffer[frame_counter % 64] = servo_pid.measured_value;
FatCookies 13:4e77264f254a 174
FatCookies 13:4e77264f254a 175 int count = 0;
FatCookies 13:4e77264f254a 176 for(i = 0; i < 10; i++) {
FatCookies 13:4e77264f254a 177 float val = abs(measuredValBuffer[(frame_counter - i) % 64]);
FatCookies 13:4e77264f254a 178 if(val > 0.09) {
FatCookies 13:4e77264f254a 179 count++;
FatCookies 13:4e77264f254a 180 }
FatCookies 13:4e77264f254a 181 }
FatCookies 12:da96e2f87465 182
FatCookies 17:6ae90788cc2b 183 if(!turning && abs(lookaheadMeasuredValue) > 0.11f){
FatCookies 15:ccde02f96449 184 TFC_SetMotorPWM(0.4,0.4);
FatCookies 15:ccde02f96449 185 }
FatCookies 15:ccde02f96449 186
FatCookies 14:13085e161dd1 187 if(turning) {
lh14g13 18:0095a3a8f8e4 188
lh14g13 18:0095a3a8f8e4 189 //default
lh14g13 18:0095a3a8f8e4 190 //TFC_SetMotorPWM(0.4,0.4);
lh14g13 18:0095a3a8f8e4 191
lh14g13 19:65f0b6febc23 192 //dutyCycleCorner(speed,servo_pid.output);
lh14g13 18:0095a3a8f8e4 193
lh14g13 18:0095a3a8f8e4 194 //may want to have just to set cornering speed at different if going to be slowing down for cornering.
lh14g13 18:0095a3a8f8e4 195 //dutyCycleCorner(float cornerspeed, servo_pid.output);
lh14g13 18:0095a3a8f8e4 196 //dutyCycleCorner(speed, servo_pid.output);
lh14g13 18:0095a3a8f8e4 197
FatCookies 14:13085e161dd1 198 //sensorCorner(left_motor_pid.desired_value, right_motor_pid.desired_value , servo_pid.output, 50);
FatCookies 14:13085e161dd1 199 }
FatCookies 14:13085e161dd1 200
FatCookies 13:4e77264f254a 201 if(abs(servo_pid.measured_value) > 0.11f){
FatCookies 15:ccde02f96449 202 if(!turning) {
FatCookies 13:4e77264f254a 203 turning = 1;
FatCookies 13:4e77264f254a 204 } else {
FatCookies 13:4e77264f254a 205 turning++;
FatCookies 13:4e77264f254a 206 }
FatCookies 13:4e77264f254a 207
FatCookies 13:4e77264f254a 208 } else {
FatCookies 13:4e77264f254a 209 if(turning) {
FatCookies 13:4e77264f254a 210 if(keepTurning == 0 || count > 6) {
FatCookies 13:4e77264f254a 211 keepTurning++;
FatCookies 13:4e77264f254a 212 } else {
FatCookies 15:ccde02f96449 213 //sendString("stop turning turned=%d",turning);
FatCookies 13:4e77264f254a 214 keepTurning = 0;
FatCookies 13:4e77264f254a 215 turning = 0;
FatCookies 13:4e77264f254a 216 TFC_SetMotorPWM(speed,speed);
FatCookies 13:4e77264f254a 217 }
FatCookies 13:4e77264f254a 218
FatCookies 13:4e77264f254a 219 }
FatCookies 13:4e77264f254a 220 }
FatCookies 13:4e77264f254a 221
maximusismax 8:7c5e6b1e7aa5 222 }
maximusismax 8:7c5e6b1e7aa5 223
FatCookies 17:6ae90788cc2b 224 inline float getLineEntropy() {
FatCookies 17:6ae90788cc2b 225 float entropy = 0;
FatCookies 17:6ae90788cc2b 226 float last = (int8_t)(CLOSE_CAMERA[0] >> 4) & 0xFF;
FatCookies 17:6ae90788cc2b 227 for(int i = 1; i < 128; i++) {
FatCookies 17:6ae90788cc2b 228 entropy += abs(last - ((int8_t)(CLOSE_CAMERA[i] >> 4) & 0xFF));
FatCookies 17:6ae90788cc2b 229 }
FatCookies 17:6ae90788cc2b 230 return entropy;
FatCookies 17:6ae90788cc2b 231 }
FatCookies 17:6ae90788cc2b 232
FatCookies 17:6ae90788cc2b 233 void handlePID(pid_instance *pid) {
FatCookies 17:6ae90788cc2b 234 pid->dt = t.read();
FatCookies 17:6ae90788cc2b 235 pid->pid_error = pid->desired_value - pid->measured_value;
FatCookies 17:6ae90788cc2b 236 pid->integral = pid->integral + pid->pid_error * pid->dt;
FatCookies 17:6ae90788cc2b 237 pid->derivative = (pid->pid_error - pid->p_error) / pid->dt;
FatCookies 17:6ae90788cc2b 238 pid->output = pid->Kp * pid->pid_error + pid->Ki * pid->integral + pid->Kd * pid->derivative;
FatCookies 17:6ae90788cc2b 239 pid->p_error = pid->pid_error;
FatCookies 17:6ae90788cc2b 240
FatCookies 17:6ae90788cc2b 241 if(pid->integral > 1.0f) {
FatCookies 17:6ae90788cc2b 242 pid->integral = 1.0f;
FatCookies 17:6ae90788cc2b 243 }
FatCookies 27:627d67e3b9b0 244 if(pid->integral < -1.0f ) {
FatCookies 17:6ae90788cc2b 245 pid->integral = -1.0f;
FatCookies 17:6ae90788cc2b 246 }
FatCookies 17:6ae90788cc2b 247 }
FatCookies 17:6ae90788cc2b 248
lh14g13 19:65f0b6febc23 249
FatCookies 17:6ae90788cc2b 250 inline void PIDController() {
FatCookies 27:627d67e3b9b0 251 // update motor measurements
lh14g13 19:65f0b6febc23 252 // Read the angular velocity of both wheels
FatCookies 27:627d67e3b9b0 253
FatCookies 27:627d67e3b9b0 254 prevL = wL;
FatCookies 27:627d67e3b9b0 255 prevR = wR;
FatCookies 27:627d67e3b9b0 256
lh14g13 19:65f0b6febc23 257 wL=Get_Speed(Time_L);
lh14g13 19:65f0b6febc23 258 wR=Get_Speed(Time_R);
FatCookies 20:ed954836d028 259
FatCookies 20:ed954836d028 260 // Check if left wheel is slipping/giving an abnormal reading and ignore reading
FatCookies 20:ed954836d028 261 if(wL - prevL < 1.2/0.025) {
FatCookies 20:ed954836d028 262 left_motor_pid.measured_value = wL;
FatCookies 20:ed954836d028 263 }
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 }
FatCookies 17:6ae90788cc2b 267
FatCookies 21:0b69fada7c5f 268
FatCookies 17:6ae90788cc2b 269 //PID Stuff!
FatCookies 17:6ae90788cc2b 270 t.start();
FatCookies 17:6ae90788cc2b 271 handlePID(&servo_pid);
FatCookies 17:6ae90788cc2b 272 handlePID(&left_motor_pid);
FatCookies 17:6ae90788cc2b 273 handlePID(&right_motor_pid);
FatCookies 17:6ae90788cc2b 274
FatCookies 17:6ae90788cc2b 275 if((-1.0 <= servo_pid.output) && (servo_pid.output <= 1.0))
FatCookies 17:6ae90788cc2b 276 {
FatCookies 17:6ae90788cc2b 277 TFC_SetServo(0, servo_pid.output);
FatCookies 17:6ae90788cc2b 278 }
FatCookies 17:6ae90788cc2b 279 else //Unhappy PID state
FatCookies 17:6ae90788cc2b 280 {
FatCookies 17:6ae90788cc2b 281 //sendString("out = %f p_err = %f", servo_pid.output, servo_pid.p_error);
FatCookies 17:6ae90788cc2b 282 ALIGN_SERVO;
FatCookies 17:6ae90788cc2b 283 if(servo_pid.output >= 1.0f) {
FatCookies 17:6ae90788cc2b 284 TFC_SetServo(0, 0.9f);
FatCookies 17:6ae90788cc2b 285 servo_pid.output = 1.0f;
FatCookies 17:6ae90788cc2b 286 } else {
FatCookies 17:6ae90788cc2b 287 TFC_SetServo(0, -0.9f);
FatCookies 17:6ae90788cc2b 288 servo_pid.output = -1.0f;
FatCookies 17:6ae90788cc2b 289 }
FatCookies 17:6ae90788cc2b 290 }
FatCookies 17:6ae90788cc2b 291
FatCookies 17:6ae90788cc2b 292
FatCookies 20:ed954836d028 293 if(left_motor_pid.output > 1.0f) {
FatCookies 20:ed954836d028 294 left_motor_pid.output = 1.0f;
lh14g13 19:65f0b6febc23 295 }
FatCookies 20:ed954836d028 296 if(left_motor_pid.output < -1.0f) {
FatCookies 27:627d67e3b9b0 297 left_motor_pid.output = 0.0f;
lh14g13 19:65f0b6febc23 298 }
lh14g13 19:65f0b6febc23 299
FatCookies 20:ed954836d028 300 if(right_motor_pid.output > 1.0f) {
FatCookies 20:ed954836d028 301 right_motor_pid.output = 1.0f;
FatCookies 20:ed954836d028 302 }
FatCookies 20:ed954836d028 303 if(right_motor_pid.output < -1.0f) {
FatCookies 27:627d67e3b9b0 304 right_motor_pid.output = 0.0f;
FatCookies 20:ed954836d028 305 }
lh14g13 19:65f0b6febc23 306
FatCookies 20:ed954836d028 307 TFC_SetMotorPWM(left_motor_pid.output,right_motor_pid.output);
lh14g13 19:65f0b6febc23 308
FatCookies 21:0b69fada7c5f 309
FatCookies 17:6ae90788cc2b 310 t.stop();
FatCookies 17:6ae90788cc2b 311 t.reset();
FatCookies 17:6ae90788cc2b 312 t.start();
FatCookies 17:6ae90788cc2b 313 }
FatCookies 17:6ae90788cc2b 314
FatCookies 17:6ae90788cc2b 315 inline void handleStartStop() {
maximusismax 22:973b95478663 316
maximusismax 22:973b95478663 317 //v1:
FatCookies 17:6ae90788cc2b 318 //Hacky way to detect the start/stop signal
oj3g13 26:f3d770f3eda1 319 /*if(right - left < 60) {
maximusismax 22:973b95478663 320 sendString("START STOP!!");
FatCookies 21:0b69fada7c5f 321
maximusismax 22:973b95478663 322 TFC_SetMotorPWM(0.f,0.f);
maximusismax 22:973b95478663 323 TFC_HBRIDGE_DISABLE;
maximusismax 22:973b95478663 324 startstop = 0;
oj3g13 26:f3d770f3eda1 325 }*/
FatCookies 21:0b69fada7c5f 326
maximusismax 22:973b95478663 327 //----------------------------START/STOP v2-----------------------------
maximusismax 22:973b95478663 328 //New method plan:
maximusismax 22:973b95478663 329 //Start at the centre of the image
maximusismax 22:973b95478663 330 //Look for 2 transitions (B->W OR W->B) - ignoring whether the centre pixel was black or white
maximusismax 22:973b95478663 331 //this should efficiently detect whether the marker is visible, and shouldn't give too many false positives
maximusismax 22:973b95478663 332 //May need to fiddle with the track width initial check, or disbale it entirely.
maximusismax 22:973b95478663 333 //NB: May want to incorporate this into findCentreValue(), it looks like they will be operating in similar ways on the exact same data...
maximusismax 22:973b95478663 334 /*
maximusismax 22:973b95478663 335 uint8_t lastPixel, currentPixel;
maximusismax 22:973b95478663 336 lastPixel = -1; //or 0?
maximusismax 22:973b95478663 337 bool startStopLeft = false;
maximusismax 22:973b95478663 338 bool startStopRight = false
maximusismax 22:973b95478663 339 //So:
maximusismax 22:973b95478663 340 //1. Starting at the middle, step left, looking for 2 transitions
maximusismax 22:973b95478663 341 for(int i = 63; i > 0; i--) {
maximusismax 22:973b95478663 342 currentPixel = (uint8_t)(CLOSE_CAMERA[i] >> 4) & 0xFF; //Cast to signed or unsigned? Edge detection code has signed, but it puts it in an unsigned variable...
maximusismax 22:973b95478663 343 if((lastPixel - currentPixel) > 10) { //1st transition
maximusismax 22:973b95478663 344 for (int j = i; j > 0; j--) { //Keep going until 2nd transition
maximusismax 22:973b95478663 345 if((lastPixel - currentPixel) > 10) { //2nd transition
maximusismax 22:973b95478663 346 //Set flag that 2 transitions on the left side are identified
maximusismax 22:973b95478663 347 startStopLeft = true;
maximusismax 22:973b95478663 348 //goto finishLeft;
maximusismax 22:973b95478663 349 }
maximusismax 22:973b95478663 350 }
maximusismax 22:973b95478663 351
maximusismax 22:973b95478663 352 }
maximusismax 22:973b95478663 353 startStopLeft = false;
maximusismax 22:973b95478663 354 lastPixel = currentPixel;
maximusismax 22:973b95478663 355 }
maximusismax 22:973b95478663 356 //finishLeft:
maximusismax 22:973b95478663 357 */
maximusismax 22:973b95478663 358
oj3g13 26:f3d770f3eda1 359 //v2.5:--------------
oj3g13 26:f3d770f3eda1 360
maximusismax 22:973b95478663 361 int slower = 0;
oj3g13 26:f3d770f3eda1 362 int difference = 0;
oj3g13 26:f3d770f3eda1 363 int lastPixel, currentPixel, transitionsSeen;
maximusismax 22:973b95478663 364 lastPixel = -1;
maximusismax 22:973b95478663 365 transitionsSeen = 0;
maximusismax 22:973b95478663 366 //Starting near the left edge, step right, counting transitions. If there are 4, it is the marker (what about 3?)
maximusismax 22:973b95478663 367 for(int i = 30; i < 98; i++) {
oj3g13 26:f3d770f3eda1 368 currentPixel = (int)(CLOSE_CAMERA[i] >> 4) & 0xFF;
maximusismax 22:973b95478663 369 difference = lastPixel - currentPixel;
FatCookies 27:627d67e3b9b0 370 if(abs(difference) > 10 && lastPixel != -1){ //transition seen, increment counter
maximusismax 22:973b95478663 371 transitionsSeen++;
oj3g13 26:f3d770f3eda1 372 i+=5;
maximusismax 22:973b95478663 373 }
maximusismax 22:973b95478663 374 lastPixel = currentPixel;
maximusismax 22:973b95478663 375 }
oj3g13 26:f3d770f3eda1 376 //if (slower % 1000 == 0) {
oj3g13 26:f3d770f3eda1 377 //sendString("Transitions seen: %d", transitionsSeen);
oj3g13 26:f3d770f3eda1 378 //}
oj3g13 26:f3d770f3eda1 379 if(transitionsSeen >= 5) {
maximusismax 22:973b95478663 380 //Stop the car!
oj3g13 26:f3d770f3eda1 381 sendString("Start/stop seen");
oj3g13 26:f3d770f3eda1 382 TFC_SetMotorPWM(0.f,0.f);
oj3g13 26:f3d770f3eda1 383 TFC_HBRIDGE_DISABLE;
FatCookies 27:627d67e3b9b0 384 lapTime();
oj3g13 26:f3d770f3eda1 385 }
oj3g13 26:f3d770f3eda1 386 // slower++;
oj3g13 26:f3d770f3eda1 387
FatCookies 17:6ae90788cc2b 388 }
FatCookies 17:6ae90788cc2b 389
FatCookies 17:6ae90788cc2b 390
FatCookies 17:6ae90788cc2b 391 inline void initSpeedSensors() {
FatCookies 17:6ae90788cc2b 392 t1.start();
FatCookies 17:6ae90788cc2b 393 t2.start();
FatCookies 17:6ae90788cc2b 394
FatCookies 17:6ae90788cc2b 395 //Left and Right are defined looking at the rear of the car, in the direction the camera points at.
FatCookies 17:6ae90788cc2b 396 leftHallSensor.rise(&GetTime_L);
FatCookies 17:6ae90788cc2b 397 rightHallSensor.rise(&GetTime_R);
FatCookies 17:6ae90788cc2b 398 }
FatCookies 17:6ae90788cc2b 399
FatCookies 17:6ae90788cc2b 400 void GetTime_L(){
FatCookies 17:6ae90788cc2b 401 Time_L=t1.read_us();
FatCookies 17:6ae90788cc2b 402 t1.reset();
FatCookies 17:6ae90788cc2b 403 }
FatCookies 17:6ae90788cc2b 404
FatCookies 17:6ae90788cc2b 405 void GetTime_R(){
FatCookies 17:6ae90788cc2b 406 Time_R=t2.read_us();
FatCookies 17:6ae90788cc2b 407 t2.reset();
FatCookies 17:6ae90788cc2b 408 }
FatCookies 17:6ae90788cc2b 409
FatCookies 17:6ae90788cc2b 410 #if USE_COMMS
FatCookies 17:6ae90788cc2b 411 void sendBattery() {
FatCookies 17:6ae90788cc2b 412
FatCookies 17:6ae90788cc2b 413 if(frame_counter % 256 == 0) {
FatCookies 17:6ae90788cc2b 414 float level = TFC_ReadBatteryVoltage() * 6.25;
FatCookies 17:6ae90788cc2b 415 pc.putc('J');
FatCookies 28:613239f10ba4 416 byte_float_union._float = level;
FatCookies 27:627d67e3b9b0 417 pc.putc(byte_float_union.bytes[0]);
FatCookies 27:627d67e3b9b0 418 pc.putc(byte_float_union.bytes[1]);
FatCookies 27:627d67e3b9b0 419 pc.putc(byte_float_union.bytes[2]);
FatCookies 27:627d67e3b9b0 420 pc.putc(byte_float_union.bytes[3]);
FatCookies 17:6ae90788cc2b 421 }
FatCookies 17:6ae90788cc2b 422 }
FatCookies 17:6ae90788cc2b 423
FatCookies 17:6ae90788cc2b 424 void sendString(const char *format, ...) {
FatCookies 17:6ae90788cc2b 425 va_list arg;
FatCookies 17:6ae90788cc2b 426
FatCookies 17:6ae90788cc2b 427 pc.putc('E');
FatCookies 17:6ae90788cc2b 428 va_start (arg, format);
FatCookies 17:6ae90788cc2b 429 pc.vprintf(format,arg);
FatCookies 17:6ae90788cc2b 430 va_end (arg);
FatCookies 17:6ae90788cc2b 431 pc.putc(0);
FatCookies 17:6ae90788cc2b 432 }
FatCookies 17:6ae90788cc2b 433
maximusismax 8:7c5e6b1e7aa5 434 inline void sendImage() {
maximusismax 8:7c5e6b1e7aa5 435 //Only send 1/3 of camera frames to GUI program
maximusismax 8:7c5e6b1e7aa5 436 if((frame_counter % 3) == 0) {
maximusismax 8:7c5e6b1e7aa5 437 pc.putc('H');
FatCookies 15:ccde02f96449 438 if(sendCam == 0) {
FatCookies 15:ccde02f96449 439 for(i = 0; i < 128; i++) {
FatCookies 17:6ae90788cc2b 440 pc.putc((int8_t)(CLOSE_CAMERA[i] >> 4) & 0xFF);
FatCookies 15:ccde02f96449 441 }
FatCookies 15:ccde02f96449 442 } else {
FatCookies 15:ccde02f96449 443 for(i = 0; i < 128; i++) {
FatCookies 17:6ae90788cc2b 444 pc.putc((int8_t)(LOOKAHEAD_CAMERA[i] >> 4) & 0xFF);
FatCookies 15:ccde02f96449 445 }
FatCookies 15:ccde02f96449 446 }
FatCookies 13:4e77264f254a 447 sendBattery();
FatCookies 13:4e77264f254a 448 }
FatCookies 13:4e77264f254a 449
FatCookies 13:4e77264f254a 450 frame_counter++;
FatCookies 13:4e77264f254a 451 }
FatCookies 13:4e77264f254a 452
FatCookies 13:4e77264f254a 453 inline void sendSpeeds() {
FatCookies 17:6ae90788cc2b 454
lh14g13 19:65f0b6febc23 455 /*float en = getLineEntropy();
FatCookies 15:ccde02f96449 456
FatCookies 15:ccde02f96449 457 if(onTrack) {
FatCookies 15:ccde02f96449 458 if(en <= 14000) {
FatCookies 15:ccde02f96449 459 onTrack = false;
FatCookies 15:ccde02f96449 460 sendString("offfffffffffffff");
FatCookies 15:ccde02f96449 461 TFC_SetMotorPWM(0.0,0.0);
FatCookies 15:ccde02f96449 462 TFC_HBRIDGE_DISABLE;
FatCookies 15:ccde02f96449 463 }
FatCookies 15:ccde02f96449 464 } else {
FatCookies 15:ccde02f96449 465 if(en > 14000) {
FatCookies 15:ccde02f96449 466 onTrack = true;
FatCookies 15:ccde02f96449 467 sendString("ON TRACK");
FatCookies 15:ccde02f96449 468 }
lh14g13 19:65f0b6febc23 469 }*/
FatCookies 15:ccde02f96449 470
FatCookies 14:13085e161dd1 471
FatCookies 13:4e77264f254a 472 pc.putc('B');
FatCookies 28:613239f10ba4 473 byte_float_union._float = wL * WHEEL_RADIUS;//left_motor_pid.output; //
FatCookies 27:627d67e3b9b0 474 pc.putc(byte_float_union.bytes[0]);
FatCookies 27:627d67e3b9b0 475 pc.putc(byte_float_union.bytes[1]);
FatCookies 27:627d67e3b9b0 476 pc.putc(byte_float_union.bytes[2]);
FatCookies 27:627d67e3b9b0 477 pc.putc(byte_float_union.bytes[3]);
FatCookies 28:613239f10ba4 478 byte_float_union._float = wR * WHEEL_RADIUS; // right_motor_pid.output; //
FatCookies 27:627d67e3b9b0 479 pc.putc(byte_float_union.bytes[0]);
FatCookies 27:627d67e3b9b0 480 pc.putc(byte_float_union.bytes[1]);
FatCookies 27:627d67e3b9b0 481 pc.putc(byte_float_union.bytes[2]);
FatCookies 27:627d67e3b9b0 482 pc.putc(byte_float_union.bytes[3]);
FatCookies 27:627d67e3b9b0 483
maximusismax 8:7c5e6b1e7aa5 484 }
maximusismax 8:7c5e6b1e7aa5 485
FatCookies 13:4e77264f254a 486
maximusismax 8:7c5e6b1e7aa5 487 inline void handleComms() {
maximusismax 8:7c5e6b1e7aa5 488 if(curr_cmd != 0) {
FatCookies 4:4afa448c9cce 489 switch(curr_cmd) {
FatCookies 4:4afa448c9cce 490 case 'A':
FatCookies 20:ed954836d028 491 if(xb.cBuffer->available() >= 12) {
FatCookies 20:ed954836d028 492
FatCookies 27:627d67e3b9b0 493 byte_float_union.bytes[0] = xb.cBuffer->read();
FatCookies 27:627d67e3b9b0 494 byte_float_union.bytes[1] = xb.cBuffer->read();
FatCookies 27:627d67e3b9b0 495 byte_float_union.bytes[2] = xb.cBuffer->read();
FatCookies 27:627d67e3b9b0 496 byte_float_union.bytes[3] = xb.cBuffer->read();
FatCookies 28:613239f10ba4 497 servo_pid.Kp = byte_float_union._float;
FatCookies 20:ed954836d028 498
FatCookies 27:627d67e3b9b0 499 byte_float_union.bytes[0] = xb.cBuffer->read();
FatCookies 27:627d67e3b9b0 500 byte_float_union.bytes[1] = xb.cBuffer->read();
FatCookies 27:627d67e3b9b0 501 byte_float_union.bytes[2] = xb.cBuffer->read();
FatCookies 27:627d67e3b9b0 502 byte_float_union.bytes[3] = xb.cBuffer->read();
FatCookies 28:613239f10ba4 503 servo_pid.Ki = byte_float_union._float;
FatCookies 20:ed954836d028 504
FatCookies 27:627d67e3b9b0 505 byte_float_union.bytes[0] = xb.cBuffer->read();
FatCookies 27:627d67e3b9b0 506 byte_float_union.bytes[1] = xb.cBuffer->read();
FatCookies 27:627d67e3b9b0 507 byte_float_union.bytes[2] = xb.cBuffer->read();
FatCookies 27:627d67e3b9b0 508 byte_float_union.bytes[3] = xb.cBuffer->read();
FatCookies 28:613239f10ba4 509 servo_pid.Kd = byte_float_union._float;
FatCookies 20:ed954836d028 510
FatCookies 20:ed954836d028 511 sendString("pid= Kp: %f, Ki: %f, Kd: %f", servo_pid.Kp, servo_pid.Ki, servo_pid.Kd);
maximusismax 8:7c5e6b1e7aa5 512
FatCookies 4:4afa448c9cce 513 curr_cmd = 0;
FatCookies 4:4afa448c9cce 514 }
FatCookies 4:4afa448c9cce 515 break;
FatCookies 4:4afa448c9cce 516
FatCookies 4:4afa448c9cce 517 case 'F':
FatCookies 6:b0e160c51013 518 if(xb.cBuffer->available() >= 1) {
FatCookies 4:4afa448c9cce 519 char a = xb.cBuffer->read();
FatCookies 27:627d67e3b9b0 520 speed = a;
FatCookies 13:4e77264f254a 521 sendString("s = %u %f",a, speed);
FatCookies 4:4afa448c9cce 522 curr_cmd = 0;
FatCookies 4:4afa448c9cce 523 }
FatCookies 4:4afa448c9cce 524 break;
FatCookies 4:4afa448c9cce 525
FatCookies 4:4afa448c9cce 526 default:
FatCookies 13:4e77264f254a 527 // Unrecognised command
FatCookies 13:4e77264f254a 528 curr_cmd = 0;
FatCookies 4:4afa448c9cce 529 break;
FatCookies 4:4afa448c9cce 530 }
FatCookies 4:4afa448c9cce 531 }
FatCookies 4:4afa448c9cce 532
FatCookies 6:b0e160c51013 533 if(xb.cBuffer->available() > 0 && curr_cmd == 0) {
lh14g13 18:0095a3a8f8e4 534 //Start car
FatCookies 4:4afa448c9cce 535 char cmd = xb.cBuffer->read();
FatCookies 4:4afa448c9cce 536 if(cmd == 'D') {
FatCookies 17:6ae90788cc2b 537 ALIGN_SERVO;
FatCookies 21:0b69fada7c5f 538 right_motor_pid.desired_value=speed;
FatCookies 21:0b69fada7c5f 539 left_motor_pid.desired_value=speed;
FatCookies 4:4afa448c9cce 540 TFC_HBRIDGE_ENABLE;
FatCookies 21:0b69fada7c5f 541
FatCookies 21:0b69fada7c5f 542
FatCookies 12:da96e2f87465 543 servo_pid.integral = 0;
FatCookies 27:627d67e3b9b0 544 lapTimer.start();
lh14g13 18:0095a3a8f8e4 545 lapNo =0;
FatCookies 6:b0e160c51013 546
FatCookies 4:4afa448c9cce 547 } else if (cmd == 'C') {
FatCookies 4:4afa448c9cce 548 TFC_SetMotorPWM(0.0,0.0);
lh14g13 19:65f0b6febc23 549 right_motor_pid.desired_value=0;
FatCookies 21:0b69fada7c5f 550 right_motor_pid.measured_value = 0;
FatCookies 21:0b69fada7c5f 551 wR = 0;
FatCookies 21:0b69fada7c5f 552 prevR = 0;
FatCookies 21:0b69fada7c5f 553
lh14g13 19:65f0b6febc23 554 left_motor_pid.desired_value=0;
FatCookies 21:0b69fada7c5f 555 left_motor_pid.measured_value = 0;
FatCookies 21:0b69fada7c5f 556 wL = 0;
FatCookies 21:0b69fada7c5f 557 prevL = 0;
FatCookies 21:0b69fada7c5f 558
FatCookies 4:4afa448c9cce 559 TFC_HBRIDGE_DISABLE;
lh14g13 18:0095a3a8f8e4 560 endTest();
FatCookies 4:4afa448c9cce 561 } else if(cmd == 'A') {
FatCookies 4:4afa448c9cce 562 curr_cmd = 'A';
FatCookies 4:4afa448c9cce 563 } else if(cmd == 'F') {
FatCookies 4:4afa448c9cce 564 curr_cmd = 'F';
FatCookies 15:ccde02f96449 565 } else if(cmd == 'K') {
FatCookies 15:ccde02f96449 566 sendCam = ~sendCam;
FatCookies 4:4afa448c9cce 567 }
FatCookies 4:4afa448c9cce 568
FatCookies 4:4afa448c9cce 569 }
maximusismax 8:7c5e6b1e7aa5 570 }
lh14g13 18:0095a3a8f8e4 571
lh14g13 18:0095a3a8f8e4 572 float testSpeed(float speed)
lh14g13 18:0095a3a8f8e4 573 {
lh14g13 18:0095a3a8f8e4 574 // search: Speed Increase
lh14g13 18:0095a3a8f8e4 575 // every time the car sees the stop start the speed of the car will increase
lh14g13 18:0095a3a8f8e4 576 // this can occur on stop start trigger.
lh14g13 18:0095a3a8f8e4 577 // may need to send the speed back to the telemetry.
lh14g13 18:0095a3a8f8e4 578 if (speed>0.4)
lh14g13 18:0095a3a8f8e4 579 {
lh14g13 18:0095a3a8f8e4 580 speed+=0.05;
lh14g13 18:0095a3a8f8e4 581 }
lh14g13 18:0095a3a8f8e4 582
lh14g13 18:0095a3a8f8e4 583 else
lh14g13 18:0095a3a8f8e4 584 {
lh14g13 18:0095a3a8f8e4 585 speed+=0.1;
lh14g13 18:0095a3a8f8e4 586
lh14g13 18:0095a3a8f8e4 587 }
lh14g13 18:0095a3a8f8e4 588
lh14g13 18:0095a3a8f8e4 589
lh14g13 18:0095a3a8f8e4 590 sendString("s = %f", speed);
lh14g13 18:0095a3a8f8e4 591 return speed;
lh14g13 18:0095a3a8f8e4 592
lh14g13 18:0095a3a8f8e4 593 }
lh14g13 18:0095a3a8f8e4 594
lh14g13 18:0095a3a8f8e4 595
lh14g13 18:0095a3a8f8e4 596
lh14g13 18:0095a3a8f8e4 597 int lapTime()
lh14g13 19:65f0b6febc23 598 {
lh14g13 18:0095a3a8f8e4 599 // function which sends the lap time back to the telemetry.
FatCookies 27:627d67e3b9b0 600 float newTime= lapTimer.read();
lh14g13 18:0095a3a8f8e4 601 lapNo += 1;
lh14g13 18:0095a3a8f8e4 602 float lapTime= newTime-oldTime;
lh14g13 18:0095a3a8f8e4 603 float avgTime= newTime/lapNo;
lh14g13 18:0095a3a8f8e4 604
lh14g13 19:65f0b6febc23 605 sendString("For lap number: %d Lap Time: %f Avergae time: %f \n\r", lapNo,lapTime,avgTime);
lh14g13 18:0095a3a8f8e4 606
lh14g13 18:0095a3a8f8e4 607 // OH WHAT UP IT'S DAT BOI!!!!
lh14g13 18:0095a3a8f8e4 608 return 0;
lh14g13 18:0095a3a8f8e4 609 }
lh14g13 18:0095a3a8f8e4 610
lh14g13 18:0095a3a8f8e4 611
lh14g13 18:0095a3a8f8e4 612 void endTest()
lh14g13 18:0095a3a8f8e4 613 {// 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 614
FatCookies 27:627d67e3b9b0 615 float time= lapTimer.read();
lh14g13 18:0095a3a8f8e4 616
lh14g13 19:65f0b6febc23 617 sendString("Laps done: %d Time elapsed: %f Average time: %f \n\r",lapNo, time,float (time/lapNo));
FatCookies 27:627d67e3b9b0 618 lapTimer.stop();
lh14g13 18:0095a3a8f8e4 619
lh14g13 18:0095a3a8f8e4 620
lh14g13 19:65f0b6febc23 621 }
lh14g13 18:0095a3a8f8e4 622
lh14g13 18:0095a3a8f8e4 623
lh14g13 18:0095a3a8f8e4 624
lh14g13 18:0095a3a8f8e4 625
lh14g13 18:0095a3a8f8e4 626
FatCookies 17:6ae90788cc2b 627 #endif