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@9:aa2ce38dec6b, 2016-11-16 (annotated)
- Committer:
- FatCookies
- Date:
- Wed Nov 16 16:05:13 2016 +0000
- Revision:
- 9:aa2ce38dec6b
- Parent:
- 8:7c5e6b1e7aa5
- Child:
- 10:1bd0224093e4
added owens hall speed sesnor + send angular speed via xbee
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" |
maximusismax | 8:7c5e6b1e7aa5 | 16 | |
maximusismax | 8:7c5e6b1e7aa5 | 17 | #define BAUD_RATE 57600 |
FatCookies | 6:b0e160c51013 | 18 | #define CAM_THRESHOLD 109 |
maximusismax | 8:7c5e6b1e7aa5 | 19 | #define CAM_DIFF 10 |
maximusismax | 8:7c5e6b1e7aa5 | 20 | |
FatCookies | 3:87a5122682fa | 21 | //Serial pc(USBTX,USBRX); |
FatCookies | 3:87a5122682fa | 22 | Serial pc(PTD3,PTD2); |
FatCookies | 4:4afa448c9cce | 23 | XBEE xb(&pc); |
FatCookies | 3:87a5122682fa | 24 | |
maximusismax | 8:7c5e6b1e7aa5 | 25 | //Woo global variables! |
maximusismax | 8:7c5e6b1e7aa5 | 26 | bool onTrack; |
FatCookies | 3:87a5122682fa | 27 | char curr_line[128]; |
FatCookies | 3:87a5122682fa | 28 | uint8_t curr_left; |
FatCookies | 3:87a5122682fa | 29 | uint8_t curr_right; |
FatCookies | 3:87a5122682fa | 30 | uint8_t right; |
FatCookies | 3:87a5122682fa | 31 | uint8_t left; |
maximusismax | 8:7c5e6b1e7aa5 | 32 | int8_t leftChange; |
maximusismax | 8:7c5e6b1e7aa5 | 33 | int8_t rightChange; |
maximusismax | 8:7c5e6b1e7aa5 | 34 | int diff; |
maximusismax | 8:7c5e6b1e7aa5 | 35 | int prev; |
maximusismax | 8:7c5e6b1e7aa5 | 36 | int i = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 37 | float measuredValBuffer[5]; |
maximusismax | 8:7c5e6b1e7aa5 | 38 | uint8_t valBufferIndex; |
maximusismax | 8:7c5e6b1e7aa5 | 39 | |
maximusismax | 8:7c5e6b1e7aa5 | 40 | //Some PID variables |
maximusismax | 8:7c5e6b1e7aa5 | 41 | float Kp; |
maximusismax | 8:7c5e6b1e7aa5 | 42 | float Ki; |
maximusismax | 8:7c5e6b1e7aa5 | 43 | float Kd; |
maximusismax | 8:7c5e6b1e7aa5 | 44 | float dt; |
maximusismax | 8:7c5e6b1e7aa5 | 45 | float p_error; |
maximusismax | 8:7c5e6b1e7aa5 | 46 | float pid_error; |
maximusismax | 8:7c5e6b1e7aa5 | 47 | float integral; |
maximusismax | 8:7c5e6b1e7aa5 | 48 | float measured_value, desired_value, derivative; |
maximusismax | 8:7c5e6b1e7aa5 | 49 | float output; |
FatCookies | 3:87a5122682fa | 50 | Timer t; |
FatCookies | 4:4afa448c9cce | 51 | |
FatCookies | 9:aa2ce38dec6b | 52 | //Speed Sensors variables |
FatCookies | 9:aa2ce38dec6b | 53 | InterruptIn leftHallSensor(D0); |
FatCookies | 9:aa2ce38dec6b | 54 | InterruptIn rightHallSensor(D2); |
FatCookies | 9:aa2ce38dec6b | 55 | Timer t1; |
FatCookies | 9:aa2ce38dec6b | 56 | Timer t2; |
FatCookies | 9:aa2ce38dec6b | 57 | volatile float Time_L,Time_R; |
FatCookies | 9:aa2ce38dec6b | 58 | float wL, wR; |
FatCookies | 9:aa2ce38dec6b | 59 | void GetTime_L(); |
FatCookies | 9:aa2ce38dec6b | 60 | void GetTime_R(); |
FatCookies | 9:aa2ce38dec6b | 61 | inline void initSpeedSensors(); |
FatCookies | 9:aa2ce38dec6b | 62 | |
FatCookies | 9:aa2ce38dec6b | 63 | // Current comms command |
FatCookies | 4:4afa448c9cce | 64 | char curr_cmd = 0; |
FatCookies | 4:4afa448c9cce | 65 | |
FatCookies | 4:4afa448c9cce | 66 | float speed = 0.3; |
FatCookies | 6:b0e160c51013 | 67 | int frame_counter = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 68 | |
maximusismax | 8:7c5e6b1e7aa5 | 69 | //Hacky start/stop signal detection |
maximusismax | 8:7c5e6b1e7aa5 | 70 | int startstop = 0; |
FatCookies | 7:ad893fc41b95 | 71 | bool seen = false; |
maximusismax | 8:7c5e6b1e7aa5 | 72 | |
maximusismax | 8:7c5e6b1e7aa5 | 73 | void sendString(const char *format, ...); |
maximusismax | 8:7c5e6b1e7aa5 | 74 | void initVariables(); |
maximusismax | 8:7c5e6b1e7aa5 | 75 | inline void handleComms(); |
maximusismax | 8:7c5e6b1e7aa5 | 76 | inline float findCentreValue(); |
maximusismax | 8:7c5e6b1e7aa5 | 77 | inline void PIDController(); |
maximusismax | 8:7c5e6b1e7aa5 | 78 | inline void sendImage(); |
maximusismax | 8:7c5e6b1e7aa5 | 79 | |
maximusismax | 0:566127ca8048 | 80 | int main() { |
maximusismax | 8:7c5e6b1e7aa5 | 81 | //Set up TFC driver stuff |
maximusismax | 0:566127ca8048 | 82 | TFC_Init(); |
FatCookies | 3:87a5122682fa | 83 | TFC_InitServos(0.00052,0.00122,0.02); |
maximusismax | 8:7c5e6b1e7aa5 | 84 | //Old things to make the car move at run-time |
maximusismax | 8:7c5e6b1e7aa5 | 85 | //Should tie these to a button for the actual races |
FatCookies | 4:4afa448c9cce | 86 | //TFC_HBRIDGE_ENABLE; |
FatCookies | 4:4afa448c9cce | 87 | //TFC_SetMotorPWM(0.3,0.3); |
maximusismax | 2:4b6f6fc84793 | 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(); |
maximusismax | 0:566127ca8048 | 95 | |
maximusismax | 0:566127ca8048 | 96 | while(1) { |
FatCookies | 3:87a5122682fa | 97 | |
maximusismax | 8:7c5e6b1e7aa5 | 98 | handleComms(); |
maximusismax | 8:7c5e6b1e7aa5 | 99 | |
maximusismax | 8:7c5e6b1e7aa5 | 100 | //If we have an image ready |
maximusismax | 8:7c5e6b1e7aa5 | 101 | if(TFC_LineScanImageReady>0) { |
maximusismax | 8:7c5e6b1e7aa5 | 102 | measured_value = findCentreValue(); |
maximusismax | 8:7c5e6b1e7aa5 | 103 | |
maximusismax | 8:7c5e6b1e7aa5 | 104 | PIDController(); |
maximusismax | 8:7c5e6b1e7aa5 | 105 | |
maximusismax | 8:7c5e6b1e7aa5 | 106 | sendImage(); |
maximusismax | 8:7c5e6b1e7aa5 | 107 | |
maximusismax | 8:7c5e6b1e7aa5 | 108 | //Hacky way to detect the start/stop signal |
maximusismax | 8:7c5e6b1e7aa5 | 109 | if(right - left < 60) { |
maximusismax | 8:7c5e6b1e7aa5 | 110 | pc.putc('E'); |
maximusismax | 8:7c5e6b1e7aa5 | 111 | pc.printf("START STOP!! &d",startstop); |
maximusismax | 8:7c5e6b1e7aa5 | 112 | pc.putc(0); |
maximusismax | 8:7c5e6b1e7aa5 | 113 | |
maximusismax | 8:7c5e6b1e7aa5 | 114 | if(seen) { |
maximusismax | 8:7c5e6b1e7aa5 | 115 | seen = false; |
maximusismax | 8:7c5e6b1e7aa5 | 116 | } else { |
maximusismax | 8:7c5e6b1e7aa5 | 117 | startstop++; |
maximusismax | 8:7c5e6b1e7aa5 | 118 | seen = true; |
maximusismax | 8:7c5e6b1e7aa5 | 119 | } |
maximusismax | 8:7c5e6b1e7aa5 | 120 | //If we've done 5 laps, stop the car |
maximusismax | 8:7c5e6b1e7aa5 | 121 | if(startstop >= 1) { |
maximusismax | 8:7c5e6b1e7aa5 | 122 | TFC_SetMotorPWM(0.0,0.0); |
maximusismax | 8:7c5e6b1e7aa5 | 123 | TFC_HBRIDGE_DISABLE; |
maximusismax | 8:7c5e6b1e7aa5 | 124 | startstop = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 125 | } |
maximusismax | 8:7c5e6b1e7aa5 | 126 | } |
maximusismax | 8:7c5e6b1e7aa5 | 127 | |
maximusismax | 8:7c5e6b1e7aa5 | 128 | //Reset image ready flag |
maximusismax | 8:7c5e6b1e7aa5 | 129 | TFC_LineScanImageReady=0; |
maximusismax | 8:7c5e6b1e7aa5 | 130 | } |
maximusismax | 8:7c5e6b1e7aa5 | 131 | } |
maximusismax | 8:7c5e6b1e7aa5 | 132 | } |
maximusismax | 8:7c5e6b1e7aa5 | 133 | |
maximusismax | 8:7c5e6b1e7aa5 | 134 | void sendString(const char *format, ...) { |
maximusismax | 8:7c5e6b1e7aa5 | 135 | va_list arg; |
maximusismax | 8:7c5e6b1e7aa5 | 136 | |
maximusismax | 8:7c5e6b1e7aa5 | 137 | pc.putc('E'); |
maximusismax | 8:7c5e6b1e7aa5 | 138 | va_start (arg, format); |
maximusismax | 8:7c5e6b1e7aa5 | 139 | pc.vprintf(format,arg); |
maximusismax | 8:7c5e6b1e7aa5 | 140 | va_end (arg); |
maximusismax | 8:7c5e6b1e7aa5 | 141 | pc.putc(0); |
maximusismax | 8:7c5e6b1e7aa5 | 142 | } |
maximusismax | 8:7c5e6b1e7aa5 | 143 | |
maximusismax | 8:7c5e6b1e7aa5 | 144 | void initVariables() { |
maximusismax | 8:7c5e6b1e7aa5 | 145 | //Tunable PID variables |
maximusismax | 8:7c5e6b1e7aa5 | 146 | Kp = 125 / 25.0f; |
maximusismax | 8:7c5e6b1e7aa5 | 147 | Ki = 12.0f / 25.0f; |
maximusismax | 8:7c5e6b1e7aa5 | 148 | Kd = 0.0f; |
maximusismax | 8:7c5e6b1e7aa5 | 149 | dt = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 150 | p_error = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 151 | pid_error = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 152 | integral = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 153 | measured_value = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 154 | desired_value = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 155 | derivative = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 156 | |
maximusismax | 8:7c5e6b1e7aa5 | 157 | valBufferIndex = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 158 | //Measured value is a float between -1.0 and 1.0 (from left to right) |
maximusismax | 8:7c5e6b1e7aa5 | 159 | //Desired value is always 0.0 (as in, car is in the middle of the road) |
maximusismax | 8:7c5e6b1e7aa5 | 160 | } |
maximusismax | 8:7c5e6b1e7aa5 | 161 | |
maximusismax | 8:7c5e6b1e7aa5 | 162 | inline void sendImage() { |
maximusismax | 8:7c5e6b1e7aa5 | 163 | //Only send 1/3 of camera frames to GUI program |
maximusismax | 8:7c5e6b1e7aa5 | 164 | if((frame_counter % 3) == 0) { |
maximusismax | 8:7c5e6b1e7aa5 | 165 | pc.putc('H'); |
maximusismax | 8:7c5e6b1e7aa5 | 166 | for(i = 0; i < 128; i++) { |
maximusismax | 8:7c5e6b1e7aa5 | 167 | pc.putc((int8_t)(TFC_LineScanImage0[i] >> 4) & 0xFF); |
maximusismax | 8:7c5e6b1e7aa5 | 168 | } |
FatCookies | 9:aa2ce38dec6b | 169 | |
FatCookies | 9:aa2ce38dec6b | 170 | wL=Get_Speed(Time_L); |
FatCookies | 9:aa2ce38dec6b | 171 | wR=Get_Speed(Time_R); |
FatCookies | 9:aa2ce38dec6b | 172 | sendString("wL = %f, wR = %f",wL,wR); |
maximusismax | 8:7c5e6b1e7aa5 | 173 | } |
maximusismax | 8:7c5e6b1e7aa5 | 174 | frame_counter++; |
maximusismax | 8:7c5e6b1e7aa5 | 175 | } |
maximusismax | 8:7c5e6b1e7aa5 | 176 | |
maximusismax | 8:7c5e6b1e7aa5 | 177 | inline void handleComms() { |
maximusismax | 8:7c5e6b1e7aa5 | 178 | if(curr_cmd != 0) { |
FatCookies | 4:4afa448c9cce | 179 | switch(curr_cmd) { |
FatCookies | 4:4afa448c9cce | 180 | case 'A': |
FatCookies | 4:4afa448c9cce | 181 | if(xb.cBuffer->available() >= 3) { |
FatCookies | 4:4afa448c9cce | 182 | char p = xb.cBuffer->read(); |
FatCookies | 4:4afa448c9cce | 183 | char i = xb.cBuffer->read(); |
FatCookies | 4:4afa448c9cce | 184 | char d = xb.cBuffer->read(); |
FatCookies | 6:b0e160c51013 | 185 | Kp = p/25.0f; |
FatCookies | 6:b0e160c51013 | 186 | Ki = i/25.0f; |
FatCookies | 6:b0e160c51013 | 187 | Kd = d/25.0f; |
FatCookies | 4:4afa448c9cce | 188 | pc.putc('E'); |
maximusismax | 8:7c5e6b1e7aa5 | 189 | pc.printf("pid change, Kp: %f, Ki: %f, Kd: %f, p: %u, i: %u, d: %u", Kp, Ki, Kd, p, i, d); |
FatCookies | 6:b0e160c51013 | 190 | pc.putc(0); |
maximusismax | 8:7c5e6b1e7aa5 | 191 | |
FatCookies | 4:4afa448c9cce | 192 | curr_cmd = 0; |
FatCookies | 4:4afa448c9cce | 193 | } |
FatCookies | 4:4afa448c9cce | 194 | break; |
FatCookies | 4:4afa448c9cce | 195 | |
FatCookies | 4:4afa448c9cce | 196 | case 'F': |
FatCookies | 6:b0e160c51013 | 197 | if(xb.cBuffer->available() >= 1) { |
FatCookies | 4:4afa448c9cce | 198 | char a = xb.cBuffer->read(); |
FatCookies | 6:b0e160c51013 | 199 | speed = a/256.0f; |
FatCookies | 7:ad893fc41b95 | 200 | TFC_SetMotorPWM(speed,speed); |
FatCookies | 4:4afa448c9cce | 201 | pc.putc('E'); |
FatCookies | 6:b0e160c51013 | 202 | pc.printf("s = %u %f",a, speed); |
FatCookies | 6:b0e160c51013 | 203 | pc.putc(0); |
FatCookies | 4:4afa448c9cce | 204 | curr_cmd = 0; |
FatCookies | 4:4afa448c9cce | 205 | |
FatCookies | 4:4afa448c9cce | 206 | } |
FatCookies | 4:4afa448c9cce | 207 | break; |
FatCookies | 4:4afa448c9cce | 208 | |
FatCookies | 4:4afa448c9cce | 209 | default: |
FatCookies | 4:4afa448c9cce | 210 | break; |
FatCookies | 4:4afa448c9cce | 211 | } |
FatCookies | 4:4afa448c9cce | 212 | } |
FatCookies | 4:4afa448c9cce | 213 | |
FatCookies | 6:b0e160c51013 | 214 | if(xb.cBuffer->available() > 0 && curr_cmd == 0) { |
FatCookies | 4:4afa448c9cce | 215 | char cmd = xb.cBuffer->read(); |
FatCookies | 4:4afa448c9cce | 216 | if(cmd == 'D') { |
FatCookies | 4:4afa448c9cce | 217 | TFC_InitServos(0.00052,0.00122,0.02); |
FatCookies | 4:4afa448c9cce | 218 | TFC_HBRIDGE_ENABLE; |
FatCookies | 4:4afa448c9cce | 219 | TFC_SetMotorPWM(speed,speed); |
FatCookies | 6:b0e160c51013 | 220 | integral = 0; |
FatCookies | 6:b0e160c51013 | 221 | |
FatCookies | 4:4afa448c9cce | 222 | } else if (cmd == 'C') { |
FatCookies | 4:4afa448c9cce | 223 | TFC_SetMotorPWM(0.0,0.0); |
FatCookies | 4:4afa448c9cce | 224 | TFC_HBRIDGE_DISABLE; |
FatCookies | 4:4afa448c9cce | 225 | } else if(cmd == 'A') { |
FatCookies | 4:4afa448c9cce | 226 | curr_cmd = 'A'; |
FatCookies | 4:4afa448c9cce | 227 | } else if(cmd == 'F') { |
FatCookies | 4:4afa448c9cce | 228 | curr_cmd = 'F'; |
FatCookies | 4:4afa448c9cce | 229 | } |
FatCookies | 4:4afa448c9cce | 230 | |
FatCookies | 4:4afa448c9cce | 231 | } |
maximusismax | 8:7c5e6b1e7aa5 | 232 | } |
maximusismax | 8:7c5e6b1e7aa5 | 233 | inline float findCentreValue() { |
maximusismax | 8:7c5e6b1e7aa5 | 234 | float measuredValue; |
maximusismax | 8:7c5e6b1e7aa5 | 235 | |
maximusismax | 8:7c5e6b1e7aa5 | 236 | diff = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 237 | prev = -1; |
maximusismax | 8:7c5e6b1e7aa5 | 238 | leftChange = left; |
maximusismax | 8:7c5e6b1e7aa5 | 239 | for(i = 63; i > 0; i--) { |
maximusismax | 8:7c5e6b1e7aa5 | 240 | curr_left = (int8_t)(TFC_LineScanImage0[i] >> 4) & 0xFF; |
maximusismax | 8:7c5e6b1e7aa5 | 241 | diff = prev - curr_left; |
maximusismax | 8:7c5e6b1e7aa5 | 242 | if(abs(diff) >= 10 && curr_left <= 100 && prev != -1) { |
maximusismax | 8:7c5e6b1e7aa5 | 243 | left = i; |
maximusismax | 8:7c5e6b1e7aa5 | 244 | break; |
maximusismax | 8:7c5e6b1e7aa5 | 245 | } |
maximusismax | 8:7c5e6b1e7aa5 | 246 | prev = curr_left; |
maximusismax | 8:7c5e6b1e7aa5 | 247 | } |
maximusismax | 8:7c5e6b1e7aa5 | 248 | |
maximusismax | 8:7c5e6b1e7aa5 | 249 | prev = -1; |
maximusismax | 8:7c5e6b1e7aa5 | 250 | rightChange = right; |
maximusismax | 8:7c5e6b1e7aa5 | 251 | for(i = 64; i < 128; i++) { |
maximusismax | 8:7c5e6b1e7aa5 | 252 | curr_right = (int8_t)(TFC_LineScanImage0[i] >> 4) & 0xFF; |
maximusismax | 8:7c5e6b1e7aa5 | 253 | int diff = prev - curr_right; |
maximusismax | 8:7c5e6b1e7aa5 | 254 | if(abs(diff) >= 10 && curr_right <= 100 && prev != -1) { |
maximusismax | 8:7c5e6b1e7aa5 | 255 | right = i; |
maximusismax | 8:7c5e6b1e7aa5 | 256 | break; |
maximusismax | 8:7c5e6b1e7aa5 | 257 | } |
maximusismax | 8:7c5e6b1e7aa5 | 258 | prev = curr_right; |
maximusismax | 8:7c5e6b1e7aa5 | 259 | } |
maximusismax | 8:7c5e6b1e7aa5 | 260 | |
maximusismax | 8:7c5e6b1e7aa5 | 261 | //Calculate how left/right we are |
maximusismax | 8:7c5e6b1e7aa5 | 262 | measuredValue = (64 - ((left+right)/2))/64.f; |
maximusismax | 8:7c5e6b1e7aa5 | 263 | measuredValBuffer[valBufferIndex % 5] = measuredValue; |
maximusismax | 8:7c5e6b1e7aa5 | 264 | valBufferIndex++; |
maximusismax | 8:7c5e6b1e7aa5 | 265 | |
maximusismax | 8:7c5e6b1e7aa5 | 266 | return measuredValue; |
maximusismax | 8:7c5e6b1e7aa5 | 267 | } |
FatCookies | 3:87a5122682fa | 268 | |
maximusismax | 8:7c5e6b1e7aa5 | 269 | inline void PIDController() { |
maximusismax | 8:7c5e6b1e7aa5 | 270 | //PID Stuff! |
FatCookies | 3:87a5122682fa | 271 | t.start(); |
maximusismax | 8:7c5e6b1e7aa5 | 272 | dt = t.read(); |
maximusismax | 8:7c5e6b1e7aa5 | 273 | pid_error = desired_value - measured_value; |
maximusismax | 8:7c5e6b1e7aa5 | 274 | integral = integral + pid_error * dt; |
maximusismax | 8:7c5e6b1e7aa5 | 275 | derivative = (pid_error - p_error) / dt; |
maximusismax | 8:7c5e6b1e7aa5 | 276 | output = Kp * pid_error + Ki * integral + Kd * derivative; |
maximusismax | 8:7c5e6b1e7aa5 | 277 | p_error = pid_error; |
maximusismax | 8:7c5e6b1e7aa5 | 278 | |
maximusismax | 8:7c5e6b1e7aa5 | 279 | if(integral > 1.0f) { |
FatCookies | 6:b0e160c51013 | 280 | integral = 1.0f; |
FatCookies | 6:b0e160c51013 | 281 | } |
FatCookies | 6:b0e160c51013 | 282 | if(integral < -1.0f) { |
FatCookies | 6:b0e160c51013 | 283 | integral = -1.0f; |
FatCookies | 6:b0e160c51013 | 284 | } |
FatCookies | 6:b0e160c51013 | 285 | |
maximusismax | 8:7c5e6b1e7aa5 | 286 | if((-1.0 <= output) && (output <= 1.0)) |
FatCookies | 6:b0e160c51013 | 287 | { |
maximusismax | 8:7c5e6b1e7aa5 | 288 | TFC_SetServo(0, output); |
maximusismax | 8:7c5e6b1e7aa5 | 289 | } |
maximusismax | 8:7c5e6b1e7aa5 | 290 | else //Unhappy PID state |
maximusismax | 8:7c5e6b1e7aa5 | 291 | { |
maximusismax | 8:7c5e6b1e7aa5 | 292 | pc.putc('E'); |
maximusismax | 8:7c5e6b1e7aa5 | 293 | pc.printf("pid unhappy"); |
maximusismax | 8:7c5e6b1e7aa5 | 294 | pc.putc(0); |
maximusismax | 8:7c5e6b1e7aa5 | 295 | pc.putc('E'); |
maximusismax | 8:7c5e6b1e7aa5 | 296 | pc.printf("out = %f p_err = %f", output, p_error); |
maximusismax | 8:7c5e6b1e7aa5 | 297 | pc.putc(0); |
maximusismax | 8:7c5e6b1e7aa5 | 298 | TFC_InitServos(0.00052, 0.00122, 0.02); |
maximusismax | 8:7c5e6b1e7aa5 | 299 | //output, pid_error, p_error, integral, derivative = 0; |
maximusismax | 8:7c5e6b1e7aa5 | 300 | |
maximusismax | 8:7c5e6b1e7aa5 | 301 | if(output >= 1.0f) { |
maximusismax | 8:7c5e6b1e7aa5 | 302 | TFC_SetServo(0, 0.9f); |
maximusismax | 8:7c5e6b1e7aa5 | 303 | output = 1.0f; |
maximusismax | 8:7c5e6b1e7aa5 | 304 | } else { |
maximusismax | 8:7c5e6b1e7aa5 | 305 | TFC_SetServo(0, -0.9f); |
maximusismax | 8:7c5e6b1e7aa5 | 306 | output = -1.0f; |
maximusismax | 8:7c5e6b1e7aa5 | 307 | } |
maximusismax | 8:7c5e6b1e7aa5 | 308 | } |
FatCookies | 6:b0e160c51013 | 309 | |
FatCookies | 3:87a5122682fa | 310 | t.stop(); |
FatCookies | 3:87a5122682fa | 311 | t.reset(); |
FatCookies | 3:87a5122682fa | 312 | t.start(); |
FatCookies | 9:aa2ce38dec6b | 313 | } |
FatCookies | 9:aa2ce38dec6b | 314 | |
FatCookies | 9:aa2ce38dec6b | 315 | |
FatCookies | 9:aa2ce38dec6b | 316 | |
FatCookies | 9:aa2ce38dec6b | 317 | inline void initSpeedSensors() { |
FatCookies | 9:aa2ce38dec6b | 318 | t1.start(); |
FatCookies | 9:aa2ce38dec6b | 319 | t2.start(); |
FatCookies | 9:aa2ce38dec6b | 320 | |
FatCookies | 9:aa2ce38dec6b | 321 | //Left and Right are defined looking at the rear of the car, in the direction the camera points at. |
FatCookies | 9:aa2ce38dec6b | 322 | leftHallSensor.rise(&GetTime_L); |
FatCookies | 9:aa2ce38dec6b | 323 | rightHallSensor.rise(&GetTime_R); |
FatCookies | 9:aa2ce38dec6b | 324 | } |
FatCookies | 9:aa2ce38dec6b | 325 | |
FatCookies | 9:aa2ce38dec6b | 326 | void GetTime_L(){ |
FatCookies | 9:aa2ce38dec6b | 327 | Time_L=t1.read_us(); |
FatCookies | 9:aa2ce38dec6b | 328 | t1.reset(); |
FatCookies | 9:aa2ce38dec6b | 329 | } |
FatCookies | 9:aa2ce38dec6b | 330 | |
FatCookies | 9:aa2ce38dec6b | 331 | void GetTime_R(){ |
FatCookies | 9:aa2ce38dec6b | 332 | Time_R=t2.read_us(); |
FatCookies | 9:aa2ce38dec6b | 333 | t2.reset(); |
maximusismax | 0:566127ca8048 | 334 | } |