Rev 1946 | Rev 1956 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1946 | Rev 1955 | ||
---|---|---|---|
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 | 64 | ||
65 | // for scope debugging |
65 | // for scope debugging |
66 | // #include "rc.h" |
66 | // #include "rc.h" |
67 | 67 | ||
68 | #include "twimaster.h" |
68 | #include "twimaster.h" |
69 | #include "attitude.h" |
69 | #include "attitude.h" |
70 | #include "controlMixer.h" |
70 | #include "controlMixer.h" |
71 | #include "commands.h" |
71 | #include "commands.h" |
72 | #ifdef USE_MK3MAG |
72 | #ifdef USE_MK3MAG |
73 | #include "gps.h" |
73 | #include "gps.h" |
74 | #endif |
74 | #endif |
75 | 75 | ||
76 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
76 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
77 | 77 | ||
78 | /* |
78 | /* |
79 | * These are no longer maintained, just left at 0. The original implementation just summed the acc. |
79 | * 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??? |
80 | * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey??? |
81 | */ |
81 | */ |
82 | // int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
82 | // int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
83 | 83 | ||
84 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
84 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
85 | uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control |
85 | uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control |
86 | 86 | ||
87 | // Some integral weight constant... |
87 | // Some integral weight constant... |
88 | uint16_t Ki = 10300 / 33; |
88 | uint16_t Ki = 10300 / 33; |
89 | uint8_t RequiredMotors = 0; |
89 | uint8_t RequiredMotors = 0; |
90 | 90 | ||
91 | /************************************************************************/ |
91 | /************************************************************************/ |
92 | /* Filter for motor value smoothing (necessary???) */ |
92 | /* Filter for motor value smoothing (necessary???) */ |
93 | /************************************************************************/ |
93 | /************************************************************************/ |
94 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
94 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
95 | switch (dynamicParams.UserParams[5]) { |
95 | switch (dynamicParams.UserParams[5]) { |
96 | case 0: |
96 | case 0: |
97 | return newvalue; |
97 | return newvalue; |
98 | case 1: |
98 | case 1: |
99 | return (oldvalue + newvalue) / 2; |
99 | return (oldvalue + newvalue) / 2; |
100 | case 2: |
100 | case 2: |
101 | if (newvalue > oldvalue) |
101 | if (newvalue > oldvalue) |
102 | 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 |
103 | else |
103 | else |
104 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
104 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
105 | case 3: |
105 | case 3: |
106 | if (newvalue < oldvalue) |
106 | if (newvalue < oldvalue) |
107 | return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new |
107 | return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new |
108 | else |
108 | else |
109 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
109 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
110 | default: |
110 | default: |
111 | return newvalue; |
111 | return newvalue; |
112 | } |
112 | } |
113 | } |
113 | } |
114 | 114 | ||
115 | /************************************************************************/ |
115 | /************************************************************************/ |
116 | /* Neutral Readings */ |
116 | /* Neutral Readings */ |
117 | /************************************************************************/ |
117 | /************************************************************************/ |
118 | void flight_setNeutral() { |
118 | void flight_setNeutral() { |
119 | MKFlags |= MKFLAG_CALIBRATE; |
119 | MKFlags |= MKFLAG_CALIBRATE; |
120 | // not really used here any more. |
120 | // not really used here any more. |
121 | dynamicParams.KalmanK = -1; |
121 | dynamicParams.KalmanK = -1; |
122 | dynamicParams.KalmanMaxDrift = 0; |
122 | dynamicParams.KalmanMaxDrift = 0; |
123 | dynamicParams.KalmanMaxFusion = 32; |
123 | dynamicParams.KalmanMaxFusion = 32; |
124 | controlMixer_initVariables(); |
124 | controlMixer_initVariables(); |
125 | } |
125 | } |
126 | 126 | ||
127 | void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor, |
127 | void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor, |
128 | uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
128 | uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
129 | Ki = 10300 / _Ki; |
129 | Ki = 10300 / _Ki; |
130 | gyroPFactor = _gyroPFactor; |
130 | gyroPFactor = _gyroPFactor; |
131 | gyroIFactor = _gyroIFactor; |
131 | gyroIFactor = _gyroIFactor; |
132 | yawPFactor = _yawPFactor; |
132 | yawPFactor = _yawPFactor; |
133 | yawIFactor = _yawIFactor; |
133 | yawIFactor = _yawIFactor; |
134 | } |
134 | } |
135 | 135 | ||
136 | void setNormalFlightParameters(void) { |
136 | void setNormalFlightParameters(void) { |
137 | setFlightParameters(dynamicParams.IFactor, dynamicParams.GyroP, |
137 | setFlightParameters(dynamicParams.IFactor, |
- | 138 | dynamicParams.GyroP, |
|
138 | staticParams.GlobalConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.GyroI, |
139 | staticParams.GlobalConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.GyroI, |
- | 140 | dynamicParams.GyroP, |
|
139 | dynamicParams.GyroP, dynamicParams.UserParams[6]); |
141 | dynamicParams.UserParams[6] |
- | 142 | ); |
|
140 | } |
143 | } |
141 | 144 | ||
142 | void setStableFlightParameters(void) { |
145 | void setStableFlightParameters(void) { |
143 | setFlightParameters(33, 90, 120, 90, 120); |
146 | setFlightParameters(33, 90, 120, 90, 120); |
144 | } |
147 | } |
145 | 148 | ||
146 | /************************************************************************/ |
149 | /************************************************************************/ |
147 | /* Main Flight Control */ |
150 | /* Main Flight Control */ |
148 | /************************************************************************/ |
151 | /************************************************************************/ |
149 | void flight_control(void) { |
152 | void flight_control(void) { |
150 | int16_t tmp_int; |
153 | int16_t tmp_int; |
151 | // Mixer Fractions that are combined for Motor Control |
154 | // Mixer Fractions that are combined for Motor Control |
152 | int16_t yawTerm, throttleTerm, term[2]; |
155 | int16_t yawTerm, throttleTerm, term[2]; |
153 | 156 | ||
154 | // PID controller variables |
157 | // PID controller variables |
155 | int16_t PDPart[2], PDPartYaw, PPart[2]; |
158 | int16_t PDPart[2], PDPartYaw, PPart[2]; |
156 | static int32_t IPart[2] = { 0, 0 }; |
159 | static int32_t IPart[2] = { 0, 0 }; |
157 | // static int32_t yawControlRate = 0; |
160 | // static int32_t yawControlRate = 0; |
158 | 161 | ||
159 | // Removed. Too complicated, and apparently not necessary with MEMS gyros anyway. |
162 | // Removed. Too complicated, and apparently not necessary with MEMS gyros anyway. |
160 | // static int32_t IntegralGyroPitchError = 0, IntegralGyroRollError = 0; |
163 | // static int32_t IntegralGyroPitchError = 0, IntegralGyroRollError = 0; |
161 | // static int32_t CorrectionPitch, CorrectionRoll; |
164 | // static int32_t CorrectionPitch, CorrectionRoll; |
162 | 165 | ||
163 | static uint16_t emergencyFlightTime; |
166 | static uint16_t emergencyFlightTime; |
164 | static int8_t debugDataTimer = 1; |
167 | static int8_t debugDataTimer = 1; |
165 | 168 | ||
166 | // High resolution motor values for smoothing of PID motor outputs |
169 | // High resolution motor values for smoothing of PID motor outputs |
167 | static int16_t motorFilters[MAX_MOTORS]; |
170 | static int16_t motorFilters[MAX_MOTORS]; |
168 | 171 | ||
169 | uint8_t i, axis; |
172 | uint8_t i, axis; |
170 | 173 | ||
171 | // Fire the main flight attitude calculation, including integration of angles. |
174 | // Fire the main flight attitude calculation, including integration of angles. |
172 | // We want that to kick as early as possible, not to delay new AD sampling further. |
175 | // We want that to kick as early as possible, not to delay new AD sampling further. |
173 | calculateFlightAttitude(); |
176 | calculateFlightAttitude(); |
174 | controlMixer_update(); |
177 | controlMixer_update(); |
175 | throttleTerm = controls[CONTROL_THROTTLE]; |
178 | throttleTerm = controls[CONTROL_THROTTLE]; |
176 | 179 | ||
177 | // This check removed. Is done on a per-motor basis, after output matrix multiplication. |
180 | // This check removed. Is done on a per-motor basis, after output matrix multiplication. |
178 | if (throttleTerm < staticParams.MinThrottle + 10) |
181 | if (throttleTerm < staticParams.MinThrottle + 10) |
179 | throttleTerm = staticParams.MinThrottle + 10; |
182 | throttleTerm = staticParams.MinThrottle + 10; |
180 | else if (throttleTerm > staticParams.MaxThrottle - 20) |
183 | else if (throttleTerm > staticParams.MaxThrottle - 20) |
181 | throttleTerm = (staticParams.MaxThrottle - 20); |
184 | throttleTerm = (staticParams.MaxThrottle - 20); |
182 | 185 | ||
183 | /************************************************************************/ |
186 | /************************************************************************/ |
184 | /* RC-signal is bad */ |
187 | /* RC-signal is bad */ |
185 | /* This part could be abstracted, as having yet another control input */ |
188 | /* This part could be abstracted, as having yet another control input */ |
186 | /* to the control mixer: An emergency autopilot control. */ |
189 | /* to the control mixer: An emergency autopilot control. */ |
187 | /************************************************************************/ |
190 | /************************************************************************/ |
188 | 191 | ||
189 | if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy |
192 | if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy |
190 | RED_ON; |
193 | RED_ON; |
191 | beepRCAlarm(); |
194 | beepRCAlarm(); |
192 | 195 | ||
193 | if (emergencyFlightTime) { |
196 | if (emergencyFlightTime) { |
194 | // continue emergency flight |
197 | // continue emergency flight |
195 | emergencyFlightTime--; |
198 | emergencyFlightTime--; |
196 | if (isFlying > 256) { |
199 | if (isFlying > 256) { |
197 | // We're probably still flying. Descend slowly. |
200 | // We're probably still flying. Descend slowly. |
198 | throttleTerm = staticParams.EmergencyGas; // Set emergency throttle |
201 | throttleTerm = staticParams.EmergencyGas; // Set emergency throttle |
199 | MKFlags |= (MKFLAG_EMERGENCY_LANDING); // Set flag for emergency landing |
202 | MKFlags |= (MKFLAG_EMERGENCY_LANDING); // Set flag for emergency landing |
200 | setStableFlightParameters(); |
203 | setStableFlightParameters(); |
201 | } else { |
204 | } else { |
202 | MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors. |
205 | MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors. |
203 | } |
206 | } |
204 | } else { |
207 | } else { |
205 | // end emergency flight (just cut the motors???) |
208 | // end emergency flight (just cut the motors???) |
206 | MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_LANDING); |
209 | MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_LANDING); |
207 | } |
210 | } |
208 | } else { |
211 | } else { |
209 | // signal is acceptable |
212 | // signal is acceptable |
210 | if (controlMixer_getSignalQuality() > SIGNAL_BAD) { |
213 | if (controlMixer_getSignalQuality() > SIGNAL_BAD) { |
211 | // Reset emergency landing control variables. |
214 | // Reset emergency landing control variables. |
212 | MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing |
215 | MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing |
213 | // The time is in whole seconds. |
216 | // The time is in whole seconds. |
214 | emergencyFlightTime = (uint16_t) staticParams.EmergencyGasDuration * 488; |
217 | emergencyFlightTime = (uint16_t) staticParams.EmergencyGasDuration * 488; |
215 | } |
218 | } |
216 | 219 | ||
217 | // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying. |
220 | // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying. |
218 | if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
221 | if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
219 | // increment flight-time counter until overflow. |
222 | // increment flight-time counter until overflow. |
220 | if (isFlying != 0xFFFF) |
223 | if (isFlying != 0xFFFF) |
221 | isFlying++; |
224 | isFlying++; |
222 | } else |
225 | } else |
223 | /* |
226 | /* |
224 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
227 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
225 | * Probably to avoid integration effects that will cause the copter to spin |
228 | * Probably to avoid integration effects that will cause the copter to spin |
226 | * or flip when taking off. |
229 | * or flip when taking off. |
227 | */ |
230 | */ |
228 | if (isFlying < 256) { |
231 | if (isFlying < 256) { |
229 | IPart[PITCH] = IPart[ROLL] = 0; |
232 | IPart[PITCH] = IPart[ROLL] = 0; |
230 | // TODO: Don't stomp on other modules' variables!!! |
233 | // TODO: Don't stomp on other modules' variables!!! |
231 | // controlYaw = 0; |
234 | // controlYaw = 0; |
232 | PDPartYaw = 0; // instead. |
235 | PDPartYaw = 0; // instead. |
233 | if (isFlying == 250) { |
236 | if (isFlying == 250) { |
234 | // HC_setGround(); |
237 | // HC_setGround(); |
235 | updateCompassCourse = 1; |
238 | updateCompassCourse = 1; |
236 | yawAngleDiff = 0; |
239 | yawAngleDiff = 0; |
237 | } |
240 | } |
238 | } else { |
241 | } else { |
239 | // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag? |
242 | // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag? |
240 | // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe. |
243 | // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe. |
241 | MKFlags |= (MKFLAG_FLY); |
244 | MKFlags |= (MKFLAG_FLY); |
242 | } |
245 | } |
243 | 246 | ||
244 | commands_handleCommands(); |
247 | commands_handleCommands(); |
245 | 248 | ||
246 | // if(controlMixer_getSignalQuality() >= SIGNAL_GOOD) { |
249 | // if(controlMixer_getSignalQuality() >= SIGNAL_GOOD) { |
247 | setNormalFlightParameters(); |
250 | setNormalFlightParameters(); |
248 | // } |
251 | // } |
249 | } // end else (not bad signal case) |
252 | } // end else (not bad signal case) |
250 | // end part1a: 750-800 usec. |
253 | // end part1a: 750-800 usec. |
251 | /* |
254 | /* |
252 | * Looping the H&I way basically is just a matter of turning off attitude angle measurement |
255 | * Looping the H&I way basically is just a matter of turning off attitude angle measurement |
253 | * by integration (because 300 deg/s gyros are too slow) and turning down the throttle. |
256 | * by integration (because 300 deg/s gyros are too slow) and turning down the throttle. |
254 | * This is the throttle part. |
257 | * This is the throttle part. |
255 | */ |
258 | */ |
256 | if (looping) { |
259 | if (looping) { |
257 | if (throttleTerm > staticParams.LoopGasLimit) |
260 | if (throttleTerm > staticParams.LoopGasLimit) |
258 | throttleTerm = staticParams.LoopGasLimit; |
261 | throttleTerm = staticParams.LoopGasLimit; |
259 | } |
262 | } |
260 | 263 | ||
261 | /************************************************************************/ |
264 | /************************************************************************/ |
262 | /* Yawing */ |
265 | /* Yawing */ |
263 | /************************************************************************/ |
266 | /************************************************************************/ |
264 | if (abs(controls[CONTROL_YAW]) > 4 * staticParams.StickYawP) { // yaw stick is activated |
267 | if (abs(controls[CONTROL_YAW]) > 4 * staticParams.StickYawP) { // yaw stick is activated |
265 | ignoreCompassTimer = 1000; |
268 | ignoreCompassTimer = 1000; |
266 | if (!(staticParams.GlobalConfig & CFG_COMPASS_FIX)) { |
269 | if (!(staticParams.GlobalConfig & CFG_COMPASS_FIX)) { |
267 | updateCompassCourse = 1; |
270 | updateCompassCourse = 1; |
268 | } |
271 | } |
269 | } |
272 | } |
270 | 273 | ||
271 | // yawControlRate = controlYaw; |
274 | // yawControlRate = controlYaw; |
272 | 275 | ||
273 | // Trim drift of yawAngleDiff with controlYaw. |
276 | // Trim drift of yawAngleDiff with controlYaw. |
274 | // TODO: We want NO feedback of control related stuff to the attitude related stuff. |
277 | // TODO: We want NO feedback of control related stuff to the attitude related stuff. |
275 | // This seems to be used as: Difference desired <--> real heading. |
278 | // This seems to be used as: Difference desired <--> real heading. |
276 | yawAngleDiff -= controls[CONTROL_YAW]; |
279 | yawAngleDiff -= controls[CONTROL_YAW]; |
277 | 280 | ||
278 | // limit the effect |
281 | // limit the effect |
279 | CHECK_MIN_MAX(yawAngleDiff, -50000, 50000); |
282 | CHECK_MIN_MAX(yawAngleDiff, -50000, 50000); |
280 | 283 | ||
281 | /************************************************************************/ |
284 | /************************************************************************/ |
282 | /* Compass is currently not supported. */ |
285 | /* Compass is currently not supported. */ |
283 | /************************************************************************/ |
286 | /************************************************************************/ |
284 | if (staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) { |
287 | if (staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) { |
285 | updateCompass(); |
288 | updateCompass(); |
286 | } |
289 | } |
287 | 290 | ||
288 | #if defined (USE_NAVICTRL) |
291 | #if defined (USE_NAVICTRL) |
289 | /************************************************************************/ |
292 | /************************************************************************/ |
290 | /* GPS is currently not supported. */ |
293 | /* GPS is currently not supported. */ |
291 | /************************************************************************/ |
294 | /************************************************************************/ |
292 | if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) { |
295 | if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) { |
293 | GPS_Main(); |
296 | GPS_Main(); |
294 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
297 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
295 | } else { |
298 | } else { |
296 | } |
299 | } |
297 | #endif |
300 | #endif |
298 | // end part 1: 750-800 usec. |
301 | // end part 1: 750-800 usec. |
299 | // start part 3: 350 - 400 usec. |
302 | // start part 3: 350 - 400 usec. |
300 | #define SENSOR_LIMIT (4096 * 4) |
303 | #define SENSOR_LIMIT (4096 * 4) |
301 | /************************************************************************/ |
304 | /************************************************************************/ |
302 | 305 | ||
303 | /* Calculate control feedback from angle (gyro integral) */ |
306 | /* Calculate control feedback from angle (gyro integral) */ |
304 | /* and angular velocity (gyro signal) */ |
307 | /* and angular velocity (gyro signal) */ |
305 | /************************************************************************/ |
308 | /************************************************************************/ |
306 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
309 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
307 | 310 | ||
308 | for (axis = PITCH; axis <= ROLL; axis++) { |
311 | for (axis = PITCH; axis <= ROLL; axis++) { |
309 | if (looping & ((1 << 4) << axis)) { |
312 | if (looping & ((1 << 4) << axis)) { |
310 | PPart[axis] = 0; |
313 | PPart[axis] = 0; |
311 | } else { // TODO: Where do the 44000 come from??? |
314 | } else { // TODO: Where do the 44000 come from??? |
312 | PPart[axis] = angle[axis] * gyroIFactor / (44000 / CONTROL_SCALING); // P-Part - Proportional to Integral |
315 | PPart[axis] = angle[axis] * gyroIFactor / (44000 / CONTROL_SCALING); // P-Part - Proportional to Integral |
313 | } |
316 | } |
314 | 317 | ||
315 | /* |
318 | /* |
316 | * Now blend in the D-part - proportional to the Differential of the integral = the rate. |
319 | * Now blend in the D-part - proportional to the Differential of the integral = the rate. |
317 | * Read this as: PDPart = PPart + rate_PID * pfactor * CONTROL_SCALING |
320 | * Read this as: PDPart = PPart + rate_PID * pfactor * CONTROL_SCALING |
318 | * where pfactor is in [0..1]. |
321 | * where pfactor is in [0..1]. |
319 | */ |
322 | */ |
320 | PDPart[axis] = PPart[axis] + (int32_t) ((int32_t) rate_PID[axis] |
323 | PDPart[axis] = PPart[axis] + (int32_t) ((int32_t) rate_PID[axis] |
321 | * gyroPFactor / (256L / CONTROL_SCALING)) + (differential[axis] |
324 | * gyroPFactor / (256L / CONTROL_SCALING)) + (differential[axis] |
322 | * (int16_t) dynamicParams.GyroD) / 16; |
325 | * (int16_t) dynamicParams.GyroD) / 16; |
323 | 326 | ||
324 | CHECK_MIN_MAX(PDPart[axis], -SENSOR_LIMIT, SENSOR_LIMIT); |
327 | CHECK_MIN_MAX(PDPart[axis], -SENSOR_LIMIT, SENSOR_LIMIT); |
325 | } |
328 | } |
326 | 329 | ||
327 | PDPartYaw = (int32_t) (yawRate * 2 * (int32_t) yawPFactor) / (256L |
330 | PDPartYaw = (int32_t) (yawRate * 2 * (int32_t) yawPFactor) / (256L |
328 | / CONTROL_SCALING) + (int32_t) (yawAngleDiff * yawIFactor) / (2 * (44000 |
331 | / CONTROL_SCALING) + (int32_t) (yawAngleDiff * yawIFactor) / (2 * (44000 |
329 | / CONTROL_SCALING)); |
332 | / CONTROL_SCALING)); |
330 | 333 | ||
331 | // limit control feedback |
334 | // limit control feedback |
332 | CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
335 | CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
333 | 336 | ||
334 | /* |
337 | /* |
335 | * Compose throttle term. |
338 | * Compose throttle term. |
336 | * If a Bl-Ctrl is missing, prevent takeoff. |
339 | * If a Bl-Ctrl is missing, prevent takeoff. |
337 | */ |
340 | */ |
338 | if (missingMotor) { |
341 | if (missingMotor) { |
339 | // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway??? |
342 | // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway??? |
340 | if (isFlying > 1 && isFlying < 50 && throttleTerm > 0) |
343 | if (isFlying > 1 && isFlying < 50 && throttleTerm > 0) |
341 | isFlying = 1; // keep within lift off condition |
344 | isFlying = 1; // keep within lift off condition |
342 | throttleTerm = staticParams.MinThrottle; // reduce gas to min to avoid lift of |
345 | throttleTerm = staticParams.MinThrottle; // reduce gas to min to avoid lift of |
343 | } |
346 | } |
344 | 347 | ||
345 | // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already? |
348 | // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already? |
346 | throttleTerm *= CONTROL_SCALING; |
349 | throttleTerm *= CONTROL_SCALING; |
347 | 350 | ||
348 | /* |
351 | /* |
349 | * Compose yaw term. |
352 | * Compose yaw term. |
350 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
353 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
351 | * However, at low throttle the yaw term is limited to a fixed value, |
354 | * However, at low throttle the yaw term is limited to a fixed value, |
352 | * and at high throttle it is limited by the throttle reserve (the difference |
355 | * and at high throttle it is limited by the throttle reserve (the difference |
353 | * between current throttle and maximum throttle). |
356 | * between current throttle and maximum throttle). |
354 | */ |
357 | */ |
355 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
358 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
356 | yawTerm = PDPartYaw - controls[CONTROL_YAW] * CONTROL_SCALING; |
359 | yawTerm = PDPartYaw - controls[CONTROL_YAW] * CONTROL_SCALING; |
357 | // Limit yawTerm |
360 | // Limit yawTerm |
358 | DebugOut.Digital[0] &= ~DEBUG_CLIP; |
361 | debugOut.digital[0] &= ~DEBUG_CLIP; |
359 | if (throttleTerm > MIN_YAWGAS) { |
362 | if (throttleTerm > MIN_YAWGAS) { |
360 | if (yawTerm < -throttleTerm / 2) { |
363 | if (yawTerm < -throttleTerm / 2) { |
361 | DebugOut.Digital[0] |= DEBUG_CLIP; |
364 | debugOut.digital[0] |= DEBUG_CLIP; |
362 | yawTerm = -throttleTerm / 2; |
365 | yawTerm = -throttleTerm / 2; |
363 | } else if (yawTerm > throttleTerm / 2) { |
366 | } else if (yawTerm > throttleTerm / 2) { |
364 | DebugOut.Digital[0] |= DEBUG_CLIP; |
367 | debugOut.digital[0] |= DEBUG_CLIP; |
365 | yawTerm = throttleTerm / 2; |
368 | yawTerm = throttleTerm / 2; |
366 | } |
369 | } |
367 | //CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2)); |
370 | //CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2)); |
368 | } else { |
371 | } else { |
369 | if (yawTerm < -MIN_YAWGAS / 2) { |
372 | if (yawTerm < -MIN_YAWGAS / 2) { |
370 | DebugOut.Digital[0] |= DEBUG_CLIP; |
373 | debugOut.digital[0] |= DEBUG_CLIP; |
371 | yawTerm = -MIN_YAWGAS / 2; |
374 | yawTerm = -MIN_YAWGAS / 2; |
372 | } else if (yawTerm > MIN_YAWGAS / 2) { |
375 | } else if (yawTerm > MIN_YAWGAS / 2) { |
373 | DebugOut.Digital[0] |= DEBUG_CLIP; |
376 | debugOut.digital[0] |= DEBUG_CLIP; |
374 | yawTerm = MIN_YAWGAS / 2; |
377 | yawTerm = MIN_YAWGAS / 2; |
375 | } |
378 | } |
376 | //CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2)); |
379 | //CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2)); |
377 | } |
380 | } |
378 | 381 | ||
379 | // FIXME: Throttle may exceed maxThrottle (there is no check no more). |
382 | // FIXME: Throttle may exceed maxThrottle (there is no check no more). |
380 | tmp_int = staticParams.MaxThrottle * CONTROL_SCALING; |
383 | tmp_int = staticParams.MaxThrottle * CONTROL_SCALING; |
381 | if (yawTerm < -(tmp_int - throttleTerm)) { |
384 | if (yawTerm < -(tmp_int - throttleTerm)) { |
382 | yawTerm = -(tmp_int - throttleTerm); |
385 | yawTerm = -(tmp_int - throttleTerm); |
383 | DebugOut.Digital[0] |= DEBUG_CLIP; |
386 | debugOut.digital[0] |= DEBUG_CLIP; |
384 | } else if (yawTerm > (tmp_int - throttleTerm)) { |
387 | } else if (yawTerm > (tmp_int - throttleTerm)) { |
385 | yawTerm = (tmp_int - throttleTerm); |
388 | yawTerm = (tmp_int - throttleTerm); |
386 | DebugOut.Digital[0] |= DEBUG_CLIP; |
389 | debugOut.digital[0] |= DEBUG_CLIP; |
387 | } |
390 | } |
388 | 391 | ||
389 | // CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm)); |
392 | // CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm)); |
390 | DebugOut.Digital[1] &= ~DEBUG_CLIP; |
393 | debugOut.digital[1] &= ~DEBUG_CLIP; |
391 | for (axis = PITCH; axis <= ROLL; axis++) { |
394 | for (axis = PITCH; axis <= ROLL; axis++) { |
392 | /* |
395 | /* |
393 | * Compose pitch and roll terms. This is finally where the sticks come into play. |
396 | * Compose pitch and roll terms. This is finally where the sticks come into play. |
394 | */ |
397 | */ |
395 | if (gyroIFactor) { |
398 | if (gyroIFactor) { |
396 | // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos. |
399 | // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos. |
397 | // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time. |
400 | // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time. |
398 | // TODO: Find out why this seems to be proportional to stick position - not integrating it at all. |
401 | // TODO: Find out why this seems to be proportional to stick position - not integrating it at all. |
399 | IPart[axis] += PPart[axis] - controls[axis]; // Integrate difference between P part (the angle) and the stick pos. |
402 | IPart[axis] += PPart[axis] - controls[axis]; // Integrate difference between P part (the angle) and the stick pos. |
400 | } else { |
403 | } else { |
401 | // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos. |
404 | // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos. |
402 | // To keep up with a full stick PDPart should be about 156... |
405 | // To keep up with a full stick PDPart should be about 156... |
403 | 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. |
406 | 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. |
404 | } |
407 | } |
405 | 408 | ||
406 | tmp_int = (int32_t) ((int32_t) dynamicParams.DynamicStability |
409 | tmp_int = (int32_t) ((int32_t) dynamicParams.DynamicStability |
407 | * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64; |
410 | * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64; |
408 | 411 | ||
409 | // TODO: From which planet comes the 16000? |
412 | // TODO: From which planet comes the 16000? |
410 | CHECK_MIN_MAX(IPart[axis], -(CONTROL_SCALING * 16000L), (CONTROL_SCALING * 16000L)); |
413 | CHECK_MIN_MAX(IPart[axis], -(CONTROL_SCALING * 16000L), (CONTROL_SCALING * 16000L)); |
411 | // Add (P, D) parts minus stick pos. to the scaled-down I part. |
414 | // Add (P, D) parts minus stick pos. to the scaled-down I part. |
412 | term[axis] = PDPart[axis] - controls[axis] + IPart[axis] / Ki; // PID-controller for pitch |
415 | term[axis] = PDPart[axis] - controls[axis] + IPart[axis] / Ki; // PID-controller for pitch |
413 | 416 | ||
414 | /* |
417 | /* |
415 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
418 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
416 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
419 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
417 | * (max. pitch or roll term is the throttle value). |
420 | * (max. pitch or roll term is the throttle value). |
418 | * TODO: Why a growing function of yaw? |
421 | * TODO: Why a growing function of yaw? |
419 | */ |
422 | */ |
420 | if (term[axis] < -tmp_int) { |
423 | if (term[axis] < -tmp_int) { |
421 | DebugOut.Digital[1] |= DEBUG_CLIP; |
424 | debugOut.digital[1] |= DEBUG_CLIP; |
422 | } else if (term[axis] > tmp_int) { |
425 | } else if (term[axis] > tmp_int) { |
423 | DebugOut.Digital[1] |= DEBUG_CLIP; |
426 | debugOut.digital[1] |= DEBUG_CLIP; |
424 | } |
427 | } |
425 | CHECK_MIN_MAX(term[axis], -tmp_int, tmp_int); |
428 | CHECK_MIN_MAX(term[axis], -tmp_int, tmp_int); |
426 | } |
429 | } |
427 | 430 | ||
428 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
431 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
429 | // Universal Mixer |
432 | // Universal Mixer |
430 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
433 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
431 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
434 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
432 | 435 | ||
433 | DebugOut.Analog[12] = term[PITCH]; |
436 | debugOut.analog[12] = term[PITCH]; |
434 | DebugOut.Analog[13] = term[ROLL]; |
437 | debugOut.analog[13] = term[ROLL]; |
435 | //DebugOut.Analog[14] = yawTerm; |
438 | debugOut.analog[14] = yawTerm; |
436 | DebugOut.Analog[15] = throttleTerm; |
439 | debugOut.analog[15] = throttleTerm; |
437 | 440 | ||
438 | for (i = 0; i < MAX_MOTORS; i++) { |
441 | for (i = 0; i < MAX_MOTORS; i++) { |
439 | int32_t tmp; |
442 | int32_t tmp; |
440 | uint8_t throttle; |
443 | uint8_t throttle; |
441 | 444 | ||
442 | tmp = (int32_t)throttleTerm * Mixer.Motor[i][MIX_THROTTLE]; |
445 | tmp = (int32_t)throttleTerm * Mixer.Motor[i][MIX_THROTTLE]; |
443 | tmp += (int32_t)term[PITCH] * Mixer.Motor[i][MIX_PITCH]; |
446 | tmp += (int32_t)term[PITCH] * Mixer.Motor[i][MIX_PITCH]; |
444 | tmp += (int32_t)term[ROLL] * Mixer.Motor[i][MIX_ROLL]; |
447 | tmp += (int32_t)term[ROLL] * Mixer.Motor[i][MIX_ROLL]; |
445 | tmp += (int32_t)yawTerm * Mixer.Motor[i][MIX_YAW]; |
448 | tmp += (int32_t)yawTerm * Mixer.Motor[i][MIX_YAW]; |
446 | tmp = tmp >> 6; |
449 | tmp = tmp >> 6; |
447 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
450 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
448 | // Now we scale back down to a 0..255 range. |
451 | // Now we scale back down to a 0..255 range. |
449 | tmp = motorFilters[i] / MOTOR_SCALING; |
452 | tmp = motorFilters[i] / MOTOR_SCALING; |
450 | 453 | ||
451 | // So this was the THIRD time a throttle was limited. But should the limitation |
454 | // So this was the THIRD time a throttle was limited. But should the limitation |
452 | // apply to the common throttle signal (the one used for setting the "power" of |
455 | // apply to the common throttle signal (the one used for setting the "power" of |
453 | // all motors together) or should it limit the throttle set for each motor, |
456 | // all motors together) or should it limit the throttle set for each motor, |
454 | // including mix components of pitch, roll and yaw? I think only the common |
457 | // including mix components of pitch, roll and yaw? I think only the common |
455 | // throttle should be limited. |
458 | // throttle should be limited. |
456 | // --> WRONG. This caused motors to stall completely in tight maneuvers. |
459 | // --> WRONG. This caused motors to stall completely in tight maneuvers. |
457 | // Apply to individual signals instead. |
460 | // Apply to individual signals instead. |
458 | CHECK_MIN_MAX(tmp, 1, 255); |
461 | CHECK_MIN_MAX(tmp, 1, 255); |
459 | throttle = tmp; |
462 | throttle = tmp; |
460 | 463 | ||
461 | if (i < 4) DebugOut.Analog[22 + i] = throttle; |
464 | if (i < 4) debugOut.analog[22 + i] = throttle; |
462 | 465 | ||
463 | if (MKFlags & MKFLAG_MOTOR_RUN && Mixer.Motor[i][MIX_THROTTLE] > 0) { |
466 | if ((MKFlags & MKFLAG_MOTOR_RUN) && Mixer.Motor[i][MIX_THROTTLE] > 0) { |
464 | motor[i].SetPoint = throttle; |
467 | motor[i].SetPoint = throttle; |
465 | } else if (motorTestActive) { |
468 | } else if (motorTestActive) { |
466 | motor[i].SetPoint = motorTest[i]; |
469 | motor[i].SetPoint = motorTest[i]; |
467 | } else { |
470 | } else { |
468 | motor[i].SetPoint = 0; |
471 | motor[i].SetPoint = 0; |
469 | } |
472 | } |
470 | } |
473 | } |
471 | 474 | ||
472 | I2C_Start(TWI_STATE_MOTOR_TX); |
475 | I2C_Start(TWI_STATE_MOTOR_TX); |
473 | 476 | ||
474 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
477 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
475 | // Debugging |
478 | // Debugging |
476 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
479 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
477 | if (!(--debugDataTimer)) { |
480 | if (!(--debugDataTimer)) { |
478 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
481 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
479 | DebugOut.Analog[0] = (10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
482 | debugOut.analog[0] = (10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
480 | DebugOut.Analog[1] = (10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
483 | debugOut.analog[1] = (10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
481 | DebugOut.Analog[2] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; |
484 | debugOut.analog[2] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; |
482 | 485 | ||
483 | DebugOut.Analog[6] = 64 >> 4; |
486 | debugOut.analog[16] = gyroPFactor; |
484 | DebugOut.Analog[7] = -64 >> 4; |
487 | debugOut.analog[17] = gyroIFactor; |
485 | - | ||
486 | /* |
- | |
487 | DebugOut.Analog[23] = (yawRate * 2 * (int32_t)yawPFactor) / (256L / CONTROL_SCALING); |
- | |
488 | DebugOut.Analog[24] = controlYaw; |
- | |
489 | DebugOut.Analog[25] = yawAngleDiff / 100L; |
- | |
490 | DebugOut.Analog[26] = accNoisePeak[PITCH]; |
- | |
491 | DebugOut.Analog[27] = accNoisePeak[ROLL]; |
- | |
492 | DebugOut.Analog[30] = gyroNoisePeak[PITCH]; |
488 | debugOut.analog[18] = dynamicParams.GyroD; |
493 | DebugOut.Analog[31] = gyroNoisePeak[ROLL]; |
- | |
494 | */ |
- | |
495 | } |
489 | } |
496 | } |
490 | } |
497 | 491 |