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