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