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:
Tue Nov 29 13:11:20 2016 +0000
Revision:
13:4e77264f254a
Parent:
12:da96e2f87465
Child:
14:13085e161dd1
add corner detection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maximusismax 8:7c5e6b1e7aa5 1 //Autonomous Car GDP controller
maximusismax 8:7c5e6b1e7aa5 2 //Written by various group members
maximusismax 8:7c5e6b1e7aa5 3 //Commented & cleaned up by Max/Adam
maximusismax 8:7c5e6b1e7aa5 4
maximusismax 8:7c5e6b1e7aa5 5 //To-do
maximusismax 8:7c5e6b1e7aa5 6 // -Change xbee transmission to non-blocking
maximusismax 8:7c5e6b1e7aa5 7 // -Improve start/stop detection and resultant action (setting PID values?)
maximusismax 8:7c5e6b1e7aa5 8
maximusismax 8:7c5e6b1e7aa5 9 #include <stdarg.h>
maximusismax 8:7c5e6b1e7aa5 10 #include <stdio.h>
maximusismax 8:7c5e6b1e7aa5 11
maximusismax 0:566127ca8048 12 #include "mbed.h"
maximusismax 0:566127ca8048 13 #include "TFC.h"
FatCookies 4:4afa448c9cce 14 #include "XBEE.h"
FatCookies 9:aa2ce38dec6b 15 #include "angular_speed.h"
FatCookies 12:da96e2f87465 16 #include "motor.h"
FatCookies 12:da96e2f87465 17 #include "main.h"
maximusismax 8:7c5e6b1e7aa5 18
maximusismax 8:7c5e6b1e7aa5 19 #define BAUD_RATE 57600
FatCookies 6:b0e160c51013 20 #define CAM_THRESHOLD 109
maximusismax 8:7c5e6b1e7aa5 21 #define CAM_DIFF 10
maximusismax 8:7c5e6b1e7aa5 22
FatCookies 10:1bd0224093e4 23 #define RIGHT_MOTOR_COMPENSATION_RATIO 1.1586276
FatCookies 10:1bd0224093e4 24
FatCookies 3:87a5122682fa 25 Serial pc(PTD3,PTD2);
FatCookies 4:4afa448c9cce 26 XBEE xb(&pc);
FatCookies 3:87a5122682fa 27
maximusismax 8:7c5e6b1e7aa5 28 //Woo global variables!
maximusismax 8:7c5e6b1e7aa5 29 bool onTrack;
FatCookies 3:87a5122682fa 30 char curr_line[128];
FatCookies 3:87a5122682fa 31 uint8_t curr_left;
FatCookies 3:87a5122682fa 32 uint8_t curr_right;
FatCookies 3:87a5122682fa 33 uint8_t right;
FatCookies 3:87a5122682fa 34 uint8_t left;
maximusismax 8:7c5e6b1e7aa5 35 int8_t leftChange;
maximusismax 8:7c5e6b1e7aa5 36 int8_t rightChange;
maximusismax 8:7c5e6b1e7aa5 37 int diff;
maximusismax 8:7c5e6b1e7aa5 38 int prev;
maximusismax 8:7c5e6b1e7aa5 39 int i = 0;
FatCookies 13:4e77264f254a 40 float measuredValBuffer[64];
maximusismax 8:7c5e6b1e7aa5 41 uint8_t valBufferIndex;
maximusismax 8:7c5e6b1e7aa5 42
maximusismax 8:7c5e6b1e7aa5 43 //Some PID variables
FatCookies 12:da96e2f87465 44 pid_instance servo_pid;
FatCookies 13:4e77264f254a 45 pid_instance right_motor_pid;
FatCookies 13:4e77264f254a 46 pid_instance left_motor_pid;
FatCookies 3:87a5122682fa 47 Timer t;
FatCookies 4:4afa448c9cce 48
FatCookies 12:da96e2f87465 49
FatCookies 9:aa2ce38dec6b 50 //Speed Sensors variables
FatCookies 9:aa2ce38dec6b 51 InterruptIn leftHallSensor(D0);
FatCookies 9:aa2ce38dec6b 52 InterruptIn rightHallSensor(D2);
FatCookies 9:aa2ce38dec6b 53 Timer t1;
FatCookies 9:aa2ce38dec6b 54 Timer t2;
FatCookies 9:aa2ce38dec6b 55 volatile float Time_L,Time_R;
FatCookies 9:aa2ce38dec6b 56 float wL, wR;
FatCookies 9:aa2ce38dec6b 57 void GetTime_L();
FatCookies 9:aa2ce38dec6b 58 void GetTime_R();
FatCookies 9:aa2ce38dec6b 59 inline void initSpeedSensors();
FatCookies 9:aa2ce38dec6b 60
FatCookies 9:aa2ce38dec6b 61 // Current comms command
FatCookies 4:4afa448c9cce 62 char curr_cmd = 0;
FatCookies 4:4afa448c9cce 63
FatCookies 4:4afa448c9cce 64 float speed = 0.3;
FatCookies 6:b0e160c51013 65 int frame_counter = 0;
maximusismax 8:7c5e6b1e7aa5 66
maximusismax 8:7c5e6b1e7aa5 67 //Hacky start/stop signal detection
maximusismax 8:7c5e6b1e7aa5 68 int startstop = 0;
FatCookies 7:ad893fc41b95 69 bool seen = false;
maximusismax 8:7c5e6b1e7aa5 70
maximusismax 8:7c5e6b1e7aa5 71 void sendString(const char *format, ...);
maximusismax 8:7c5e6b1e7aa5 72 void initVariables();
maximusismax 8:7c5e6b1e7aa5 73 inline void handleComms();
maximusismax 8:7c5e6b1e7aa5 74 inline float findCentreValue();
maximusismax 8:7c5e6b1e7aa5 75 inline void PIDController();
maximusismax 8:7c5e6b1e7aa5 76 inline void sendImage();
FatCookies 13:4e77264f254a 77 inline void sendSpeeds();
FatCookies 13:4e77264f254a 78 void handlePID(pid_instance *pid);
FatCookies 13:4e77264f254a 79 void initPID(pid_instance* pid);
FatCookies 13:4e77264f254a 80 inline void handleStartStop();
FatCookies 13:4e77264f254a 81 inline void recordMeasuredVal();
FatCookies 13:4e77264f254a 82 void sendBattery();
maximusismax 8:7c5e6b1e7aa5 83
maximusismax 0:566127ca8048 84 int main() {
maximusismax 8:7c5e6b1e7aa5 85 //Set up TFC driver stuff
maximusismax 0:566127ca8048 86 TFC_Init();
FatCookies 3:87a5122682fa 87 TFC_InitServos(0.00052,0.00122,0.02);
FatCookies 13:4e77264f254a 88
maximusismax 8:7c5e6b1e7aa5 89 //Setup baud rate for serial link, do not change!
maximusismax 8:7c5e6b1e7aa5 90 pc.baud(BAUD_RATE);
maximusismax 0:566127ca8048 91
maximusismax 8:7c5e6b1e7aa5 92 //Initialise/reset PID variables
maximusismax 8:7c5e6b1e7aa5 93 initVariables();
FatCookies 9:aa2ce38dec6b 94 initSpeedSensors();
FatCookies 12:da96e2f87465 95
FatCookies 13:4e77264f254a 96
maximusismax 0:566127ca8048 97 while(1) {
FatCookies 3:87a5122682fa 98
maximusismax 8:7c5e6b1e7aa5 99 handleComms();
maximusismax 8:7c5e6b1e7aa5 100
maximusismax 8:7c5e6b1e7aa5 101 //If we have an image ready
FatCookies 13:4e77264f254a 102 if(TFC_LineScanImageReady>0) {
FatCookies 13:4e77264f254a 103 /* Find the bounds of the track and calculate how close we are to
FatCookies 13:4e77264f254a 104 * the centre */
FatCookies 12:da96e2f87465 105 servo_pid.measured_value = findCentreValue();
FatCookies 13:4e77264f254a 106 recordMeasuredVal();
maximusismax 8:7c5e6b1e7aa5 107
FatCookies 13:4e77264f254a 108 // Read the angular velocity of both wheels
FatCookies 13:4e77264f254a 109 wL=Get_Speed(Time_L);
FatCookies 13:4e77264f254a 110 wR=Get_Speed(Time_R);
FatCookies 13:4e77264f254a 111
FatCookies 13:4e77264f254a 112 // Run the PID controllers and adjust steering/motor accordingly
maximusismax 8:7c5e6b1e7aa5 113 PIDController();
maximusismax 8:7c5e6b1e7aa5 114
FatCookies 13:4e77264f254a 115 // Send the line scan image over serial
maximusismax 8:7c5e6b1e7aa5 116 sendImage();
maximusismax 8:7c5e6b1e7aa5 117
FatCookies 13:4e77264f254a 118 // Send the wheel speeds over serial
FatCookies 13:4e77264f254a 119 sendSpeeds();
FatCookies 13:4e77264f254a 120
FatCookies 13:4e77264f254a 121 // Check if car is at the stop line
FatCookies 13:4e77264f254a 122 //handleStartStop();
maximusismax 8:7c5e6b1e7aa5 123
maximusismax 8:7c5e6b1e7aa5 124 //Reset image ready flag
maximusismax 8:7c5e6b1e7aa5 125 TFC_LineScanImageReady=0;
maximusismax 8:7c5e6b1e7aa5 126 }
maximusismax 8:7c5e6b1e7aa5 127 }
maximusismax 8:7c5e6b1e7aa5 128 }
maximusismax 8:7c5e6b1e7aa5 129
FatCookies 13:4e77264f254a 130 void sendBattery() {
FatCookies 13:4e77264f254a 131
FatCookies 13:4e77264f254a 132 if(frame_counter % 256 == 0) {
FatCookies 13:4e77264f254a 133 float level = TFC_ReadBatteryVoltage() * 6.25;
FatCookies 13:4e77264f254a 134 union {
FatCookies 13:4e77264f254a 135 float a;
FatCookies 13:4e77264f254a 136 unsigned char bytes[4];
FatCookies 13:4e77264f254a 137 } thing;
FatCookies 13:4e77264f254a 138
FatCookies 13:4e77264f254a 139 pc.putc('J');
FatCookies 13:4e77264f254a 140 thing.a = level;
FatCookies 13:4e77264f254a 141 pc.putc(thing.bytes[0]);
FatCookies 13:4e77264f254a 142 pc.putc(thing.bytes[1]);
FatCookies 13:4e77264f254a 143 pc.putc(thing.bytes[2]);
FatCookies 13:4e77264f254a 144 pc.putc(thing.bytes[3]);
FatCookies 13:4e77264f254a 145 }
FatCookies 13:4e77264f254a 146 }
FatCookies 13:4e77264f254a 147
maximusismax 8:7c5e6b1e7aa5 148 void sendString(const char *format, ...) {
maximusismax 8:7c5e6b1e7aa5 149 va_list arg;
maximusismax 8:7c5e6b1e7aa5 150
maximusismax 8:7c5e6b1e7aa5 151 pc.putc('E');
maximusismax 8:7c5e6b1e7aa5 152 va_start (arg, format);
maximusismax 8:7c5e6b1e7aa5 153 pc.vprintf(format,arg);
maximusismax 8:7c5e6b1e7aa5 154 va_end (arg);
maximusismax 8:7c5e6b1e7aa5 155 pc.putc(0);
maximusismax 8:7c5e6b1e7aa5 156 }
maximusismax 8:7c5e6b1e7aa5 157
FatCookies 13:4e77264f254a 158 void initPID(pid_instance* pid, float Kp, float Ki, float Kd) {
FatCookies 13:4e77264f254a 159 pid->Kp = Kd;
FatCookies 13:4e77264f254a 160 pid->Ki = Ki;
FatCookies 13:4e77264f254a 161 pid->Kd = Kd;
FatCookies 13:4e77264f254a 162 pid->dt = 0;
FatCookies 13:4e77264f254a 163 pid->p_error = 0;
FatCookies 13:4e77264f254a 164 pid->pid_error = 0;
FatCookies 13:4e77264f254a 165 pid->integral = 0;
FatCookies 13:4e77264f254a 166 pid->measured_value = 0;
FatCookies 13:4e77264f254a 167 pid->desired_value = 0;
FatCookies 13:4e77264f254a 168 pid->derivative = 0;
FatCookies 13:4e77264f254a 169 }
FatCookies 13:4e77264f254a 170
maximusismax 8:7c5e6b1e7aa5 171 void initVariables() {
FatCookies 13:4e77264f254a 172 // Initialise three PID controllers for the servo and each wheel.
FatCookies 13:4e77264f254a 173 initPID(&servo_pid, 2.2f, 0.6f, 0.f);
FatCookies 13:4e77264f254a 174 initPID(&left_motor_pid, 1.0f, 0.f, 0.f);
FatCookies 13:4e77264f254a 175 initPID(&right_motor_pid, 1.0f, 0.f, 0.f);
maximusismax 8:7c5e6b1e7aa5 176
maximusismax 8:7c5e6b1e7aa5 177 valBufferIndex = 0;
FatCookies 13:4e77264f254a 178 }
FatCookies 13:4e77264f254a 179
FatCookies 13:4e77264f254a 180 int turning = 0;
FatCookies 13:4e77264f254a 181 int keepTurning = 0;
FatCookies 13:4e77264f254a 182 bool slow = false;
FatCookies 13:4e77264f254a 183 inline void recordMeasuredVal() {
FatCookies 12:da96e2f87465 184
FatCookies 13:4e77264f254a 185 measuredValBuffer[frame_counter % 64] = servo_pid.measured_value;
FatCookies 13:4e77264f254a 186
FatCookies 13:4e77264f254a 187 int count = 0;
FatCookies 13:4e77264f254a 188 for(i = 0; i < 10; i++) {
FatCookies 13:4e77264f254a 189 float val = abs(measuredValBuffer[(frame_counter - i) % 64]);
FatCookies 13:4e77264f254a 190 if(val > 0.09) {
FatCookies 13:4e77264f254a 191 count++;
FatCookies 13:4e77264f254a 192 }
FatCookies 13:4e77264f254a 193 }
FatCookies 12:da96e2f87465 194
FatCookies 13:4e77264f254a 195 if(abs(servo_pid.measured_value) > 0.11f){
FatCookies 13:4e77264f254a 196 if(!turning) {
FatCookies 13:4e77264f254a 197 sendString("start turning");
FatCookies 13:4e77264f254a 198
FatCookies 13:4e77264f254a 199 TFC_SetMotorPWM(0.2,0.2);
FatCookies 13:4e77264f254a 200 turning = 1;
FatCookies 13:4e77264f254a 201
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 13:4e77264f254a 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
maximusismax 8:7c5e6b1e7aa5 222 inline void sendImage() {
maximusismax 8:7c5e6b1e7aa5 223 //Only send 1/3 of camera frames to GUI program
maximusismax 8:7c5e6b1e7aa5 224 if((frame_counter % 3) == 0) {
maximusismax 8:7c5e6b1e7aa5 225 pc.putc('H');
maximusismax 8:7c5e6b1e7aa5 226 for(i = 0; i < 128; i++) {
maximusismax 8:7c5e6b1e7aa5 227 pc.putc((int8_t)(TFC_LineScanImage0[i] >> 4) & 0xFF);
FatCookies 13:4e77264f254a 228 }
FatCookies 13:4e77264f254a 229 sendBattery();
FatCookies 13:4e77264f254a 230 }
FatCookies 13:4e77264f254a 231
FatCookies 13:4e77264f254a 232 frame_counter++;
FatCookies 13:4e77264f254a 233 }
FatCookies 13:4e77264f254a 234
FatCookies 13:4e77264f254a 235 inline void sendSpeeds() {
FatCookies 13:4e77264f254a 236 union {
FatCookies 12:da96e2f87465 237 float a;
FatCookies 12:da96e2f87465 238 unsigned char bytes[4];
FatCookies 12:da96e2f87465 239 } thing;
FatCookies 13:4e77264f254a 240
FatCookies 13:4e77264f254a 241 pc.putc('B');
FatCookies 13:4e77264f254a 242 thing.a = wL;
FatCookies 12:da96e2f87465 243 pc.putc(thing.bytes[0]);
FatCookies 12:da96e2f87465 244 pc.putc(thing.bytes[1]);
FatCookies 12:da96e2f87465 245 pc.putc(thing.bytes[2]);
FatCookies 12:da96e2f87465 246 pc.putc(thing.bytes[3]);
FatCookies 13:4e77264f254a 247 thing.a = wR;
FatCookies 13:4e77264f254a 248 pc.putc(thing.bytes[0]);
FatCookies 13:4e77264f254a 249 pc.putc(thing.bytes[1]);
FatCookies 13:4e77264f254a 250 pc.putc(thing.bytes[2]);
FatCookies 13:4e77264f254a 251 pc.putc(thing.bytes[3]);
maximusismax 8:7c5e6b1e7aa5 252 }
maximusismax 8:7c5e6b1e7aa5 253
FatCookies 13:4e77264f254a 254
maximusismax 8:7c5e6b1e7aa5 255 inline void handleComms() {
maximusismax 8:7c5e6b1e7aa5 256 if(curr_cmd != 0) {
FatCookies 4:4afa448c9cce 257 switch(curr_cmd) {
FatCookies 4:4afa448c9cce 258 case 'A':
FatCookies 4:4afa448c9cce 259 if(xb.cBuffer->available() >= 3) {
FatCookies 4:4afa448c9cce 260 char p = xb.cBuffer->read();
FatCookies 4:4afa448c9cce 261 char i = xb.cBuffer->read();
FatCookies 4:4afa448c9cce 262 char d = xb.cBuffer->read();
FatCookies 12:da96e2f87465 263 servo_pid.Kp = p/25.0f;
FatCookies 12:da96e2f87465 264 servo_pid.Ki = i/25.0f;
FatCookies 12:da96e2f87465 265 servo_pid.Kd = d/25.0f;
FatCookies 13:4e77264f254a 266 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 267
FatCookies 4:4afa448c9cce 268 curr_cmd = 0;
FatCookies 4:4afa448c9cce 269 }
FatCookies 4:4afa448c9cce 270 break;
FatCookies 4:4afa448c9cce 271
FatCookies 4:4afa448c9cce 272 case 'F':
FatCookies 6:b0e160c51013 273 if(xb.cBuffer->available() >= 1) {
FatCookies 4:4afa448c9cce 274 char a = xb.cBuffer->read();
FatCookies 6:b0e160c51013 275 speed = a/256.0f;
FatCookies 7:ad893fc41b95 276 TFC_SetMotorPWM(speed,speed);
FatCookies 13:4e77264f254a 277 sendString("s = %u %f",a, speed);
FatCookies 4:4afa448c9cce 278 curr_cmd = 0;
FatCookies 4:4afa448c9cce 279 }
FatCookies 4:4afa448c9cce 280 break;
FatCookies 4:4afa448c9cce 281
FatCookies 4:4afa448c9cce 282 default:
FatCookies 13:4e77264f254a 283 // Unrecognised command
FatCookies 13:4e77264f254a 284 curr_cmd = 0;
FatCookies 4:4afa448c9cce 285 break;
FatCookies 4:4afa448c9cce 286 }
FatCookies 4:4afa448c9cce 287 }
FatCookies 4:4afa448c9cce 288
FatCookies 6:b0e160c51013 289 if(xb.cBuffer->available() > 0 && curr_cmd == 0) {
FatCookies 4:4afa448c9cce 290 char cmd = xb.cBuffer->read();
FatCookies 4:4afa448c9cce 291 if(cmd == 'D') {
FatCookies 4:4afa448c9cce 292 TFC_InitServos(0.00052,0.00122,0.02);
FatCookies 4:4afa448c9cce 293 TFC_HBRIDGE_ENABLE;
FatCookies 10:1bd0224093e4 294 TFC_SetMotorPWM(RIGHT_MOTOR_COMPENSATION_RATIO*speed,speed);
FatCookies 12:da96e2f87465 295 servo_pid.integral = 0;
FatCookies 12:da96e2f87465 296
FatCookies 6:b0e160c51013 297
FatCookies 4:4afa448c9cce 298 } else if (cmd == 'C') {
FatCookies 4:4afa448c9cce 299 TFC_SetMotorPWM(0.0,0.0);
FatCookies 4:4afa448c9cce 300 TFC_HBRIDGE_DISABLE;
FatCookies 4:4afa448c9cce 301 } else if(cmd == 'A') {
FatCookies 4:4afa448c9cce 302 curr_cmd = 'A';
FatCookies 4:4afa448c9cce 303 } else if(cmd == 'F') {
FatCookies 4:4afa448c9cce 304 curr_cmd = 'F';
FatCookies 4:4afa448c9cce 305 }
FatCookies 4:4afa448c9cce 306
FatCookies 4:4afa448c9cce 307 }
maximusismax 8:7c5e6b1e7aa5 308 }
maximusismax 8:7c5e6b1e7aa5 309 inline float findCentreValue() {
maximusismax 8:7c5e6b1e7aa5 310
maximusismax 8:7c5e6b1e7aa5 311 diff = 0;
maximusismax 8:7c5e6b1e7aa5 312 prev = -1;
maximusismax 8:7c5e6b1e7aa5 313 leftChange = left;
maximusismax 8:7c5e6b1e7aa5 314 for(i = 63; i > 0; i--) {
maximusismax 8:7c5e6b1e7aa5 315 curr_left = (int8_t)(TFC_LineScanImage0[i] >> 4) & 0xFF;
maximusismax 8:7c5e6b1e7aa5 316 diff = prev - curr_left;
maximusismax 8:7c5e6b1e7aa5 317 if(abs(diff) >= 10 && curr_left <= 100 && prev != -1) {
maximusismax 8:7c5e6b1e7aa5 318 left = i;
maximusismax 8:7c5e6b1e7aa5 319 break;
maximusismax 8:7c5e6b1e7aa5 320 }
maximusismax 8:7c5e6b1e7aa5 321 prev = curr_left;
maximusismax 8:7c5e6b1e7aa5 322 }
maximusismax 8:7c5e6b1e7aa5 323
maximusismax 8:7c5e6b1e7aa5 324 prev = -1;
maximusismax 8:7c5e6b1e7aa5 325 rightChange = right;
maximusismax 8:7c5e6b1e7aa5 326 for(i = 64; i < 128; i++) {
maximusismax 8:7c5e6b1e7aa5 327 curr_right = (int8_t)(TFC_LineScanImage0[i] >> 4) & 0xFF;
maximusismax 8:7c5e6b1e7aa5 328 int diff = prev - curr_right;
maximusismax 8:7c5e6b1e7aa5 329 if(abs(diff) >= 10 && curr_right <= 100 && prev != -1) {
maximusismax 8:7c5e6b1e7aa5 330 right = i;
maximusismax 8:7c5e6b1e7aa5 331 break;
maximusismax 8:7c5e6b1e7aa5 332 }
maximusismax 8:7c5e6b1e7aa5 333 prev = curr_right;
maximusismax 8:7c5e6b1e7aa5 334 }
maximusismax 8:7c5e6b1e7aa5 335
maximusismax 8:7c5e6b1e7aa5 336 //Calculate how left/right we are
FatCookies 13:4e77264f254a 337 servo_pid.measured_value = (64 - ((left+right)/2))/64.f;
maximusismax 8:7c5e6b1e7aa5 338
FatCookies 13:4e77264f254a 339 return servo_pid.measured_value;
maximusismax 8:7c5e6b1e7aa5 340 }
FatCookies 3:87a5122682fa 341
FatCookies 12:da96e2f87465 342 void handlePID(pid_instance *pid) {
FatCookies 12:da96e2f87465 343 pid->dt = t.read();
FatCookies 12:da96e2f87465 344 pid->pid_error = pid->desired_value - pid->measured_value;
FatCookies 12:da96e2f87465 345 pid->integral = pid->integral + pid->pid_error * pid->dt;
FatCookies 12:da96e2f87465 346 pid->derivative = (pid->pid_error - pid->p_error) / pid->dt;
FatCookies 12:da96e2f87465 347 pid->output = pid->Kp * pid->pid_error + pid->Ki * pid->integral + pid->Kd * pid->derivative;
FatCookies 12:da96e2f87465 348 pid->p_error = pid->pid_error;
FatCookies 12:da96e2f87465 349
FatCookies 12:da96e2f87465 350 if(pid->integral > 1.0f) {
FatCookies 12:da96e2f87465 351 pid->integral = 1.0f;
FatCookies 12:da96e2f87465 352 }
FatCookies 12:da96e2f87465 353 if(pid->integral < -1.0f) {
FatCookies 12:da96e2f87465 354 pid->integral = -1.0f;
FatCookies 12:da96e2f87465 355 }
FatCookies 12:da96e2f87465 356 }
FatCookies 12:da96e2f87465 357
maximusismax 8:7c5e6b1e7aa5 358 inline void PIDController() {
FatCookies 13:4e77264f254a 359 // update motor measurements
FatCookies 13:4e77264f254a 360 left_motor_pid.measured_value = wL;
FatCookies 13:4e77264f254a 361 right_motor_pid.measured_value = wR;
FatCookies 13:4e77264f254a 362
maximusismax 8:7c5e6b1e7aa5 363 //PID Stuff!
FatCookies 3:87a5122682fa 364 t.start();
FatCookies 12:da96e2f87465 365 handlePID(&servo_pid);
FatCookies 13:4e77264f254a 366 handlePID(&left_motor_pid);
FatCookies 13:4e77264f254a 367 handlePID(&right_motor_pid);
FatCookies 12:da96e2f87465 368
FatCookies 12:da96e2f87465 369 if((-1.0 <= servo_pid.output) && (servo_pid.output <= 1.0))
FatCookies 6:b0e160c51013 370 {
FatCookies 12:da96e2f87465 371 TFC_SetServo(0, servo_pid.output);
maximusismax 8:7c5e6b1e7aa5 372 }
maximusismax 8:7c5e6b1e7aa5 373 else //Unhappy PID state
maximusismax 8:7c5e6b1e7aa5 374 {
FatCookies 13:4e77264f254a 375 sendString("out = %f p_err = %f", servo_pid.output, servo_pid.p_error);
maximusismax 8:7c5e6b1e7aa5 376 TFC_InitServos(0.00052, 0.00122, 0.02);
FatCookies 12:da96e2f87465 377 if(servo_pid.output >= 1.0f) {
maximusismax 8:7c5e6b1e7aa5 378 TFC_SetServo(0, 0.9f);
FatCookies 12:da96e2f87465 379 servo_pid.output = 1.0f;
maximusismax 8:7c5e6b1e7aa5 380 } else {
maximusismax 8:7c5e6b1e7aa5 381 TFC_SetServo(0, -0.9f);
FatCookies 12:da96e2f87465 382 servo_pid.output = -1.0f;
maximusismax 8:7c5e6b1e7aa5 383 }
FatCookies 13:4e77264f254a 384 }
FatCookies 12:da96e2f87465 385
FatCookies 6:b0e160c51013 386
FatCookies 3:87a5122682fa 387 t.stop();
FatCookies 3:87a5122682fa 388 t.reset();
FatCookies 3:87a5122682fa 389 t.start();
FatCookies 9:aa2ce38dec6b 390 }
FatCookies 9:aa2ce38dec6b 391
FatCookies 13:4e77264f254a 392 inline void handleStartStop() {
FatCookies 13:4e77264f254a 393 //Hacky way to detect the start/stop signal
FatCookies 13:4e77264f254a 394 if(right - left < 60) {
FatCookies 13:4e77264f254a 395 sendString("START STOP!! &d",startstop);
FatCookies 13:4e77264f254a 396
FatCookies 13:4e77264f254a 397 if(seen) {
FatCookies 13:4e77264f254a 398 seen = false;
FatCookies 13:4e77264f254a 399 } else {
FatCookies 13:4e77264f254a 400 startstop++;
FatCookies 13:4e77264f254a 401 seen = true;
FatCookies 13:4e77264f254a 402 }
FatCookies 13:4e77264f254a 403 //If we've done 5 laps, stop the car
FatCookies 13:4e77264f254a 404 if(startstop >= 1) {
FatCookies 13:4e77264f254a 405 TFC_SetMotorPWM(0.f,0.f);
FatCookies 13:4e77264f254a 406 wait(0.08);
FatCookies 13:4e77264f254a 407 TFC_SetMotorPWM(-0.6f,-0.6f);
FatCookies 13:4e77264f254a 408 wait(0.3);
FatCookies 13:4e77264f254a 409 TFC_SetMotorPWM(0.f,0.f);
FatCookies 13:4e77264f254a 410 TFC_HBRIDGE_DISABLE;
FatCookies 13:4e77264f254a 411 startstop = 0;
FatCookies 13:4e77264f254a 412 }
FatCookies 13:4e77264f254a 413 }
FatCookies 13:4e77264f254a 414 }
FatCookies 9:aa2ce38dec6b 415
FatCookies 9:aa2ce38dec6b 416
FatCookies 9:aa2ce38dec6b 417 inline void initSpeedSensors() {
FatCookies 9:aa2ce38dec6b 418 t1.start();
FatCookies 9:aa2ce38dec6b 419 t2.start();
FatCookies 9:aa2ce38dec6b 420
FatCookies 9:aa2ce38dec6b 421 //Left and Right are defined looking at the rear of the car, in the direction the camera points at.
FatCookies 9:aa2ce38dec6b 422 leftHallSensor.rise(&GetTime_L);
FatCookies 9:aa2ce38dec6b 423 rightHallSensor.rise(&GetTime_R);
FatCookies 9:aa2ce38dec6b 424 }
FatCookies 9:aa2ce38dec6b 425
FatCookies 9:aa2ce38dec6b 426 void GetTime_L(){
FatCookies 9:aa2ce38dec6b 427 Time_L=t1.read_us();
FatCookies 9:aa2ce38dec6b 428 t1.reset();
FatCookies 9:aa2ce38dec6b 429 }
FatCookies 9:aa2ce38dec6b 430
FatCookies 9:aa2ce38dec6b 431 void GetTime_R(){
FatCookies 9:aa2ce38dec6b 432 Time_R=t2.read_us();
FatCookies 9:aa2ce38dec6b 433 t2.reset();
maximusismax 0:566127ca8048 434 }