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