Rev 1227 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1227 | Rev 1237 | ||
---|---|---|---|
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 | #include <inttypes.h> |
51 | #include <inttypes.h> |
52 | #include <stdlib.h> |
52 | #include <stdlib.h> |
53 | #include "fc.h" |
53 | #include "fc.h" |
54 | #include "ubx.h" |
54 | #include "ubx.h" |
55 | #include "mymath.h" |
55 | #include "mymath.h" |
56 | #include "timer0.h" |
56 | #include "timer0.h" |
57 | #include "uart0.h" |
57 | #include "uart0.h" |
58 | #include "rc.h" |
58 | #include "rc.h" |
59 | #include "eeprom.h" |
59 | #include "eeprom.h" |
60 | 60 | ||
61 | typedef enum |
61 | typedef enum |
62 | { |
62 | { |
63 | GPS_FLIGHT_MODE_UNDEF, |
63 | GPS_FLIGHT_MODE_UNDEF, |
64 | GPS_FLIGHT_MODE_FREE, |
64 | GPS_FLIGHT_MODE_FREE, |
65 | GPS_FLIGHT_MODE_AID, |
65 | GPS_FLIGHT_MODE_AID, |
66 | GPS_FLIGHT_MODE_HOME, |
66 | GPS_FLIGHT_MODE_HOME, |
67 | } FlightMode_t; |
67 | } FlightMode_t; |
68 | 68 | ||
69 | #define GPS_POSINTEGRAL_LIMIT 32000 |
69 | #define GPS_POSINTEGRAL_LIMIT 32000 |
70 | #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 |
71 | #define GPS_P_LIMIT 25 |
71 | #define GPS_P_LIMIT 25 |
72 | 72 | ||
73 | 73 | ||
74 | typedef struct |
74 | typedef struct |
75 | { |
75 | { |
76 | int32_t Longitude; |
76 | int32_t Longitude; |
77 | int32_t Latitude; |
77 | int32_t Latitude; |
78 | int32_t Altitude; |
78 | int32_t Altitude; |
79 | Status_t Status; |
79 | Status_t Status; |
80 | } GPS_Pos_t; |
80 | } GPS_Pos_t; |
81 | 81 | ||
82 | // GPS coordinates for hold position |
82 | // GPS coordinates for hold position |
83 | GPS_Pos_t HoldPosition = {0,0,0,INVALID}; |
83 | GPS_Pos_t HoldPosition = {0,0,0,INVALID}; |
84 | // GPS coordinates for home position |
84 | // GPS coordinates for home position |
85 | GPS_Pos_t HomePosition = {0,0,0,INVALID}; |
85 | GPS_Pos_t HomePosition = {0,0,0,INVALID}; |
86 | // the current flight mode |
86 | // the current flight mode |
87 | FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF; |
87 | FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF; |
88 | 88 | ||
89 | 89 | ||
90 | // --------------------------------------------------------------------------------- |
90 | // --------------------------------------------------------------------------------- |
91 | void GPS_UpdateParameter(void) |
91 | void GPS_UpdateParameter(void) |
92 | { |
92 | { |
93 | static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF; |
93 | static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF; |
94 | 94 | ||
95 | if((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING)) |
95 | if((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING)) |
96 | { |
96 | { |
97 | FlightMode = GPS_FLIGHT_MODE_FREE; |
97 | FlightMode = GPS_FLIGHT_MODE_FREE; |
98 | } |
98 | } |
99 | else |
99 | else |
100 | { |
100 | { |
101 | if (FCParam.NaviGpsModeControl < 50) FlightMode = GPS_FLIGHT_MODE_AID; |
101 | if (FCParam.NaviGpsModeControl < 50) FlightMode = GPS_FLIGHT_MODE_AID; |
102 | else if(FCParam.NaviGpsModeControl < 180) FlightMode = GPS_FLIGHT_MODE_FREE; |
102 | else if(FCParam.NaviGpsModeControl < 180) FlightMode = GPS_FLIGHT_MODE_FREE; |
103 | else FlightMode = GPS_FLIGHT_MODE_HOME; |
103 | else FlightMode = GPS_FLIGHT_MODE_HOME; |
104 | } |
104 | } |
105 | if (FlightMode != FlightModeOld) |
105 | if (FlightMode != FlightModeOld) |
106 | { |
106 | { |
107 | BeepTime = 100; |
107 | BeepTime = 100; |
108 | } |
108 | } |
109 | FlightModeOld = FlightMode; |
109 | FlightModeOld = FlightMode; |
110 | } |
110 | } |
111 | 111 | ||
112 | 112 | ||
113 | 113 | ||
114 | // --------------------------------------------------------------------------------- |
114 | // --------------------------------------------------------------------------------- |
115 | // This function defines a good GPS signal condition |
115 | // This function defines a good GPS signal condition |
116 | uint8_t GPS_IsSignalOK(void) |
116 | uint8_t GPS_IsSignalOK(void) |
117 | { |
117 | { |
118 | static uint8_t GPSFix = 0; |
118 | static uint8_t GPSFix = 0; |
119 | 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)) |
120 | { |
120 | { |
121 | GPSFix = 1; |
121 | GPSFix = 1; |
122 | return(1); |
122 | return(1); |
123 | 123 | ||
124 | } |
124 | } |
125 | else return (0); |
125 | else return (0); |
126 | 126 | ||
127 | } |
127 | } |
128 | // --------------------------------------------------------------------------------- |
128 | // --------------------------------------------------------------------------------- |
129 | // rescale xy-vector length to limit |
129 | // rescale xy-vector length to limit |
130 | 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) |
131 | { |
131 | { |
132 | uint8_t retval = 0; |
132 | uint8_t retval = 0; |
133 | int32_t len; |
133 | int32_t len; |
134 | len = (int32_t)c_sqrt(*x * *x + *y * *y); |
134 | len = (int32_t)c_sqrt(*x * *x + *y * *y); |
135 | if (len > limit) |
135 | if (len > limit) |
136 | { |
136 | { |
137 | // normalize control vector components to the limit |
137 | // normalize control vector components to the limit |
138 | *x = (*x * limit) / len; |
138 | *x = (*x * limit) / len; |
139 | *y = (*y * limit) / len; |
139 | *y = (*y * limit) / len; |
140 | retval = 1; |
140 | retval = 1; |
141 | } |
141 | } |
142 | return(retval); |
142 | return(retval); |
143 | } |
143 | } |
144 | 144 | ||
145 | // checks nick and roll sticks for manual control |
145 | // checks nick and roll sticks for manual control |
146 | uint8_t GPS_IsManualControlled(void) |
146 | uint8_t GPS_IsManualControlled(void) |
147 | { |
147 | { |
148 | 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; |
149 | else return 1; |
149 | else return 1; |
150 | } |
150 | } |
151 | 151 | ||
152 | // set given position to current gps position |
152 | // set given position to current gps position |
153 | uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos) |
153 | uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos) |
154 | { |
154 | { |
155 | uint8_t retval = 0; |
155 | uint8_t retval = 0; |
156 | if(pGPSPos == NULL) return(retval); // bad pointer |
156 | if(pGPSPos == NULL) return(retval); // bad pointer |
157 | 157 | ||
158 | if(GPS_IsSignalOK()) |
158 | if(GPS_IsSignalOK()) |
159 | { // is GPS signal condition is fine |
159 | { // is GPS signal condition is fine |
160 | pGPSPos->Longitude = GPSInfo.longitude; |
160 | pGPSPos->Longitude = GPSInfo.longitude; |
161 | pGPSPos->Latitude = GPSInfo.latitude; |
161 | pGPSPos->Latitude = GPSInfo.latitude; |
162 | pGPSPos->Altitude = GPSInfo.altitude; |
162 | pGPSPos->Altitude = GPSInfo.altitude; |
163 | pGPSPos->Status = NEWDATA; |
163 | pGPSPos->Status = NEWDATA; |
164 | retval = 1; |
164 | retval = 1; |
165 | } |
165 | } |
166 | else |
166 | else |
167 | { // bad GPS signal condition |
167 | { // bad GPS signal condition |
168 | pGPSPos->Status = INVALID; |
168 | pGPSPos->Status = INVALID; |
169 | retval = 0; |
169 | retval = 0; |
170 | } |
170 | } |
171 | return(retval); |
171 | return(retval); |
172 | } |
172 | } |
173 | 173 | ||
174 | // clear position |
174 | // clear position |
175 | uint8_t GPS_ClearPosition(GPS_Pos_t * pGPSPos) |
175 | uint8_t GPS_ClearPosition(GPS_Pos_t * pGPSPos) |
176 | { |
176 | { |
177 | uint8_t retval = 0; |
177 | uint8_t retval = 0; |
178 | if(pGPSPos == NULL) return(retval); // bad pointer |
178 | if(pGPSPos == NULL) return(retval); // bad pointer |
179 | else |
179 | else |
180 | { |
180 | { |
181 | pGPSPos->Longitude = 0; |
181 | pGPSPos->Longitude = 0; |
182 | pGPSPos->Latitude = 0; |
182 | pGPSPos->Latitude = 0; |
183 | pGPSPos->Altitude = 0; |
183 | pGPSPos->Altitude = 0; |
184 | pGPSPos->Status = INVALID; |
184 | pGPSPos->Status = INVALID; |
185 | retval = 1; |
185 | retval = 1; |
186 | } |
186 | } |
187 | return (retval); |
187 | return (retval); |
188 | } |
188 | } |
189 | 189 | ||
190 | // disable GPS control sticks |
190 | // disable GPS control sticks |
191 | void GPS_Neutral(void) |
191 | void GPS_Neutral(void) |
192 | { |
192 | { |
193 | GPSStickNick = 0; |
193 | GPSStickNick = 0; |
194 | GPSStickRoll = 0; |
194 | GPSStickRoll = 0; |
195 | } |
195 | } |
196 | 196 | ||
197 | // 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 |
198 | // 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 |
199 | // then the P part of the controller is deactivated. |
199 | // then the P part of the controller is deactivated. |
200 | void GPS_PIDController(GPS_Pos_t *pTargetPos) |
200 | void GPS_PIDController(GPS_Pos_t *pTargetPos) |
201 | { |
201 | { |
202 | static int32_t PID_Nick, PID_Roll; |
202 | static int32_t PID_Nick, PID_Roll; |
203 | int32_t coscompass, sincompass; |
203 | int32_t coscompass, sincompass; |
204 | int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm |
204 | int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm |
205 | 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; |
206 | int32_t PID_North = 0, PID_East = 0; |
206 | int32_t PID_North = 0, PID_East = 0; |
207 | static int32_t cos_target_latitude = 1; |
207 | static int32_t cos_target_latitude = 1; |
208 | static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0; |
208 | static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0; |
209 | static GPS_Pos_t *pLastTargetPos = 0; |
209 | static GPS_Pos_t *pLastTargetPos = 0; |
210 | 210 | ||
211 | // if GPS data and Compass are ok |
211 | // if GPS data and Compass are ok |
212 | if( GPS_IsSignalOK() && (CompassHeading >= 0) ) |
212 | if( GPS_IsSignalOK() && (CompassHeading >= 0) ) |
213 | { |
213 | { |
214 | 214 | ||
215 | if(pTargetPos != NULL) // if there is a target position |
215 | if(pTargetPos != NULL) // if there is a target position |
216 | { |
216 | { |
217 | if(pTargetPos->Status != INVALID) // and the position data are valid |
217 | if(pTargetPos->Status != INVALID) // and the position data are valid |
218 | { |
218 | { |
219 | // 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 |
220 | if ((pTargetPos->Status != PROCESSED) || (pTargetPos != pLastTargetPos) ) |
220 | if ((pTargetPos->Status != PROCESSED) || (pTargetPos != pLastTargetPos) ) |
221 | { |
221 | { |
222 | // reset error integral |
222 | // reset error integral |
223 | GPSPosDevIntegral_North = 0; |
223 | GPSPosDevIntegral_North = 0; |
224 | GPSPosDevIntegral_East = 0; |
224 | GPSPosDevIntegral_East = 0; |
225 | // recalculate latitude projection |
225 | // recalculate latitude projection |
226 | 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)); |
227 | // remember last target pointer |
227 | // remember last target pointer |
228 | pLastTargetPos = pTargetPos; |
228 | pLastTargetPos = pTargetPos; |
229 | // mark data as processed |
229 | // mark data as processed |
230 | pTargetPos->Status = PROCESSED; |
230 | pTargetPos->Status = PROCESSED; |
231 | } |
231 | } |
232 | // calculate position deviation from latitude and longitude differences |
232 | // calculate position deviation from latitude and longitude differences |
233 | 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 |
234 | 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 |
235 | // calculate latitude projection |
235 | // calculate latitude projection |
236 | GPSPosDev_East *= cos_target_latitude; |
236 | GPSPosDev_East *= cos_target_latitude; |
237 | GPSPosDev_East /= 8192; |
237 | GPSPosDev_East /= 8192; |
238 | } |
238 | } |
239 | else // no valid target position available |
239 | else // no valid target position available |
240 | { |
240 | { |
241 | // reset error |
241 | // reset error |
242 | GPSPosDev_North = 0; |
242 | GPSPosDev_North = 0; |
243 | GPSPosDev_East = 0; |
243 | GPSPosDev_East = 0; |
244 | // reset error integral |
244 | // reset error integral |
245 | GPSPosDevIntegral_North = 0; |
245 | GPSPosDevIntegral_North = 0; |
246 | GPSPosDevIntegral_East = 0; |
246 | GPSPosDevIntegral_East = 0; |
247 | } |
247 | } |
248 | } |
248 | } |
249 | else // no target position available |
249 | else // no target position available |
250 | { |
250 | { |
251 | // reset error |
251 | // reset error |
252 | GPSPosDev_North = 0; |
252 | GPSPosDev_North = 0; |
253 | GPSPosDev_East = 0; |
253 | GPSPosDev_East = 0; |
254 | // reset error integral |
254 | // reset error integral |
255 | GPSPosDevIntegral_North = 0; |
255 | GPSPosDevIntegral_North = 0; |
256 | GPSPosDevIntegral_East = 0; |
256 | GPSPosDevIntegral_East = 0; |
257 | } |
257 | } |
258 | 258 | ||
259 | //Calculate PID-components of the controller |
259 | //Calculate PID-components of the controller |
260 | 260 | ||
261 | // D-Part |
261 | // D-Part |
262 | D_North = ((int32_t)FCParam.NaviGpsD * GPSInfo.velnorth)/512; |
262 | D_North = ((int32_t)FCParam.NaviGpsD * GPSInfo.velnorth)/512; |
263 | D_East = ((int32_t)FCParam.NaviGpsD * GPSInfo.veleast)/512; |
263 | D_East = ((int32_t)FCParam.NaviGpsD * GPSInfo.veleast)/512; |
264 | 264 | ||
265 | // P-Part |
265 | // P-Part |
266 | P_North = ((int32_t)FCParam.NaviGpsP * GPSPosDev_North)/2048; |
266 | P_North = ((int32_t)FCParam.NaviGpsP * GPSPosDev_North)/2048; |
267 | P_East = ((int32_t)FCParam.NaviGpsP * GPSPosDev_East)/2048; |
267 | P_East = ((int32_t)FCParam.NaviGpsP * GPSPosDev_East)/2048; |
268 | 268 | ||
269 | // I-Part |
269 | // I-Part |
270 | I_North = ((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_North)/8192; |
270 | I_North = ((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_North)/8192; |
271 | I_East = ((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_East)/8192; |
271 | I_East = ((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_East)/8192; |
272 | 272 | ||
273 | 273 | ||
274 | // combine P & I |
274 | // combine P & I |
275 | PID_North = P_North + I_North; |
275 | PID_North = P_North + I_North; |
276 | PID_East = P_East + I_East; |
276 | PID_East = P_East + I_East; |
277 | if(!GPS_LimitXY(&PID_North, &PID_East, GPS_P_LIMIT)) |
277 | if(!GPS_LimitXY(&PID_North, &PID_East, GPS_P_LIMIT)) |
278 | { |
278 | { |
279 | GPSPosDevIntegral_North += GPSPosDev_North/16; |
279 | GPSPosDevIntegral_North += GPSPosDev_North/16; |
280 | GPSPosDevIntegral_East += GPSPosDev_East/16; |
280 | GPSPosDevIntegral_East += GPSPosDev_East/16; |
281 | GPS_LimitXY(&GPSPosDevIntegral_North, &GPSPosDevIntegral_North, GPS_POSINTEGRAL_LIMIT); |
281 | GPS_LimitXY(&GPSPosDevIntegral_North, &GPSPosDevIntegral_East, GPS_POSINTEGRAL_LIMIT); |
282 | } |
282 | } |
283 | 283 | ||
284 | // combine PI- and D-Part |
284 | // combine PI- and D-Part |
285 | PID_North += D_North; |
285 | PID_North += D_North; |
286 | PID_East += D_East; |
286 | PID_East += D_East; |
287 | 287 | ||
288 | 288 | ||
289 | // scale combination with gain. |
289 | // scale combination with gain. |
290 | PID_North = (PID_North * (int32_t)FCParam.NaviGpsGain) / 100; |
290 | PID_North = (PID_North * (int32_t)FCParam.NaviGpsGain) / 100; |
291 | PID_East = (PID_East * (int32_t)FCParam.NaviGpsGain) / 100; |
291 | PID_East = (PID_East * (int32_t)FCParam.NaviGpsGain) / 100; |
292 | 292 | ||
293 | // GPS to nick and roll settings |
293 | // GPS to nick and roll settings |
294 | 294 | ||
295 | // A positive nick angle moves head downwards (flying forward). |
295 | // A positive nick angle moves head downwards (flying forward). |
296 | // A positive roll angle tilts left side downwards (flying left). |
296 | // A positive roll angle tilts left side downwards (flying left). |
297 | // 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. |
298 | // 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. |
299 | // In case of a positive north deviation/velocity the |
299 | // In case of a positive north deviation/velocity the |
300 | // copter should fly to south (negative nick). |
300 | // copter should fly to south (negative nick). |
301 | // 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 |
302 | // copter should fly to west (positive roll). |
302 | // copter should fly to west (positive roll). |
303 | // The influence of the GPSStickNick and GPSStickRoll variable is contrarily to the stick values |
303 | // The influence of the GPSStickNick and GPSStickRoll variable is contrarily to the stick values |
304 | // 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 |
305 | // GPSStickNick and a positive east deviation/velocity should result in a negative GPSStickRoll. |
305 | // GPSStickNick and a positive east deviation/velocity should result in a negative GPSStickRoll. |
306 | 306 | ||
307 | coscompass = (int32_t)c_cos_8192(YawGyroHeading / GYRO_DEG_FACTOR); |
307 | coscompass = (int32_t)c_cos_8192(YawGyroHeading / GYRO_DEG_FACTOR); |
308 | sincompass = (int32_t)c_sin_8192(YawGyroHeading / GYRO_DEG_FACTOR); |
308 | sincompass = (int32_t)c_sin_8192(YawGyroHeading / GYRO_DEG_FACTOR); |
309 | PID_Nick = (coscompass * PID_North + sincompass * PID_East) / 8192; |
309 | PID_Nick = (coscompass * PID_North + sincompass * PID_East) / 8192; |
310 | PID_Roll = (sincompass * PID_North - coscompass * PID_East) / 8192; |
310 | PID_Roll = (sincompass * PID_North - coscompass * PID_East) / 8192; |
311 | 311 | ||
312 | 312 | ||
313 | // limit resulting GPS control vector |
313 | // limit resulting GPS control vector |
314 | GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT); |
314 | GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT); |
315 | 315 | ||
316 | GPSStickNick = (int16_t)PID_Nick; |
316 | GPSStickNick = (int16_t)PID_Nick; |
317 | GPSStickRoll = (int16_t)PID_Roll; |
317 | GPSStickRoll = (int16_t)PID_Roll; |
318 | } |
318 | } |
319 | else // invalid GPS data or bad compass reading |
319 | else // invalid GPS data or bad compass reading |
320 | { |
320 | { |
321 | GPS_Neutral(); // do nothing |
321 | GPS_Neutral(); // do nothing |
322 | // reset error integral |
322 | // reset error integral |
323 | GPSPosDevIntegral_North = 0; |
323 | GPSPosDevIntegral_North = 0; |
324 | GPSPosDevIntegral_East = 0; |
324 | GPSPosDevIntegral_East = 0; |
325 | } |
325 | } |
326 | } |
326 | } |
327 | 327 | ||
328 | 328 | ||
329 | 329 | ||
330 | 330 | ||
331 | void GPS_Main(void) |
331 | void GPS_Main(void) |
332 | { |
332 | { |
333 | static uint8_t GPS_P_Delay = 0; |
333 | static uint8_t GPS_P_Delay = 0; |
334 | static uint16_t beep_rythm = 0; |
334 | static uint16_t beep_rythm = 0; |
335 | 335 | ||
336 | GPS_UpdateParameter(); |
336 | GPS_UpdateParameter(); |
337 | 337 | ||
338 | // store home position if start of flight flag is set |
338 | // store home position if start of flight flag is set |
339 | if(MKFlags & MKFLAG_CALIBRATE) |
339 | if(MKFlags & MKFLAG_CALIBRATE) |
340 | { |
340 | { |
341 | if(GPS_SetCurrPosition(&HomePosition)) BeepTime = 700; |
341 | if(GPS_SetCurrPosition(&HomePosition)) BeepTime = 700; |
342 | } |
342 | } |
343 | 343 | ||
344 | switch(GPSInfo.status) |
344 | switch(GPSInfo.status) |
345 | { |
345 | { |
346 | case INVALID: // invalid gps data |
346 | case INVALID: // invalid gps data |
347 | GPS_Neutral(); |
347 | GPS_Neutral(); |
348 | if(FlightMode != GPS_FLIGHT_MODE_FREE) |
348 | if(FlightMode != GPS_FLIGHT_MODE_FREE) |
349 | { |
349 | { |
350 | BeepTime = 100; // beep if signal is neccesary |
350 | BeepTime = 100; // beep if signal is neccesary |
351 | } |
351 | } |
352 | break; |
352 | break; |
353 | case PROCESSED: // if gps data are already processed do nothing |
353 | case PROCESSED: // if gps data are already processed do nothing |
354 | // downcount timeout |
354 | // downcount timeout |
355 | if(GPSTimeout) GPSTimeout--; |
355 | if(GPSTimeout) GPSTimeout--; |
356 | // if no new data arrived within timeout set current data invalid |
356 | // if no new data arrived within timeout set current data invalid |
357 | // and therefore disable GPS |
357 | // and therefore disable GPS |
358 | else |
358 | else |
359 | { |
359 | { |
360 | GPS_Neutral(); |
360 | GPS_Neutral(); |
361 | GPSInfo.status = INVALID; |
361 | GPSInfo.status = INVALID; |
362 | } |
362 | } |
363 | break; |
363 | break; |
364 | case NEWDATA: // new valid data from gps device |
364 | case NEWDATA: // new valid data from gps device |
365 | // if the gps data quality is good |
365 | // if the gps data quality is good |
366 | beep_rythm++; |
366 | beep_rythm++; |
367 | 367 | ||
368 | if (GPS_IsSignalOK()) |
368 | if (GPS_IsSignalOK()) |
369 | { |
369 | { |
370 | switch(FlightMode) // check what's to do |
370 | switch(FlightMode) // check what's to do |
371 | { |
371 | { |
372 | case GPS_FLIGHT_MODE_FREE: |
372 | case GPS_FLIGHT_MODE_FREE: |
373 | // update hold position to current gps position |
373 | // update hold position to current gps position |
374 | GPS_SetCurrPosition(&HoldPosition); // can get invalid if gps signal is bad |
374 | GPS_SetCurrPosition(&HoldPosition); // can get invalid if gps signal is bad |
375 | // disable gps control |
375 | // disable gps control |
376 | GPS_Neutral(); |
376 | GPS_Neutral(); |
377 | break; |
377 | break; |
378 | 378 | ||
379 | case GPS_FLIGHT_MODE_AID: |
379 | case GPS_FLIGHT_MODE_AID: |
380 | if(HoldPosition.Status != INVALID) |
380 | if(HoldPosition.Status != INVALID) |
381 | { |
381 | { |
382 | if( GPS_IsManualControlled() ) // MK controlled by user |
382 | if( GPS_IsManualControlled() ) // MK controlled by user |
383 | { |
383 | { |
384 | // update hold point to current gps position |
384 | // update hold point to current gps position |
385 | GPS_SetCurrPosition(&HoldPosition); |
385 | GPS_SetCurrPosition(&HoldPosition); |
386 | // disable gps control |
386 | // disable gps control |
387 | GPS_Neutral(); |
387 | GPS_Neutral(); |
388 | GPS_P_Delay = 0; |
388 | GPS_P_Delay = 0; |
389 | } |
389 | } |
390 | else // GPS control active |
390 | else // GPS control active |
391 | { |
391 | { |
392 | if(GPS_P_Delay < 7) |
392 | if(GPS_P_Delay < 7) |
393 | { // 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) |
394 | GPS_P_Delay++; |
394 | GPS_P_Delay++; |
395 | GPS_SetCurrPosition(&HoldPosition); // update hold point to current gps position |
395 | GPS_SetCurrPosition(&HoldPosition); // update hold point to current gps position |
396 | GPS_PIDController(NULL); // activates only the D-Part |
396 | GPS_PIDController(NULL); // activates only the D-Part |
397 | } |
397 | } |
398 | else GPS_PIDController(&HoldPosition);// activates the P&D-Part |
398 | else GPS_PIDController(&HoldPosition);// activates the P&D-Part |
399 | } |
399 | } |
400 | } |
400 | } |
401 | else // invalid Hold Position |
401 | else // invalid Hold Position |
402 | { // try to catch a valid hold position from gps data input |
402 | { // try to catch a valid hold position from gps data input |
403 | GPS_SetCurrPosition(&HoldPosition); |
403 | GPS_SetCurrPosition(&HoldPosition); |
404 | GPS_Neutral(); |
404 | GPS_Neutral(); |
405 | } |
405 | } |
406 | break; |
406 | break; |
407 | 407 | ||
408 | case GPS_FLIGHT_MODE_HOME: |
408 | case GPS_FLIGHT_MODE_HOME: |
409 | if(HomePosition.Status != INVALID) |
409 | if(HomePosition.Status != INVALID) |
410 | { |
410 | { |
411 | // update hold point to current gps position |
411 | // update hold point to current gps position |
412 | // to avoid a flight back if home comming is deactivated |
412 | // to avoid a flight back if home comming is deactivated |
413 | GPS_SetCurrPosition(&HoldPosition); |
413 | GPS_SetCurrPosition(&HoldPosition); |
414 | if( GPS_IsManualControlled() ) // MK controlled by user |
414 | if( GPS_IsManualControlled() ) // MK controlled by user |
415 | { |
415 | { |
416 | GPS_Neutral(); |
416 | GPS_Neutral(); |
417 | } |
417 | } |
418 | else // GPS control active |
418 | else // GPS control active |
419 | { |
419 | { |
420 | GPS_PIDController(&HomePosition); |
420 | GPS_PIDController(&HomePosition); |
421 | } |
421 | } |
422 | } |
422 | } |
423 | else // bad home position |
423 | else // bad home position |
424 | { |
424 | { |
425 | BeepTime = 50; // signal invalid home position |
425 | BeepTime = 50; // signal invalid home position |
426 | // try to hold at least the position as a fallback option |
426 | // try to hold at least the position as a fallback option |
427 | 427 | ||
428 | if (HoldPosition.Status != INVALID) |
428 | if (HoldPosition.Status != INVALID) |
429 | { |
429 | { |
430 | if( GPS_IsManualControlled() ) // MK controlled by user |
430 | if( GPS_IsManualControlled() ) // MK controlled by user |
431 | { |
431 | { |
432 | GPS_Neutral(); |
432 | GPS_Neutral(); |
433 | } |
433 | } |
434 | else // GPS control active |
434 | else // GPS control active |
435 | { |
435 | { |
436 | GPS_PIDController(&HoldPosition); |
436 | GPS_PIDController(&HoldPosition); |
437 | } |
437 | } |
438 | } |
438 | } |
439 | else |
439 | else |
440 | { // try to catch a valid hold position |
440 | { // try to catch a valid hold position |
441 | GPS_SetCurrPosition(&HoldPosition); |
441 | GPS_SetCurrPosition(&HoldPosition); |
442 | GPS_Neutral(); |
442 | GPS_Neutral(); |
443 | } |
443 | } |
444 | } |
444 | } |
445 | break; // eof TSK_HOME |
445 | break; // eof TSK_HOME |
446 | default: // unhandled task |
446 | default: // unhandled task |
447 | GPS_Neutral(); |
447 | GPS_Neutral(); |
448 | break; // eof default |
448 | break; // eof default |
449 | } // eof switch GPS_Task |
449 | } // eof switch GPS_Task |
450 | } // eof gps data quality is good |
450 | } // eof gps data quality is good |
451 | else // gps data quality is bad |
451 | else // gps data quality is bad |
452 | { // disable gps control |
452 | { // disable gps control |
453 | GPS_Neutral(); |
453 | GPS_Neutral(); |
454 | if(FlightMode != GPS_FLIGHT_MODE_FREE) |
454 | if(FlightMode != GPS_FLIGHT_MODE_FREE) |
455 | { |
455 | { |
456 | // beep if signal is not sufficient |
456 | // beep if signal is not sufficient |
457 | if(!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100; |
457 | if(!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100; |
458 | else if (GPSInfo.satnum < ParamSet.NaviGpsMinSat && !(beep_rythm % 5)) BeepTime = 10; |
458 | else if (GPSInfo.satnum < ParamSet.NaviGpsMinSat && !(beep_rythm % 5)) BeepTime = 10; |
459 | } |
459 | } |
460 | } |
460 | } |
461 | // 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 |
462 | GPSInfo.status = PROCESSED; |
462 | GPSInfo.status = PROCESSED; |
463 | break; |
463 | break; |
464 | } // eof GPSInfo.status |
464 | } // eof GPSInfo.status |
465 | } |
465 | } |
466 | 466 | ||
467 | 467 |