Rev 1180 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1180 | Rev 1238 | ||
---|---|---|---|
1 | /*####################################################################################### |
1 | /*####################################################################################### |
2 | Flight Control |
2 | Flight Control |
3 | #######################################################################################*/ |
3 | #######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + Copyright (c) 04.2007 Holger Buss |
5 | // + Copyright (c) 04.2007 Holger Buss |
6 | // + Nur für den privaten Gebrauch |
6 | // + Nur für den privaten Gebrauch |
7 | // + www.MikroKopter.com |
7 | // + www.MikroKopter.com |
8 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
8 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
9 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
10 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
10 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
11 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
11 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
12 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
12 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
13 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
13 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
14 | // + Verkauf von Luftbildaufnahmen, usw. |
14 | // + Verkauf von Luftbildaufnahmen, usw. |
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
16 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
17 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
17 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
18 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
18 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
19 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
20 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
20 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
21 | // + eindeutig als Ursprung verlinkt werden |
21 | // + eindeutig als Ursprung verlinkt werden |
22 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
22 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
23 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
24 | // + Benutzung auf eigene Gefahr |
24 | // + Benutzung auf eigene Gefahr |
25 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
25 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
27 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + mit unserer Zustimmung zulässig |
28 | // + mit unserer Zustimmung zulässig |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + this list of conditions and the following disclaimer. |
33 | // + this list of conditions and the following disclaimer. |
34 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
34 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
35 | // + from this software without specific prior written permission. |
35 | // + from this software without specific prior written permission. |
36 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
36 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
37 | // + for non-commercial use (directly or indirectly) |
37 | // + for non-commercial use (directly or indirectly) |
38 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
38 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + with our written permission |
39 | // + with our written permission |
40 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
40 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + clearly linked as origin |
41 | // + clearly linked as origin |
42 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
42 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
43 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
43 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
44 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
44 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
45 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
45 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
46 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
46 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
47 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
47 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
48 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
48 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
49 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
49 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
50 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
50 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
51 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
51 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
52 | // + POSSIBILITY OF SUCH DAMAGE. |
52 | // + POSSIBILITY OF SUCH DAMAGE. |
53 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
53 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
54 | #include <stdlib.h> |
54 | #include <stdlib.h> |
55 | #include <avr/io.h> |
55 | #include <avr/io.h> |
56 | 56 | ||
57 | #include "main.h" |
57 | #include "main.h" |
58 | #include "eeprom.h" |
58 | #include "eeprom.h" |
59 | #include "timer0.h" |
59 | #include "timer0.h" |
60 | #include "analog.h" |
60 | #include "analog.h" |
61 | #include "fc.h" |
61 | #include "fc.h" |
62 | #include "uart0.h" |
62 | #include "uart0.h" |
63 | #include "rc.h" |
63 | #include "rc.h" |
64 | #include "twimaster.h" |
64 | #include "twimaster.h" |
65 | #include "timer2.h" |
65 | #include "timer2.h" |
66 | #ifdef USE_KILLAGREG |
66 | #ifdef USE_KILLAGREG |
67 | #include "mm3.h" |
67 | #include "mm3.h" |
68 | #include "gps.h" |
68 | #include "gps.h" |
69 | #endif |
69 | #endif |
70 | #ifdef USE_MK3MAG |
70 | #ifdef USE_MK3MAG |
71 | #include "mk3mag.h" |
71 | #include "mk3mag.h" |
72 | #include "gps.h" |
72 | #include "gps.h" |
73 | #endif |
73 | #endif |
74 | #include "led.h" |
74 | #include "led.h" |
75 | #ifdef USE_NAVICTRL |
75 | #ifdef USE_NAVICTRL |
76 | #include "spi.h" |
76 | #include "spi.h" |
77 | #endif |
77 | #endif |
78 | 78 | ||
79 | 79 | ||
80 | #define STICK_GAIN 4 |
80 | #define STICK_GAIN 4 |
81 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
81 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
82 | 82 | ||
83 | // gyro readings |
83 | // gyro readings |
84 | int16_t GyroNick, GyroRoll, GyroYaw; |
84 | int16_t GyroNick, GyroRoll, GyroYaw; |
85 | 85 | ||
86 | // gyro bias |
86 | // gyro bias |
87 | int16_t BiasHiResGyroNick = 0, BiasHiResGyroRoll = 0, AdBiasGyroYaw = 0; |
87 | int16_t BiasHiResGyroNick = 0, BiasHiResGyroRoll = 0, AdBiasGyroYaw = 0; |
88 | 88 | ||
89 | // accelerations |
89 | // accelerations |
90 | int16_t AccNick, AccRoll, AccTop; |
90 | int16_t AccNick, AccRoll, AccTop; |
91 | 91 | ||
92 | // neutral acceleration readings |
92 | // neutral acceleration readings |
93 | int16_t AdBiasAccNick = 0, AdBiasAccRoll = 0; |
93 | int16_t AdBiasAccNick = 0, AdBiasAccRoll = 0; |
94 | volatile float AdBiasAccTop = 0; |
94 | volatile float AdBiasAccTop = 0; |
95 | // the additive gyro rate corrections according to the axis coupling |
95 | // the additive gyro rate corrections according to the axis coupling |
96 | int16_t TrimNick, TrimRoll; |
96 | int16_t TrimNick, TrimRoll; |
97 | 97 | ||
98 | 98 | ||
99 | // attitude gyro integrals |
99 | // attitude gyro integrals |
100 | int32_t IntegralGyroNick = 0,IntegralGyroNick2 = 0; |
100 | int32_t IntegralGyroNick = 0,IntegralGyroNick2 = 0; |
101 | int32_t IntegralGyroRoll = 0,IntegralGyroRoll2 = 0; |
101 | int32_t IntegralGyroRoll = 0,IntegralGyroRoll2 = 0; |
102 | int32_t IntegralGyroYaw = 0; |
102 | int32_t IntegralGyroYaw = 0; |
103 | int32_t ReadingIntegralGyroNick = 0, ReadingIntegralGyroNick2 = 0; |
103 | int32_t ReadingIntegralGyroNick = 0, ReadingIntegralGyroNick2 = 0; |
104 | int32_t ReadingIntegralGyroRoll = 0, ReadingIntegralGyroRoll2 = 0; |
104 | int32_t ReadingIntegralGyroRoll = 0, ReadingIntegralGyroRoll2 = 0; |
105 | int32_t ReadingIntegralGyroYaw = 0; |
105 | int32_t ReadingIntegralGyroYaw = 0; |
106 | int32_t MeanIntegralGyroNick; |
106 | int32_t MeanIntegralGyroNick; |
107 | int32_t MeanIntegralGyroRoll; |
107 | int32_t MeanIntegralGyroRoll; |
108 | 108 | ||
109 | // attitude acceleration integrals |
109 | // attitude acceleration integrals |
110 | int32_t MeanAccNick = 0, MeanAccRoll = 0; |
110 | int32_t MeanAccNick = 0, MeanAccRoll = 0; |
111 | volatile int32_t ReadingIntegralTop = 0; |
111 | volatile int32_t ReadingIntegralTop = 0; |
112 | 112 | ||
113 | // compass course |
113 | // compass course |
114 | int16_t CompassHeading = -1; // negative angle indicates invalid data. |
114 | int16_t CompassHeading = -1; // negative angle indicates invalid data. |
115 | int16_t CompassCourse = -1; |
115 | int16_t CompassCourse = -1; |
116 | int16_t CompassOffCourse = 0; |
116 | int16_t CompassOffCourse = 0; |
117 | uint8_t CompassCalState = 0; |
117 | uint8_t CompassCalState = 0; |
118 | uint8_t FunnelCourse = 0; |
118 | uint8_t FunnelCourse = 0; |
119 | uint16_t BadCompassHeading = 500; |
119 | uint16_t BadCompassHeading = 500; |
120 | int32_t YawGyroHeading; // Yaw Gyro Integral supported by compass |
120 | int32_t YawGyroHeading; // Yaw Gyro Integral supported by compass |
121 | int16_t YawGyroDrift; |
121 | int16_t YawGyroDrift; |
122 | 122 | ||
123 | 123 | ||
124 | int16_t NaviAccNick = 0, NaviAccRoll = 0, NaviCntAcc = 0; |
124 | int16_t NaviAccNick = 0, NaviAccRoll = 0, NaviCntAcc = 0; |
125 | 125 | ||
126 | 126 | ||
127 | // MK flags |
127 | // MK flags |
128 | uint16_t ModelIsFlying = 0; |
128 | uint16_t ModelIsFlying = 0; |
129 | uint8_t MKFlags = 0; |
129 | uint8_t MKFlags = 0; |
130 | 130 | ||
131 | int32_t TurnOver180Nick = 250000L, TurnOver180Roll = 250000L; |
131 | int32_t TurnOver180Nick = 250000L, TurnOver180Roll = 250000L; |
132 | 132 | ||
133 | uint8_t GyroPFactor, GyroIFactor; // the PD factors for the attitude control |
133 | uint8_t GyroPFactor, GyroIFactor; // the PD factors for the attitude control |
134 | int16_t Ki = 10300 / 33; |
134 | int16_t Ki = 10300 / 33; |
135 | 135 | ||
136 | int16_t Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0; |
136 | int16_t Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0; |
137 | 137 | ||
138 | // setpoints for motors |
138 | // setpoints for motors |
139 | 139 | ||
140 | volatile uint8_t Motor1, Motor2, Motor3, Motor4, Motor5, Motor6, Motor7, Motor8; |
140 | volatile uint8_t Motor1, Motor2, Motor3, Motor4, Motor5, Motor6, Motor7, Motor8; |
141 | 141 | ||
142 | 142 | ||
143 | // stick values derived by rc channels readings |
143 | // stick values derived by rc channels readings |
144 | int16_t StickNick = 0, StickRoll = 0, StickYaw = 0, StickGas = 0; |
144 | int16_t StickNick = 0, StickRoll = 0, StickYaw = 0, StickGas = 0; |
145 | int16_t GPSStickNick = 0, GPSStickRoll = 0; |
145 | int16_t GPSStickNick = 0, GPSStickRoll = 0; |
146 | 146 | ||
147 | int16_t MaxStickNick = 0, MaxStickRoll = 0; |
147 | int16_t MaxStickNick = 0, MaxStickRoll = 0; |
148 | 148 | ||
149 | // stick values derived by uart inputs |
149 | // stick values derived by uart inputs |
150 | int16_t ExternStickNick = 0, ExternStickRoll = 0, ExternStickYaw = 0, ExternHeightValue = -20; |
150 | int16_t ExternStickNick = 0, ExternStickRoll = 0, ExternStickYaw = 0, ExternHeightValue = -20; |
151 | 151 | ||
152 | int16_t ReadingHeight = 0; |
152 | int16_t ReadingHeight = 0; |
153 | int16_t SetPointHeight = 0; |
153 | int16_t SetPointHeight = 0; |
154 | 154 | ||
155 | int16_t AttitudeCorrectionRoll = 0, AttitudeCorrectionNick = 0; |
155 | int16_t AttitudeCorrectionRoll = 0, AttitudeCorrectionNick = 0; |
156 | 156 | ||
157 | uint8_t LoopingNick = 0, LoopingRoll = 0; |
157 | uint8_t LoopingNick = 0, LoopingRoll = 0; |
158 | uint8_t LoopingLeft = 0, LoopingRight = 0, LoopingDown = 0, LoopingTop = 0; |
158 | uint8_t LoopingLeft = 0, LoopingRight = 0, LoopingDown = 0, LoopingTop = 0; |
159 | 159 | ||
160 | 160 | ||
161 | fc_param_t FCParam = {48,251,16,58,64,8,150,150,2,10,0,0,0,0,0,0,0,0,100,70,90,65,64,100}; |
161 | fc_param_t FCParam = {48,251,16,58,64,8,150,150,2,10,0,0,0,0,0,0,0,0,100,70,90,65,64,100}; |
162 | 162 | ||
163 | 163 | ||
164 | 164 | ||
165 | /************************************************************************/ |
165 | /************************************************************************/ |
166 | /* Filter for motor value smoothing */ |
166 | /* Filter for motor value smoothing */ |
167 | /************************************************************************/ |
167 | /************************************************************************/ |
168 | int16_t MotorSmoothing(int16_t newvalue, int16_t oldvalue) |
168 | int16_t MotorSmoothing(int16_t newvalue, int16_t oldvalue) |
169 | { |
169 | { |
170 | int16_t motor; |
170 | int16_t motor; |
171 | if(newvalue > oldvalue) motor = (1 * (int16_t)oldvalue + newvalue) / 2; //mean of old and new |
171 | if(newvalue > oldvalue) motor = (1 * (int16_t)oldvalue + newvalue) / 2; //mean of old and new |
172 | else motor = newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
172 | else motor = newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
173 | return(motor); |
173 | return(motor); |
174 | } |
174 | } |
175 | 175 | ||
176 | /************************************************************************/ |
176 | /************************************************************************/ |
177 | /* Creates numbeeps beeps at the speaker */ |
177 | /* Creates numbeeps beeps at the speaker */ |
178 | /************************************************************************/ |
178 | /************************************************************************/ |
179 | void Beep(uint8_t numbeeps) |
179 | void Beep(uint8_t numbeeps) |
180 | { |
180 | { |
181 | while(numbeeps--) |
181 | while(numbeeps--) |
182 | { |
182 | { |
183 | if(MKFlags & MKFLAG_MOTOR_RUN) return; //auf keinen Fall bei laufenden Motoren! |
183 | if(MKFlags & MKFLAG_MOTOR_RUN) return; //auf keinen Fall bei laufenden Motoren! |
184 | BeepTime = 100; // 0.1 second |
184 | BeepTime = 100; // 0.1 second |
185 | Delay_ms(250); // blocks 250 ms as pause to next beep, |
185 | Delay_ms(250); // blocks 250 ms as pause to next beep, |
186 | // this will block the flight control loop, |
186 | // this will block the flight control loop, |
187 | // therefore do not use this function if motors are running |
187 | // therefore do not use this function if motors are running |
188 | } |
188 | } |
189 | } |
189 | } |
190 | 190 | ||
191 | /************************************************************************/ |
191 | /************************************************************************/ |
192 | /* Neutral Readings */ |
192 | /* Neutral Readings */ |
193 | /************************************************************************/ |
193 | /************************************************************************/ |
194 | void SetNeutral(uint8_t AccAdjustment) |
194 | void SetNeutral(uint8_t AccAdjustment) |
195 | { |
195 | { |
196 | uint8_t i; |
196 | uint8_t i; |
197 | int32_t Sum_1, Sum_2 = 0, Sum_3; |
197 | int32_t Sum_1, Sum_2 = 0, Sum_3; |
198 | 198 | ||
199 | Servo_Off(); // disable servo output |
199 | Servo_Off(); // disable servo output |
200 | 200 | ||
201 | AdBiasAccNick = 0; |
201 | AdBiasAccNick = 0; |
202 | AdBiasAccRoll = 0; |
202 | AdBiasAccRoll = 0; |
203 | AdBiasAccTop = 0; |
203 | AdBiasAccTop = 0; |
204 | 204 | ||
205 | BiasHiResGyroNick = 0; |
205 | BiasHiResGyroNick = 0; |
206 | BiasHiResGyroRoll = 0; |
206 | BiasHiResGyroRoll = 0; |
207 | AdBiasGyroYaw = 0; |
207 | AdBiasGyroYaw = 0; |
208 | 208 | ||
209 | FCParam.AxisCoupling1 = 0; |
209 | FCParam.AxisCoupling1 = 0; |
210 | FCParam.AxisCoupling2 = 0; |
210 | FCParam.AxisCoupling2 = 0; |
211 | 211 | ||
212 | ExpandBaro = 0; |
212 | ExpandBaro = 0; |
213 | 213 | ||
214 | // sample values with bias set to zero |
214 | // sample values with bias set to zero |
215 | Delay_ms_Mess(100); |
215 | Delay_ms_Mess(100); |
216 | 216 | ||
217 | if(BoardRelease == 13) SearchDacGyroOffset(); |
217 | if(BoardRelease == 13) SearchDacGyroOffset(); |
218 | 218 | ||
219 | if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL)) // Height Control activated? |
219 | if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL)) // Height Control activated? |
220 | { |
220 | { |
221 | if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset(); |
221 | if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset(); |
222 | } |
222 | } |
223 | 223 | ||
224 | // determine gyro bias by averaging (require no rotation movement) |
224 | // determine gyro bias by averaging (require no rotation movement) |
225 | #define GYRO_BIAS_AVERAGE 32 |
225 | #define GYRO_BIAS_AVERAGE 32 |
226 | Sum_1 = 0; |
226 | Sum_1 = 0; |
227 | Sum_2 = 0; |
227 | Sum_2 = 0; |
228 | Sum_3 = 0; |
228 | Sum_3 = 0; |
229 | for(i=0; i < GYRO_BIAS_AVERAGE; i++) |
229 | for(i=0; i < GYRO_BIAS_AVERAGE; i++) |
230 | { |
230 | { |
231 | Delay_ms_Mess(10); |
231 | Delay_ms_Mess(10); |
232 | Sum_1 += AdValueGyroNick * HIRES_GYRO_AMPLIFY; |
232 | Sum_1 += AdValueGyroNick * HIRES_GYRO_AMPLIFY; |
233 | Sum_2 += AdValueGyroRoll * HIRES_GYRO_AMPLIFY; |
233 | Sum_2 += AdValueGyroRoll * HIRES_GYRO_AMPLIFY; |
234 | Sum_3 += AdValueGyroYaw; |
234 | Sum_3 += AdValueGyroYaw; |
235 | } |
235 | } |
236 | BiasHiResGyroNick = (int16_t)((Sum_1 + GYRO_BIAS_AVERAGE / 2) / GYRO_BIAS_AVERAGE); |
236 | BiasHiResGyroNick = (int16_t)((Sum_1 + GYRO_BIAS_AVERAGE / 2) / GYRO_BIAS_AVERAGE); |
237 | BiasHiResGyroRoll = (int16_t)((Sum_2 + GYRO_BIAS_AVERAGE / 2) / GYRO_BIAS_AVERAGE); |
237 | BiasHiResGyroRoll = (int16_t)((Sum_2 + GYRO_BIAS_AVERAGE / 2) / GYRO_BIAS_AVERAGE); |
238 | AdBiasGyroYaw = (int16_t)((Sum_3 + GYRO_BIAS_AVERAGE / 2) / GYRO_BIAS_AVERAGE); |
238 | AdBiasGyroYaw = (int16_t)((Sum_3 + GYRO_BIAS_AVERAGE / 2) / GYRO_BIAS_AVERAGE); |
239 | 239 | ||
240 | if(AccAdjustment) |
240 | if(AccAdjustment) |
241 | { |
241 | { |
242 | // determine acc bias by averaging (require horizontal adjustment in nick and roll attitude) |
242 | // determine acc bias by averaging (require horizontal adjustment in nick and roll attitude) |
243 | #define ACC_BIAS_AVERAGE 10 |
243 | #define ACC_BIAS_AVERAGE 10 |
244 | Sum_1 = 0; |
244 | Sum_1 = 0; |
245 | Sum_2 = 0; |
245 | Sum_2 = 0; |
246 | Sum_3 = 0; |
246 | Sum_3 = 0; |
247 | for(i=0; i < ACC_BIAS_AVERAGE; i++) |
247 | for(i=0; i < ACC_BIAS_AVERAGE; i++) |
248 | { |
248 | { |
249 | Delay_ms_Mess(10); |
249 | Delay_ms_Mess(10); |
250 | Sum_1 += AdValueAccNick; |
250 | Sum_1 += AdValueAccNick; |
251 | Sum_2 += AdValueAccRoll; |
251 | Sum_2 += AdValueAccRoll; |
252 | Sum_3 += AdValueAccZ; |
252 | Sum_3 += AdValueAccZ; |
253 | } |
253 | } |
254 | // use abs() to avoid negative bias settings because of adc sign flip in adc.c |
254 | // use abs() to avoid negative bias settings because of adc sign flip in adc.c |
255 | AdBiasAccNick = (int16_t)((abs(Sum_1) + ACC_BIAS_AVERAGE / 2) / ACC_BIAS_AVERAGE); |
255 | AdBiasAccNick = (int16_t)((abs(Sum_1) + ACC_BIAS_AVERAGE / 2) / ACC_BIAS_AVERAGE); |
256 | AdBiasAccRoll = (int16_t)((abs(Sum_2) + ACC_BIAS_AVERAGE / 2) / ACC_BIAS_AVERAGE); |
256 | AdBiasAccRoll = (int16_t)((abs(Sum_2) + ACC_BIAS_AVERAGE / 2) / ACC_BIAS_AVERAGE); |
257 | AdBiasAccTop = (int16_t)((abs(Sum_3) + ACC_BIAS_AVERAGE / 2) / ACC_BIAS_AVERAGE); |
257 | AdBiasAccTop = (int16_t)((abs(Sum_3) + ACC_BIAS_AVERAGE / 2) / ACC_BIAS_AVERAGE); |
258 | 258 | ||
259 | // Save ACC neutral settings to eeprom |
259 | // Save ACC neutral settings to eeprom |
260 | SetParamWord(PID_ACC_NICK, (uint16_t)AdBiasAccNick); |
260 | SetParamWord(PID_ACC_NICK, (uint16_t)AdBiasAccNick); |
261 | SetParamWord(PID_ACC_ROLL, (uint16_t)AdBiasAccRoll); |
261 | SetParamWord(PID_ACC_ROLL, (uint16_t)AdBiasAccRoll); |
262 | SetParamWord(PID_ACC_TOP, (uint16_t)AdBiasAccTop); |
262 | SetParamWord(PID_ACC_TOP, (uint16_t)AdBiasAccTop); |
263 | } |
263 | } |
264 | else // restore from eeprom |
264 | else // restore from eeprom |
265 | { |
265 | { |
266 | AdBiasAccNick = (int16_t)GetParamWord(PID_ACC_NICK); |
266 | AdBiasAccNick = (int16_t)GetParamWord(PID_ACC_NICK); |
267 | AdBiasAccRoll = (int16_t)GetParamWord(PID_ACC_ROLL); |
267 | AdBiasAccRoll = (int16_t)GetParamWord(PID_ACC_ROLL); |
268 | AdBiasAccTop = (int16_t)GetParamWord(PID_ACC_TOP); |
268 | AdBiasAccTop = (int16_t)GetParamWord(PID_ACC_TOP); |
269 | } |
269 | } |
270 | // setting acc bias values has an influence in the analog.c ISR |
270 | // setting acc bias values has an influence in the analog.c ISR |
271 | // therefore run measurement for 100ms to achive stable readings |
271 | // therefore run measurement for 100ms to achive stable readings |
272 | Delay_ms_Mess(100); |
272 | Delay_ms_Mess(100); |
273 | 273 | ||
274 | // reset acc averaging and integrals |
274 | // reset acc averaging and integrals |
275 | AccNick = ACC_AMPLIFY * (int32_t)AdValueAccNick; |
275 | AccNick = ACC_AMPLIFY * (int32_t)AdValueAccNick; |
276 | AccRoll = ACC_AMPLIFY * (int32_t)AdValueAccRoll; |
276 | AccRoll = ACC_AMPLIFY * (int32_t)AdValueAccRoll; |
277 | AccTop = AdValueAccTop; |
277 | AccTop = AdValueAccTop; |
278 | ReadingIntegralTop = AdValueAccTop; |
278 | ReadingIntegralTop = AdValueAccTop; |
279 | 279 | ||
280 | // and gyro readings |
280 | // and gyro readings |
281 | GyroNick = 0; |
281 | GyroNick = 0; |
282 | GyroRoll = 0; |
282 | GyroRoll = 0; |
283 | GyroYaw = 0; |
283 | GyroYaw = 0; |
284 | 284 | ||
285 | // reset gyro integrals to acc guessing |
285 | // reset gyro integrals to acc guessing |
286 | IntegralGyroNick = ParamSet.GyroAccFactor * (int32_t)AccNick; |
286 | IntegralGyroNick = ParamSet.GyroAccFactor * (int32_t)AccNick; |
287 | IntegralGyroRoll = ParamSet.GyroAccFactor * (int32_t)AccRoll; |
287 | IntegralGyroRoll = ParamSet.GyroAccFactor * (int32_t)AccRoll; |
288 | //ReadingIntegralGyroNick = IntegralGyroNick; |
288 | //ReadingIntegralGyroNick = IntegralGyroNick; |
289 | //ReadingIntegralGyroRoll = IntegralGyroRoll; |
289 | //ReadingIntegralGyroRoll = IntegralGyroRoll; |
290 | ReadingIntegralGyroNick2 = IntegralGyroNick; |
290 | ReadingIntegralGyroNick2 = IntegralGyroNick; |
291 | ReadingIntegralGyroRoll2 = IntegralGyroRoll; |
291 | ReadingIntegralGyroRoll2 = IntegralGyroRoll; |
292 | ReadingIntegralGyroYaw = 0; |
292 | ReadingIntegralGyroYaw = 0; |
293 | 293 | ||
294 | 294 | ||
295 | StartAirPressure = AirPressure; |
295 | StartAirPressure = AirPressure; |
296 | HeightD = 0; |
296 | HeightD = 0; |
297 | 297 | ||
298 | // update compass course to current heading |
298 | // update compass course to current heading |
299 | CompassCourse = CompassHeading; |
299 | CompassCourse = CompassHeading; |
300 | // Inititialize YawGyroIntegral value with current compass heading |
300 | // Inititialize YawGyroIntegral value with current compass heading |
301 | YawGyroHeading = (int32_t)CompassHeading * GYRO_DEG_FACTOR; |
301 | YawGyroHeading = (int32_t)CompassHeading * GYRO_DEG_FACTOR; |
302 | YawGyroDrift = 0; |
302 | YawGyroDrift = 0; |
303 | 303 | ||
304 | BeepTime = 50; |
304 | BeepTime = 50; |
305 | 305 | ||
306 | TurnOver180Nick = ((int32_t) ParamSet.AngleTurnOverNick * 2500L) +15000L; |
306 | TurnOver180Nick = ((int32_t) ParamSet.AngleTurnOverNick * 2500L) +15000L; |
307 | TurnOver180Roll = ((int32_t) ParamSet.AngleTurnOverRoll * 2500L) +15000L; |
307 | TurnOver180Roll = ((int32_t) ParamSet.AngleTurnOverRoll * 2500L) +15000L; |
308 | 308 | ||
309 | ExternHeightValue = 0; |
309 | ExternHeightValue = 0; |
310 | 310 | ||
311 | GPSStickNick = 0; |
311 | GPSStickNick = 0; |
312 | GPSStickRoll = 0; |
312 | GPSStickRoll = 0; |
313 | 313 | ||
314 | MKFlags |= MKFLAG_CALIBRATE; |
314 | MKFlags |= MKFLAG_CALIBRATE; |
315 | 315 | ||
316 | FCParam.KalmanK = -1; |
316 | FCParam.KalmanK = -1; |
317 | FCParam.KalmanMaxDrift = 0; |
317 | FCParam.KalmanMaxDrift = 0; |
318 | FCParam.KalmanMaxFusion = 32; |
318 | FCParam.KalmanMaxFusion = 32; |
319 | 319 | ||
320 | Poti1 = PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110; |
320 | Poti1 = PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110; |
321 | Poti2 = PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110; |
321 | Poti2 = PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110; |
322 | Poti3 = PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110; |
322 | Poti3 = PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110; |
323 | Poti4 = PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110; |
323 | Poti4 = PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110; |
324 | 324 | ||
325 | Servo_On(); //enable servo output |
325 | Servo_On(); //enable servo output |
326 | RC_Quality = 100; |
326 | RC_Quality = 100; |
327 | } |
327 | } |
328 | 328 | ||
329 | /************************************************************************/ |
329 | /************************************************************************/ |
330 | /* Averaging Measurement Readings */ |
330 | /* Averaging Measurement Readings */ |
331 | /************************************************************************/ |
331 | /************************************************************************/ |
332 | void Mean(void) |
332 | void Mean(void) |
333 | { |
333 | { |
334 | int32_t tmpl = 0, tmpl2 = 0, tmp13 = 0, tmp14 = 0; |
334 | int32_t tmpl = 0, tmpl2 = 0, tmp13 = 0, tmp14 = 0; |
335 | int16_t FilterGyroNick, FilterGyroRoll; |
335 | int16_t FilterGyroNick, FilterGyroRoll; |
336 | static int16_t Last_GyroRoll = 0, Last_GyroNick = 0; |
336 | static int16_t Last_GyroRoll = 0, Last_GyroNick = 0; |
337 | int16_t d2Nick, d2Roll; |
337 | int16_t d2Nick, d2Roll; |
338 | int32_t AngleNick, AngleRoll; |
338 | int32_t AngleNick, AngleRoll; |
339 | int16_t CouplingNickRoll = 0, CouplingRollNick = 0; |
339 | int16_t CouplingNickRoll = 0, CouplingRollNick = 0; |
340 | 340 | ||
341 | // Get bias free gyro readings |
341 | // Get bias free gyro readings |
342 | GyroNick = HiResGyroNick / HIRES_GYRO_AMPLIFY; // unfiltered gyro rate |
342 | GyroNick = HiResGyroNick / HIRES_GYRO_AMPLIFY; // unfiltered gyro rate |
343 | FilterGyroNick = FilterHiResGyroNick / HIRES_GYRO_AMPLIFY; // use filtered gyro rate |
343 | FilterGyroNick = FilterHiResGyroNick / HIRES_GYRO_AMPLIFY; // use filtered gyro rate |
344 | 344 | ||
345 | // handle rotation rates that violate adc ranges |
345 | // handle rotation rates that violate adc ranges |
346 | if(AdValueGyroNick < 15) GyroNick = -1000; |
346 | if(AdValueGyroNick < 15) GyroNick = -1000; |
347 | if(AdValueGyroNick < 7) GyroNick = -2000; |
347 | if(AdValueGyroNick < 7) GyroNick = -2000; |
348 | if(BoardRelease == 10) |
348 | if(BoardRelease == 10) |
349 | { |
349 | { |
350 | if(AdValueGyroNick > 1010) GyroNick = +1000; |
350 | if(AdValueGyroNick > 1010) GyroNick = +1000; |
351 | if(AdValueGyroNick > 1017) GyroNick = +2000; |
351 | if(AdValueGyroNick > 1017) GyroNick = +2000; |
352 | } |
352 | } |
353 | else |
353 | else |
354 | { |
354 | { |
355 | if(AdValueGyroNick > 2000) GyroNick = +1000; |
355 | if(AdValueGyroNick > 2000) GyroNick = +1000; |
356 | if(AdValueGyroNick > 2015) GyroNick = +2000; |
356 | if(AdValueGyroNick > 2015) GyroNick = +2000; |
357 | } |
357 | } |
358 | 358 | ||
359 | GyroRoll = HiResGyroRoll / HIRES_GYRO_AMPLIFY; // unfiltered gyro rate |
359 | GyroRoll = HiResGyroRoll / HIRES_GYRO_AMPLIFY; // unfiltered gyro rate |
360 | FilterGyroRoll = FilterHiResGyroRoll / HIRES_GYRO_AMPLIFY; // use filtered gyro rate |
360 | FilterGyroRoll = FilterHiResGyroRoll / HIRES_GYRO_AMPLIFY; // use filtered gyro rate |
361 | // handle rotation rates that violate adc ranges |
361 | // handle rotation rates that violate adc ranges |
362 | if(AdValueGyroRoll < 15) GyroRoll = -1000; |
362 | if(AdValueGyroRoll < 15) GyroRoll = -1000; |
363 | if(AdValueGyroRoll < 7) GyroRoll = -2000; |
363 | if(AdValueGyroRoll < 7) GyroRoll = -2000; |
364 | if(BoardRelease == 10) |
364 | if(BoardRelease == 10) |
365 | { |
365 | { |
366 | if(AdValueGyroRoll > 1010) GyroRoll = +1000; |
366 | if(AdValueGyroRoll > 1010) GyroRoll = +1000; |
367 | if(AdValueGyroRoll > 1017) GyroRoll = +2000; |
367 | if(AdValueGyroRoll > 1017) GyroRoll = +2000; |
368 | } |
368 | } |
369 | else |
369 | else |
370 | { |
370 | { |
371 | if(AdValueGyroRoll > 2000) GyroRoll = +1000; |
371 | if(AdValueGyroRoll > 2000) GyroRoll = +1000; |
372 | if(AdValueGyroRoll > 2015) GyroRoll = +2000; |
372 | if(AdValueGyroRoll > 2015) GyroRoll = +2000; |
373 | } |
373 | } |
374 | 374 | ||
375 | GyroYaw = AdBiasGyroYaw - AdValueGyroYaw; |
375 | GyroYaw = AdBiasGyroYaw - AdValueGyroYaw; |
376 | 376 | ||
377 | // Acceleration Sensor |
377 | // Acceleration Sensor |
378 | // lowpass acc measurement and scale AccNick/AccRoll by a factor of ACC_AMPLIFY to have a better resolution |
378 | // lowpass acc measurement and scale AccNick/AccRoll by a factor of ACC_AMPLIFY to have a better resolution |
379 | AccNick = ((int32_t)AccNick * 3 + ((ACC_AMPLIFY * (int32_t)AdValueAccNick))) / 4L; |
379 | AccNick = ((int32_t)AccNick * 3 + ((ACC_AMPLIFY * (int32_t)AdValueAccNick))) / 4L; |
380 | AccRoll = ((int32_t)AccRoll * 3 + ((ACC_AMPLIFY * (int32_t)AdValueAccRoll))) / 4L; |
380 | AccRoll = ((int32_t)AccRoll * 3 + ((ACC_AMPLIFY * (int32_t)AdValueAccRoll))) / 4L; |
381 | AccTop = ((int32_t)AccTop * 3 + ((int32_t)AdValueAccTop)) / 4L; |
381 | AccTop = ((int32_t)AccTop * 3 + ((int32_t)AdValueAccTop)) / 4L; |
382 | 382 | ||
383 | // sum acc sensor readings for later averaging |
383 | // sum acc sensor readings for later averaging |
384 | MeanAccNick += ACC_AMPLIFY * AdValueAccNick; |
384 | MeanAccNick += ACC_AMPLIFY * AdValueAccNick; |
385 | MeanAccRoll += ACC_AMPLIFY * AdValueAccRoll; |
385 | MeanAccRoll += ACC_AMPLIFY * AdValueAccRoll; |
386 | 386 | ||
387 | NaviAccNick += AdValueAccNick; |
387 | NaviAccNick += AdValueAccNick; |
388 | NaviAccRoll += AdValueAccRoll; |
388 | NaviAccRoll += AdValueAccRoll; |
389 | NaviCntAcc++; |
389 | NaviCntAcc++; |
390 | 390 | ||
391 | 391 | ||
392 | // enable ADC to meassure next readings, before that point all variables should be read that are written by the ADC ISR |
392 | // enable ADC to meassure next readings, before that point all variables should be read that are written by the ADC ISR |
393 | ADC_Enable(); |
393 | ADC_Enable(); |
394 | ADReady = 0; |
394 | ADReady = 0; |
395 | 395 | ||
396 | // limit angle readings for axis coupling calculations |
396 | // limit angle readings for axis coupling calculations |
397 | #define ANGLE_LIMIT 93000L // aprox. 93000/GYRO_DEG_FACTOR = 82 deg |
397 | #define ANGLE_LIMIT 93000L // aprox. 93000/GYRO_DEG_FACTOR = 82 deg |
398 | 398 | ||
399 | AngleNick = ReadingIntegralGyroNick; |
399 | AngleNick = ReadingIntegralGyroNick; |
400 | CHECK_MIN_MAX(AngleNick, -ANGLE_LIMIT, ANGLE_LIMIT); |
400 | CHECK_MIN_MAX(AngleNick, -ANGLE_LIMIT, ANGLE_LIMIT); |
401 | 401 | ||
402 | AngleRoll = ReadingIntegralGyroRoll; |
402 | AngleRoll = ReadingIntegralGyroRoll; |
403 | CHECK_MIN_MAX(AngleRoll, -ANGLE_LIMIT, ANGLE_LIMIT); |
403 | CHECK_MIN_MAX(AngleRoll, -ANGLE_LIMIT, ANGLE_LIMIT); |
404 | 404 | ||
405 | 405 | ||
406 | // Yaw |
406 | // Yaw |
407 | // calculate yaw gyro integral (~ to rotation angle) |
407 | // calculate yaw gyro integral (~ to rotation angle) |
408 | YawGyroHeading += GyroYaw; |
408 | YawGyroHeading += GyroYaw; |
409 | ReadingIntegralGyroYaw += GyroYaw; |
409 | ReadingIntegralGyroYaw += GyroYaw; |
410 | 410 | ||
411 | 411 | ||
412 | // Coupling fraction |
412 | // Coupling fraction |
413 | if(! LoopingNick && !LoopingRoll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) |
413 | if(! LoopingNick && !LoopingRoll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) |
414 | { |
414 | { |
415 | tmp13 = (FilterGyroRoll * AngleNick) / 2048L; |
415 | tmp13 = (FilterGyroRoll * AngleNick) / 2048L; |
416 | tmp13 *= FCParam.AxisCoupling2; // 65 |
416 | tmp13 *= FCParam.AxisCoupling2; // 65 |
417 | tmp13 /= 4096L; |
417 | tmp13 /= 4096L; |
418 | CouplingNickRoll = tmp13; |
418 | CouplingNickRoll = tmp13; |
419 | 419 | ||
420 | tmp14 = (FilterGyroNick * AngleRoll) / 2048L; |
420 | tmp14 = (FilterGyroNick * AngleRoll) / 2048L; |
421 | tmp14 *= FCParam.AxisCoupling2; // 65 |
421 | tmp14 *= FCParam.AxisCoupling2; // 65 |
422 | tmp14 /= 4096L; |
422 | tmp14 /= 4096L; |
423 | CouplingRollNick = tmp14; |
423 | CouplingRollNick = tmp14; |
424 | 424 | ||
425 | tmp14 -= tmp13; |
425 | tmp14 -= tmp13; |
426 | YawGyroHeading += tmp14; |
426 | YawGyroHeading += tmp14; |
427 | if(!FCParam.AxisCouplingYawCorrection) ReadingIntegralGyroYaw -= tmp14 / 2; // force yaw |
427 | if(!FCParam.AxisCouplingYawCorrection) ReadingIntegralGyroYaw -= tmp14 / 2; // force yaw |
428 | 428 | ||
429 | tmpl = ((GyroYaw + tmp14) * AngleNick) / 2048L; |
429 | tmpl = ((GyroYaw + tmp14) * AngleNick) / 2048L; |
430 | tmpl *= FCParam.AxisCoupling1; |
430 | tmpl *= FCParam.AxisCoupling1; |
431 | tmpl /= 4096L; |
431 | tmpl /= 4096L; |
432 | 432 | ||
433 | tmpl2 = ((GyroYaw + tmp14) * AngleRoll) / 2048L; |
433 | tmpl2 = ((GyroYaw + tmp14) * AngleRoll) / 2048L; |
434 | tmpl2 *= FCParam.AxisCoupling1; |
434 | tmpl2 *= FCParam.AxisCoupling1; |
435 | tmpl2 /= 4096L; |
435 | tmpl2 /= 4096L; |
436 | if(labs(tmpl) > 128 || labs(tmpl2) > 128) FunnelCourse = 1; |
436 | if(labs(tmpl) > 128 || labs(tmpl2) > 128) FunnelCourse = 1; |
437 | 437 | ||
438 | TrimNick = -tmpl2 + tmpl / 100L; |
438 | TrimNick = -tmpl2 + tmpl / 100L; |
439 | TrimRoll = tmpl - tmpl2 / 100L; |
439 | TrimRoll = tmpl - tmpl2 / 100L; |
440 | } |
440 | } |
441 | else |
441 | else |
442 | { |
442 | { |
443 | CouplingNickRoll = 0; |
443 | CouplingNickRoll = 0; |
444 | CouplingRollNick = 0; |
444 | CouplingRollNick = 0; |
445 | TrimNick = 0; |
445 | TrimNick = 0; |
446 | TrimRoll = 0; |
446 | TrimRoll = 0; |
447 | } |
447 | } |
448 | 448 | ||
449 | 449 | ||
450 | // Yaw |
450 | // Yaw |
451 | 451 | ||
452 | // limit YawGyroHeading proportional to 0° to 360° |
452 | // limit YawGyroHeading proportional to 0° to 360° |
453 | if(YawGyroHeading >= (360L * GYRO_DEG_FACTOR)) YawGyroHeading -= 360L * GYRO_DEG_FACTOR; // 360° Wrap |
453 | if(YawGyroHeading >= (360L * GYRO_DEG_FACTOR)) YawGyroHeading -= 360L * GYRO_DEG_FACTOR; // 360° Wrap |
454 | if(YawGyroHeading < 0) YawGyroHeading += 360L * GYRO_DEG_FACTOR; |
454 | if(YawGyroHeading < 0) YawGyroHeading += 360L * GYRO_DEG_FACTOR; |
455 | 455 | ||
456 | // Roll |
456 | // Roll |
457 | ReadingIntegralGyroRoll2 += FilterGyroRoll + TrimRoll; |
457 | ReadingIntegralGyroRoll2 += FilterGyroRoll + TrimRoll; |
458 | ReadingIntegralGyroRoll += FilterGyroRoll + TrimRoll- AttitudeCorrectionRoll; |
458 | ReadingIntegralGyroRoll += FilterGyroRoll + TrimRoll- AttitudeCorrectionRoll; |
459 | if(ReadingIntegralGyroRoll > TurnOver180Roll) |
459 | if(ReadingIntegralGyroRoll > TurnOver180Roll) |
460 | { |
460 | { |
461 | ReadingIntegralGyroRoll = -(TurnOver180Roll - 10000L); |
461 | ReadingIntegralGyroRoll = -(TurnOver180Roll - 10000L); |
462 | ReadingIntegralGyroRoll2 = ReadingIntegralGyroRoll; |
462 | ReadingIntegralGyroRoll2 = ReadingIntegralGyroRoll; |
463 | } |
463 | } |
464 | if(ReadingIntegralGyroRoll < -TurnOver180Roll) |
464 | if(ReadingIntegralGyroRoll < -TurnOver180Roll) |
465 | { |
465 | { |
466 | ReadingIntegralGyroRoll = (TurnOver180Roll - 10000L); |
466 | ReadingIntegralGyroRoll = (TurnOver180Roll - 10000L); |
467 | ReadingIntegralGyroRoll2 = ReadingIntegralGyroRoll; |
467 | ReadingIntegralGyroRoll2 = ReadingIntegralGyroRoll; |
468 | } |
468 | } |
469 | 469 | ||
470 | // Nick |
470 | // Nick |
471 | ReadingIntegralGyroNick2 += FilterGyroNick + TrimNick; |
471 | ReadingIntegralGyroNick2 += FilterGyroNick + TrimNick; |
472 | ReadingIntegralGyroNick += FilterGyroNick + TrimNick - AttitudeCorrectionNick; |
472 | ReadingIntegralGyroNick += FilterGyroNick + TrimNick - AttitudeCorrectionNick; |
473 | if(ReadingIntegralGyroNick > TurnOver180Nick) |
473 | if(ReadingIntegralGyroNick > TurnOver180Nick) |
474 | { |
474 | { |
475 | ReadingIntegralGyroNick = -(TurnOver180Nick - 25000L); |
475 | ReadingIntegralGyroNick = -(TurnOver180Nick - 25000L); |
476 | ReadingIntegralGyroNick2 = ReadingIntegralGyroNick; |
476 | ReadingIntegralGyroNick2 = ReadingIntegralGyroNick; |
477 | } |
477 | } |
478 | if(ReadingIntegralGyroNick < -TurnOver180Nick) |
478 | if(ReadingIntegralGyroNick < -TurnOver180Nick) |
479 | { |
479 | { |
480 | ReadingIntegralGyroNick = (TurnOver180Nick - 25000L); |
480 | ReadingIntegralGyroNick = (TurnOver180Nick - 25000L); |
481 | ReadingIntegralGyroNick2 = ReadingIntegralGyroNick; |
481 | ReadingIntegralGyroNick2 = ReadingIntegralGyroNick; |
482 | } |
482 | } |
483 | 483 | ||
484 | IntegralGyroYaw = ReadingIntegralGyroYaw; |
484 | IntegralGyroYaw = ReadingIntegralGyroYaw; |
485 | IntegralGyroNick = ReadingIntegralGyroNick; |
485 | IntegralGyroNick = ReadingIntegralGyroNick; |
486 | IntegralGyroRoll = ReadingIntegralGyroRoll; |
486 | IntegralGyroRoll = ReadingIntegralGyroRoll; |
487 | IntegralGyroNick2 = ReadingIntegralGyroNick2; |
487 | IntegralGyroNick2 = ReadingIntegralGyroNick2; |
488 | IntegralGyroRoll2 = ReadingIntegralGyroRoll2; |
488 | IntegralGyroRoll2 = ReadingIntegralGyroRoll2; |
489 | 489 | ||
490 | 490 | ||
491 | #define D_LIMIT 128 |
491 | #define D_LIMIT 128 |
492 | 492 | ||
493 | if(FCParam.GyroD) |
493 | if(FCParam.GyroD) |
494 | { |
494 | { |
495 | d2Nick = (HiResGyroNick - Last_GyroNick); // change of gyro rate |
495 | d2Nick = (HiResGyroNick - Last_GyroNick); // change of gyro rate |
496 | Last_GyroNick = (Last_GyroNick + HiResGyroNick) / 2; |
496 | Last_GyroNick = (Last_GyroNick + HiResGyroNick) / 2; |
497 | CHECK_MIN_MAX(d2Nick, -D_LIMIT, D_LIMIT); |
497 | CHECK_MIN_MAX(d2Nick, -D_LIMIT, D_LIMIT); |
498 | GyroNick += (d2Nick * (int16_t)FCParam.GyroD) / 16; |
498 | GyroNick += (d2Nick * (int16_t)FCParam.GyroD) / 16; |
499 | 499 | ||
500 | d2Roll = (HiResGyroRoll - Last_GyroRoll); // change of gyro rate |
500 | d2Roll = (HiResGyroRoll - Last_GyroRoll); // change of gyro rate |
501 | Last_GyroRoll = (Last_GyroRoll + HiResGyroRoll) / 2; |
501 | Last_GyroRoll = (Last_GyroRoll + HiResGyroRoll) / 2; |
502 | CHECK_MIN_MAX(d2Roll, -D_LIMIT, D_LIMIT); |
502 | CHECK_MIN_MAX(d2Roll, -D_LIMIT, D_LIMIT); |
503 | GyroRoll += (d2Roll * (int16_t)FCParam.GyroD) / 16; |
503 | GyroRoll += (d2Roll * (int16_t)FCParam.GyroD) / 16; |
504 | 504 | ||
505 | HiResGyroNick += (d2Nick * (int16_t)FCParam.GyroD); |
505 | HiResGyroNick += (d2Nick * (int16_t)FCParam.GyroD); |
506 | HiResGyroRoll += (d2Roll * (int16_t)FCParam.GyroD); |
506 | HiResGyroRoll += (d2Roll * (int16_t)FCParam.GyroD); |
507 | } |
507 | } |
508 | 508 | ||
509 | // Increase the roll/nick rate virtually proportional to the coupling to suppress a faster rotation |
509 | // Increase the roll/nick rate virtually proportional to the coupling to suppress a faster rotation |
510 | if(FilterGyroNick > 0) TrimNick += ((int32_t)abs(CouplingRollNick) * FCParam.AxisCouplingYawCorrection) / 64L; |
510 | if(FilterGyroNick > 0) TrimNick += ((int32_t)abs(CouplingRollNick) * FCParam.AxisCouplingYawCorrection) / 64L; |
511 | else TrimNick -= ((int32_t)abs(CouplingRollNick) * FCParam.AxisCouplingYawCorrection) / 64L; |
511 | else TrimNick -= ((int32_t)abs(CouplingRollNick) * FCParam.AxisCouplingYawCorrection) / 64L; |
512 | if(FilterGyroRoll > 0) TrimRoll += ((int32_t)abs(CouplingNickRoll) * FCParam.AxisCouplingYawCorrection) / 64L; |
512 | if(FilterGyroRoll > 0) TrimRoll += ((int32_t)abs(CouplingNickRoll) * FCParam.AxisCouplingYawCorrection) / 64L; |
513 | else TrimRoll -= ((int32_t)abs(CouplingNickRoll) * FCParam.AxisCouplingYawCorrection) / 64L; |
513 | else TrimRoll -= ((int32_t)abs(CouplingNickRoll) * FCParam.AxisCouplingYawCorrection) / 64L; |
514 | 514 | ||
515 | // increase the nick/roll rates virtually from the threshold of 245 to slow down higher rotation rates |
515 | // increase the nick/roll rates virtually from the threshold of 245 to slow down higher rotation rates |
516 | if((ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER) && ! LoopingNick && !LoopingRoll) |
516 | if((ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER) && ! LoopingNick && !LoopingRoll) |
517 | { |
517 | { |
518 | if(FilterGyroNick > 256) GyroNick += 1 * (FilterGyroNick - 256); |
518 | if(FilterGyroNick > 256) GyroNick += 1 * (FilterGyroNick - 256); |
519 | else if(FilterGyroNick < -256) GyroNick += 1 * (FilterGyroNick + 256); |
519 | else if(FilterGyroNick < -256) GyroNick += 1 * (FilterGyroNick + 256); |
520 | if(FilterGyroRoll > 256) GyroRoll += 1 * (FilterGyroRoll - 256); |
520 | if(FilterGyroRoll > 256) GyroRoll += 1 * (FilterGyroRoll - 256); |
521 | else if(FilterGyroRoll < -256) GyroRoll += 1 * (FilterGyroRoll + 256); |
521 | else if(FilterGyroRoll < -256) GyroRoll += 1 * (FilterGyroRoll + 256); |
522 | } |
522 | } |
523 | 523 | ||
524 | } |
524 | } |
525 | 525 | ||
526 | 526 | ||
527 | /************************************************************************/ |
527 | /************************************************************************/ |
528 | /* Transmit Motor Data via I2C */ |
528 | /* Transmit Motor Data via I2C */ |
529 | /************************************************************************/ |
529 | /************************************************************************/ |
530 | void SendMotorData(void) |
530 | void SendMotorData(void) |
531 | { |
531 | { |
532 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) |
532 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) |
533 | { |
533 | { |
534 | #ifdef USE_QUADRO |
534 | #ifdef USE_QUADRO |
535 | Motor1 = 0; |
535 | Motor1 = 0; |
536 | Motor2 = 0; |
536 | Motor2 = 0; |
537 | Motor3 = 0; |
537 | Motor3 = 0; |
538 | Motor4 = 0; |
538 | Motor4 = 0; |
539 | if(MotorTest[0]) Motor1 = MotorTest[0]; |
539 | if(MotorTest[0]) Motor1 = MotorTest[0]; |
540 | if(MotorTest[1]) Motor2 = MotorTest[1]; |
540 | if(MotorTest[1]) Motor2 = MotorTest[1]; |
541 | if(MotorTest[2]) Motor3 = MotorTest[2]; |
541 | if(MotorTest[2]) Motor3 = MotorTest[2]; |
542 | if(MotorTest[3]) Motor4 = MotorTest[3]; |
542 | if(MotorTest[3]) Motor4 = MotorTest[3]; |
543 | #else |
543 | #else |
544 | Motor1 = 0; |
544 | Motor1 = 0; |
545 | Motor2 = 0; |
545 | Motor2 = 0; |
546 | Motor3 = 0; |
546 | Motor3 = 0; |
547 | Motor4 = 0; |
547 | Motor4 = 0; |
548 | Motor5 = 0; |
548 | Motor5 = 0; |
549 | Motor6 = 0; |
549 | Motor6 = 0; |
550 | Motor7 = 0; |
550 | Motor7 = 0; |
551 | Motor8 = 0; |
551 | Motor8 = 0; |
552 | if(MotorTest[0]) {Motor1 = MotorTest[0]; Motor2 = MotorTest[0];} |
552 | if(MotorTest[0]) {Motor1 = MotorTest[0]; Motor2 = MotorTest[0];} |
553 | if(MotorTest[3]) {Motor3 = MotorTest[3]; Motor4 = MotorTest[3];} |
553 | if(MotorTest[3]) {Motor3 = MotorTest[3]; Motor4 = MotorTest[3];} |
554 | if(MotorTest[1]) {Motor5 = MotorTest[1]; Motor6 = MotorTest[1];} |
554 | if(MotorTest[1]) {Motor5 = MotorTest[1]; Motor6 = MotorTest[1];} |
555 | if(MotorTest[2]) {Motor7 = MotorTest[2]; Motor8 = MotorTest[2];} |
555 | if(MotorTest[2]) {Motor7 = MotorTest[2]; Motor8 = MotorTest[2];} |
556 | 556 | ||
557 | #endif |
557 | #endif |
558 | MKFlags &= ~(MKFLAG_FLY|MKFLAG_START); // clear flag FLY and START if motors are off |
558 | MKFlags &= ~(MKFLAG_FLY|MKFLAG_START); // clear flag FLY and START if motors are off |
559 | } |
559 | } |
560 | #ifdef USE_QUADRO |
560 | #ifdef USE_QUADRO |
561 | 561 | ||
562 | DebugOut.Analog[12] = Motor1; // Front |
562 | DebugOut.Analog[12] = Motor1; // Front |
563 | DebugOut.Analog[13] = Motor2; // Rear |
563 | DebugOut.Analog[13] = Motor2; // Rear |
564 | DebugOut.Analog[14] = Motor4; // Left |
564 | DebugOut.Analog[14] = Motor4; // Left |
565 | DebugOut.Analog[15] = Motor3; // Right |
565 | DebugOut.Analog[15] = Motor3; // Right |
566 | #else // OCTO Motor addresses are counted clockwise starting at the head |
566 | #else // OCTO Motor addresses are counted clockwise starting at the head |
567 | DebugOut.Analog[12] = (Motor1 + Motor2) / 2; |
567 | DebugOut.Analog[12] = (Motor1 + Motor2) / 2; |
568 | DebugOut.Analog[13] = (Motor5 + Motor6) / 2; |
568 | DebugOut.Analog[13] = (Motor5 + Motor6) / 2; |
569 | DebugOut.Analog[14] = (Motor7 + Motor8) / 2; |
569 | DebugOut.Analog[14] = (Motor7 + Motor8) / 2; |
570 | DebugOut.Analog[15] = (Motor3 + Motor4) / 2; |
570 | DebugOut.Analog[15] = (Motor3 + Motor4) / 2; |
571 | #endif |
571 | #endif |
572 | //Start I2C Interrupt Mode |
572 | //Start I2C Interrupt Mode |
573 | twi_state = TWI_STATE_MOTOR_TX; |
573 | twi_state = TWI_STATE_MOTOR_TX; |
574 | I2C_Start(); |
574 | I2C_Start(); |
575 | } |
575 | } |
576 | 576 | ||
577 | 577 | ||
578 | 578 | ||
579 | /************************************************************************/ |
579 | /************************************************************************/ |
580 | /* Map the parameter to poti values */ |
580 | /* Map the parameter to poti values */ |
581 | /************************************************************************/ |
581 | /************************************************************************/ |
582 | void ParameterMapping(void) |
582 | void ParameterMapping(void) |
583 | { |
583 | { |
584 | if(RC_Quality > 160) // do the mapping of RC-Potis only if the rc-signal is ok |
584 | if(RC_Quality > 160) // do the mapping of RC-Potis only if the rc-signal is ok |
585 | // else the last updated values are used |
585 | // else the last updated values are used |
586 | { |
586 | { |
587 | //update poti values by rc-signals |
587 | //update poti values by rc-signals |
588 | #define CHK_POTI_MM(b,a,min,max) { if(a > 250) { if(a == 251) b = Poti1; else if(a == 252) b = Poti2; else if(a == 253) b = Poti3; else if(a == 254) b = Poti4;} else b = a; if(b <= min) b = min; else if(b >= max) b = max;} |
588 | #define CHK_POTI_MM(b,a,min,max) { if(a > 250) { if(a == 251) b = Poti1; else if(a == 252) b = Poti2; else if(a == 253) b = Poti3; else if(a == 254) b = Poti4;} else b = a; if(b <= min) b = min; else if(b >= max) b = max;} |
589 | #define CHK_POTI(b,a) { if(a > 250) { if(a == 251) b = Poti1; else if(a == 252) b = Poti2; else if(a == 253) b = Poti3; else if(a == 254) b = Poti4;} else b = a;} |
589 | #define CHK_POTI(b,a) { if(a > 250) { if(a == 251) b = Poti1; else if(a == 252) b = Poti2; else if(a == 253) b = Poti3; else if(a == 254) b = Poti4;} else b = a;} |
590 | CHK_POTI(FCParam.MaxHeight,ParamSet.MaxHeight); |
590 | CHK_POTI(FCParam.MaxHeight,ParamSet.MaxHeight); |
591 | CHK_POTI_MM(FCParam.HeightD,ParamSet.HeightD,0,100); |
591 | CHK_POTI_MM(FCParam.HeightD,ParamSet.HeightD,0,100); |
592 | CHK_POTI_MM(FCParam.HeightP,ParamSet.HeightP,0,100); |
592 | CHK_POTI_MM(FCParam.HeightP,ParamSet.HeightP,0,100); |
593 | CHK_POTI(FCParam.Height_ACC_Effect,ParamSet.Height_ACC_Effect); |
593 | CHK_POTI(FCParam.Height_ACC_Effect,ParamSet.Height_ACC_Effect); |
594 | CHK_POTI(FCParam.CompassYawEffect,ParamSet.CompassYawEffect); |
594 | CHK_POTI(FCParam.CompassYawEffect,ParamSet.CompassYawEffect); |
595 | CHK_POTI_MM(FCParam.GyroP,ParamSet.GyroP,10,255); |
595 | CHK_POTI_MM(FCParam.GyroP,ParamSet.GyroP,10,255); |
596 | CHK_POTI(FCParam.GyroI,ParamSet.GyroI); |
596 | CHK_POTI(FCParam.GyroI,ParamSet.GyroI); |
597 | CHK_POTI(FCParam.GyroD,ParamSet.GyroD); |
597 | CHK_POTI(FCParam.GyroD,ParamSet.GyroD); |
598 | CHK_POTI(FCParam.IFactor,ParamSet.IFactor); |
598 | CHK_POTI(FCParam.IFactor,ParamSet.IFactor); |
599 | CHK_POTI(FCParam.UserParam1,ParamSet.UserParam1); |
599 | CHK_POTI(FCParam.UserParam1,ParamSet.UserParam1); |
600 | CHK_POTI(FCParam.UserParam2,ParamSet.UserParam2); |
600 | CHK_POTI(FCParam.UserParam2,ParamSet.UserParam2); |
601 | CHK_POTI(FCParam.UserParam3,ParamSet.UserParam3); |
601 | CHK_POTI(FCParam.UserParam3,ParamSet.UserParam3); |
602 | CHK_POTI(FCParam.UserParam4,ParamSet.UserParam4); |
602 | CHK_POTI(FCParam.UserParam4,ParamSet.UserParam4); |
603 | CHK_POTI(FCParam.UserParam5,ParamSet.UserParam5); |
603 | CHK_POTI(FCParam.UserParam5,ParamSet.UserParam5); |
604 | CHK_POTI(FCParam.UserParam6,ParamSet.UserParam6); |
604 | CHK_POTI(FCParam.UserParam6,ParamSet.UserParam6); |
605 | CHK_POTI(FCParam.UserParam7,ParamSet.UserParam7); |
605 | CHK_POTI(FCParam.UserParam7,ParamSet.UserParam7); |
606 | CHK_POTI(FCParam.UserParam8,ParamSet.UserParam8); |
606 | CHK_POTI(FCParam.UserParam8,ParamSet.UserParam8); |
607 | CHK_POTI(FCParam.ServoNickControl,ParamSet.ServoNickControl); |
607 | CHK_POTI(FCParam.ServoNickControl,ParamSet.ServoNickControl); |
608 | CHK_POTI(FCParam.LoopGasLimit,ParamSet.LoopGasLimit); |
608 | CHK_POTI(FCParam.LoopGasLimit,ParamSet.LoopGasLimit); |
609 | CHK_POTI(FCParam.AxisCoupling1,ParamSet.AxisCoupling1); |
609 | CHK_POTI(FCParam.AxisCoupling1,ParamSet.AxisCoupling1); |
610 | CHK_POTI(FCParam.AxisCoupling2,ParamSet.AxisCoupling2); |
610 | CHK_POTI(FCParam.AxisCoupling2,ParamSet.AxisCoupling2); |
611 | CHK_POTI(FCParam.AxisCouplingYawCorrection,ParamSet.AxisCouplingYawCorrection); |
611 | CHK_POTI(FCParam.AxisCouplingYawCorrection,ParamSet.AxisCouplingYawCorrection); |
612 | CHK_POTI(FCParam.DynamicStability,ParamSet.DynamicStability); |
612 | CHK_POTI(FCParam.DynamicStability,ParamSet.DynamicStability); |
613 | CHK_POTI_MM(FCParam.J16Timing,ParamSet.J16Timing,1,255); |
613 | CHK_POTI_MM(FCParam.J16Timing,ParamSet.J16Timing,1,255); |
614 | CHK_POTI_MM(FCParam.J17Timing,ParamSet.J17Timing,1,255); |
614 | CHK_POTI_MM(FCParam.J17Timing,ParamSet.J17Timing,1,255); |
615 | #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG)) |
615 | #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG)) |
616 | CHK_POTI(FCParam.NaviGpsModeControl,ParamSet.NaviGpsModeControl); |
616 | CHK_POTI(FCParam.NaviGpsModeControl,ParamSet.NaviGpsModeControl); |
617 | CHK_POTI(FCParam.NaviGpsGain,ParamSet.NaviGpsGain); |
617 | CHK_POTI(FCParam.NaviGpsGain,ParamSet.NaviGpsGain); |
618 | CHK_POTI(FCParam.NaviGpsP,ParamSet.NaviGpsP); |
618 | CHK_POTI(FCParam.NaviGpsP,ParamSet.NaviGpsP); |
619 | CHK_POTI(FCParam.NaviGpsI,ParamSet.NaviGpsI); |
619 | CHK_POTI(FCParam.NaviGpsI,ParamSet.NaviGpsI); |
620 | CHK_POTI(FCParam.NaviGpsD,ParamSet.NaviGpsD); |
620 | CHK_POTI(FCParam.NaviGpsD,ParamSet.NaviGpsD); |
621 | CHK_POTI(FCParam.NaviGpsACC,ParamSet.NaviGpsACC); |
621 | CHK_POTI(FCParam.NaviGpsACC,ParamSet.NaviGpsACC); |
622 | CHK_POTI_MM(FCParam.NaviOperatingRadius,ParamSet.NaviOperatingRadius,10, 255); |
622 | CHK_POTI_MM(FCParam.NaviOperatingRadius,ParamSet.NaviOperatingRadius,10, 255); |
623 | CHK_POTI(FCParam.NaviWindCorrection,ParamSet.NaviWindCorrection); |
623 | CHK_POTI(FCParam.NaviWindCorrection,ParamSet.NaviWindCorrection); |
624 | CHK_POTI(FCParam.NaviSpeedCompensation,ParamSet.NaviSpeedCompensation); |
624 | CHK_POTI(FCParam.NaviSpeedCompensation,ParamSet.NaviSpeedCompensation); |
625 | #endif |
625 | #endif |
626 | CHK_POTI(FCParam.ExternalControl,ParamSet.ExternalControl); |
626 | CHK_POTI(FCParam.ExternalControl,ParamSet.ExternalControl); |
627 | Ki = 10300 / ( FCParam.IFactor + 1 ); |
627 | Ki = 10300 / ( FCParam.IFactor + 1 ); |
628 | } |
628 | } |
629 | } |
629 | } |
630 | 630 | ||
631 | 631 | ||
632 | void SetCompassCalState(void) |
632 | void SetCompassCalState(void) |
633 | { |
633 | { |
634 | static uint8_t stick = 1; |
634 | static uint8_t stick = 1; |
635 | 635 | ||
636 | // if nick is centered or top set stick to zero |
636 | // if nick is centered or top set stick to zero |
637 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -20) stick = 0; |
637 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -20) stick = 0; |
638 | // if nick is down trigger to next cal state |
638 | // if nick is down trigger to next cal state |
639 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70) && !stick) |
639 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70) && !stick) |
640 | { |
640 | { |
641 | stick = 1; |
641 | stick = 1; |
642 | CompassCalState++; |
642 | CompassCalState++; |
643 | if(CompassCalState < 5) Beep(CompassCalState); |
643 | if(CompassCalState < 5) Beep(CompassCalState); |
644 | else BeepTime = 1000; |
644 | else BeepTime = 1000; |
645 | } |
645 | } |
646 | } |
646 | } |
647 | 647 | ||
648 | 648 | ||
649 | 649 | ||
650 | /************************************************************************/ |
650 | /************************************************************************/ |
651 | /* MotorControl */ |
651 | /* MotorControl */ |
652 | /************************************************************************/ |
652 | /************************************************************************/ |
653 | void MotorControl(void) |
653 | void MotorControl(void) |
654 | { |
654 | { |
655 | int16_t MotorValue, h, tmp_int; |
655 | int16_t MotorValue, h, tmp_int; |
656 | 656 | ||
657 | // Mixer Fractions that are combined for Motor Control |
657 | // Mixer Fractions that are combined for Motor Control |
658 | int16_t YawMixFraction, GasMixFraction, NickMixFraction, RollMixFraction; |
658 | int16_t YawMixFraction, GasMixFraction, NickMixFraction, RollMixFraction; |
659 | 659 | ||
660 | // PID controller variables |
660 | // PID controller variables |
661 | int16_t DiffNick, DiffRoll; |
661 | int16_t DiffNick, DiffRoll; |
662 | int16_t PDPartNick, PDPartRoll, PDPartYaw, PPartNick, PPartRoll; |
662 | int16_t PDPartNick, PDPartRoll, PDPartYaw, PPartNick, PPartRoll; |
663 | static int32_t IPartNick = 0, IPartRoll = 0; |
663 | static int32_t IPartNick = 0, IPartRoll = 0; |
664 | 664 | ||
665 | static int32_t SetPointYaw = 0; |
665 | static int32_t SetPointYaw = 0; |
666 | static int32_t IntegralGyroNickError = 0, IntegralGyroRollError = 0; |
666 | static int32_t IntegralGyroNickError = 0, IntegralGyroRollError = 0; |
667 | static int32_t CorrectionNick, CorrectionRoll; |
667 | static int32_t CorrectionNick, CorrectionRoll; |
668 | static uint16_t RcLostTimer; |
668 | static uint16_t RcLostTimer; |
669 | static uint8_t delay_neutral = 0, delay_startmotors = 0, delay_stopmotors = 0; |
669 | static uint8_t delay_neutral = 0, delay_startmotors = 0, delay_stopmotors = 0; |
670 | static uint8_t HeightControlActive = 0; |
670 | static uint8_t HeightControlActive = 0; |
671 | static int16_t HeightControlGas = 0; |
671 | static int16_t HeightControlGas = 0; |
672 | static int8_t TimerDebugOut = 0; |
672 | static int8_t TimerDebugOut = 0; |
673 | static uint16_t UpdateCompassCourse = 0; |
673 | static uint16_t UpdateCompassCourse = 0; |
674 | // high resolution motor values for smoothing of PID motor outputs |
674 | // high resolution motor values for smoothing of PID motor outputs |
675 | static int16_t MotorValue1 = 0, MotorValue2 = 0, MotorValue3 = 0, MotorValue4 = 0; |
675 | static int16_t MotorValue1 = 0, MotorValue2 = 0, MotorValue3 = 0, MotorValue4 = 0; |
676 | #ifndef USE_QUADRO |
676 | #ifndef USE_QUADRO |
677 | static int16_t MotorValue5 = 0, MotorValue6 = 0, MotorValue7 = 0, MotorValue8 = 0; |
677 | static int16_t MotorValue5 = 0, MotorValue6 = 0, MotorValue7 = 0, MotorValue8 = 0; |
678 | #endif |
678 | #endif |
679 | 679 | ||
680 | Mean(); |
680 | Mean(); |
681 | GRN_ON; |
681 | GRN_ON; |
682 | 682 | ||
683 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
683 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
684 | // determine gas value |
684 | // determine gas value |
685 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
685 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
686 | GasMixFraction = StickGas; |
686 | GasMixFraction = StickGas; |
687 | if(GasMixFraction < ParamSet.GasMin + 10) GasMixFraction = ParamSet.GasMin + 10; |
687 | if(GasMixFraction < ParamSet.GasMin + 10) GasMixFraction = ParamSet.GasMin + 10; |
688 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
688 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
689 | // RC-signal is bad |
689 | // RC-signal is bad |
690 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
690 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
691 | if(RC_Quality < 120) // the rc-frame signal is not reveived or noisy |
691 | if(RC_Quality < 120) // the rc-frame signal is not reveived or noisy |
692 | { |
692 | { |
693 | if(!PcAccess) // if also no PC-Access via UART |
693 | if(!PcAccess) // if also no PC-Access via UART |
694 | { |
694 | { |
695 | if(BeepModulation == 0xFFFF) |
695 | if(BeepModulation == 0xFFFF) |
696 | { |
696 | { |
697 | BeepTime = 15000; // 1.5 seconds |
697 | BeepTime = 15000; // 1.5 seconds |
698 | BeepModulation = 0x0C00; |
698 | BeepModulation = 0x0C00; |
699 | } |
699 | } |
700 | } |
700 | } |
701 | if(RcLostTimer) RcLostTimer--; // decremtent timer after rc sigal lost |
701 | if(RcLostTimer) RcLostTimer--; // decremtent timer after rc sigal lost |
702 | else // rc lost countdown finished |
702 | else // rc lost countdown finished |
703 | { |
703 | { |
704 | MKFlags &= ~(MKFLAG_MOTOR_RUN|MKFLAG_EMERGENCY_LANDING); // clear motor run flag that stop the motors in SendMotorData() |
704 | MKFlags &= ~(MKFLAG_MOTOR_RUN|MKFLAG_EMERGENCY_LANDING); // clear motor run flag that stop the motors in SendMotorData() |
705 | } |
705 | } |
706 | RED_ON; // set red led |
706 | RED_ON; // set red led |
707 | if(ModelIsFlying > 1000) // wahrscheinlich in der Luft --> langsam absenken |
707 | if(ModelIsFlying > 1000) // wahrscheinlich in der Luft --> langsam absenken |
708 | { |
708 | { |
709 | GasMixFraction = ParamSet.EmergencyGas; // set emergency gas |
709 | GasMixFraction = ParamSet.EmergencyGas; // set emergency gas |
710 | MKFlags |= (MKFLAG_EMERGENCY_LANDING); // ser flag fpr emergency landing |
710 | MKFlags |= (MKFLAG_EMERGENCY_LANDING); // ser flag fpr emergency landing |
711 | // set neutral rc inputs |
711 | // set neutral rc inputs |
712 | PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] = 0; |
712 | PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] = 0; |
713 | PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
713 | PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
714 | PPM_diff[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
714 | PPM_diff[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
715 | PPM_in[ParamSet.ChannelAssignment[CH_NICK]] = 0; |
715 | PPM_in[ParamSet.ChannelAssignment[CH_NICK]] = 0; |
716 | PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
716 | PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
717 | PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
717 | PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
718 | } |
718 | } |
719 | else MKFlags &= ~(MKFLAG_MOTOR_RUN); // clear motor run flag that stop the motors in SendMotorData() |
719 | else MKFlags &= ~(MKFLAG_MOTOR_RUN); // clear motor run flag that stop the motors in SendMotorData() |
720 | } // eof RC_Quality < 120 |
720 | } // eof RC_Quality < 120 |
721 | else |
721 | else |
722 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
722 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
723 | // RC-signal is good |
723 | // RC-signal is good |
724 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
724 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
725 | if(RC_Quality > 140) |
725 | if(RC_Quality > 140) |
726 | { |
726 | { |
727 | MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing |
727 | MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing |
728 | // reset emergency timer |
728 | // reset emergency timer |
729 | RcLostTimer = ParamSet.EmergencyGasDuration * 50; |
729 | RcLostTimer = ParamSet.EmergencyGasDuration * 50; |
730 | if(GasMixFraction > 40 && (MKFlags & MKFLAG_MOTOR_RUN) ) |
730 | if(GasMixFraction > 40 && (MKFlags & MKFLAG_MOTOR_RUN) ) |
731 | { |
731 | { |
732 | if(ModelIsFlying < 0xFFFF) ModelIsFlying++; |
732 | if(ModelIsFlying < 0xFFFF) ModelIsFlying++; |
733 | } |
733 | } |
734 | if(ModelIsFlying < 256) |
734 | if(ModelIsFlying < 256) |
735 | { |
735 | { |
736 | IPartNick = 0; |
736 | IPartNick = 0; |
737 | IPartRoll = 0; |
737 | IPartRoll = 0; |
738 | StickYaw = 0; |
738 | StickYaw = 0; |
739 | if(ModelIsFlying == 250) |
739 | if(ModelIsFlying == 250) |
740 | { |
740 | { |
741 | UpdateCompassCourse = 1; |
741 | UpdateCompassCourse = 1; |
742 | ReadingIntegralGyroYaw = 0; |
742 | ReadingIntegralGyroYaw = 0; |
743 | SetPointYaw = 0; |
743 | SetPointYaw = 0; |
744 | } |
744 | } |
745 | } |
745 | } |
746 | else MKFlags |= (MKFLAG_FLY); // set fly flag |
746 | else MKFlags |= (MKFLAG_FLY); // set fly flag |
747 | 747 | ||
748 | if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--; |
748 | if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--; |
749 | if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--; |
749 | if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--; |
750 | if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--; |
750 | if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--; |
751 | if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--; |
751 | if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--; |
752 | //PPM24-Extension |
752 | //PPM24-Extension |
753 | if(Poti5 < PPM_in[9] + 110) Poti5++; else if(Poti5 > PPM_in[9] + 110 && Poti5) Poti5--; |
753 | if(Poti5 < PPM_in[9] + 110) Poti5++; else if(Poti5 > PPM_in[9] + 110 && Poti5) Poti5--; |
754 | if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--; |
754 | if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--; |
755 | if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--; |
755 | if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--; |
756 | if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--; |
756 | if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--; |
757 | //limit poti values |
757 | //limit poti values |
758 | if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255; |
758 | if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255; |
759 | if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255; |
759 | if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255; |
760 | if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255; |
760 | if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255; |
761 | if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255; |
761 | if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255; |
762 | //PPM24-Extension |
762 | //PPM24-Extension |
763 | if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255; |
763 | if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255; |
764 | if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255; |
764 | if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255; |
765 | if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255; |
765 | if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255; |
766 | if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255; |
766 | if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255; |
767 | 767 | ||
768 | // if motors are off and the gas stick is in the upper position |
768 | // if motors are off and the gas stick is in the upper position |
769 | if((PPM_in[ParamSet.ChannelAssignment[CH_GAS]] > 80) && !(MKFlags & MKFLAG_MOTOR_RUN) ) |
769 | if((PPM_in[ParamSet.ChannelAssignment[CH_GAS]] > 80) && !(MKFlags & MKFLAG_MOTOR_RUN) ) |
770 | { |
770 | { |
771 | // and if the yaw stick is in the leftmost position |
771 | // and if the yaw stick is in the leftmost position |
772 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75) |
772 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75) |
773 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
773 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
774 | // calibrate the neutral readings of all attitude sensors |
774 | // calibrate the neutral readings of all attitude sensors |
775 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
775 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
776 | { |
776 | { |
777 | // gas/yaw joystick is top left |
777 | // gas/yaw joystick is top left |
778 | // _________ |
778 | // _________ |
779 | // |x | |
779 | // |x | |
780 | // | | |
780 | // | | |
781 | // | | |
781 | // | | |
782 | // | | |
782 | // | | |
783 | // | | |
783 | // | | |
784 | // ¯¯¯¯¯¯¯¯¯ |
784 | // ¯¯¯¯¯¯¯¯¯ |
785 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
785 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
786 | { |
786 | { |
787 | delay_neutral = 0; |
787 | delay_neutral = 0; |
788 | GRN_OFF; |
788 | GRN_OFF; |
789 | ModelIsFlying = 0; |
789 | ModelIsFlying = 0; |
790 | // check roll/nick stick position |
790 | // check roll/nick stick position |
791 | // if nick stick is top or roll stick is left or right --> change parameter setting |
791 | // if nick stick is top or roll stick is left or right --> change parameter setting |
792 | // according to roll/nick stick position |
792 | // according to roll/nick stick position |
793 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70) |
793 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70) |
794 | { |
794 | { |
795 | uint8_t setting = 1; // default |
795 | uint8_t setting = 1; // default |
796 | // nick/roll joystick |
796 | // nick/roll joystick |
797 | // _________ |
797 | // _________ |
798 | // |2 3 4| |
798 | // |2 3 4| |
799 | // | | |
799 | // | | |
800 | // |1 5| |
800 | // |1 5| |
801 | // | | |
801 | // | | |
802 | // | | |
802 | // | | |
803 | // ¯¯¯¯¯¯¯¯¯ |
803 | // ¯¯¯¯¯¯¯¯¯ |
804 | // roll stick leftmost and nick stick centered --> setting 1 |
804 | // roll stick leftmost and nick stick centered --> setting 1 |
805 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 1; |
805 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 1; |
806 | // roll stick leftmost and nick stick topmost --> setting 2 |
806 | // roll stick leftmost and nick stick topmost --> setting 2 |
807 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 2; |
807 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 2; |
808 | // roll stick centered an nick stick topmost --> setting 3 |
808 | // roll stick centered an nick stick topmost --> setting 3 |
809 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 3; |
809 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 3; |
810 | // roll stick rightmost and nick stick topmost --> setting 4 |
810 | // roll stick rightmost and nick stick topmost --> setting 4 |
811 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 4; |
811 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 4; |
812 | // roll stick rightmost and nick stick centered --> setting 5 |
812 | // roll stick rightmost and nick stick centered --> setting 5 |
813 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 5; |
813 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 5; |
814 | // update active parameter set in eeprom |
814 | // update active parameter set in eeprom |
815 | SetActiveParamSet(setting); |
815 | SetActiveParamSet(setting); |
816 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
816 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
817 | SetNeutral(NO_ACC_CALIB); |
817 | SetNeutral(NO_ACC_CALIB); |
818 | Beep(GetActiveParamSet()); |
818 | Beep(GetActiveParamSet()); |
819 | } |
819 | } |
820 | else |
820 | else |
821 | { |
821 | { |
822 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
822 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
823 | { |
823 | { |
824 | // if roll stick is centered and nick stick is down |
824 | // if roll stick is centered and nick stick is down |
825 | if (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < 30 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70) |
825 | if (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < 30 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70) |
826 | { |
826 | { |
827 | // nick/roll joystick |
827 | // nick/roll joystick |
828 | // _________ |
828 | // _________ |
829 | // | | |
829 | // | | |
830 | // | | |
830 | // | | |
831 | // | | |
831 | // | | |
832 | // | | |
832 | // | | |
833 | // | x | |
833 | // | x | |
834 | // ¯¯¯¯¯¯¯¯¯ |
834 | // ¯¯¯¯¯¯¯¯¯ |
835 | // enable calibration state of compass |
835 | // enable calibration state of compass |
836 | CompassCalState = 1; |
836 | CompassCalState = 1; |
837 | BeepTime = 1000; |
837 | BeepTime = 1000; |
838 | } |
838 | } |
839 | else // nick and roll are centered |
839 | else // nick and roll are centered |
840 | { |
840 | { |
841 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
841 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
842 | SetNeutral(NO_ACC_CALIB); |
842 | SetNeutral(NO_ACC_CALIB); |
843 | Beep(GetActiveParamSet()); |
843 | Beep(GetActiveParamSet()); |
844 | } |
844 | } |
845 | } |
845 | } |
846 | else // nick and roll are centered |
846 | else // nick and roll are centered |
847 | { |
847 | { |
848 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
848 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
849 | SetNeutral(NO_ACC_CALIB); |
849 | SetNeutral(NO_ACC_CALIB); |
850 | Beep(GetActiveParamSet()); |
850 | Beep(GetActiveParamSet()); |
851 | } |
851 | } |
852 | } |
852 | } |
853 | } |
853 | } |
854 | } |
854 | } |
855 | // and if the yaw stick is in the rightmost position |
855 | // and if the yaw stick is in the rightmost position |
856 | // save the ACC neutral setting to eeprom |
856 | // save the ACC neutral setting to eeprom |
857 | else if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75) |
857 | else if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75) |
858 | { |
858 | { |
859 | // gas/yaw joystick is top right |
859 | // gas/yaw joystick is top right |
860 | // _________ |
860 | // _________ |
861 | // | x| |
861 | // | x| |
862 | // | | |
862 | // | | |
863 | // | | |
863 | // | | |
864 | // | | |
864 | // | | |
865 | // | | |
865 | // | | |
866 | // ¯¯¯¯¯¯¯¯¯ |
866 | // ¯¯¯¯¯¯¯¯¯ |
867 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
867 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
868 | { |
868 | { |
869 | delay_neutral = 0; |
869 | delay_neutral = 0; |
870 | GRN_OFF; |
870 | GRN_OFF; |
871 | ModelIsFlying = 0; |
871 | ModelIsFlying = 0; |
872 | SetNeutral(ACC_CALIB); |
872 | SetNeutral(ACC_CALIB); |
873 | Beep(GetActiveParamSet()); |
873 | Beep(GetActiveParamSet()); |
874 | } |
874 | } |
875 | } |
875 | } |
876 | else delay_neutral = 0; |
876 | else delay_neutral = 0; |
877 | } |
877 | } |
878 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
878 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
879 | // gas stick is down |
879 | // gas stick is down |
880 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
880 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
881 | if(PPM_in[ParamSet.ChannelAssignment[CH_GAS]] < -85) |
881 | if(PPM_in[ParamSet.ChannelAssignment[CH_GAS]] < -85) |
882 | { |
882 | { |
883 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75) |
883 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75) |
884 | { |
884 | { |
885 | // gas/yaw joystick is bottom right |
885 | // gas/yaw joystick is bottom right |
886 | // _________ |
886 | // _________ |
887 | // | | |
887 | // | | |
888 | // | | |
888 | // | | |
889 | // | | |
889 | // | | |
890 | // | | |
890 | // | | |
891 | // | x| |
891 | // | x| |
892 | // ¯¯¯¯¯¯¯¯¯ |
892 | // ¯¯¯¯¯¯¯¯¯ |
893 | // Start Motors |
893 | // Start Motors |
894 | if(++delay_startmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
894 | if(++delay_startmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
895 | { |
895 | { |
896 | delay_startmotors = 200; // do not repeat if once executed |
896 | delay_startmotors = 200; // do not repeat if once executed |
897 | ModelIsFlying = 1; |
897 | ModelIsFlying = 1; |
898 | MKFlags |= (MKFLAG_MOTOR_RUN|MKFLAG_START); // set flag RUN and START |
898 | MKFlags |= (MKFLAG_MOTOR_RUN|MKFLAG_START); // set flag RUN and START |
899 | SetPointYaw = 0; |
899 | SetPointYaw = 0; |
900 | ReadingIntegralGyroYaw = 0; |
900 | ReadingIntegralGyroYaw = 0; |
901 | ReadingIntegralGyroNick = ParamSet.GyroAccFactor * (int32_t)AccNick; |
901 | ReadingIntegralGyroNick = ParamSet.GyroAccFactor * (int32_t)AccNick; |
902 | ReadingIntegralGyroRoll = ParamSet.GyroAccFactor * (int32_t)AccRoll; |
902 | ReadingIntegralGyroRoll = ParamSet.GyroAccFactor * (int32_t)AccRoll; |
903 | ReadingIntegralGyroNick2 = IntegralGyroNick; |
903 | ReadingIntegralGyroNick2 = IntegralGyroNick; |
904 | ReadingIntegralGyroRoll2 = IntegralGyroRoll; |
904 | ReadingIntegralGyroRoll2 = IntegralGyroRoll; |
905 | IPartNick = 0; |
905 | IPartNick = 0; |
906 | IPartRoll = 0; |
906 | IPartRoll = 0; |
907 | } |
907 | } |
908 | } |
908 | } |
909 | else delay_startmotors = 0; // reset delay timer if sticks are not in this position |
909 | else delay_startmotors = 0; // reset delay timer if sticks are not in this position |
910 | 910 | ||
911 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75) |
911 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75) |
912 | { |
912 | { |
913 | // gas/yaw joystick is bottom left |
913 | // gas/yaw joystick is bottom left |
914 | // _________ |
914 | // _________ |
915 | // | | |
915 | // | | |
916 | // | | |
916 | // | | |
917 | // | | |
917 | // | | |
918 | // | | |
918 | // | | |
919 | // |x | |
919 | // |x | |
920 | // ¯¯¯¯¯¯¯¯¯ |
920 | // ¯¯¯¯¯¯¯¯¯ |
921 | // Stop Motors |
921 | // Stop Motors |
922 | if(++delay_stopmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
922 | if(++delay_stopmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
923 | { |
923 | { |
924 | delay_stopmotors = 200; // do not repeat if once executed |
924 | delay_stopmotors = 200; // do not repeat if once executed |
925 | ModelIsFlying = 0; |
925 | ModelIsFlying = 0; |
926 | MKFlags &= ~(MKFLAG_MOTOR_RUN); |
926 | MKFlags &= ~(MKFLAG_MOTOR_RUN); |
927 | } |
927 | } |
928 | } |
928 | } |
929 | else delay_stopmotors = 0; // reset delay timer if sticks are not in this position |
929 | else delay_stopmotors = 0; // reset delay timer if sticks are not in this position |
930 | } |
930 | } |
931 | // remapping of paameters only if the signal rc-sigbnal conditions are good |
931 | // remapping of paameters only if the signal rc-sigbnal conditions are good |
932 | } // eof RC_Quality > 150 |
932 | } // eof RC_Quality > 150 |
933 | 933 | ||
934 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
934 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
935 | // new values from RC |
935 | // new values from RC |
936 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
936 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
937 | if(!NewPpmData-- || (MKFlags & MKFLAG_EMERGENCY_LANDING) ) // NewData = 0 means new data from RC |
937 | if(!NewPpmData-- || (MKFlags & MKFLAG_EMERGENCY_LANDING) ) // NewData = 0 means new data from RC |
938 | { |
938 | { |
- | 939 | static int16_t stick_nick = 0, stick_roll = 0; |
|
- | 940 | ||
939 | ParameterMapping(); // remapping params (online poti replacement) |
941 | ParameterMapping(); // remapping params (online poti replacement) |
- | 942 | ||
940 | // calculate Stick inputs by rc channels (P) and changing of rc channels (D) |
943 | // calculate Stick inputs by rc channels (P) and changing of rc channels (D) |
941 | StickNick = (StickNick * 3 + PPM_in[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.StickP) / 4; |
944 | stick_nick = (stick_nick * 3 + PPM_in[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.StickP) / 4; |
942 | StickNick += PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.StickD; |
945 | stick_nick += PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.StickD; |
943 | StickNick -= (GPSStickNick); |
946 | StickNick = stick_nick - GPSStickNick; |
944 | 947 | ||
945 | StickRoll = (StickRoll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.StickP) / 4; |
948 | stick_roll = (stick_roll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.StickP) / 4; |
946 | StickRoll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.StickD; |
949 | stick_roll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.StickD; |
947 | StickRoll -= (GPSStickRoll); |
950 | StickRoll = stick_roll - GPSStickRoll; |
948 | 951 | ||
949 | // mapping of yaw |
952 | // mapping of yaw |
950 | StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]]; |
953 | StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]]; |
951 | // (range of -2 .. 2 is set to zero, to avoid unwanted yaw trimming on compass correction) |
954 | // (range of -2 .. 2 is set to zero, to avoid unwanted yaw trimming on compass correction) |
952 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
955 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
953 | { |
956 | { |
954 | if (StickYaw > 2) StickYaw-= 2; |
957 | if (StickYaw > 2) StickYaw-= 2; |
955 | else if (StickYaw< -2) StickYaw += 2; |
958 | else if (StickYaw< -2) StickYaw += 2; |
956 | else StickYaw = 0; |
959 | else StickYaw = 0; |
957 | } |
960 | } |
958 | 961 | ||
959 | // mapping of gas |
962 | // mapping of gas |
960 | StickGas = PPM_in[ParamSet.ChannelAssignment[CH_GAS]] + 120;// shift to positive numbers |
963 | StickGas = PPM_in[ParamSet.ChannelAssignment[CH_GAS]] + 120;// shift to positive numbers |
961 | 964 | ||
962 | // update gyro control loop factors |
965 | // update gyro control loop factors |
963 | GyroPFactor = FCParam.GyroP + 10; |
966 | GyroPFactor = FCParam.GyroP + 10; |
964 | GyroIFactor = FCParam.GyroI; |
967 | GyroIFactor = FCParam.GyroI; |
965 | 968 | ||
966 | 969 | ||
967 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
970 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
968 | //+ Analog control via serial communication |
971 | //+ Analog control via serial communication |
969 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
972 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
970 | 973 | ||
971 | if(ExternControl.Config & 0x01 && FCParam.ExternalControl > 128) |
974 | if(ExternControl.Config & 0x01 && FCParam.ExternalControl > 128) |
972 | { |
975 | { |
973 | StickNick += (int16_t) ExternControl.Nick * (int16_t) ParamSet.StickP; |
976 | StickNick += (int16_t) ExternControl.Nick * (int16_t) ParamSet.StickP; |
974 | StickRoll += (int16_t) ExternControl.Roll * (int16_t) ParamSet.StickP; |
977 | StickRoll += (int16_t) ExternControl.Roll * (int16_t) ParamSet.StickP; |
975 | StickYaw += ExternControl.Yaw; |
978 | StickYaw += ExternControl.Yaw; |
976 | ExternHeightValue = (int16_t) ExternControl.Height * (int16_t)ParamSet.Height_Gain; |
979 | ExternHeightValue = (int16_t) ExternControl.Height * (int16_t)ParamSet.Height_Gain; |
977 | if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas; |
980 | if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas; |
978 | } |
981 | } |
979 | if(StickGas < 0) StickGas = 0; |
982 | if(StickGas < 0) StickGas = 0; |
980 | 983 | ||
981 | // disable I part of gyro control feedback |
984 | // disable I part of gyro control feedback |
982 | if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) GyroIFactor = 0; |
985 | if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) GyroIFactor = 0; |
983 | 986 | ||
984 | // update max stick positions for nick and roll |
987 | // update max stick positions for nick and roll |
985 | if(abs(StickNick / STICK_GAIN) > MaxStickNick) |
988 | if(abs(StickNick / STICK_GAIN) > MaxStickNick) |
986 | { |
989 | { |
987 | MaxStickNick = abs(StickNick)/STICK_GAIN; |
990 | MaxStickNick = abs(StickNick)/STICK_GAIN; |
988 | if(MaxStickNick > 100) MaxStickNick = 100; |
991 | if(MaxStickNick > 100) MaxStickNick = 100; |
989 | } |
992 | } |
990 | else MaxStickNick--; |
993 | else MaxStickNick--; |
991 | if(abs(StickRoll / STICK_GAIN) > MaxStickRoll) |
994 | if(abs(StickRoll / STICK_GAIN) > MaxStickRoll) |
992 | { |
995 | { |
993 | MaxStickRoll = abs(StickRoll)/STICK_GAIN; |
996 | MaxStickRoll = abs(StickRoll)/STICK_GAIN; |
994 | if(MaxStickRoll > 100) MaxStickRoll = 100; |
997 | if(MaxStickRoll > 100) MaxStickRoll = 100; |
995 | } |
998 | } |
996 | else MaxStickRoll--; |
999 | else MaxStickRoll--; |
997 | 1000 | ||
998 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1001 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
999 | // Looping? |
1002 | // Looping? |
1000 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1003 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1001 | 1004 | ||
1002 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > ParamSet.LoopThreshold) && ParamSet.BitConfig & CFG_LOOP_LEFT) LoopingLeft = 1; |
1005 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > ParamSet.LoopThreshold) && ParamSet.BitConfig & CFG_LOOP_LEFT) LoopingLeft = 1; |
1003 | else |
1006 | else |
1004 | { |
1007 | { |
1005 | if(LoopingLeft) // Hysteresis |
1008 | if(LoopingLeft) // Hysteresis |
1006 | { |
1009 | { |
1007 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) LoopingLeft = 0; |
1010 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) LoopingLeft = 0; |
1008 | } |
1011 | } |
1009 | } |
1012 | } |
1010 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < -ParamSet.LoopThreshold) && ParamSet.BitConfig & CFG_LOOP_RIGHT) LoopingRight = 1; |
1013 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < -ParamSet.LoopThreshold) && ParamSet.BitConfig & CFG_LOOP_RIGHT) LoopingRight = 1; |
1011 | else |
1014 | else |
1012 | { |
1015 | { |
1013 | if(LoopingRight) // Hysteresis |
1016 | if(LoopingRight) // Hysteresis |
1014 | { |
1017 | { |
1015 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) LoopingRight = 0; |
1018 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) LoopingRight = 0; |
1016 | } |
1019 | } |
1017 | } |
1020 | } |
1018 | 1021 | ||
1019 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > ParamSet.LoopThreshold) && ParamSet.BitConfig & CFG_LOOP_UP) LoopingTop = 1; |
1022 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > ParamSet.LoopThreshold) && ParamSet.BitConfig & CFG_LOOP_UP) LoopingTop = 1; |
1020 | else |
1023 | else |
1021 | { |
1024 | { |
1022 | if(LoopingTop) // Hysteresis |
1025 | if(LoopingTop) // Hysteresis |
1023 | { |
1026 | { |
1024 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) LoopingTop = 0; |
1027 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) LoopingTop = 0; |
1025 | } |
1028 | } |
1026 | } |
1029 | } |
1027 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -ParamSet.LoopThreshold) && ParamSet.BitConfig & CFG_LOOP_DOWN) LoopingDown = 1; |
1030 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -ParamSet.LoopThreshold) && ParamSet.BitConfig & CFG_LOOP_DOWN) LoopingDown = 1; |
1028 | else |
1031 | else |
1029 | { |
1032 | { |
1030 | if(LoopingDown) // Hysteresis |
1033 | if(LoopingDown) // Hysteresis |
1031 | { |
1034 | { |
1032 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) LoopingDown = 0; |
1035 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) LoopingDown = 0; |
1033 | } |
1036 | } |
1034 | } |
1037 | } |
1035 | 1038 | ||
1036 | if(LoopingLeft || LoopingRight) LoopingRoll = 1; else LoopingRoll = 0; |
1039 | if(LoopingLeft || LoopingRight) LoopingRoll = 1; else LoopingRoll = 0; |
1037 | if(LoopingTop || LoopingDown) { LoopingNick = 1; LoopingRoll = 0; LoopingLeft = 0; LoopingRight = 0;} else LoopingNick = 0; |
1040 | if(LoopingTop || LoopingDown) { LoopingNick = 1; LoopingRoll = 0; LoopingLeft = 0; LoopingRight = 0;} else LoopingNick = 0; |
1038 | } // End of new RC-Values or Emergency Landing |
1041 | } // End of new RC-Values or Emergency Landing |
1039 | 1042 | ||
1040 | 1043 | ||
1041 | if(LoopingRoll || LoopingNick) |
1044 | if(LoopingRoll || LoopingNick) |
1042 | { |
1045 | { |
1043 | if(GasMixFraction > ParamSet.LoopGasLimit) GasMixFraction = ParamSet.LoopGasLimit; |
1046 | if(GasMixFraction > ParamSet.LoopGasLimit) GasMixFraction = ParamSet.LoopGasLimit; |
1044 | FunnelCourse = 1; |
1047 | FunnelCourse = 1; |
1045 | } |
1048 | } |
1046 | 1049 | ||
1047 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1050 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1048 | // in case of emergency landing |
1051 | // in case of emergency landing |
1049 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1052 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1050 | // set all inputs to save values |
1053 | // set all inputs to save values |
1051 | if(MKFlags & MKFLAG_EMERGENCY_LANDING) |
1054 | if(MKFlags & MKFLAG_EMERGENCY_LANDING) |
1052 | { |
1055 | { |
1053 | StickYaw = 0; |
1056 | StickYaw = 0; |
1054 | StickNick = 0; |
1057 | StickNick = 0; |
1055 | StickRoll = 0; |
1058 | StickRoll = 0; |
1056 | GyroPFactor = 90; |
1059 | GyroPFactor = 90; |
1057 | GyroIFactor = 120; |
1060 | GyroIFactor = 120; |
1058 | LoopingRoll = 0; |
1061 | LoopingRoll = 0; |
1059 | LoopingNick = 0; |
1062 | LoopingNick = 0; |
1060 | MaxStickNick = 0; |
1063 | MaxStickNick = 0; |
1061 | MaxStickRoll = 0; |
1064 | MaxStickRoll = 0; |
1062 | } |
1065 | } |
1063 | 1066 | ||
1064 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1067 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1065 | // Trim Gyro-Integrals to ACC-Signals |
1068 | // Trim Gyro-Integrals to ACC-Signals |
1066 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1069 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1067 | 1070 | ||
1068 | #define BALANCE_NUMBER 256L |
1071 | #define BALANCE_NUMBER 256L |
1069 | // sum for averaging |
1072 | // sum for averaging |
1070 | MeanIntegralGyroNick += IntegralGyroNick; |
1073 | MeanIntegralGyroNick += IntegralGyroNick; |
1071 | MeanIntegralGyroRoll += IntegralGyroRoll; |
1074 | MeanIntegralGyroRoll += IntegralGyroRoll; |
1072 | 1075 | ||
1073 | if( LoopingNick || LoopingRoll) // if looping in any direction |
1076 | if( LoopingNick || LoopingRoll) // if looping in any direction |
1074 | { |
1077 | { |
1075 | // reset averaging for acc and gyro integral as well as gyro integral acc correction |
1078 | // reset averaging for acc and gyro integral as well as gyro integral acc correction |
1076 | MeasurementCounter = 0; |
1079 | MeasurementCounter = 0; |
1077 | 1080 | ||
1078 | MeanAccNick = 0; |
1081 | MeanAccNick = 0; |
1079 | MeanAccRoll = 0; |
1082 | MeanAccRoll = 0; |
1080 | 1083 | ||
1081 | MeanIntegralGyroNick = 0; |
1084 | MeanIntegralGyroNick = 0; |
1082 | MeanIntegralGyroRoll = 0; |
1085 | MeanIntegralGyroRoll = 0; |
1083 | 1086 | ||
1084 | ReadingIntegralGyroNick2 = ReadingIntegralGyroNick; |
1087 | ReadingIntegralGyroNick2 = ReadingIntegralGyroNick; |
1085 | ReadingIntegralGyroRoll2 = ReadingIntegralGyroRoll; |
1088 | ReadingIntegralGyroRoll2 = ReadingIntegralGyroRoll; |
1086 | 1089 | ||
1087 | AttitudeCorrectionNick = 0; |
1090 | AttitudeCorrectionNick = 0; |
1088 | AttitudeCorrectionRoll = 0; |
1091 | AttitudeCorrectionRoll = 0; |
1089 | } |
1092 | } |
1090 | 1093 | ||
1091 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1094 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1092 | if(! LoopingNick && !LoopingRoll && (AdValueAccZ > 512)) // if not lopping in any direction or rapid falling |
1095 | if(! LoopingNick && !LoopingRoll && (AdValueAccZ > 512)) // if not lopping in any direction or rapid falling |
1093 | { |
1096 | { |
1094 | int32_t tmp_long, tmp_long2; |
1097 | int32_t tmp_long, tmp_long2; |
1095 | if( FCParam.KalmanK != -1) |
1098 | if( FCParam.KalmanK != -1) |
1096 | { |
1099 | { |
1097 | // determine the deviation of gyro integral from averaged acceleration sensor |
1100 | // determine the deviation of gyro integral from averaged acceleration sensor |
1098 | tmp_long = (int32_t)(IntegralGyroNick / ParamSet.GyroAccFactor - (int32_t)AccNick); |
1101 | tmp_long = (int32_t)(IntegralGyroNick / ParamSet.GyroAccFactor - (int32_t)AccNick); |
1099 | tmp_long = (tmp_long * FCParam.KalmanK) / (32 * 16); |
1102 | tmp_long = (tmp_long * FCParam.KalmanK) / (32 * 16); |
1100 | tmp_long2 = (int32_t)(IntegralGyroRoll / ParamSet.GyroAccFactor - (int32_t)AccRoll); |
1103 | tmp_long2 = (int32_t)(IntegralGyroRoll / ParamSet.GyroAccFactor - (int32_t)AccRoll); |
1101 | tmp_long2 = (tmp_long2 * FCParam.KalmanK) / (32 * 16); |
1104 | tmp_long2 = (tmp_long2 * FCParam.KalmanK) / (32 * 16); |
1102 | 1105 | ||
1103 | if((MaxStickNick > 64) || (MaxStickRoll > 64)) // reduce effect during stick commands |
1106 | if((MaxStickNick > 64) || (MaxStickRoll > 64)) // reduce effect during stick commands |
1104 | { |
1107 | { |
1105 | tmp_long /= 2; |
1108 | tmp_long /= 2; |
1106 | tmp_long2 /= 2; |
1109 | tmp_long2 /= 2; |
1107 | } |
1110 | } |
1108 | if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25) // reduce further if yaw stick is active |
1111 | if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25) // reduce further if yaw stick is active |
1109 | { |
1112 | { |
1110 | tmp_long /= 3; |
1113 | tmp_long /= 3; |
1111 | tmp_long2 /= 3; |
1114 | tmp_long2 /= 3; |
1112 | } |
1115 | } |
1113 | // limit correction effect |
1116 | // limit correction effect |
1114 | if(tmp_long > (int32_t)FCParam.KalmanMaxFusion) tmp_long = (int32_t)FCParam.KalmanMaxFusion; |
1117 | if(tmp_long > (int32_t)FCParam.KalmanMaxFusion) tmp_long = (int32_t)FCParam.KalmanMaxFusion; |
1115 | if(tmp_long < -(int32_t)FCParam.KalmanMaxFusion) tmp_long =-(int32_t)FCParam.KalmanMaxFusion; |
1118 | if(tmp_long < -(int32_t)FCParam.KalmanMaxFusion) tmp_long =-(int32_t)FCParam.KalmanMaxFusion; |
1116 | if(tmp_long2 > (int32_t)FCParam.KalmanMaxFusion) tmp_long2 = (int32_t)FCParam.KalmanMaxFusion; |
1119 | if(tmp_long2 > (int32_t)FCParam.KalmanMaxFusion) tmp_long2 = (int32_t)FCParam.KalmanMaxFusion; |
1117 | if(tmp_long2 <-(int32_t)FCParam.KalmanMaxFusion) tmp_long2 =-(int32_t)FCParam.KalmanMaxFusion; |
1120 | if(tmp_long2 <-(int32_t)FCParam.KalmanMaxFusion) tmp_long2 =-(int32_t)FCParam.KalmanMaxFusion; |
1118 | } |
1121 | } |
1119 | else |
1122 | else |
1120 | { |
1123 | { |
1121 | // determine the deviation of gyro integral from acceleration sensor |
1124 | // determine the deviation of gyro integral from acceleration sensor |
1122 | tmp_long = (int32_t)(IntegralGyroNick / ParamSet.GyroAccFactor - (int32_t)AccNick); |
1125 | tmp_long = (int32_t)(IntegralGyroNick / ParamSet.GyroAccFactor - (int32_t)AccNick); |
1123 | tmp_long /= 16; |
1126 | tmp_long /= 16; |
1124 | tmp_long2 = (int32_t)(IntegralGyroRoll / ParamSet.GyroAccFactor - (int32_t)AccRoll); |
1127 | tmp_long2 = (int32_t)(IntegralGyroRoll / ParamSet.GyroAccFactor - (int32_t)AccRoll); |
1125 | tmp_long2 /= 16; |
1128 | tmp_long2 /= 16; |
1126 | 1129 | ||
1127 | if((MaxStickNick > 64) || (MaxStickRoll > 64)) // reduce effect during stick commands |
1130 | if((MaxStickNick > 64) || (MaxStickRoll > 64)) // reduce effect during stick commands |
1128 | { |
1131 | { |
1129 | tmp_long /= 3; |
1132 | tmp_long /= 3; |
1130 | tmp_long2 /= 3; |
1133 | tmp_long2 /= 3; |
1131 | } |
1134 | } |
1132 | if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25) // reduce further if yaw stick is active |
1135 | if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25) // reduce further if yaw stick is active |
1133 | { |
1136 | { |
1134 | tmp_long /= 3; |
1137 | tmp_long /= 3; |
1135 | tmp_long2 /= 3; |
1138 | tmp_long2 /= 3; |
1136 | } |
1139 | } |
1137 | 1140 | ||
1138 | #define BALANCE 32 |
1141 | #define BALANCE 32 |
1139 | // limit correction effect |
1142 | // limit correction effect |
1140 | CHECK_MIN_MAX(tmp_long, -BALANCE, BALANCE); |
1143 | CHECK_MIN_MAX(tmp_long, -BALANCE, BALANCE); |
1141 | CHECK_MIN_MAX(tmp_long2, -BALANCE, BALANCE); |
1144 | CHECK_MIN_MAX(tmp_long2, -BALANCE, BALANCE); |
1142 | } |
1145 | } |
1143 | // correct current readings |
1146 | // correct current readings |
1144 | ReadingIntegralGyroNick -= tmp_long; |
1147 | ReadingIntegralGyroNick -= tmp_long; |
1145 | ReadingIntegralGyroRoll -= tmp_long2; |
1148 | ReadingIntegralGyroRoll -= tmp_long2; |
1146 | } |
1149 | } |
1147 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1150 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1148 | // MeasurementCounter is incremented in the isr of analog.c |
1151 | // MeasurementCounter is incremented in the isr of analog.c |
1149 | if(MeasurementCounter >= BALANCE_NUMBER) // averaging number has reached |
1152 | if(MeasurementCounter >= BALANCE_NUMBER) // averaging number has reached |
1150 | { |
1153 | { |
1151 | static int16_t cnt = 0; |
1154 | static int16_t cnt = 0; |
1152 | static int8_t last_n_p, last_n_n, last_r_p, last_r_n; |
1155 | static int8_t last_n_p, last_n_n, last_r_p, last_r_n; |
1153 | static int32_t MeanIntegralGyroNick_old, MeanIntegralGyroRoll_old; |
1156 | static int32_t MeanIntegralGyroNick_old, MeanIntegralGyroRoll_old; |
1154 | 1157 | ||
1155 | // if not lopping in any direction (this should be always the case, |
1158 | // if not lopping in any direction (this should be always the case, |
1156 | // because the Measurement counter is reset to 0 if looping in any direction is active.) |
1159 | // because the Measurement counter is reset to 0 if looping in any direction is active.) |
1157 | if(! LoopingNick && !LoopingRoll && !FunnelCourse && ParamSet.DriftComp) |
1160 | if(! LoopingNick && !LoopingRoll && !FunnelCourse && ParamSet.DriftComp) |
1158 | { |
1161 | { |
1159 | // Calculate mean value of the gyro integrals |
1162 | // Calculate mean value of the gyro integrals |
1160 | MeanIntegralGyroNick /= BALANCE_NUMBER; |
1163 | MeanIntegralGyroNick /= BALANCE_NUMBER; |
1161 | MeanIntegralGyroRoll /= BALANCE_NUMBER; |
1164 | MeanIntegralGyroRoll /= BALANCE_NUMBER; |
1162 | 1165 | ||
1163 | // Calculate mean of the acceleration values scaled to the gyro integrals |
1166 | // Calculate mean of the acceleration values scaled to the gyro integrals |
1164 | MeanAccNick = (ParamSet.GyroAccFactor * MeanAccNick) / BALANCE_NUMBER; |
1167 | MeanAccNick = (ParamSet.GyroAccFactor * MeanAccNick) / BALANCE_NUMBER; |
1165 | MeanAccRoll = (ParamSet.GyroAccFactor * MeanAccRoll) / BALANCE_NUMBER; |
1168 | MeanAccRoll = (ParamSet.GyroAccFactor * MeanAccRoll) / BALANCE_NUMBER; |
1166 | 1169 | ||
1167 | // Nick ++++++++++++++++++++++++++++++++++++++++++++++++ |
1170 | // Nick ++++++++++++++++++++++++++++++++++++++++++++++++ |
1168 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
1171 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
1169 | IntegralGyroNickError = (int32_t)(MeanIntegralGyroNick - (int32_t)MeanAccNick); |
1172 | IntegralGyroNickError = (int32_t)(MeanIntegralGyroNick - (int32_t)MeanAccNick); |
1170 | CorrectionNick = IntegralGyroNickError / ParamSet.GyroAccTrim; |
1173 | CorrectionNick = IntegralGyroNickError / ParamSet.GyroAccTrim; |
1171 | AttitudeCorrectionNick = CorrectionNick / BALANCE_NUMBER; |
1174 | AttitudeCorrectionNick = CorrectionNick / BALANCE_NUMBER; |
1172 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
1175 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
1173 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
1176 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
1174 | IntegralGyroRollError = (int32_t)(MeanIntegralGyroRoll - (int32_t)MeanAccRoll); |
1177 | IntegralGyroRollError = (int32_t)(MeanIntegralGyroRoll - (int32_t)MeanAccRoll); |
1175 | CorrectionRoll = IntegralGyroRollError / ParamSet.GyroAccTrim; |
1178 | CorrectionRoll = IntegralGyroRollError / ParamSet.GyroAccTrim; |
1176 | AttitudeCorrectionRoll = CorrectionRoll / BALANCE_NUMBER; |
1179 | AttitudeCorrectionRoll = CorrectionRoll / BALANCE_NUMBER; |
1177 | 1180 | ||
1178 | if(((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25)) && (FCParam.KalmanK == -1) ) |
1181 | if(((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25)) && (FCParam.KalmanK == -1) ) |
1179 | { |
1182 | { |
1180 | AttitudeCorrectionNick /= 2; |
1183 | AttitudeCorrectionNick /= 2; |
1181 | AttitudeCorrectionRoll /= 2; |
1184 | AttitudeCorrectionRoll /= 2; |
1182 | } |
1185 | } |
1183 | 1186 | ||
1184 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1187 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1185 | // Gyro-Drift ermitteln |
1188 | // Gyro-Drift ermitteln |
1186 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1189 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1187 | // deviation of gyro nick integral (IntegralGyroNick is corrected by averaged acc sensor) |
1190 | // deviation of gyro nick integral (IntegralGyroNick is corrected by averaged acc sensor) |
1188 | IntegralGyroNickError = IntegralGyroNick2 - IntegralGyroNick; |
1191 | IntegralGyroNickError = IntegralGyroNick2 - IntegralGyroNick; |
1189 | ReadingIntegralGyroNick2 -= IntegralGyroNickError; |
1192 | ReadingIntegralGyroNick2 -= IntegralGyroNickError; |
1190 | // deviation of gyro nick integral (IntegralGyroNick is corrected by averaged acc sensor) |
1193 | // deviation of gyro nick integral (IntegralGyroNick is corrected by averaged acc sensor) |
1191 | IntegralGyroRollError = IntegralGyroRoll2 - IntegralGyroRoll; |
1194 | IntegralGyroRollError = IntegralGyroRoll2 - IntegralGyroRoll; |
1192 | ReadingIntegralGyroRoll2 -= IntegralGyroRollError; |
1195 | ReadingIntegralGyroRoll2 -= IntegralGyroRollError; |
1193 | 1196 | ||
1194 | if(ParamSet.DriftComp) |
1197 | if(ParamSet.DriftComp) |
1195 | { |
1198 | { |
1196 | if(YawGyroDrift > BALANCE_NUMBER/2) AdBiasGyroYaw++; |
1199 | if(YawGyroDrift > BALANCE_NUMBER/2) AdBiasGyroYaw++; |
1197 | if(YawGyroDrift < -BALANCE_NUMBER/2) AdBiasGyroYaw--; |
1200 | if(YawGyroDrift < -BALANCE_NUMBER/2) AdBiasGyroYaw--; |
1198 | } |
1201 | } |
1199 | YawGyroDrift = 0; |
1202 | YawGyroDrift = 0; |
1200 | 1203 | ||
1201 | #define ERROR_LIMIT (BALANCE_NUMBER * 4) |
1204 | #define ERROR_LIMIT (BALANCE_NUMBER * 4) |
1202 | #define ERROR_LIMIT2 (BALANCE_NUMBER * 16) |
1205 | #define ERROR_LIMIT2 (BALANCE_NUMBER * 16) |
1203 | #define MOVEMENT_LIMIT 20000 |
1206 | #define MOVEMENT_LIMIT 20000 |
1204 | // Nick +++++++++++++++++++++++++++++++++++++++++++++++++ |
1207 | // Nick +++++++++++++++++++++++++++++++++++++++++++++++++ |
1205 | cnt = 1;// + labs(IntegralGyroNickError) / 4096; |
1208 | cnt = 1;// + labs(IntegralGyroNickError) / 4096; |
1206 | CorrectionNick = 0; |
1209 | CorrectionNick = 0; |
1207 | if((labs(MeanIntegralGyroNick_old - MeanIntegralGyroNick) < MOVEMENT_LIMIT) || (FCParam.KalmanMaxDrift > 3 * 8)) |
1210 | if((labs(MeanIntegralGyroNick_old - MeanIntegralGyroNick) < MOVEMENT_LIMIT) || (FCParam.KalmanMaxDrift > 3 * 8)) |
1208 | { |
1211 | { |
1209 | if(IntegralGyroNickError > ERROR_LIMIT2) |
1212 | if(IntegralGyroNickError > ERROR_LIMIT2) |
1210 | { |
1213 | { |
1211 | if(last_n_p) |
1214 | if(last_n_p) |
1212 | { |
1215 | { |
1213 | cnt += labs(IntegralGyroNickError) / (ERROR_LIMIT2 / 8); |
1216 | cnt += labs(IntegralGyroNickError) / (ERROR_LIMIT2 / 8); |
1214 | CorrectionNick = IntegralGyroNickError / 8; |
1217 | CorrectionNick = IntegralGyroNickError / 8; |
1215 | if(CorrectionNick > 5000) CorrectionNick = 5000; |
1218 | if(CorrectionNick > 5000) CorrectionNick = 5000; |
1216 | AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER; |
1219 | AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER; |
1217 | } |
1220 | } |
1218 | else last_n_p = 1; |
1221 | else last_n_p = 1; |
1219 | } |
1222 | } |
1220 | else last_n_p = 0; |
1223 | else last_n_p = 0; |
1221 | if(IntegralGyroNickError < -ERROR_LIMIT2) |
1224 | if(IntegralGyroNickError < -ERROR_LIMIT2) |
1222 | { |
1225 | { |
1223 | if(last_n_n) |
1226 | if(last_n_n) |
1224 | { |
1227 | { |
1225 | cnt += labs(IntegralGyroNickError) / (ERROR_LIMIT2 / 8); |
1228 | cnt += labs(IntegralGyroNickError) / (ERROR_LIMIT2 / 8); |
1226 | CorrectionNick = IntegralGyroNickError / 8; |
1229 | CorrectionNick = IntegralGyroNickError / 8; |
1227 | if(CorrectionNick < -5000) CorrectionNick = -5000; |
1230 | if(CorrectionNick < -5000) CorrectionNick = -5000; |
1228 | AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER; |
1231 | AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER; |
1229 | } |
1232 | } |
1230 | else last_n_n = 1; |
1233 | else last_n_n = 1; |
1231 | } |
1234 | } |
1232 | else last_n_n = 0; |
1235 | else last_n_n = 0; |
1233 | } |
1236 | } |
1234 | else |
1237 | else |
1235 | { |
1238 | { |
1236 | cnt = 0; |
1239 | cnt = 0; |
1237 | BadCompassHeading = 1000; |
1240 | BadCompassHeading = 1000; |
1238 | } |
1241 | } |
1239 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
1242 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
1240 | if(FCParam.KalmanMaxDrift) if(cnt > FCParam.KalmanMaxDrift) cnt = FCParam.KalmanMaxDrift; |
1243 | if(FCParam.KalmanMaxDrift) if(cnt > FCParam.KalmanMaxDrift) cnt = FCParam.KalmanMaxDrift; |
1241 | // correct Gyro Offsets |
1244 | // correct Gyro Offsets |
1242 | if(IntegralGyroNickError > ERROR_LIMIT) BiasHiResGyroNick += cnt; |
1245 | if(IntegralGyroNickError > ERROR_LIMIT) BiasHiResGyroNick += cnt; |
1243 | if(IntegralGyroNickError < -ERROR_LIMIT) BiasHiResGyroNick -= cnt; |
1246 | if(IntegralGyroNickError < -ERROR_LIMIT) BiasHiResGyroNick -= cnt; |
1244 | 1247 | ||
1245 | // Roll +++++++++++++++++++++++++++++++++++++++++++++++++ |
1248 | // Roll +++++++++++++++++++++++++++++++++++++++++++++++++ |
1246 | cnt = 1;// + labs(IntegralGyroNickError) / 4096; |
1249 | cnt = 1;// + labs(IntegralGyroNickError) / 4096; |
1247 | CorrectionRoll = 0; |
1250 | CorrectionRoll = 0; |
1248 | if((labs(MeanIntegralGyroRoll_old - MeanIntegralGyroRoll) < MOVEMENT_LIMIT) || (FCParam.KalmanMaxDrift > 3 * 8)) |
1251 | if((labs(MeanIntegralGyroRoll_old - MeanIntegralGyroRoll) < MOVEMENT_LIMIT) || (FCParam.KalmanMaxDrift > 3 * 8)) |
1249 | { |
1252 | { |
1250 | if(IntegralGyroRollError > ERROR_LIMIT2) |
1253 | if(IntegralGyroRollError > ERROR_LIMIT2) |
1251 | { |
1254 | { |
1252 | if(last_r_p) |
1255 | if(last_r_p) |
1253 | { |
1256 | { |
1254 | cnt += labs(IntegralGyroRollError) / (ERROR_LIMIT2 / 8); |
1257 | cnt += labs(IntegralGyroRollError) / (ERROR_LIMIT2 / 8); |
1255 | CorrectionRoll = IntegralGyroRollError / 8; |
1258 | CorrectionRoll = IntegralGyroRollError / 8; |
1256 | if(CorrectionRoll > 5000) CorrectionRoll = 5000; |
1259 | if(CorrectionRoll > 5000) CorrectionRoll = 5000; |
1257 | AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER; |
1260 | AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER; |
1258 | } |
1261 | } |
1259 | else last_r_p = 1; |
1262 | else last_r_p = 1; |
1260 | } |
1263 | } |
1261 | else last_r_p = 0; |
1264 | else last_r_p = 0; |
1262 | if(IntegralGyroRollError < -ERROR_LIMIT2) |
1265 | if(IntegralGyroRollError < -ERROR_LIMIT2) |
1263 | { |
1266 | { |
1264 | if(last_r_n) |
1267 | if(last_r_n) |
1265 | { |
1268 | { |
1266 | cnt += labs(IntegralGyroRollError) / (ERROR_LIMIT2 / 8); |
1269 | cnt += labs(IntegralGyroRollError) / (ERROR_LIMIT2 / 8); |
1267 | CorrectionRoll = IntegralGyroRollError / 8; |
1270 | CorrectionRoll = IntegralGyroRollError / 8; |
1268 | if(CorrectionRoll < -5000) CorrectionRoll = -5000; |
1271 | if(CorrectionRoll < -5000) CorrectionRoll = -5000; |
1269 | AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER; |
1272 | AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER; |
1270 | } |
1273 | } |
1271 | else last_r_n = 1; |
1274 | else last_r_n = 1; |
1272 | } |
1275 | } |
1273 | else last_r_n = 0; |
1276 | else last_r_n = 0; |
1274 | } |
1277 | } |
1275 | else |
1278 | else |
1276 | { |
1279 | { |
1277 | cnt = 0; |
1280 | cnt = 0; |
1278 | BadCompassHeading = 1000; |
1281 | BadCompassHeading = 1000; |
1279 | } |
1282 | } |
1280 | // correct Gyro Offsets |
1283 | // correct Gyro Offsets |
1281 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
1284 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
1282 | if(FCParam.KalmanMaxDrift) if(cnt > FCParam.KalmanMaxDrift) cnt = FCParam.KalmanMaxDrift; |
1285 | if(FCParam.KalmanMaxDrift) if(cnt > FCParam.KalmanMaxDrift) cnt = FCParam.KalmanMaxDrift; |
1283 | if(IntegralGyroRollError > ERROR_LIMIT) BiasHiResGyroRoll += cnt; |
1286 | if(IntegralGyroRollError > ERROR_LIMIT) BiasHiResGyroRoll += cnt; |
1284 | if(IntegralGyroRollError < -ERROR_LIMIT) BiasHiResGyroRoll -= cnt; |
1287 | if(IntegralGyroRollError < -ERROR_LIMIT) BiasHiResGyroRoll -= cnt; |
1285 | 1288 | ||
1286 | } |
1289 | } |
1287 | else // looping is active |
1290 | else // looping is active |
1288 | { |
1291 | { |
1289 | AttitudeCorrectionRoll = 0; |
1292 | AttitudeCorrectionRoll = 0; |
1290 | AttitudeCorrectionNick = 0; |
1293 | AttitudeCorrectionNick = 0; |
1291 | FunnelCourse = 0; |
1294 | FunnelCourse = 0; |
1292 | } |
1295 | } |
1293 | 1296 | ||
1294 | // if GyroIFactor == 0 , for example at Heading Hold, ignore attitude correction |
1297 | // if GyroIFactor == 0 , for example at Heading Hold, ignore attitude correction |
1295 | if(!GyroIFactor) |
1298 | if(!GyroIFactor) |
1296 | { |
1299 | { |
1297 | AttitudeCorrectionRoll = 0; |
1300 | AttitudeCorrectionRoll = 0; |
1298 | AttitudeCorrectionNick = 0; |
1301 | AttitudeCorrectionNick = 0; |
1299 | } |
1302 | } |
1300 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1303 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1301 | MeanIntegralGyroNick_old = MeanIntegralGyroNick; |
1304 | MeanIntegralGyroNick_old = MeanIntegralGyroNick; |
1302 | MeanIntegralGyroRoll_old = MeanIntegralGyroRoll; |
1305 | MeanIntegralGyroRoll_old = MeanIntegralGyroRoll; |
1303 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1306 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1304 | // reset variables used for next averaging |
1307 | // reset variables used for next averaging |
1305 | MeanAccNick = 0; |
1308 | MeanAccNick = 0; |
1306 | MeanAccRoll = 0; |
1309 | MeanAccRoll = 0; |
1307 | MeanIntegralGyroNick = 0; |
1310 | MeanIntegralGyroNick = 0; |
1308 | MeanIntegralGyroRoll = 0; |
1311 | MeanIntegralGyroRoll = 0; |
1309 | MeasurementCounter = 0; |
1312 | MeasurementCounter = 0; |
1310 | } // end of averaging |
1313 | } // end of averaging |
1311 | 1314 | ||
1312 | 1315 | ||
1313 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1316 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1314 | // Yawing |
1317 | // Yawing |
1315 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1318 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1316 | if(abs(StickYaw) > 15 ) // yaw stick is activated |
1319 | if(abs(StickYaw) > 15 ) // yaw stick is activated |
1317 | { |
1320 | { |
1318 | BadCompassHeading = 1000; |
1321 | BadCompassHeading = 1000; |
1319 | if(!(ParamSet.GlobalConfig & CFG_COMPASS_FIX)) |
1322 | if(!(ParamSet.GlobalConfig & CFG_COMPASS_FIX)) |
1320 | { |
1323 | { |
1321 | UpdateCompassCourse = 1; |
1324 | UpdateCompassCourse = 1; |
1322 | } |
1325 | } |
1323 | } |
1326 | } |
1324 | // exponential stick sensitivity in yawring rate |
1327 | // exponential stick sensitivity in yawring rate |
1325 | tmp_int = (int32_t) ParamSet.StickYawP * ((int32_t)StickYaw * abs(StickYaw)) / 512L; // expo y = ax + bx² |
1328 | tmp_int = (int32_t) ParamSet.StickYawP * ((int32_t)StickYaw * abs(StickYaw)) / 512L; // expo y = ax + bx² |
1326 | tmp_int += (ParamSet.StickYawP * StickYaw) / 4; |
1329 | tmp_int += (ParamSet.StickYawP * StickYaw) / 4; |
1327 | SetPointYaw = tmp_int; |
1330 | SetPointYaw = tmp_int; |
1328 | // trimm drift of ReadingIntegralGyroYaw with SetPointYaw(StickYaw) |
1331 | // trimm drift of ReadingIntegralGyroYaw with SetPointYaw(StickYaw) |
1329 | ReadingIntegralGyroYaw -= tmp_int; |
1332 | ReadingIntegralGyroYaw -= tmp_int; |
1330 | // limit the effect |
1333 | // limit the effect |
1331 | CHECK_MIN_MAX(ReadingIntegralGyroYaw, -50000, 50000) |
1334 | CHECK_MIN_MAX(ReadingIntegralGyroYaw, -50000, 50000) |
1332 | 1335 | ||
1333 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1336 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1334 | // Compass |
1337 | // Compass |
1335 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1338 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1336 | // compass code is used if Compass option is selected |
1339 | // compass code is used if Compass option is selected |
1337 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
1340 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
1338 | { |
1341 | { |
1339 | int16_t w, v, r,correction, error; |
1342 | int16_t w, v, r,correction, error; |
1340 | 1343 | ||
1341 | if(CompassCalState && !(MKFlags & MKFLAG_MOTOR_RUN) ) |
1344 | if(CompassCalState && !(MKFlags & MKFLAG_MOTOR_RUN) ) |
1342 | { |
1345 | { |
1343 | SetCompassCalState(); |
1346 | SetCompassCalState(); |
1344 | #ifdef USE_KILLAGREG |
1347 | #ifdef USE_KILLAGREG |
1345 | MM3_Calibrate(); |
1348 | MM3_Calibrate(); |
1346 | #endif |
1349 | #endif |
1347 | } |
1350 | } |
1348 | else |
1351 | else |
1349 | { |
1352 | { |
1350 | #ifdef USE_KILLAGREG |
1353 | #ifdef USE_KILLAGREG |
1351 | static uint8_t updCompass = 0; |
1354 | static uint8_t updCompass = 0; |
1352 | if (!updCompass--) |
1355 | if (!updCompass--) |
1353 | { |
1356 | { |
1354 | updCompass = 49; // update only at 2ms*50 = 100ms (10Hz) |
1357 | updCompass = 49; // update only at 2ms*50 = 100ms (10Hz) |
1355 | MM3_Heading(); |
1358 | MM3_Heading(); |
1356 | } |
1359 | } |
1357 | #endif |
1360 | #endif |
1358 | 1361 | ||
1359 | // get maximum attitude angle |
1362 | // get maximum attitude angle |
1360 | w = abs(IntegralGyroNick / 512); |
1363 | w = abs(IntegralGyroNick / 512); |
1361 | v = abs(IntegralGyroRoll / 512); |
1364 | v = abs(IntegralGyroRoll / 512); |
1362 | if(v > w) w = v; |
1365 | if(v > w) w = v; |
1363 | correction = w / 8 + 1; |
1366 | correction = w / 8 + 1; |
1364 | // calculate the deviation of the yaw gyro heading and the compass heading |
1367 | // calculate the deviation of the yaw gyro heading and the compass heading |
1365 | if (CompassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined |
1368 | if (CompassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined |
1366 | else error = ((540 + CompassHeading - (YawGyroHeading / GYRO_DEG_FACTOR)) % 360) - 180; |
1369 | else error = ((540 + CompassHeading - (YawGyroHeading / GYRO_DEG_FACTOR)) % 360) - 180; |
1367 | if(abs(GyroYaw) > 128) // spinning fast |
1370 | if(abs(GyroYaw) > 128) // spinning fast |
1368 | { |
1371 | { |
1369 | error = 0; |
1372 | error = 0; |
1370 | } |
1373 | } |
1371 | if(!BadCompassHeading && w < 25) |
1374 | if(!BadCompassHeading && w < 25) |
1372 | { |
1375 | { |
1373 | YawGyroDrift += error; |
1376 | YawGyroDrift += error; |
1374 | if(UpdateCompassCourse) |
1377 | if(UpdateCompassCourse) |
1375 | { |
1378 | { |
1376 | BeepTime = 200; |
1379 | BeepTime = 200; |
1377 | YawGyroHeading = (int32_t)CompassHeading * GYRO_DEG_FACTOR; |
1380 | YawGyroHeading = (int32_t)CompassHeading * GYRO_DEG_FACTOR; |
1378 | CompassCourse = (int16_t)(YawGyroHeading / GYRO_DEG_FACTOR); |
1381 | CompassCourse = (int16_t)(YawGyroHeading / GYRO_DEG_FACTOR); |
1379 | UpdateCompassCourse = 0; |
1382 | UpdateCompassCourse = 0; |
1380 | } |
1383 | } |
1381 | } |
1384 | } |
1382 | YawGyroHeading += (error * 8) / correction; |
1385 | YawGyroHeading += (error * 8) / correction; |
1383 | w = (w * FCParam.CompassYawEffect) / 32; |
1386 | w = (w * FCParam.CompassYawEffect) / 32; |
1384 | w = FCParam.CompassYawEffect - w; |
1387 | w = FCParam.CompassYawEffect - w; |
1385 | if(w >= 0) |
1388 | if(w >= 0) |
1386 | { |
1389 | { |
1387 | if(!BadCompassHeading) |
1390 | if(!BadCompassHeading) |
1388 | { |
1391 | { |
1389 | v = 64 + (MaxStickNick + MaxStickRoll) / 8; |
1392 | v = 64 + (MaxStickNick + MaxStickRoll) / 8; |
1390 | // calc course deviation |
1393 | // calc course deviation |
1391 | r = ((540 + (YawGyroHeading / GYRO_DEG_FACTOR) - CompassCourse) % 360) - 180; |
1394 | r = ((540 + (YawGyroHeading / GYRO_DEG_FACTOR) - CompassCourse) % 360) - 180; |
1392 | v = (r * w) / v; // align to compass course |
1395 | v = (r * w) / v; // align to compass course |
1393 | // limit yaw rate |
1396 | // limit yaw rate |
1394 | w = 3 * FCParam.CompassYawEffect; |
1397 | w = 3 * FCParam.CompassYawEffect; |
1395 | if (v > w) v = w; |
1398 | if (v > w) v = w; |
1396 | else if (v < -w) v = -w; |
1399 | else if (v < -w) v = -w; |
1397 | ReadingIntegralGyroYaw += v; |
1400 | ReadingIntegralGyroYaw += v; |
1398 | } |
1401 | } |
1399 | else |
1402 | else |
1400 | { // wait a while |
1403 | { // wait a while |
1401 | BadCompassHeading--; |
1404 | BadCompassHeading--; |
1402 | } |
1405 | } |
1403 | } |
1406 | } |
1404 | else |
1407 | else |
1405 | { // ignore compass at extreme attitudes for a while |
1408 | { // ignore compass at extreme attitudes for a while |
1406 | BadCompassHeading = 500; |
1409 | BadCompassHeading = 500; |
1407 | } |
1410 | } |
1408 | } |
1411 | } |
1409 | } |
1412 | } |
1410 | 1413 | ||
1411 | #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG)) |
1414 | #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG)) |
1412 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1415 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1413 | // GPS |
1416 | // GPS |
1414 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1417 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1415 | if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE) |
1418 | if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE) |
1416 | { |
1419 | { |
1417 | GPS_Main(); |
1420 | GPS_Main(); |
1418 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
1421 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
1419 | } |
1422 | } |
1420 | else |
1423 | else |
1421 | { |
1424 | { |
1422 | GPSStickNick = 0; |
1425 | GPSStickNick = 0; |
1423 | GPSStickRoll = 0; |
1426 | GPSStickRoll = 0; |
1424 | } |
1427 | } |
1425 | #endif |
1428 | #endif |
1426 | 1429 | ||
1427 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1430 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1428 | // Debugwerte zuordnen |
1431 | // Debugwerte zuordnen |
1429 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1432 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1430 | if(!TimerDebugOut--) |
1433 | if(!TimerDebugOut--) |
1431 | { |
1434 | { |
1432 | TimerDebugOut = 24; // update debug outputs every 25*2ms = 50 ms (20Hz) |
1435 | TimerDebugOut = 24; // update debug outputs every 25*2ms = 50 ms (20Hz) |
1433 | DebugOut.Analog[0] = (10 * IntegralGyroNick) / GYRO_DEG_FACTOR; // in 0.1 deg |
1436 | DebugOut.Analog[0] = (10 * IntegralGyroNick) / GYRO_DEG_FACTOR; // in 0.1 deg |
1434 | DebugOut.Analog[1] = (10 * IntegralGyroRoll) / GYRO_DEG_FACTOR; // in 0.1 deg |
1437 | DebugOut.Analog[1] = (10 * IntegralGyroRoll) / GYRO_DEG_FACTOR; // in 0.1 deg |
1435 | DebugOut.Analog[2] = (10 * AccNick) / ACC_DEG_FACTOR; // in 0.1 deg |
1438 | DebugOut.Analog[2] = (10 * AccNick) / ACC_DEG_FACTOR; // in 0.1 deg |
1436 | DebugOut.Analog[3] = (10 * AccRoll) / ACC_DEG_FACTOR; // in 0.1 deg |
1439 | DebugOut.Analog[3] = (10 * AccRoll) / ACC_DEG_FACTOR; // in 0.1 deg |
1437 | DebugOut.Analog[4] = GyroYaw; |
1440 | DebugOut.Analog[4] = GyroYaw; |
1438 | DebugOut.Analog[5] = ReadingHeight; |
1441 | DebugOut.Analog[5] = ReadingHeight; |
1439 | DebugOut.Analog[6] = (ReadingIntegralTop / 512); |
1442 | DebugOut.Analog[6] = (ReadingIntegralTop / 512); |
1440 | DebugOut.Analog[8] = CompassHeading; |
1443 | DebugOut.Analog[8] = CompassHeading; |
1441 | DebugOut.Analog[9] = UBat; |
1444 | DebugOut.Analog[9] = UBat; |
1442 | DebugOut.Analog[10] = RC_Quality; |
1445 | DebugOut.Analog[10] = RC_Quality; |
1443 | DebugOut.Analog[11] = YawGyroHeading / GYRO_DEG_FACTOR; |
1446 | DebugOut.Analog[11] = YawGyroHeading / GYRO_DEG_FACTOR; |
1444 | DebugOut.Analog[19] = CompassCalState; |
1447 | DebugOut.Analog[19] = CompassCalState; |
1445 | // DebugOut.Analog[24] = GyroNick/2; |
1448 | // DebugOut.Analog[24] = GyroNick/2; |
1446 | // DebugOut.Analog[25] = GyroRoll/2; |
1449 | // DebugOut.Analog[25] = GyroRoll/2; |
1447 | DebugOut.Analog[27] = (int16_t)FCParam.KalmanMaxDrift; |
1450 | DebugOut.Analog[27] = (int16_t)FCParam.KalmanMaxDrift; |
1448 | // DebugOut.Analog[28] = (int16_t)FCParam.KalmanMaxFusion; |
1451 | // DebugOut.Analog[28] = (int16_t)FCParam.KalmanMaxFusion; |
1449 | // DebugOut.Analog[29] = (int16_t)FCParam.KalmanK; |
1452 | // DebugOut.Analog[29] = (int16_t)FCParam.KalmanK; |
1450 | DebugOut.Analog[30] = GPSStickNick; |
1453 | DebugOut.Analog[30] = GPSStickNick; |
1451 | DebugOut.Analog[31] = GPSStickRoll; |
1454 | DebugOut.Analog[31] = GPSStickRoll; |
1452 | } |
1455 | } |
1453 | 1456 | ||
1454 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1457 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1455 | // calculate control feedback from angle (gyro integral) and agular velocity (gyro signal) |
1458 | // calculate control feedback from angle (gyro integral) and agular velocity (gyro signal) |
1456 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1459 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1457 | 1460 | ||
1458 | #define TRIM_LIMIT 200 |
1461 | #define TRIM_LIMIT 200 |
1459 | CHECK_MIN_MAX(TrimNick, -TRIM_LIMIT, TRIM_LIMIT); |
1462 | CHECK_MIN_MAX(TrimNick, -TRIM_LIMIT, TRIM_LIMIT); |
1460 | CHECK_MIN_MAX(TrimRoll, -TRIM_LIMIT, TRIM_LIMIT); |
1463 | CHECK_MIN_MAX(TrimRoll, -TRIM_LIMIT, TRIM_LIMIT); |
1461 | 1464 | ||
1462 | if(FunnelCourse) |
1465 | if(FunnelCourse) |
1463 | { |
1466 | { |
1464 | IPartNick = 0; |
1467 | IPartNick = 0; |
1465 | IPartRoll = 0; |
1468 | IPartRoll = 0; |
1466 | } |
1469 | } |
1467 | 1470 | ||
1468 | if(! LoopingNick) |
1471 | if(! LoopingNick) |
1469 | { |
1472 | { |
1470 | PPartNick = (IntegralGyroNick * GyroIFactor) / (44000 / STICK_GAIN); // P-Part |
1473 | PPartNick = (IntegralGyroNick * GyroIFactor) / (44000 / STICK_GAIN); // P-Part |
1471 | } |
1474 | } |
1472 | else |
1475 | else |
1473 | { |
1476 | { |
1474 | PPartNick = 0; |
1477 | PPartNick = 0; |
1475 | } |
1478 | } |
1476 | PDPartNick = PPartNick + (int32_t)((int32_t)GyroNick * GyroPFactor + (int32_t)TrimNick * 128L) / (256L / STICK_GAIN); // +D-Part |
1479 | PDPartNick = PPartNick + (int32_t)((int32_t)GyroNick * GyroPFactor + (int32_t)TrimNick * 128L) / (256L / STICK_GAIN); // +D-Part |
1477 | 1480 | ||
1478 | if(!LoopingRoll) |
1481 | if(!LoopingRoll) |
1479 | { |
1482 | { |
1480 | PPartRoll = (IntegralGyroRoll * GyroIFactor) / (44000 / STICK_GAIN); // P-Part |
1483 | PPartRoll = (IntegralGyroRoll * GyroIFactor) / (44000 / STICK_GAIN); // P-Part |
1481 | } |
1484 | } |
1482 | else |
1485 | else |
1483 | { |
1486 | { |
1484 | PPartRoll = 0; |
1487 | PPartRoll = 0; |
1485 | } |
1488 | } |
1486 | PDPartRoll = PPartRoll + (int32_t)((int32_t)GyroRoll * GyroPFactor + (int32_t)TrimRoll * 128L) / (256L / STICK_GAIN); // +D-Part |
1489 | PDPartRoll = PPartRoll + (int32_t)((int32_t)GyroRoll * GyroPFactor + (int32_t)TrimRoll * 128L) / (256L / STICK_GAIN); // +D-Part |
1487 | 1490 | ||
1488 | // octo has a double yaw momentum because of the doubled motor number |
1491 | // octo has a double yaw momentum because of the doubled motor number |
1489 | // therefore double D-Part and halfen P-Part for the same result |
1492 | // therefore double D-Part and halfen P-Part for the same result |
1490 | #ifdef USE_OCTO |
1493 | #ifdef USE_OCTO |
1491 | PDPartYaw = (int32_t)(GyroYaw * 4 * (int32_t)GyroPFactor) / (256L / STICK_GAIN) + (int32_t)(IntegralGyroYaw * GyroIFactor) / (4 * (44000 / STICK_GAIN)); |
1494 | PDPartYaw = (int32_t)(GyroYaw * 4 * (int32_t)GyroPFactor) / (256L / STICK_GAIN) + (int32_t)(IntegralGyroYaw * GyroIFactor) / (4 * (44000 / STICK_GAIN)); |
1492 | #else |
1495 | #else |
1493 | PDPartYaw = (int32_t)(GyroYaw * 2 * (int32_t)GyroPFactor) / (256L / STICK_GAIN) + (int32_t)(IntegralGyroYaw * GyroIFactor) / (2 * (44000 / STICK_GAIN)); |
1496 | PDPartYaw = (int32_t)(GyroYaw * 2 * (int32_t)GyroPFactor) / (256L / STICK_GAIN) + (int32_t)(IntegralGyroYaw * GyroIFactor) / (2 * (44000 / STICK_GAIN)); |
1494 | #endif |
1497 | #endif |
1495 | 1498 | ||
1496 | //DebugOut.Analog[21] = PDPartNick; |
1499 | //DebugOut.Analog[21] = PDPartNick; |
1497 | //DebugOut.Analog[22] = PDPartRoll; |
1500 | //DebugOut.Analog[22] = PDPartRoll; |
1498 | 1501 | ||
1499 | // limit control feedback |
1502 | // limit control feedback |
1500 | #define SENSOR_LIMIT (4096 * 4) |
1503 | #define SENSOR_LIMIT (4096 * 4) |
1501 | CHECK_MIN_MAX(PDPartNick, -SENSOR_LIMIT, SENSOR_LIMIT); |
1504 | CHECK_MIN_MAX(PDPartNick, -SENSOR_LIMIT, SENSOR_LIMIT); |
1502 | CHECK_MIN_MAX(PDPartRoll, -SENSOR_LIMIT, SENSOR_LIMIT); |
1505 | CHECK_MIN_MAX(PDPartRoll, -SENSOR_LIMIT, SENSOR_LIMIT); |
1503 | CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
1506 | CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
1504 | 1507 | ||
1505 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1508 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1506 | // Height Control |
1509 | // Height Control |
1507 | // The height control algorithm reduces the gas but does not increase the gas. |
1510 | // The height control algorithm reduces the gas but does not increase the gas. |
1508 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1511 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1509 | 1512 | ||
1510 | GasMixFraction *= STICK_GAIN; |
1513 | GasMixFraction *= STICK_GAIN; |
1511 | 1514 | ||
1512 | // if height control is activated and no emergency landing is active |
1515 | // if height control is activated and no emergency landing is active |
1513 | if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL) && !(MKFlags & MKFLAG_EMERGENCY_LANDING) ) |
1516 | if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL) && !(MKFlags & MKFLAG_EMERGENCY_LANDING) ) |
1514 | { |
1517 | { |
1515 | int tmp_int; |
1518 | int tmp_int; |
1516 | static uint8_t delay = 100; |
1519 | static uint8_t delay = 100; |
1517 | // if height control is activated by an rc channel |
1520 | // if height control is activated by an rc channel |
1518 | if(ParamSet.GlobalConfig & CFG_HEIGHT_SWITCH) |
1521 | if(ParamSet.GlobalConfig & CFG_HEIGHT_SWITCH) |
1519 | { // check if parameter is less than activation threshold |
1522 | { // check if parameter is less than activation threshold |
1520 | if( |
1523 | if( |
1521 | ( (ParamSet.BitConfig & CFG_HEIGHT_3SWITCH) && ( (FCParam.MaxHeight > 80) && (FCParam.MaxHeight < 140) ) )|| // for 3-state switch height control is only disabled in center position |
1524 | ( (ParamSet.BitConfig & CFG_HEIGHT_3SWITCH) && ( (FCParam.MaxHeight > 80) && (FCParam.MaxHeight < 140) ) )|| // for 3-state switch height control is only disabled in center position |
1522 | (!(ParamSet.BitConfig & CFG_HEIGHT_3SWITCH) && (FCParam.MaxHeight < 50) ) // for 2-State switch height control is disabled in lower position |
1525 | (!(ParamSet.BitConfig & CFG_HEIGHT_3SWITCH) && (FCParam.MaxHeight < 50) ) // for 2-State switch height control is disabled in lower position |
1523 | ) |
1526 | ) |
1524 | { //hight control not active |
1527 | { //hight control not active |
1525 | if(!delay--) |
1528 | if(!delay--) |
1526 | { |
1529 | { |
1527 | // measurement of air pressure close to upper limit |
1530 | // measurement of air pressure close to upper limit |
1528 | if(ReadingAirPressure > 1000) |
1531 | if(ReadingAirPressure > 1000) |
1529 | { // lower offset |
1532 | { // lower offset |
1530 | ExpandBaro -= 10; |
1533 | ExpandBaro -= 10; |
1531 | OCR0A = PressureSensorOffset - ExpandBaro; |
1534 | OCR0A = PressureSensorOffset - ExpandBaro; |
1532 | BeepTime = 300; |
1535 | BeepTime = 300; |
1533 | delay = 250; |
1536 | delay = 250; |
1534 | } |
1537 | } |
1535 | // measurement of air pressure close to lower limit |
1538 | // measurement of air pressure close to lower limit |
1536 | else if(ReadingAirPressure < 100) |
1539 | else if(ReadingAirPressure < 100) |
1537 | { // increase offset |
1540 | { // increase offset |
1538 | ExpandBaro += 10; |
1541 | ExpandBaro += 10; |
1539 | OCR0A = PressureSensorOffset - ExpandBaro; |
1542 | OCR0A = PressureSensorOffset - ExpandBaro; |
1540 | BeepTime = 300; |
1543 | BeepTime = 300; |
1541 | delay = 250; |
1544 | delay = 250; |
1542 | } |
1545 | } |
1543 | else |
1546 | else |
1544 | { |
1547 | { |
1545 | SetPointHeight = ReadingHeight - 20; // update SetPoint with current reading |
1548 | SetPointHeight = ReadingHeight - 20; // update SetPoint with current reading |
1546 | HeightControlActive = 0; // disable height control |
1549 | HeightControlActive = 0; // disable height control |
1547 | delay = 1; |
1550 | delay = 1; |
1548 | } |
1551 | } |
1549 | } |
1552 | } |
1550 | } |
1553 | } |
1551 | else |
1554 | else |
1552 | { //hight control not active |
1555 | { //hight control not active |
1553 | HeightControlActive = 1; // enable height control |
1556 | HeightControlActive = 1; // enable height control |
1554 | delay = 200; |
1557 | delay = 200; |
1555 | } |
1558 | } |
1556 | } |
1559 | } |
1557 | else // no switchable height control |
1560 | else // no switchable height control |
1558 | { |
1561 | { |
1559 | SetPointHeight = ((int16_t) ExternHeightValue + (int16_t) FCParam.MaxHeight) * (int16_t)ParamSet.Height_Gain - 20; |
1562 | SetPointHeight = ((int16_t) ExternHeightValue + (int16_t) FCParam.MaxHeight) * (int16_t)ParamSet.Height_Gain - 20; |
1560 | HeightControlActive = 1; |
1563 | HeightControlActive = 1; |
1561 | } |
1564 | } |
1562 | // get current height |
1565 | // get current height |
1563 | h = ReadingHeight; |
1566 | h = ReadingHeight; |
1564 | // if current height is above the setpoint reduce gas |
1567 | // if current height is above the setpoint reduce gas |
1565 | if((h > SetPointHeight) && HeightControlActive) |
1568 | if((h > SetPointHeight) && HeightControlActive) |
1566 | { |
1569 | { |
1567 | // height difference -> P control part |
1570 | // height difference -> P control part |
1568 | h = ((h - SetPointHeight) * (int16_t) FCParam.HeightP) / (16 / STICK_GAIN); |
1571 | h = ((h - SetPointHeight) * (int16_t) FCParam.HeightP) / (16 / STICK_GAIN); |
1569 | h = GasMixFraction - h; // reduce gas |
1572 | h = GasMixFraction - h; // reduce gas |
1570 | // height gradient --> D control part |
1573 | // height gradient --> D control part |
1571 | //h -= (HeightD * FCParam.HeightD) / (8 / STICK_GAIN); // D control part |
1574 | //h -= (HeightD * FCParam.HeightD) / (8 / STICK_GAIN); // D control part |
1572 | h -= (HeightD) / (8 / STICK_GAIN); // D control part |
1575 | h -= (HeightD) / (8 / STICK_GAIN); // D control part |
1573 | // acceleration sensor effect |
1576 | // acceleration sensor effect |
1574 | tmp_int = ((ReadingIntegralTop / 128) * (int32_t) FCParam.Height_ACC_Effect) / (128 / STICK_GAIN); |
1577 | tmp_int = ((ReadingIntegralTop / 128) * (int32_t) FCParam.Height_ACC_Effect) / (128 / STICK_GAIN); |
1575 | if(tmp_int > 70 * STICK_GAIN) tmp_int = 70 * STICK_GAIN; |
1578 | if(tmp_int > 70 * STICK_GAIN) tmp_int = 70 * STICK_GAIN; |
1576 | else if(tmp_int < -(70 * STICK_GAIN)) tmp_int = -(70 * STICK_GAIN); |
1579 | else if(tmp_int < -(70 * STICK_GAIN)) tmp_int = -(70 * STICK_GAIN); |
1577 | h -= tmp_int; |
1580 | h -= tmp_int; |
1578 | // update height control gas |
1581 | // update height control gas |
1579 | HeightControlGas = (HeightControlGas*15 + h) / 16; |
1582 | HeightControlGas = (HeightControlGas*15 + h) / 16; |
1580 | // limit gas reduction |
1583 | // limit gas reduction |
1581 | if(HeightControlGas < ParamSet.HeightMinGas * STICK_GAIN) |
1584 | if(HeightControlGas < ParamSet.HeightMinGas * STICK_GAIN) |
1582 | { |
1585 | { |
1583 | if(GasMixFraction >= ParamSet.HeightMinGas * STICK_GAIN) HeightControlGas = ParamSet.HeightMinGas * STICK_GAIN; |
1586 | if(GasMixFraction >= ParamSet.HeightMinGas * STICK_GAIN) HeightControlGas = ParamSet.HeightMinGas * STICK_GAIN; |
1584 | // allows landing also if gas stick is reduced below min gas on height control |
1587 | // allows landing also if gas stick is reduced below min gas on height control |
1585 | if(GasMixFraction < ParamSet.HeightMinGas * STICK_GAIN) HeightControlGas = GasMixFraction; |
1588 | if(GasMixFraction < ParamSet.HeightMinGas * STICK_GAIN) HeightControlGas = GasMixFraction; |
1586 | } |
1589 | } |
1587 | // limit gas to stick setting |
1590 | // limit gas to stick setting |
1588 | if(HeightControlGas > GasMixFraction) HeightControlGas = GasMixFraction; |
1591 | if(HeightControlGas > GasMixFraction) HeightControlGas = GasMixFraction; |
1589 | GasMixFraction = HeightControlGas; |
1592 | GasMixFraction = HeightControlGas; |
1590 | } |
1593 | } |
1591 | } |
1594 | } |
1592 | // limit gas to parameter setting |
1595 | // limit gas to parameter setting |
1593 | if(GasMixFraction > (ParamSet.GasMax - 20) * STICK_GAIN) GasMixFraction = (ParamSet.GasMax - 20) * STICK_GAIN; |
1596 | if(GasMixFraction > (ParamSet.GasMax - 20) * STICK_GAIN) GasMixFraction = (ParamSet.GasMax - 20) * STICK_GAIN; |
1594 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1597 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1595 | // + Mixer and PI-Controller |
1598 | // + Mixer and PI-Controller |
1596 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1599 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1597 | DebugOut.Analog[7] = GasMixFraction; |
1600 | DebugOut.Analog[7] = GasMixFraction; |
1598 | 1601 | ||
1599 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1602 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1600 | // Yaw-Fraction |
1603 | // Yaw-Fraction |
1601 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1604 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1602 | YawMixFraction = PDPartYaw - SetPointYaw * STICK_GAIN; // yaw controller |
1605 | YawMixFraction = PDPartYaw - SetPointYaw * STICK_GAIN; // yaw controller |
1603 | #define MIN_YAWGAS (40 * STICK_GAIN) // yaw also below this gas value |
1606 | #define MIN_YAWGAS (40 * STICK_GAIN) // yaw also below this gas value |
1604 | // limit YawMixFraction |
1607 | // limit YawMixFraction |
1605 | if(GasMixFraction > MIN_YAWGAS) |
1608 | if(GasMixFraction > MIN_YAWGAS) |
1606 | { |
1609 | { |
1607 | CHECK_MIN_MAX(YawMixFraction, -(GasMixFraction / 2), (GasMixFraction / 2)); |
1610 | CHECK_MIN_MAX(YawMixFraction, -(GasMixFraction / 2), (GasMixFraction / 2)); |
1608 | } |
1611 | } |
1609 | else |
1612 | else |
1610 | { |
1613 | { |
1611 | CHECK_MIN_MAX(YawMixFraction, -(MIN_YAWGAS / 2), (MIN_YAWGAS / 2)); |
1614 | CHECK_MIN_MAX(YawMixFraction, -(MIN_YAWGAS / 2), (MIN_YAWGAS / 2)); |
1612 | } |
1615 | } |
1613 | tmp_int = ParamSet.GasMax * STICK_GAIN; |
1616 | tmp_int = ParamSet.GasMax * STICK_GAIN; |
1614 | CHECK_MIN_MAX(YawMixFraction, -(tmp_int - GasMixFraction), (tmp_int - GasMixFraction)); |
1617 | CHECK_MIN_MAX(YawMixFraction, -(tmp_int - GasMixFraction), (tmp_int - GasMixFraction)); |
1615 | 1618 | ||
1616 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1619 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1617 | // Nick-Axis |
1620 | // Nick-Axis |
1618 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1621 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1619 | DiffNick = PDPartNick - StickNick; // get difference |
1622 | DiffNick = PDPartNick - StickNick; // get difference |
1620 | if(GyroIFactor) IPartNick += PPartNick - StickNick; // I-part for attitude control |
1623 | if(GyroIFactor) IPartNick += PPartNick - StickNick; // I-part for attitude control |
1621 | else IPartNick += DiffNick; // I-part for head holding |
1624 | else IPartNick += DiffNick; // I-part for head holding |
1622 | CHECK_MIN_MAX(IPartNick, -(STICK_GAIN * 16000L), (STICK_GAIN * 16000L)); |
1625 | CHECK_MIN_MAX(IPartNick, -(STICK_GAIN * 16000L), (STICK_GAIN * 16000L)); |
1623 | NickMixFraction = DiffNick + (IPartNick / Ki); // PID-controller for nick |
1626 | NickMixFraction = DiffNick + (IPartNick / Ki); // PID-controller for nick |
1624 | 1627 | ||
1625 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1628 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1626 | // Roll-Axis |
1629 | // Roll-Axis |
1627 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1630 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1628 | DiffRoll = PDPartRoll - StickRoll; // get difference |
1631 | DiffRoll = PDPartRoll - StickRoll; // get difference |
1629 | if(GyroIFactor) IPartRoll += PPartRoll - StickRoll; // I-part for attitude control |
1632 | if(GyroIFactor) IPartRoll += PPartRoll - StickRoll; // I-part for attitude control |
1630 | else IPartRoll += DiffRoll; // I-part for head holding |
1633 | else IPartRoll += DiffRoll; // I-part for head holding |
1631 | CHECK_MIN_MAX(IPartRoll, -(STICK_GAIN * 16000L), (STICK_GAIN * 16000L)); |
1634 | CHECK_MIN_MAX(IPartRoll, -(STICK_GAIN * 16000L), (STICK_GAIN * 16000L)); |
1632 | RollMixFraction = DiffRoll + (IPartRoll / Ki); // PID-controller for roll |
1635 | RollMixFraction = DiffRoll + (IPartRoll / Ki); // PID-controller for roll |
1633 | 1636 | ||
1634 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1637 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1635 | // Limiter |
1638 | // Limiter |
1636 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1639 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1637 | tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(GasMixFraction + abs(YawMixFraction) / 2)) / 64; |
1640 | tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(GasMixFraction + abs(YawMixFraction) / 2)) / 64; |
1638 | CHECK_MIN_MAX(NickMixFraction, -tmp_int, tmp_int); |
1641 | CHECK_MIN_MAX(NickMixFraction, -tmp_int, tmp_int); |
1639 | CHECK_MIN_MAX(RollMixFraction, -tmp_int, tmp_int); |
1642 | CHECK_MIN_MAX(RollMixFraction, -tmp_int, tmp_int); |
1640 | 1643 | ||
1641 | #ifdef USE_QUADRO |
1644 | #ifdef USE_QUADRO |
1642 | 1645 | ||
1643 | // QuadroKopter Mixer |
1646 | // QuadroKopter Mixer |
1644 | 1647 | ||
1645 | // Motor Front (++) |
1648 | // Motor Front (++) |
1646 | MotorValue = GasMixFraction + NickMixFraction + YawMixFraction; |
1649 | MotorValue = GasMixFraction + NickMixFraction + YawMixFraction; |
1647 | MotorValue1 = MotorSmoothing(MotorValue, MotorValue1); |
1650 | MotorValue1 = MotorSmoothing(MotorValue, MotorValue1); |
1648 | MotorValue = MotorValue1 / STICK_GAIN; |
1651 | MotorValue = MotorValue1 / STICK_GAIN; |
1649 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1652 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1650 | Motor1 = MotorValue; |
1653 | Motor1 = MotorValue; |
1651 | 1654 | ||
1652 | // Motor Rear (-+) |
1655 | // Motor Rear (-+) |
1653 | MotorValue = GasMixFraction - NickMixFraction + YawMixFraction; |
1656 | MotorValue = GasMixFraction - NickMixFraction + YawMixFraction; |
1654 | MotorValue2 = MotorSmoothing(MotorValue, MotorValue2); |
1657 | MotorValue2 = MotorSmoothing(MotorValue, MotorValue2); |
1655 | MotorValue = MotorValue2 / STICK_GAIN; |
1658 | MotorValue = MotorValue2 / STICK_GAIN; |
1656 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1659 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1657 | Motor2 = MotorValue; |
1660 | Motor2 = MotorValue; |
1658 | 1661 | ||
1659 | // Motor Right (--) |
1662 | // Motor Right (--) |
1660 | MotorValue = GasMixFraction - RollMixFraction - YawMixFraction; |
1663 | MotorValue = GasMixFraction - RollMixFraction - YawMixFraction; |
1661 | MotorValue3 = MotorSmoothing(MotorValue, MotorValue3); |
1664 | MotorValue3 = MotorSmoothing(MotorValue, MotorValue3); |
1662 | MotorValue = MotorValue3 / STICK_GAIN; |
1665 | MotorValue = MotorValue3 / STICK_GAIN; |
1663 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1666 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1664 | Motor3 = MotorValue; |
1667 | Motor3 = MotorValue; |
1665 | 1668 | ||
1666 | // Motor Left (+-) |
1669 | // Motor Left (+-) |
1667 | MotorValue = GasMixFraction + RollMixFraction - YawMixFraction; |
1670 | MotorValue = GasMixFraction + RollMixFraction - YawMixFraction; |
1668 | MotorValue4 = MotorSmoothing(MotorValue, MotorValue4); |
1671 | MotorValue4 = MotorSmoothing(MotorValue, MotorValue4); |
1669 | MotorValue = MotorValue4 / STICK_GAIN; |
1672 | MotorValue = MotorValue4 / STICK_GAIN; |
1670 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1673 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1671 | Motor4 = MotorValue; |
1674 | Motor4 = MotorValue; |
1672 | 1675 | ||
1673 | #endif |
1676 | #endif |
1674 | 1677 | ||
1675 | #ifdef USE_OCTO |
1678 | #ifdef USE_OCTO |
1676 | 1679 | ||
1677 | // OctoKopter Mixer |
1680 | // OctoKopter Mixer |
1678 | 1681 | ||
1679 | // Motor 1 (+++) |
1682 | // Motor 1 (+++) |
1680 | MotorValue = GasMixFraction + NickMixFraction + RollMixFraction + YawMixFraction; |
1683 | MotorValue = GasMixFraction + NickMixFraction + RollMixFraction + YawMixFraction; |
1681 | MotorValue1 = MotorSmoothing(MotorValue, MotorValue1); |
1684 | MotorValue1 = MotorSmoothing(MotorValue, MotorValue1); |
1682 | MotorValue = MotorValue1 / STICK_GAIN; |
1685 | MotorValue = MotorValue1 / STICK_GAIN; |
1683 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1686 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1684 | Motor1= MotorValue; |
1687 | Motor1= MotorValue; |
1685 | 1688 | ||
1686 | // Motor 2 (+--) |
1689 | // Motor 2 (+--) |
1687 | MotorValue = GasMixFraction + NickMixFraction - RollMixFraction - YawMixFraction; |
1690 | MotorValue = GasMixFraction + NickMixFraction - RollMixFraction - YawMixFraction; |
1688 | MotorValue2 = MotorSmoothing(MotorValue, MotorValue2); |
1691 | MotorValue2 = MotorSmoothing(MotorValue, MotorValue2); |
1689 | MotorValue = MotorValue2 / STICK_GAIN; |
1692 | MotorValue = MotorValue2 / STICK_GAIN; |
1690 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1693 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1691 | Motor2 = MotorValue; |
1694 | Motor2 = MotorValue; |
1692 | 1695 | ||
1693 | // Motor 3 (+-+) |
1696 | // Motor 3 (+-+) |
1694 | MotorValue = GasMixFraction + NickMixFraction - RollMixFraction + YawMixFraction; |
1697 | MotorValue = GasMixFraction + NickMixFraction - RollMixFraction + YawMixFraction; |
1695 | MotorValue3 = MotorSmoothing(MotorValue, MotorValue3); |
1698 | MotorValue3 = MotorSmoothing(MotorValue, MotorValue3); |
1696 | MotorValue = MotorValue3 / STICK_GAIN; |
1699 | MotorValue = MotorValue3 / STICK_GAIN; |
1697 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1700 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1698 | Motor3 = MotorValue; |
1701 | Motor3 = MotorValue; |
1699 | 1702 | ||
1700 | // Motor 4 (---) |
1703 | // Motor 4 (---) |
1701 | MotorValue = GasMixFraction - NickMixFraction - RollMixFraction - YawMixFraction; |
1704 | MotorValue = GasMixFraction - NickMixFraction - RollMixFraction - YawMixFraction; |
1702 | MotorValue4 = MotorSmoothing(MotorValue, MotorValue4); |
1705 | MotorValue4 = MotorSmoothing(MotorValue, MotorValue4); |
1703 | MotorValue = MotorValue4 / STICK_GAIN; |
1706 | MotorValue = MotorValue4 / STICK_GAIN; |
1704 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1707 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1705 | Motor4 = MotorValue; |
1708 | Motor4 = MotorValue; |
1706 | 1709 | ||
1707 | // Motor 5 (--+) |
1710 | // Motor 5 (--+) |
1708 | MotorValue = GasMixFraction - NickMixFraction - RollMixFraction + YawMixFraction; |
1711 | MotorValue = GasMixFraction - NickMixFraction - RollMixFraction + YawMixFraction; |
1709 | MotorValue5 = MotorSmoothing(MotorValue, MotorValue5); |
1712 | MotorValue5 = MotorSmoothing(MotorValue, MotorValue5); |
1710 | MotorValue = MotorValue5 / STICK_GAIN; |
1713 | MotorValue = MotorValue5 / STICK_GAIN; |
1711 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1714 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1712 | Motor5 = MotorValue; |
1715 | Motor5 = MotorValue; |
1713 | 1716 | ||
1714 | // Motor 6 (-+-) |
1717 | // Motor 6 (-+-) |
1715 | MotorValue = GasMixFraction - NickMixFraction + RollMixFraction - YawMixFraction; |
1718 | MotorValue = GasMixFraction - NickMixFraction + RollMixFraction - YawMixFraction; |
1716 | MotorValue6 = MotorSmoothing(MotorValue, MotorValue6); |
1719 | MotorValue6 = MotorSmoothing(MotorValue, MotorValue6); |
1717 | MotorValue = MotorValue6 / STICK_GAIN; |
1720 | MotorValue = MotorValue6 / STICK_GAIN; |
1718 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1721 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1719 | Motor6 = MotorValue; |
1722 | Motor6 = MotorValue; |
1720 | 1723 | ||
1721 | // Motor7 (-++) |
1724 | // Motor7 (-++) |
1722 | MotorValue = GasMixFraction - NickMixFraction + RollMixFraction + YawMixFraction; |
1725 | MotorValue = GasMixFraction - NickMixFraction + RollMixFraction + YawMixFraction; |
1723 | MotorValue7 = MotorSmoothing(MotorValue, MotorValue7); |
1726 | MotorValue7 = MotorSmoothing(MotorValue, MotorValue7); |
1724 | MotorValue = MotorValue7 / STICK_GAIN; |
1727 | MotorValue = MotorValue7 / STICK_GAIN; |
1725 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1728 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1726 | Motor7 = MotorValue; |
1729 | Motor7 = MotorValue; |
1727 | 1730 | ||
1728 | // Motor8 (++-) |
1731 | // Motor8 (++-) |
1729 | MotorValue = GasMixFraction + NickMixFraction + RollMixFraction - YawMixFraction; |
1732 | MotorValue = GasMixFraction + NickMixFraction + RollMixFraction - YawMixFraction; |
1730 | MotorValue8 = MotorSmoothing(MotorValue, MotorValue8); |
1733 | MotorValue8 = MotorSmoothing(MotorValue, MotorValue8); |
1731 | MotorValue = MotorValue8 / STICK_GAIN; |
1734 | MotorValue = MotorValue8 / STICK_GAIN; |
1732 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1735 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1733 | Motor8 = MotorValue; |
1736 | Motor8 = MotorValue; |
1734 | #endif |
1737 | #endif |
1735 | 1738 | ||
1736 | #ifdef USE_OCTO2 |
1739 | #ifdef USE_OCTO2 |
1737 | 1740 | ||
1738 | // Octokopter Mixer alternativ setup |
1741 | // Octokopter Mixer alternativ setup |
1739 | 1742 | ||
1740 | MotorValue = GasMixFraction + NickMixFraction + YawMixFraction; |
1743 | MotorValue = GasMixFraction + NickMixFraction + YawMixFraction; |
1741 | MotorValue1 = MotorSmoothing(MotorValue, MotorValue1); |
1744 | MotorValue1 = MotorSmoothing(MotorValue, MotorValue1); |
1742 | MotorValue = MotorValue1 / STICK_GAIN; |
1745 | MotorValue = MotorValue1 / STICK_GAIN; |
1743 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1746 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1744 | Motor1 = MotorValue; |
1747 | Motor1 = MotorValue; |
1745 | 1748 | ||
1746 | MotorValue = GasMixFraction + NickMixFraction - RollMixFraction - YawMixFraction; |
1749 | MotorValue = GasMixFraction + NickMixFraction - RollMixFraction - YawMixFraction; |
1747 | MotorValue2 = MotorSmoothing(MotorValue, MotorValue2); |
1750 | MotorValue2 = MotorSmoothing(MotorValue, MotorValue2); |
1748 | MotorValue = MotorValue2 / STICK_GAIN; |
1751 | MotorValue = MotorValue2 / STICK_GAIN; |
1749 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1752 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1750 | Motor2 = MotorValue; |
1753 | Motor2 = MotorValue; |
1751 | 1754 | ||
1752 | MotorValue = GasMixFraction - RollMixFraction + YawMixFraction; |
1755 | MotorValue = GasMixFraction - RollMixFraction + YawMixFraction; |
1753 | MotorValue3 = MotorSmoothing(MotorValue, MotorValue3); |
1756 | MotorValue3 = MotorSmoothing(MotorValue, MotorValue3); |
1754 | MotorValue = MotorValue3 / STICK_GAIN; |
1757 | MotorValue = MotorValue3 / STICK_GAIN; |
1755 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1758 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1756 | Motor3 = MotorValue; |
1759 | Motor3 = MotorValue; |
1757 | 1760 | ||
1758 | MotorValue = GasMixFraction - NickMixFraction - RollMixFraction - YawMixFraction; |
1761 | MotorValue = GasMixFraction - NickMixFraction - RollMixFraction - YawMixFraction; |
1759 | MotorValue4 = MotorSmoothing(MotorValue, MotorValue4); |
1762 | MotorValue4 = MotorSmoothing(MotorValue, MotorValue4); |
1760 | MotorValue = MotorValue4 / STICK_GAIN; |
1763 | MotorValue = MotorValue4 / STICK_GAIN; |
1761 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1764 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1762 | Motor4 = MotorValue; |
1765 | Motor4 = MotorValue; |
1763 | 1766 | ||
1764 | MotorValue = GasMixFraction - RollMixFraction + YawMixFraction; |
1767 | MotorValue = GasMixFraction - RollMixFraction + YawMixFraction; |
1765 | MotorValue5 = MotorSmoothing(MotorValue, MotorValue5); |
1768 | MotorValue5 = MotorSmoothing(MotorValue, MotorValue5); |
1766 | MotorValue = MotorValue5 / STICK_GAIN; |
1769 | MotorValue = MotorValue5 / STICK_GAIN; |
1767 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1770 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1768 | Motor5 = MotorValue; |
1771 | Motor5 = MotorValue; |
1769 | 1772 | ||
1770 | MotorValue = GasMixFraction - NickMixFraction + RollMixFraction - YawMixFraction; |
1773 | MotorValue = GasMixFraction - NickMixFraction + RollMixFraction - YawMixFraction; |
1771 | MotorValue6 = MotorSmoothing(MotorValue, MotorValue6); |
1774 | MotorValue6 = MotorSmoothing(MotorValue, MotorValue6); |
1772 | MotorValue = MotorValue6 / STICK_GAIN; |
1775 | MotorValue = MotorValue6 / STICK_GAIN; |
1773 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1776 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1774 | Motor6 = MotorValue; |
1777 | Motor6 = MotorValue; |
1775 | 1778 | ||
1776 | MotorValue = GasMixFraction + RollMixFraction + YawMixFraction; |
1779 | MotorValue = GasMixFraction + RollMixFraction + YawMixFraction; |
1777 | MotorValue7 = MotorSmoothing(MotorValue, MotorValue7); |
1780 | MotorValue7 = MotorSmoothing(MotorValue, MotorValue7); |
1778 | MotorValue = MotorValue7 / STICK_GAIN; |
1781 | MotorValue = MotorValue7 / STICK_GAIN; |
1779 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1782 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1780 | Motor7 = MotorValue; |
1783 | Motor7 = MotorValue; |
1781 | 1784 | ||
1782 | MotorValue = GasMixFraction + NickMixFraction + RollMixFraction - YawMixFraction; |
1785 | MotorValue = GasMixFraction + NickMixFraction + RollMixFraction - YawMixFraction; |
1783 | MotorValue8 = MotorSmoothing(MotorValue, MotorValue8); |
1786 | MotorValue8 = MotorSmoothing(MotorValue, MotorValue8); |
1784 | MotorValue = MotorValue8 / STICK_GAIN; |
1787 | MotorValue = MotorValue8 / STICK_GAIN; |
1785 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1788 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1786 | Motor8 = MotorValue; |
1789 | Motor8 = MotorValue; |
1787 | #endif |
1790 | #endif |
1788 | 1791 | ||
1789 | #ifdef USE_OCTO3 |
1792 | #ifdef USE_OCTO3 |
1790 | 1793 | ||
1791 | // Octokopter Mixer alternativ setup |
1794 | // Octokopter Mixer alternativ setup |
1792 | 1795 | ||
1793 | MotorValue = GasMixFraction + NickMixFraction + YawMixFraction; |
1796 | MotorValue = GasMixFraction + NickMixFraction + YawMixFraction; |
1794 | MotorValue1 = MotorSmoothing(MotorValue, MotorValue1); |
1797 | MotorValue1 = MotorSmoothing(MotorValue, MotorValue1); |
1795 | MotorValue = MotorValue1 / STICK_GAIN; |
1798 | MotorValue = MotorValue1 / STICK_GAIN; |
1796 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1799 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1797 | Motor1 = MotorValue; |
1800 | Motor1 = MotorValue; |
1798 | 1801 | ||
1799 | MotorValue = GasMixFraction + NickMixFraction - YawMixFraction; |
1802 | MotorValue = GasMixFraction + NickMixFraction - YawMixFraction; |
1800 | MotorValue2 = MotorSmoothing(MotorValue, MotorValue2); |
1803 | MotorValue2 = MotorSmoothing(MotorValue, MotorValue2); |
1801 | MotorValue = MotorValue2 / STICK_GAIN; |
1804 | MotorValue = MotorValue2 / STICK_GAIN; |
1802 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1805 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1803 | Motor2 = MotorValue; |
1806 | Motor2 = MotorValue; |
1804 | 1807 | ||
1805 | MotorValue = GasMixFraction - RollMixFraction + YawMixFraction; |
1808 | MotorValue = GasMixFraction - RollMixFraction + YawMixFraction; |
1806 | MotorValue3 = MotorSmoothing(MotorValue, MotorValue3); |
1809 | MotorValue3 = MotorSmoothing(MotorValue, MotorValue3); |
1807 | MotorValue = MotorValue3 / STICK_GAIN; |
1810 | MotorValue = MotorValue3 / STICK_GAIN; |
1808 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1811 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1809 | Motor3 = MotorValue; |
1812 | Motor3 = MotorValue; |
1810 | 1813 | ||
1811 | MotorValue = GasMixFraction - RollMixFraction - YawMixFraction; |
1814 | MotorValue = GasMixFraction - RollMixFraction - YawMixFraction; |
1812 | MotorValue4 = MotorSmoothing(MotorValue, MotorValue4); |
1815 | MotorValue4 = MotorSmoothing(MotorValue, MotorValue4); |
1813 | MotorValue = MotorValue4 / STICK_GAIN; |
1816 | MotorValue = MotorValue4 / STICK_GAIN; |
1814 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1817 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1815 | Motor4 = MotorValue; |
1818 | Motor4 = MotorValue; |
1816 | 1819 | ||
1817 | MotorValue = GasMixFraction - NickMixFraction + YawMixFraction; |
1820 | MotorValue = GasMixFraction - NickMixFraction + YawMixFraction; |
1818 | MotorValue5 = MotorSmoothing(MotorValue, MotorValue5); |
1821 | MotorValue5 = MotorSmoothing(MotorValue, MotorValue5); |
1819 | MotorValue = MotorValue5 / STICK_GAIN; |
1822 | MotorValue = MotorValue5 / STICK_GAIN; |
1820 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1823 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1821 | Motor5 = MotorValue; |
1824 | Motor5 = MotorValue; |
1822 | 1825 | ||
1823 | MotorValue = GasMixFraction - NickMixFraction - YawMixFraction; |
1826 | MotorValue = GasMixFraction - NickMixFraction - YawMixFraction; |
1824 | MotorValue6 = MotorSmoothing(MotorValue, MotorValue6); |
1827 | MotorValue6 = MotorSmoothing(MotorValue, MotorValue6); |
1825 | MotorValue = MotorValue6 / STICK_GAIN; |
1828 | MotorValue = MotorValue6 / STICK_GAIN; |
1826 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1829 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1827 | Motor6 = MotorValue; |
1830 | Motor6 = MotorValue; |
1828 | 1831 | ||
1829 | MotorValue = GasMixFraction + RollMixFraction + YawMixFraction; |
1832 | MotorValue = GasMixFraction + RollMixFraction + YawMixFraction; |
1830 | MotorValue7 = MotorSmoothing(MotorValue, MotorValue7); |
1833 | MotorValue7 = MotorSmoothing(MotorValue, MotorValue7); |
1831 | MotorValue = MotorValue7 / STICK_GAIN; |
1834 | MotorValue = MotorValue7 / STICK_GAIN; |
1832 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1835 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1833 | Motor7 = MotorValue; |
1836 | Motor7 = MotorValue; |
1834 | 1837 | ||
1835 | MotorValue = GasMixFraction + RollMixFraction - YawMixFraction; |
1838 | MotorValue = GasMixFraction + RollMixFraction - YawMixFraction; |
1836 | MotorValue8 = MotorSmoothing(MotorValue, MotorValue8); |
1839 | MotorValue8 = MotorSmoothing(MotorValue, MotorValue8); |
1837 | MotorValue = MotorValue8 / STICK_GAIN; |
1840 | MotorValue = MotorValue8 / STICK_GAIN; |
1838 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1841 | CHECK_MIN_MAX(MotorValue, ParamSet.GasMin, ParamSet.GasMax); |
1839 | Motor8 = MotorValue; |
1842 | Motor8 = MotorValue; |
1840 | #endif |
1843 | #endif |
1841 | 1844 | ||
1842 | 1845 | ||
1843 | } |
1846 | } |
1844 | 1847 | ||
1845 | 1848 |