Rev 1956 | Rev 1964 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1956 | Rev 1960 | ||
---|---|---|---|
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
2 | // + Copyright (c) 04.2007 Holger Buss |
3 | // + Nur für den privaten Gebrauch |
3 | // + Nur für den privaten Gebrauch |
4 | // + www.MikroKopter.com |
4 | // + www.MikroKopter.com |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
6 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
7 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
7 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
11 | // + Verkauf von Luftbildaufnahmen, usw. |
11 | // + Verkauf von Luftbildaufnahmen, usw. |
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
17 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
17 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
18 | // + eindeutig als Ursprung verlinkt werden |
18 | // + eindeutig als Ursprung verlinkt werden |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
21 | // + Benutzung auf eigene Gefahr |
21 | // + Benutzung auf eigene Gefahr |
22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
25 | // + mit unserer Zustimmung zulässig |
25 | // + mit unserer Zustimmung zulässig |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
30 | // + this list of conditions and the following disclaimer. |
30 | // + this list of conditions and the following disclaimer. |
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
32 | // + from this software without specific prior written permission. |
32 | // + from this software without specific prior written permission. |
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
34 | // + for non-commercial use (directly or indirectly) |
34 | // + for non-commercial use (directly or indirectly) |
35 | // + Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
35 | // + Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
36 | // + with our written permission |
36 | // + with our written permission |
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
38 | // + clearly linked as origin |
38 | // + clearly linked as origin |
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
49 | // + POSSIBILITY OF SUCH DAMAGE. |
49 | // + POSSIBILITY OF SUCH DAMAGE. |
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
51 | 51 | ||
52 | #include <stdlib.h> |
52 | #include <stdlib.h> |
53 | #include <avr/io.h> |
53 | #include <avr/io.h> |
54 | #include "eeprom.h" |
54 | #include "eeprom.h" |
55 | #include "flight.h" |
55 | #include "flight.h" |
56 | #include "output.h" |
56 | #include "output.h" |
57 | 57 | ||
58 | // Only for debug. Remove. |
58 | // Only for debug. Remove. |
59 | //#include "analog.h" |
59 | //#include "analog.h" |
60 | //#include "rc.h" |
60 | //#include "rc.h" |
61 | 61 | ||
62 | // Necessary for external control and motor test |
62 | // Necessary for external control and motor test |
63 | #include "uart0.h" |
63 | #include "uart0.h" |
64 | - | ||
65 | // for scope debugging |
- | |
66 | // #include "rc.h" |
- | |
67 | - | ||
68 | #include "twimaster.h" |
64 | #include "twimaster.h" |
69 | #include "attitude.h" |
65 | #include "attitude.h" |
70 | #include "controlMixer.h" |
66 | #include "controlMixer.h" |
71 | #include "commands.h" |
67 | #include "commands.h" |
72 | #ifdef USE_MK3MAG |
68 | #ifdef USE_MK3MAG |
73 | #include "gps.h" |
69 | #include "gps.h" |
74 | #endif |
70 | #endif |
75 | 71 | ||
76 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
72 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
77 | 73 | ||
78 | /* |
74 | /* |
79 | * These are no longer maintained, just left at 0. The original implementation just summed the acc. |
75 | * These are no longer maintained, just left at 0. The original implementation just summed the acc. |
80 | * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey??? |
76 | * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey??? |
81 | */ |
77 | */ |
82 | // int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
78 | // int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
83 | 79 | ||
84 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
80 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
85 | uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control |
81 | uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control |
86 | 82 | ||
87 | // Some integral weight constant... |
83 | // Some integral weight constant... |
88 | uint16_t Ki = 10300 / 33; |
84 | uint16_t Ki = 10300 / 33; |
89 | uint8_t RequiredMotors = 0; |
- | |
90 | 85 | ||
91 | /************************************************************************/ |
86 | /************************************************************************/ |
92 | /* Filter for motor value smoothing (necessary???) */ |
87 | /* Filter for motor value smoothing (necessary???) */ |
93 | /************************************************************************/ |
88 | /************************************************************************/ |
94 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
89 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
95 | switch (dynamicParams.UserParams[5]) { |
90 | switch (dynamicParams.motorSmoothing) { |
96 | case 0: |
91 | case 0: |
97 | return newvalue; |
92 | return newvalue; |
98 | case 1: |
93 | case 1: |
99 | return (oldvalue + newvalue) / 2; |
94 | return (oldvalue + newvalue) / 2; |
100 | case 2: |
95 | case 2: |
101 | if (newvalue > oldvalue) |
96 | if (newvalue > oldvalue) |
102 | return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new |
97 | return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new |
103 | else |
98 | else |
104 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
99 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
105 | case 3: |
100 | case 3: |
106 | if (newvalue < oldvalue) |
101 | if (newvalue < oldvalue) |
107 | return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new |
102 | return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new |
108 | else |
103 | else |
109 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
104 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
110 | default: |
105 | default: |
111 | return newvalue; |
106 | return newvalue; |
112 | } |
107 | } |
113 | } |
108 | } |
114 | 109 | ||
115 | /************************************************************************/ |
110 | /************************************************************************/ |
116 | /* Neutral Readings */ |
111 | /* Neutral Readings */ |
117 | /************************************************************************/ |
112 | /************************************************************************/ |
118 | void flight_setNeutral() { |
113 | void flight_setNeutral() { |
119 | MKFlags |= MKFLAG_CALIBRATE; |
114 | MKFlags |= MKFLAG_CALIBRATE; |
120 | // not really used here any more. |
115 | // not really used here any more. |
- | 116 | /* |
|
121 | dynamicParams.KalmanK = -1; |
117 | dynamicParams.KalmanK = -1; |
122 | dynamicParams.KalmanMaxDrift = 0; |
118 | dynamicParams.KalmanMaxDrift = 0; |
123 | dynamicParams.KalmanMaxFusion = 32; |
119 | dynamicParams.KalmanMaxFusion = 32; |
- | 120 | */ |
|
124 | controlMixer_initVariables(); |
121 | controlMixer_initVariables(); |
125 | } |
122 | } |
126 | 123 | ||
127 | void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor, |
124 | void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor, |
128 | uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
125 | uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
129 | Ki = 10300 / _Ki; |
126 | Ki = 10300 / _Ki; |
130 | gyroPFactor = _gyroPFactor; |
127 | gyroPFactor = _gyroPFactor; |
131 | gyroIFactor = _gyroIFactor; |
128 | gyroIFactor = _gyroIFactor; |
132 | yawPFactor = _yawPFactor; |
129 | yawPFactor = _yawPFactor; |
133 | yawIFactor = _yawIFactor; |
130 | yawIFactor = _yawIFactor; |
134 | } |
131 | } |
135 | 132 | ||
136 | void setNormalFlightParameters(void) { |
133 | void setNormalFlightParameters(void) { |
137 | setFlightParameters( |
134 | setFlightParameters( |
138 | dynamicParams.IFactor, |
135 | dynamicParams.IFactor, |
139 | dynamicParams.GyroP, |
136 | dynamicParams.gyroP, |
140 | staticParams.GlobalConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.GyroI, |
137 | staticParams.bitConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.gyroI, |
141 | dynamicParams.GyroP, |
138 | dynamicParams.gyroP, |
142 | dynamicParams.UserParams[6] |
139 | dynamicParams.yawIFactor |
143 | ); |
140 | ); |
144 | } |
141 | } |
145 | 142 | ||
146 | void setStableFlightParameters(void) { |
143 | void setStableFlightParameters(void) { |
147 | setFlightParameters(33, 90, 120, 90, 120); |
144 | setFlightParameters(33, 90, 120, 90, 120); |
148 | } |
145 | } |
149 | 146 | ||
150 | /************************************************************************/ |
147 | /************************************************************************/ |
151 | /* Main Flight Control */ |
148 | /* Main Flight Control */ |
152 | /************************************************************************/ |
149 | /************************************************************************/ |
153 | void flight_control(void) { |
150 | void flight_control(void) { |
154 | int16_t tmp_int; |
151 | int16_t tmp_int; |
155 | // Mixer Fractions that are combined for Motor Control |
152 | // Mixer Fractions that are combined for Motor Control |
156 | int16_t yawTerm, throttleTerm, term[2]; |
153 | int16_t yawTerm, throttleTerm, term[2]; |
157 | 154 | ||
158 | // PID controller variables |
155 | // PID controller variables |
159 | int16_t PDPart[2], PDPartYaw, PPart[2]; |
156 | int16_t PDPart[2], PDPartYaw, PPart[2]; |
160 | static int32_t IPart[2] = { 0, 0 }; |
157 | static int32_t IPart[2] = { 0, 0 }; |
161 | // static int32_t yawControlRate = 0; |
158 | // static int32_t yawControlRate = 0; |
162 | 159 | ||
163 | // Removed. Too complicated, and apparently not necessary with MEMS gyros anyway. |
160 | // Removed. Too complicated, and apparently not necessary with MEMS gyros anyway. |
164 | // static int32_t IntegralGyroPitchError = 0, IntegralGyroRollError = 0; |
161 | // static int32_t IntegralGyroPitchError = 0, IntegralGyroRollError = 0; |
165 | // static int32_t CorrectionPitch, CorrectionRoll; |
162 | // static int32_t CorrectionPitch, CorrectionRoll; |
166 | 163 | ||
167 | static uint16_t emergencyFlightTime; |
164 | static uint16_t emergencyFlightTime; |
168 | static int8_t debugDataTimer = 1; |
165 | static int8_t debugDataTimer = 1; |
169 | 166 | ||
170 | // High resolution motor values for smoothing of PID motor outputs |
167 | // High resolution motor values for smoothing of PID motor outputs |
171 | static int16_t motorFilters[MAX_MOTORS]; |
168 | static int16_t motorFilters[MAX_MOTORS]; |
172 | 169 | ||
173 | uint8_t i, axis; |
170 | uint8_t i, axis; |
174 | 171 | ||
175 | // Fire the main flight attitude calculation, including integration of angles. |
172 | // Fire the main flight attitude calculation, including integration of angles. |
176 | // We want that to kick as early as possible, not to delay new AD sampling further. |
173 | // We want that to kick as early as possible, not to delay new AD sampling further. |
177 | calculateFlightAttitude(); |
174 | calculateFlightAttitude(); |
178 | controlMixer_update(); |
175 | controlMixer_update(); |
179 | throttleTerm = controls[CONTROL_THROTTLE]; |
176 | throttleTerm = controls[CONTROL_THROTTLE]; |
180 | 177 | ||
181 | // This check removed. Is done on a per-motor basis, after output matrix multiplication. |
178 | // This check removed. Is done on a per-motor basis, after output matrix multiplication. |
182 | if (throttleTerm < staticParams.MinThrottle + 10) |
179 | if (throttleTerm < staticParams.minThrottle + 10) |
183 | throttleTerm = staticParams.MinThrottle + 10; |
180 | throttleTerm = staticParams.minThrottle + 10; |
184 | else if (throttleTerm > staticParams.MaxThrottle - 20) |
181 | else if (throttleTerm > staticParams.maxThrottle - 20) |
185 | throttleTerm = (staticParams.MaxThrottle - 20); |
182 | throttleTerm = (staticParams.maxThrottle - 20); |
186 | 183 | ||
187 | /************************************************************************/ |
184 | /************************************************************************/ |
188 | /* RC-signal is bad */ |
185 | /* RC-signal is bad */ |
189 | /* This part could be abstracted, as having yet another control input */ |
186 | /* This part could be abstracted, as having yet another control input */ |
190 | /* to the control mixer: An emergency autopilot control. */ |
187 | /* to the control mixer: An emergency autopilot control. */ |
191 | /************************************************************************/ |
188 | /************************************************************************/ |
192 | 189 | ||
193 | if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy |
190 | if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy |
194 | RED_ON; |
191 | RED_ON; |
195 | beepRCAlarm(); |
192 | beepRCAlarm(); |
196 | 193 | ||
197 | if (emergencyFlightTime) { |
194 | if (emergencyFlightTime) { |
198 | // continue emergency flight |
195 | // continue emergency flight |
199 | emergencyFlightTime--; |
196 | emergencyFlightTime--; |
200 | if (isFlying > 256) { |
197 | if (isFlying > 256) { |
201 | // We're probably still flying. Descend slowly. |
198 | // We're probably still flying. Descend slowly. |
202 | throttleTerm = staticParams.EmergencyGas; // Set emergency throttle |
199 | throttleTerm = staticParams.emergencyThrottle; // Set emergency throttle |
203 | MKFlags |= (MKFLAG_EMERGENCY_LANDING); // Set flag for emergency landing |
200 | MKFlags |= (MKFLAG_EMERGENCY_FLIGHT); // Set flag for emergency landing |
204 | setStableFlightParameters(); |
201 | setStableFlightParameters(); |
205 | } else { |
202 | } else { |
206 | MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors. |
203 | MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors. |
207 | } |
204 | } |
208 | } else { |
205 | } else { |
209 | // end emergency flight (just cut the motors???) |
206 | // end emergency flight (just cut the motors???) |
210 | MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_LANDING); |
207 | MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_FLIGHT); |
211 | } |
208 | } |
212 | } else { |
209 | } else { |
213 | // signal is acceptable |
210 | // signal is acceptable |
214 | if (controlMixer_getSignalQuality() > SIGNAL_BAD) { |
211 | if (controlMixer_getSignalQuality() > SIGNAL_BAD) { |
215 | // Reset emergency landing control variables. |
212 | // Reset emergency landing control variables. |
216 | MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing |
213 | MKFlags &= ~(MKFLAG_EMERGENCY_FLIGHT); // clear flag for emergency landing |
217 | // The time is in whole seconds. |
214 | // The time is in whole seconds. |
218 | emergencyFlightTime = (uint16_t) staticParams.EmergencyGasDuration * 488; |
215 | emergencyFlightTime = (uint16_t) staticParams.emergencyFlightDuration * 488; |
219 | } |
216 | } |
220 | 217 | ||
221 | // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying. |
218 | // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying. |
222 | if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
219 | if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
223 | // increment flight-time counter until overflow. |
220 | // increment flight-time counter until overflow. |
224 | if (isFlying != 0xFFFF) |
221 | if (isFlying != 0xFFFF) |
225 | isFlying++; |
222 | isFlying++; |
226 | } else |
223 | } else |
227 | /* |
224 | /* |
228 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
225 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
229 | * Probably to avoid integration effects that will cause the copter to spin |
226 | * Probably to avoid integration effects that will cause the copter to spin |
230 | * or flip when taking off. |
227 | * or flip when taking off. |
231 | */ |
228 | */ |
232 | if (isFlying < 256) { |
229 | if (isFlying < 256) { |
233 | IPart[PITCH] = IPart[ROLL] = 0; |
230 | IPart[PITCH] = IPart[ROLL] = 0; |
234 | // TODO: Don't stomp on other modules' variables!!! |
231 | // TODO: Don't stomp on other modules' variables!!! |
235 | // controlYaw = 0; |
232 | // controlYaw = 0; |
236 | PDPartYaw = 0; // instead. |
233 | PDPartYaw = 0; // instead. |
237 | if (isFlying == 250) { |
234 | if (isFlying == 250) { |
238 | // HC_setGround(); |
235 | // HC_setGround(); |
239 | updateCompassCourse = 1; |
236 | updateCompassCourse = 1; |
240 | yawAngleDiff = 0; |
237 | yawAngleDiff = 0; |
- | 238 | } |
|
- | 239 | } else { |
|
- | 240 | // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag? |
|
- | 241 | // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe. |
|
- | 242 | MKFlags |= (MKFLAG_FLY); |
|
241 | } |
243 | } |
242 | } else { |
- | |
243 | // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag? |
- | |
244 | // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe. |
- | |
245 | MKFlags |= (MKFLAG_FLY); |
- | |
246 | } |
- | |
247 | 244 | ||
248 | commands_handleCommands(); |
245 | commands_handleCommands(); |
249 | - | ||
250 | // if(controlMixer_getSignalQuality() >= SIGNAL_GOOD) { |
- | |
251 | setNormalFlightParameters(); |
246 | setNormalFlightParameters(); |
252 | // } |
- | |
253 | } // end else (not bad signal case) |
247 | } // end else (not bad signal case) |
254 | // end part1a: 750-800 usec. |
- | |
255 | /* |
- | |
256 | * Looping the H&I way basically is just a matter of turning off attitude angle measurement |
- | |
257 | * by integration (because 300 deg/s gyros are too slow) and turning down the throttle. |
- | |
258 | * This is the throttle part. |
- | |
259 | */ |
- | |
260 | if (looping) { |
- | |
261 | if (throttleTerm > staticParams.LoopGasLimit) |
- | |
262 | throttleTerm = staticParams.LoopGasLimit; |
- | |
263 | } |
- | |
264 | 248 | ||
265 | /************************************************************************/ |
249 | /************************************************************************/ |
266 | /* Yawing */ |
250 | /* Yawing */ |
267 | /************************************************************************/ |
251 | /************************************************************************/ |
268 | if (abs(controls[CONTROL_YAW]) > 4 * staticParams.StickYawP) { // yaw stick is activated |
252 | if (abs(controls[CONTROL_YAW]) > 4 * staticParams.stickYawP) { // yaw stick is activated |
269 | ignoreCompassTimer = 1000; |
253 | ignoreCompassTimer = 1000; |
270 | if (!(staticParams.GlobalConfig & CFG_COMPASS_FIX)) { |
254 | if (!(staticParams.bitConfig & CFG_COMPASS_FIX)) { |
271 | updateCompassCourse = 1; |
255 | updateCompassCourse = 1; |
272 | } |
256 | } |
273 | } |
257 | } |
274 | 258 | ||
275 | // yawControlRate = controlYaw; |
- | |
276 | 259 | // yawControlRate = controlYaw; |
|
277 | // Trim drift of yawAngleDiff with controlYaw. |
260 | // Trim drift of yawAngleDiff with controlYaw. |
278 | // TODO: We want NO feedback of control related stuff to the attitude related stuff. |
261 | // TODO: We want NO feedback of control related stuff to the attitude related stuff. |
279 | // This seems to be used as: Difference desired <--> real heading. |
262 | // This seems to be used as: Difference desired <--> real heading. |
280 | yawAngleDiff -= controls[CONTROL_YAW]; |
263 | yawAngleDiff -= controls[CONTROL_YAW]; |
281 | 264 | ||
282 | // limit the effect |
265 | // limit the effect |
283 | CHECK_MIN_MAX(yawAngleDiff, -50000, 50000); |
266 | CHECK_MIN_MAX(yawAngleDiff, -50000, 50000); |
284 | 267 | ||
285 | /************************************************************************/ |
268 | /************************************************************************/ |
286 | /* Compass is currently not supported. */ |
269 | /* Compass is currently not supported. */ |
287 | /************************************************************************/ |
270 | /************************************************************************/ |
288 | if (staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) { |
271 | if (staticParams.bitConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) { |
289 | updateCompass(); |
272 | updateCompass(); |
290 | } |
273 | } |
291 | 274 | ||
292 | #if defined (USE_NAVICTRL) |
275 | #if defined (USE_NAVICTRL) |
293 | /************************************************************************/ |
276 | /************************************************************************/ |
294 | /* GPS is currently not supported. */ |
277 | /* GPS is currently not supported. */ |
295 | /************************************************************************/ |
278 | /************************************************************************/ |
296 | if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) { |
279 | if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) { |
297 | GPS_Main(); |
280 | GPS_Main(); |
298 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
281 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
299 | } else { |
282 | } else { |
300 | } |
283 | } |
301 | #endif |
284 | #endif |
302 | // end part 1: 750-800 usec. |
285 | // end part 1: 750-800 usec. |
303 | // start part 3: 350 - 400 usec. |
286 | // start part 3: 350 - 400 usec. |
304 | #define SENSOR_LIMIT (4096 * 4) |
287 | #define SENSOR_LIMIT (4096 * 4) |
305 | /************************************************************************/ |
288 | /************************************************************************/ |
306 | 289 | ||
307 | /* Calculate control feedback from angle (gyro integral) */ |
290 | /* Calculate control feedback from angle (gyro integral) */ |
308 | /* and angular velocity (gyro signal) */ |
291 | /* and angular velocity (gyro signal) */ |
309 | /************************************************************************/ |
292 | /************************************************************************/ |
310 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
293 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
311 | 294 | ||
312 | for (axis = PITCH; axis <= ROLL; axis++) { |
295 | for (axis = PITCH; axis <= ROLL; axis++) { |
313 | if (looping & ((1 << 4) << axis)) { |
- | |
314 | PPart[axis] = 0; |
- | |
315 | } else { // TODO: Where do the 44000 come from??? |
- | |
316 | PPart[axis] = angle[axis] * gyroIFactor / (44000 / CONTROL_SCALING); // P-Part - Proportional to Integral |
296 | PPart[axis] = angle[axis] * gyroIFactor / (44000 / CONTROL_SCALING); // P-Part - Proportional to Integral |
317 | } |
- | |
318 | 297 | ||
319 | /* |
298 | /* |
320 | * Now blend in the D-part - proportional to the Differential of the integral = the rate. |
299 | * Now blend in the D-part - proportional to the Differential of the integral = the rate. |
321 | * Read this as: PDPart = PPart + rate_PID * pfactor * CONTROL_SCALING |
300 | * Read this as: PDPart = PPart + rate_PID * pfactor * CONTROL_SCALING |
322 | * where pfactor is in [0..1]. |
301 | * where pfactor is in [0..1]. |
323 | */ |
302 | */ |
324 | PDPart[axis] = PPart[axis] + (int32_t) ((int32_t) rate_PID[axis] |
- | |
325 | * gyroPFactor / (256L / CONTROL_SCALING)) + (differential[axis] |
303 | PDPart[axis] = PPart[axis] + (int32_t) ((int32_t) rate_PID[axis] * gyroPFactor / (256L / CONTROL_SCALING)) + (differential[axis] |
326 | * (int16_t) dynamicParams.GyroD) / 16; |
304 | * (int16_t) dynamicParams.gyroD) / 16; |
327 | 305 | ||
328 | CHECK_MIN_MAX(PDPart[axis], -SENSOR_LIMIT, SENSOR_LIMIT); |
306 | CHECK_MIN_MAX(PDPart[axis], -SENSOR_LIMIT, SENSOR_LIMIT); |
329 | } |
307 | } |
330 | 308 | ||
331 | PDPartYaw = (int32_t) (yawRate * 2 * (int32_t) yawPFactor) / (256L |
309 | PDPartYaw = (int32_t) (yawRate * 2 * (int32_t) yawPFactor) / (256L |
332 | / CONTROL_SCALING) + (int32_t) (yawAngleDiff * yawIFactor) / (2 * (44000 |
310 | / CONTROL_SCALING) + (int32_t) (yawAngleDiff * yawIFactor) / (2 * (44000 |
333 | / CONTROL_SCALING)); |
311 | / CONTROL_SCALING)); |
334 | 312 | ||
335 | // limit control feedback |
313 | // limit control feedback |
336 | CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
314 | CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
337 | 315 | ||
338 | /* |
316 | /* |
339 | * Compose throttle term. |
317 | * Compose throttle term. |
340 | * If a Bl-Ctrl is missing, prevent takeoff. |
318 | * If a Bl-Ctrl is missing, prevent takeoff. |
341 | */ |
319 | */ |
342 | if (missingMotor) { |
320 | if (missingMotor) { |
343 | // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway??? |
321 | // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway??? |
344 | if (isFlying > 1 && isFlying < 50 && throttleTerm > 0) |
322 | if (isFlying > 1 && isFlying < 50 && throttleTerm > 0) |
345 | isFlying = 1; // keep within lift off condition |
323 | isFlying = 1; // keep within lift off condition |
346 | throttleTerm = staticParams.MinThrottle; // reduce gas to min to avoid lift of |
324 | throttleTerm = staticParams.minThrottle; // reduce gas to min to avoid lift of |
347 | } |
325 | } |
348 | 326 | ||
349 | // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already? |
327 | // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already? |
350 | throttleTerm *= CONTROL_SCALING; |
328 | throttleTerm *= CONTROL_SCALING; |
351 | 329 | ||
352 | /* |
330 | /* |
353 | * Compose yaw term. |
331 | * Compose yaw term. |
354 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
332 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
355 | * However, at low throttle the yaw term is limited to a fixed value, |
333 | * However, at low throttle the yaw term is limited to a fixed value, |
356 | * and at high throttle it is limited by the throttle reserve (the difference |
334 | * and at high throttle it is limited by the throttle reserve (the difference |
357 | * between current throttle and maximum throttle). |
335 | * between current throttle and maximum throttle). |
358 | */ |
336 | */ |
359 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
337 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
360 | yawTerm = PDPartYaw - controls[CONTROL_YAW] * CONTROL_SCALING; |
338 | yawTerm = PDPartYaw - controls[CONTROL_YAW] * CONTROL_SCALING; |
361 | // Limit yawTerm |
339 | // Limit yawTerm |
362 | debugOut.digital[0] &= ~DEBUG_CLIP; |
340 | debugOut.digital[0] &= ~DEBUG_CLIP; |
363 | if (throttleTerm > MIN_YAWGAS) { |
341 | if (throttleTerm > MIN_YAWGAS) { |
364 | if (yawTerm < -throttleTerm / 2) { |
342 | if (yawTerm < -throttleTerm / 2) { |
365 | debugOut.digital[0] |= DEBUG_CLIP; |
343 | debugOut.digital[0] |= DEBUG_CLIP; |
366 | yawTerm = -throttleTerm / 2; |
344 | yawTerm = -throttleTerm / 2; |
367 | } else if (yawTerm > throttleTerm / 2) { |
345 | } else if (yawTerm > throttleTerm / 2) { |
368 | debugOut.digital[0] |= DEBUG_CLIP; |
346 | debugOut.digital[0] |= DEBUG_CLIP; |
369 | yawTerm = throttleTerm / 2; |
347 | yawTerm = throttleTerm / 2; |
370 | } |
348 | } |
371 | //CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2)); |
349 | //CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2)); |
372 | } else { |
350 | } else { |
373 | if (yawTerm < -MIN_YAWGAS / 2) { |
351 | if (yawTerm < -MIN_YAWGAS / 2) { |
374 | debugOut.digital[0] |= DEBUG_CLIP; |
352 | debugOut.digital[0] |= DEBUG_CLIP; |
375 | yawTerm = -MIN_YAWGAS / 2; |
353 | yawTerm = -MIN_YAWGAS / 2; |
376 | } else if (yawTerm > MIN_YAWGAS / 2) { |
354 | } else if (yawTerm > MIN_YAWGAS / 2) { |
377 | debugOut.digital[0] |= DEBUG_CLIP; |
355 | debugOut.digital[0] |= DEBUG_CLIP; |
378 | yawTerm = MIN_YAWGAS / 2; |
356 | yawTerm = MIN_YAWGAS / 2; |
379 | } |
357 | } |
380 | //CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2)); |
358 | //CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2)); |
381 | } |
359 | } |
382 | 360 | ||
383 | // FIXME: Throttle may exceed maxThrottle (there is no check no more). |
361 | // FIXME: Throttle may exceed maxThrottle (there is no check no more). |
384 | tmp_int = staticParams.MaxThrottle * CONTROL_SCALING; |
362 | tmp_int = staticParams.maxThrottle * CONTROL_SCALING; |
385 | if (yawTerm < -(tmp_int - throttleTerm)) { |
363 | if (yawTerm < -(tmp_int - throttleTerm)) { |
386 | yawTerm = -(tmp_int - throttleTerm); |
364 | yawTerm = -(tmp_int - throttleTerm); |
387 | debugOut.digital[0] |= DEBUG_CLIP; |
365 | debugOut.digital[0] |= DEBUG_CLIP; |
388 | } else if (yawTerm > (tmp_int - throttleTerm)) { |
366 | } else if (yawTerm > (tmp_int - throttleTerm)) { |
389 | yawTerm = (tmp_int - throttleTerm); |
367 | yawTerm = (tmp_int - throttleTerm); |
390 | debugOut.digital[0] |= DEBUG_CLIP; |
368 | debugOut.digital[0] |= DEBUG_CLIP; |
391 | } |
369 | } |
392 | 370 | ||
393 | // CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm)); |
371 | // CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm)); |
394 | debugOut.digital[1] &= ~DEBUG_CLIP; |
372 | debugOut.digital[1] &= ~DEBUG_CLIP; |
395 | for (axis = PITCH; axis <= ROLL; axis++) { |
373 | for (axis = PITCH; axis <= ROLL; axis++) { |
396 | /* |
374 | /* |
397 | * Compose pitch and roll terms. This is finally where the sticks come into play. |
375 | * Compose pitch and roll terms. This is finally where the sticks come into play. |
398 | */ |
376 | */ |
399 | if (gyroIFactor) { |
377 | if (gyroIFactor) { |
400 | // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos. |
378 | // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos. |
401 | // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time. |
379 | // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time. |
402 | // TODO: Find out why this seems to be proportional to stick position - not integrating it at all. |
380 | // TODO: Find out why this seems to be proportional to stick position - not integrating it at all. |
403 | IPart[axis] += PPart[axis] - controls[axis]; // Integrate difference between P part (the angle) and the stick pos. |
381 | IPart[axis] += PPart[axis] - controls[axis]; // Integrate difference between P part (the angle) and the stick pos. |
404 | } else { |
382 | } else { |
405 | // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos. |
383 | // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos. |
406 | // To keep up with a full stick PDPart should be about 156... |
384 | // To keep up with a full stick PDPart should be about 156... |
407 | IPart[axis] += PDPart[axis] - controls[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
385 | IPart[axis] += PDPart[axis] - controls[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
408 | } |
386 | } |
409 | 387 | ||
410 | tmp_int = (int32_t) ((int32_t) dynamicParams.DynamicStability |
388 | tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability |
411 | * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64; |
389 | * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64; |
412 | 390 | ||
413 | // TODO: From which planet comes the 16000? |
391 | // TODO: From which planet comes the 16000? |
414 | CHECK_MIN_MAX(IPart[axis], -(CONTROL_SCALING * 16000L), (CONTROL_SCALING * 16000L)); |
392 | CHECK_MIN_MAX(IPart[axis], -(CONTROL_SCALING * 16000L), (CONTROL_SCALING * 16000L)); |
415 | // Add (P, D) parts minus stick pos. to the scaled-down I part. |
393 | // Add (P, D) parts minus stick pos. to the scaled-down I part. |
416 | term[axis] = PDPart[axis] - controls[axis] + IPart[axis] / Ki; // PID-controller for pitch |
394 | term[axis] = PDPart[axis] - controls[axis] + IPart[axis] / Ki; // PID-controller for pitch |
417 | 395 | ||
418 | /* |
396 | /* |
419 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
397 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
420 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
398 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
421 | * (max. pitch or roll term is the throttle value). |
399 | * (max. pitch or roll term is the throttle value). |
422 | * TODO: Why a growing function of yaw? |
400 | * TODO: Why a growing function of yaw? |
423 | */ |
401 | */ |
424 | if (term[axis] < -tmp_int) { |
402 | if (term[axis] < -tmp_int) { |
425 | debugOut.digital[1] |= DEBUG_CLIP; |
403 | debugOut.digital[1] |= DEBUG_CLIP; |
426 | } else if (term[axis] > tmp_int) { |
404 | } else if (term[axis] > tmp_int) { |
427 | debugOut.digital[1] |= DEBUG_CLIP; |
405 | debugOut.digital[1] |= DEBUG_CLIP; |
428 | } |
406 | } |
429 | CHECK_MIN_MAX(term[axis], -tmp_int, tmp_int); |
407 | CHECK_MIN_MAX(term[axis], -tmp_int, tmp_int); |
430 | } |
408 | } |
431 | 409 | ||
432 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
410 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
433 | // Universal Mixer |
411 | // Universal Mixer |
434 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
412 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
435 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
413 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
436 | 414 | ||
437 | debugOut.analog[12] = term[PITCH]; |
415 | debugOut.analog[12] = term[PITCH]; |
438 | debugOut.analog[13] = term[ROLL]; |
416 | debugOut.analog[13] = term[ROLL]; |
439 | debugOut.analog[14] = yawTerm; |
417 | debugOut.analog[14] = yawTerm; |
440 | debugOut.analog[15] = throttleTerm; |
418 | debugOut.analog[15] = throttleTerm; |
441 | 419 | ||
442 | for (i = 0; i < MAX_MOTORS; i++) { |
420 | for (i = 0; i < MAX_MOTORS; i++) { |
443 | int32_t tmp; |
421 | int32_t tmp; |
444 | uint8_t throttle; |
422 | uint8_t throttle; |
445 | 423 | ||
446 | tmp = (int32_t)throttleTerm * Mixer.Motor[i][MIX_THROTTLE]; |
424 | tmp = (int32_t)throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE]; |
447 | tmp += (int32_t)term[PITCH] * Mixer.Motor[i][MIX_PITCH]; |
425 | tmp += (int32_t)term[PITCH] * mixerMatrix.motor[i][MIX_PITCH]; |
448 | tmp += (int32_t)term[ROLL] * Mixer.Motor[i][MIX_ROLL]; |
426 | tmp += (int32_t)term[ROLL] * mixerMatrix.motor[i][MIX_ROLL]; |
449 | tmp += (int32_t)yawTerm * Mixer.Motor[i][MIX_YAW]; |
427 | tmp += (int32_t)yawTerm * mixerMatrix.motor[i][MIX_YAW]; |
450 | tmp = tmp >> 6; |
428 | tmp = tmp >> 6; |
451 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
429 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
452 | // Now we scale back down to a 0..255 range. |
430 | // Now we scale back down to a 0..255 range. |
453 | tmp = motorFilters[i] / MOTOR_SCALING; |
431 | tmp = motorFilters[i] / MOTOR_SCALING; |
454 | 432 | ||
455 | // So this was the THIRD time a throttle was limited. But should the limitation |
433 | // So this was the THIRD time a throttle was limited. But should the limitation |
456 | // apply to the common throttle signal (the one used for setting the "power" of |
434 | // apply to the common throttle signal (the one used for setting the "power" of |
457 | // all motors together) or should it limit the throttle set for each motor, |
435 | // all motors together) or should it limit the throttle set for each motor, |
458 | // including mix components of pitch, roll and yaw? I think only the common |
436 | // including mix components of pitch, roll and yaw? I think only the common |
459 | // throttle should be limited. |
437 | // throttle should be limited. |
460 | // --> WRONG. This caused motors to stall completely in tight maneuvers. |
438 | // --> WRONG. This caused motors to stall completely in tight maneuvers. |
461 | // Apply to individual signals instead. |
439 | // Apply to individual signals instead. |
462 | CHECK_MIN_MAX(tmp, 1, 255); |
440 | CHECK_MIN_MAX(tmp, 1, 255); |
463 | throttle = tmp; |
441 | throttle = tmp; |
464 | 442 | ||
465 | if (i < 4) debugOut.analog[22 + i] = throttle; |
443 | if (i < 4) debugOut.analog[22 + i] = throttle; |
466 | 444 | ||
467 | if ((MKFlags & MKFLAG_MOTOR_RUN) && Mixer.Motor[i][MIX_THROTTLE] > 0) { |
445 | if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) { |
468 | motor[i].SetPoint = throttle; |
446 | motor[i].SetPoint = throttle; |
469 | } else if (motorTestActive) { |
447 | } else if (motorTestActive) { |
470 | motor[i].SetPoint = motorTest[i]; |
448 | motor[i].SetPoint = motorTest[i]; |
471 | } else { |
449 | } else { |
472 | motor[i].SetPoint = 0; |
450 | motor[i].SetPoint = 0; |
473 | } |
451 | } |
474 | } |
452 | } |
475 | 453 | ||
476 | I2C_Start(TWI_STATE_MOTOR_TX); |
454 | I2C_Start(TWI_STATE_MOTOR_TX); |
477 | 455 | ||
478 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
456 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
479 | // Debugging |
457 | // Debugging |
480 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
458 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
481 | if (!(--debugDataTimer)) { |
459 | if (!(--debugDataTimer)) { |
482 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
460 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
483 | debugOut.analog[0] = (10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
461 | debugOut.analog[0] = (10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
484 | debugOut.analog[1] = (10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
462 | debugOut.analog[1] = (10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
485 | debugOut.analog[2] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; |
463 | debugOut.analog[2] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; |
486 | 464 | ||
487 | debugOut.analog[16] = gyroPFactor; |
465 | debugOut.analog[16] = gyroPFactor; |
488 | debugOut.analog[17] = gyroIFactor; |
466 | debugOut.analog[17] = gyroIFactor; |
489 | debugOut.analog[18] = dynamicParams.GyroD; |
467 | debugOut.analog[18] = dynamicParams.gyroD; |
490 | } |
468 | } |
491 | } |
469 | } |
492 | 470 |