Rev 2129 | Rev 2132 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1910 | - | 1 | #include <stdlib.h> |
2 | #include <avr/io.h> |
||
3 | #include "eeprom.h" |
||
4 | #include "flight.h" |
||
5 | #include "output.h" |
||
6 | |||
7 | // Necessary for external control and motor test |
||
8 | #include "uart0.h" |
||
9 | #include "timer2.h" |
||
2103 | - | 10 | #include "analog.h" |
1910 | - | 11 | #include "attitude.h" |
12 | #include "controlMixer.h" |
||
2104 | - | 13 | #include "configuration.h" |
1910 | - | 14 | |
15 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
||
16 | |||
17 | /* |
||
2099 | - | 18 | * target-directions integrals. |
1910 | - | 19 | */ |
2099 | - | 20 | int32_t target[3]; |
1910 | - | 21 | |
2099 | - | 22 | /* |
23 | * Error integrals. |
||
24 | */ |
||
25 | int32_t error[3]; |
||
1910 | - | 26 | |
2099 | - | 27 | uint8_t reverse[3]; |
2104 | - | 28 | int32_t maxError[3]; |
2099 | - | 29 | int32_t IPart[3] = { 0, 0, 0 }; |
2104 | - | 30 | PID_t airspeedPID[3]; |
1910 | - | 31 | |
2104 | - | 32 | int16_t controlServos[NUM_CONTROL_SERVOS]; |
1910 | - | 33 | |
34 | /************************************************************************/ |
||
35 | /* Neutral Readings */ |
||
36 | /************************************************************************/ |
||
37 | #define CONTROL_CONFIG_SCALE 10 |
||
38 | |||
2099 | - | 39 | void flight_setGround(void) { |
2102 | - | 40 | IPart[PITCH] = IPart[ROLL] = IPart[YAW] = 0; |
41 | target[PITCH] = attitude[PITCH]; |
||
42 | target[ROLL] = attitude[ROLL]; |
||
43 | target[YAW] = attitude[YAW]; |
||
1910 | - | 44 | } |
45 | |||
2104 | - | 46 | void flight_updateFlightParametersToFlightMode(void) { |
47 | debugOut.analog[16] = currentFlightMode; |
||
2119 | - | 48 | reverse[PITCH] = staticParams.servosReverse & CONTROL_SERVO_REVERSE_ELEVATOR; |
49 | reverse[ROLL] = staticParams.servosReverse & CONTROL_SERVO_REVERSE_AILERONS; |
||
50 | reverse[YAW] = staticParams.servosReverse & CONTROL_SERVO_REVERSE_RUDDER; |
||
1910 | - | 51 | |
2104 | - | 52 | // At a switch to angles, we want to kill errors first. |
53 | // This should be triggered only once per mode change! |
||
54 | if (currentFlightMode == FLIGHT_MODE_ANGLES) { |
||
55 | target[PITCH] = attitude[PITCH]; |
||
56 | target[ROLL] = attitude[ROLL]; |
||
57 | target[YAW] = attitude[YAW]; |
||
58 | } |
||
1910 | - | 59 | |
2104 | - | 60 | for (uint8_t axis=0; axis<3; axis++) { |
61 | maxError[axis] = (int32_t)staticParams.gyroPID[axis].iMax * GYRO_DEG_FACTOR; |
||
62 | } |
||
63 | } |
||
2102 | - | 64 | |
2104 | - | 65 | // Normal at airspeed = 10. |
66 | uint8_t calcAirspeedPID(uint8_t pid) { |
||
2119 | - | 67 | if (!(staticParams.bitConfig & CFG_USE_AIRSPEED_PID)) { |
2104 | - | 68 | return pid; |
2102 | - | 69 | } |
2104 | - | 70 | |
2106 | - | 71 | uint16_t result = (pid * 10) / airspeedVelocity; |
2104 | - | 72 | |
2106 | - | 73 | if (result > 240 || airspeedVelocity == 0) { |
2104 | - | 74 | result = 240; |
75 | } |
||
76 | |||
77 | return result; |
||
1910 | - | 78 | } |
79 | |||
2104 | - | 80 | void setAirspeedPIDs(void) { |
81 | for (uint8_t axis = 0; axis<3; axis++) { |
||
82 | airspeedPID[axis].P = calcAirspeedPID(dynamicParams.gyroPID[axis].P); |
||
83 | airspeedPID[axis].I = calcAirspeedPID(dynamicParams.gyroPID[axis].I); // Should this be??? |
||
84 | airspeedPID[axis].D = dynamicParams.gyroPID[axis].D; |
||
85 | } |
||
86 | } |
||
87 | |||
2119 | - | 88 | #define LOG_STICK_SCALE 8 |
89 | #define LOG_P_SCALE 6 |
||
90 | #define LOG_I_SCALE 10 |
||
91 | #define LOG_D_SCALE 6 |
||
92 | |||
1910 | - | 93 | /************************************************************************/ |
94 | /* Main Flight Control */ |
||
95 | /************************************************************************/ |
||
96 | void flight_control(void) { |
||
2102 | - | 97 | // Mixer Fractions that are combined for Motor Control |
2103 | - | 98 | int16_t term[4]; |
2099 | - | 99 | |
2102 | - | 100 | // PID controller variables |
101 | int16_t PDPart[3]; |
||
1910 | - | 102 | |
2102 | - | 103 | static int8_t debugDataTimer = 1; |
1910 | - | 104 | |
2102 | - | 105 | // High resolution motor values for smoothing of PID motor outputs |
106 | // static int16_t outputFilters[MAX_OUTPUTS]; |
||
1910 | - | 107 | |
2102 | - | 108 | uint8_t axis; |
1910 | - | 109 | |
2104 | - | 110 | setAirspeedPIDs(); |
111 | |||
2103 | - | 112 | term[CONTROL_THROTTLE] = controls[CONTROL_THROTTLE]; |
1910 | - | 113 | |
2102 | - | 114 | // These params are just left the same in all modes. In MANUAL and RATE the results are ignored anyway. |
2122 | - | 115 | int32_t tmp; |
1910 | - | 116 | |
2122 | - | 117 | tmp = ((int32_t)controls[CONTROL_ELEVATOR] * staticParams.stickIElevator) >> LOG_STICK_SCALE; |
2126 | - | 118 | if (reverse[PITCH]) target[PITCH] += tmp; else target[PITCH] -= tmp; |
2122 | - | 119 | |
120 | tmp = ((int32_t)controls[CONTROL_AILERONS] * staticParams.stickIAilerons) >> LOG_STICK_SCALE; |
||
2126 | - | 121 | if (reverse[ROLL]) target[ROLL] += tmp; else target[ROLL] -= tmp; |
2122 | - | 122 | |
123 | tmp = ((int32_t)controls[CONTROL_RUDDER] * staticParams.stickIRudder) >> LOG_STICK_SCALE; |
||
2126 | - | 124 | if (reverse[YAW]) target[YAW] += tmp; else target[YAW] -= tmp; |
2122 | - | 125 | |
2102 | - | 126 | for (axis = PITCH; axis <= YAW; axis++) { |
127 | if (target[axis] > OVER180) { |
||
128 | target[axis] -= OVER360; |
||
2103 | - | 129 | } else if (target[axis] <= -OVER180) { |
2104 | - | 130 | target[axis] += OVER360; |
2102 | - | 131 | } |
1910 | - | 132 | |
2131 | - | 133 | error[axis] = attitude[axis] - target[axis]; |
2129 | - | 134 | |
2131 | - | 135 | #define ROTATETARGET 1 |
136 | // #define TRUNCATEERROR 1 |
||
137 | |||
138 | #ifdef(ROTATETARGET) |
||
139 | if(abs(error[axis]) > OVER180) { |
||
140 | // The shortest way from attitude to target crosses -180. |
||
141 | // Well there are 2 possibilities: A is >0 and T is < 0, that makes E a (too) large positive number. It should be wrapped to negative. |
||
142 | // Or A is <0 and T is >0, that makes E a (too) large negative number. It should be wrapped to positive. |
||
143 | if (error[axis] > 0) { |
||
144 | if (error[axis] < OVER360 - maxError[axis]) { |
||
145 | // too much err. |
||
146 | error[axis] = -maxError[axis]; |
||
147 | target[axis] = attitude[axis] + maxError[axis]; |
||
148 | if (target[axis]) > OVER180) target[axis] -= OVER360; |
||
149 | } else { |
||
150 | // Normal case, we just need to correct for the wrap. Error will be negative. |
||
151 | error[axis] -= OVER360; |
||
152 | } |
||
153 | } else { |
||
154 | if (error[axis] > maxError[axis] - OVER360) { |
||
155 | // too much err. |
||
156 | error[axis] = maxError[axis]; |
||
157 | target[axis] = attitude[axis] - maxError[axis]; |
||
158 | if (target[axis]) < -OVER180) target[axis] += OVER360; |
||
159 | } else { |
||
160 | // Normal case, we just need to correct for the wrap. Error will be negative. |
||
161 | error[axis] += OVER360; |
||
162 | } |
||
163 | } |
||
164 | } else { |
||
165 | // Simple case, linear range. |
||
166 | if (error[axis] > maxError[axis]) { |
||
167 | error[axis] = maxError[axis]; |
||
168 | target[axis] = attitude[axis] - maxError[axis]; |
||
169 | } else if (error[axis] < -maxError[axis]) { |
||
170 | error[axis] = -maxError[axis]; |
||
171 | target[axis] = attitude[axis] + maxError[axis]; |
||
172 | } |
||
2129 | - | 173 | } |
2131 | - | 174 | #endif |
175 | #ifdef(TUNCATEERROR) |
||
2104 | - | 176 | if (error[axis] > maxError[axis]) { |
2119 | - | 177 | error[axis] = maxError[axis]; |
2104 | - | 178 | } else if (error[axis] < -maxError[axis]) { |
2129 | - | 179 | error[axis] = -maxError[axis]; |
2118 | - | 180 | } else { |
2129 | - | 181 | // update I parts here for angles mode. I parts in rate mode is something different. |
2102 | - | 182 | } |
2131 | - | 183 | #endif |
1910 | - | 184 | |
2129 | - | 185 | /* |
186 | * This is the beginning of a version that adjusts the target to differ from the attitude |
||
187 | * by a limited amount. This will eliminate memory over large errors but also knock target angles. |
||
188 | * Idea: The limit could be calculated from the max. servo deflection divided by I factor... |
||
189 | * |
||
190 | */ |
||
191 | /* |
||
192 | if(abs(attitude[axis]-target[axis]) > OVER180) { |
||
193 | if(target[axis] > attitude[axis]) { |
||
194 | |||
195 | } |
||
196 | } |
||
197 | */ |
||
198 | |||
2102 | - | 199 | /************************************************************************/ |
200 | /* Calculate control feedback from angle (gyro integral) */ |
||
201 | /* and angular velocity (gyro signal) */ |
||
202 | /************************************************************************/ |
||
2119 | - | 203 | if (currentFlightMode == FLIGHT_MODE_ANGLES || currentFlightMode == FLIGHT_MODE_RATE) { |
2122 | - | 204 | PDPart[axis] = +(((int32_t) gyro_PID[axis] * (int32_t) airspeedPID[axis].P) >> LOG_P_SCALE) |
2119 | - | 205 | + (((int16_t)gyroD[axis] * (int16_t) airspeedPID[axis].D) >> LOG_D_SCALE); |
2122 | - | 206 | //if (reverse[axis]) |
207 | // PDPart[axis] = -PDPart[axis]; |
||
2104 | - | 208 | } else { |
209 | PDPart[axis] = 0; |
||
210 | } |
||
1910 | - | 211 | |
2104 | - | 212 | if (currentFlightMode == FLIGHT_MODE_ANGLES) { |
2119 | - | 213 | int16_t anglePart = (int32_t)(error[axis] * (int32_t) airspeedPID[axis].I) >> LOG_I_SCALE; |
2122 | - | 214 | // if (reverse[axis]) |
215 | PDPart[axis] += anglePart; |
||
216 | // else |
||
217 | // PDPart[axis] -= anglePart; |
||
2104 | - | 218 | } |
2118 | - | 219 | |
2102 | - | 220 | // Add I parts here... these are integrated errors. |
2122 | - | 221 | if (reverse[axis]) |
222 | term[axis] = controls[axis] - PDPart[axis]; // + IPart[axis]; |
||
223 | else |
||
224 | term[axis] = controls[axis] + PDPart[axis]; // + IPart[axis]; |
||
2102 | - | 225 | } |
1910 | - | 226 | |
2102 | - | 227 | debugOut.analog[12] = term[PITCH]; |
228 | debugOut.analog[13] = term[ROLL]; |
||
2103 | - | 229 | debugOut.analog[14] = term[YAW]; |
230 | debugOut.analog[15] = term[THROTTLE]; |
||
2099 | - | 231 | |
2104 | - | 232 | for (uint8_t i = 0; i < NUM_CONTROL_SERVOS; i++) { |
2102 | - | 233 | int16_t tmp; |
234 | if (servoTestActive) { |
||
2119 | - | 235 | controlServos[i] = ((int16_t) servoTest[i] - 128) * 8; |
2102 | - | 236 | } else { |
237 | // Follow the normal order of servos: Ailerons, elevator, throttle, rudder. |
||
238 | switch (i) { |
||
239 | case 0: |
||
240 | tmp = term[ROLL]; |
||
241 | break; |
||
242 | case 1: |
||
243 | tmp = term[PITCH]; |
||
244 | break; |
||
245 | case 2: |
||
2103 | - | 246 | tmp = term[THROTTLE]; |
2102 | - | 247 | break; |
248 | case 3: |
||
249 | tmp = term[YAW]; |
||
250 | break; |
||
251 | default: |
||
252 | tmp = 0; |
||
253 | } |
||
254 | // These are all signed and in the same units as the RC stuff in rc.c. |
||
255 | controlServos[i] = tmp; |
||
256 | } |
||
257 | } |
||
1910 | - | 258 | |
2103 | - | 259 | calculateControlServoValues(); |
1910 | - | 260 | |
2102 | - | 261 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
262 | // Debugging |
||
263 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
264 | if (!(--debugDataTimer)) { |
||
265 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
||
2103 | - | 266 | debugOut.analog[0] = gyro_PID[PITCH]; // in 0.1 deg |
267 | debugOut.analog[1] = gyro_PID[ROLL]; // in 0.1 deg |
||
268 | debugOut.analog[2] = gyro_PID[YAW]; |
||
1910 | - | 269 | |
2102 | - | 270 | debugOut.analog[3] = attitude[PITCH] / (GYRO_DEG_FACTOR / 10); // in 0.1 deg |
271 | debugOut.analog[4] = attitude[ROLL] / (GYRO_DEG_FACTOR / 10); // in 0.1 deg |
||
272 | debugOut.analog[5] = attitude[YAW] / (GYRO_DEG_FACTOR / 10); |
||
2099 | - | 273 | |
2102 | - | 274 | debugOut.analog[6] = target[PITCH] / (GYRO_DEG_FACTOR / 10); // in 0.1 deg |
275 | debugOut.analog[7] = target[ROLL] / (GYRO_DEG_FACTOR / 10); // in 0.1 deg |
||
276 | debugOut.analog[8] = target[YAW] / (GYRO_DEG_FACTOR / 10); |
||
277 | |||
278 | debugOut.analog[9] = error[PITCH] / (GYRO_DEG_FACTOR / 10); // in 0.1 deg |
||
279 | debugOut.analog[10] = error[ROLL] / (GYRO_DEG_FACTOR / 10); // in 0.1 deg |
||
280 | debugOut.analog[11] = error[YAW] / (GYRO_DEG_FACTOR / 10); |
||
281 | |||
282 | debugOut.analog[12] = term[PITCH]; |
||
283 | debugOut.analog[13] = term[ROLL]; |
||
284 | debugOut.analog[14] = term[YAW]; |
||
285 | |||
286 | //DebugOut.Analog[18] = (10 * controlIntegrals[CONTROL_ELEVATOR]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
||
287 | //DebugOut.Analog[19] = (10 * controlIntegrals[CONTROL_AILERONS]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
||
288 | //debugOut.analog[22] = (10 * IPart[PITCH]) / GYRO_DEG_FACTOR; // in 0.1 deg |
||
289 | //debugOut.analog[23] = (10 * IPart[ROLL]) / GYRO_DEG_FACTOR; // in 0.1 deg |
||
290 | } |
||
1910 | - | 291 | } |