Rev 2138 | 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 | */ |
||
1910 | - | 25 | |
2104 | - | 26 | int32_t maxError[3]; |
2099 | - | 27 | int32_t IPart[3] = { 0, 0, 0 }; |
2104 | - | 28 | PID_t airspeedPID[3]; |
1910 | - | 29 | |
2104 | - | 30 | int16_t controlServos[NUM_CONTROL_SERVOS]; |
1910 | - | 31 | |
32 | /************************************************************************/ |
||
33 | /* Neutral Readings */ |
||
34 | /************************************************************************/ |
||
35 | #define CONTROL_CONFIG_SCALE 10 |
||
36 | |||
2099 | - | 37 | void flight_setGround(void) { |
2102 | - | 38 | IPart[PITCH] = IPart[ROLL] = IPart[YAW] = 0; |
39 | target[PITCH] = attitude[PITCH]; |
||
40 | target[ROLL] = attitude[ROLL]; |
||
41 | target[YAW] = attitude[YAW]; |
||
1910 | - | 42 | } |
43 | |||
2104 | - | 44 | void flight_updateFlightParametersToFlightMode(void) { |
45 | debugOut.analog[16] = currentFlightMode; |
||
1910 | - | 46 | |
2104 | - | 47 | // At a switch to angles, we want to kill errors first. |
48 | // This should be triggered only once per mode change! |
||
49 | if (currentFlightMode == FLIGHT_MODE_ANGLES) { |
||
50 | target[PITCH] = attitude[PITCH]; |
||
51 | target[ROLL] = attitude[ROLL]; |
||
52 | target[YAW] = attitude[YAW]; |
||
53 | } |
||
1910 | - | 54 | |
2104 | - | 55 | for (uint8_t axis=0; axis<3; axis++) { |
56 | maxError[axis] = (int32_t)staticParams.gyroPID[axis].iMax * GYRO_DEG_FACTOR; |
||
57 | } |
||
58 | } |
||
2102 | - | 59 | |
2104 | - | 60 | // Normal at airspeed = 10. |
61 | uint8_t calcAirspeedPID(uint8_t pid) { |
||
2119 | - | 62 | if (!(staticParams.bitConfig & CFG_USE_AIRSPEED_PID)) { |
2104 | - | 63 | return pid; |
2102 | - | 64 | } |
2104 | - | 65 | |
2106 | - | 66 | uint16_t result = (pid * 10) / airspeedVelocity; |
2104 | - | 67 | |
2106 | - | 68 | if (result > 240 || airspeedVelocity == 0) { |
2104 | - | 69 | result = 240; |
70 | } |
||
71 | |||
72 | return result; |
||
1910 | - | 73 | } |
74 | |||
2104 | - | 75 | void setAirspeedPIDs(void) { |
76 | for (uint8_t axis = 0; axis<3; axis++) { |
||
77 | airspeedPID[axis].P = calcAirspeedPID(dynamicParams.gyroPID[axis].P); |
||
78 | airspeedPID[axis].I = calcAirspeedPID(dynamicParams.gyroPID[axis].I); // Should this be??? |
||
79 | airspeedPID[axis].D = dynamicParams.gyroPID[axis].D; |
||
80 | } |
||
81 | } |
||
82 | |||
1910 | - | 83 | /************************************************************************/ |
84 | /* Main Flight Control */ |
||
85 | /************************************************************************/ |
||
86 | void flight_control(void) { |
||
2102 | - | 87 | // Mixer Fractions that are combined for Motor Control |
2103 | - | 88 | int16_t term[4]; |
2099 | - | 89 | |
2102 | - | 90 | // PID controller variables |
91 | int16_t PDPart[3]; |
||
1910 | - | 92 | |
2102 | - | 93 | static int8_t debugDataTimer = 1; |
1910 | - | 94 | |
2102 | - | 95 | // High resolution motor values for smoothing of PID motor outputs |
96 | // static int16_t outputFilters[MAX_OUTPUTS]; |
||
1910 | - | 97 | |
2102 | - | 98 | uint8_t axis; |
1910 | - | 99 | |
2104 | - | 100 | setAirspeedPIDs(); |
101 | |||
2103 | - | 102 | term[CONTROL_THROTTLE] = controls[CONTROL_THROTTLE]; |
1910 | - | 103 | |
2102 | - | 104 | // These params are just left the same in all modes. In MANUAL and RATE the results are ignored anyway. |
2122 | - | 105 | int32_t tmp; |
1910 | - | 106 | |
2122 | - | 107 | tmp = ((int32_t)controls[CONTROL_ELEVATOR] * staticParams.stickIElevator) >> LOG_STICK_SCALE; |
2142 | - | 108 | if (staticParams.servos[PITCH].reverse) target[PITCH] += tmp; else target[PITCH] -= tmp; |
2122 | - | 109 | |
110 | tmp = ((int32_t)controls[CONTROL_AILERONS] * staticParams.stickIAilerons) >> LOG_STICK_SCALE; |
||
2142 | - | 111 | if (staticParams.servos[ROLL].reverse) target[ROLL] += tmp; else target[ROLL] -= tmp; |
2122 | - | 112 | |
113 | tmp = ((int32_t)controls[CONTROL_RUDDER] * staticParams.stickIRudder) >> LOG_STICK_SCALE; |
||
2142 | - | 114 | if (staticParams.servos[YAW].reverse) target[YAW] += tmp; else target[YAW] -= tmp; |
2122 | - | 115 | |
2102 | - | 116 | for (axis = PITCH; axis <= YAW; axis++) { |
117 | if (target[axis] > OVER180) { |
||
118 | target[axis] -= OVER360; |
||
2103 | - | 119 | } else if (target[axis] <= -OVER180) { |
2104 | - | 120 | target[axis] += OVER360; |
2102 | - | 121 | } |
1910 | - | 122 | |
2135 | - | 123 | int32_t error = attitude[axis] - target[axis]; |
2129 | - | 124 | |
2131 | - | 125 | #define ROTATETARGET 1 |
126 | // #define TRUNCATEERROR 1 |
||
127 | |||
2132 | - | 128 | #ifdef ROTATETARGET |
2135 | - | 129 | //if(abs(error) > OVER180) { // doesnt work!!! |
130 | if(error > OVER180 || error < -OVER180) { |
||
2131 | - | 131 | // The shortest way from attitude to target crosses -180. |
132 | // 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. |
||
133 | // Or A is <0 and T is >0, that makes E a (too) large negative number. It should be wrapped to positive. |
||
2135 | - | 134 | if (error > 0) { |
135 | if (error < OVER360 - maxError[axis]) { |
||
2131 | - | 136 | // too much err. |
2135 | - | 137 | error = -maxError[axis]; |
2131 | - | 138 | target[axis] = attitude[axis] + maxError[axis]; |
2132 | - | 139 | if (target[axis] > OVER180) target[axis] -= OVER360; |
2131 | - | 140 | } else { |
141 | // Normal case, we just need to correct for the wrap. Error will be negative. |
||
2135 | - | 142 | error -= OVER360; |
2131 | - | 143 | } |
144 | } else { |
||
2135 | - | 145 | if (error > maxError[axis] - OVER360) { |
2131 | - | 146 | // too much err. |
2135 | - | 147 | error = maxError[axis]; |
2131 | - | 148 | target[axis] = attitude[axis] - maxError[axis]; |
2132 | - | 149 | if (target[axis] < -OVER180) target[axis] += OVER360; |
2131 | - | 150 | } else { |
151 | // Normal case, we just need to correct for the wrap. Error will be negative. |
||
2135 | - | 152 | error += OVER360; |
2131 | - | 153 | } |
154 | } |
||
155 | } else { |
||
156 | // Simple case, linear range. |
||
2135 | - | 157 | if (error > maxError[axis]) { |
158 | error = maxError[axis]; |
||
2131 | - | 159 | target[axis] = attitude[axis] - maxError[axis]; |
2135 | - | 160 | } else if (error < -maxError[axis]) { |
161 | error = -maxError[axis]; |
||
2131 | - | 162 | target[axis] = attitude[axis] + maxError[axis]; |
163 | } |
||
2129 | - | 164 | } |
2131 | - | 165 | #endif |
2132 | - | 166 | #ifdef TUNCATEERROR |
2135 | - | 167 | if (error > maxError[axis]) { |
168 | error = maxError[axis]; |
||
169 | } else if (error < -maxError[axis]) { |
||
170 | error = -maxError[axis]; |
||
2118 | - | 171 | } else { |
2129 | - | 172 | // update I parts here for angles mode. I parts in rate mode is something different. |
2102 | - | 173 | } |
2131 | - | 174 | #endif |
1910 | - | 175 | |
2135 | - | 176 | debugOut.analog[9+axis] = error / (GYRO_DEG_FACTOR / 10); // in 0.1 deg |
177 | |||
2102 | - | 178 | /************************************************************************/ |
179 | /* Calculate control feedback from angle (gyro integral) */ |
||
180 | /* and angular velocity (gyro signal) */ |
||
181 | /************************************************************************/ |
||
2119 | - | 182 | if (currentFlightMode == FLIGHT_MODE_ANGLES || currentFlightMode == FLIGHT_MODE_RATE) { |
2122 | - | 183 | PDPart[axis] = +(((int32_t) gyro_PID[axis] * (int32_t) airspeedPID[axis].P) >> LOG_P_SCALE) |
2119 | - | 184 | + (((int16_t)gyroD[axis] * (int16_t) airspeedPID[axis].D) >> LOG_D_SCALE); |
2104 | - | 185 | } else { |
186 | PDPart[axis] = 0; |
||
187 | } |
||
1910 | - | 188 | |
2104 | - | 189 | if (currentFlightMode == FLIGHT_MODE_ANGLES) { |
2135 | - | 190 | int16_t anglePart = (int32_t)(error * (int32_t) airspeedPID[axis].I) >> LOG_I_SCALE; |
2122 | - | 191 | PDPart[axis] += anglePart; |
2104 | - | 192 | } |
2118 | - | 193 | |
2102 | - | 194 | // Add I parts here... these are integrated errors. |
2142 | - | 195 | if (staticParams.servos[axis].reverse) |
2122 | - | 196 | term[axis] = controls[axis] - PDPart[axis]; // + IPart[axis]; |
197 | else |
||
198 | term[axis] = controls[axis] + PDPart[axis]; // + IPart[axis]; |
||
2102 | - | 199 | } |
1910 | - | 200 | |
2104 | - | 201 | for (uint8_t i = 0; i < NUM_CONTROL_SERVOS; i++) { |
2102 | - | 202 | int16_t tmp; |
203 | if (servoTestActive) { |
||
2119 | - | 204 | controlServos[i] = ((int16_t) servoTest[i] - 128) * 8; |
2102 | - | 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: |
||
2103 | - | 215 | tmp = term[THROTTLE]; |
2102 | - | 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 | } |
||
1910 | - | 227 | |
2103 | - | 228 | calculateControlServoValues(); |
1910 | - | 229 | |
2102 | - | 230 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
231 | // Debugging |
||
232 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
233 | if (!(--debugDataTimer)) { |
||
234 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
||
2103 | - | 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]; |
||
1910 | - | 238 | |
2102 | - | 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); |
||
2099 | - | 242 | |
2102 | - | 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 | |||
247 | debugOut.analog[12] = term[PITCH]; |
||
248 | debugOut.analog[13] = term[ROLL]; |
||
249 | debugOut.analog[14] = term[YAW]; |
||
2142 | - | 250 | debugOut.analog[15] = term[THROTTLE]; |
2102 | - | 251 | } |
1910 | - | 252 | } |