Rev 957 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 957 | Rev 958 | ||
---|---|---|---|
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 "_Settings.h" |
60 | #include "_Settings.h" |
61 | #include "analog.h" |
61 | #include "analog.h" |
62 | #include "fc.h" |
62 | #include "fc.h" |
63 | #include "uart.h" |
63 | #include "uart.h" |
64 | #include "rc.h" |
64 | #include "rc.h" |
65 | #include "twimaster.h" |
65 | #include "twimaster.h" |
66 | #include "timer2.h" |
66 | #include "timer2.h" |
67 | #ifdef USE_KILLAGREG |
67 | #ifdef USE_KILLAGREG |
68 | #include "mm3.h" |
68 | #include "mm3.h" |
69 | #include "gps.h" |
69 | #include "gps.h" |
70 | #endif |
70 | #endif |
71 | #ifdef USE_MK3MAG |
71 | #ifdef USE_MK3MAG |
72 | #include "mk3mag.h" |
72 | #include "mk3mag.h" |
73 | #include "gps.h" |
73 | #include "gps.h" |
74 | #endif |
74 | #endif |
75 | #include "led.h" |
75 | #include "led.h" |
76 | 76 | ||
77 | // gyro readings |
77 | // gyro readings |
78 | int16_t Reading_GyroNick, Reading_GyroRoll, Reading_GyroYaw; |
78 | int16_t Reading_GyroNick, Reading_GyroRoll, Reading_GyroYaw; |
79 | // gyro neutral readings |
79 | // gyro neutral readings |
80 | int16_t AdNeutralNick = 0, AdNeutralRoll = 0, AdNeutralYaw = 0; |
80 | int16_t AdNeutralNick = 0, AdNeutralRoll = 0, AdNeutralYaw = 0; |
81 | int16_t StartNeutralRoll = 0, StartNeutralNick = 0; |
81 | int16_t StartNeutralRoll = 0, StartNeutralNick = 0; |
82 | // mean accelerations |
82 | // mean accelerations |
83 | int16_t Mean_AccNick, Mean_AccRoll, Mean_AccTop; |
83 | int16_t Mean_AccNick, Mean_AccRoll, Mean_AccTop; |
84 | 84 | ||
85 | // neutral acceleration readings |
85 | // neutral acceleration readings |
86 | volatile int16_t NeutralAccX=0, NeutralAccY=0; |
86 | volatile int16_t NeutralAccX=0, NeutralAccY=0; |
87 | volatile float NeutralAccZ = 0; |
87 | volatile float NeutralAccZ = 0; |
88 | 88 | ||
89 | // attitude gyro integrals |
89 | // attitude gyro integrals |
90 | int32_t IntegralNick = 0,IntegralNick2 = 0; |
90 | int32_t IntegralNick = 0,IntegralNick2 = 0; |
91 | int32_t IntegralRoll = 0,IntegralRoll2 = 0; |
91 | int32_t IntegralRoll = 0,IntegralRoll2 = 0; |
92 | int32_t IntegralYaw = 0; |
92 | int32_t IntegralYaw = 0; |
93 | int32_t Reading_IntegralGyroNick = 0, Reading_IntegralGyroNick2 = 0; |
93 | int32_t Reading_IntegralGyroNick = 0, Reading_IntegralGyroNick2 = 0; |
94 | int32_t Reading_IntegralGyroRoll = 0, Reading_IntegralGyroRoll2 = 0; |
94 | int32_t Reading_IntegralGyroRoll = 0, Reading_IntegralGyroRoll2 = 0; |
95 | int32_t Reading_IntegralGyroYaw = 0; |
95 | int32_t Reading_IntegralGyroYaw = 0; |
96 | int32_t MeanIntegralNick; |
96 | int32_t MeanIntegralNick; |
97 | int32_t MeanIntegralRoll; |
97 | int32_t MeanIntegralRoll; |
98 | 98 | ||
99 | // attitude acceleration integrals |
99 | // attitude acceleration integrals |
100 | int32_t IntegralAccNick = 0, IntegralAccRoll = 0; |
100 | int32_t IntegralAccNick = 0, IntegralAccRoll = 0; |
101 | volatile int32_t Reading_Integral_Top = 0; |
101 | volatile int32_t Reading_Integral_Top = 0; |
102 | 102 | ||
103 | // compass course |
103 | // compass course |
104 | volatile int16_t CompassHeading = -1; // negative angle indicates invalid data. |
104 | volatile int16_t CompassHeading = -1; // negative angle indicates invalid data. |
105 | volatile int16_t CompassCourse = -1; |
105 | volatile int16_t CompassCourse = -1; |
106 | volatile int16_t CompassOffCourse = 0; |
106 | volatile int16_t CompassOffCourse = 0; |
107 | volatile uint8_t CompassCalState = 0; |
107 | volatile uint8_t CompassCalState = 0; |
108 | uint8_t FunnelCourse = 0; |
108 | uint8_t FunnelCourse = 0; |
109 | uint16_t BadCompassHeading = 500; |
109 | uint16_t BadCompassHeading = 500; |
110 | int32_t YawGyroHeading; |
110 | int32_t YawGyroHeading; |
111 | int16_t YawGyroDrift; |
111 | int16_t YawGyroDrift; |
112 | 112 | ||
113 | 113 | ||
114 | int16_t NaviAccNick = 0, NaviAccRoll = 0, NaviCntAcc = 0; |
114 | int16_t NaviAccNick = 0, NaviAccRoll = 0, NaviCntAcc = 0; |
115 | 115 | ||
116 | 116 | ||
117 | // MK flags |
117 | // MK flags |
118 | uint16_t Model_Is_Flying = 0; |
118 | uint16_t Model_Is_Flying = 0; |
119 | volatile uint8_t MKFlags = 0; |
119 | volatile uint8_t MKFlags = 0; |
120 | 120 | ||
121 | int32_t TurnOver180Nick = 250000L, TurnOver180Roll = 250000L; |
121 | int32_t TurnOver180Nick = 250000L, TurnOver180Roll = 250000L; |
122 | 122 | ||
123 | float Gyro_P_Factor; |
123 | float Gyro_P_Factor; |
124 | float Gyro_I_Factor; |
124 | float Gyro_I_Factor; |
125 | 125 | ||
126 | int16_t DiffNick, DiffRoll; |
126 | int16_t DiffNick, DiffRoll; |
127 | 127 | ||
128 | int16_t Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0; |
128 | int16_t Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0; |
129 | 129 | ||
130 | // setpoints for motors |
130 | // setpoints for motors |
131 | #ifdef HEXAKOPTER |
131 | #ifdef HEXAKOPTER |
132 | volatile uint8_t Motor_FrontLeft, Motor_FrontRight, Motor_RearLeft, Motor_RearRight, Motor_Right, Motor_Left; |
132 | volatile uint8_t Motor_FrontLeft, Motor_FrontRight, Motor_RearLeft, Motor_RearRight, Motor_Right, Motor_Left; |
133 | #else |
133 | #else |
134 | volatile uint8_t Motor_Front, Motor_Rear, Motor_Right, Motor_Left; //used by twimaster isr |
134 | volatile uint8_t Motor_Front, Motor_Rear, Motor_Right, Motor_Left; //used by twimaster isr |
135 | #endif |
135 | #endif |
136 | 136 | ||
137 | // stick values derived by rc channels readings |
137 | // stick values derived by rc channels readings |
138 | int16_t StickNick = 0, StickRoll = 0, StickYaw = 0, StickGas = 0; |
138 | int16_t StickNick = 0, StickRoll = 0, StickYaw = 0, StickGas = 0; |
139 | int16_t GPS_Nick = 0, GPS_Roll = 0; |
139 | int16_t GPS_Nick = 0, GPS_Roll = 0; |
140 | 140 | ||
141 | int16_t MaxStickNick = 0, MaxStickRoll = 0; |
141 | int16_t MaxStickNick = 0, MaxStickRoll = 0; |
142 | // stick values derived by uart inputs |
142 | // stick values derived by uart inputs |
143 | int16_t ExternStickNick = 0, ExternStickRoll = 0, ExternStickYaw = 0, ExternHeightValue = -20; |
143 | int16_t ExternStickNick = 0, ExternStickRoll = 0, ExternStickYaw = 0, ExternHeightValue = -20; |
144 | 144 | ||
145 | 145 | ||
146 | 146 | ||
147 | 147 | ||
148 | int16_t ReadingHeight = 0; |
148 | int16_t ReadingHeight = 0; |
149 | int16_t SetPointHeight = 0; |
149 | int16_t SetPointHeight = 0; |
150 | 150 | ||
151 | int16_t AttitudeCorrectionRoll = 0, AttitudeCorrectionNick = 0; |
151 | int16_t AttitudeCorrectionRoll = 0, AttitudeCorrectionNick = 0; |
152 | 152 | ||
153 | float Ki = FACTOR_I; |
153 | float Ki = FACTOR_I; |
154 | 154 | ||
155 | uint8_t Looping_Nick = 0, Looping_Roll = 0; |
155 | uint8_t Looping_Nick = 0, Looping_Roll = 0; |
156 | uint8_t Looping_Left = 0, Looping_Right = 0, Looping_Down = 0, Looping_Top = 0; |
156 | uint8_t Looping_Left = 0, Looping_Right = 0, Looping_Down = 0, Looping_Top = 0; |
157 | 157 | ||
158 | 158 | ||
159 | fc_param_t FCParam = {48,251,16,58,64,150,150,2,10,0,0,0,0,0,0,0,0,100,70,0,0,100}; |
159 | fc_param_t FCParam = {48,251,16,58,64,150,150,2,10,0,0,0,0,0,0,0,0,100,70,0,0,100}; |
160 | 160 | ||
161 | 161 | ||
162 | /************************************************************************/ |
162 | /************************************************************************/ |
163 | /* Creates numbeeps beeps at the speaker */ |
163 | /* Creates numbeeps beeps at the speaker */ |
164 | /************************************************************************/ |
164 | /************************************************************************/ |
165 | void Beep(uint8_t numbeeps) |
165 | void Beep(uint8_t numbeeps) |
166 | { |
166 | { |
167 | while(numbeeps--) |
167 | while(numbeeps--) |
168 | { |
168 | { |
169 | if(MKFlags & MKFLAG_MOTOR_RUN) return; //auf keinen Fall bei laufenden Motoren! |
169 | if(MKFlags & MKFLAG_MOTOR_RUN) return; //auf keinen Fall bei laufenden Motoren! |
170 | BeepTime = 100; // 0.1 second |
170 | BeepTime = 100; // 0.1 second |
171 | Delay_ms(250); // blocks 250 ms as pause to next beep, |
171 | Delay_ms(250); // blocks 250 ms as pause to next beep, |
172 | // this will block the flight control loop, |
172 | // this will block the flight control loop, |
173 | // therefore do not use this function if motors are running |
173 | // therefore do not use this function if motors are running |
174 | } |
174 | } |
175 | } |
175 | } |
176 | 176 | ||
177 | /************************************************************************/ |
177 | /************************************************************************/ |
178 | /* Neutral Readings */ |
178 | /* Neutral Readings */ |
179 | /************************************************************************/ |
179 | /************************************************************************/ |
180 | void SetNeutral(void) |
180 | void SetNeutral(void) |
181 | { |
181 | { |
182 | NeutralAccX = 0; |
182 | NeutralAccX = 0; |
183 | NeutralAccY = 0; |
183 | NeutralAccY = 0; |
184 | NeutralAccZ = 0; |
184 | NeutralAccZ = 0; |
185 | AdNeutralNick = 0; |
185 | AdNeutralNick = 0; |
186 | AdNeutralRoll = 0; |
186 | AdNeutralRoll = 0; |
187 | AdNeutralYaw = 0; |
187 | AdNeutralYaw = 0; |
188 | FCParam.Yaw_PosFeedback = 0; |
188 | FCParam.Yaw_PosFeedback = 0; |
189 | FCParam.Yaw_NegFeedback = 0; |
189 | FCParam.Yaw_NegFeedback = 0; |
190 | CalibMean(); |
190 | CalibMean(); |
191 | Delay_ms_Mess(100); |
191 | Delay_ms_Mess(100); |
192 | CalibMean(); |
192 | CalibMean(); |
193 | if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL)) // Height Control activated? |
193 | if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL)) // Height Control activated? |
194 | { |
194 | { |
195 | if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset(); |
195 | if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset(); |
196 | } |
196 | } |
197 | AdNeutralNick = AdValueGyrNick; |
197 | AdNeutralNick = AdValueGyrNick; |
198 | AdNeutralRoll = AdValueGyrRoll; |
198 | AdNeutralRoll = AdValueGyrRoll; |
199 | AdNeutralYaw = AdValueGyrYaw; |
199 | AdNeutralYaw = AdValueGyrYaw; |
200 | StartNeutralRoll = AdNeutralRoll; |
200 | StartNeutralRoll = AdNeutralRoll; |
201 | StartNeutralNick = AdNeutralNick; |
201 | StartNeutralNick = AdNeutralNick; |
202 | if(GetParamWord(PID_ACC_NICK) > 1023) |
202 | if(GetParamWord(PID_ACC_NICK) > 1023) |
203 | { |
203 | { |
204 | NeutralAccY = abs(Mean_AccRoll) / ACC_AMPLIFY; |
204 | NeutralAccY = abs(Mean_AccRoll) / ACC_AMPLIFY; |
205 | NeutralAccX = abs(Mean_AccNick) / ACC_AMPLIFY; |
205 | NeutralAccX = abs(Mean_AccNick) / ACC_AMPLIFY; |
206 | NeutralAccZ = Current_AccZ; |
206 | NeutralAccZ = Current_AccZ; |
207 | } |
207 | } |
208 | else |
208 | else |
209 | { |
209 | { |
210 | NeutralAccX = (int16_t)GetParamWord(PID_ACC_NICK); |
210 | NeutralAccX = (int16_t)GetParamWord(PID_ACC_NICK); |
211 | NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL); |
211 | NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL); |
212 | NeutralAccZ = (int16_t)GetParamWord(PID_ACC_Z); |
212 | NeutralAccZ = (int16_t)GetParamWord(PID_ACC_Z); |
213 | } |
213 | } |
214 | Reading_IntegralGyroNick = 0; |
214 | Reading_IntegralGyroNick = 0; |
215 | Reading_IntegralGyroNick2 = 0; |
215 | Reading_IntegralGyroNick2 = 0; |
216 | Reading_IntegralGyroRoll = 0; |
216 | Reading_IntegralGyroRoll = 0; |
217 | Reading_IntegralGyroRoll2 = 0; |
217 | Reading_IntegralGyroRoll2 = 0; |
218 | Reading_IntegralGyroYaw = 0; |
218 | Reading_IntegralGyroYaw = 0; |
219 | Reading_GyroNick = 0; |
219 | Reading_GyroNick = 0; |
220 | Reading_GyroRoll = 0; |
220 | Reading_GyroRoll = 0; |
221 | Reading_GyroYaw = 0; |
221 | Reading_GyroYaw = 0; |
222 | Delay_ms_Mess(100); |
222 | Delay_ms_Mess(100); |
223 | StartAirPressure = AirPressure; |
223 | StartAirPressure = AirPressure; |
224 | HeightD = 0; |
224 | HeightD = 0; |
225 | Reading_Integral_Top = 0; |
225 | Reading_Integral_Top = 0; |
226 | CompassCourse = CompassHeading; |
226 | CompassCourse = CompassHeading; |
227 | BeepTime = 50; |
227 | BeepTime = 50; |
228 | TurnOver180Nick = ((int32_t) ParamSet.AngleTurnOverNick * 2500L) +15000L; |
228 | TurnOver180Nick = ((int32_t) ParamSet.AngleTurnOverNick * 2500L) +15000L; |
229 | TurnOver180Roll = ((int32_t) ParamSet.AngleTurnOverRoll * 2500L) +15000L; |
229 | TurnOver180Roll = ((int32_t) ParamSet.AngleTurnOverRoll * 2500L) +15000L; |
230 | ExternHeightValue = 0; |
230 | ExternHeightValue = 0; |
231 | GPS_Nick = 0; |
231 | GPS_Nick = 0; |
232 | GPS_Roll = 0; |
232 | GPS_Roll = 0; |
233 | YawGyroHeading = CompassHeading * YAW_GYRO_DEG_FACTOR; |
233 | YawGyroHeading = CompassHeading * YAW_GYRO_DEG_FACTOR; |
234 | YawGyroDrift = 0; |
234 | YawGyroDrift = 0; |
235 | MKFlags |= MKFLAG_CALIBRATE; |
235 | MKFlags |= MKFLAG_CALIBRATE; |
236 | } |
236 | } |
237 | 237 | ||
238 | /************************************************************************/ |
238 | /************************************************************************/ |
239 | /* Averaging Measurement Readings */ |
239 | /* Averaging Measurement Readings */ |
240 | /************************************************************************/ |
240 | /************************************************************************/ |
241 | void Mean(void) |
241 | void Mean(void) |
242 | { |
242 | { |
243 | static int32_t tmpl,tmpl2; |
243 | static int32_t tmpl,tmpl2; |
244 | 244 | ||
245 | // Get offset corrected gyro readings (~ to angular velocity) |
245 | // Get offset corrected gyro readings (~ to angular velocity) |
246 | Reading_GyroYaw = AdNeutralYaw - AdValueGyrYaw; |
246 | Reading_GyroYaw = AdNeutralYaw - AdValueGyrYaw; |
247 | Reading_GyroRoll = AdValueGyrRoll - AdNeutralRoll; |
247 | Reading_GyroRoll = AdValueGyrRoll - AdNeutralRoll; |
248 | Reading_GyroNick = AdValueGyrNick - AdNeutralNick; |
248 | Reading_GyroNick = AdValueGyrNick - AdNeutralNick; |
249 | 249 | ||
250 | // Acceleration Sensor |
250 | // Acceleration Sensor |
251 | // sliding average sensor readings |
251 | // sliding average sensor readings |
252 | Mean_AccNick = ((int32_t)Mean_AccNick * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccNick))) / 2L; |
252 | Mean_AccNick = ((int32_t)Mean_AccNick * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccNick))) / 2L; |
253 | Mean_AccRoll = ((int32_t)Mean_AccRoll * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccRoll))) / 2L; |
253 | Mean_AccRoll = ((int32_t)Mean_AccRoll * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccRoll))) / 2L; |
254 | Mean_AccTop = ((int32_t)Mean_AccTop * 1 + ((int32_t)AdValueAccTop)) / 2L; |
254 | Mean_AccTop = ((int32_t)Mean_AccTop * 1 + ((int32_t)AdValueAccTop)) / 2L; |
255 | 255 | ||
256 | // sum sensor readings for later averaging |
256 | // sum sensor readings for later averaging |
257 | IntegralAccNick += ACC_AMPLIFY * AdValueAccNick; |
257 | IntegralAccNick += ACC_AMPLIFY * AdValueAccNick; |
258 | IntegralAccRoll += ACC_AMPLIFY * AdValueAccRoll; |
258 | IntegralAccRoll += ACC_AMPLIFY * AdValueAccRoll; |
259 | 259 | ||
260 | NaviAccNick += AdValueAccNick; |
260 | NaviAccNick += AdValueAccNick; |
261 | NaviAccRoll += AdValueAccRoll; |
261 | NaviAccRoll += AdValueAccRoll; |
262 | NaviCntAcc++; |
262 | NaviCntAcc++; |
263 | 263 | ||
264 | // Yaw |
264 | // Yaw |
265 | // calculate yaw gyro integral (~ to rotation angle) |
265 | // calculate yaw gyro integral (~ to rotation angle) |
266 | Reading_IntegralGyroYaw += Reading_GyroYaw; |
266 | Reading_IntegralGyroYaw += Reading_GyroYaw; |
267 | YawGyroHeading += Reading_GyroYaw; |
267 | YawGyroHeading += Reading_GyroYaw; |
268 | if(YawGyroHeading >= (360L * YAW_GYRO_DEG_FACTOR)) YawGyroHeading -= 360L * YAW_GYRO_DEG_FACTOR; // 360° Wrap |
268 | if(YawGyroHeading >= (360L * YAW_GYRO_DEG_FACTOR)) YawGyroHeading -= 360L * YAW_GYRO_DEG_FACTOR; // 360° Wrap |
269 | if(YawGyroHeading < 0) YawGyroHeading += 360L * YAW_GYRO_DEG_FACTOR; |
269 | if(YawGyroHeading < 0) YawGyroHeading += 360L * YAW_GYRO_DEG_FACTOR; |
270 | 270 | ||
271 | 271 | ||
272 | // Coupling fraction |
272 | // Coupling fraction |
273 | if(!Looping_Nick && !Looping_Roll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) |
273 | if(!Looping_Nick && !Looping_Roll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) |
274 | { |
274 | { |
275 | tmpl = (Reading_GyroYaw * Reading_IntegralGyroNick) / 2048L; |
275 | tmpl = (Reading_GyroYaw * Reading_IntegralGyroNick) / 2048L; |
276 | tmpl *= FCParam.Yaw_PosFeedback; |
276 | tmpl *= FCParam.Yaw_PosFeedback; |
277 | tmpl /= 4096L; |
277 | tmpl /= 4096L; |
278 | tmpl2 = ( Reading_GyroYaw * Reading_IntegralGyroRoll) / 2048L; |
278 | tmpl2 = ( Reading_GyroYaw * Reading_IntegralGyroRoll) / 2048L; |
279 | tmpl2 *= FCParam.Yaw_PosFeedback; |
279 | tmpl2 *= FCParam.Yaw_PosFeedback; |
280 | tmpl2 /= 4096L; |
280 | tmpl2 /= 4096L; |
281 | if(labs(tmpl) > 128 || labs(tmpl2) > 128) FunnelCourse = 1; |
281 | if(labs(tmpl) > 128 || labs(tmpl2) > 128) FunnelCourse = 1; |
282 | } |
282 | } |
283 | else tmpl = tmpl2 = 0; |
283 | else tmpl = tmpl2 = 0; |
284 | 284 | ||
285 | // Roll |
285 | // Roll |
286 | Reading_GyroRoll += tmpl; |
286 | Reading_GyroRoll += tmpl; |
287 | Reading_GyroRoll += (tmpl2 * FCParam.Yaw_NegFeedback) / 512L; |
287 | Reading_GyroRoll += (tmpl2 * FCParam.Yaw_NegFeedback) / 512L; |
288 | Reading_IntegralGyroRoll2 += Reading_GyroRoll; |
288 | Reading_IntegralGyroRoll2 += Reading_GyroRoll; |
289 | Reading_IntegralGyroRoll += Reading_GyroRoll - AttitudeCorrectionRoll; |
289 | Reading_IntegralGyroRoll += Reading_GyroRoll - AttitudeCorrectionRoll; |
290 | if(Reading_IntegralGyroRoll > TurnOver180Roll) |
290 | if(Reading_IntegralGyroRoll > TurnOver180Roll) |
291 | { |
291 | { |
292 | Reading_IntegralGyroRoll = -(TurnOver180Roll - 10000L); |
292 | Reading_IntegralGyroRoll = -(TurnOver180Roll - 10000L); |
293 | Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll; |
293 | Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll; |
294 | } |
294 | } |
295 | if(Reading_IntegralGyroRoll < -TurnOver180Roll) |
295 | if(Reading_IntegralGyroRoll < -TurnOver180Roll) |
296 | { |
296 | { |
297 | Reading_IntegralGyroRoll = (TurnOver180Roll - 10000L); |
297 | Reading_IntegralGyroRoll = (TurnOver180Roll - 10000L); |
298 | Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll; |
298 | Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll; |
299 | } |
299 | } |
300 | if(AdValueGyrRoll < 15) Reading_GyroRoll = -1000; |
300 | if(AdValueGyrRoll < 15) Reading_GyroRoll = -1000; |
301 | if(AdValueGyrRoll < 7) Reading_GyroRoll = -2000; |
301 | if(AdValueGyrRoll < 7) Reading_GyroRoll = -2000; |
302 | if(BoardRelease == 10) |
302 | if(BoardRelease == 10) |
303 | { |
303 | { |
304 | if(AdValueGyrRoll > 1010) Reading_GyroRoll = +1000; |
304 | if(AdValueGyrRoll > 1010) Reading_GyroRoll = +1000; |
305 | if(AdValueGyrRoll > 1017) Reading_GyroRoll = +2000; |
305 | if(AdValueGyrRoll > 1017) Reading_GyroRoll = +2000; |
306 | } |
306 | } |
307 | else |
307 | else |
308 | { |
308 | { |
309 | if(AdValueGyrRoll > 2020) Reading_GyroRoll = +1000; |
309 | if(AdValueGyrRoll > 2020) Reading_GyroRoll = +1000; |
310 | if(AdValueGyrRoll > 2034) Reading_GyroRoll = +2000; |
310 | if(AdValueGyrRoll > 2034) Reading_GyroRoll = +2000; |
311 | } |
311 | } |
312 | // Nick |
312 | // Nick |
313 | Reading_GyroNick -= tmpl2; |
313 | Reading_GyroNick -= tmpl2; |
314 | Reading_GyroNick -= (tmpl*FCParam.Yaw_NegFeedback) / 512L; |
314 | Reading_GyroNick -= (tmpl*FCParam.Yaw_NegFeedback) / 512L; |
315 | Reading_IntegralGyroNick2 += Reading_GyroNick; |
315 | Reading_IntegralGyroNick2 += Reading_GyroNick; |
316 | Reading_IntegralGyroNick += Reading_GyroNick - AttitudeCorrectionNick; |
316 | Reading_IntegralGyroNick += Reading_GyroNick - AttitudeCorrectionNick; |
317 | if(Reading_IntegralGyroNick > TurnOver180Nick) |
317 | if(Reading_IntegralGyroNick > TurnOver180Nick) |
318 | { |
318 | { |
319 | Reading_IntegralGyroNick = -(TurnOver180Nick - 25000L); |
319 | Reading_IntegralGyroNick = -(TurnOver180Nick - 25000L); |
320 | Reading_IntegralGyroNick2 = Reading_IntegralGyroNick; |
320 | Reading_IntegralGyroNick2 = Reading_IntegralGyroNick; |
321 | } |
321 | } |
322 | if(Reading_IntegralGyroNick < -TurnOver180Nick) |
322 | if(Reading_IntegralGyroNick < -TurnOver180Nick) |
323 | { |
323 | { |
324 | Reading_IntegralGyroNick = (TurnOver180Nick - 25000L); |
324 | Reading_IntegralGyroNick = (TurnOver180Nick - 25000L); |
325 | Reading_IntegralGyroNick2 = Reading_IntegralGyroNick; |
325 | Reading_IntegralGyroNick2 = Reading_IntegralGyroNick; |
326 | } |
326 | } |
327 | if(AdValueGyrNick < 15) Reading_GyroNick = -1000; |
327 | if(AdValueGyrNick < 15) Reading_GyroNick = -1000; |
328 | if(AdValueGyrNick < 7) Reading_GyroNick = -2000; |
328 | if(AdValueGyrNick < 7) Reading_GyroNick = -2000; |
329 | if(BoardRelease == 10) |
329 | if(BoardRelease == 10) |
330 | { |
330 | { |
331 | if(AdValueGyrNick > 1010) Reading_GyroNick = +1000; |
331 | if(AdValueGyrNick > 1010) Reading_GyroNick = +1000; |
332 | if(AdValueGyrNick > 1017) Reading_GyroNick = +2000; |
332 | if(AdValueGyrNick > 1017) Reading_GyroNick = +2000; |
333 | } |
333 | } |
334 | else |
334 | else |
335 | { |
335 | { |
336 | if(AdValueGyrNick > 2020) Reading_GyroNick = +1000; |
336 | if(AdValueGyrNick > 2020) Reading_GyroNick = +1000; |
337 | if(AdValueGyrNick > 2034) Reading_GyroNick = +2000; |
337 | if(AdValueGyrNick > 2034) Reading_GyroNick = +2000; |
338 | } |
338 | } |
339 | 339 | ||
340 | // start ADC again to capture measurement values for the next loop |
340 | // start ADC again to capture measurement values for the next loop |
341 | ADC_Enable(); |
341 | ADC_Enable(); |
342 | 342 | ||
343 | IntegralYaw = Reading_IntegralGyroYaw; |
343 | IntegralYaw = Reading_IntegralGyroYaw; |
344 | IntegralNick = Reading_IntegralGyroNick; |
344 | IntegralNick = Reading_IntegralGyroNick; |
345 | IntegralRoll = Reading_IntegralGyroRoll; |
345 | IntegralRoll = Reading_IntegralGyroRoll; |
346 | IntegralNick2 = Reading_IntegralGyroNick2; |
346 | IntegralNick2 = Reading_IntegralGyroNick2; |
347 | IntegralRoll2 = Reading_IntegralGyroRoll2; |
347 | IntegralRoll2 = Reading_IntegralGyroRoll2; |
348 | 348 | ||
349 | if((ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER) && !Looping_Nick && !Looping_Roll) |
349 | if((ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER) && !Looping_Nick && !Looping_Roll) |
350 | { |
350 | { |
351 | if(Reading_GyroNick > 200) Reading_GyroNick += 4 * (Reading_GyroNick - 200); |
351 | if(Reading_GyroNick > 200) Reading_GyroNick += 4 * (Reading_GyroNick - 200); |
352 | else if(Reading_GyroNick < -200) Reading_GyroNick += 4 * (Reading_GyroNick + 200); |
352 | else if(Reading_GyroNick < -200) Reading_GyroNick += 4 * (Reading_GyroNick + 200); |
353 | if(Reading_GyroRoll > 200) Reading_GyroRoll += 4 * (Reading_GyroRoll - 200); |
353 | if(Reading_GyroRoll > 200) Reading_GyroRoll += 4 * (Reading_GyroRoll - 200); |
354 | else if(Reading_GyroRoll < -200) Reading_GyroRoll += 4 * (Reading_GyroRoll + 200); |
354 | else if(Reading_GyroRoll < -200) Reading_GyroRoll += 4 * (Reading_GyroRoll + 200); |
355 | } |
355 | } |
356 | } |
356 | } |
357 | 357 | ||
358 | /************************************************************************/ |
358 | /************************************************************************/ |
359 | /* Averaging Measurement Readings for Calibration */ |
359 | /* Averaging Measurement Readings for Calibration */ |
360 | /************************************************************************/ |
360 | /************************************************************************/ |
361 | void CalibMean(void) |
361 | void CalibMean(void) |
362 | { |
362 | { |
363 | if(BoardRelease >= 13) SearchGyroOffset(); |
363 | if(BoardRelease >= 13) SearchGyroOffset(); |
364 | // stop ADC to avoid changing values during calculation |
364 | // stop ADC to avoid changing values during calculation |
365 | ADC_Disable(); |
365 | ADC_Disable(); |
366 | 366 | ||
367 | Reading_GyroNick = AdValueGyrNick; |
367 | Reading_GyroNick = AdValueGyrNick; |
368 | Reading_GyroRoll = AdValueGyrRoll; |
368 | Reading_GyroRoll = AdValueGyrRoll; |
369 | Reading_GyroYaw = AdValueGyrYaw; |
369 | Reading_GyroYaw = AdValueGyrYaw; |
370 | 370 | ||
371 | Mean_AccNick = ACC_AMPLIFY * (int32_t)AdValueAccNick; |
371 | Mean_AccNick = ACC_AMPLIFY * (int32_t)AdValueAccNick; |
372 | Mean_AccRoll = ACC_AMPLIFY * (int32_t)AdValueAccRoll; |
372 | Mean_AccRoll = ACC_AMPLIFY * (int32_t)AdValueAccRoll; |
373 | Mean_AccTop = (int32_t)AdValueAccTop; |
373 | Mean_AccTop = (int32_t)AdValueAccTop; |
374 | // start ADC (enables internal trigger so that the ISR in analog.c |
374 | // start ADC (enables internal trigger so that the ISR in analog.c |
375 | // updates the readings once) |
375 | // updates the readings once) |
376 | ADC_Enable(); |
376 | ADC_Enable(); |
377 | 377 | ||
378 | TurnOver180Nick = (int32_t) ParamSet.AngleTurnOverNick * 2500L; |
378 | TurnOver180Nick = (int32_t) ParamSet.AngleTurnOverNick * 2500L; |
379 | TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L; |
379 | TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L; |
380 | } |
380 | } |
381 | 381 | ||
382 | /************************************************************************/ |
382 | /************************************************************************/ |
383 | /* Transmit Motor Data via I2C */ |
383 | /* Transmit Motor Data via I2C */ |
384 | /************************************************************************/ |
384 | /************************************************************************/ |
385 | void SendMotorData(void) |
385 | void SendMotorData(void) |
386 | { |
386 | { |
387 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) |
387 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) |
388 | { |
388 | { |
389 | #ifdef HEXAKOPTER |
389 | #ifdef HEXAKOPTER |
390 | Motor_RearLeft = 0; |
390 | Motor_RearLeft = 0; |
391 | Motor_FrontLeft = 0; |
391 | Motor_FrontLeft = 0; |
392 | Motor_RearRight = 0; |
392 | Motor_RearRight = 0; |
393 | Motor_FrontRight = 0; |
393 | Motor_FrontRight = 0; |
394 | Motor_Right = 0; |
394 | Motor_Right = 0; |
395 | Motor_Left = 0; |
395 | Motor_Left = 0; |
396 | if(MotorTest[0]) Motor_FrontLeft = Motor_FrontRight = MotorTest[0]; |
396 | if(MotorTest[0]) Motor_FrontLeft = Motor_FrontRight = MotorTest[0]; |
397 | if(MotorTest[1]) Motor_RearLeft = Motor_RearRight = MotorTest[1]; |
397 | if(MotorTest[1]) Motor_RearLeft = Motor_RearRight = MotorTest[1]; |
398 | if(MotorTest[2]) Motor_Left = MotorTest[2]; |
398 | if(MotorTest[2]) Motor_Left = MotorTest[2]; |
399 | if(MotorTest[3]) Motor_Right = MotorTest[3]; |
399 | if(MotorTest[3]) Motor_Right = MotorTest[3]; |
400 | #else |
400 | #else |
401 | Motor_Rear = 0; |
401 | Motor_Rear = 0; |
402 | Motor_Front = 0; |
402 | Motor_Front = 0; |
403 | Motor_Right = 0; |
403 | Motor_Right = 0; |
404 | Motor_Left = 0; |
404 | Motor_Left = 0; |
405 | if(MotorTest[0]) Motor_Front = MotorTest[0]; |
405 | if(MotorTest[0]) Motor_Front = MotorTest[0]; |
406 | if(MotorTest[1]) Motor_Rear = MotorTest[1]; |
406 | if(MotorTest[1]) Motor_Rear = MotorTest[1]; |
407 | if(MotorTest[2]) Motor_Left = MotorTest[2]; |
407 | if(MotorTest[2]) Motor_Left = MotorTest[2]; |
408 | if(MotorTest[3]) Motor_Right = MotorTest[3]; |
408 | if(MotorTest[3]) Motor_Right = MotorTest[3]; |
409 | #endif |
409 | #endif |
410 | MKFlags &= ~(MKFLAG_FLY|MKFLAG_START); // clear flag FLY and START if motors are off |
410 | MKFlags &= ~(MKFLAG_FLY|MKFLAG_START); // clear flag FLY and START if motors are off |
411 | } |
411 | } |
412 | 412 | ||
413 | //Start I2C Interrupt Mode |
413 | //Start I2C Interrupt Mode |
414 | twi_state = TWI_STATE_MOTOR_TX; |
414 | twi_state = TWI_STATE_MOTOR_TX; |
415 | I2C_Start(); |
415 | I2C_Start(); |
416 | } |
416 | } |
417 | 417 | ||
418 | 418 | ||
419 | 419 | ||
420 | /************************************************************************/ |
420 | /************************************************************************/ |
421 | /* Maps the parameter to poti values */ |
421 | /* Maps the parameter to poti values */ |
422 | /************************************************************************/ |
422 | /************************************************************************/ |
423 | void ParameterMapping(void) |
423 | void ParameterMapping(void) |
424 | { |
424 | { |
425 | if(RC_Quality > 160) // do the mapping of RC-Potis only if the rc-signal is ok |
425 | if(RC_Quality > 160) // do the mapping of RC-Potis only if the rc-signal is ok |
426 | // else the last updated values are used |
426 | // else the last updated values are used |
427 | { |
427 | { |
428 | //update poti values by rc-signals |
428 | //update poti values by rc-signals |
429 | #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;} |
429 | #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;} |
430 | #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;} |
430 | #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;} |
431 | CHK_POTI(FCParam.MaxHeight,ParamSet.MaxHeight); |
431 | CHK_POTI(FCParam.MaxHeight,ParamSet.MaxHeight); |
432 | CHK_POTI_MM(FCParam.Height_D,ParamSet.Height_D,0,100); |
432 | CHK_POTI_MM(FCParam.Height_D,ParamSet.Height_D,0,100); |
433 | CHK_POTI_MM(FCParam.Height_P,ParamSet.Height_P,0,100); |
433 | CHK_POTI_MM(FCParam.Height_P,ParamSet.Height_P,0,100); |
434 | CHK_POTI(FCParam.Height_ACC_Effect,ParamSet.Height_ACC_Effect); |
434 | CHK_POTI(FCParam.Height_ACC_Effect,ParamSet.Height_ACC_Effect); |
435 | CHK_POTI(FCParam.CompassYawEffect,ParamSet.CompassYawEffect); |
435 | CHK_POTI(FCParam.CompassYawEffect,ParamSet.CompassYawEffect); |
436 | CHK_POTI_MM(FCParam.Gyro_P,ParamSet.Gyro_P,10,255); |
436 | CHK_POTI_MM(FCParam.Gyro_P,ParamSet.Gyro_P,10,255); |
437 | CHK_POTI(FCParam.Gyro_I,ParamSet.Gyro_I); |
437 | CHK_POTI(FCParam.Gyro_I,ParamSet.Gyro_I); |
438 | CHK_POTI(FCParam.I_Factor,ParamSet.I_Factor); |
438 | CHK_POTI(FCParam.I_Factor,ParamSet.I_Factor); |
439 | CHK_POTI(FCParam.UserParam1,ParamSet.UserParam1); |
439 | CHK_POTI(FCParam.UserParam1,ParamSet.UserParam1); |
440 | CHK_POTI(FCParam.UserParam2,ParamSet.UserParam2); |
440 | CHK_POTI(FCParam.UserParam2,ParamSet.UserParam2); |
441 | CHK_POTI(FCParam.UserParam3,ParamSet.UserParam3); |
441 | CHK_POTI(FCParam.UserParam3,ParamSet.UserParam3); |
442 | CHK_POTI(FCParam.UserParam4,ParamSet.UserParam4); |
442 | CHK_POTI(FCParam.UserParam4,ParamSet.UserParam4); |
443 | CHK_POTI(FCParam.UserParam5,ParamSet.UserParam5); |
443 | CHK_POTI(FCParam.UserParam5,ParamSet.UserParam5); |
444 | CHK_POTI(FCParam.UserParam6,ParamSet.UserParam6); |
444 | CHK_POTI(FCParam.UserParam6,ParamSet.UserParam6); |
445 | CHK_POTI(FCParam.UserParam7,ParamSet.UserParam7); |
445 | CHK_POTI(FCParam.UserParam7,ParamSet.UserParam7); |
446 | CHK_POTI(FCParam.UserParam8,ParamSet.UserParam8); |
446 | CHK_POTI(FCParam.UserParam8,ParamSet.UserParam8); |
447 | CHK_POTI(FCParam.ServoNickControl,ParamSet.ServoNickControl); |
447 | CHK_POTI(FCParam.ServoNickControl,ParamSet.ServoNickControl); |
448 | CHK_POTI(FCParam.LoopGasLimit,ParamSet.LoopGasLimit); |
448 | CHK_POTI(FCParam.LoopGasLimit,ParamSet.LoopGasLimit); |
449 | CHK_POTI(FCParam.Yaw_PosFeedback,ParamSet.Yaw_PosFeedback); |
449 | CHK_POTI(FCParam.Yaw_PosFeedback,ParamSet.Yaw_PosFeedback); |
450 | CHK_POTI(FCParam.Yaw_NegFeedback,ParamSet.Yaw_NegFeedback); |
450 | CHK_POTI(FCParam.Yaw_NegFeedback,ParamSet.Yaw_NegFeedback); |
451 | CHK_POTI(FCParam.DynamicStability,ParamSet.DynamicStability); |
451 | CHK_POTI(FCParam.DynamicStability,ParamSet.DynamicStability); |
452 | CHK_POTI_MM(FCParam.J16Timing,ParamSet.J16Timing,1,255); |
452 | CHK_POTI_MM(FCParam.J16Timing,ParamSet.J16Timing,1,255); |
453 | CHK_POTI_MM(FCParam.J17Timing,ParamSet.J17Timing,1,255); |
453 | CHK_POTI_MM(FCParam.J17Timing,ParamSet.J17Timing,1,255); |
454 | CHK_POTI(FCParam.NaviGpsModeControl,ParamSet.NaviGpsModeControl); |
454 | CHK_POTI(FCParam.NaviGpsModeControl,ParamSet.NaviGpsModeControl); |
455 | CHK_POTI(FCParam.NaviGpsGain,ParamSet.NaviGpsGain); |
455 | CHK_POTI(FCParam.NaviGpsGain,ParamSet.NaviGpsGain); |
456 | CHK_POTI(FCParam.NaviGpsP,ParamSet.NaviGpsP); |
456 | CHK_POTI(FCParam.NaviGpsP,ParamSet.NaviGpsP); |
457 | CHK_POTI(FCParam.NaviGpsI,ParamSet.NaviGpsI); |
457 | CHK_POTI(FCParam.NaviGpsI,ParamSet.NaviGpsI); |
458 | CHK_POTI(FCParam.NaviGpsD,ParamSet.NaviGpsD); |
458 | CHK_POTI(FCParam.NaviGpsD,ParamSet.NaviGpsD); |
459 | CHK_POTI(FCParam.NaviGpsACC,ParamSet.NaviGpsACC); |
459 | CHK_POTI(FCParam.NaviGpsACC,ParamSet.NaviGpsACC); |
460 | CHK_POTI(FCParam.ExternalControl,ParamSet.ExternalControl); |
460 | CHK_POTI(FCParam.ExternalControl,ParamSet.ExternalControl); |
461 | Ki = (float) FCParam.I_Factor * FACTOR_I; |
461 | Ki = (float) FCParam.I_Factor * FACTOR_I; |
462 | } |
462 | } |
463 | } |
463 | } |
464 | 464 | ||
465 | 465 | ||
466 | void SetCompassCalState(void) |
466 | void SetCompassCalState(void) |
467 | { |
467 | { |
468 | static uint8_t stick = 1; |
468 | static uint8_t stick = 1; |
469 | 469 | ||
470 | // if nick is centered or top set stick to zero |
470 | // if nick is centered or top set stick to zero |
471 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -20) stick = 0; |
471 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -20) stick = 0; |
472 | // if nick is down trigger to next cal state |
472 | // if nick is down trigger to next cal state |
473 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70) && !stick) |
473 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70) && !stick) |
474 | { |
474 | { |
475 | stick = 1; |
475 | stick = 1; |
476 | CompassCalState++; |
476 | CompassCalState++; |
477 | if(CompassCalState < 5) Beep(CompassCalState); |
477 | if(CompassCalState < 5) Beep(CompassCalState); |
478 | else BeepTime = 1000; |
478 | else BeepTime = 1000; |
479 | } |
479 | } |
480 | } |
480 | } |
481 | 481 | ||
482 | 482 | ||
483 | 483 | ||
484 | /************************************************************************/ |
484 | /************************************************************************/ |
485 | /* MotorControl */ |
485 | /* MotorControl */ |
486 | /************************************************************************/ |
486 | /************************************************************************/ |
487 | void MotorControl(void) |
487 | void MotorControl(void) |
488 | { |
488 | { |
489 | int16_t MotorValue, pd_result, h, tmp_int; |
489 | int16_t MotorValue, pd_result, h, tmp_int; |
490 | int16_t YawMixFraction, GasMixFraction, NickMixFraction, RollMixFraction; |
490 | int16_t YawMixFraction, GasMixFraction, NickMixFraction, RollMixFraction; |
491 | static int32_t SumNick = 0, SumRoll = 0; |
491 | static int32_t SumNick = 0, SumRoll = 0; |
492 | static int32_t SetPointYaw = 0; |
492 | static int32_t SetPointYaw = 0; |
493 | static int32_t IntegralErrorNick = 0; |
493 | static int32_t IntegralErrorNick = 0; |
494 | static int32_t IntegralErrorRoll = 0; |
494 | static int32_t IntegralErrorRoll = 0; |
495 | static uint16_t RcLostTimer; |
495 | static uint16_t RcLostTimer; |
496 | static uint8_t delay_neutral = 0, delay_startmotors = 0, delay_stopmotors = 0; |
496 | static uint8_t delay_neutral = 0, delay_startmotors = 0, delay_stopmotors = 0; |
497 | static uint8_t HeightControlActive = 0; |
497 | static uint8_t HeightControlActive = 0; |
498 | static int16_t HeightControlGas = 0; |
498 | static int16_t HeightControlGas = 0; |
499 | static int8_t TimerDebugOut = 0; |
499 | static int8_t TimerDebugOut = 0; |
500 | static uint16_t UpdateCompassCourse = 0; |
500 | static uint16_t UpdateCompassCourse = 0; |
501 | static int32_t CorrectionNick, CorrectionRoll; |
501 | static int32_t CorrectionNick, CorrectionRoll; |
502 | 502 | ||
503 | Mean(); |
503 | Mean(); |
504 | GRN_ON; |
504 | GRN_ON; |
505 | 505 | ||
506 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
506 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
507 | // determine gas value |
507 | // determine gas value |
508 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
508 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
509 | GasMixFraction = StickGas; |
509 | GasMixFraction = StickGas; |
510 | if(GasMixFraction < ParamSet.Gas_Min + 10) GasMixFraction = ParamSet.Gas_Min + 10; |
510 | if(GasMixFraction < ParamSet.Gas_Min + 10) GasMixFraction = ParamSet.Gas_Min + 10; |
511 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
511 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
512 | // RC-signal is bad |
512 | // RC-signal is bad |
513 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
513 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
514 | if(RC_Quality < 120) // the rc-frame signal is not reveived or noisy |
514 | if(RC_Quality < 120) // the rc-frame signal is not reveived or noisy |
515 | { |
515 | { |
516 | if(!PcAccess) // if also no PC-Access via UART |
516 | if(!PcAccess) // if also no PC-Access via UART |
517 | { |
517 | { |
518 | if(BeepModulation == 0xFFFF) |
518 | if(BeepModulation == 0xFFFF) |
519 | { |
519 | { |
520 | BeepTime = 15000; // 1.5 seconds |
520 | BeepTime = 15000; // 1.5 seconds |
521 | BeepModulation = 0x0C00; |
521 | BeepModulation = 0x0C00; |
522 | } |
522 | } |
523 | } |
523 | } |
524 | if(RcLostTimer) RcLostTimer--; // decremtent timer after rc sigal lost |
524 | if(RcLostTimer) RcLostTimer--; // decremtent timer after rc sigal lost |
525 | else // rc lost countdown finished |
525 | else // rc lost countdown finished |
526 | { |
526 | { |
527 | MKFlags &= ~(MKFLAG_MOTOR_RUN|MKFLAG_EMERGENCY_LANDING); // clear motor run flag that stop the motors in SendMotorData() |
527 | MKFlags &= ~(MKFLAG_MOTOR_RUN|MKFLAG_EMERGENCY_LANDING); // clear motor run flag that stop the motors in SendMotorData() |
528 | } |
528 | } |
529 | RED_ON; // set red led |
529 | RED_ON; // set red led |
530 | if(Model_Is_Flying > 1000) // wahrscheinlich in der Luft --> langsam absenken |
530 | if(Model_Is_Flying > 1000) // wahrscheinlich in der Luft --> langsam absenken |
531 | { |
531 | { |
532 | GasMixFraction = ParamSet.EmergencyGas; // set emergency gas |
532 | GasMixFraction = ParamSet.EmergencyGas; // set emergency gas |
533 | MKFlags |= (MKFLAG_EMERGENCY_LANDING); // ser flag fpr emergency landing |
533 | MKFlags |= (MKFLAG_EMERGENCY_LANDING); // ser flag fpr emergency landing |
534 | // set neutral rc inputs |
534 | // set neutral rc inputs |
535 | PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] = 0; |
535 | PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] = 0; |
536 | PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
536 | PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
537 | PPM_diff[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
537 | PPM_diff[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
538 | PPM_in[ParamSet.ChannelAssignment[CH_NICK]] = 0; |
538 | PPM_in[ParamSet.ChannelAssignment[CH_NICK]] = 0; |
539 | PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
539 | PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
540 | PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
540 | PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
541 | } |
541 | } |
542 | else MKFlags &= ~(MKFLAG_MOTOR_RUN); // clear motor run flag that stop the motors in SendMotorData() |
542 | else MKFlags &= ~(MKFLAG_MOTOR_RUN); // clear motor run flag that stop the motors in SendMotorData() |
543 | } // eof RC_Quality < 120 |
543 | } // eof RC_Quality < 120 |
544 | else |
544 | else |
545 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
545 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
546 | // RC-signal is good |
546 | // RC-signal is good |
547 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
547 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
548 | if(RC_Quality > 140) |
548 | if(RC_Quality > 140) |
549 | { |
549 | { |
550 | MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing |
550 | MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing |
551 | // reset emergency timer |
551 | // reset emergency timer |
552 | RcLostTimer = ParamSet.EmergencyGasDuration * 50; |
552 | RcLostTimer = ParamSet.EmergencyGasDuration * 50; |
553 | if(GasMixFraction > 40 && (MKFlags & MKFLAG_MOTOR_RUN) ) |
553 | if(GasMixFraction > 40 && (MKFlags & MKFLAG_MOTOR_RUN) ) |
554 | { |
554 | { |
555 | if(Model_Is_Flying < 0xFFFF) Model_Is_Flying++; |
555 | if(Model_Is_Flying < 0xFFFF) Model_Is_Flying++; |
556 | } |
556 | } |
557 | if(Model_Is_Flying < 256) |
557 | if(Model_Is_Flying < 256) |
558 | { |
558 | { |
559 | SumNick = 0; |
559 | SumNick = 0; |
560 | SumRoll = 0; |
560 | SumRoll = 0; |
561 | StickYaw = 0; |
561 | StickYaw = 0; |
562 | if(Model_Is_Flying == 250) |
562 | if(Model_Is_Flying == 250) |
563 | { |
563 | { |
564 | UpdateCompassCourse = 1; |
564 | UpdateCompassCourse = 1; |
565 | Reading_IntegralGyroYaw = 0; |
565 | Reading_IntegralGyroYaw = 0; |
566 | SetPointYaw = 0; |
566 | SetPointYaw = 0; |
567 | } |
567 | } |
568 | } |
568 | } |
569 | else MKFlags |= (MKFLAG_FLY); // set fly flag |
569 | else MKFlags |= (MKFLAG_FLY); // set fly flag |
570 | 570 | ||
571 | if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--; |
571 | if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--; |
572 | if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--; |
572 | if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--; |
573 | if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--; |
573 | if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--; |
574 | if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--; |
574 | if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--; |
575 | //PPM24-Extension |
575 | //PPM24-Extension |
576 | if(Poti5 < PPM_in[9] + 110) Poti5++; else if(Poti5 > PPM_in[9] + 110 && Poti5) Poti5--; |
576 | if(Poti5 < PPM_in[9] + 110) Poti5++; else if(Poti5 > PPM_in[9] + 110 && Poti5) Poti5--; |
577 | if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--; |
577 | if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--; |
578 | if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--; |
578 | if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--; |
579 | if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--; |
579 | if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--; |
580 | //limit poti values |
580 | //limit poti values |
581 | if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255; |
581 | if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255; |
582 | if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255; |
582 | if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255; |
583 | if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255; |
583 | if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255; |
584 | if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255; |
584 | if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255; |
585 | //PPM24-Extension |
585 | //PPM24-Extension |
586 | if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255; |
586 | if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255; |
587 | if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255; |
587 | if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255; |
588 | if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255; |
588 | if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255; |
589 | if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255; |
589 | if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255; |
590 | 590 | ||
591 | 591 | ||
592 | // if motors are off and the gas stick is in the upper position |
592 | // if motors are off and the gas stick is in the upper position |
593 | if((PPM_in[ParamSet.ChannelAssignment[CH_GAS]] > 80) && !(MKFlags & MKFLAG_MOTOR_RUN) ) |
593 | if((PPM_in[ParamSet.ChannelAssignment[CH_GAS]] > 80) && !(MKFlags & MKFLAG_MOTOR_RUN) ) |
594 | { |
594 | { |
595 | // and if the yaw stick is in the leftmost position |
595 | // and if the yaw stick is in the leftmost position |
596 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75) |
596 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75) |
597 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
597 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
598 | // calibrate the neutral readings of all attitude sensors |
598 | // calibrate the neutral readings of all attitude sensors |
599 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
599 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
600 | { |
600 | { |
601 | // gas/yaw joystick is top left |
601 | // gas/yaw joystick is top left |
602 | // _________ |
602 | // _________ |
603 | // |x | |
603 | // |x | |
604 | // | | |
604 | // | | |
605 | // | | |
605 | // | | |
606 | // | | |
606 | // | | |
607 | // | | |
607 | // | | |
608 | // ¯¯¯¯¯¯¯¯¯ |
608 | // ¯¯¯¯¯¯¯¯¯ |
609 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
609 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
610 | { |
610 | { |
611 | delay_neutral = 0; |
611 | delay_neutral = 0; |
612 | GRN_OFF; |
612 | GRN_OFF; |
613 | Model_Is_Flying = 0; |
613 | Model_Is_Flying = 0; |
614 | // check roll/nick stick position |
614 | // check roll/nick stick position |
615 | // if nick stick is top or roll stick is left or right --> change parameter setting |
615 | // if nick stick is top or roll stick is left or right --> change parameter setting |
616 | // according to roll/nick stick position |
616 | // according to roll/nick stick position |
617 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70) |
617 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70) |
618 | { |
618 | { |
619 | uint8_t setting = 1; // default |
619 | uint8_t setting = 1; // default |
620 | // nick/roll joystick |
620 | // nick/roll joystick |
621 | // _________ |
621 | // _________ |
622 | // |2 3 4| |
622 | // |2 3 4| |
623 | // | | |
623 | // | | |
624 | // |1 5| |
624 | // |1 5| |
625 | // | | |
625 | // | | |
626 | // | | |
626 | // | | |
627 | // ¯¯¯¯¯¯¯¯¯ |
627 | // ¯¯¯¯¯¯¯¯¯ |
628 | // roll stick leftmost and nick stick centered --> setting 1 |
628 | // roll stick leftmost and nick stick centered --> setting 1 |
629 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 1; |
629 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 1; |
630 | // roll stick leftmost and nick stick topmost --> setting 2 |
630 | // roll stick leftmost and nick stick topmost --> setting 2 |
631 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 2; |
631 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 2; |
632 | // roll stick centered an nick stick topmost --> setting 3 |
632 | // roll stick centered an nick stick topmost --> setting 3 |
633 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 3; |
633 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 3; |
634 | // roll stick rightmost and nick stick topmost --> setting 4 |
634 | // roll stick rightmost and nick stick topmost --> setting 4 |
635 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 4; |
635 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 4; |
636 | // roll stick rightmost and nick stick centered --> setting 5 |
636 | // roll stick rightmost and nick stick centered --> setting 5 |
637 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 5; |
637 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 5; |
638 | // update active parameter set in eeprom |
638 | // update active parameter set in eeprom |
639 | SetActiveParamSet(setting); |
639 | SetActiveParamSet(setting); |
640 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
640 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
641 | SetNeutral(); |
641 | SetNeutral(); |
642 | Beep(GetActiveParamSet()); |
642 | Beep(GetActiveParamSet()); |
643 | } |
643 | } |
644 | else |
644 | else |
645 | { |
645 | { |
646 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
646 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
647 | { |
647 | { |
648 | // if roll stick is centered and nick stick is down |
648 | // if roll stick is centered and nick stick is down |
649 | if (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < 20 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70) |
649 | if (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < 20 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70) |
650 | { |
650 | { |
651 | // nick/roll joystick |
651 | // nick/roll joystick |
652 | // _________ |
652 | // _________ |
653 | // | | |
653 | // | | |
654 | // | | |
654 | // | | |
655 | // | | |
655 | // | | |
656 | // | | |
656 | // | | |
657 | // | x | |
657 | // | x | |
658 | // ¯¯¯¯¯¯¯¯¯ |
658 | // ¯¯¯¯¯¯¯¯¯ |
659 | // enable calibration state of compass |
659 | // enable calibration state of compass |
660 | CompassCalState = 1; |
660 | CompassCalState = 1; |
661 | BeepTime = 1000; |
661 | BeepTime = 1000; |
662 | } |
662 | } |
663 | else // nick and roll are centered |
663 | else // nick and roll are centered |
664 | { |
664 | { |
665 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
665 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
666 | SetNeutral(); |
666 | SetNeutral(); |
667 | Beep(GetActiveParamSet()); |
667 | Beep(GetActiveParamSet()); |
668 | } |
668 | } |
669 | } |
669 | } |
670 | else // nick and roll are centered |
670 | else // nick and roll are centered |
671 | { |
671 | { |
672 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
672 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
673 | SetNeutral(); |
673 | SetNeutral(); |
674 | Beep(GetActiveParamSet()); |
674 | Beep(GetActiveParamSet()); |
675 | } |
675 | } |
676 | } |
676 | } |
677 | } |
677 | } |
678 | } |
678 | } |
679 | // and if the yaw stick is in the rightmost position |
679 | // and if the yaw stick is in the rightmost position |
680 | // save the ACC neutral setting to eeprom |
680 | // save the ACC neutral setting to eeprom |
681 | else if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75) |
681 | else if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75) |
682 | { |
682 | { |
683 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
683 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
684 | { |
684 | { |
685 | delay_neutral = 0; |
685 | delay_neutral = 0; |
686 | GRN_OFF; |
686 | GRN_OFF; |
687 | SetParamWord(PID_ACC_NICK, 0xFFFF); // make value invalid |
687 | SetParamWord(PID_ACC_NICK, 0xFFFF); // make value invalid |
688 | Model_Is_Flying = 0; |
688 | Model_Is_Flying = 0; |
689 | SetNeutral(); |
689 | SetNeutral(); |
690 | // Save ACC neutral settings to eeprom |
690 | // Save ACC neutral settings to eeprom |
691 | SetParamWord(PID_ACC_NICK, (uint16_t)NeutralAccX); |
691 | SetParamWord(PID_ACC_NICK, (uint16_t)NeutralAccX); |
692 | SetParamWord(PID_ACC_ROLL, (uint16_t)NeutralAccY); |
692 | SetParamWord(PID_ACC_ROLL, (uint16_t)NeutralAccY); |
693 | SetParamWord(PID_ACC_Z, (uint16_t)NeutralAccZ); |
693 | SetParamWord(PID_ACC_Z, (uint16_t)NeutralAccZ); |
694 | Beep(GetActiveParamSet()); |
694 | Beep(GetActiveParamSet()); |
695 | } |
695 | } |
696 | } |
696 | } |
697 | else delay_neutral = 0; |
697 | else delay_neutral = 0; |
698 | } |
698 | } |
699 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
699 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
700 | // gas stick is down |
700 | // gas stick is down |
701 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
701 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
702 | if(PPM_in[ParamSet.ChannelAssignment[CH_GAS]] < -85) |
702 | if(PPM_in[ParamSet.ChannelAssignment[CH_GAS]] < -85) |
703 | { |
703 | { |
704 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
704 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
705 | // and yaw stick is rightmost --> start motors |
705 | // and yaw stick is rightmost --> start motors |
706 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
706 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
707 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75) |
707 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75) |
708 | { |
708 | { |
709 | if(++delay_startmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
709 | if(++delay_startmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
710 | { |
710 | { |
711 | delay_startmotors = 0; // do not repeat if once executed |
711 | delay_startmotors = 0; // do not repeat if once executed |
712 | Model_Is_Flying = 1; |
712 | Model_Is_Flying = 1; |
713 | MKFlags |= (MKFLAG_MOTOR_RUN|MKFLAG_START); // set flag RUN and START |
713 | MKFlags |= (MKFLAG_MOTOR_RUN|MKFLAG_START); // set flag RUN and START |
714 | SetPointYaw = 0; |
714 | SetPointYaw = 0; |
715 | Reading_IntegralGyroYaw = 0; |
715 | Reading_IntegralGyroYaw = 0; |
716 | Reading_IntegralGyroNick = 0; |
716 | Reading_IntegralGyroNick = 0; |
717 | Reading_IntegralGyroRoll = 0; |
717 | Reading_IntegralGyroRoll = 0; |
718 | Reading_IntegralGyroNick2 = IntegralNick; |
718 | Reading_IntegralGyroNick2 = IntegralNick; |
719 | Reading_IntegralGyroRoll2 = IntegralRoll; |
719 | Reading_IntegralGyroRoll2 = IntegralRoll; |
720 | SumNick = 0; |
720 | SumNick = 0; |
721 | SumRoll = 0; |
721 | SumRoll = 0; |
722 | } |
722 | } |
723 | } |
723 | } |
724 | else delay_startmotors = 0; // reset delay timer if sticks are not in this position |
724 | else delay_startmotors = 0; // reset delay timer if sticks are not in this position |
725 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
725 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
726 | // and yaw stick is leftmost --> stop motors |
726 | // and yaw stick is leftmost --> stop motors |
727 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
727 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
728 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75) |
728 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75) |
729 | { |
729 | { |
730 | if(++delay_stopmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
730 | if(++delay_stopmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
731 | { |
731 | { |
732 | delay_stopmotors = 0; // do not repeat if once executed |
732 | delay_stopmotors = 0; // do not repeat if once executed |
733 | Model_Is_Flying = 0; |
733 | Model_Is_Flying = 0; |
734 | MKFlags &= ~(MKFLAG_MOTOR_RUN); |
734 | MKFlags &= ~(MKFLAG_MOTOR_RUN); |
735 | } |
735 | } |
736 | } |
736 | } |
737 | else delay_stopmotors = 0; // reset delay timer if sticks are not in this position |
737 | else delay_stopmotors = 0; // reset delay timer if sticks are not in this position |
738 | } |
738 | } |
739 | // remapping of paameters only if the signal rc-sigbnal conditions are good |
739 | // remapping of paameters only if the signal rc-sigbnal conditions are good |
740 | } // eof RC_Quality > 150 |
740 | } // eof RC_Quality > 150 |
741 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
741 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
742 | // new values from RC |
742 | // new values from RC |
743 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
743 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
744 | if(!NewPpmData-- || (MKFlags & MKFLAG_EMERGENCY_LANDING) ) // NewData = 0 means new data from RC |
744 | if(!NewPpmData-- || (MKFlags & MKFLAG_EMERGENCY_LANDING) ) // NewData = 0 means new data from RC |
745 | { |
745 | { |
746 | int tmp_int; |
746 | int tmp_int; |
747 | ParameterMapping(); // remapping params (online poti replacement) |
747 | ParameterMapping(); // remapping params (online poti replacement) |
748 | // calculate Stick inputs by rc channels (P) and changing of rc channels (D) |
748 | // calculate Stick inputs by rc channels (P) and changing of rc channels (D) |
749 | StickNick = (StickNick * 3 + PPM_in[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.Stick_P) / 4; |
749 | StickNick = (StickNick * 3 + PPM_in[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.Stick_P) / 4; |
750 | StickNick += PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.Stick_D; |
750 | StickNick += PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.Stick_D; |
751 | StickNick -= (GPS_Nick); |
751 | StickNick -= (GPS_Nick); |
752 | 752 | ||
753 | StickRoll = (StickRoll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_P) / 4; |
753 | StickRoll = (StickRoll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_P) / 4; |
754 | StickRoll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_D; |
754 | StickRoll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_D; |
755 | StickRoll -= (GPS_Roll); |
755 | StickRoll -= (GPS_Roll); |
756 | 756 | ||
757 | // direct mapping of yaw and gas |
757 | // direct mapping of yaw and gas |
758 | StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]]; |
758 | StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]]; |
759 | StickGas = PPM_in[ParamSet.ChannelAssignment[CH_GAS]] + 120;// shift to positive numbers |
759 | StickGas = PPM_in[ParamSet.ChannelAssignment[CH_GAS]] + 120;// shift to positive numbers |
760 | 760 | ||
761 | // update gyro control loop factors |
761 | // update gyro control loop factors |
762 | Gyro_P_Factor = ((float) FCParam.Gyro_P + 10.0) / (256.0 / STICK_GAIN); |
762 | Gyro_P_Factor = ((float) FCParam.Gyro_P + 10.0) / (256.0 / STICK_GAIN); |
763 | Gyro_I_Factor = ((float) FCParam.Gyro_I) / (44000 / STICK_GAIN); |
763 | Gyro_I_Factor = ((float) FCParam.Gyro_I) / (44000 / STICK_GAIN); |
764 | 764 | ||
765 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
765 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
766 | // Digital Control via DubWise |
766 | // Digital Control via DubWise |
767 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
767 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
768 | 768 | ||
769 | #define KEY_VALUE (FCParam.ExternalControl * 4) // step width |
769 | #define KEY_VALUE (FCParam.ExternalControl * 4) // step width |
770 | if(DubWiseKeys[1]) BeepTime = 10; |
770 | if(DubWiseKeys[1]) BeepTime = 10; |
771 | if(DubWiseKeys[1] & DUB_KEY_UP) tmp_int = KEY_VALUE; |
771 | if(DubWiseKeys[1] & DUB_KEY_UP) tmp_int = KEY_VALUE; |
772 | else if(DubWiseKeys[1] & DUB_KEY_DOWN) tmp_int = -KEY_VALUE; |
772 | else if(DubWiseKeys[1] & DUB_KEY_DOWN) tmp_int = -KEY_VALUE; |
773 | else tmp_int = 0; |
773 | else tmp_int = 0; |
774 | ExternStickNick = (ExternStickNick * 7 + tmp_int) / 8; |
774 | ExternStickNick = (ExternStickNick * 7 + tmp_int) / 8; |
775 | if(DubWiseKeys[1] & DUB_KEY_LEFT) tmp_int = KEY_VALUE; |
775 | if(DubWiseKeys[1] & DUB_KEY_LEFT) tmp_int = KEY_VALUE; |
776 | else if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; |
776 | else if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; |
777 | else tmp_int = 0; |
777 | else tmp_int = 0; |
778 | ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8; |
778 | ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8; |
779 | 779 | ||
780 | if(DubWiseKeys[0] & 8) ExternStickYaw = 50;else |
780 | if(DubWiseKeys[0] & 8) ExternStickYaw = 50;else |
781 | if(DubWiseKeys[0] & 4) ExternStickYaw =-50;else ExternStickYaw = 0; |
781 | if(DubWiseKeys[0] & 4) ExternStickYaw =-50;else ExternStickYaw = 0; |
782 | if(DubWiseKeys[0] & 2) ExternHeightValue++; |
782 | if(DubWiseKeys[0] & 2) ExternHeightValue++; |
783 | if(DubWiseKeys[0] & 16) ExternHeightValue--; |
783 | if(DubWiseKeys[0] & 16) ExternHeightValue--; |
784 | 784 | ||
785 | StickNick += (STICK_GAIN * ExternStickNick) / 8; |
785 | StickNick += (STICK_GAIN * ExternStickNick) / 8; |
786 | StickRoll += (STICK_GAIN * ExternStickRoll) / 8; |
786 | StickRoll += (STICK_GAIN * ExternStickRoll) / 8; |
787 | StickYaw += (STICK_GAIN * ExternStickYaw); |
787 | StickYaw += (STICK_GAIN * ExternStickYaw); |
788 | 788 | ||
789 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
789 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
790 | //+ Analog control via serial communication |
790 | //+ Analog control via serial communication |
791 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
791 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
792 | 792 | ||
793 | if(ExternControl.Config & 0x01 && FCParam.ExternalControl > 128) |
793 | if(ExternControl.Config & 0x01 && FCParam.ExternalControl > 128) |
794 | { |
794 | { |
795 | StickNick += (int16_t) ExternControl.Nick * (int16_t) ParamSet.Stick_P; |
795 | StickNick += (int16_t) ExternControl.Nick * (int16_t) ParamSet.Stick_P; |
796 | StickRoll += (int16_t) ExternControl.Roll * (int16_t) ParamSet.Stick_P; |
796 | StickRoll += (int16_t) ExternControl.Roll * (int16_t) ParamSet.Stick_P; |
797 | StickYaw += ExternControl.Yaw; |
797 | StickYaw += ExternControl.Yaw; |
798 | ExternHeightValue = (int16_t) ExternControl.Height * (int16_t)ParamSet.Height_Gain; |
798 | ExternHeightValue = (int16_t) ExternControl.Height * (int16_t)ParamSet.Height_Gain; |
799 | if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas; |
799 | if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas; |
800 | } |
800 | } |
801 | if(StickGas < 0) StickGas = 0; |
801 | if(StickGas < 0) StickGas = 0; |
802 | 802 | ||
803 | // disable I part of gyro control feedback |
803 | // disable I part of gyro control feedback |
804 | if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) Gyro_I_Factor = 0; |
804 | if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) Gyro_I_Factor = 0; |
805 | // avoid negative scaling factors |
805 | // avoid negative scaling factors |
806 | if(Gyro_P_Factor < 0) Gyro_P_Factor = 0; |
806 | if(Gyro_P_Factor < 0) Gyro_P_Factor = 0; |
807 | if(Gyro_I_Factor < 0) Gyro_I_Factor = 0; |
807 | if(Gyro_I_Factor < 0) Gyro_I_Factor = 0; |
808 | 808 | ||
809 | 809 | ||
810 | // update max stick positions for nick and roll |
810 | // update max stick positions for nick and roll |
811 | 811 | ||
812 | if(abs(StickNick / STICK_GAIN) > MaxStickNick) |
812 | if(abs(StickNick / STICK_GAIN) > MaxStickNick) |
813 | { |
813 | { |
814 | MaxStickNick = abs(StickNick)/STICK_GAIN; |
814 | MaxStickNick = abs(StickNick)/STICK_GAIN; |
815 | if(MaxStickNick > 100) MaxStickNick = 100; |
815 | if(MaxStickNick > 100) MaxStickNick = 100; |
816 | } |
816 | } |
817 | else MaxStickNick--; |
817 | else MaxStickNick--; |
818 | if(abs(StickRoll / STICK_GAIN) > MaxStickRoll) |
818 | if(abs(StickRoll / STICK_GAIN) > MaxStickRoll) |
819 | { |
819 | { |
820 | MaxStickRoll = abs(StickRoll)/STICK_GAIN; |
820 | MaxStickRoll = abs(StickRoll)/STICK_GAIN; |
821 | if(MaxStickRoll > 100) MaxStickRoll = 100; |
821 | if(MaxStickRoll > 100) MaxStickRoll = 100; |
822 | } |
822 | } |
823 | else MaxStickRoll--; |
823 | else MaxStickRoll--; |
824 | 824 | ||
825 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
825 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
826 | // Looping? |
826 | // Looping? |
827 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
827 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
828 | 828 | ||
829 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_LEFT) Looping_Left = 1; |
829 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_LEFT) Looping_Left = 1; |
830 | else |
830 | else |
831 | { |
831 | { |
832 | if(Looping_Left) // Hysteresis |
832 | if(Looping_Left) // Hysteresis |
833 | { |
833 | { |
834 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Left = 0; |
834 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Left = 0; |
835 | } |
835 | } |
836 | } |
836 | } |
837 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_RIGHT) Looping_Right = 1; |
837 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_RIGHT) Looping_Right = 1; |
838 | else |
838 | else |
839 | { |
839 | { |
840 | if(Looping_Right) // Hysteresis |
840 | if(Looping_Right) // Hysteresis |
841 | { |
841 | { |
842 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Right = 0; |
842 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Right = 0; |
843 | } |
843 | } |
844 | } |
844 | } |
845 | 845 | ||
846 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_UP) Looping_Top = 1; |
846 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_UP) Looping_Top = 1; |
847 | else |
847 | else |
848 | { |
848 | { |
849 | if(Looping_Top) // Hysteresis |
849 | if(Looping_Top) // Hysteresis |
850 | { |
850 | { |
851 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Top = 0; |
851 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Top = 0; |
852 | } |
852 | } |
853 | } |
853 | } |
854 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_DOWN) Looping_Down = 1; |
854 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_DOWN) Looping_Down = 1; |
855 | else |
855 | else |
856 | { |
856 | { |
857 | if(Looping_Down) // Hysteresis |
857 | if(Looping_Down) // Hysteresis |
858 | { |
858 | { |
859 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Down = 0; |
859 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Down = 0; |
860 | } |
860 | } |
861 | } |
861 | } |
862 | 862 | ||
863 | if(Looping_Left || Looping_Right) Looping_Roll = 1; else Looping_Roll = 0; |
863 | if(Looping_Left || Looping_Right) Looping_Roll = 1; else Looping_Roll = 0; |
864 | if(Looping_Top || Looping_Down) {Looping_Nick = 1; Looping_Roll = 0; Looping_Left = 0; Looping_Right = 0;} else Looping_Nick = 0; |
864 | if(Looping_Top || Looping_Down) {Looping_Nick = 1; Looping_Roll = 0; Looping_Left = 0; Looping_Right = 0;} else Looping_Nick = 0; |
865 | } // End of new RC-Values or Emergency Landing |
865 | } // End of new RC-Values or Emergency Landing |
866 | 866 | ||
867 | 867 | ||
868 | if(Looping_Roll || Looping_Nick) |
868 | if(Looping_Roll || Looping_Nick) |
869 | { |
869 | { |
870 | if(GasMixFraction > ParamSet.LoopGasLimit) GasMixFraction = ParamSet.LoopGasLimit; |
870 | if(GasMixFraction > ParamSet.LoopGasLimit) GasMixFraction = ParamSet.LoopGasLimit; |
871 | } |
871 | } |
872 | 872 | ||
873 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
873 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
874 | // in case of emergency landing |
874 | // in case of emergency landing |
875 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
875 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
876 | // set all inputs to save values |
876 | // set all inputs to save values |
877 | if(MKFlags & MKFLAG_EMERGENCY_LANDING) |
877 | if(MKFlags & MKFLAG_EMERGENCY_LANDING) |
878 | { |
878 | { |
879 | StickYaw = 0; |
879 | StickYaw = 0; |
880 | StickNick = 0; |
880 | StickNick = 0; |
881 | StickRoll = 0; |
881 | StickRoll = 0; |
882 | Gyro_P_Factor = (float) 100 / (256.0 / STICK_GAIN); |
882 | Gyro_P_Factor = (float) 100 / (256.0 / STICK_GAIN); |
883 | Gyro_I_Factor = (float) 120 / (44000 / STICK_GAIN); |
883 | Gyro_I_Factor = (float) 120 / (44000 / STICK_GAIN); |
884 | Looping_Roll = 0; |
884 | Looping_Roll = 0; |
885 | Looping_Nick = 0; |
885 | Looping_Nick = 0; |
886 | MaxStickNick = 0; |
886 | MaxStickNick = 0; |
887 | MaxStickRoll = 0; |
887 | MaxStickRoll = 0; |
888 | } |
888 | } |
889 | 889 | ||
890 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
890 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
891 | // Trim Gyro-Integrals to ACC-Signals |
891 | // Trim Gyro-Integrals to ACC-Signals |
892 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
892 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
893 | 893 | ||
894 | #define BALANCE_NUMBER 256L |
894 | #define BALANCE_NUMBER 256L |
895 | // sum for averaging |
895 | // sum for averaging |
896 | MeanIntegralNick += IntegralNick; |
896 | MeanIntegralNick += IntegralNick; |
897 | MeanIntegralRoll += IntegralRoll; |
897 | MeanIntegralRoll += IntegralRoll; |
898 | 898 | ||
899 | if(Looping_Nick || Looping_Roll) // if looping in any direction |
899 | if(Looping_Nick || Looping_Roll) // if looping in any direction |
900 | { |
900 | { |
901 | // reset averaging for acc and gyro integral as well as gyro integral acc correction |
901 | // reset averaging for acc and gyro integral as well as gyro integral acc correction |
902 | MeasurementCounter = 0; |
902 | MeasurementCounter = 0; |
903 | 903 | ||
904 | IntegralAccNick = 0; |
904 | IntegralAccNick = 0; |
905 | IntegralAccRoll = 0; |
905 | IntegralAccRoll = 0; |
906 | 906 | ||
907 | MeanIntegralNick = 0; |
907 | MeanIntegralNick = 0; |
908 | MeanIntegralRoll = 0; |
908 | MeanIntegralRoll = 0; |
909 | 909 | ||
910 | Reading_IntegralGyroNick2 = Reading_IntegralGyroNick; |
910 | Reading_IntegralGyroNick2 = Reading_IntegralGyroNick; |
911 | Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll; |
911 | Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll; |
912 | 912 | ||
913 | AttitudeCorrectionNick = 0; |
913 | AttitudeCorrectionNick = 0; |
914 | AttitudeCorrectionRoll = 0; |
914 | AttitudeCorrectionRoll = 0; |
915 | } |
915 | } |
916 | 916 | ||
917 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
917 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
918 | if(!Looping_Nick && !Looping_Roll) // if not lopping in any direction |
918 | if(!Looping_Nick && !Looping_Roll) // if not lopping in any direction |
919 | { |
919 | { |
920 | int32_t tmp_long, tmp_long2; |
920 | int32_t tmp_long, tmp_long2; |
921 | // determine the deviation of gyro integral from averaged acceleration sensor |
921 | // determine the deviation of gyro integral from averaged acceleration sensor |
922 | tmp_long = (int32_t)(IntegralNick / ParamSet.GyroAccFactor - (int32_t)Mean_AccNick); |
922 | tmp_long = (int32_t)(IntegralNick / ParamSet.GyroAccFactor - (int32_t)Mean_AccNick); |
923 | tmp_long /= 16; |
923 | tmp_long /= 16; |
924 | tmp_long2 = (int32_t)(IntegralRoll / ParamSet.GyroAccFactor - (int32_t)Mean_AccRoll); |
924 | tmp_long2 = (int32_t)(IntegralRoll / ParamSet.GyroAccFactor - (int32_t)Mean_AccRoll); |
925 | tmp_long2 /= 16; |
925 | tmp_long2 /= 16; |
926 | 926 | ||
927 | if((MaxStickNick > 64) || (MaxStickRoll > 64)) // reduce effect during stick commands |
927 | if((MaxStickNick > 64) || (MaxStickRoll > 64)) // reduce effect during stick commands |
928 | { |
928 | { |
929 | tmp_long /= 3; |
929 | tmp_long /= 3; |
930 | tmp_long2 /= 3; |
930 | tmp_long2 /= 3; |
931 | } |
931 | } |
932 | if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25) // reduce further if yaw stick is active |
932 | if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25) // reduce further if yaw stick is active |
933 | { |
933 | { |
934 | tmp_long /= 3; |
934 | tmp_long /= 3; |
935 | tmp_long2 /= 3; |
935 | tmp_long2 /= 3; |
936 | } |
936 | } |
937 | 937 | ||
938 | #define BALANCE 32 |
938 | #define BALANCE 32 |
939 | // limit correction effect |
939 | // limit correction effect |
940 | if(tmp_long > BALANCE) tmp_long = BALANCE; |
940 | if(tmp_long > BALANCE) tmp_long = BALANCE; |
941 | if(tmp_long < -BALANCE) tmp_long =-BALANCE; |
941 | if(tmp_long < -BALANCE) tmp_long =-BALANCE; |
942 | if(tmp_long2 > BALANCE) tmp_long2 = BALANCE; |
942 | if(tmp_long2 > BALANCE) tmp_long2 = BALANCE; |
943 | if(tmp_long2 <-BALANCE) tmp_long2 =-BALANCE; |
943 | if(tmp_long2 <-BALANCE) tmp_long2 =-BALANCE; |
944 | // correct current readings |
944 | // correct current readings |
945 | Reading_IntegralGyroNick -= tmp_long; |
945 | Reading_IntegralGyroNick -= tmp_long; |
946 | Reading_IntegralGyroRoll -= tmp_long2; |
946 | Reading_IntegralGyroRoll -= tmp_long2; |
947 | } |
947 | } |
948 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
948 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
949 | // MeasurementCounter is incremented in the isr of analog.c |
949 | // MeasurementCounter is incremented in the isr of analog.c |
950 | if(MeasurementCounter >= BALANCE_NUMBER) // averaging number has reached |
950 | if(MeasurementCounter >= BALANCE_NUMBER) // averaging number has reached |
951 | { |
951 | { |
952 | static int16_t cnt = 0; |
952 | static int16_t cnt = 0; |
953 | static int8_t last_n_p, last_n_n, last_r_p, last_r_n; |
953 | static int8_t last_n_p, last_n_n, last_r_p, last_r_n; |
954 | static int32_t MeanIntegralNick_old, MeanIntegralRoll_old; |
954 | static int32_t MeanIntegralNick_old, MeanIntegralRoll_old; |
955 | 955 | ||
956 | // if not lopping in any direction (this should be alwais the case, |
956 | // if not lopping in any direction (this should be alwais the case, |
957 | // because the Measurement counter is reset to 0 if looping in any direction is active.) |
957 | // because the Measurement counter is reset to 0 if looping in any direction is active.) |
958 | if(!Looping_Nick && !Looping_Roll && !FunnelCourse) |
958 | if(!Looping_Nick && !Looping_Roll && !FunnelCourse) |
959 | { |
959 | { |
960 | // Calculate mean value of the gyro integrals |
960 | // Calculate mean value of the gyro integrals |
961 | MeanIntegralNick /= BALANCE_NUMBER; |
961 | MeanIntegralNick /= BALANCE_NUMBER; |
962 | MeanIntegralRoll /= BALANCE_NUMBER; |
962 | MeanIntegralRoll /= BALANCE_NUMBER; |
963 | 963 | ||
964 | // Calculate mean of the acceleration values |
964 | // Calculate mean of the acceleration values |
965 | IntegralAccNick = (ParamSet.GyroAccFactor * IntegralAccNick) / BALANCE_NUMBER; |
965 | IntegralAccNick = (ParamSet.GyroAccFactor * IntegralAccNick) / BALANCE_NUMBER; |
966 | IntegralAccRoll = (ParamSet.GyroAccFactor * IntegralAccRoll ) / BALANCE_NUMBER; |
966 | IntegralAccRoll = (ParamSet.GyroAccFactor * IntegralAccRoll ) / BALANCE_NUMBER; |
967 | 967 | ||
968 | // Nick ++++++++++++++++++++++++++++++++++++++++++++++++ |
968 | // Nick ++++++++++++++++++++++++++++++++++++++++++++++++ |
969 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
969 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
970 | IntegralErrorNick = (int32_t)(MeanIntegralNick - (int32_t)IntegralAccNick); |
970 | IntegralErrorNick = (int32_t)(MeanIntegralNick - (int32_t)IntegralAccNick); |
971 | CorrectionNick = IntegralErrorNick / ParamSet.GyroAccTrim; |
971 | CorrectionNick = IntegralErrorNick / ParamSet.GyroAccTrim; |
972 | AttitudeCorrectionNick = CorrectionNick / BALANCE_NUMBER; |
972 | AttitudeCorrectionNick = CorrectionNick / BALANCE_NUMBER; |
973 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
973 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
974 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
974 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
975 | IntegralErrorRoll = (int32_t)(MeanIntegralRoll - (int32_t)IntegralAccRoll); |
975 | IntegralErrorRoll = (int32_t)(MeanIntegralRoll - (int32_t)IntegralAccRoll); |
976 | CorrectionRoll = IntegralErrorRoll / ParamSet.GyroAccTrim; |
976 | CorrectionRoll = IntegralErrorRoll / ParamSet.GyroAccTrim; |
977 | AttitudeCorrectionRoll = CorrectionRoll / BALANCE_NUMBER; |
977 | AttitudeCorrectionRoll = CorrectionRoll / BALANCE_NUMBER; |
978 | 978 | ||
979 | if((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25)) |
979 | if((MaxStickNick > 64) || (MaxStickRoll > 64) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25)) |
980 | { |
980 | { |
981 | AttitudeCorrectionNick /= 2; |
981 | AttitudeCorrectionNick /= 2; |
982 | AttitudeCorrectionRoll /= 2; |
982 | AttitudeCorrectionRoll /= 2; |
983 | } |
983 | } |
984 | 984 | ||
985 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
985 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
986 | // Gyro-Drift ermitteln |
986 | // Gyro-Drift ermitteln |
987 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
987 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
988 | // deviation of gyro nick integral (IntegralNick is corrected by averaged acc sensor) |
988 | // deviation of gyro nick integral (IntegralNick is corrected by averaged acc sensor) |
989 | IntegralErrorNick = IntegralNick2 - IntegralNick; |
989 | IntegralErrorNick = IntegralNick2 - IntegralNick; |
990 | Reading_IntegralGyroNick2 -= IntegralErrorNick; |
990 | Reading_IntegralGyroNick2 -= IntegralErrorNick; |
991 | // deviation of gyro nick integral (IntegralNick is corrected by averaged acc sensor) |
991 | // deviation of gyro nick integral (IntegralNick is corrected by averaged acc sensor) |
992 | IntegralErrorRoll = IntegralRoll2 - IntegralRoll; |
992 | IntegralErrorRoll = IntegralRoll2 - IntegralRoll; |
993 | Reading_IntegralGyroRoll2 -= IntegralErrorRoll; |
993 | Reading_IntegralGyroRoll2 -= IntegralErrorRoll; |
994 | 994 | ||
995 | if(YawGyroDrift > BALANCE_NUMBER/2) AdNeutralYaw++; |
995 | if(YawGyroDrift > BALANCE_NUMBER/2) AdNeutralYaw++; |
996 | if(YawGyroDrift < -BALANCE_NUMBER/2) AdNeutralYaw--; |
996 | if(YawGyroDrift < -BALANCE_NUMBER/2) AdNeutralYaw--; |
997 | YawGyroDrift = 0; |
997 | YawGyroDrift = 0; |
998 | /* |
998 | /* |
999 | DebugOut.Analog[17] = IntegralAccNick / 26; |
999 | DebugOut.Analog[17] = IntegralAccNick / 26; |
1000 | DebugOut.Analog[18] = IntegralAccRoll / 26; |
1000 | DebugOut.Analog[18] = IntegralAccRoll / 26; |
1001 | DebugOut.Analog[19] = IntegralErrorNick;// / 26; |
1001 | DebugOut.Analog[19] = IntegralErrorNick;// / 26; |
1002 | DebugOut.Analog[20] = IntegralErrorRoll;// / 26; |
1002 | DebugOut.Analog[20] = IntegralErrorRoll;// / 26; |
1003 | DebugOut.Analog[21] = MeanIntegralNick / 26; |
1003 | DebugOut.Analog[21] = MeanIntegralNick / 26; |
1004 | DebugOut.Analog[22] = MeanIntegralRoll / 26; |
1004 | DebugOut.Analog[22] = MeanIntegralRoll / 26; |
1005 | //DebugOut.Analog[28] = CorrectionNick; |
1005 | //DebugOut.Analog[28] = CorrectionNick; |
1006 | DebugOut.Analog[29] = CorrectionRoll; |
1006 | DebugOut.Analog[29] = CorrectionRoll; |
1007 | DebugOut.Analog[30] = AttitudeCorrectionRoll * 10; |
1007 | DebugOut.Analog[30] = AttitudeCorrectionRoll * 10; |
1008 | */ |
1008 | */ |
1009 | 1009 | ||
1010 | #define ERROR_LIMIT (BALANCE_NUMBER * 4) |
1010 | #define ERROR_LIMIT (BALANCE_NUMBER * 4) |
1011 | #define ERROR_LIMIT2 (BALANCE_NUMBER * 16) |
1011 | #define ERROR_LIMIT2 (BALANCE_NUMBER * 16) |
1012 | #define MOVEMENT_LIMIT 20000 |
1012 | #define MOVEMENT_LIMIT 20000 |
1013 | // Nick +++++++++++++++++++++++++++++++++++++++++++++++++ |
1013 | // Nick +++++++++++++++++++++++++++++++++++++++++++++++++ |
1014 | cnt = 1;// + labs(IntegralErrorNick) / 4096; |
1014 | cnt = 1;// + labs(IntegralErrorNick) / 4096; |
1015 | CorrectionNick = 0; |
1015 | CorrectionNick = 0; |
1016 | if(labs(MeanIntegralNick_old - MeanIntegralNick) < MOVEMENT_LIMIT) |
1016 | if(labs(MeanIntegralNick_old - MeanIntegralNick) < MOVEMENT_LIMIT) |
1017 | { |
1017 | { |
1018 | if(IntegralErrorNick > ERROR_LIMIT2) |
1018 | if(IntegralErrorNick > ERROR_LIMIT2) |
1019 | { |
1019 | { |
1020 | if(last_n_p) |
1020 | if(last_n_p) |
1021 | { |
1021 | { |
1022 | cnt += labs(IntegralErrorNick) / ERROR_LIMIT2; |
1022 | cnt += labs(IntegralErrorNick) / ERROR_LIMIT2; |
1023 | CorrectionNick = IntegralErrorNick / 8; |
1023 | CorrectionNick = IntegralErrorNick / 8; |
1024 | if(CorrectionNick > 5000) CorrectionNick = 5000; |
1024 | if(CorrectionNick > 5000) CorrectionNick = 5000; |
1025 | AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER; |
1025 | AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER; |
1026 | } |
1026 | } |
1027 | else last_n_p = 1; |
1027 | else last_n_p = 1; |
1028 | } |
1028 | } |
1029 | else last_n_p = 0; |
1029 | else last_n_p = 0; |
1030 | if(IntegralErrorNick < -ERROR_LIMIT2) |
1030 | if(IntegralErrorNick < -ERROR_LIMIT2) |
1031 | { |
1031 | { |
1032 | if(last_n_n) |
1032 | if(last_n_n) |
1033 | { |
1033 | { |
1034 | cnt += labs(IntegralErrorNick) / ERROR_LIMIT2; |
1034 | cnt += labs(IntegralErrorNick) / ERROR_LIMIT2; |
1035 | CorrectionNick = IntegralErrorNick / 8; |
1035 | CorrectionNick = IntegralErrorNick / 8; |
1036 | if(CorrectionNick < -5000) CorrectionNick = -5000; |
1036 | if(CorrectionNick < -5000) CorrectionNick = -5000; |
1037 | AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER; |
1037 | AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER; |
1038 | } |
1038 | } |
1039 | else last_n_n = 1; |
1039 | else last_n_n = 1; |
1040 | } |
1040 | } |
1041 | else last_n_n = 0; |
1041 | else last_n_n = 0; |
1042 | } |
1042 | } |
1043 | else |
1043 | else |
1044 | { |
1044 | { |
1045 | cnt = 0; |
1045 | cnt = 0; |
1046 | BadCompassHeading = 1000; |
1046 | BadCompassHeading = 1000; |
1047 | } |
1047 | } |
1048 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
1048 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
1049 | // correct Gyro Offsets |
1049 | // correct Gyro Offsets |
1050 | if(IntegralErrorNick > ERROR_LIMIT) AdNeutralNick += cnt; |
1050 | if(IntegralErrorNick > ERROR_LIMIT) AdNeutralNick += cnt; |
1051 | if(IntegralErrorNick < -ERROR_LIMIT) AdNeutralNick -= cnt; |
1051 | if(IntegralErrorNick < -ERROR_LIMIT) AdNeutralNick -= cnt; |
1052 | 1052 | ||
1053 | // Roll +++++++++++++++++++++++++++++++++++++++++++++++++ |
1053 | // Roll +++++++++++++++++++++++++++++++++++++++++++++++++ |
1054 | cnt = 1;// + labs(IntegralErrorNick) / 4096; |
1054 | cnt = 1;// + labs(IntegralErrorNick) / 4096; |
1055 | CorrectionRoll = 0; |
1055 | CorrectionRoll = 0; |
1056 | if(labs(MeanIntegralRoll_old - MeanIntegralRoll) < MOVEMENT_LIMIT) |
1056 | if(labs(MeanIntegralRoll_old - MeanIntegralRoll) < MOVEMENT_LIMIT) |
1057 | { |
1057 | { |
1058 | if(IntegralErrorRoll > ERROR_LIMIT2) |
1058 | if(IntegralErrorRoll > ERROR_LIMIT2) |
1059 | { |
1059 | { |
1060 | if(last_r_p) |
1060 | if(last_r_p) |
1061 | { |
1061 | { |
1062 | cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2; |
1062 | cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2; |
1063 | CorrectionRoll = IntegralErrorRoll / 8; |
1063 | CorrectionRoll = IntegralErrorRoll / 8; |
1064 | if(CorrectionRoll > 5000) CorrectionRoll = 5000; |
1064 | if(CorrectionRoll > 5000) CorrectionRoll = 5000; |
1065 | AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER; |
1065 | AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER; |
1066 | } |
1066 | } |
1067 | else last_r_p = 1; |
1067 | else last_r_p = 1; |
1068 | } |
1068 | } |
1069 | else last_r_p = 0; |
1069 | else last_r_p = 0; |
1070 | if(IntegralErrorRoll < -ERROR_LIMIT2) |
1070 | if(IntegralErrorRoll < -ERROR_LIMIT2) |
1071 | { |
1071 | { |
1072 | if(last_r_n) |
1072 | if(last_r_n) |
1073 | { |
1073 | { |
1074 | cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2; |
1074 | cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2; |
1075 | CorrectionRoll = IntegralErrorRoll / 8; |
1075 | CorrectionRoll = IntegralErrorRoll / 8; |
1076 | if(CorrectionRoll < -5000) CorrectionRoll = -5000; |
1076 | if(CorrectionRoll < -5000) CorrectionRoll = -5000; |
1077 | AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER; |
1077 | AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER; |
1078 | } |
1078 | } |
1079 | else last_r_n = 1; |
1079 | else last_r_n = 1; |
1080 | } |
1080 | } |
1081 | else last_r_n = 0; |
1081 | else last_r_n = 0; |
1082 | } |
1082 | } |
1083 | else |
1083 | else |
1084 | { |
1084 | { |
1085 | cnt = 0; |
1085 | cnt = 0; |
1086 | BadCompassHeading = 1000; |
1086 | BadCompassHeading = 1000; |
1087 | } |
1087 | } |
1088 | // correct Gyro Offsets |
1088 | // correct Gyro Offsets |
1089 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
1089 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
1090 | if(IntegralErrorRoll > ERROR_LIMIT) AdNeutralRoll += cnt; |
1090 | if(IntegralErrorRoll > ERROR_LIMIT) AdNeutralRoll += cnt; |
1091 | if(IntegralErrorRoll < -ERROR_LIMIT) AdNeutralRoll -= cnt; |
1091 | if(IntegralErrorRoll < -ERROR_LIMIT) AdNeutralRoll -= cnt; |
1092 | /* |
1092 | /* |
1093 | DebugOut.Analog[27] = CorrectionRoll; |
1093 | DebugOut.Analog[27] = CorrectionRoll; |
1094 | DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick); |
1094 | DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick); |
1095 | DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll); |
1095 | DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll); |
1096 | */ |
1096 | */ |
1097 | } |
1097 | } |
1098 | else // looping is active |
1098 | else // looping is active |
1099 | { |
1099 | { |
1100 | AttitudeCorrectionRoll = 0; |
1100 | AttitudeCorrectionRoll = 0; |
1101 | AttitudeCorrectionNick = 0; |
1101 | AttitudeCorrectionNick = 0; |
1102 | FunnelCourse = 0; |
1102 | FunnelCourse = 0; |
1103 | } |
1103 | } |
1104 | 1104 | ||
1105 | // if Gyro_I_Factor == 0 , for example at Heading Hold, ignore attitude correction |
1105 | // if Gyro_I_Factor == 0 , for example at Heading Hold, ignore attitude correction |
1106 | if(!Gyro_I_Factor) |
1106 | if(!Gyro_I_Factor) |
1107 | { |
1107 | { |
1108 | AttitudeCorrectionRoll = 0; |
1108 | AttitudeCorrectionRoll = 0; |
1109 | AttitudeCorrectionNick = 0; |
1109 | AttitudeCorrectionNick = 0; |
1110 | } |
1110 | } |
1111 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1111 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1112 | MeanIntegralNick_old = MeanIntegralNick; |
1112 | MeanIntegralNick_old = MeanIntegralNick; |
1113 | MeanIntegralRoll_old = MeanIntegralRoll; |
1113 | MeanIntegralRoll_old = MeanIntegralRoll; |
1114 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1114 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1115 | // reset variables used for averaging |
1115 | // reset variables used for averaging |
1116 | IntegralAccNick = 0; |
1116 | IntegralAccNick = 0; |
1117 | IntegralAccRoll = 0; |
1117 | IntegralAccRoll = 0; |
1118 | MeanIntegralNick = 0; |
1118 | MeanIntegralNick = 0; |
1119 | MeanIntegralRoll = 0; |
1119 | MeanIntegralRoll = 0; |
1120 | MeasurementCounter = 0; |
1120 | MeasurementCounter = 0; |
1121 | } // end of averaging |
1121 | } // end of averaging |
1122 | 1122 | ||
1123 | 1123 | ||
1124 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1124 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1125 | // Yawing |
1125 | // Yawing |
1126 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1126 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1127 | if(abs(StickYaw) > 15 ) // yaw stick is activated |
1127 | if(abs(StickYaw) > 15 ) // yaw stick is activated |
1128 | { |
1128 | { |
1129 | BadCompassHeading = 1000; |
1129 | BadCompassHeading = 1000; |
1130 | if(!(ParamSet.GlobalConfig & CFG_COMPASS_FIX)) |
1130 | if(!(ParamSet.GlobalConfig & CFG_COMPASS_FIX)) |
1131 | { |
1131 | { |
1132 | UpdateCompassCourse = 1; |
1132 | UpdateCompassCourse = 1; |
1133 | } |
1133 | } |
1134 | } |
1134 | } |
1135 | // exponential stick sensitivity in yawring rate |
1135 | // exponential stick sensitivity in yawring rate |
1136 | tmp_int = (int32_t) ParamSet.Yaw_P * ((int32_t)StickYaw * abs(StickYaw)) / 512L; // expo y = ax + bx² |
1136 | tmp_int = (int32_t) ParamSet.Yaw_P * ((int32_t)StickYaw * abs(StickYaw)) / 512L; // expo y = ax + bx² |
1137 | tmp_int += (ParamSet.Yaw_P * StickYaw) / 4; |
1137 | tmp_int += (ParamSet.Yaw_P * StickYaw) / 4; |
1138 | SetPointYaw = tmp_int; |
1138 | SetPointYaw = tmp_int; |
1139 | // trimm drift of Reading_IntegralGyroYaw with SetPointYaw(StickYaw) |
1139 | // trimm drift of Reading_IntegralGyroYaw with SetPointYaw(StickYaw) |
1140 | Reading_IntegralGyroYaw -= tmp_int; |
1140 | Reading_IntegralGyroYaw -= tmp_int; |
1141 | // limit the effect |
1141 | // limit the effect |
1142 | if(Reading_IntegralGyroYaw > 50000) Reading_IntegralGyroYaw = 50000; |
1142 | if(Reading_IntegralGyroYaw > 50000) Reading_IntegralGyroYaw = 50000; |
1143 | if(Reading_IntegralGyroYaw <-50000) Reading_IntegralGyroYaw =-50000; |
1143 | if(Reading_IntegralGyroYaw <-50000) Reading_IntegralGyroYaw =-50000; |
1144 | 1144 | ||
1145 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1145 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1146 | // Compass |
1146 | // Compass |
1147 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1147 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1148 | // compass code is used if Compass option is selected |
1148 | // compass code is used if Compass option is selected |
1149 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
1149 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
1150 | { |
1150 | { |
1151 | int16_t w, v, r,correction, error; |
1151 | int16_t w, v, r,correction, error; |
1152 | 1152 | ||
1153 | if(CompassCalState && !(MKFlags & MKFLAG_MOTOR_RUN) ) |
1153 | if(CompassCalState && !(MKFlags & MKFLAG_MOTOR_RUN) ) |
1154 | { |
1154 | { |
1155 | SetCompassCalState(); |
1155 | SetCompassCalState(); |
1156 | #ifdef USE_KILLAGREG |
1156 | #ifdef USE_KILLAGREG |
1157 | MM3_Calibrate(); |
1157 | MM3_Calibrate(); |
1158 | #endif |
1158 | #endif |
1159 | } |
1159 | } |
1160 | else |
1160 | else |
1161 | { |
1161 | { |
1162 | #ifdef USE_KILLAGREG |
1162 | #ifdef USE_KILLAGREG |
1163 | static uint8_t updCompass = 0; |
1163 | static uint8_t updCompass = 0; |
1164 | if (!updCompass--) |
1164 | if (!updCompass--) |
1165 | { |
1165 | { |
1166 | updCompass = 49; // update only at 2ms*50 = 100ms (10Hz) |
1166 | updCompass = 49; // update only at 2ms*50 = 100ms (10Hz) |
1167 | MM3_Heading(); |
1167 | MM3_Heading(); |
1168 | } |
1168 | } |
1169 | #endif |
1169 | #endif |
1170 | 1170 | ||
1171 | // get maximum attitude angle |
1171 | // get maximum attitude angle |
1172 | w = abs(IntegralNick / 512); |
1172 | w = abs(IntegralNick / 512); |
1173 | v = abs(IntegralRoll / 512); |
1173 | v = abs(IntegralRoll / 512); |
1174 | if(v > w) w = v; |
1174 | if(v > w) w = v; |
1175 | correction = w / 8 + 1; |
1175 | correction = w / 8 + 1; |
1176 | // calculate the deviation of the yaw gyro heading and the compass heading |
1176 | // calculate the deviation of the yaw gyro heading and the compass heading |
1177 | if (CompassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined |
1177 | if (CompassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined |
1178 | else error = ((540 + CompassHeading - (YawGyroHeading / YAW_GYRO_DEG_FACTOR)) % 360) - 180; |
1178 | else error = ((540 + CompassHeading - (YawGyroHeading / YAW_GYRO_DEG_FACTOR)) % 360) - 180; |
1179 | 1179 | ||
1180 | if(!BadCompassHeading && w < 25) |
1180 | if(!BadCompassHeading && w < 25) |
1181 | { |
1181 | { |
1182 | YawGyroDrift += error; |
1182 | YawGyroDrift += error; |
1183 | if(UpdateCompassCourse) |
1183 | if(UpdateCompassCourse) |
1184 | { |
1184 | { |
1185 | BeepTime = 200; |
1185 | BeepTime = 200; |
1186 | CompassCourse = (YawGyroHeading / YAW_GYRO_DEG_FACTOR); |
1186 | CompassCourse = (YawGyroHeading / YAW_GYRO_DEG_FACTOR); |
1187 | UpdateCompassCourse = 0; |
1187 | UpdateCompassCourse = 0; |
1188 | } |
1188 | } |
1189 | } |
1189 | } |
1190 | YawGyroHeading += (error * 8) / correction; |
1190 | YawGyroHeading += (error * 8) / correction; |
1191 | w = (w * FCParam.CompassYawEffect) / 32; |
1191 | w = (w * FCParam.CompassYawEffect) / 32; |
1192 | w = FCParam.CompassYawEffect - w; |
1192 | w = FCParam.CompassYawEffect - w; |
1193 | if(w >= 0) |
1193 | if(w >= 0) |
1194 | { |
1194 | { |
1195 | if(!BadCompassHeading) |
1195 | if(!BadCompassHeading) |
1196 | { |
1196 | { |
1197 | v = 64 + (MaxStickNick + MaxStickRoll) / 8; |
1197 | v = 64 + (MaxStickNick + MaxStickRoll) / 8; |
1198 | // calc course deviation |
1198 | // calc course deviation |
1199 | r = ((540 + (YawGyroHeading / YAW_GYRO_DEG_FACTOR) - CompassCourse) % 360) - 180; |
1199 | r = ((540 + (YawGyroHeading / YAW_GYRO_DEG_FACTOR) - CompassCourse) % 360) - 180; |
1200 | v = (r * w) / v; // align to compass course |
1200 | v = (r * w) / v; // align to compass course |
1201 | // limit yaw rate |
1201 | // limit yaw rate |
1202 | w = 3 * FCParam.CompassYawEffect; |
1202 | w = 3 * FCParam.CompassYawEffect; |
1203 | if (v > w) v = w; |
1203 | if (v > w) v = w; |
1204 | else if (v < -w) v = -w; |
1204 | else if (v < -w) v = -w; |
1205 | Reading_IntegralGyroYaw += v; |
1205 | Reading_IntegralGyroYaw += v; |
1206 | } |
1206 | } |
1207 | else |
1207 | else |
1208 | { // wait a while |
1208 | { // wait a while |
1209 | BadCompassHeading--; |
1209 | BadCompassHeading--; |
1210 | } |
1210 | } |
1211 | } |
1211 | } |
1212 | else |
1212 | else |
1213 | { // ignore compass at extreme attitudes for a while |
1213 | { // ignore compass at extreme attitudes for a while |
1214 | BadCompassHeading = 500; |
1214 | BadCompassHeading = 500; |
1215 | } |
1215 | } |
1216 | } |
1216 | } |
1217 | } |
1217 | } |
1218 | 1218 | ||
1219 | #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG)) |
1219 | #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG)) |
1220 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1220 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1221 | // GPS |
1221 | // GPS |
1222 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1222 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1223 | if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE) |
1223 | if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE) |
1224 | { |
1224 | { |
1225 | GPS_Main(); |
1225 | GPS_Main(); |
1226 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
1226 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
1227 | } |
1227 | } |
1228 | else |
1228 | else |
1229 | { |
1229 | { |
1230 | GPS_Nick = 0; |
1230 | GPS_Nick = 0; |
1231 | GPS_Roll = 0; |
1231 | GPS_Roll = 0; |
1232 | } |
1232 | } |
1233 | #endif |
1233 | #endif |
1234 | 1234 | ||
1235 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1235 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1236 | // Debugwerte zuordnen |
1236 | // Debugwerte zuordnen |
1237 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1237 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1238 | if(!TimerDebugOut--) |
1238 | if(!TimerDebugOut--) |
1239 | { |
1239 | { |
1240 | TimerDebugOut = 24; // update debug outputs every 25*2ms = 50 ms (20Hz) |
1240 | TimerDebugOut = 24; // update debug outputs every 25*2ms = 50 ms (20Hz) |
1241 | DebugOut.Analog[0] = IntegralNick / ParamSet.GyroAccFactor; |
1241 | DebugOut.Analog[0] = IntegralNick / ParamSet.GyroAccFactor; |
1242 | DebugOut.Analog[1] = IntegralRoll / ParamSet.GyroAccFactor; |
1242 | DebugOut.Analog[1] = IntegralRoll / ParamSet.GyroAccFactor; |
1243 | DebugOut.Analog[2] = Mean_AccNick; |
1243 | DebugOut.Analog[2] = Mean_AccNick; |
1244 | DebugOut.Analog[3] = Mean_AccRoll; |
1244 | DebugOut.Analog[3] = Mean_AccRoll; |
1245 | DebugOut.Analog[4] = Reading_GyroYaw; |
1245 | DebugOut.Analog[4] = Reading_GyroYaw; |
1246 | DebugOut.Analog[5] = ReadingHeight; |
1246 | DebugOut.Analog[5] = ReadingHeight; |
1247 | DebugOut.Analog[6] = (Reading_Integral_Top / 512); |
1247 | DebugOut.Analog[6] = (Reading_Integral_Top / 512); |
1248 | // DebugOut.Analog[8] = CompassHeading; |
1248 | // DebugOut.Analog[8] = CompassHeading; |
1249 | DebugOut.Analog[8] = RC_Quality; |
1249 | DebugOut.Analog[8] = RC_Quality; |
1250 | DebugOut.Analog[9] = UBat; |
1250 | DebugOut.Analog[9] = UBat; |
1251 | 1251 | ||
1252 | #ifdef HEXAKOPTER |
1252 | #ifdef HEXAKOPTER |
1253 | DebugOut.Analog[10] = Motor_FrontLeft; |
1253 | DebugOut.Analog[10] = Motor_FrontLeft; |
1254 | DebugOut.Analog[11] = Motor_FrontRight; |
1254 | DebugOut.Analog[11] = Motor_FrontRight; |
1255 | DebugOut.Analog[12] = Motor_RearLeft; |
1255 | DebugOut.Analog[12] = Motor_RearLeft; |
1256 | DebugOut.Analog[13] = Motor_RearRight; |
1256 | DebugOut.Analog[13] = Motor_RearRight; |
1257 | DebugOut.Analog[14] = Motor_Left; |
1257 | DebugOut.Analog[14] = Motor_Left; |
1258 | DebugOut.Analog[15] = Motor_Right; |
1258 | DebugOut.Analog[15] = Motor_Right; |
1259 | #else |
1259 | #else |
1260 | DebugOut.Analog[11] = YawGyroHeading / YAW_GYRO_DEG_FACTOR; |
1260 | DebugOut.Analog[11] = YawGyroHeading / YAW_GYRO_DEG_FACTOR; |
1261 | 1261 | ||
1262 | DebugOut.Analog[12] = Motor_Front; |
1262 | DebugOut.Analog[12] = Motor_Front; |
1263 | DebugOut.Analog[13] = Motor_Rear; |
1263 | DebugOut.Analog[13] = Motor_Rear; |
1264 | DebugOut.Analog[14] = Motor_Left; |
1264 | DebugOut.Analog[14] = Motor_Left; |
1265 | DebugOut.Analog[15] = Motor_Right; |
1265 | DebugOut.Analog[15] = Motor_Right; |
1266 | #endif |
1266 | #endif |
1267 | 1267 | ||
1268 | DebugOut.Analog[16] = Mean_AccTop; |
1268 | DebugOut.Analog[16] = Mean_AccTop; |
1269 | 1269 | ||
1270 | DebugOut.Analog[20] = ServoValue; |
1270 | DebugOut.Analog[20] = ServoValue; |
1271 | 1271 | ||
1272 | 1272 | ||
1273 | 1273 | ||
1274 | DebugOut.Analog[30] = GPS_Nick; |
1274 | DebugOut.Analog[30] = GPS_Nick; |
1275 | DebugOut.Analog[31] = GPS_Roll; |
1275 | DebugOut.Analog[31] = GPS_Roll; |
1276 | 1276 | ||
1277 | /* DebugOut.Analog[16] = motor_rx[0]; |
1277 | /* DebugOut.Analog[16] = motor_rx[0]; |
1278 | DebugOut.Analog[17] = motor_rx[1]; |
1278 | DebugOut.Analog[17] = motor_rx[1]; |
1279 | DebugOut.Analog[18] = motor_rx[2]; |
1279 | DebugOut.Analog[18] = motor_rx[2]; |
1280 | DebugOut.Analog[19] = motor_rx[3]; |
1280 | DebugOut.Analog[19] = motor_rx[3]; |
1281 | DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3]; |
1281 | DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3]; |
1282 | DebugOut.Analog[20] /= 14; |
1282 | DebugOut.Analog[20] /= 14; |
1283 | DebugOut.Analog[21] = motor_rx[4]; |
1283 | DebugOut.Analog[21] = motor_rx[4]; |
1284 | DebugOut.Analog[22] = motor_rx[5]; |
1284 | DebugOut.Analog[22] = motor_rx[5]; |
1285 | DebugOut.Analog[23] = motor_rx[6]; |
1285 | DebugOut.Analog[23] = motor_rx[6]; |
1286 | DebugOut.Analog[24] = motor_rx[7]; |
1286 | DebugOut.Analog[24] = motor_rx[7]; |
1287 | DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7]; |
1287 | DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7]; |
1288 | 1288 | ||
1289 | DebugOut.Analog[9] = Reading_GyroNick; |
1289 | DebugOut.Analog[9] = Reading_GyroNick; |
1290 | DebugOut.Analog[9] = SetPointHeight; |
1290 | DebugOut.Analog[9] = SetPointHeight; |
1291 | DebugOut.Analog[10] = Reading_IntegralGyroYaw / 128; |
1291 | DebugOut.Analog[10] = Reading_IntegralGyroYaw / 128; |
1292 | 1292 | ||
1293 | DebugOut.Analog[10] = FCParam.Gyro_I; |
1293 | DebugOut.Analog[10] = FCParam.Gyro_I; |
1294 | DebugOut.Analog[10] = ParamSet.Gyro_I; |
1294 | DebugOut.Analog[10] = ParamSet.Gyro_I; |
1295 | DebugOut.Analog[9] = CompassOffCourse; |
1295 | DebugOut.Analog[9] = CompassOffCourse; |
1296 | DebugOut.Analog[10] = GasMixFraction; |
1296 | DebugOut.Analog[10] = GasMixFraction; |
1297 | DebugOut.Analog[3] = HeightD * 32; |
1297 | DebugOut.Analog[3] = HeightD * 32; |
1298 | DebugOut.Analog[4] = HeightControlGas; |
1298 | DebugOut.Analog[4] = HeightControlGas; |
1299 | */ |
1299 | */ |
1300 | } |
1300 | } |
1301 | 1301 | ||
1302 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1302 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1303 | // calculate control feedback from angle (gyro integral) and agular velocity (gyro signal) |
1303 | // calculate control feedback from angle (gyro integral) and agular velocity (gyro signal) |
1304 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1304 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1305 | 1305 | ||
1306 | if(Looping_Nick) Reading_GyroNick = Reading_GyroNick * Gyro_P_Factor; |
1306 | if(Looping_Nick) Reading_GyroNick = Reading_GyroNick * Gyro_P_Factor; |
1307 | else Reading_GyroNick = IntegralNick * Gyro_I_Factor + Reading_GyroNick * Gyro_P_Factor; |
1307 | else Reading_GyroNick = IntegralNick * Gyro_I_Factor + Reading_GyroNick * Gyro_P_Factor; |
1308 | if(Looping_Roll) Reading_GyroRoll = Reading_GyroRoll * Gyro_P_Factor; |
1308 | if(Looping_Roll) Reading_GyroRoll = Reading_GyroRoll * Gyro_P_Factor; |
1309 | else Reading_GyroRoll = IntegralRoll * Gyro_I_Factor + Reading_GyroRoll * Gyro_P_Factor; |
1309 | else Reading_GyroRoll = IntegralRoll * Gyro_I_Factor + Reading_GyroRoll * Gyro_P_Factor; |
1310 | Reading_GyroYaw = Reading_GyroYaw * (2 * Gyro_P_Factor) + IntegralYaw * Gyro_I_Factor / 2; |
1310 | Reading_GyroYaw = Reading_GyroYaw * (2 * Gyro_P_Factor) + IntegralYaw * Gyro_I_Factor / 2; |
1311 | 1311 | ||
1312 | DebugOut.Analog[21] = Reading_GyroNick; |
1312 | DebugOut.Analog[21] = Reading_GyroNick; |
1313 | DebugOut.Analog[22] = Reading_GyroRoll; |
1313 | DebugOut.Analog[22] = Reading_GyroRoll; |
1314 | 1314 | ||
1315 | // limit control feedback |
1315 | // limit control feedback |
1316 | #define MAX_SENSOR (4096 * STICK_GAIN) |
1316 | #define MAX_SENSOR (4096 * STICK_GAIN) |
1317 | if(Reading_GyroNick > MAX_SENSOR) Reading_GyroNick = MAX_SENSOR; |
1317 | if(Reading_GyroNick > MAX_SENSOR) Reading_GyroNick = MAX_SENSOR; |
1318 | if(Reading_GyroNick < -MAX_SENSOR) Reading_GyroNick = -MAX_SENSOR; |
1318 | if(Reading_GyroNick < -MAX_SENSOR) Reading_GyroNick = -MAX_SENSOR; |
1319 | if(Reading_GyroRoll > MAX_SENSOR) Reading_GyroRoll = MAX_SENSOR; |
1319 | if(Reading_GyroRoll > MAX_SENSOR) Reading_GyroRoll = MAX_SENSOR; |
1320 | if(Reading_GyroRoll < -MAX_SENSOR) Reading_GyroRoll = -MAX_SENSOR; |
1320 | if(Reading_GyroRoll < -MAX_SENSOR) Reading_GyroRoll = -MAX_SENSOR; |
1321 | if(Reading_GyroYaw > MAX_SENSOR) Reading_GyroYaw = MAX_SENSOR; |
1321 | if(Reading_GyroYaw > MAX_SENSOR) Reading_GyroYaw = MAX_SENSOR; |
1322 | if(Reading_GyroYaw < -MAX_SENSOR) Reading_GyroYaw = -MAX_SENSOR; |
1322 | if(Reading_GyroYaw < -MAX_SENSOR) Reading_GyroYaw = -MAX_SENSOR; |
1323 | 1323 | ||
1324 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1324 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1325 | // Height Control |
1325 | // Height Control |
1326 | // The height control algorithm reduces the gas but does not increase the gas. |
1326 | // The height control algorithm reduces the gas but does not increase the gas. |
1327 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1327 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1328 | 1328 | ||
1329 | GasMixFraction *= STICK_GAIN; |
1329 | GasMixFraction *= STICK_GAIN; |
1330 | 1330 | ||
1331 | // If height control is activated and no emergency landing is active |
1331 | // If height control is activated and no emergency landing is active |
1332 | if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL) && !(MKFlags & MKFLAG_EMERGENCY_LANDING) ) |
1332 | if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL) && !(MKFlags & MKFLAG_EMERGENCY_LANDING) ) |
1333 | { |
1333 | { |
1334 | int tmp_int; |
1334 | int tmp_int; |
1335 | // if height control is activated by an rc channel |
1335 | // if height control is activated by an rc channel |
1336 | if(ParamSet.GlobalConfig & CFG_HEIGHT_SWITCH) |
1336 | if(ParamSet.GlobalConfig & CFG_HEIGHT_SWITCH) |
1337 | { // check if parameter is less than activation threshold |
1337 | { // check if parameter is less than activation threshold |
1338 | if(FCParam.MaxHeight < 50) |
1338 | if(FCParam.MaxHeight < 50) |
1339 | { |
1339 | { |
1340 | SetPointHeight = ReadingHeight - 20; // update SetPoint with current reading |
1340 | SetPointHeight = ReadingHeight - 20; // update SetPoint with current reading |
1341 | HeightControlActive = 0; // disable height control |
1341 | HeightControlActive = 0; // disable height control |
1342 | } |
1342 | } |
1343 | else HeightControlActive = 1; // enable height control |
1343 | else HeightControlActive = 1; // enable height control |
1344 | } |
1344 | } |
1345 | else // no switchable height control |
1345 | else // no switchable height control |
1346 | { |
1346 | { |
1347 | SetPointHeight = ((int16_t) ExternHeightValue + (int16_t) FCParam.MaxHeight) * (int16_t)ParamSet.Height_Gain - 20; |
1347 | SetPointHeight = ((int16_t) ExternHeightValue + (int16_t) FCParam.MaxHeight) * (int16_t)ParamSet.Height_Gain - 20; |
1348 | HeightControlActive = 1; |
1348 | HeightControlActive = 1; |
1349 | } |
1349 | } |
1350 | // get current height |
1350 | // get current height |
1351 | h = ReadingHeight; |
1351 | h = ReadingHeight; |
1352 | // if current height is above the setpoint reduce gas |
1352 | // if current height is above the setpoint reduce gas |
1353 | if((h > SetPointHeight) && HeightControlActive) |
1353 | if((h > SetPointHeight) && HeightControlActive) |
1354 | { |
1354 | { |
1355 | // GasMixFraction - HightDeviation * P - HeightChange * D - ACCTop * DACC |
1355 | // GasMixFraction - HightDeviation * P - HeightChange * D - ACCTop * DACC |
1356 | // height difference -> P control part |
1356 | // height difference -> P control part |
1357 | h = ((h - SetPointHeight) * (int16_t) FCParam.Height_P) / (16 / STICK_GAIN); |
1357 | h = ((h - SetPointHeight) * (int16_t) FCParam.Height_P) / (16 / STICK_GAIN); |
1358 | h = GasMixFraction - h; // reduce gas |
1358 | h = GasMixFraction - h; // reduce gas |
1359 | // height gradient --> D control part |
1359 | // height gradient --> D control part |
1360 | //h -= (HeightD * FCParam.Height_D) / (8 / STICK_GAIN); // D control part |
1360 | //h -= (HeightD * FCParam.Height_D) / (8 / STICK_GAIN); // D control part |
1361 | h -= (HeightD) / (8 / STICK_GAIN); // D control part |
1361 | h -= (HeightD) / (8 / STICK_GAIN); // D control part |
1362 | // acceleration sensor effect |
1362 | // acceleration sensor effect |
1363 | tmp_int = ((Reading_Integral_Top / 128) * (int32_t) FCParam.Height_ACC_Effect) / (128 / STICK_GAIN); |
1363 | tmp_int = ((Reading_Integral_Top / 128) * (int32_t) FCParam.Height_ACC_Effect) / (128 / STICK_GAIN); |
1364 | if(tmp_int > 70 * STICK_GAIN) tmp_int = 70 * STICK_GAIN; |
1364 | if(tmp_int > 70 * STICK_GAIN) tmp_int = 70 * STICK_GAIN; |
1365 | else if(tmp_int < -(70 * STICK_GAIN)) tmp_int = -(70 * STICK_GAIN); |
1365 | else if(tmp_int < -(70 * STICK_GAIN)) tmp_int = -(70 * STICK_GAIN); |
1366 | h -= tmp_int; |
1366 | h -= tmp_int; |
1367 | // update height control gas |
1367 | // update height control gas |
1368 | HeightControlGas = (HeightControlGas*15 + h) / 16; |
1368 | HeightControlGas = (HeightControlGas*15 + h) / 16; |
1369 | // limit gas reduction |
1369 | // limit gas reduction |
1370 | if(HeightControlGas < ParamSet.Height_MinGas * STICK_GAIN) |
1370 | if(HeightControlGas < ParamSet.Height_MinGas * STICK_GAIN) |
1371 | { |
1371 | { |
1372 | if(GasMixFraction >= ParamSet.Height_MinGas * STICK_GAIN) HeightControlGas = ParamSet.Height_MinGas * STICK_GAIN; |
1372 | if(GasMixFraction >= ParamSet.Height_MinGas * STICK_GAIN) HeightControlGas = ParamSet.Height_MinGas * STICK_GAIN; |
1373 | // allows landing also if gas stick is reduced below min gas on height control |
1373 | // allows landing also if gas stick is reduced below min gas on height control |
1374 | if(GasMixFraction < ParamSet.Height_MinGas * STICK_GAIN) HeightControlGas = GasMixFraction; |
1374 | if(GasMixFraction < ParamSet.Height_MinGas * STICK_GAIN) HeightControlGas = GasMixFraction; |
1375 | } |
1375 | } |
1376 | // limit gas to stick setting |
1376 | // limit gas to stick setting |
1377 | if(HeightControlGas > GasMixFraction) HeightControlGas = GasMixFraction; |
1377 | if(HeightControlGas > GasMixFraction) HeightControlGas = GasMixFraction; |
1378 | GasMixFraction = HeightControlGas; |
1378 | GasMixFraction = HeightControlGas; |
1379 | } |
1379 | } |
1380 | } |
1380 | } |
1381 | // limit gas to parameter setting |
1381 | // limit gas to parameter setting |
1382 | if(GasMixFraction > (ParamSet.Gas_Max - 20) * STICK_GAIN) GasMixFraction = (ParamSet.Gas_Max - 20) * STICK_GAIN; |
1382 | if(GasMixFraction > (ParamSet.Gas_Max - 20) * STICK_GAIN) GasMixFraction = (ParamSet.Gas_Max - 20) * STICK_GAIN; |
1383 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1383 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1384 | // + Mixer and PI-Controller |
1384 | // + Mixer and PI-Controller |
1385 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1385 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1386 | DebugOut.Analog[7] = GasMixFraction; |
1386 | DebugOut.Analog[7] = GasMixFraction; |
1387 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1387 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1388 | // Yaw-Fraction |
1388 | // Yaw-Fraction |
1389 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1389 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1390 | YawMixFraction = Reading_GyroYaw - SetPointYaw * STICK_GAIN; // yaw controller |
1390 | YawMixFraction = Reading_GyroYaw - SetPointYaw * STICK_GAIN; // yaw controller |
1391 | #define MIN_YAWGAS (40 * STICK_GAIN) // yaw also below this gas value |
1391 | #define MIN_YAWGAS (40 * STICK_GAIN) // yaw also below this gas value |
1392 | // limit YawMixFraction |
1392 | // limit YawMixFraction |
1393 | if(GasMixFraction > MIN_YAWGAS) |
1393 | if(GasMixFraction > MIN_YAWGAS) |
1394 | { |
1394 | { |
1395 | if(YawMixFraction > (GasMixFraction / 2)) YawMixFraction = GasMixFraction / 2; |
1395 | if(YawMixFraction > (GasMixFraction / 2)) YawMixFraction = GasMixFraction / 2; |
1396 | if(YawMixFraction < -(GasMixFraction / 2)) YawMixFraction = -(GasMixFraction / 2); |
1396 | if(YawMixFraction < -(GasMixFraction / 2)) YawMixFraction = -(GasMixFraction / 2); |
1397 | } |
1397 | } |
1398 | else |
1398 | else |
1399 | { |
1399 | { |
1400 | if(YawMixFraction > (MIN_YAWGAS / 2)) YawMixFraction = MIN_YAWGAS / 2; |
1400 | if(YawMixFraction > (MIN_YAWGAS / 2)) YawMixFraction = MIN_YAWGAS / 2; |
1401 | if(YawMixFraction < -(MIN_YAWGAS / 2)) YawMixFraction = -(MIN_YAWGAS / 2); |
1401 | if(YawMixFraction < -(MIN_YAWGAS / 2)) YawMixFraction = -(MIN_YAWGAS / 2); |
1402 | } |
1402 | } |
1403 | tmp_int = ParamSet.Gas_Max * STICK_GAIN; |
1403 | tmp_int = ParamSet.Gas_Max * STICK_GAIN; |
1404 | if(YawMixFraction > ((tmp_int - GasMixFraction))) YawMixFraction = ((tmp_int - GasMixFraction)); |
1404 | if(YawMixFraction > ((tmp_int - GasMixFraction))) YawMixFraction = ((tmp_int - GasMixFraction)); |
1405 | if(YawMixFraction < -((tmp_int - GasMixFraction))) YawMixFraction = -((tmp_int - GasMixFraction)); |
1405 | if(YawMixFraction < -((tmp_int - GasMixFraction))) YawMixFraction = -((tmp_int - GasMixFraction)); |
1406 | 1406 | ||
1407 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1407 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1408 | // Nick-Axis |
1408 | // Nick-Axis |
1409 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1409 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1410 | DiffNick = Reading_GyroNick - StickNick; // get difference |
1410 | DiffNick = Reading_GyroNick - StickNick; // get difference |
1411 | if(Gyro_I_Factor) SumNick += IntegralNick * Gyro_I_Factor - StickNick; // I-part for attitude control |
1411 | if(Gyro_I_Factor) SumNick += IntegralNick * Gyro_I_Factor - StickNick; // I-part for attitude control |
1412 | else SumNick += DiffNick; // I-part for head holding |
1412 | else SumNick += DiffNick; // I-part for head holding |
1413 | if(SumNick > (STICK_GAIN * 16000L)) SumNick = (STICK_GAIN * 16000L); |
1413 | if(SumNick > (STICK_GAIN * 16000L)) SumNick = (STICK_GAIN * 16000L); |
1414 | if(SumNick < -(STICK_GAIN * 16000L)) SumNick = -(STICK_GAIN * 16000L); |
1414 | if(SumNick < -(STICK_GAIN * 16000L)) SumNick = -(STICK_GAIN * 16000L); |
1415 | pd_result = DiffNick + Ki * SumNick; // PI-controller for nick |
1415 | pd_result = DiffNick + Ki * SumNick; // PI-controller for nick |
1416 | 1416 | ||
1417 | tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(GasMixFraction + abs(YawMixFraction)/2)) / 64; |
1417 | tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(GasMixFraction + abs(YawMixFraction)/2)) / 64; |
1418 | if(pd_result > tmp_int) pd_result = tmp_int; |
1418 | if(pd_result > tmp_int) pd_result = tmp_int; |
1419 | if(pd_result < -tmp_int) pd_result = -tmp_int; |
1419 | if(pd_result < -tmp_int) pd_result = -tmp_int; |
1420 | 1420 | ||
1421 | NickMixFraction = pd_result; |
1421 | NickMixFraction = pd_result; |
1422 | 1422 | ||
1423 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1423 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1424 | // Roll-Axis |
1424 | // Roll-Axis |
1425 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1425 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1426 | DiffRoll = Reading_GyroRoll - StickRoll; // get difference |
1426 | DiffRoll = Reading_GyroRoll - StickRoll; // get difference |
1427 | if(Gyro_I_Factor) SumRoll += IntegralRoll * Gyro_I_Factor - StickRoll; // I-part for attitude control |
1427 | if(Gyro_I_Factor) SumRoll += IntegralRoll * Gyro_I_Factor - StickRoll; // I-part for attitude control |
1428 | else SumRoll += DiffRoll; // I-part for head holding |
1428 | else SumRoll += DiffRoll; // I-part for head holding |
1429 | if(SumRoll > (STICK_GAIN * 16000L)) SumRoll = (STICK_GAIN * 16000L); |
1429 | if(SumRoll > (STICK_GAIN * 16000L)) SumRoll = (STICK_GAIN * 16000L); |
1430 | if(SumRoll < -(STICK_GAIN * 16000L)) SumRoll = -(STICK_GAIN * 16000L); |
1430 | if(SumRoll < -(STICK_GAIN * 16000L)) SumRoll = -(STICK_GAIN * 16000L); |
1431 | pd_result = DiffRoll + Ki * SumRoll; // PI-controller for roll |
1431 | pd_result = DiffRoll + Ki * SumRoll; // PI-controller for roll |
1432 | tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(GasMixFraction + abs(YawMixFraction)/2)) / 64; |
1432 | tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(GasMixFraction + abs(YawMixFraction)/2)) / 64; |
1433 | if(pd_result > tmp_int) pd_result = tmp_int; |
1433 | if(pd_result > tmp_int) pd_result = tmp_int; |
1434 | if(pd_result < -tmp_int) pd_result = -tmp_int; |
1434 | if(pd_result < -tmp_int) pd_result = -tmp_int; |
1435 | 1435 | ||
1436 | RollMixFraction = pd_result; |
1436 | RollMixFraction = pd_result; |
1437 | 1437 | ||
1438 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1438 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1439 | // Calculate Motor Mixes |
1439 | // Calculate Motor Mixes |
1440 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1440 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1441 | 1441 | ||
1442 | #ifdef HEXAKOPTER |
1442 | #ifdef HEXAKOPTER |
- | 1443 | ||
- | 1444 | NickMixFraction = (NickMixFraction*4) / 7; |
|
- | 1445 | RollMixFraction /= 3; |
|
- | 1446 | ||
1443 | // Motor FrontLeft |
1447 | // Motor FrontLeft |
1444 | MotorValue = GasMixFraction |
1448 | MotorValue = GasMixFraction |
1445 | + NickMixFraction |
1449 | + NickMixFraction |
1446 | + RollMixFraction/2 |
1450 | + RollMixFraction/2 |
1447 | - YawMixFraction; // Mixer |
1451 | - YawMixFraction; // Mixer |
1448 | MotorValue /= STICK_GAIN; |
1452 | MotorValue /= STICK_GAIN; |
1449 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1453 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1450 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1454 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1451 | Motor_FrontLeft = MotorValue; |
1455 | Motor_FrontLeft = MotorValue; |
1452 | 1456 | ||
1453 | // Motor FrontRight |
1457 | // Motor FrontRight |
1454 | MotorValue = GasMixFraction |
1458 | MotorValue = GasMixFraction |
1455 | + NickMixFraction |
1459 | + NickMixFraction |
1456 | - RollMixFraction/2 |
1460 | - RollMixFraction/2 |
1457 | + YawMixFraction; // Mixer |
1461 | + YawMixFraction; // Mixer |
1458 | MotorValue /= STICK_GAIN; |
1462 | MotorValue /= STICK_GAIN; |
1459 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1463 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1460 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1464 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1461 | Motor_FrontRight = MotorValue; |
1465 | Motor_FrontRight = MotorValue; |
1462 | 1466 | ||
1463 | // Motor RearLeft |
1467 | // Motor RearLeft |
1464 | MotorValue = GasMixFraction |
1468 | MotorValue = GasMixFraction |
1465 | - NickMixFraction |
1469 | - NickMixFraction |
1466 | + RollMixFraction/2 |
1470 | + RollMixFraction/2 |
1467 | - YawMixFraction; // Mixer |
1471 | - YawMixFraction; // Mixer |
1468 | MotorValue /= STICK_GAIN; |
1472 | MotorValue /= STICK_GAIN; |
1469 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1473 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1470 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1474 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1471 | Motor_RearLeft = MotorValue; |
1475 | Motor_RearLeft = MotorValue; |
1472 | 1476 | ||
1473 | // Motor RearRight |
1477 | // Motor RearRight |
1474 | MotorValue = GasMixFraction |
1478 | MotorValue = GasMixFraction |
1475 | - NickMixFraction |
1479 | - NickMixFraction |
1476 | - RollMixFraction/2 |
1480 | - RollMixFraction/2 |
1477 | + YawMixFraction; // Mixer |
1481 | + YawMixFraction; // Mixer |
1478 | MotorValue /= STICK_GAIN; |
1482 | MotorValue /= STICK_GAIN; |
1479 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1483 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1480 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1484 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1481 | Motor_RearRight= MotorValue; |
1485 | Motor_RearRight= MotorValue; |
1482 | 1486 | ||
1483 | // Motor Left |
1487 | // Motor Left |
1484 | MotorValue = GasMixFraction |
1488 | MotorValue = GasMixFraction |
1485 | + RollMixFraction |
1489 | + RollMixFraction |
1486 | + YawMixFraction; // Mixer |
1490 | + YawMixFraction; // Mixer |
1487 | MotorValue /= STICK_GAIN; |
1491 | MotorValue /= STICK_GAIN; |
1488 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1492 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1489 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1493 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1490 | Motor_Left = MotorValue; |
1494 | Motor_Left = MotorValue; |
1491 | 1495 | ||
1492 | // Motor Right |
1496 | // Motor Right |
1493 | MotorValue = GasMixFraction |
1497 | MotorValue = GasMixFraction |
1494 | - RollMixFraction |
1498 | - RollMixFraction |
1495 | - YawMixFraction; // Mixer |
1499 | - YawMixFraction; // Mixer |
1496 | MotorValue /= STICK_GAIN; |
1500 | MotorValue /= STICK_GAIN; |
1497 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1501 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1498 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1502 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1499 | Motor_Right = MotorValue; |
1503 | Motor_Right = MotorValue; |
1500 | 1504 | ||
1501 | #else |
1505 | #else |
1502 | 1506 | ||
1503 | // Motor Front |
1507 | // Motor Front |
1504 | MotorValue = GasMixFraction + NickMixFraction + YawMixFraction; // Mixer |
1508 | MotorValue = GasMixFraction + NickMixFraction + YawMixFraction; // Mixer |
1505 | MotorValue /= STICK_GAIN; |
1509 | MotorValue /= STICK_GAIN; |
1506 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1510 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1507 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1511 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1508 | Motor_Front = MotorValue; |
1512 | Motor_Front = MotorValue; |
1509 | 1513 | ||
1510 | // Motor Rear |
1514 | // Motor Rear |
1511 | MotorValue = GasMixFraction - NickMixFraction + YawMixFraction; // Mixer |
1515 | MotorValue = GasMixFraction - NickMixFraction + YawMixFraction; // Mixer |
1512 | MotorValue /= STICK_GAIN; |
1516 | MotorValue /= STICK_GAIN; |
1513 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1517 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1514 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1518 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1515 | Motor_Rear = MotorValue; |
1519 | Motor_Rear = MotorValue; |
1516 | 1520 | ||
1517 | 1521 | ||
1518 | // Motor Left |
1522 | // Motor Left |
1519 | MotorValue = GasMixFraction + RollMixFraction - YawMixFraction; // Mixer |
1523 | MotorValue = GasMixFraction + RollMixFraction - YawMixFraction; // Mixer |
1520 | MotorValue /= STICK_GAIN; |
1524 | MotorValue /= STICK_GAIN; |
1521 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1525 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1522 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1526 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1523 | Motor_Left = MotorValue; |
1527 | Motor_Left = MotorValue; |
1524 | 1528 | ||
1525 | 1529 | ||
1526 | // Motor Right |
1530 | // Motor Right |
1527 | MotorValue = GasMixFraction - RollMixFraction - YawMixFraction; // Mixer |
1531 | MotorValue = GasMixFraction - RollMixFraction - YawMixFraction; // Mixer |
1528 | MotorValue /= STICK_GAIN; |
1532 | MotorValue /= STICK_GAIN; |
1529 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1533 | if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1530 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1534 | else if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1531 | Motor_Right = MotorValue; |
1535 | Motor_Right = MotorValue; |
1532 | #endif |
1536 | #endif |
1533 | } |
1537 | } |
1534 | 1538 | ||
1535 | 1539 |