Rev 1612 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1612 | Rev 1615 | ||
---|---|---|---|
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
2 | // + Copyright (c) 04.2007 Holger Buss |
3 | // + Nur für den privaten Gebrauch |
3 | // + Nur f�r den privaten Gebrauch |
4 | // + www.MikroKopter.com |
4 | // + www.MikroKopter.com |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
6 | // + Es gilt f�r das gesamte Projekt (Hardware, Software, Bin�rfiles, Sourcecode und Dokumentation), |
7 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
7 | // + dass eine Nutzung (auch auszugsweise) nur f�r den privaten (nicht-kommerziellen) Gebrauch zul�ssig ist. |
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Best�ckung und Verkauf von Platinen oder Baus�tzen, |
11 | // + Verkauf von Luftbildaufnahmen, usw. |
11 | // + Verkauf von Luftbildaufnahmen, usw. |
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder ver�ffentlicht, |
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright m�ssen dann beiliegen |
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
17 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
17 | // + auf anderen Webseiten oder sonstigen Medien ver�ffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
18 | // + eindeutig als Ursprung verlinkt werden |
18 | // + eindeutig als Ursprung verlinkt werden |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
20 | // + Keine Gew�hr auf Fehlerfreiheit, Vollst�ndigkeit oder Funktion |
21 | // + Benutzung auf eigene Gefahr |
21 | // + Benutzung auf eigene Gefahr |
22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
22 | // + Wir �bernehmen keinerlei Haftung f�r direkte oder indirekte Personen- oder Sachsch�den |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
25 | // + mit unserer Zustimmung zulässig |
25 | // + mit unserer Zustimmung zul�ssig |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
30 | // + this list of conditions and the following disclaimer. |
30 | // + this list of conditions and the following disclaimer. |
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
32 | // + from this software without specific prior written permission. |
32 | // + from this software without specific prior written permission. |
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
34 | // + for non-commercial use (directly or indirectly) |
34 | // + for non-commercial use (directly or indirectly) |
35 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
35 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
36 | // + with our written permission |
36 | // + with our written permission |
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
38 | // + clearly linked as origin |
38 | // + clearly linked as origin |
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
49 | // + POSSIBILITY OF SUCH DAMAGE. |
49 | // + POSSIBILITY OF SUCH DAMAGE. |
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
51 | 51 | ||
52 | #include <stdlib.h> |
52 | #include <stdlib.h> |
53 | #include <avr/io.h> |
53 | #include <avr/io.h> |
54 | #include "eeprom.h" |
54 | #include "eeprom.h" |
55 | #include "flight.h" |
55 | #include "flight.h" |
56 | 56 | ||
57 | // Only for debug. Remove. |
57 | // Only for debug. Remove. |
58 | #include "analog.h" |
58 | #include "analog.h" |
59 | 59 | ||
60 | // Necessary for external control and motor test |
60 | // Necessary for external control and motor test |
61 | #include "uart0.h" |
61 | #include "uart0.h" |
62 | #include "twimaster.h" |
62 | #include "twimaster.h" |
63 | #include "attitude.h" |
63 | #include "attitude.h" |
64 | #include "controlMixer.h" |
64 | #include "controlMixer.h" |
65 | #ifdef USE_MK3MAG |
65 | #ifdef USE_MK3MAG |
66 | #include "gps.h" |
66 | #include "gps.h" |
67 | #endif |
67 | #endif |
68 | 68 | ||
69 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
69 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
70 | 70 | ||
71 | // TODO: These are no longer maintained, just left at 0. The original implementation just summed the acc. |
71 | // TODO: These are no longer maintained, just left at 0. The original implementation just summed the acc. |
72 | // value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey??? |
72 | // value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey??? |
73 | int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
73 | int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
74 | 74 | ||
75 | // MK flags |
75 | // MK flags |
76 | uint16_t isFlying = 0; |
76 | uint16_t isFlying = 0; |
77 | volatile uint8_t MKFlags = 0; |
77 | volatile uint8_t MKFlags = 0; |
78 | 78 | ||
79 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
79 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
80 | uint8_t yawRatePFactor, yawRateIFactor; // the PD factors for the yaw control |
80 | uint8_t yawRatePFactor, yawRateIFactor; // the PD factors for the yaw control |
81 | 81 | ||
82 | // Some integral weight constant... |
82 | // Some integral weight constant... |
83 | uint16_t Ki = 10300 / 33; |
83 | uint16_t Ki = 10300 / 33; |
84 | 84 | ||
85 | uint8_t RequiredMotors = 0; |
85 | uint8_t RequiredMotors = 0; |
86 | 86 | ||
87 | // No support for altitude control right |
87 | // No support for altitude control right |
88 | // int16_t SetPointHeight = 0; |
88 | // int16_t SetPointHeight = 0; |
89 | 89 | ||
90 | /************************************************************************/ |
90 | /************************************************************************/ |
91 | /* Filter for motor value smoothing (necessary???) */ |
91 | /* Filter for motor value smoothing (necessary???) */ |
92 | /************************************************************************/ |
92 | /************************************************************************/ |
93 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
93 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
94 | switch(dynamicParams.UserParams[5]) { |
94 | switch(dynamicParams.UserParams[5]) { |
95 | case 0: |
95 | case 0: |
96 | return newvalue; |
96 | return newvalue; |
97 | case 1: |
97 | case 1: |
98 | return (oldvalue + newvalue) / 2; |
98 | return (oldvalue + newvalue) / 2; |
99 | case 2: |
99 | case 2: |
100 | if(newvalue > oldvalue) |
100 | if(newvalue > oldvalue) |
101 | return (1 * (int16_t)oldvalue + newvalue) / 2; //mean of old and new |
101 | return (1 * (int16_t)oldvalue + newvalue) / 2; //mean of old and new |
102 | else |
102 | else |
103 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
103 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
104 | case 3: |
104 | case 3: |
105 | if(newvalue < oldvalue) |
105 | if(newvalue < oldvalue) |
106 | return (1 * (int16_t)oldvalue + newvalue) / 2; //mean of old and new |
106 | return (1 * (int16_t)oldvalue + newvalue) / 2; //mean of old and new |
107 | else |
107 | else |
108 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
108 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
109 | default: return newvalue; |
109 | default: return newvalue; |
110 | } |
110 | } |
111 | } |
111 | } |
112 | 112 | ||
113 | /************************************************************************/ |
113 | /************************************************************************/ |
114 | /* Neutral Readings */ |
114 | /* Neutral Readings */ |
115 | /************************************************************************/ |
115 | /************************************************************************/ |
116 | void flight_setNeutral() { |
116 | void flight_setNeutral() { |
117 | // GPSStickPitch = 0; |
117 | // GPSStickPitch = 0; |
118 | // GPSStickRoll = 0; |
118 | // GPSStickRoll = 0; |
119 | 119 | ||
120 | MKFlags |= MKFLAG_CALIBRATE; |
120 | MKFlags |= MKFLAG_CALIBRATE; |
121 | 121 | ||
122 | // not really used here any more. |
122 | // not really used here any more. |
123 | dynamicParams.KalmanK = -1; |
123 | dynamicParams.KalmanK = -1; |
124 | dynamicParams.KalmanMaxDrift = 0; |
124 | dynamicParams.KalmanMaxDrift = 0; |
125 | dynamicParams.KalmanMaxFusion = 32; |
125 | dynamicParams.KalmanMaxFusion = 32; |
126 | 126 | ||
127 | controlMixer_initVariables(); |
127 | controlMixer_initVariables(); |
128 | 128 | ||
129 | // TODO: Move off. |
129 | // TODO: Move off. |
130 | // RC_Quality = 100; |
130 | // RC_Quality = 100; |
131 | } |
131 | } |
132 | 132 | ||
133 | /************************************************************************/ |
133 | /************************************************************************/ |
134 | /* Transmit Motor Data via I2C */ |
134 | /* Transmit Motor Data via I2C */ |
135 | /************************************************************************/ |
135 | /************************************************************************/ |
136 | void sendMotorData(void) { |
136 | void sendMotorData(void) { |
137 | uint8_t i; |
137 | uint8_t i; |
138 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) { |
138 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) { |
139 | // If pilot has not started the engines.... |
139 | // If pilot has not started the engines.... |
140 | MKFlags &= ~(MKFLAG_FLY | MKFLAG_START); // clear flag FLY and START if motors are off |
140 | MKFlags &= ~(MKFLAG_FLY | MKFLAG_START); // clear flag FLY and START if motors are off |
141 | for(i = 0; i < MAX_MOTORS; i++) { |
141 | for(i = 0; i < MAX_MOTORS; i++) { |
142 | // and if we are not in motor test mode, cut throttle on all motors. |
142 | // and if we are not in motor test mode, cut throttle on all motors. |
143 | if(!motorTestActive) Motor[i].SetPoint = 0; |
143 | if(!motorTestActive) Motor[i].SetPoint = 0; |
144 | else Motor[i].SetPoint = motorTest[i]; |
144 | else Motor[i].SetPoint = motorTest[i]; |
145 | } |
145 | } |
146 | if(motorTestActive) motorTestActive--; |
146 | if(motorTestActive) motorTestActive--; |
147 | } |
147 | } |
148 | 148 | ||
149 | DebugOut.Analog[12] = Motor[0].SetPoint; // Front |
149 | DebugOut.Analog[12] = Motor[0].SetPoint; // Front |
150 | DebugOut.Analog[13] = Motor[1].SetPoint; // Rear |
150 | DebugOut.Analog[13] = Motor[1].SetPoint; // Rear |
151 | DebugOut.Analog[14] = Motor[3].SetPoint; // Left |
151 | DebugOut.Analog[14] = Motor[3].SetPoint; // Left |
152 | DebugOut.Analog[15] = Motor[2].SetPoint; // Right |
152 | DebugOut.Analog[15] = Motor[2].SetPoint; // Right |
153 | // Start I2C Interrupt Mode |
153 | // Start I2C Interrupt Mode |
154 | I2C_Start(TWI_STATE_MOTOR_TX); |
154 | I2C_Start(TWI_STATE_MOTOR_TX); |
155 | } |
155 | } |
156 | 156 | ||
157 | void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor, uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
157 | void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor, uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
158 | Ki = 10300 / _Ki; |
158 | Ki = 10300 / _Ki; |
159 | gyroPFactor = _gyroPFactor; |
159 | gyroPFactor = _gyroPFactor; |
160 | gyroIFactor = _gyroIFactor; |
160 | gyroIFactor = _gyroIFactor; |
161 | yawRatePFactor = _yawPFactor; |
161 | yawRatePFactor = _yawPFactor; |
162 | yawRateIFactor = _yawIFactor; |
162 | yawRateIFactor = _yawIFactor; |
163 | } |
163 | } |
164 | 164 | ||
165 | void setNormalFlightParameters(void) { |
165 | void setNormalFlightParameters(void) { |
166 | if(staticParams.GlobalConfig & CFG_HEADING_HOLD) gyroIFactor = 0; |
166 | if(staticParams.GlobalConfig & CFG_HEADING_HOLD) gyroIFactor = 0; |
167 | else gyroIFactor = dynamicParams.GyroI; |
167 | else gyroIFactor = dynamicParams.GyroI; |
168 | 168 | ||
169 | setFlightParameters(dynamicParams.IFactor + 1, |
169 | setFlightParameters(dynamicParams.IFactor + 1, |
170 | dynamicParams.GyroP + 10, |
170 | dynamicParams.GyroP + 10, |
171 | staticParams.GlobalConfig & CFG_HEADING_HOLD ? dynamicParams.GyroI : 0, |
171 | staticParams.GlobalConfig & CFG_HEADING_HOLD ? dynamicParams.GyroI : 0, |
172 | dynamicParams.GyroP + 10, |
172 | dynamicParams.GyroP + 10, |
173 | dynamicParams.UserParams[6] |
173 | dynamicParams.UserParams[6] |
174 | ); |
174 | ); |
175 | } |
175 | } |
176 | 176 | ||
177 | void setStableFlightParameters(void) { |
177 | void setStableFlightParameters(void) { |
178 | setFlightParameters(33, 90, 120, 90, 120); |
178 | setFlightParameters(33, 90, 120, 90, 120); |
179 | } |
179 | } |
180 | 180 | ||
181 | void handleCommands(uint8_t command, uint8_t isCommandRepeated) { |
181 | void handleCommands(uint8_t command, uint8_t isCommandRepeated) { |
182 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) { |
182 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) { |
183 | if (command == COMMAND_GYROCAL && !isCommandRepeated) { |
183 | if (command == COMMAND_GYROCAL && !isCommandRepeated) { |
184 | // Run gyro calibration but do not repeat it. |
184 | // Run gyro calibration but do not repeat it. |
185 | GRN_OFF; |
185 | GRN_OFF; |
186 | 186 | ||
187 | // TODO: out of here. Anyway, MKFLAG_MOTOR_RUN is cleared. Not enough? |
187 | // TODO: out of here. Anyway, MKFLAG_MOTOR_RUN is cleared. Not enough? |
188 | // isFlying = 0; |
188 | // isFlying = 0; |
189 | // check roll/pitch stick position |
189 | // check roll/pitch stick position |
190 | // if pitch stick is top or roll stick is left or right --> change parameter setting |
190 | // if pitch stick is top or roll stick is left or right --> change parameter setting |
191 | // according to roll/pitch stick position |
191 | // according to roll/pitch stick position |
192 | 192 | ||
193 | uint8_t setting = controlMixer_getArgument(); |
193 | uint8_t setting = controlMixer_getArgument(); |
194 | 194 | ||
195 | if ((setting > 0 && setting < 6) || setting == 9) { |
195 | if ((setting > 0 && setting < 6) || setting == 9) { |
196 | // Gyro calinbration, with or without selecting a new parameter-set. |
196 | // Gyro calinbration, with or without selecting a new parameter-set. |
197 | if(setting > 0 && setting < 6) { |
197 | if(setting > 0 && setting < 6) { |
198 | // A valid parameter-set (1..5) was chosen - use it. |
198 | // A valid parameter-set (1..5) was chosen - use it. |
199 | setActiveParamSet(setting); |
199 | setActiveParamSet(setting); |
200 | } |
200 | } |
201 | ParamSet_ReadFromEEProm(getActiveParamSet()); |
201 | ParamSet_ReadFromEEProm(getActiveParamSet()); |
202 | attitude_setNeutral(); |
202 | attitude_setNeutral(); |
203 | flight_setNeutral(); |
203 | flight_setNeutral(); |
204 | // Right stick is centered; calibrate it to zero (hmm strictly does not belong here). |
204 | // Right stick is centered; calibrate it to zero (hmm strictly does not belong here). |
205 | controlMixer_setNeutral(setting == 9); // Calibrate right stick neutral position. |
205 | controlMixer_setNeutral(setting == 9); // Calibrate right stick neutral position. |
206 | beepNumber(getActiveParamSet()); |
206 | beepNumber(getActiveParamSet()); |
207 | } else if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE) && setting == 7) { |
207 | } else if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE) && setting == 7) { |
208 | // If right stick is centered and down |
208 | // If right stick is centered and down |
209 | compassCalState = 1; |
209 | compassCalState = 1; |
210 | beep(1000); |
210 | beep(1000); |
211 | } |
211 | } |
212 | } |
212 | } |
213 | 213 | ||
214 | // save the ACC neutral setting to eeprom |
214 | // save the ACC neutral setting to eeprom |
215 | else { |
215 | else { |
216 | if(command == COMMAND_ACCCAL && !isCommandRepeated) { |
216 | if(command == COMMAND_ACCCAL && !isCommandRepeated) { |
217 | // Run gyro and acc. meter calibration but do not repeat it. |
217 | // Run gyro and acc. meter calibration but do not repeat it. |
218 | GRN_OFF; |
218 | GRN_OFF; |
219 | analog_calibrateAcc(); |
219 | analog_calibrateAcc(); |
220 | attitude_setNeutral(); |
220 | attitude_setNeutral(); |
221 | flight_setNeutral(); |
221 | flight_setNeutral(); |
222 | controlMixer_setNeutral(1); // Calibrate right stick neutral position. |
222 | controlMixer_setNeutral(1); // Calibrate right stick neutral position. |
223 | beepNumber(getActiveParamSet()); |
223 | beepNumber(getActiveParamSet()); |
224 | } |
224 | } |
225 | } |
225 | } |
226 | } // end !MOTOR_RUN condition. |
226 | } // end !MOTOR_RUN condition. |
227 | if (command == COMMAND_START) { |
227 | if (command == COMMAND_START) { |
228 | isFlying = 1; // TODO: Really???? |
228 | isFlying = 1; // TODO: Really???? |
229 | // if (!controlMixer_isCommandRepeated()) { |
229 | // if (!controlMixer_isCommandRepeated()) { |
230 | // attitude_startDynamicCalibration(); // Try sense the effect of the motors on sensors. |
230 | // attitude_startDynamicCalibration(); // Try sense the effect of the motors on sensors. |
231 | MKFlags |= (MKFLAG_MOTOR_RUN | MKFLAG_START); // set flag RUN and START. TODO: Is that START flag used at all??? |
231 | MKFlags |= (MKFLAG_MOTOR_RUN | MKFLAG_START); // set flag RUN and START. TODO: Is that START flag used at all??? |
232 | // } else { // Pilot is holding stick, ever after motor start. Continue to sense the effect of the motors on sensors. |
232 | // } else { // Pilot is holding stick, ever after motor start. Continue to sense the effect of the motors on sensors. |
233 | // attitude_continueDynamicCalibration(); |
233 | // attitude_continueDynamicCalibration(); |
234 | // setPointYaw = 0; |
234 | // setPointYaw = 0; |
235 | // IPartPitch = 0; |
235 | // IPartPitch = 0; |
236 | // IPartRoll = 0; |
236 | // IPartRoll = 0; |
237 | // } |
237 | // } |
238 | } else if (command == COMMAND_STOP) { |
238 | } else if (command == COMMAND_STOP) { |
239 | isFlying = 0; |
239 | isFlying = 0; |
240 | MKFlags &= ~(MKFLAG_MOTOR_RUN); |
240 | MKFlags &= ~(MKFLAG_MOTOR_RUN); |
241 | } |
241 | } |
242 | } |
242 | } |
243 | 243 | ||
244 | /************************************************************************/ |
244 | /************************************************************************/ |
245 | /* Main Flight Control */ |
245 | /* Main Flight Control */ |
246 | /************************************************************************/ |
246 | /************************************************************************/ |
247 | void flight_control(void) { |
247 | void flight_control(void) { |
248 | int16_t tmp_int; |
248 | int16_t tmp_int; |
249 | // Mixer Fractions that are combined for Motor Control |
249 | // Mixer Fractions that are combined for Motor Control |
250 | int16_t yawTerm, throttleTerm, pitchTerm, rollTerm; |
250 | int16_t yawTerm, throttleTerm, pitchTerm, rollTerm; |
251 | 251 | ||
252 | // PID controller variables |
252 | // PID controller variables |
253 | int16_t PDPartPitch, PDPartRoll, PDPartYaw, PPartPitch, PPartRoll; |
253 | int16_t PDPartPitch, PDPartRoll, PDPartYaw, PPartPitch, PPartRoll; |
254 | static int32_t IPartPitch = 0, IPartRoll = 0; |
254 | static int32_t IPartPitch = 0, IPartRoll = 0; |
255 | 255 | ||
256 | static int32_t setPointYaw = 0; |
256 | static int32_t setPointYaw = 0; |
257 | 257 | ||
258 | // Removed. Too complicated, and apparently not necessary with MEMS gyros anyway. |
258 | // Removed. Too complicated, and apparently not necessary with MEMS gyros anyway. |
259 | // static int32_t IntegralGyroPitchError = 0, IntegralGyroRollError = 0; |
259 | // static int32_t IntegralGyroPitchError = 0, IntegralGyroRollError = 0; |
260 | // static int32_t CorrectionPitch, CorrectionRoll; |
260 | // static int32_t CorrectionPitch, CorrectionRoll; |
261 | 261 | ||
262 | static uint16_t emergencyFlightTime; |
262 | static uint16_t emergencyFlightTime; |
263 | 263 | ||
264 | // No support for altitude control right now. |
264 | // No support for altitude control right now. |
265 | // static uint8_t HeightControlActive = 0; |
265 | // static uint8_t HeightControlActive = 0; |
266 | // static int16_t HeightControlGas = 0; |
266 | // static int16_t HeightControlGas = 0; |
267 | 267 | ||
268 | static int8_t debugDataTimer = 0; |
268 | static int8_t debugDataTimer = 0; |
269 | 269 | ||
270 | // High resolution motor values for smoothing of PID motor outputs |
270 | // High resolution motor values for smoothing of PID motor outputs |
271 | static int16_t motorFilters[MAX_MOTORS]; |
271 | static int16_t motorFilters[MAX_MOTORS]; |
272 | 272 | ||
273 | uint8_t i; |
273 | uint8_t i; |
274 | 274 | ||
275 | // Fire the main flight attitude calculation, including integration of angles. |
275 | // Fire the main flight attitude calculation, including integration of angles. |
276 | calculateFlightAttitude(); |
276 | calculateFlightAttitude(); |
277 | GRN_ON; |
277 | GRN_ON; |
278 | 278 | ||
279 | /* |
279 | /* |
280 | * TODO: update should: Set the stick variables if good signal, set them to zero if bad. |
280 | * TODO: update should: Set the stick variables if good signal, set them to zero if bad. |
281 | * Set variables also. |
281 | * Set variables also. |
282 | */ |
282 | */ |
283 | controlMixer_update(); |
283 | controlMixer_update(); |
284 | 284 | ||
285 | throttleTerm = controlThrottle; |
285 | throttleTerm = controlThrottle; |
286 | if(throttleTerm < staticParams.GasMin + 10) throttleTerm = staticParams.GasMin + 10; |
286 | if(throttleTerm < staticParams.MinThrottle + 10) throttleTerm = staticParams.MinThrottle + 10; |
287 | 287 | ||
288 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
288 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
289 | // RC-signal is bad |
289 | // RC-signal is bad |
290 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
290 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
291 | if(controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy |
291 | if(controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy |
292 | RED_ON; |
292 | RED_ON; |
293 | beepRCAlarm(); |
293 | beepRCAlarm(); |
294 | 294 | ||
295 | if(emergencyFlightTime) { |
295 | if(emergencyFlightTime) { |
296 | // continue emergency flight |
296 | // continue emergency flight |
297 | emergencyFlightTime--; |
297 | emergencyFlightTime--; |
298 | if(isFlying > 1000) { |
298 | if(isFlying > 1000) { |
299 | // We're probably still flying. Descend slowly. |
299 | // We're probably still flying. Descend slowly. |
300 | throttleTerm = staticParams.EmergencyGas; // Set emergency throttle |
300 | throttleTerm = staticParams.EmergencyGas; // Set emergency throttle |
301 | MKFlags |= (MKFLAG_EMERGENCY_LANDING); // Set flag for emergency landing |
301 | MKFlags |= (MKFLAG_EMERGENCY_LANDING); // Set flag for emergency landing |
302 | setStableFlightParameters(); |
302 | setStableFlightParameters(); |
303 | } else { |
303 | } else { |
304 | MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors. |
304 | MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors. |
305 | } |
305 | } |
306 | } else { |
306 | } else { |
307 | // end emergency flight (just cut the motors???) |
307 | // end emergency flight (just cut the motors???) |
308 | MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_LANDING); |
308 | MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_LANDING); |
309 | } |
309 | } |
310 | } else { |
310 | } else { |
311 | // signal is acceptable |
311 | // signal is acceptable |
312 | if(controlMixer_getSignalQuality() > SIGNAL_BAD) { |
312 | if(controlMixer_getSignalQuality() > SIGNAL_BAD) { |
313 | // Reset emergency landing control variables. |
313 | // Reset emergency landing control variables. |
314 | MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing |
314 | MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing |
315 | // The time is in whole seconds. |
315 | // The time is in whole seconds. |
316 | emergencyFlightTime = staticParams.EmergencyGasDuration * 488; |
316 | emergencyFlightTime = staticParams.EmergencyGasDuration * 488; |
317 | } |
317 | } |
318 | 318 | ||
319 | // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying. |
319 | // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying. |
320 | if(throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
320 | if(throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
321 | // increment flight-time counter until overflow. |
321 | // increment flight-time counter until overflow. |
322 | if(isFlying != 0xFFFF) isFlying++; |
322 | if(isFlying != 0xFFFF) isFlying++; |
323 | } else |
323 | } else |
324 | /* |
324 | /* |
325 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
325 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
326 | * Probably to avoid integration effects that will cause the copter to spin |
326 | * Probably to avoid integration effects that will cause the copter to spin |
327 | * or flip when taking off. |
327 | * or flip when taking off. |
328 | * TODO: What was the value of IPartPitch? At 1st run of this, it's 0 already. |
328 | * TODO: What was the value of IPartPitch? At 1st run of this, it's 0 already. |
329 | */ |
329 | */ |
330 | if(isFlying < 256) { |
330 | if(isFlying < 256) { |
331 | IPartPitch = 0; |
331 | IPartPitch = 0; |
332 | IPartRoll = 0; |
332 | IPartRoll = 0; |
333 | // TODO: Don't stomp on other modules' variables!!! |
333 | // TODO: Don't stomp on other modules' variables!!! |
334 | controlYaw = 0; |
334 | controlYaw = 0; |
335 | if(isFlying == 250) { |
335 | if(isFlying == 250) { |
336 | updateCompassCourse = 1; |
336 | updateCompassCourse = 1; |
337 | yawAngle = 0; |
337 | yawAngle = 0; |
338 | setPointYaw = 0; |
338 | setPointYaw = 0; |
339 | } |
339 | } |
340 | } else { |
340 | } else { |
341 | // DebugOut.Digital[1] = 0; |
341 | // DebugOut.Digital[1] = 0; |
342 | // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag? |
342 | // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag? |
343 | // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe. |
343 | // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe. |
344 | MKFlags |= (MKFLAG_FLY); |
344 | MKFlags |= (MKFLAG_FLY); |
345 | } |
345 | } |
346 | 346 | ||
347 | /* |
347 | /* |
348 | * Get the current command (start/stop motors, calibrate), if any. |
348 | * Get the current command (start/stop motors, calibrate), if any. |
349 | */ |
349 | */ |
350 | handleCommands(controlMixer_getCommand(), controlMixer_isCommandRepeated()); |
350 | handleCommands(controlMixer_getCommand(), controlMixer_isCommandRepeated()); |
351 | 351 | ||
352 | // if(controlMixer_getSignalQuality() >= SIGNAL_GOOD) { |
352 | // if(controlMixer_getSignalQuality() >= SIGNAL_GOOD) { |
353 | setNormalFlightParameters(); |
353 | setNormalFlightParameters(); |
354 | // } |
354 | // } |
355 | } |
355 | } |
356 | 356 | ||
357 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
357 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
358 | // in case of emergency landing |
358 | // in case of emergency landing |
359 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
359 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | // set all inputs to save values |
360 | // set all inputs to save values |
361 | /* |
361 | /* |
362 | * Looping the H&I way basically is just a matter of turning off attitude angle measurement |
362 | * Looping the H&I way basically is just a matter of turning off attitude angle measurement |
363 | * by integration (because 300 deg/s gyros are too slow) and turning down the throttle. |
363 | * by integration (because 300 deg/s gyros are too slow) and turning down the throttle. |
364 | * This is the latter. |
364 | * This is the latter. |
365 | */ |
365 | */ |
366 | if(looping) { |
366 | if(looping) { |
367 | if(throttleTerm > staticParams.LoopGasLimit) throttleTerm = staticParams.LoopGasLimit; |
367 | if(throttleTerm > staticParams.LoopGasLimit) throttleTerm = staticParams.LoopGasLimit; |
368 | } |
368 | } |
369 | 369 | ||
370 | /* |
370 | /* |
371 | * Here is a dynamic calibration experiment: Adjust integrals and gyro offsets if the pilot appears to be always |
371 | * Here is a dynamic calibration experiment: Adjust integrals and gyro offsets if the pilot appears to be always |
372 | * pushing of pulling on the pitch or roll stick. |
372 | * pushing of pulling on the pitch or roll stick. |
373 | */ |
373 | */ |
374 | /* |
374 | /* |
375 | if(ADCycleCount >= dynamicParams.UserParam2 * 10) { |
375 | if(ADCycleCount >= dynamicParams.UserParam2 * 10) { |
376 | // This algo works OK on the desk but it is a little sluggish and it oscillates. |
376 | // This algo works OK on the desk but it is a little sluggish and it oscillates. |
377 | // It does not very effectively cancel drift because of dynamics. |
377 | // It does not very effectively cancel drift because of dynamics. |
378 | |
378 | |
379 | minStickForAutoCal = dynamicParams.UserParam3 * 10; |
379 | minStickForAutoCal = dynamicParams.UserParam3 * 10; |
380 | maxStickForAutoCal = dynamicParams.UserParam4 * 10; |
380 | maxStickForAutoCal = dynamicParams.UserParam4 * 10; |
381 | |
381 | |
382 | // If not already corrected to the limit, and dynamic calibration is enabled: |
382 | // If not already corrected to the limit, and dynamic calibration is enabled: |
383 | if (abs(dynamicOffsetPitch - savedDynamicOffsetPitch) < dynamicParams.UserParam1 && !dynamicParams.UserParam6) { |
383 | if (abs(dynamicOffsetPitch - savedDynamicOffsetPitch) < dynamicParams.UserParam1 && !dynamicParams.UserParam6) { |
384 | // The pilot pushes on the stick, the integral is > 0, and the gyro val is > 0. Looks like a value-too-high case, so increase the offset. |
384 | // The pilot pushes on the stick, the integral is > 0, and the gyro val is > 0. Looks like a value-too-high case, so increase the offset. |
385 | if (filteredHiResPitchGyro > dynamicOffsetPitch && pitchAngle > 0 && maxStickPitch >= minStickForAutoCal && maxStickPitch <= maxStickForAutoCal) { |
385 | if (filteredHiResPitchGyro > dynamicOffsetPitch && pitchAngle > 0 && maxStickPitch >= minStickForAutoCal && maxStickPitch <= maxStickForAutoCal) { |
386 | dynamicOffsetPitch += (int8_t)(dynamicParams.UserParam7 - 128); // (adding something seems right...) |
386 | dynamicOffsetPitch += (int8_t)(dynamicParams.UserParam7 - 128); // (adding something seems right...) |
387 | pitchAngle = (pitchAngle * (int32_t)dynamicParams.UserParam5) / 100L; |
387 | pitchAngle = (pitchAngle * (int32_t)dynamicParams.UserParam5) / 100L; |
388 | } else if (filteredHiResPitchGyro < dynamicOffsetPitch && pitchAngle < 0 && maxStickPitch <= -minStickForAutoCal && maxStickPitch >= -maxStickForAutoCal) { |
388 | } else if (filteredHiResPitchGyro < dynamicOffsetPitch && pitchAngle < 0 && maxStickPitch <= -minStickForAutoCal && maxStickPitch >= -maxStickForAutoCal) { |
389 | dynamicOffsetPitch -= (int8_t)(dynamicParams.UserParam7 - 128); // (subtracting something seems right...) |
389 | dynamicOffsetPitch -= (int8_t)(dynamicParams.UserParam7 - 128); // (subtracting something seems right...) |
390 | pitchAngle = (pitchAngle * (int32_t)dynamicParams.UserParam5) / 100L; |
390 | pitchAngle = (pitchAngle * (int32_t)dynamicParams.UserParam5) / 100L; |
391 | } |
391 | } |
392 | } |
392 | } |
393 | |
393 | |
394 | // If not already corrected to the limit, and dynamic calibration is enabled: |
394 | // If not already corrected to the limit, and dynamic calibration is enabled: |
395 | if (abs(dynamicOffsetRoll - savedDynamicOffsetRoll) <= dynamicParams.UserParam1 && !dynamicParams.UserParam6) { |
395 | if (abs(dynamicOffsetRoll - savedDynamicOffsetRoll) <= dynamicParams.UserParam1 && !dynamicParams.UserParam6) { |
396 | if (filteredHiResRollGyro > dynamicOffsetRoll && rollAngle > 0 && maxStickRoll >= minStickForAutoCal && maxStickRoll <= maxStickForAutoCal) { |
396 | if (filteredHiResRollGyro > dynamicOffsetRoll && rollAngle > 0 && maxStickRoll >= minStickForAutoCal && maxStickRoll <= maxStickForAutoCal) { |
397 | dynamicOffsetRoll += (int8_t)(dynamicParams.UserParam8 - 128); |
397 | dynamicOffsetRoll += (int8_t)(dynamicParams.UserParam8 - 128); |
398 | rollAngle = (rollAngle * (int32_t)dynamicParams.UserParam5) / 100L; |
398 | rollAngle = (rollAngle * (int32_t)dynamicParams.UserParam5) / 100L; |
399 | } else if (filteredHiResRollGyro < dynamicOffsetRoll && rollAngle < 0 && maxStickRoll <= -minStickForAutoCal && maxStickRoll >= -maxStickForAutoCal) { |
399 | } else if (filteredHiResRollGyro < dynamicOffsetRoll && rollAngle < 0 && maxStickRoll <= -minStickForAutoCal && maxStickRoll >= -maxStickForAutoCal) { |
400 | dynamicOffsetRoll -= (int8_t)(dynamicParams.UserParam8 - 128); |
400 | dynamicOffsetRoll -= (int8_t)(dynamicParams.UserParam8 - 128); |
401 | rollAngle = (rollAngle * (int32_t)dynamicParams.UserParam5) / 100L; |
401 | rollAngle = (rollAngle * (int32_t)dynamicParams.UserParam5) / 100L; |
402 | } |
402 | } |
403 | } |
403 | } |
404 | ADCycleCount = 0; |
404 | ADCycleCount = 0; |
405 | } |
405 | } |
406 | */ |
406 | */ |
407 | 407 | ||
408 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
408 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
409 | // Yawing |
409 | // Yawing |
410 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
410 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
411 | if(abs(controlYaw) > 4 * staticParams.StickYawP) { // yaw stick is activated |
411 | if(abs(controlYaw) > 4 * staticParams.StickYawP) { // yaw stick is activated |
412 | badCompassHeading = 1000; |
412 | badCompassHeading = 1000; |
413 | if(!(staticParams.GlobalConfig & CFG_COMPASS_FIX)) { |
413 | if(!(staticParams.GlobalConfig & CFG_COMPASS_FIX)) { |
414 | updateCompassCourse = 1; |
414 | updateCompassCourse = 1; |
415 | } |
415 | } |
416 | } |
416 | } |
417 | 417 | ||
418 | setPointYaw = controlYaw; |
418 | setPointYaw = controlYaw; |
419 | 419 | ||
420 | // trimm drift of ReadingIntegralGyroYaw with SetPointYaw(StickYaw) |
420 | // trimm drift of ReadingIntegralGyroYaw with SetPointYaw(StickYaw) |
421 | // TODO: We want NO feedback of control related stuff to the attitude related stuff. |
421 | // TODO: We want NO feedback of control related stuff to the attitude related stuff. |
422 | yawAngle -= tmp_int; |
422 | yawAngle -= tmp_int; |
423 | 423 | ||
424 | // limit the effect |
424 | // limit the effect |
425 | CHECK_MIN_MAX(yawAngle, -50000, 50000) |
425 | CHECK_MIN_MAX(yawAngle, -50000, 50000) |
426 | 426 | ||
427 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
427 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
428 | // Compass |
428 | // Compass |
429 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
429 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
430 | // compass code is used if Compass option is selected |
430 | // compass code is used if Compass option is selected |
431 | 431 | ||
432 | /* |
432 | /* |
433 | if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) { |
433 | if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) { |
434 | updateCompass(); |
434 | updateCompass(); |
435 | } |
435 | } |
436 | */ |
436 | */ |
437 | 437 | ||
438 | #if defined (USE_MK3MAG) |
438 | #if defined (USE_MK3MAG) |
439 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
439 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
440 | // GPS |
440 | // GPS |
441 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
441 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
442 | if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) { |
442 | if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) { |
443 | GPS_Main(); |
443 | GPS_Main(); |
444 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
444 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
445 | } |
445 | } |
446 | else { |
446 | else { |
447 | // GPSStickPitch = 0; |
447 | // GPSStickPitch = 0; |
448 | // GPSStickRoll = 0; |
448 | // GPSStickRoll = 0; |
449 | } |
449 | } |
450 | #endif |
450 | #endif |
451 | 451 | ||
452 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
452 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
453 | // calculate control feedback from angle (gyro integral) and agular velocity (gyro signal) |
453 | // calculate control feedback from angle (gyro integral) and agular velocity (gyro signal) |
454 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
454 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
455 | // The P-part is actually the I-part... |
455 | // The P-part is actually the I-part... |
456 | if(looping & LOOPING_PITCH_AXIS) { |
456 | if(looping & LOOPING_PITCH_AXIS) { |
457 | PPartPitch = 0; |
457 | PPartPitch = 0; |
458 | } else { |
458 | } else { |
459 | // TODO: Where do the 44000 come from??? |
459 | // TODO: Where do the 44000 come from??? |
460 | PPartPitch = (pitchAngle * gyroIFactor) / (44000 / STICK_GAIN); // P-Part - Proportional to Integral |
460 | PPartPitch = (pitchAngle * gyroIFactor) / (44000 / STICK_GAIN); // P-Part - Proportional to Integral |
461 | // PPartPitch = IntegralNickMalFaktor in H&I code. |
461 | // PPartPitch = IntegralNickMalFaktor in H&I code. |
462 | } |
462 | } |
463 | 463 | ||
464 | // Now blend in the P-part - proportional to the Differential of the integral = the gyro value. |
464 | // Now blend in the P-part - proportional to the Differential of the integral = the gyro value. |
465 | PDPartPitch = PPartPitch + (int32_t)((int32_t)pitchRate_PID * gyroPFactor) / (256L / STICK_GAIN) |
465 | PDPartPitch = PPartPitch + (int32_t)((int32_t)pitchRate_PID * gyroPFactor) / (256L / STICK_GAIN) |
466 | + (pitchDifferential * dynamicParams.GyroD) / 16; |
466 | + (pitchDifferential * dynamicParams.GyroD) / 16; |
467 | // = MesswertNick in H&I code |
467 | // = MesswertNick in H&I code |
468 | 468 | ||
469 | // The P-part is actually the I-part... |
469 | // The P-part is actually the I-part... |
470 | if(looping & LOOPING_ROLL_AXIS) { |
470 | if(looping & LOOPING_ROLL_AXIS) { |
471 | PPartRoll = 0; |
471 | PPartRoll = 0; |
472 | } else { |
472 | } else { |
473 | PPartRoll = (rollAngle * gyroIFactor) / (44000 / STICK_GAIN); // P-Part - Proportional to Integral |
473 | PPartRoll = (rollAngle * gyroIFactor) / (44000 / STICK_GAIN); // P-Part - Proportional to Integral |
474 | } |
474 | } |
475 | 475 | ||
476 | // Now blend in the P-part - proportional to the Differential of the integral = the gyro value. |
476 | // Now blend in the P-part - proportional to the Differential of the integral = the gyro value. |
477 | PDPartRoll = PPartRoll + (int32_t)((int32_t)rollRate_PID * gyroPFactor) / (256L / STICK_GAIN) |
477 | PDPartRoll = PPartRoll + (int32_t)((int32_t)rollRate_PID * gyroPFactor) / (256L / STICK_GAIN) |
478 | + (rollDifferential * dynamicParams.GyroD) / 16; |
478 | + (rollDifferential * dynamicParams.GyroD) / 16; |
479 | 479 | ||
480 | PDPartYaw = (int32_t)(yawRate * 2 * (int32_t)yawRatePFactor) / (256L / STICK_GAIN) + (int32_t)(yawAngle * yawRateIFactor) / (2 * (44000 / STICK_GAIN)); |
480 | PDPartYaw = (int32_t)(yawRate * 2 * (int32_t)yawRatePFactor) / (256L / STICK_GAIN) + (int32_t)(yawAngle * yawRateIFactor) / (2 * (44000 / STICK_GAIN)); |
481 | 481 | ||
482 | // limit control feedback |
482 | // limit control feedback |
483 | #define SENSOR_LIMIT (4096 * 4) |
483 | #define SENSOR_LIMIT (4096 * 4) |
484 | CHECK_MIN_MAX(PDPartPitch, -SENSOR_LIMIT, SENSOR_LIMIT); |
484 | CHECK_MIN_MAX(PDPartPitch, -SENSOR_LIMIT, SENSOR_LIMIT); |
485 | CHECK_MIN_MAX(PDPartRoll, -SENSOR_LIMIT, SENSOR_LIMIT); |
485 | CHECK_MIN_MAX(PDPartRoll, -SENSOR_LIMIT, SENSOR_LIMIT); |
486 | CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
486 | CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
487 | 487 | ||
488 | /* |
488 | /* |
489 | * Compose throttle term. |
489 | * Compose throttle term. |
490 | * If a Bl-Ctrl is missing, prevent takeoff. |
490 | * If a Bl-Ctrl is missing, prevent takeoff. |
491 | */ |
491 | */ |
492 | if(missingMotor) { |
492 | if(missingMotor) { |
493 | // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway??? |
493 | // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway??? |
494 | if((isFlying > 1) && (isFlying < 50) && (throttleTerm > 0)) |
494 | if((isFlying > 1) && (isFlying < 50) && (throttleTerm > 0)) |
495 | isFlying = 1; // keep within lift off condition |
495 | isFlying = 1; // keep within lift off condition |
496 | throttleTerm = staticParams.GasMin; // reduce gas to min to avoid lift of |
496 | throttleTerm = staticParams.MinThrottle; // reduce gas to min to avoid lift of |
497 | } |
497 | } |
498 | 498 | ||
499 | /* |
499 | /* |
500 | * Height control was here. |
500 | * Height control was here. |
501 | */ |
501 | */ |
502 | 502 | ||
503 | if(throttleTerm > staticParams.GasMax - 20) throttleTerm = (staticParams.GasMax - 20); |
503 | if(throttleTerm > staticParams.MaxThrottle - 20) throttleTerm = (staticParams.MaxThrottle - 20); |
504 | throttleTerm *= STICK_GAIN; |
504 | throttleTerm *= STICK_GAIN; |
505 | 505 | ||
506 | /* |
506 | /* |
507 | * Compose yaw term. |
507 | * Compose yaw term. |
508 | */ |
508 | */ |
509 | #define MIN_YAWGAS (40 * STICK_GAIN) // yaw also below this gas value |
509 | #define MIN_YAWGAS (40 * STICK_GAIN) // yaw also below this gas value |
510 | yawTerm = PDPartYaw - setPointYaw * STICK_GAIN; // yaw controller |
510 | yawTerm = PDPartYaw - setPointYaw * STICK_GAIN; // yaw controller |
511 | // limit yawTerm |
511 | // limit yawTerm |
512 | if(throttleTerm > MIN_YAWGAS) { |
512 | if(throttleTerm > MIN_YAWGAS) { |
513 | CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2)); |
513 | CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2)); |
514 | } else { |
514 | } else { |
515 | CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2)); |
515 | CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2)); |
516 | } |
516 | } |
517 | 517 | ||
518 | tmp_int = staticParams.GasMax * STICK_GAIN; |
518 | tmp_int = staticParams.MaxThrottle * STICK_GAIN; |
519 | CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm)); |
519 | CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm)); |
520 | 520 | ||
521 | /* |
521 | /* |
522 | * Compose pitch and roll terms. This is finally where the sticks come into play. |
522 | * Compose pitch and roll terms. This is finally where the sticks come into play. |
523 | */ |
523 | */ |
524 | if(gyroIFactor) { |
524 | if(gyroIFactor) { |
525 | // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos. |
525 | // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos. |
526 | // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time. |
526 | // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time. |
527 | // TODO: Find out why this seems to be proportional to stick position - not integrating it at all. |
527 | // TODO: Find out why this seems to be proportional to stick position - not integrating it at all. |
528 | IPartPitch += PPartPitch - controlPitch; // Integrate difference between P part (the angle) and the stick pos. |
528 | IPartPitch += PPartPitch - controlPitch; // Integrate difference between P part (the angle) and the stick pos. |
529 | IPartRoll += PPartRoll - controlRoll; // I-part for attitude control OK |
529 | IPartRoll += PPartRoll - controlRoll; // I-part for attitude control OK |
530 | } else { |
530 | } else { |
531 | // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos. |
531 | // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos. |
532 | IPartPitch += PDPartPitch - controlPitch; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
532 | IPartPitch += PDPartPitch - controlPitch; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
533 | IPartRoll += PDPartRoll - controlRoll; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
533 | IPartRoll += PDPartRoll - controlRoll; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
534 | } |
534 | } |
535 | 535 | ||
536 | // TODO: From which planet comes the 16000? |
536 | // TODO: From which planet comes the 16000? |
537 | CHECK_MIN_MAX(IPartPitch, -(STICK_GAIN * 16000L), (STICK_GAIN * 16000L)); |
537 | CHECK_MIN_MAX(IPartPitch, -(STICK_GAIN * 16000L), (STICK_GAIN * 16000L)); |
538 | 538 | ||
539 | // Add (P, D) parts minus stick pos. to the scaled-down I part. |
539 | // Add (P, D) parts minus stick pos. to the scaled-down I part. |
540 | pitchTerm = PDPartPitch - controlPitch + IPartPitch / Ki; // PID-controller for pitch |
540 | pitchTerm = PDPartPitch - controlPitch + IPartPitch / Ki; // PID-controller for pitch |
541 | 541 | ||
542 | CHECK_MIN_MAX(IPartRoll, -(STICK_GAIN * 16000L), (STICK_GAIN * 16000L)); |
542 | CHECK_MIN_MAX(IPartRoll, -(STICK_GAIN * 16000L), (STICK_GAIN * 16000L)); |
543 | rollTerm = PDPartRoll - controlRoll + IPartRoll / Ki; // PID-controller for roll |
543 | rollTerm = PDPartRoll - controlRoll + IPartRoll / Ki; // PID-controller for roll |
544 | 544 | ||
545 | /* |
545 | /* |
546 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
546 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
547 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
547 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
548 | * (max. pitch or roll term is the throttle value). |
548 | * (max. pitch or roll term is the throttle value). |
549 | * TODO: Why a growing function of yaw? |
549 | * TODO: Why a growing function of yaw? |
550 | */ |
550 | */ |
551 | tmp_int = (int32_t)((int32_t)dynamicParams.DynamicStability * (int32_t)(throttleTerm + abs(yawTerm) / 2)) / 64; |
551 | tmp_int = (int32_t)((int32_t)dynamicParams.DynamicStability * (int32_t)(throttleTerm + abs(yawTerm) / 2)) / 64; |
552 | CHECK_MIN_MAX(pitchTerm, -tmp_int, tmp_int); |
552 | CHECK_MIN_MAX(pitchTerm, -tmp_int, tmp_int); |
553 | CHECK_MIN_MAX(rollTerm, -tmp_int, tmp_int); |
553 | CHECK_MIN_MAX(rollTerm, -tmp_int, tmp_int); |
554 | 554 | ||
555 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
555 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
556 | // Universal Mixer |
556 | // Universal Mixer |
557 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
557 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
558 | for(i = 0; i < MAX_MOTORS; i++) { |
558 | for(i = 0; i < MAX_MOTORS; i++) { |
559 | int16_t tmp; |
559 | int16_t tmp; |
560 | if(Mixer.Motor[i][MIX_THROTTLE] > 0) { // If a motor has a zero throttle mix, it is not considered. |
560 | if(Mixer.Motor[i][MIX_THROTTLE] > 0) { // If a motor has a zero throttle mix, it is not considered. |
561 | tmp = ((int32_t)throttleTerm * Mixer.Motor[i][MIX_THROTTLE]) / 64L; |
561 | tmp = ((int32_t)throttleTerm * Mixer.Motor[i][MIX_THROTTLE]) / 64L; |
562 | tmp += ((int32_t)pitchTerm * Mixer.Motor[i][MIX_PITCH]) / 64L; |
562 | tmp += ((int32_t)pitchTerm * Mixer.Motor[i][MIX_PITCH]) / 64L; |
563 | tmp += ((int32_t)rollTerm * Mixer.Motor[i][MIX_ROLL]) / 64L; |
563 | tmp += ((int32_t)rollTerm * Mixer.Motor[i][MIX_ROLL]) / 64L; |
564 | tmp += ((int32_t)yawTerm * Mixer.Motor[i][MIX_YAW]) / 64L; |
564 | tmp += ((int32_t)yawTerm * Mixer.Motor[i][MIX_YAW]) / 64L; |
565 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
565 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
566 | tmp = motorFilters[i] / STICK_GAIN; |
566 | tmp = motorFilters[i] / STICK_GAIN; |
567 | CHECK_MIN_MAX(tmp, staticParams.GasMin, staticParams.GasMax); |
567 | CHECK_MIN_MAX(tmp, staticParams.MinThrottle, staticParams.MaxThrottle); |
568 | Motor[i].SetPoint = tmp; |
568 | Motor[i].SetPoint = tmp; |
569 | } |
569 | } |
570 | else Motor[i].SetPoint = 0; |
570 | else Motor[i].SetPoint = 0; |
571 | } |
571 | } |
572 | 572 | ||
573 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
573 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
574 | // Debugwerte zuordnen |
574 | // Debugwerte zuordnen |
575 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
575 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
576 | if(!debugDataTimer--) { |
576 | if(!debugDataTimer--) { |
577 | debugDataTimer = 24; // update debug outputs every 25*2ms = 50 ms (20Hz) |
577 | debugDataTimer = 24; // update debug outputs every 25*2ms = 50 ms (20Hz) |
578 | DebugOut.Analog[0] = (10 * pitchAngle) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
578 | DebugOut.Analog[0] = (10 * pitchAngle) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
579 | DebugOut.Analog[1] = (10 * rollAngle) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
579 | DebugOut.Analog[1] = (10 * rollAngle) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
580 | DebugOut.Analog[2] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; |
580 | DebugOut.Analog[2] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; |
581 | 581 | ||
582 | /* |
582 | /* |
583 | DebugOut.Analog[9] = UBat; |
583 | DebugOut.Analog[9] = UBat; |
584 | DebugOut.Analog[10] = RC_Quality; |
584 | DebugOut.Analog[10] = RC_Quality; |
585 | DebugOut.Analog[11] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; |
585 | DebugOut.Analog[11] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; |
586 | */ |
586 | */ |
587 | 587 | ||
588 | // 12..15 are the 4 first motors. |
588 | // 12..15 are the 4 first motors. |
589 | 589 | ||
590 | DebugOut.Analog[16] = pitchAxisAcc; |
590 | DebugOut.Analog[16] = pitchAxisAcc; |
591 | DebugOut.Analog[17] = rollAxisAcc; |
591 | DebugOut.Analog[17] = rollAxisAcc; |
592 | // DebugOut.Analog[18] = ZAxisAcc; |
592 | // DebugOut.Analog[18] = ZAxisAcc; |
593 | DebugOut.Analog[19] = throttleTerm; |
593 | DebugOut.Analog[19] = throttleTerm; |
594 | DebugOut.Analog[20] = pitchTerm; |
594 | DebugOut.Analog[20] = pitchTerm; |
595 | DebugOut.Analog[21] = rollTerm; |
595 | DebugOut.Analog[21] = rollTerm; |
596 | DebugOut.Analog[22] = yawTerm; |
596 | DebugOut.Analog[22] = yawTerm; |
597 | DebugOut.Analog[23] = PPartPitch; // |
597 | DebugOut.Analog[23] = PPartPitch; // |
598 | DebugOut.Analog[24] = IPartPitch /Ki; // meget meget lille. |
598 | DebugOut.Analog[24] = IPartPitch /Ki; // meget meget lille. |
599 | DebugOut.Analog[25] = PDPartPitch; // omtrent lig ppart. |
599 | DebugOut.Analog[25] = PDPartPitch; // omtrent lig ppart. |
600 | 600 | ||
601 | DebugOut.Analog[26] = pitchAccNoisePeak; |
601 | DebugOut.Analog[26] = pitchAccNoisePeak; |
602 | DebugOut.Analog[27] = rollAccNoisePeak; |
602 | DebugOut.Analog[27] = rollAccNoisePeak; |
603 | 603 | ||
604 | DebugOut.Analog[30] = pitchGyroNoisePeak; |
604 | DebugOut.Analog[30] = pitchGyroNoisePeak; |
605 | DebugOut.Analog[31] = rollGyroNoisePeak; |
605 | DebugOut.Analog[31] = rollGyroNoisePeak; |
606 | } |
606 | } |
607 | } |
607 | } |
608 | 608 |