Rev 1179 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1179 | Rev 1180 | ||
---|---|---|---|
- | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 2 | // + Copyright (c) 04.2007 Holger Buss |
|
- | 3 | // + Nur für den privaten Gebrauch |
|
- | 4 | // + www.MikroKopter.com |
|
- | 5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 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. |
|
- | 8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
|
- | 9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
|
- | 10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
|
- | 11 | // + Verkauf von Luftbildaufnahmen, usw. |
|
- | 12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 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 |
|
- | 15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 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" |
|
- | 18 | // + eindeutig als Ursprung verlinkt werden |
|
- | 19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
|
- | 21 | // + Benutzung auf eigene Gefahr |
|
- | 22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
|
- | 23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 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 |
|
- | 26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
|
- | 28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
|
- | 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 |
|
- | 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 |
|
- | 34 | // + for non-commercial use (directly or indirectly) |
|
- | 35 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
|
- | 36 | // + with our written permission |
|
- | 37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
|
- | 38 | // + clearly linked as origin |
|
- | 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" |
|
- | 41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
- | 42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
- | 43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
|
- | 44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
- | 45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
- | 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) |
|
- | 48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
- | 49 | // + POSSIBILITY OF SUCH DAMAGE. |
|
- | 50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
1 | #include <inttypes.h> |
51 | #include <inttypes.h> |
2 | #include <stdlib.h> |
52 | #include <stdlib.h> |
3 | #include "fc.h" |
53 | #include "fc.h" |
4 | #include "ubx.h" |
54 | #include "ubx.h" |
5 | #include "mymath.h" |
55 | #include "mymath.h" |
6 | #include "timer0.h" |
56 | #include "timer0.h" |
7 | #include "uart.h" |
57 | #include "uart0.h" |
8 | #include "rc.h" |
58 | #include "rc.h" |
9 | #include "eeprom.h" |
59 | #include "eeprom.h" |
10 | 60 | ||
11 | typedef enum |
61 | typedef enum |
12 | { |
62 | { |
13 | GPS_FLIGHT_MODE_UNDEF, |
63 | GPS_FLIGHT_MODE_UNDEF, |
14 | GPS_FLIGHT_MODE_FREE, |
64 | GPS_FLIGHT_MODE_FREE, |
15 | GPS_FLIGHT_MODE_AID, |
65 | GPS_FLIGHT_MODE_AID, |
16 | GPS_FLIGHT_MODE_HOME, |
66 | GPS_FLIGHT_MODE_HOME, |
17 | } FlightMode_t; |
67 | } FlightMode_t; |
18 | 68 | ||
19 | #define GPS_POSINTEGRAL_LIMIT 32000 |
69 | #define GPS_POSINTEGRAL_LIMIT 32000 |
20 | #define GPS_STICK_LIMIT 45 // limit of gps stick control to avoid critical flight attitudes |
70 | #define GPS_STICK_LIMIT 45 // limit of gps stick control to avoid critical flight attitudes |
21 | #define GPS_P_LIMIT 25 |
71 | #define GPS_P_LIMIT 25 |
22 | 72 | ||
23 | 73 | ||
24 | typedef struct |
74 | typedef struct |
25 | { |
75 | { |
26 | int32_t Longitude; |
76 | int32_t Longitude; |
27 | int32_t Latitude; |
77 | int32_t Latitude; |
28 | int32_t Altitude; |
78 | int32_t Altitude; |
29 | Status_t Status; |
79 | Status_t Status; |
30 | } GPS_Pos_t; |
80 | } GPS_Pos_t; |
31 | 81 | ||
32 | // GPS coordinates for hold position |
82 | // GPS coordinates for hold position |
33 | GPS_Pos_t HoldPosition = {0,0,0,INVALID}; |
83 | GPS_Pos_t HoldPosition = {0,0,0,INVALID}; |
34 | // GPS coordinates for home position |
84 | // GPS coordinates for home position |
35 | GPS_Pos_t HomePosition = {0,0,0,INVALID}; |
85 | GPS_Pos_t HomePosition = {0,0,0,INVALID}; |
36 | // the current flight mode |
86 | // the current flight mode |
37 | FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF; |
87 | FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF; |
38 | 88 | ||
39 | 89 | ||
40 | // --------------------------------------------------------------------------------- |
90 | // --------------------------------------------------------------------------------- |
41 | void GPS_UpdateParameter(void) |
91 | void GPS_UpdateParameter(void) |
42 | { |
92 | { |
43 | static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF; |
93 | static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF; |
44 | 94 | ||
45 | if((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING)) |
95 | if((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING)) |
46 | { |
96 | { |
47 | FlightMode = GPS_FLIGHT_MODE_FREE; |
97 | FlightMode = GPS_FLIGHT_MODE_FREE; |
48 | } |
98 | } |
49 | else |
99 | else |
50 | { |
100 | { |
51 | if (FCParam.NaviGpsModeControl < 50) FlightMode = GPS_FLIGHT_MODE_AID; |
101 | if (FCParam.NaviGpsModeControl < 50) FlightMode = GPS_FLIGHT_MODE_AID; |
52 | else if(FCParam.NaviGpsModeControl < 180) FlightMode = GPS_FLIGHT_MODE_FREE; |
102 | else if(FCParam.NaviGpsModeControl < 180) FlightMode = GPS_FLIGHT_MODE_FREE; |
53 | else FlightMode = GPS_FLIGHT_MODE_HOME; |
103 | else FlightMode = GPS_FLIGHT_MODE_HOME; |
54 | } |
104 | } |
55 | if (FlightMode != FlightModeOld) |
105 | if (FlightMode != FlightModeOld) |
56 | { |
106 | { |
57 | BeepTime = 100; |
107 | BeepTime = 100; |
58 | } |
108 | } |
59 | FlightModeOld = FlightMode; |
109 | FlightModeOld = FlightMode; |
60 | } |
110 | } |
61 | 111 | ||
62 | 112 | ||
63 | 113 | ||
64 | // --------------------------------------------------------------------------------- |
114 | // --------------------------------------------------------------------------------- |
65 | // This function defines a good GPS signal condition |
115 | // This function defines a good GPS signal condition |
66 | uint8_t GPS_IsSignalOK(void) |
116 | uint8_t GPS_IsSignalOK(void) |
67 | { |
117 | { |
68 | static uint8_t GPSFix = 0; |
118 | static uint8_t GPSFix = 0; |
69 | if( (GPSInfo.status != INVALID) && (GPSInfo.satfix == SATFIX_3D) && (GPSInfo.flags & FLAG_GPSFIXOK) && ((GPSInfo.satnum >= ParamSet.NaviGpsMinSat) || GPSFix)) |
119 | if( (GPSInfo.status != INVALID) && (GPSInfo.satfix == SATFIX_3D) && (GPSInfo.flags & FLAG_GPSFIXOK) && ((GPSInfo.satnum >= ParamSet.NaviGpsMinSat) || GPSFix)) |
70 | { |
120 | { |
71 | GPSFix = 1; |
121 | GPSFix = 1; |
72 | return(1); |
122 | return(1); |
73 | 123 | ||
74 | } |
124 | } |
75 | else return (0); |
125 | else return (0); |
76 | 126 | ||
77 | } |
127 | } |
78 | // --------------------------------------------------------------------------------- |
128 | // --------------------------------------------------------------------------------- |
79 | // rescale xy-vector length to limit |
129 | // rescale xy-vector length to limit |
80 | uint8_t GPS_LimitXY(int32_t *x, int32_t *y, int32_t limit) |
130 | uint8_t GPS_LimitXY(int32_t *x, int32_t *y, int32_t limit) |
81 | { |
131 | { |
82 | uint8_t retval = 0; |
132 | uint8_t retval = 0; |
83 | int32_t len; |
133 | int32_t len; |
84 | len = (int32_t)c_sqrt(*x * *x + *y * *y); |
134 | len = (int32_t)c_sqrt(*x * *x + *y * *y); |
85 | if (len > limit) |
135 | if (len > limit) |
86 | { |
136 | { |
87 | // normalize control vector components to the limit |
137 | // normalize control vector components to the limit |
88 | *x = (*x * limit) / len; |
138 | *x = (*x * limit) / len; |
89 | *y = (*y * limit) / len; |
139 | *y = (*y * limit) / len; |
90 | retval = 1; |
140 | retval = 1; |
91 | } |
141 | } |
92 | return(retval); |
142 | return(retval); |
93 | } |
143 | } |
94 | 144 | ||
95 | // checks nick and roll sticks for manual control |
145 | // checks nick and roll sticks for manual control |
96 | uint8_t GPS_IsManualControlled(void) |
146 | uint8_t GPS_IsManualControlled(void) |
97 | { |
147 | { |
98 | if ( (abs(PPM_in[ParamSet.ChannelAssignment[CH_NICK]]) < ParamSet.NaviStickThreshold) && (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < ParamSet.NaviStickThreshold)) return 0; |
148 | if ( (abs(PPM_in[ParamSet.ChannelAssignment[CH_NICK]]) < ParamSet.NaviStickThreshold) && (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < ParamSet.NaviStickThreshold)) return 0; |
99 | else return 1; |
149 | else return 1; |
100 | } |
150 | } |
101 | 151 | ||
102 | // set given position to current gps position |
152 | // set given position to current gps position |
103 | uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos) |
153 | uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos) |
104 | { |
154 | { |
105 | uint8_t retval = 0; |
155 | uint8_t retval = 0; |
106 | if(pGPSPos == NULL) return(retval); // bad pointer |
156 | if(pGPSPos == NULL) return(retval); // bad pointer |
107 | 157 | ||
108 | if(GPS_IsSignalOK()) |
158 | if(GPS_IsSignalOK()) |
109 | { // is GPS signal condition is fine |
159 | { // is GPS signal condition is fine |
110 | pGPSPos->Longitude = GPSInfo.longitude; |
160 | pGPSPos->Longitude = GPSInfo.longitude; |
111 | pGPSPos->Latitude = GPSInfo.latitude; |
161 | pGPSPos->Latitude = GPSInfo.latitude; |
112 | pGPSPos->Altitude = GPSInfo.altitude; |
162 | pGPSPos->Altitude = GPSInfo.altitude; |
113 | pGPSPos->Status = NEWDATA; |
163 | pGPSPos->Status = NEWDATA; |
114 | retval = 1; |
164 | retval = 1; |
115 | } |
165 | } |
116 | else |
166 | else |
117 | { // bad GPS signal condition |
167 | { // bad GPS signal condition |
118 | pGPSPos->Status = INVALID; |
168 | pGPSPos->Status = INVALID; |
119 | retval = 0; |
169 | retval = 0; |
120 | } |
170 | } |
121 | return(retval); |
171 | return(retval); |
122 | } |
172 | } |
123 | 173 | ||
124 | // clear position |
174 | // clear position |
125 | uint8_t GPS_ClearPosition(GPS_Pos_t * pGPSPos) |
175 | uint8_t GPS_ClearPosition(GPS_Pos_t * pGPSPos) |
126 | { |
176 | { |
127 | uint8_t retval = 0; |
177 | uint8_t retval = 0; |
128 | if(pGPSPos == NULL) return(retval); // bad pointer |
178 | if(pGPSPos == NULL) return(retval); // bad pointer |
129 | else |
179 | else |
130 | { |
180 | { |
131 | pGPSPos->Longitude = 0; |
181 | pGPSPos->Longitude = 0; |
132 | pGPSPos->Latitude = 0; |
182 | pGPSPos->Latitude = 0; |
133 | pGPSPos->Altitude = 0; |
183 | pGPSPos->Altitude = 0; |
134 | pGPSPos->Status = INVALID; |
184 | pGPSPos->Status = INVALID; |
135 | retval = 1; |
185 | retval = 1; |
136 | } |
186 | } |
137 | return (retval); |
187 | return (retval); |
138 | } |
188 | } |
139 | 189 | ||
140 | // disable GPS control sticks |
190 | // disable GPS control sticks |
141 | void GPS_Neutral(void) |
191 | void GPS_Neutral(void) |
142 | { |
192 | { |
143 | GPS_Nick = 0; |
193 | GPSStickNick = 0; |
144 | GPS_Roll = 0; |
194 | GPSStickRoll = 0; |
145 | } |
195 | } |
146 | 196 | ||
147 | // calculates the GPS control stick values from the deviation to target position |
197 | // calculates the GPS control stick values from the deviation to target position |
148 | // if the pointer to the target positin is NULL or is the target position invalid |
198 | // if the pointer to the target positin is NULL or is the target position invalid |
149 | // then the P part of the controller is deactivated. |
199 | // then the P part of the controller is deactivated. |
150 | void GPS_PIDController(GPS_Pos_t *pTargetPos) |
200 | void GPS_PIDController(GPS_Pos_t *pTargetPos) |
151 | { |
201 | { |
152 | static int32_t PID_Nick, PID_Roll; |
202 | static int32_t PID_Nick, PID_Roll; |
153 | int32_t coscompass, sincompass; |
203 | int32_t coscompass, sincompass; |
154 | int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm |
204 | int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm |
155 | int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0; |
205 | int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0; |
156 | int32_t PID_North = 0, PID_East = 0; |
206 | int32_t PID_North = 0, PID_East = 0; |
157 | static int32_t cos_target_latitude = 1; |
207 | static int32_t cos_target_latitude = 1; |
158 | static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0; |
208 | static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0; |
159 | static GPS_Pos_t *pLastTargetPos = 0; |
209 | static GPS_Pos_t *pLastTargetPos = 0; |
160 | 210 | ||
161 | // if GPS data and Compass are ok |
211 | // if GPS data and Compass are ok |
162 | if( GPS_IsSignalOK() && (CompassHeading >= 0) ) |
212 | if( GPS_IsSignalOK() && (CompassHeading >= 0) ) |
163 | { |
213 | { |
164 | 214 | ||
165 | if(pTargetPos != NULL) // if there is a target position |
215 | if(pTargetPos != NULL) // if there is a target position |
166 | { |
216 | { |
167 | if(pTargetPos->Status != INVALID) // and the position data are valid |
217 | if(pTargetPos->Status != INVALID) // and the position data are valid |
168 | { |
218 | { |
169 | // if the target data are updated or the target pointer has changed |
219 | // if the target data are updated or the target pointer has changed |
170 | if ((pTargetPos->Status != PROCESSED) || (pTargetPos != pLastTargetPos) ) |
220 | if ((pTargetPos->Status != PROCESSED) || (pTargetPos != pLastTargetPos) ) |
171 | { |
221 | { |
172 | // reset error integral |
222 | // reset error integral |
173 | GPSPosDevIntegral_North = 0; |
223 | GPSPosDevIntegral_North = 0; |
174 | GPSPosDevIntegral_East = 0; |
224 | GPSPosDevIntegral_East = 0; |
175 | // recalculate latitude projection |
225 | // recalculate latitude projection |
176 | cos_target_latitude = (int32_t)c_cos_8192((int16_t)(pTargetPos->Latitude/10000000L)); |
226 | cos_target_latitude = (int32_t)c_cos_8192((int16_t)(pTargetPos->Latitude/10000000L)); |
177 | // remember last target pointer |
227 | // remember last target pointer |
178 | pLastTargetPos = pTargetPos; |
228 | pLastTargetPos = pTargetPos; |
179 | // mark data as processed |
229 | // mark data as processed |
180 | pTargetPos->Status = PROCESSED; |
230 | pTargetPos->Status = PROCESSED; |
181 | } |
231 | } |
182 | // calculate position deviation from latitude and longitude differences |
232 | // calculate position deviation from latitude and longitude differences |
183 | GPSPosDev_North = (GPSInfo.latitude - pTargetPos->Latitude); // to calculate real cm we would need *111/100 additionally |
233 | GPSPosDev_North = (GPSInfo.latitude - pTargetPos->Latitude); // to calculate real cm we would need *111/100 additionally |
184 | GPSPosDev_East = (GPSInfo.longitude - pTargetPos->Longitude); // to calculate real cm we would need *111/100 additionally |
234 | GPSPosDev_East = (GPSInfo.longitude - pTargetPos->Longitude); // to calculate real cm we would need *111/100 additionally |
185 | // calculate latitude projection |
235 | // calculate latitude projection |
186 | GPSPosDev_East *= cos_target_latitude; |
236 | GPSPosDev_East *= cos_target_latitude; |
187 | GPSPosDev_East /= 8192; |
237 | GPSPosDev_East /= 8192; |
188 | } |
238 | } |
189 | else // no valid target position available |
239 | else // no valid target position available |
190 | { |
240 | { |
191 | // reset error |
241 | // reset error |
192 | GPSPosDev_North = 0; |
242 | GPSPosDev_North = 0; |
193 | GPSPosDev_East = 0; |
243 | GPSPosDev_East = 0; |
194 | // reset error integral |
244 | // reset error integral |
195 | GPSPosDevIntegral_North = 0; |
245 | GPSPosDevIntegral_North = 0; |
196 | GPSPosDevIntegral_East = 0; |
246 | GPSPosDevIntegral_East = 0; |
197 | } |
247 | } |
198 | } |
248 | } |
199 | else // no target position available |
249 | else // no target position available |
200 | { |
250 | { |
201 | // reset error |
251 | // reset error |
202 | GPSPosDev_North = 0; |
252 | GPSPosDev_North = 0; |
203 | GPSPosDev_East = 0; |
253 | GPSPosDev_East = 0; |
204 | // reset error integral |
254 | // reset error integral |
205 | GPSPosDevIntegral_North = 0; |
255 | GPSPosDevIntegral_North = 0; |
206 | GPSPosDevIntegral_East = 0; |
256 | GPSPosDevIntegral_East = 0; |
207 | } |
257 | } |
208 | 258 | ||
209 | //Calculate PID-components of the controller |
259 | //Calculate PID-components of the controller |
210 | 260 | ||
211 | // D-Part |
261 | // D-Part |
212 | D_North = ((int32_t)FCParam.NaviGpsD * GPSInfo.velnorth)/512; |
262 | D_North = ((int32_t)FCParam.NaviGpsD * GPSInfo.velnorth)/512; |
213 | D_East = ((int32_t)FCParam.NaviGpsD * GPSInfo.veleast)/512; |
263 | D_East = ((int32_t)FCParam.NaviGpsD * GPSInfo.veleast)/512; |
214 | 264 | ||
215 | // P-Part |
265 | // P-Part |
216 | P_North = ((int32_t)FCParam.NaviGpsP * GPSPosDev_North)/2048; |
266 | P_North = ((int32_t)FCParam.NaviGpsP * GPSPosDev_North)/2048; |
217 | P_East = ((int32_t)FCParam.NaviGpsP * GPSPosDev_East)/2048; |
267 | P_East = ((int32_t)FCParam.NaviGpsP * GPSPosDev_East)/2048; |
218 | 268 | ||
219 | // I-Part |
269 | // I-Part |
220 | I_North = ((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_North)/8192; |
270 | I_North = ((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_North)/8192; |
221 | I_East = ((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_East)/8192; |
271 | I_East = ((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_East)/8192; |
222 | 272 | ||
223 | 273 | ||
224 | // combine P & I |
274 | // combine P & I |
225 | PID_North = P_North + I_North; |
275 | PID_North = P_North + I_North; |
226 | PID_East = P_East + I_East; |
276 | PID_East = P_East + I_East; |
227 | if(!GPS_LimitXY(&PID_North, &PID_East, GPS_P_LIMIT)) |
277 | if(!GPS_LimitXY(&PID_North, &PID_East, GPS_P_LIMIT)) |
228 | { |
278 | { |
229 | GPSPosDevIntegral_North += GPSPosDev_North/16; |
279 | GPSPosDevIntegral_North += GPSPosDev_North/16; |
230 | GPSPosDevIntegral_East += GPSPosDev_East/16; |
280 | GPSPosDevIntegral_East += GPSPosDev_East/16; |
231 | GPS_LimitXY(&GPSPosDevIntegral_North, &GPSPosDevIntegral_North, GPS_POSINTEGRAL_LIMIT); |
281 | GPS_LimitXY(&GPSPosDevIntegral_North, &GPSPosDevIntegral_North, GPS_POSINTEGRAL_LIMIT); |
232 | } |
282 | } |
233 | 283 | ||
234 | // combine PI- and D-Part |
284 | // combine PI- and D-Part |
235 | PID_North += D_North; |
285 | PID_North += D_North; |
236 | PID_East += D_East; |
286 | PID_East += D_East; |
237 | 287 | ||
238 | 288 | ||
239 | // scale combination with gain. |
289 | // scale combination with gain. |
240 | PID_North = (PID_North * (int32_t)FCParam.NaviGpsGain) / 100; |
290 | PID_North = (PID_North * (int32_t)FCParam.NaviGpsGain) / 100; |
241 | PID_East = (PID_East * (int32_t)FCParam.NaviGpsGain) / 100; |
291 | PID_East = (PID_East * (int32_t)FCParam.NaviGpsGain) / 100; |
242 | 292 | ||
243 | // GPS to nick and roll settings |
293 | // GPS to nick and roll settings |
244 | 294 | ||
245 | // A positive nick angle moves head downwards (flying forward). |
295 | // A positive nick angle moves head downwards (flying forward). |
246 | // A positive roll angle tilts left side downwards (flying left). |
296 | // A positive roll angle tilts left side downwards (flying left). |
247 | // If compass heading is 0 the head of the copter is in north direction. |
297 | // If compass heading is 0 the head of the copter is in north direction. |
248 | // A positive nick angle will fly to north and a positive roll angle will fly to west. |
298 | // A positive nick angle will fly to north and a positive roll angle will fly to west. |
249 | // In case of a positive north deviation/velocity the |
299 | // In case of a positive north deviation/velocity the |
250 | // copter should fly to south (negative nick). |
300 | // copter should fly to south (negative nick). |
251 | // In case of a positive east position deviation and a positive east velocity the |
301 | // In case of a positive east position deviation and a positive east velocity the |
252 | // copter should fly to west (positive roll). |
302 | // copter should fly to west (positive roll). |
253 | // The influence of the GPS_Nick and GPS_Roll variable is contrarily to the stick values |
303 | // The influence of the GPSStickNick and GPSStickRoll variable is contrarily to the stick values |
254 | // in the fc.c. Therefore a positive north deviation/velocity should result in a positive |
304 | // in the fc.c. Therefore a positive north deviation/velocity should result in a positive |
255 | // GPS_Nick and a positive east deviation/velocity should result in a negative GPS_Roll. |
305 | // GPSStickNick and a positive east deviation/velocity should result in a negative GPSStickRoll. |
256 | 306 | ||
257 | coscompass = (int32_t)c_cos_8192(YawGyroHeading / YAW_GYRO_DEG_FACTOR); |
307 | coscompass = (int32_t)c_cos_8192(YawGyroHeading / YAW_GyroDegFactor); |
258 | sincompass = (int32_t)c_sin_8192(YawGyroHeading / YAW_GYRO_DEG_FACTOR); |
308 | sincompass = (int32_t)c_sin_8192(YawGyroHeading / YAW_GyroDegFactor); |
259 | PID_Nick = (coscompass * PID_North + sincompass * PID_East) / 8192; |
309 | PID_Nick = (coscompass * PID_North + sincompass * PID_East) / 8192; |
260 | PID_Roll = (sincompass * PID_North - coscompass * PID_East) / 8192; |
310 | PID_Roll = (sincompass * PID_North - coscompass * PID_East) / 8192; |
261 | 311 | ||
262 | 312 | ||
263 | // limit resulting GPS control vector |
313 | // limit resulting GPS control vector |
264 | GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT); |
314 | GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT); |
265 | 315 | ||
266 | GPS_Nick = (int16_t)PID_Nick; |
316 | GPSStickNick = (int16_t)PID_Nick; |
267 | GPS_Roll = (int16_t)PID_Roll; |
317 | GPSStickRoll = (int16_t)PID_Roll; |
268 | } |
318 | } |
269 | else // invalid GPS data or bad compass reading |
319 | else // invalid GPS data or bad compass reading |
270 | { |
320 | { |
271 | GPS_Neutral(); // do nothing |
321 | GPS_Neutral(); // do nothing |
272 | // reset error integral |
322 | // reset error integral |
273 | GPSPosDevIntegral_North = 0; |
323 | GPSPosDevIntegral_North = 0; |
274 | GPSPosDevIntegral_East = 0; |
324 | GPSPosDevIntegral_East = 0; |
275 | } |
325 | } |
276 | } |
326 | } |
277 | 327 | ||
278 | 328 | ||
279 | 329 | ||
280 | 330 | ||
281 | void GPS_Main(void) |
331 | void GPS_Main(void) |
282 | { |
332 | { |
283 | static uint8_t GPS_P_Delay = 0; |
333 | static uint8_t GPS_P_Delay = 0; |
284 | static uint16_t beep_rythm = 0; |
334 | static uint16_t beep_rythm = 0; |
285 | 335 | ||
286 | GPS_UpdateParameter(); |
336 | GPS_UpdateParameter(); |
287 | 337 | ||
288 | // store home position if start of flight flag is set |
338 | // store home position if start of flight flag is set |
289 | if(MKFlags & MKFLAG_CALIBRATE) |
339 | if(MKFlags & MKFLAG_CALIBRATE) |
290 | { |
340 | { |
291 | if(GPS_SetCurrPosition(&HomePosition)) BeepTime = 700; |
341 | if(GPS_SetCurrPosition(&HomePosition)) BeepTime = 700; |
292 | } |
342 | } |
293 | 343 | ||
294 | switch(GPSInfo.status) |
344 | switch(GPSInfo.status) |
295 | { |
345 | { |
296 | case INVALID: // invalid gps data |
346 | case INVALID: // invalid gps data |
297 | GPS_Neutral(); |
347 | GPS_Neutral(); |
298 | if(FlightMode != GPS_FLIGHT_MODE_FREE) |
348 | if(FlightMode != GPS_FLIGHT_MODE_FREE) |
299 | { |
349 | { |
300 | BeepTime = 100; // beep if signal is neccesary |
350 | BeepTime = 100; // beep if signal is neccesary |
301 | } |
351 | } |
302 | break; |
352 | break; |
303 | case PROCESSED: // if gps data are already processed do nothing |
353 | case PROCESSED: // if gps data are already processed do nothing |
304 | // downcount timeout |
354 | // downcount timeout |
305 | if(GPSTimeout) GPSTimeout--; |
355 | if(GPSTimeout) GPSTimeout--; |
306 | // if no new data arrived within timeout set current data invalid |
356 | // if no new data arrived within timeout set current data invalid |
307 | // and therefore disable GPS |
357 | // and therefore disable GPS |
308 | else |
358 | else |
309 | { |
359 | { |
310 | GPS_Neutral(); |
360 | GPS_Neutral(); |
311 | GPSInfo.status = INVALID; |
361 | GPSInfo.status = INVALID; |
312 | } |
362 | } |
313 | break; |
363 | break; |
314 | case NEWDATA: // new valid data from gps device |
364 | case NEWDATA: // new valid data from gps device |
315 | // if the gps data quality is good |
365 | // if the gps data quality is good |
316 | beep_rythm++; |
366 | beep_rythm++; |
317 | 367 | ||
318 | if (GPS_IsSignalOK()) |
368 | if (GPS_IsSignalOK()) |
319 | { |
369 | { |
320 | switch(FlightMode) // check what's to do |
370 | switch(FlightMode) // check what's to do |
321 | { |
371 | { |
322 | case GPS_FLIGHT_MODE_FREE: |
372 | case GPS_FLIGHT_MODE_FREE: |
323 | // update hold position to current gps position |
373 | // update hold position to current gps position |
324 | GPS_SetCurrPosition(&HoldPosition); // can get invalid if gps signal is bad |
374 | GPS_SetCurrPosition(&HoldPosition); // can get invalid if gps signal is bad |
325 | // disable gps control |
375 | // disable gps control |
326 | GPS_Neutral(); |
376 | GPS_Neutral(); |
327 | break; |
377 | break; |
328 | 378 | ||
329 | case GPS_FLIGHT_MODE_AID: |
379 | case GPS_FLIGHT_MODE_AID: |
330 | if(HoldPosition.Status != INVALID) |
380 | if(HoldPosition.Status != INVALID) |
331 | { |
381 | { |
332 | if( GPS_IsManualControlled() ) // MK controlled by user |
382 | if( GPS_IsManualControlled() ) // MK controlled by user |
333 | { |
383 | { |
334 | // update hold point to current gps position |
384 | // update hold point to current gps position |
335 | GPS_SetCurrPosition(&HoldPosition); |
385 | GPS_SetCurrPosition(&HoldPosition); |
336 | // disable gps control |
386 | // disable gps control |
337 | GPS_Neutral(); |
387 | GPS_Neutral(); |
338 | GPS_P_Delay = 0; |
388 | GPS_P_Delay = 0; |
339 | } |
389 | } |
340 | else // GPS control active |
390 | else // GPS control active |
341 | { |
391 | { |
342 | if(GPS_P_Delay < 7) |
392 | if(GPS_P_Delay < 7) |
343 | { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s) |
393 | { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s) |
344 | GPS_P_Delay++; |
394 | GPS_P_Delay++; |
345 | GPS_SetCurrPosition(&HoldPosition); // update hold point to current gps position |
395 | GPS_SetCurrPosition(&HoldPosition); // update hold point to current gps position |
346 | GPS_PIDController(NULL); // activates only the D-Part |
396 | GPS_PIDController(NULL); // activates only the D-Part |
347 | } |
397 | } |
348 | else GPS_PIDController(&HoldPosition);// activates the P&D-Part |
398 | else GPS_PIDController(&HoldPosition);// activates the P&D-Part |
349 | } |
399 | } |
350 | } |
400 | } |
351 | else // invalid Hold Position |
401 | else // invalid Hold Position |
352 | { // try to catch a valid hold position from gps data input |
402 | { // try to catch a valid hold position from gps data input |
353 | GPS_SetCurrPosition(&HoldPosition); |
403 | GPS_SetCurrPosition(&HoldPosition); |
354 | GPS_Neutral(); |
404 | GPS_Neutral(); |
355 | } |
405 | } |
356 | break; |
406 | break; |
357 | 407 | ||
358 | case GPS_FLIGHT_MODE_HOME: |
408 | case GPS_FLIGHT_MODE_HOME: |
359 | if(HomePosition.Status != INVALID) |
409 | if(HomePosition.Status != INVALID) |
360 | { |
410 | { |
361 | // update hold point to current gps position |
411 | // update hold point to current gps position |
362 | // to avoid a flight back if home comming is deactivated |
412 | // to avoid a flight back if home comming is deactivated |
363 | GPS_SetCurrPosition(&HoldPosition); |
413 | GPS_SetCurrPosition(&HoldPosition); |
364 | if( GPS_IsManualControlled() ) // MK controlled by user |
414 | if( GPS_IsManualControlled() ) // MK controlled by user |
365 | { |
415 | { |
366 | GPS_Neutral(); |
416 | GPS_Neutral(); |
367 | } |
417 | } |
368 | else // GPS control active |
418 | else // GPS control active |
369 | { |
419 | { |
370 | GPS_PIDController(&HomePosition); |
420 | GPS_PIDController(&HomePosition); |
371 | } |
421 | } |
372 | } |
422 | } |
373 | else // bad home position |
423 | else // bad home position |
374 | { |
424 | { |
375 | BeepTime = 50; // signal invalid home position |
425 | BeepTime = 50; // signal invalid home position |
376 | // try to hold at least the position as a fallback option |
426 | // try to hold at least the position as a fallback option |
377 | 427 | ||
378 | if (HoldPosition.Status != INVALID) |
428 | if (HoldPosition.Status != INVALID) |
379 | { |
429 | { |
380 | if( GPS_IsManualControlled() ) // MK controlled by user |
430 | if( GPS_IsManualControlled() ) // MK controlled by user |
381 | { |
431 | { |
382 | GPS_Neutral(); |
432 | GPS_Neutral(); |
383 | } |
433 | } |
384 | else // GPS control active |
434 | else // GPS control active |
385 | { |
435 | { |
386 | GPS_PIDController(&HoldPosition); |
436 | GPS_PIDController(&HoldPosition); |
387 | } |
437 | } |
388 | } |
438 | } |
389 | else |
439 | else |
390 | { // try to catch a valid hold position |
440 | { // try to catch a valid hold position |
391 | GPS_SetCurrPosition(&HoldPosition); |
441 | GPS_SetCurrPosition(&HoldPosition); |
392 | GPS_Neutral(); |
442 | GPS_Neutral(); |
393 | } |
443 | } |
394 | } |
444 | } |
395 | break; // eof TSK_HOME |
445 | break; // eof TSK_HOME |
396 | default: // unhandled task |
446 | default: // unhandled task |
397 | GPS_Neutral(); |
447 | GPS_Neutral(); |
398 | break; // eof default |
448 | break; // eof default |
399 | } // eof switch GPS_Task |
449 | } // eof switch GPS_Task |
400 | } // eof gps data quality is good |
450 | } // eof gps data quality is good |
401 | else // gps data quality is bad |
451 | else // gps data quality is bad |
402 | { // disable gps control |
452 | { // disable gps control |
403 | GPS_Neutral(); |
453 | GPS_Neutral(); |
404 | if(FlightMode != GPS_FLIGHT_MODE_FREE) |
454 | if(FlightMode != GPS_FLIGHT_MODE_FREE) |
405 | { |
455 | { |
406 | // beep if signal is not sufficient |
456 | // beep if signal is not sufficient |
407 | if(!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100; |
457 | if(!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100; |
408 | else if (GPSInfo.satnum < ParamSet.NaviGpsMinSat && !(beep_rythm % 5)) BeepTime = 10; |
458 | else if (GPSInfo.satnum < ParamSet.NaviGpsMinSat && !(beep_rythm % 5)) BeepTime = 10; |
409 | } |
459 | } |
410 | } |
460 | } |
411 | // set current data as processed to avoid further calculations on the same gps data |
461 | // set current data as processed to avoid further calculations on the same gps data |
412 | GPSInfo.status = PROCESSED; |
462 | GPSInfo.status = PROCESSED; |
413 | break; |
463 | break; |
414 | } // eof GPSInfo.status |
464 | } // eof GPSInfo.status |
415 | } |
465 | } |
416 | 466 | ||
417 | 467 |