Rev 1634 | Rev 1646 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1634 | Rev 1645 | ||
---|---|---|---|
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 und nicht-kommerziellen Gebrauch zulässig ist. |
7 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten und 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 Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
17 | // + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
18 | // + eindeutig als Ursprung verlinkt und genannt werden |
18 | // + eindeutig als Ursprung verlinkt und genannt 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 |
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
48 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
48 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
49 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
49 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
50 | // + POSSIBILITY OF SUCH DAMAGE. |
50 | // + POSSIBILITY OF SUCH DAMAGE. |
51 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
51 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
52 | 52 | ||
53 | #include <stdlib.h> |
53 | #include <stdlib.h> |
54 | #include <avr/io.h> |
54 | #include <avr/io.h> |
55 | #include "eeprom.h" |
55 | #include "eeprom.h" |
56 | #include "flight.h" |
56 | #include "flight.h" |
57 | 57 | ||
58 | // Only for debug. Remove. |
58 | // Only for debug. Remove. |
59 | #include "analog.h" |
59 | //#include "analog.h" |
60 | #include "rc.h" |
60 | //#include "rc.h" |
61 | 61 | ||
62 | // Necessary for external control and motor test |
62 | // Necessary for external control and motor test |
63 | #include "uart0.h" |
63 | #include "uart0.h" |
64 | #include "twimaster.h" |
64 | #include "twimaster.h" |
65 | #include "attitude.h" |
65 | #include "attitude.h" |
66 | #include "controlMixer.h" |
66 | #include "controlMixer.h" |
67 | #ifdef USE_MK3MAG |
67 | #ifdef USE_MK3MAG |
68 | #include "gps.h" |
68 | #include "gps.h" |
69 | #endif |
69 | #endif |
70 | 70 | ||
71 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
71 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
72 | 72 | ||
73 | /* |
73 | /* |
74 | * These are no longer maintained, just left at 0. The original implementation just summed the acc. |
74 | * These are no longer maintained, just left at 0. The original implementation just summed the acc. |
75 | * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey??? |
75 | * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey??? |
76 | */ |
76 | */ |
77 | int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
77 | // int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
78 | 78 | ||
79 | // MK flags |
79 | // MK flags |
80 | uint16_t isFlying = 0; |
80 | uint16_t isFlying = 0; |
81 | volatile uint8_t MKFlags = 0; |
81 | volatile uint8_t MKFlags = 0; |
82 | 82 | ||
83 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
83 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
84 | uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control |
84 | uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control |
85 | 85 | ||
86 | // Some integral weight constant... |
86 | // Some integral weight constant... |
87 | uint16_t Ki = 10300 / 33; |
87 | uint16_t Ki = 10300 / 33; |
88 | uint8_t RequiredMotors = 0; |
88 | uint8_t RequiredMotors = 0; |
89 | 89 | ||
90 | // No support for altitude control right now. |
90 | // No support for altitude control right now. |
91 | // int16_t SetPointHeight = 0; |
91 | // int16_t SetPointHeight = 0; |
92 | 92 | ||
93 | /************************************************************************/ |
93 | /************************************************************************/ |
94 | /* Filter for motor value smoothing (necessary???) */ |
94 | /* Filter for motor value smoothing (necessary???) */ |
95 | /************************************************************************/ |
95 | /************************************************************************/ |
96 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
96 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
97 | switch(dynamicParams.UserParams[5]) { |
97 | switch(dynamicParams.UserParams[5]) { |
98 | case 0: |
98 | case 0: |
99 | return newvalue; |
99 | return newvalue; |
100 | case 1: |
100 | case 1: |
101 | return (oldvalue + newvalue) / 2; |
101 | return (oldvalue + newvalue) / 2; |
102 | case 2: |
102 | case 2: |
103 | if(newvalue > oldvalue) |
103 | if(newvalue > oldvalue) |
104 | return (1 * (int16_t)oldvalue + newvalue) / 2; //mean of old and new |
104 | return (1 * (int16_t)oldvalue + newvalue) / 2; //mean of old and new |
105 | else |
105 | else |
106 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
106 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
107 | case 3: |
107 | case 3: |
108 | if(newvalue < oldvalue) |
108 | if(newvalue < oldvalue) |
109 | return (1 * (int16_t)oldvalue + newvalue) / 2; //mean of old and new |
109 | return (1 * (int16_t)oldvalue + newvalue) / 2; //mean of old and new |
110 | else |
110 | else |
111 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
111 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
112 | default: return newvalue; |
112 | default: return newvalue; |
113 | } |
113 | } |
114 | } |
114 | } |
115 | 115 | ||
116 | /************************************************************************/ |
116 | /************************************************************************/ |
117 | /* Neutral Readings */ |
117 | /* Neutral Readings */ |
118 | /************************************************************************/ |
118 | /************************************************************************/ |
119 | void flight_setNeutral() { |
119 | void flight_setNeutral() { |
120 | // GPSStickPitch = 0; |
120 | // GPSStickPitch = 0; |
121 | // GPSStickRoll = 0; |
121 | // GPSStickRoll = 0; |
122 | 122 | ||
123 | MKFlags |= MKFLAG_CALIBRATE; |
123 | MKFlags |= MKFLAG_CALIBRATE; |
124 | 124 | ||
125 | // not really used here any more. |
125 | // not really used here any more. |
126 | dynamicParams.KalmanK = -1; |
126 | dynamicParams.KalmanK = -1; |
127 | dynamicParams.KalmanMaxDrift = 0; |
127 | dynamicParams.KalmanMaxDrift = 0; |
128 | dynamicParams.KalmanMaxFusion = 32; |
128 | dynamicParams.KalmanMaxFusion = 32; |
129 | 129 | ||
130 | controlMixer_initVariables(); |
130 | controlMixer_initVariables(); |
131 | - | ||
132 | // TODO: Move off. |
- | |
133 | // RC_Quality = 100; |
- | |
134 | } |
131 | } |
135 | 132 | ||
136 | /************************************************************************/ |
133 | /************************************************************************/ |
137 | /* Transmit Motor Data via I2C */ |
134 | /* Transmit Motor Data via I2C */ |
138 | /************************************************************************/ |
135 | /************************************************************************/ |
139 | void sendMotorData(void) { |
136 | void sendMotorData(void) { |
140 | uint8_t i; |
137 | uint8_t i; |
141 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) { |
138 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) { |
142 | // If pilot has not started the engines.... |
139 | // If pilot has not started the engines.... |
143 | 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 |
144 | for(i = 0; i < MAX_MOTORS; i++) { |
141 | for(i = 0; i < MAX_MOTORS; i++) { |
145 | // 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. |
146 | if(!motorTestActive) Motor[i].SetPoint = 0; |
143 | if(!motorTestActive) Motor[i].SetPoint = 0; |
147 | else Motor[i].SetPoint = motorTest[i]; |
144 | else Motor[i].SetPoint = motorTest[i]; |
148 | } |
145 | } |
149 | if(motorTestActive) motorTestActive--; |
146 | if(motorTestActive) motorTestActive--; |
150 | } |
147 | } |
151 | 148 | ||
152 | /* |
149 | /* |
153 | DebugOut.Analog[12] = Motor[0].SetPoint; // Front |
150 | DebugOut.Analog[] = Motor[0].SetPoint; // Front |
154 | DebugOut.Analog[13] = Motor[1].SetPoint; // Rear |
151 | DebugOut.Analog[] = Motor[1].SetPoint; // Rear |
155 | DebugOut.Analog[14] = Motor[3].SetPoint; // Left |
152 | DebugOut.Analog[] = Motor[3].SetPoint; // Left |
156 | DebugOut.Analog[15] = Motor[2].SetPoint; // Right |
153 | DebugOut.Analog[] = Motor[2].SetPoint; // Right |
157 | */ |
154 | */ |
158 | // Start I2C Interrupt Mode |
155 | // Start I2C Interrupt Mode |
159 | I2C_Start(TWI_STATE_MOTOR_TX); |
156 | I2C_Start(TWI_STATE_MOTOR_TX); |
160 | } |
157 | } |
161 | 158 | ||
162 | void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor, uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
159 | void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor, uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
163 | Ki = 10300 / _Ki; |
160 | Ki = 10300 / _Ki; |
164 | gyroPFactor = _gyroPFactor; |
161 | gyroPFactor = _gyroPFactor; |
165 | gyroIFactor = _gyroIFactor; |
162 | gyroIFactor = _gyroIFactor; |
166 | yawPFactor = _yawPFactor; |
163 | yawPFactor = _yawPFactor; |
167 | yawIFactor = _yawIFactor; |
164 | yawIFactor = _yawIFactor; |
168 | } |
165 | } |
169 | 166 | ||
170 | void setNormalFlightParameters(void) { |
167 | void setNormalFlightParameters(void) { |
171 | setFlightParameters(dynamicParams.IFactor + 1, |
168 | setFlightParameters(dynamicParams.IFactor + 1, |
172 | dynamicParams.GyroP + 10, |
169 | dynamicParams.GyroP + 10, |
173 | staticParams.GlobalConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.GyroI, |
170 | staticParams.GlobalConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.GyroI, |
174 | dynamicParams.GyroP + 10, |
171 | dynamicParams.GyroP + 10, |
175 | dynamicParams.UserParams[6] |
172 | dynamicParams.UserParams[6] |
176 | ); |
173 | ); |
177 | } |
174 | } |
178 | 175 | ||
179 | void setStableFlightParameters(void) { |
176 | void setStableFlightParameters(void) { |
180 | setFlightParameters(33, 90, 120, 90, 120); |
177 | setFlightParameters(33, 90, 120, 90, 120); |
181 | } |
178 | } |
182 | 179 | ||
183 | void handleCommands(uint8_t command, uint8_t argument, uint8_t isCommandRepeated) { |
180 | void handleCommands(uint8_t command, uint8_t argument, uint8_t isCommandRepeated) { |
184 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) { |
181 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) { |
185 | if (command == COMMAND_GYROCAL && !isCommandRepeated) { |
182 | if (command == COMMAND_GYROCAL && !isCommandRepeated) { |
186 | // Run gyro calibration but do not repeat it. |
183 | // Run gyro calibration but do not repeat it. |
187 | GRN_OFF; |
184 | GRN_OFF; |
188 | 185 | ||
189 | // TODO: out of here. Anyway, MKFLAG_MOTOR_RUN is cleared. Not enough? |
186 | // TODO: out of here. Anyway, MKFLAG_MOTOR_RUN is cleared. Not enough? |
190 | // isFlying = 0; |
187 | // isFlying = 0; |
191 | // check roll/pitch stick position |
188 | // check roll/pitch stick position |
192 | // if pitch stick is top or roll stick is left or right --> change parameter setting |
189 | // if pitch stick is top or roll stick is left or right --> change parameter setting |
193 | // according to roll/pitch stick position |
190 | // according to roll/pitch stick position |
194 | 191 | ||
195 | if (argument < 6) { |
192 | if (argument < 6) { |
196 | // Gyro calinbration, with or without selecting a new parameter-set. |
193 | // Gyro calinbration, with or without selecting a new parameter-set. |
197 | if(argument > 0 && argument < 6) { |
194 | if(argument > 0 && argument < 6) { |
198 | // A valid parameter-set (1..5) was chosen - use it. |
195 | // A valid parameter-set (1..5) was chosen - use it. |
199 | setActiveParamSet(argument); |
196 | setActiveParamSet(argument); |
200 | } |
197 | } |
201 | ParamSet_ReadFromEEProm(getActiveParamSet()); |
198 | ParamSet_ReadFromEEProm(getActiveParamSet()); |
202 | attitude_setNeutral(); |
199 | attitude_setNeutral(); |
203 | flight_setNeutral(); |
200 | flight_setNeutral(); |
204 | // Right stick is centered; calibrate it to zero (hmm strictly does not belong here). |
201 | // Right stick is centered; calibrate it to zero (hmm strictly does not belong here). |
205 | // If heading hold is active, do not do it. TODO: We also want to re-set old calibration. |
202 | // If heading hold is active, do not do it. TODO: We also want to re-set old calibration. |
206 | controlMixer_setNeutral(!argument); |
203 | controlMixer_setNeutral(!argument); |
207 | beepNumber(getActiveParamSet()); |
204 | beepNumber(getActiveParamSet()); |
208 | } else if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE) && argument == 7) { |
205 | } else if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE) && argument == 7) { |
209 | // If right stick is centered and down |
206 | // If right stick is centered and down |
210 | compassCalState = 1; |
207 | compassCalState = 1; |
211 | beep(1000); |
208 | beep(1000); |
212 | } |
209 | } |
213 | } |
210 | } |
214 | 211 | ||
215 | // save the ACC neutral setting to eeprom |
212 | // save the ACC neutral setting to eeprom |
216 | else { |
213 | else { |
217 | if(command == COMMAND_ACCCAL && !isCommandRepeated) { |
214 | if(command == COMMAND_ACCCAL && !isCommandRepeated) { |
218 | // Run gyro and acc. meter calibration but do not repeat it. |
215 | // Run gyro and acc. meter calibration but do not repeat it. |
219 | GRN_OFF; |
216 | GRN_OFF; |
220 | analog_calibrateAcc(); |
217 | analog_calibrateAcc(); |
221 | attitude_setNeutral(); |
218 | attitude_setNeutral(); |
222 | flight_setNeutral(); |
219 | flight_setNeutral(); |
223 | controlMixer_setNeutral(1); // Calibrate right stick neutral position. |
220 | controlMixer_setNeutral(1); // Calibrate right stick neutral position. |
224 | beepNumber(getActiveParamSet()); |
221 | beepNumber(getActiveParamSet()); |
225 | } |
222 | } |
226 | } |
223 | } |
227 | } // end !MOTOR_RUN condition. |
224 | } // end !MOTOR_RUN condition. |
228 | if (command == COMMAND_START) { |
225 | if (command == COMMAND_START) { |
229 | isFlying = 1; // TODO: Really???? |
226 | isFlying = 1; // TODO: Really???? |
230 | // if (!controlMixer_isCommandRepeated()) { |
227 | // if (!controlMixer_isCommandRepeated()) { |
231 | // attitude_startDynamicCalibration(); // Try sense the effect of the motors on sensors. |
228 | // attitude_startDynamicCalibration(); // Try sense the effect of the motors on sensors. |
232 | MKFlags |= (MKFLAG_MOTOR_RUN | MKFLAG_START); // set flag RUN and START. TODO: Is that START flag used at all??? |
229 | MKFlags |= (MKFLAG_MOTOR_RUN | MKFLAG_START); // set flag RUN and START. TODO: Is that START flag used at all??? |
233 | // } else { // Pilot is holding stick, ever after motor start. Continue to sense the effect of the motors on sensors. |
230 | // } else { // Pilot is holding stick, ever after motor start. Continue to sense the effect of the motors on sensors. |
234 | // attitude_continueDynamicCalibration(); |
231 | // attitude_continueDynamicCalibration(); |
235 | // setPointYaw = 0; |
232 | // setPointYaw = 0; |
236 | // IPartPitch = 0; |
233 | // IPartPitch = 0; |
237 | // IPartRoll = 0; |
234 | // IPartRoll = 0; |
238 | // } |
235 | // } |
239 | } else if (command == COMMAND_STOP) { |
236 | } else if (command == COMMAND_STOP) { |
240 | isFlying = 0; |
237 | isFlying = 0; |
241 | MKFlags &= ~(MKFLAG_MOTOR_RUN); |
238 | MKFlags &= ~(MKFLAG_MOTOR_RUN); |
242 | } |
239 | } |
243 | } |
240 | } |
244 | 241 | ||
245 | /************************************************************************/ |
242 | /************************************************************************/ |
246 | /* Main Flight Control */ |
243 | /* Main Flight Control */ |
247 | /************************************************************************/ |
244 | /************************************************************************/ |
248 | void flight_control(void) { |
245 | void flight_control(void) { |
249 | int16_t tmp_int; |
246 | int16_t tmp_int; |
250 | // Mixer Fractions that are combined for Motor Control |
247 | // Mixer Fractions that are combined for Motor Control |
251 | int16_t yawTerm, throttleTerm, pitchTerm, rollTerm; |
248 | int16_t yawTerm, throttleTerm, term[2]; |
252 | 249 | ||
253 | // PID controller variables |
250 | // PID controller variables |
254 | int16_t PDPartPitch, PDPartRoll, PDPartYaw, PPartPitch, PPartRoll; |
251 | int16_t PDPart[2], PDPartYaw, PPart[2]; |
255 | static int32_t IPartPitch = 0, IPartRoll = 0; |
- | |
256 | 252 | static int32_t IPart[2] = {0,0}; |
|
257 | static int32_t setPointYaw = 0; |
253 | static int32_t setPointYaw = 0; |
258 | 254 | ||
259 | // Removed. Too complicated, and apparently not necessary with MEMS gyros anyway. |
255 | // Removed. Too complicated, and apparently not necessary with MEMS gyros anyway. |
260 | // static int32_t IntegralGyroPitchError = 0, IntegralGyroRollError = 0; |
256 | // static int32_t IntegralGyroPitchError = 0, IntegralGyroRollError = 0; |
261 | // static int32_t CorrectionPitch, CorrectionRoll; |
257 | // static int32_t CorrectionPitch, CorrectionRoll; |
262 | 258 | ||
263 | static uint16_t emergencyFlightTime; |
259 | static uint16_t emergencyFlightTime; |
264 | - | ||
265 | // No support for altitude control right now. |
- | |
266 | // static uint8_t HeightControlActive = 0; |
- | |
267 | // static int16_t HeightControlGas = 0; |
- | |
268 | - | ||
269 | static int8_t debugDataTimer = 1; |
260 | static int8_t debugDataTimer = 1; |
270 | 261 | ||
271 | // High resolution motor values for smoothing of PID motor outputs |
262 | // High resolution motor values for smoothing of PID motor outputs |
272 | static int16_t motorFilters[MAX_MOTORS]; |
263 | static int16_t motorFilters[MAX_MOTORS]; |
273 | 264 | ||
274 | uint8_t i; |
265 | uint8_t i, axis; |
275 | 266 | ||
276 | // Fire the main flight attitude calculation, including integration of angles. |
267 | // Fire the main flight attitude calculation, including integration of angles. |
277 | calculateFlightAttitude(); |
268 | calculateFlightAttitude(); |
278 | GRN_ON; |
269 | GRN_ON; |
279 | 270 | ||
280 | /* |
271 | /* |
281 | * TODO: update should: Set the stick variables if good signal, set them to zero if bad. |
272 | * TODO: update should: Set the stick variables if good signal, set them to zero if bad. |
282 | * Set variables also. |
273 | * Set variables also. |
283 | */ |
274 | */ |
284 | controlMixer_update(); |
275 | controlMixer_update(); |
285 | 276 | ||
286 | throttleTerm = controlThrottle; |
277 | throttleTerm = controlThrottle; |
287 | if(throttleTerm < staticParams.MinThrottle + 10) throttleTerm = staticParams.MinThrottle + 10; |
278 | if(throttleTerm < staticParams.MinThrottle + 10) throttleTerm = staticParams.MinThrottle + 10; |
288 | 279 | ||
289 | /************************************************************************/ |
280 | /************************************************************************/ |
290 | /* RC-signal is bad */ |
281 | /* RC-signal is bad */ |
291 | /* This part could be abstracted, as having yet another control input */ |
282 | /* This part could be abstracted, as having yet another control input */ |
292 | /* to the control mixer: An emergency autopilot control. */ |
283 | /* to the control mixer: An emergency autopilot control. */ |
293 | /************************************************************************/ |
284 | /************************************************************************/ |
294 | if(controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy |
285 | if(controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy |
295 | RED_ON; |
286 | RED_ON; |
296 | beepRCAlarm(); |
287 | beepRCAlarm(); |
297 | 288 | ||
298 | if(emergencyFlightTime) { |
289 | if(emergencyFlightTime) { |
299 | // continue emergency flight |
290 | // continue emergency flight |
300 | emergencyFlightTime--; |
291 | emergencyFlightTime--; |
301 | if(isFlying > 1000) { |
292 | if(isFlying > 1000) { |
302 | // We're probably still flying. Descend slowly. |
293 | // We're probably still flying. Descend slowly. |
303 | throttleTerm = staticParams.EmergencyGas; // Set emergency throttle |
294 | throttleTerm = staticParams.EmergencyGas; // Set emergency throttle |
304 | MKFlags |= (MKFLAG_EMERGENCY_LANDING); // Set flag for emergency landing |
295 | MKFlags |= (MKFLAG_EMERGENCY_LANDING); // Set flag for emergency landing |
305 | setStableFlightParameters(); |
296 | setStableFlightParameters(); |
306 | } else { |
297 | } else { |
307 | MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors. |
298 | MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors. |
308 | } |
299 | } |
309 | } else { |
300 | } else { |
310 | // end emergency flight (just cut the motors???) |
301 | // end emergency flight (just cut the motors???) |
311 | MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_LANDING); |
302 | MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_LANDING); |
312 | } |
303 | } |
313 | } else { |
304 | } else { |
314 | // signal is acceptable |
305 | // signal is acceptable |
315 | if(controlMixer_getSignalQuality() > SIGNAL_BAD) { |
306 | if(controlMixer_getSignalQuality() > SIGNAL_BAD) { |
316 | // Reset emergency landing control variables. |
307 | // Reset emergency landing control variables. |
317 | MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing |
308 | MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing |
318 | // The time is in whole seconds. |
309 | // The time is in whole seconds. |
319 | emergencyFlightTime = staticParams.EmergencyGasDuration * 488; |
310 | emergencyFlightTime = staticParams.EmergencyGasDuration * 488; |
320 | } |
311 | } |
321 | 312 | ||
322 | // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying. |
313 | // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying. |
323 | if(throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
314 | if(throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
324 | // increment flight-time counter until overflow. |
315 | // increment flight-time counter until overflow. |
325 | if(isFlying != 0xFFFF) isFlying++; |
316 | if(isFlying != 0xFFFF) isFlying++; |
326 | } else |
317 | } else |
327 | /* |
318 | /* |
328 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
319 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
329 | * Probably to avoid integration effects that will cause the copter to spin |
320 | * Probably to avoid integration effects that will cause the copter to spin |
330 | * or flip when taking off. |
321 | * or flip when taking off. |
331 | * TODO: What was the value of IPartPitch? At 1st run of this, it's 0 already. |
- | |
332 | */ |
322 | */ |
333 | if(isFlying < 256) { |
323 | if(isFlying < 256) { |
334 | IPartPitch = 0; |
324 | IPart[PITCH] = IPart[ROLL] = 0; |
335 | IPartRoll = 0; |
- | |
336 | // TODO: Don't stomp on other modules' variables!!! |
325 | // TODO: Don't stomp on other modules' variables!!! |
337 | controlYaw = 0; |
326 | controlYaw = 0; |
338 | if(isFlying == 250) { |
327 | if(isFlying == 250) { |
339 | updateCompassCourse = 1; |
328 | updateCompassCourse = 1; |
340 | yawAngle = 0; |
329 | yawAngle = 0; |
341 | setPointYaw = 0; |
330 | setPointYaw = 0; |
342 | } |
331 | } |
343 | } else { |
332 | } else { |
344 | // DebugOut.Digital[1] = 0; |
333 | // DebugOut.Digital[1] = 0; |
345 | // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag? |
334 | // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag? |
346 | // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe. |
335 | // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe. |
347 | MKFlags |= (MKFLAG_FLY); |
336 | MKFlags |= (MKFLAG_FLY); |
348 | } |
337 | } |
349 | 338 | ||
350 | /* |
339 | /* |
351 | * Get the current command (start/stop motors, calibrate), if any. |
340 | * Get the current command (start/stop motors, calibrate), if any. |
352 | */ |
341 | */ |
353 | uint8_t command = controlMixer_getCommand(); |
342 | uint8_t command = controlMixer_getCommand(); |
354 | uint8_t repeated = controlMixer_isCommandRepeated(); |
343 | uint8_t repeated = controlMixer_isCommandRepeated(); |
355 | uint8_t argument = controlMixer_getArgument(); |
344 | uint8_t argument = controlMixer_getArgument(); |
356 | 345 | ||
357 | handleCommands(command, argument, repeated); |
346 | handleCommands(command, argument, repeated); |
358 | 347 | ||
359 | // if(controlMixer_getSignalQuality() >= SIGNAL_GOOD) { |
348 | // if(controlMixer_getSignalQuality() >= SIGNAL_GOOD) { |
360 | setNormalFlightParameters(); |
349 | setNormalFlightParameters(); |
361 | // } |
350 | // } |
362 | } // end else (not bad signal case) |
351 | } // end else (not bad signal case) |
363 | 352 | ||
364 | /* |
353 | /* |
365 | * Looping the H&I way basically is just a matter of turning off attitude angle measurement |
354 | * Looping the H&I way basically is just a matter of turning off attitude angle measurement |
366 | * by integration (because 300 deg/s gyros are too slow) and turning down the throttle. |
355 | * by integration (because 300 deg/s gyros are too slow) and turning down the throttle. |
367 | * This is the throttle part. |
356 | * This is the throttle part. |
368 | */ |
357 | */ |
369 | if(looping) { |
358 | if(looping) { |
370 | if(throttleTerm > staticParams.LoopGasLimit) throttleTerm = staticParams.LoopGasLimit; |
359 | if(throttleTerm > staticParams.LoopGasLimit) throttleTerm = staticParams.LoopGasLimit; |
371 | } |
360 | } |
372 | 361 | ||
373 | /************************************************************************/ |
362 | /************************************************************************/ |
374 | /* Yawing */ |
363 | /* Yawing */ |
375 | /************************************************************************/ |
364 | /************************************************************************/ |
376 | if(abs(controlYaw) > 4 * staticParams.StickYawP) { // yaw stick is activated |
365 | if(abs(controlYaw) > 4 * staticParams.StickYawP) { // yaw stick is activated |
377 | badCompassHeading = 1000; |
366 | badCompassHeading = 1000; |
378 | if(!(staticParams.GlobalConfig & CFG_COMPASS_FIX)) { |
367 | if(!(staticParams.GlobalConfig & CFG_COMPASS_FIX)) { |
379 | updateCompassCourse = 1; |
368 | updateCompassCourse = 1; |
380 | } |
369 | } |
381 | } |
370 | } |
382 | 371 | ||
383 | setPointYaw = controlYaw; |
372 | setPointYaw = controlYaw; |
384 | 373 | ||
385 | // Trim drift of yawAngle with controlYaw. |
374 | // Trim drift of yawAngle with controlYaw. |
386 | // TODO: We want NO feedback of control related stuff to the attitude related stuff. |
375 | // TODO: We want NO feedback of control related stuff to the attitude related stuff. |
387 | yawAngle -= controlYaw; |
376 | yawAngle -= controlYaw; |
388 | 377 | ||
389 | // limit the effect |
378 | // limit the effect |
390 | CHECK_MIN_MAX(yawAngle, -50000, 50000) |
379 | CHECK_MIN_MAX(yawAngle, -50000, 50000) |
391 | 380 | ||
392 | /************************************************************************/ |
381 | /************************************************************************/ |
393 | /* Compass is currently not supported. */ |
382 | /* Compass is currently not supported. */ |
394 | /************************************************************************/ |
383 | /************************************************************************/ |
395 | /* |
384 | /* |
396 | if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) { |
385 | if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) { |
397 | updateCompass(); |
386 | updateCompass(); |
398 | } |
387 | } |
399 | */ |
388 | */ |
400 | 389 | ||
401 | #if defined (USE_MK3MAG) |
390 | #if defined (USE_MK3MAG) |
402 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
403 | - | ||
404 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
405 | /************************************************************************/ |
391 | /************************************************************************/ |
406 | /* GPS is currently not supported. */ |
392 | /* GPS is currently not supported. */ |
407 | /************************************************************************/ |
393 | /************************************************************************/ |
408 | /* |
394 | /* |
409 | if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) { |
395 | if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) { |
410 | GPS_Main(); |
396 | GPS_Main(); |
411 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
397 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
412 | } |
398 | } |
413 | else { |
399 | else { |
414 | // GPSStickPitch = 0; |
400 | // GPSStickPitch = 0; |
415 | // GPSStickRoll = 0; |
401 | // GPSStickRoll = 0; |
416 | } |
402 | } |
417 | */ |
403 | */ |
418 | #endif |
404 | #endif |
- | 405 | ||
- | 406 | #define SENSOR_LIMIT (4096 * 4) |
|
419 | 407 | ||
420 | /************************************************************************/ |
408 | /************************************************************************/ |
421 | /* Calculate control feedback from angle (gyro integral) */ |
409 | /* Calculate control feedback from angle (gyro integral) */ |
422 | /* and angular velocity (gyro signal) */ |
410 | /* and angular velocity (gyro signal) */ |
423 | /************************************************************************/ |
411 | /************************************************************************/ |
424 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
412 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
- | 413 | for (axis=PITCH; axis<=ROLL; axis++) { |
|
425 | if(looping & LOOPING_PITCH_AXIS) { |
414 | if(looping & (1<<(4+axis))) { |
426 | PPartPitch = 0; |
415 | PPart[axis] = 0; |
427 | } else { // TODO: Where do the 44000 come from??? |
416 | } else { // TODO: Where do the 44000 come from??? |
428 | PPartPitch = pitchAngle * gyroIFactor / (44000 / STICK_GAIN); // P-Part - Proportional to Integral |
417 | PPart[axis] = angle[axis] * gyroIFactor / (44000 / CONTROL_SCALING); // P-Part - Proportional to Integral |
429 | } |
418 | } |
430 | 419 | ||
- | 420 | /* |
|
431 | // Now blend in the D-part - proportional to the Differential of the integral = the rate. |
421 | * Now blend in the D-part - proportional to the Differential of the integral = the rate. |
- | 422 | * Read this as: PDPart = PPart + rate_PID * pfactor * CONTROL_SCALING |
|
- | 423 | * where pfactor is in [0..1]. |
|
- | 424 | */ |
|
432 | PDPartPitch = PPartPitch + (int32_t)((int32_t)pitchRate_PID * gyroPFactor / (256L / STICK_GAIN)) |
425 | PDPart[axis] = PPart[axis] + (int32_t)((int32_t)rate_PID[axis] * gyroPFactor / (256L / CONTROL_SCALING)) |
433 | + (pitchDifferential * (int16_t)dynamicParams.GyroD) / 16; |
426 | + (differential[axis] * (int16_t)dynamicParams.GyroD) / 16; |
434 | 427 | ||
435 | // The P-part is actually the I-part... |
- | |
436 | if(looping & LOOPING_ROLL_AXIS) { |
- | |
437 | PPartRoll = 0; |
- | |
438 | } else { // TODO: Where do the 44000 come from??? |
428 | CHECK_MIN_MAX(PDPart[axis], -SENSOR_LIMIT, SENSOR_LIMIT); |
439 | PPartRoll = (rollAngle * gyroIFactor) / (44000 / STICK_GAIN); // P-Part - Proportional to Integral |
- | |
440 | } |
429 | } |
441 | - | ||
442 | // Now blend in the P-part - proportional to the Differential of the integral = the rate |
- | |
443 | PDPartRoll = PPartRoll + (int32_t)((int32_t)rollRate_PID * gyroPFactor / (256L / STICK_GAIN)) |
- | |
444 | + (rollDifferential * (int16_t)dynamicParams.GyroD) / 16; |
- | |
445 | 430 | ||
446 | PDPartYaw = (int32_t)(yawRate * 2 * (int32_t)yawPFactor) / (256L / STICK_GAIN) |
431 | PDPartYaw = (int32_t)(yawRate * 2 * (int32_t)yawPFactor) / (256L / CONTROL_SCALING) |
447 | + (int32_t)(yawAngle * yawIFactor) / (2 * (44000 / STICK_GAIN)); |
432 | + (int32_t)(yawAngle * yawIFactor) / (2 * (44000 / CONTROL_SCALING)); |
448 | - | ||
449 | // limit control feedback |
- | |
450 | #define SENSOR_LIMIT (4096 * 4) |
- | |
451 | CHECK_MIN_MAX(PDPartPitch, -SENSOR_LIMIT, SENSOR_LIMIT); |
433 | |
452 | CHECK_MIN_MAX(PDPartRoll, -SENSOR_LIMIT, SENSOR_LIMIT); |
434 | // limit control feedback |
453 | CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
435 | CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
454 | 436 | ||
455 | /* |
437 | /* |
456 | * Compose throttle term. |
438 | * Compose throttle term. |
457 | * If a Bl-Ctrl is missing, prevent takeoff. |
439 | * If a Bl-Ctrl is missing, prevent takeoff. |
458 | */ |
440 | */ |
459 | if(missingMotor) { |
441 | if(missingMotor) { |
460 | // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway??? |
442 | // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway??? |
461 | if((isFlying > 1) && (isFlying < 50) && (throttleTerm > 0)) |
443 | if((isFlying > 1) && (isFlying < 50) && (throttleTerm > 0)) |
462 | isFlying = 1; // keep within lift off condition |
444 | isFlying = 1; // keep within lift off condition |
463 | throttleTerm = staticParams.MinThrottle; // reduce gas to min to avoid lift of |
445 | throttleTerm = staticParams.MinThrottle; // reduce gas to min to avoid lift of |
464 | } |
446 | } |
465 | 447 | ||
466 | /* |
448 | /* |
467 | * Height control was here. |
449 | * Height control was here. |
468 | */ |
450 | */ |
469 | if(throttleTerm > staticParams.MaxThrottle - 20) throttleTerm = (staticParams.MaxThrottle - 20); |
451 | if(throttleTerm > staticParams.MaxThrottle - 20) throttleTerm = (staticParams.MaxThrottle - 20); |
470 | throttleTerm *= STICK_GAIN; |
452 | throttleTerm *= CONTROL_SCALING; |
471 | 453 | ||
472 | /* |
454 | /* |
473 | * Compose yaw term. |
455 | * Compose yaw term. |
- | 456 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
|
- | 457 | * However, at low throttle the yaw term is limited to a fixed value, |
|
- | 458 | * and at high throttle it is limited by the throttle reserve (the difference |
|
- | 459 | * between current throttle and maximum throttle). |
|
474 | */ |
460 | */ |
475 | #define MIN_YAWGAS (40 * STICK_GAIN) // yaw also below this gas value |
461 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
476 | yawTerm = PDPartYaw - setPointYaw * STICK_GAIN; |
462 | yawTerm = PDPartYaw - setPointYaw * CONTROL_SCALING; |
477 | // limit yawTerm |
463 | // limit yawTerm |
478 | if(throttleTerm > MIN_YAWGAS) { |
464 | if(throttleTerm > MIN_YAWGAS) { |
479 | /* |
- | |
480 | * -throttle/2 < -20 <= yawTerm <= 20 < throttle/2 |
- | |
481 | */ |
- | |
482 | CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2)); |
465 | CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2)); |
483 | } else { |
466 | } else { |
484 | /* |
- | |
485 | * -20 <= yawTerm <= 20 |
- | |
486 | */ |
- | |
487 | CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2)); |
467 | CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2)); |
488 | } |
468 | } |
489 | 469 | ||
490 | tmp_int = staticParams.MaxThrottle * STICK_GAIN; |
- | |
491 | - | ||
492 | /* |
- | |
493 | * throttle-MaxThrottle <= yawTerm <= MaxThrottle-throttle |
- | |
494 | */ |
470 | tmp_int = staticParams.MaxThrottle * CONTROL_SCALING; |
495 | CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm)); |
- | |
496 | - | ||
497 | /* |
- | |
498 | * Compose pitch and roll terms. This is finally where the sticks come into play. |
- | |
499 | */ |
- | |
500 | if(gyroIFactor) { |
- | |
501 | // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos. |
- | |
502 | // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time. |
- | |
503 | // TODO: Find out why this seems to be proportional to stick position - not integrating it at all. |
- | |
504 | IPartPitch += PPartPitch - controlPitch; // Integrate difference between P part (the angle) and the stick pos. |
- | |
505 | IPartRoll += PPartRoll - controlRoll; // I-part for attitude control OK |
- | |
506 | } else { |
- | |
507 | // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos. |
- | |
508 | IPartPitch += PDPartPitch - controlPitch; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
- | |
509 | IPartRoll += PDPartRoll - controlRoll; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
- | |
510 | } |
- | |
511 | - | ||
512 | // TODO: From which planet comes the 16000? |
- | |
513 | CHECK_MIN_MAX(IPartPitch, -(STICK_GAIN * 16000L), (STICK_GAIN * 16000L)); |
- | |
514 | - | ||
515 | // Add (P, D) parts minus stick pos. to the scaled-down I part. |
- | |
516 | pitchTerm = PDPartPitch - controlPitch + IPartPitch / Ki; // PID-controller for pitch |
- | |
517 | - | ||
518 | CHECK_MIN_MAX(IPartRoll, -(STICK_GAIN * 16000L), (STICK_GAIN * 16000L)); |
- | |
519 | rollTerm = PDPartRoll - controlRoll + IPartRoll / Ki; // PID-controller for roll |
- | |
520 | - | ||
521 | /* |
- | |
522 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
- | |
523 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
- | |
524 | * (max. pitch or roll term is the throttle value). |
- | |
525 | * TODO: Why a growing function of yaw? |
471 | CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm)); |
- | 472 | ||
- | 473 | tmp_int = (int32_t)((int32_t)dynamicParams.DynamicStability * (int32_t)(throttleTerm + abs(yawTerm) / 2)) / 64; |
|
- | 474 | ||
- | 475 | for (axis=PITCH; axis<=ROLL; axis++) { |
|
- | 476 | /* |
|
- | 477 | * Compose pitch and roll terms. This is finally where the sticks come into play. |
|
- | 478 | */ |
|
- | 479 | if(gyroIFactor) { |
|
- | 480 | // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos. |
|
- | 481 | // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time. |
|
- | 482 | // TODO: Find out why this seems to be proportional to stick position - not integrating it at all. |
|
- | 483 | IPart[axis] += PPart[axis] - control[axis]; // Integrate difference between P part (the angle) and the stick pos. |
|
- | 484 | } else { |
|
- | 485 | // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos. |
|
- | 486 | // To keep up with a full stick PDPart should be about 156... |
|
- | 487 | IPart[axis] += PDPart[axis] - control[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
|
- | 488 | } |
|
- | 489 | ||
- | 490 | // TODO: From which planet comes the 16000? |
|
- | 491 | CHECK_MIN_MAX(IPart[axis], -(CONTROL_SCALING * 16000L), (CONTROL_SCALING * 16000L)); |
|
- | 492 | // Add (P, D) parts minus stick pos. to the scaled-down I part. |
|
- | 493 | term[axis] = PDPart[axis] - control[axis] + IPart[axis] / Ki; // PID-controller for pitch |
|
- | 494 | ||
- | 495 | /* |
|
526 | */ |
496 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
- | 497 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
|
- | 498 | * (max. pitch or roll term is the throttle value). |
|
527 | tmp_int = (int32_t)((int32_t)dynamicParams.DynamicStability * (int32_t)(throttleTerm + abs(yawTerm) / 2)) / 64; |
499 | * TODO: Why a growing function of yaw? |
- | 500 | */ |
|
528 | CHECK_MIN_MAX(pitchTerm, -tmp_int, tmp_int); |
501 | CHECK_MIN_MAX(term[axis], -tmp_int, tmp_int); |
529 | CHECK_MIN_MAX(rollTerm, -tmp_int, tmp_int); |
502 | } |
530 | 503 | ||
531 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
504 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
532 | // Universal Mixer |
505 | // Universal Mixer |
- | 506 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
|
533 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
507 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
534 | for(i = 0; i < MAX_MOTORS; i++) { |
508 | for(i = 0; i < MAX_MOTORS; i++) { |
535 | int16_t tmp; |
509 | int16_t tmp; |
536 | if(Mixer.Motor[i][MIX_THROTTLE] > 0) { // If a motor has a zero throttle mix, it is not considered. |
510 | if(Mixer.Motor[i][MIX_THROTTLE] > 0) { // If a motor has a zero throttle mix, it is not considered. |
537 | tmp = ((int32_t)throttleTerm * Mixer.Motor[i][MIX_THROTTLE]) / 64L; |
511 | tmp = ((int32_t)throttleTerm * Mixer.Motor[i][MIX_THROTTLE]) / 64L; |
538 | tmp += ((int32_t)pitchTerm * Mixer.Motor[i][MIX_PITCH]) / 64L; |
512 | tmp += ((int32_t)term[PITCH] * Mixer.Motor[i][MIX_PITCH]) / 64L; |
539 | tmp += ((int32_t)rollTerm * Mixer.Motor[i][MIX_ROLL]) / 64L; |
513 | tmp += ((int32_t)term[ROLL] * Mixer.Motor[i][MIX_ROLL]) / 64L; |
540 | tmp += ((int32_t)yawTerm * Mixer.Motor[i][MIX_YAW]) / 64L; |
514 | tmp += ((int32_t)yawTerm * Mixer.Motor[i][MIX_YAW]) / 64L; |
541 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
515 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
542 | tmp = motorFilters[i] / STICK_GAIN; |
516 | tmp = motorFilters[i] / CONTROL_SCALING; |
543 | CHECK_MIN_MAX(tmp, staticParams.MinThrottle, staticParams.MaxThrottle); |
517 | CHECK_MIN_MAX(tmp, staticParams.MinThrottle, staticParams.MaxThrottle); |
544 | Motor[i].SetPoint = tmp; |
518 | Motor[i].SetPoint = tmp; |
545 | } |
519 | } |
546 | else Motor[i].SetPoint = 0; |
520 | else Motor[i].SetPoint = 0; |
547 | } |
521 | } |
548 | 522 | ||
549 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
523 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
550 | // Debugwerte zuordnen |
524 | // Debugging |
551 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
525 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
552 | if(!(--debugDataTimer)) { |
526 | if(!(--debugDataTimer)) { |
553 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
527 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
554 | DebugOut.Analog[0] = (10 * pitchAngle) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
528 | DebugOut.Analog[0] = (10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
555 | DebugOut.Analog[1] = (10 * rollAngle) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
529 | DebugOut.Analog[1] = (10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
556 | DebugOut.Analog[2] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; |
530 | DebugOut.Analog[2] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; |
557 | 531 | ||
558 | DebugOut.Analog[9] = setPointYaw; |
532 | // DebugOut.Analog[9] = setPointYaw; |
559 | DebugOut.Analog[10] = yawIFactor; |
533 | // DebugOut.Analog[10] = yawIFactor; |
560 | DebugOut.Analog[11] = gyroIFactor; |
534 | // DebugOut.Analog[11] = gyroIFactor; |
561 | // DebugOut.Analog[12] = RC_getVariable(0); |
535 | // DebugOut.Analog[12] = RC_getVariable(0); |
562 | // DebugOut.Analog[13] = dynamicParams.UserParams[0]; |
536 | // DebugOut.Analog[13] = dynamicParams.UserParams[0]; |
563 | DebugOut.Analog[14] = RC_getVariable(4); |
537 | // DebugOut.Analog[14] = RC_getVariable(4); |
564 | DebugOut.Analog[15] = dynamicParams.UserParams[4]; |
538 | // DebugOut.Analog[15] = dynamicParams.UserParams[4]; |
565 | /* DebugOut.Analog[11] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; */ |
539 | /* DebugOut.Analog[11] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; */ |
566 | 540 | ||
567 | // 12..15 are the controls. |
541 | // 12..15 are the controls. |
568 | // DebugOut.Analog[16] = pitchAxisAcc; |
542 | // DebugOut.Analog[16] = pitchAxisAcc; |
569 | // DebugOut.Analog[17] = rollAxisAcc; |
543 | // DebugOut.Analog[17] = rollAxisAcc; |
570 | // DebugOut.Analog[18] = ZAxisAcc; |
544 | // DebugOut.Analog[18] = ZAxisAcc; |
571 | 545 | ||
572 | DebugOut.Analog[19] = throttleTerm; |
546 | DebugOut.Analog[19] = throttleTerm; |
573 | DebugOut.Analog[20] = pitchTerm; |
547 | DebugOut.Analog[20] = term[PITCH]; |
574 | DebugOut.Analog[21] = rollTerm; |
548 | DebugOut.Analog[21] = term[ROLL]; |
575 | DebugOut.Analog[22] = yawTerm; |
549 | DebugOut.Analog[22] = yawTerm; |
576 | DebugOut.Analog[23] = PPartPitch; // |
- | |
577 | DebugOut.Analog[24] = IPartPitch /Ki; // meget meget lille. |
- | |
578 | DebugOut.Analog[25] = PDPartPitch; // omtrent lig ppart. |
- | |
- | 550 | ||
- | 551 | DebugOut.Analog[23] = PPart[PITCH]; // |
|
- | 552 | DebugOut.Analog[24] = IPart[PITCH] /Ki; // meget meget lille. |
|
- | 553 | DebugOut.Analog[25] = PDPart[PITCH]; // omtrent lig ppart. |
|
579 | 554 | ||
580 | DebugOut.Analog[26] = pitchAccNoisePeak; |
555 | DebugOut.Analog[26] = accNoisePeak[PITCH]; |
581 | DebugOut.Analog[27] = rollAccNoisePeak; |
556 | DebugOut.Analog[27] = accNoisePeak[ROLL]; |
582 | 557 | ||
583 | DebugOut.Analog[30] = pitchGyroNoisePeak; |
558 | DebugOut.Analog[30] = gyroNoisePeak[PITCH]; |
584 | DebugOut.Analog[31] = rollGyroNoisePeak; |
559 | DebugOut.Analog[31] = gyroNoisePeak[ROLL]; |
585 | } |
560 | } |
586 | } |
561 | } |
587 | 562 |