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