Rev 2053 | Rev 2057 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1612 | dongfang | 1 | #include <stdlib.h> |
2 | #include <avr/io.h> |
||
3 | #include "eeprom.h" |
||
4 | #include "flight.h" |
||
1845 | - | 5 | #include "output.h" |
2052 | - | 6 | #include "uart0.h" |
1612 | dongfang | 7 | |
8 | // Necessary for external control and motor test |
||
9 | #include "twimaster.h" |
||
10 | #include "attitude.h" |
||
11 | #include "controlMixer.h" |
||
1775 | - | 12 | #include "commands.h" |
2052 | - | 13 | #include "heightControl.h" |
1612 | dongfang | 14 | |
2052 | - | 15 | #ifdef USE_MK3MAG |
16 | #include "mk3mag.h" |
||
17 | #include "compassControl.h" |
||
18 | #endif |
||
19 | |||
1612 | dongfang | 20 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
21 | |||
22 | /* |
||
23 | * These are no longer maintained, just left at 0. The original implementation just summed the acc. |
||
24 | * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey??? |
||
25 | */ |
||
1645 | - | 26 | // int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
1872 | - | 27 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
1612 | dongfang | 28 | uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control |
2055 | - | 29 | uint8_t invKi; |
30 | int32_t IPart[2]; |
||
1612 | dongfang | 31 | |
32 | /************************************************************************/ |
||
33 | /* Filter for motor value smoothing (necessary???) */ |
||
34 | /************************************************************************/ |
||
35 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
||
1988 | - | 36 | switch (staticParams.motorSmoothing) { |
1841 | - | 37 | case 0: |
38 | return newvalue; |
||
39 | case 1: |
||
1872 | - | 40 | return (oldvalue + newvalue) / 2; |
1841 | - | 41 | case 2: |
1872 | - | 42 | if (newvalue > oldvalue) |
43 | return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new |
||
44 | else |
||
1841 | - | 45 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
46 | case 3: |
||
1872 | - | 47 | if (newvalue < oldvalue) |
48 | return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new |
||
49 | else |
||
1841 | - | 50 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
1872 | - | 51 | default: |
52 | return newvalue; |
||
1841 | - | 53 | } |
1612 | dongfang | 54 | } |
55 | |||
2055 | - | 56 | void flight_setParameters(uint8_t _invKi, uint8_t _gyroPFactor, |
1872 | - | 57 | uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
2053 | - | 58 | invKi = _invKi; |
1841 | - | 59 | gyroPFactor = _gyroPFactor; |
60 | gyroIFactor = _gyroIFactor; |
||
61 | yawPFactor = _yawPFactor; |
||
62 | yawIFactor = _yawIFactor; |
||
1612 | dongfang | 63 | } |
64 | |||
2055 | - | 65 | void flight_setGround() { |
66 | // Just reset all I terms. |
||
67 | IPart[PITCH] = IPart[ROLL] = 0; |
||
68 | headingError = 0; |
||
1612 | dongfang | 69 | } |
70 | |||
2055 | - | 71 | void flight_takeOff() { |
72 | HC_setGround(); |
||
73 | #ifdef USE_MK3MAG |
||
74 | attitude_resetHeadingToMagnetic(); |
||
75 | compass_setTakeoffHeading(heading); |
||
76 | #endif |
||
1612 | dongfang | 77 | } |
78 | |||
79 | /************************************************************************/ |
||
80 | /* Main Flight Control */ |
||
81 | /************************************************************************/ |
||
82 | void flight_control(void) { |
||
2055 | - | 83 | int16_t tmp_int; |
1872 | - | 84 | // Mixer Fractions that are combined for Motor Control |
1841 | - | 85 | int16_t yawTerm, throttleTerm, term[2]; |
1612 | dongfang | 86 | |
1841 | - | 87 | // PID controller variables |
2053 | - | 88 | int16_t PDPart; |
1841 | - | 89 | static int8_t debugDataTimer = 1; |
1612 | dongfang | 90 | |
1841 | - | 91 | // High resolution motor values for smoothing of PID motor outputs |
92 | static int16_t motorFilters[MAX_MOTORS]; |
||
1612 | dongfang | 93 | |
1841 | - | 94 | uint8_t i, axis; |
1612 | dongfang | 95 | |
1908 | - | 96 | throttleTerm = controls[CONTROL_THROTTLE]; |
1870 | - | 97 | |
2055 | - | 98 | if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
99 | // increment flight-time counter until overflow. |
||
100 | if (isFlying != 0xFFFF) |
||
101 | isFlying++; |
||
102 | } |
||
103 | /* |
||
104 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
||
105 | * Probably to avoid integration effects that will cause the copter to spin |
||
106 | * or flip when taking off. |
||
107 | */ |
||
108 | if (isFlying < 256) { |
||
109 | flight_setGround(); |
||
110 | if (isFlying == 250) |
||
111 | flight_takeOff(); |
||
112 | } |
||
113 | |||
1841 | - | 114 | // This check removed. Is done on a per-motor basis, after output matrix multiplication. |
1960 | - | 115 | if (throttleTerm < staticParams.minThrottle + 10) |
116 | throttleTerm = staticParams.minThrottle + 10; |
||
117 | else if (throttleTerm > staticParams.maxThrottle - 20) |
||
118 | throttleTerm = (staticParams.maxThrottle - 20); |
||
1612 | dongfang | 119 | |
2055 | - | 120 | // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already? |
121 | throttleTerm *= CONTROL_SCALING; |
||
122 | // TODO: We dont need to repeat this for every iteration! |
||
1775 | - | 123 | |
2055 | - | 124 | // end part 1: 750-800 usec. |
125 | // start part 3: 350 - 400 usec. |
||
2051 | - | 126 | #define YAW_I_LIMIT (45L * GYRO_DEG_FACTOR_YAW) |
2055 | - | 127 | // This is where control affects the target heading. It also (later) directly controls yaw. |
2051 | - | 128 | headingError -= controls[CONTROL_YAW]; |
2055 | - | 129 | if (headingError < -YAW_I_LIMIT) |
130 | headingError = -YAW_I_LIMIT; |
||
131 | if (headingError > YAW_I_LIMIT) |
||
132 | headingError = YAW_I_LIMIT; |
||
2048 | - | 133 | |
2055 | - | 134 | PDPart = (int32_t) (headingError * yawIFactor) / (GYRO_DEG_FACTOR_YAW << 4); |
135 | // Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on. |
||
136 | PDPart += (int32_t) (yawRate * yawPFactor) / (GYRO_DEG_FACTOR_YAW >> 5); |
||
1872 | - | 137 | |
2055 | - | 138 | // Lets not limit P and D. |
139 | // CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
||
1612 | dongfang | 140 | |
1841 | - | 141 | /* |
142 | * Compose yaw term. |
||
143 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
||
144 | * However, at low throttle the yaw term is limited to a fixed value, |
||
145 | * and at high throttle it is limited by the throttle reserve (the difference |
||
146 | * between current throttle and maximum throttle). |
||
147 | */ |
||
1645 | - | 148 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
2053 | - | 149 | yawTerm = PDPart - controls[CONTROL_YAW] * CONTROL_SCALING; |
2055 | - | 150 | // Limit yawTerm |
1955 | - | 151 | debugOut.digital[0] &= ~DEBUG_CLIP; |
1872 | - | 152 | if (throttleTerm > MIN_YAWGAS) { |
153 | if (yawTerm < -throttleTerm / 2) { |
||
1955 | - | 154 | debugOut.digital[0] |= DEBUG_CLIP; |
1872 | - | 155 | yawTerm = -throttleTerm / 2; |
156 | } else if (yawTerm > throttleTerm / 2) { |
||
1955 | - | 157 | debugOut.digital[0] |= DEBUG_CLIP; |
1872 | - | 158 | yawTerm = throttleTerm / 2; |
1841 | - | 159 | } |
160 | } else { |
||
1872 | - | 161 | if (yawTerm < -MIN_YAWGAS / 2) { |
1955 | - | 162 | debugOut.digital[0] |= DEBUG_CLIP; |
1872 | - | 163 | yawTerm = -MIN_YAWGAS / 2; |
164 | } else if (yawTerm > MIN_YAWGAS / 2) { |
||
1955 | - | 165 | debugOut.digital[0] |= DEBUG_CLIP; |
1872 | - | 166 | yawTerm = MIN_YAWGAS / 2; |
1841 | - | 167 | } |
168 | } |
||
1775 | - | 169 | |
1960 | - | 170 | tmp_int = staticParams.maxThrottle * CONTROL_SCALING; |
2055 | - | 171 | |
1845 | - | 172 | if (yawTerm < -(tmp_int - throttleTerm)) { |
173 | yawTerm = -(tmp_int - throttleTerm); |
||
1955 | - | 174 | debugOut.digital[0] |= DEBUG_CLIP; |
1845 | - | 175 | } else if (yawTerm > (tmp_int - throttleTerm)) { |
176 | yawTerm = (tmp_int - throttleTerm); |
||
1955 | - | 177 | debugOut.digital[0] |= DEBUG_CLIP; |
1841 | - | 178 | } |
1867 | - | 179 | |
1955 | - | 180 | debugOut.digital[1] &= ~DEBUG_CLIP; |
2053 | - | 181 | |
2055 | - | 182 | tmp_int = ((uint16_t)dynamicParams.dynamicStability * ((uint16_t)throttleTerm + (abs(yawTerm) >> 1)) >> 6); |
183 | //tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64; |
||
2053 | - | 184 | |
185 | /************************************************************************/ |
||
186 | /* Calculate control feedback from angle (gyro integral) */ |
||
187 | /* and angular velocity (gyro signal) */ |
||
188 | /************************************************************************/ |
||
189 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
||
1872 | - | 190 | for (axis = PITCH; axis <= ROLL; axis++) { |
2055 | - | 191 | PDPart = (int32_t) rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 4); |
2053 | - | 192 | PDPart += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16; |
193 | // In acc. mode the I part is summed only from the attitude (IFaktor) angle minus stick. |
||
194 | // In HH mode, the I part is summed from P and D of gyros minus stick. |
||
1872 | - | 195 | if (gyroIFactor) { |
2055 | - | 196 | int16_t iDiff = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 3); |
197 | PDPart += iDiff; |
||
2053 | - | 198 | IPart[axis] += iDiff - controls[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
1841 | - | 199 | } else { |
2053 | - | 200 | IPart[axis] += PDPart - controls[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
1841 | - | 201 | } |
1612 | dongfang | 202 | |
2055 | - | 203 | // With normal Ki, limit I parts to +/- 205 (of about 1024) |
2053 | - | 204 | if (IPart[axis] < -64000) { |
205 | IPart[axis] = -64000; |
||
2052 | - | 206 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
2053 | - | 207 | } else if (IPart[axis] > 64000) { |
208 | IPart[axis] = 64000; |
||
2052 | - | 209 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
210 | } |
||
211 | |||
2055 | - | 212 | term[axis] = PDPart - controls[axis] + (((int32_t) IPart[axis] * invKi) >> 14); |
213 | term[axis] += (dynamicParams.levelCorrection[axis] - 128); |
||
214 | |||
215 | /* |
||
1841 | - | 216 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
217 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
||
218 | * (max. pitch or roll term is the throttle value). |
||
219 | * TODO: Why a growing function of yaw? |
||
220 | */ |
||
221 | if (term[axis] < -tmp_int) { |
||
1955 | - | 222 | debugOut.digital[1] |= DEBUG_CLIP; |
2055 | - | 223 | term[axis] = -tmp_int; |
1841 | - | 224 | } else if (term[axis] > tmp_int) { |
1955 | - | 225 | debugOut.digital[1] |= DEBUG_CLIP; |
2055 | - | 226 | term[axis] = tmp_int; |
1841 | - | 227 | } |
228 | } |
||
1775 | - | 229 | |
1841 | - | 230 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
231 | // Universal Mixer |
||
232 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
||
233 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1612 | dongfang | 234 | |
2055 | - | 235 | if (!(--debugDataTimer)) { |
236 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
||
237 | debugOut.analog[0] = attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg |
||
238 | debugOut.analog[1] = attitude[ROLL] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg |
||
239 | debugOut.analog[2] = heading / GYRO_DEG_FACTOR_YAW; |
||
1976 | - | 240 | |
2055 | - | 241 | debugOut.analog[3] = rate_ATT[PITCH]; |
242 | debugOut.analog[4] = rate_ATT[ROLL]; |
||
243 | debugOut.analog[5] = yawRate; |
||
244 | } |
||
1976 | - | 245 | |
2055 | - | 246 | debugOut.analog[8] = yawTerm; |
247 | debugOut.analog[9] = throttleTerm; |
||
1775 | - | 248 | |
2055 | - | 249 | debugOut.analog[16] = gyroActivity; |
250 | |||
1872 | - | 251 | for (i = 0; i < MAX_MOTORS; i++) { |
1874 | - | 252 | int32_t tmp; |
1908 | - | 253 | uint8_t throttle; |
254 | |||
2055 | - | 255 | tmp = (int32_t) throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE]; |
256 | tmp += (int32_t) term[PITCH] * mixerMatrix.motor[i][MIX_PITCH]; |
||
257 | tmp += (int32_t) term[ROLL] * mixerMatrix.motor[i][MIX_ROLL]; |
||
258 | tmp += (int32_t) yawTerm * mixerMatrix.motor[i][MIX_YAW]; |
||
1908 | - | 259 | tmp = tmp >> 6; |
260 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
||
261 | // Now we scale back down to a 0..255 range. |
||
262 | tmp = motorFilters[i] / MOTOR_SCALING; |
||
263 | |||
264 | // So this was the THIRD time a throttle was limited. But should the limitation |
||
265 | // apply to the common throttle signal (the one used for setting the "power" of |
||
266 | // all motors together) or should it limit the throttle set for each motor, |
||
267 | // including mix components of pitch, roll and yaw? I think only the common |
||
268 | // throttle should be limited. |
||
269 | // --> WRONG. This caused motors to stall completely in tight maneuvers. |
||
270 | // Apply to individual signals instead. |
||
271 | CHECK_MIN_MAX(tmp, 1, 255); |
||
272 | throttle = tmp; |
||
273 | |||
2055 | - | 274 | if (i < 4) |
275 | debugOut.analog[10 + i] = throttle; |
||
1908 | - | 276 | |
1960 | - | 277 | if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) { |
2035 | - | 278 | motor[i].throttle = throttle; |
1872 | - | 279 | } else if (motorTestActive) { |
2035 | - | 280 | motor[i].throttle = motorTest[i]; |
1841 | - | 281 | } else { |
2035 | - | 282 | motor[i].throttle = 0; |
1841 | - | 283 | } |
284 | } |
||
1872 | - | 285 | |
1841 | - | 286 | I2C_Start(TWI_STATE_MOTOR_TX); |
1612 | dongfang | 287 | } |