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