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