Rev 130 | Rev 133 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 130 | Rev 131 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /*#######################################################################################*/ |
2 | /*#######################################################################################*/ |
3 | 3 | ||
4 | // IMPORTANT NOTE: |
4 | // IMPORTANT NOTE: |
5 | 5 | ||
6 | // This is only a dummy implementation for errorfree compiling of the NaviCtrl sources. |
6 | // This is only a dummy implementation for errorfree compiling of the NaviCtrl sources. |
7 | 7 | ||
8 | // The GPS navigation routines are NOT included ! |
8 | // The GPS navigation routines are NOT included ! |
9 | 9 | ||
10 | /*#######################################################################################*/ |
10 | /*#######################################################################################*/ |
11 | /*#######################################################################################*/ |
11 | /*#######################################################################################*/ |
12 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
12 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
13 | /*#######################################################################################*/ |
13 | /*#######################################################################################*/ |
14 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
14 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
15 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
15 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
16 | // + Nur für den privaten Gebrauch |
16 | // + Nur für den privaten Gebrauch |
17 | // + FOR NON COMMERCIAL USE ONLY |
17 | // + FOR NON COMMERCIAL USE ONLY |
18 | // + www.MikroKopter.com |
18 | // + www.MikroKopter.com |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
20 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
21 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
21 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
22 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
22 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
23 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
23 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
24 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
24 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
25 | // + Verkauf von Luftbildaufnahmen, usw. |
25 | // + Verkauf von Luftbildaufnahmen, usw. |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
27 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
28 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
28 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
30 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
31 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
31 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
32 | // + eindeutig als Ursprung verlinkt werden |
32 | // + eindeutig als Ursprung verlinkt werden |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
34 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
34 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
35 | // + Benutzung auf eigene Gefahr |
35 | // + Benutzung auf eigene Gefahr |
36 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
36 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
37 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
37 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
38 | // + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
38 | // + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
39 | // + mit unserer Zustimmung zulässig |
39 | // + mit unserer Zustimmung zulässig |
40 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
40 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
41 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
41 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
42 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
42 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
43 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
43 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
44 | // + this list of conditions and the following disclaimer. |
44 | // + this list of conditions and the following disclaimer. |
45 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
45 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
46 | // + from this software without specific prior written permission. |
46 | // + from this software without specific prior written permission. |
47 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
47 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
48 | // + for non-commercial use (directly or indirectly) |
48 | // + for non-commercial use (directly or indirectly) |
49 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
49 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
50 | // + with our written permission |
50 | // + with our written permission |
51 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
51 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
52 | // + clearly linked as origin |
52 | // + clearly linked as origin |
53 | // + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
53 | // + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
54 | // |
54 | // |
55 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
55 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
56 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
56 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
57 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
57 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
58 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
58 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
59 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
59 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
60 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
60 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
61 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
61 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
62 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
62 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
63 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
63 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
64 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
64 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
65 | // + POSSIBILITY OF SUCH DAMAGE. |
65 | // + POSSIBILITY OF SUCH DAMAGE. |
66 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
66 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
67 | #include <stdio.h> |
67 | #include <stdio.h> |
68 | #include <stdlib.h> |
68 | #include <stdlib.h> |
69 | #include <math.h> |
69 | #include <math.h> |
70 | #include "91x_lib.h" |
70 | #include "91x_lib.h" |
71 | #include "main.h" |
71 | #include "main.h" |
72 | #include "uart1.h" |
72 | #include "uart1.h" |
73 | #include "GPS.h" |
73 | #include "GPS.h" |
74 | #include "timer1.h" |
74 | #include "timer1.h" |
75 | #include "spi_slave.h" |
75 | #include "spi_slave.h" |
76 | #include "waypoints.h" |
76 | #include "waypoints.h" |
77 | #include "i2c.h" |
77 | #include "i2c.h" |
78 | 78 | ||
79 | 79 | ||
80 | #define M_PI_180 (M_PI / 180.0f) |
80 | #define M_PI_180 (M_PI / 180.0f) |
81 | #define GPS_UPDATETIME_MS 200 // 200ms is 5 Hz |
81 | #define GPS_UPDATETIME_MS 200 // 200ms is 5 Hz |
82 | typedef enum |
82 | typedef enum |
83 | { |
83 | { |
84 | GPS_FLIGHT_MODE_UNDEF, |
84 | GPS_FLIGHT_MODE_UNDEF, |
85 | GPS_FLIGHT_MODE_FREE, |
85 | GPS_FLIGHT_MODE_FREE, |
86 | GPS_FLIGHT_MODE_AID, |
86 | GPS_FLIGHT_MODE_AID, |
87 | GPS_FLIGHT_MODE_WAYPOINT |
87 | GPS_FLIGHT_MODE_WAYPOINT |
88 | } GPS_FlightMode_t; |
88 | } GPS_FlightMode_t; |
89 | 89 | ||
90 | typedef struct |
90 | typedef struct |
91 | { |
91 | { |
92 | float Gain; |
92 | float Gain; |
93 | float P; |
93 | float P; |
94 | float I; |
94 | float I; |
95 | float D; |
95 | float D; |
96 | float A; |
96 | float A; |
97 | float ACC; |
97 | float ACC; |
98 | s32 P_Limit; |
98 | s32 P_Limit; |
99 | s32 I_Limit; |
99 | s32 I_Limit; |
100 | s32 D_Limit; |
100 | s32 D_Limit; |
101 | s32 PID_Limit; |
101 | s32 PID_Limit; |
102 | u32 BrakingDuration; |
102 | u32 BrakingDuration; |
103 | u8 MinSat; |
103 | u8 MinSat; |
104 | s8 StickThreshold; |
104 | s8 StickThreshold; |
105 | float WindCorrection; |
105 | float WindCorrection; |
106 | float SpeedCompensation; |
106 | float SpeedCompensation; |
107 | s32 OperatingRadius; |
107 | s32 OperatingRadius; |
108 | GPS_FlightMode_t FlightMode; |
108 | GPS_FlightMode_t FlightMode; |
109 | } __attribute__((packed)) GPS_Parameter_t; |
109 | } __attribute__((packed)) GPS_Parameter_t; |
110 | 110 | ||
111 | typedef struct |
111 | typedef struct |
112 | { |
112 | { |
113 | u8 Status; // invalid, newdata, processed |
113 | u8 Status; // invalid, newdata, processed |
114 | s32 North; // in cm |
114 | s32 North; // in cm |
115 | s32 East; // in cm |
115 | s32 East; // in cm |
116 | s32 Bearing; // in deg |
116 | s32 Bearing; // in deg |
117 | s32 Distance; // in cm |
117 | s32 Distance; // in cm |
118 | } __attribute__((packed)) GPS_Deviation_t; |
118 | } __attribute__((packed)) GPS_Deviation_t; |
119 | GPS_Deviation_t CurrentTargetDeviation; // Deviation from Target |
119 | GPS_Deviation_t CurrentTargetDeviation; // Deviation from Target |
120 | GPS_Deviation_t CurrentHomeDeviation; // Deviation from Home |
120 | GPS_Deviation_t CurrentHomeDeviation; // Deviation from Home |
121 | GPS_Deviation_t TargetHomeDeviation; // Deviation from Target to Home |
121 | GPS_Deviation_t TargetHomeDeviation; // Deviation from Target to Home |
122 | 122 | ||
123 | GPS_Stick_t GPS_Stick; |
123 | GPS_Stick_t GPS_Stick; |
124 | GPS_Parameter_t GPS_Parameter; |
124 | GPS_Parameter_t GPS_Parameter; |
125 | 125 | ||
126 | // the gps reference positions |
126 | // the gps reference positions |
127 | GPS_Pos_t GPS_HoldPosition = {0,0,0, INVALID}; // the hold position |
127 | GPS_Pos_t GPS_HoldPosition = {0,0,0, INVALID}; // the hold position |
128 | GPS_Pos_t GPS_HomePosition = {0,0,0, INVALID}; // the home position |
128 | GPS_Pos_t GPS_HomePosition = {0,0,0, INVALID}; // the home position |
129 | GPS_Pos_t * GPS_pTargetPosition = NULL; // pointer to the actual target position |
129 | GPS_Pos_t * GPS_pTargetPosition = NULL; // pointer to the actual target position |
130 | u32 GPS_TargetRadius = 0; // catch radius for target area |
130 | u32 GPS_TargetRadius = 0; // catch radius for target area |
131 | Waypoint_t* GPS_pWaypoint = NULL; // pointer to the actual waypoint |
131 | Waypoint_t* GPS_pWaypoint = NULL; // pointer to the actual waypoint |
132 | 132 | ||
133 | //------------------------------------------------------------- |
133 | //------------------------------------------------------------- |
134 | // Update GPSParamter |
134 | // Update GPSParamter |
135 | void GPS_UpdateParameter(void) |
135 | void GPS_UpdateParameter(void) |
136 | { |
136 | { |
137 | static GPS_FlightMode_t FlightMode_Old = GPS_FLIGHT_MODE_UNDEF; |
137 | static GPS_FlightMode_t FlightMode_Old = GPS_FLIGHT_MODE_UNDEF; |
138 | // in case of bad receiving conditions |
138 | // in case of bad receiving conditions |
139 | if(FC.RC_Quality < 100) |
139 | if(FC.RC_Quality < 100) |
140 | { // set fixed parameter |
140 | { // set fixed parameter |
141 | GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_WAYPOINT; |
141 | GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_WAYPOINT; |
142 | GPS_Parameter.Gain = (float) 100; |
142 | GPS_Parameter.Gain = (float) 100; |
143 | GPS_Parameter.P = (float) 90; |
143 | GPS_Parameter.P = (float) 90; |
144 | GPS_Parameter.I = (float) 90; |
144 | GPS_Parameter.I = (float) 90; |
145 | GPS_Parameter.D = (float) 90; |
145 | GPS_Parameter.D = (float) 90; |
146 | GPS_Parameter.A = (float) 90; |
146 | GPS_Parameter.A = (float) 90; |
147 | GPS_Parameter.ACC = (float) 0; |
147 | GPS_Parameter.ACC = (float) 0; |
148 | GPS_Parameter.P_Limit = 90; |
148 | GPS_Parameter.P_Limit = 90; |
149 | GPS_Parameter.I_Limit = 90; |
149 | GPS_Parameter.I_Limit = 90; |
150 | GPS_Parameter.D_Limit = 90; |
150 | GPS_Parameter.D_Limit = 90; |
151 | GPS_Parameter.PID_Limit = 200; |
151 | GPS_Parameter.PID_Limit = 200; |
152 | GPS_Parameter.BrakingDuration = 0; |
152 | GPS_Parameter.BrakingDuration = 0; |
153 | GPS_Parameter.SpeedCompensation = (float) 30; |
153 | GPS_Parameter.SpeedCompensation = (float) 30; |
154 | GPS_Parameter.MinSat = 6; |
154 | GPS_Parameter.MinSat = 6; |
155 | GPS_Parameter.StickThreshold = 8; |
155 | GPS_Parameter.StickThreshold = 8; |
156 | GPS_Parameter.WindCorrection = 0.0; |
156 | GPS_Parameter.WindCorrection = 0.0; |
157 | GPS_Parameter.OperatingRadius = 0; // forces the aircraft to fly to home positon |
157 | GPS_Parameter.OperatingRadius = 0; // forces the aircraft to fly to home positon |
158 | 158 | ||
159 | } |
159 | } |
160 | else |
160 | else |
161 | { |
161 | { |
162 | // update parameter from FC |
162 | // update parameter from FC |
163 | if(StopNavigation) GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE; |
163 | if(StopNavigation) GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE; |
164 | else |
164 | else |
165 | { |
165 | { |
166 | if(Parameter.NaviGpsModeControl < 50) |
166 | if(Parameter.NaviGpsModeControl < 50) |
167 | { |
167 | { |
168 | GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE; |
168 | GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE; |
169 | NCFlags &= ~(NC_FLAG_PH | NC_FLAG_CH); |
169 | NCFlags &= ~(NC_FLAG_PH | NC_FLAG_CH); |
170 | NCFlags |= NC_FLAG_FREE; |
170 | NCFlags |= NC_FLAG_FREE; |
171 | } |
171 | } |
172 | else if(Parameter.NaviGpsModeControl < 180) |
172 | else if(Parameter.NaviGpsModeControl < 180) |
173 | { |
173 | { |
174 | GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_AID; |
174 | GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_AID; |
175 | NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_CH); |
175 | NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_CH); |
176 | NCFlags |= NC_FLAG_PH; |
176 | NCFlags |= NC_FLAG_PH; |
177 | } |
177 | } |
178 | else |
178 | else |
179 | { |
179 | { |
180 | GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_WAYPOINT; |
180 | GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_WAYPOINT; |
181 | NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_PH); |
181 | NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_PH); |
182 | NCFlags |= NC_FLAG_CH; |
182 | NCFlags |= NC_FLAG_CH; |
183 | } |
183 | } |
184 | } |
184 | } |
185 | GPS_Parameter.Gain = (float)Parameter.NaviGpsGain; |
185 | GPS_Parameter.Gain = (float)Parameter.NaviGpsGain; |
186 | GPS_Parameter.P = (float)Parameter.NaviGpsP; |
186 | GPS_Parameter.P = (float)Parameter.NaviGpsP; |
187 | GPS_Parameter.I = (float)Parameter.NaviGpsI; |
187 | GPS_Parameter.I = (float)Parameter.NaviGpsI; |
188 | GPS_Parameter.D = (float)Parameter.NaviGpsD; |
188 | GPS_Parameter.D = (float)Parameter.NaviGpsD; |
189 | GPS_Parameter.A = (float)Parameter.NaviGpsD; |
189 | GPS_Parameter.A = (float)Parameter.NaviGpsD; |
190 | GPS_Parameter.ACC = (float)Parameter.NaviGpsACC; |
190 | GPS_Parameter.ACC = (float)Parameter.NaviGpsACC; |
191 | GPS_Parameter.P_Limit = (s32)Parameter.NaviGpsPLimit; |
191 | GPS_Parameter.P_Limit = (s32)Parameter.NaviGpsPLimit; |
192 | GPS_Parameter.I_Limit = (s32)Parameter.NaviGpsILimit; |
192 | GPS_Parameter.I_Limit = (s32)Parameter.NaviGpsILimit; |
193 | GPS_Parameter.D_Limit = (s32)Parameter.NaviGpsDLimit; |
193 | GPS_Parameter.D_Limit = (s32)Parameter.NaviGpsDLimit; |
194 | GPS_Parameter.PID_Limit = 2* (u32)Parameter.NaviAngleLimitation; |
194 | GPS_Parameter.PID_Limit = 2* (u32)Parameter.NaviAngleLimitation; |
195 | GPS_Parameter.BrakingDuration = (u32)Parameter.NaviPH_LoginTime; |
195 | GPS_Parameter.BrakingDuration = (u32)Parameter.NaviPH_LoginTime; |
196 | GPS_Parameter.SpeedCompensation = (float)Parameter.NaviSpeedCompensation; |
196 | GPS_Parameter.SpeedCompensation = (float)Parameter.NaviSpeedCompensation; |
197 | GPS_Parameter.MinSat = (u8)Parameter.NaviGpsMinSat; |
197 | GPS_Parameter.MinSat = (u8)Parameter.NaviGpsMinSat; |
198 | GPS_Parameter.StickThreshold = (s8)Parameter.NaviStickThreshold; |
198 | GPS_Parameter.StickThreshold = (s8)Parameter.NaviStickThreshold; |
199 | GPS_Parameter.WindCorrection = (float)Parameter.NaviWindCorrection; |
199 | GPS_Parameter.WindCorrection = (float)Parameter.NaviWindCorrection; |
200 | GPS_Parameter.OperatingRadius = (s32)Parameter.NaviOperatingRadius * 100; // conversion of m to cm |
200 | GPS_Parameter.OperatingRadius = (s32)Parameter.NaviOperatingRadius * 100; // conversion of m to cm |
201 | } |
201 | } |
202 | // FlightMode changed? |
202 | // FlightMode changed? |
203 | if(GPS_Parameter.FlightMode != FlightMode_Old) |
203 | if(GPS_Parameter.FlightMode != FlightMode_Old) |
204 | { |
204 | { |
205 | BeepTime = 100; // beep to indicate that mode has been switched |
205 | BeepTime = 100; // beep to indicate that mode has been switched |
206 | NCFlags &= ~NC_FLAG_TARGET_REACHED; |
206 | NCFlags &= ~NC_FLAG_TARGET_REACHED; |
207 | // if the mode has changed to free |
207 | // if the mode has changed to free |
208 | if(GPS_Parameter.FlightMode == GPS_FLIGHT_MODE_FREE) WPList_Clear(); // clear WPList if mode has changed to Free |
208 | if(GPS_Parameter.FlightMode == GPS_FLIGHT_MODE_FREE) WPList_Clear(); // clear WPList if mode has changed to Free |
209 | } |
209 | } |
210 | FlightMode_Old = GPS_Parameter.FlightMode; |
210 | FlightMode_Old = GPS_Parameter.FlightMode; |
211 | } |
211 | } |
212 | 212 | ||
213 | //------------------------------------------------------------- |
213 | //------------------------------------------------------------- |
214 | // This function defines a good GPS signal condition |
214 | // This function defines a good GPS signal condition |
215 | u8 GPS_IsSignalOK(void) |
215 | u8 GPS_IsSignalOK(void) |
216 | { |
216 | { |
217 | if( (GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) && (GPSData.NumOfSats >= GPS_Parameter.MinSat)) return(1); |
217 | if( (GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) && (GPSData.NumOfSats >= GPS_Parameter.MinSat)) return(1); |
218 | else return(0); |
218 | else return(0); |
219 | } |
219 | } |
220 | 220 | ||
221 | //------------------------------------------------------------ |
221 | //------------------------------------------------------------ |
222 | // Checks for manual control action |
222 | // Checks for manual control action |
223 | u8 GPS_IsManuallyControlled(void) |
223 | u8 GPS_IsManuallyControlled(void) |
224 | { |
224 | { |
225 | if( ( (abs(FC.StickNick) > GPS_Parameter.StickThreshold) || (abs(FC.StickRoll) > GPS_Parameter.StickThreshold)) && (GPS_Parameter.StickThreshold > 0) && (FC.RC_Quality > 150) ) |
225 | if( ( (abs(FC.StickNick) > GPS_Parameter.StickThreshold) || (abs(FC.StickRoll) > GPS_Parameter.StickThreshold)) && (GPS_Parameter.StickThreshold > 0) && (FC.RC_Quality > 150) ) |
226 | { |
226 | { |
227 | NCFlags |= NC_FLAG_MANUAL_CONTROL; |
227 | NCFlags |= NC_FLAG_MANUAL_CONTROL; |
228 | return(1); |
228 | return(1); |
229 | } |
229 | } |
230 | else |
230 | else |
231 | { |
231 | { |
232 | NCFlags &= ~NC_FLAG_MANUAL_CONTROL; |
232 | NCFlags &= ~NC_FLAG_MANUAL_CONTROL; |
233 | return(0); |
233 | return(0); |
234 | } |
234 | } |
235 | } |
235 | } |
236 | 236 | ||
237 | //------------------------------------------------------------ |
237 | //------------------------------------------------------------ |
238 | // copy GPS position from source position to target position |
238 | // copy GPS position from source position to target position |
239 | u8 GPS_CopyPosition(GPS_Pos_t * pGPSPosSrc, GPS_Pos_t* pGPSPosTgt) |
239 | u8 GPS_CopyPosition(GPS_Pos_t * pGPSPosSrc, GPS_Pos_t* pGPSPosTgt) |
240 | { |
240 | { |
241 | u8 retval = 0; |
241 | u8 retval = 0; |
242 | if((pGPSPosSrc == NULL) || (pGPSPosTgt == NULL)) return(retval); // bad pointer |
242 | if((pGPSPosSrc == NULL) || (pGPSPosTgt == NULL)) return(retval); // bad pointer |
243 | // copy only valid positions |
243 | // copy only valid positions |
244 | if(pGPSPosSrc->Status != INVALID) |
244 | if(pGPSPosSrc->Status != INVALID) |
245 | { |
245 | { |
246 | // if the source GPS position is not invalid |
246 | // if the source GPS position is not invalid |
247 | pGPSPosTgt->Longitude = pGPSPosSrc->Longitude; |
247 | pGPSPosTgt->Longitude = pGPSPosSrc->Longitude; |
248 | pGPSPosTgt->Latitude = pGPSPosSrc->Latitude; |
248 | pGPSPosTgt->Latitude = pGPSPosSrc->Latitude; |
249 | pGPSPosTgt->Altitude = pGPSPosSrc->Altitude; |
249 | pGPSPosTgt->Altitude = pGPSPosSrc->Altitude; |
250 | pGPSPosTgt->Status = NEWDATA; // mark data in target position as new |
250 | pGPSPosTgt->Status = NEWDATA; // mark data in target position as new |
251 | retval = 1; |
251 | retval = 1; |
252 | } |
252 | } |
253 | return(retval); |
253 | return(retval); |
254 | } |
254 | } |
255 | 255 | ||
256 | //------------------------------------------------------------ |
256 | //------------------------------------------------------------ |
257 | // clear position data |
257 | // clear position data |
258 | u8 GPS_ClearPosition(GPS_Pos_t * pGPSPos) |
258 | u8 GPS_ClearPosition(GPS_Pos_t * pGPSPos) |
259 | { |
259 | { |
260 | u8 retval = FALSE; |
260 | u8 retval = FALSE; |
261 | if(pGPSPos == NULL) return(retval); // bad pointer |
261 | if(pGPSPos == NULL) return(retval); // bad pointer |
262 | else |
262 | else |
263 | { |
263 | { |
264 | pGPSPos->Longitude = 0; |
264 | pGPSPos->Longitude = 0; |
265 | pGPSPos->Latitude = 0; |
265 | pGPSPos->Latitude = 0; |
266 | pGPSPos->Altitude = 0; |
266 | pGPSPos->Altitude = 0; |
267 | pGPSPos->Status = INVALID; |
267 | pGPSPos->Status = INVALID; |
268 | retval = TRUE; |
268 | retval = TRUE; |
269 | } |
269 | } |
270 | return (retval); |
270 | return (retval); |
271 | } |
271 | } |
272 | 272 | ||
273 | 273 | ||
274 | //------------------------------------------------------------ |
274 | //------------------------------------------------------------ |
275 | void GPS_Neutral(void) |
275 | void GPS_Neutral(void) |
276 | { |
276 | { |
277 | GPS_Stick.Nick = 0; |
277 | GPS_Stick.Nick = 0; |
278 | GPS_Stick.Roll = 0; |
278 | GPS_Stick.Roll = 0; |
279 | GPS_Stick.Yaw = 0; |
279 | GPS_Stick.Yaw = 0; |
280 | } |
280 | } |
281 | 281 | ||
282 | //------------------------------------------------------------ |
282 | //------------------------------------------------------------ |
283 | void GPS_Init(void) |
283 | void GPS_Init(void) |
284 | { |
284 | { |
285 | UART1_PutString("\r\n GPS init..."); |
285 | UART1_PutString("\r\n GPS init..."); |
286 | UBX_Init(); |
286 | UBX_Init(); |
287 | GPS_Neutral(); |
287 | GPS_Neutral(); |
288 | GPS_ClearPosition(&GPS_HoldPosition); |
288 | GPS_ClearPosition(&GPS_HoldPosition); |
289 | GPS_ClearPosition(&GPS_HomePosition); |
289 | GPS_ClearPosition(&GPS_HomePosition); |
290 | GPS_pTargetPosition = NULL; |
290 | GPS_pTargetPosition = NULL; |
291 | WPList_Init(); |
291 | WPList_Init(); |
292 | GPS_pWaypoint = WPList_Begin(); |
292 | GPS_pWaypoint = WPList_Begin(); |
293 | GPS_UpdateParameter(); |
293 | GPS_UpdateParameter(); |
294 | UART1_PutString("ok"); |
294 | UART1_PutString("ok"); |
295 | } |
295 | } |
296 | 296 | ||
297 | //------------------------------------------------------------ |
297 | //------------------------------------------------------------ |
298 | // calculate the bearing to target position from its deviation |
298 | // calculate the bearing to target position from its deviation |
299 | s32 DirectionToTarget_N_E(float northdev, float eastdev) |
299 | s32 DirectionToTarget_N_E(float northdev, float eastdev) |
300 | { |
300 | { |
301 | s32 bearing; |
301 | s32 bearing; |
302 | bearing = (s32)(atan2(northdev, eastdev) / M_PI_180); |
302 | bearing = (s32)(atan2(northdev, eastdev) / M_PI_180); |
303 | bearing = (270L - bearing)%360L; |
303 | bearing = (270L - bearing)%360L; |
304 | return(bearing); |
304 | return(bearing); |
305 | } |
305 | } |
306 | 306 | ||
307 | 307 | ||
308 | //------------------------------------------------------------ |
308 | //------------------------------------------------------------ |
309 | // Rescale xy-vector length if length limit is violated |
309 | // Rescale xy-vector length if length limit is violated |
310 | // returns vector len after scaling |
310 | // returns vector len after scaling |
311 | s32 GPS_LimitXY(s32 *x, s32 *y, s32 limit) |
311 | s32 GPS_LimitXY(s32 *x, s32 *y, s32 limit) |
312 | { |
312 | { |
313 | s32 dist; |
313 | s32 dist; |
314 | 314 | ||
315 | dist = (s32)hypot(*x,*y); // the length of the vector |
315 | dist = (s32)hypot(*x,*y); // the length of the vector |
316 | if (dist > limit) |
316 | if (dist > limit) |
317 | // if vector length is larger than the given limit |
317 | // if vector length is larger than the given limit |
318 | { // scale vector compontents so that the length is cut off to limit |
318 | { // scale vector compontents so that the length is cut off to limit |
319 | *x = (s32)(( (double)(*x) * (double)limit ) / (double)dist); |
319 | *x = (s32)(( (double)(*x) * (double)limit ) / (double)dist); |
320 | *y = (s32)(( (double)(*y) * (double)limit ) / (double)dist); |
320 | *y = (s32)(( (double)(*y) * (double)limit ) / (double)dist); |
321 | dist = limit; |
321 | dist = limit; |
322 | } |
322 | } |
323 | return(dist); |
323 | return(dist); |
324 | } |
324 | } |
325 | 325 | ||
326 | //------------------------------------------------------------ |
326 | //------------------------------------------------------------ |
327 | // transform the integer deg into float radians |
327 | // transform the integer deg into float radians |
328 | inline double RadiansFromGPS(s32 deg) |
328 | inline double RadiansFromGPS(s32 deg) |
329 | { |
329 | { |
330 | return ((double)deg * 1e-7f * M_PI_180); // 1E-7 because deg is the value in ° * 1E7 |
330 | return ((double)deg * 1e-7f * M_PI_180); // 1E-7 because deg is the value in ° * 1E7 |
331 | } |
331 | } |
332 | 332 | ||
333 | //------------------------------------------------------------ |
333 | //------------------------------------------------------------ |
334 | // transform the integer deg into float deg |
334 | // transform the integer deg into float deg |
335 | inline double DegFromGPS(s32 deg) |
335 | inline double DegFromGPS(s32 deg) |
336 | { |
336 | { |
337 | return ((double)deg * 1e-7f); // 1E-7 because deg is the value in ° * 1E7 |
337 | return ((double)deg * 1e-7f); // 1E-7 because deg is the value in ° * 1E7 |
338 | } |
338 | } |
339 | 339 | ||
340 | //------------------------------------------------------------ |
340 | //------------------------------------------------------------ |
341 | // calculate the deviation from the current position to the target position |
341 | // calculate the deviation from the current position to the target position |
342 | u8 GPS_CalculateDeviation(GPS_Pos_t * pCurrentPos, GPS_Pos_t * pTargetPos, GPS_Deviation_t* pDeviationFromTarget) |
342 | u8 GPS_CalculateDeviation(GPS_Pos_t * pCurrentPos, GPS_Pos_t * pTargetPos, GPS_Deviation_t* pDeviationFromTarget) |
343 | { |
343 | { |
344 | double temp1, temp2; |
344 | double temp1, temp2; |
345 | // if given pointer is NULL |
345 | // if given pointer is NULL |
346 | if((pCurrentPos == NULL) || (pTargetPos == NULL)) goto baddata; |
346 | if((pCurrentPos == NULL) || (pTargetPos == NULL)) goto baddata; |
347 | // if positions are invalid |
347 | // if positions are invalid |
348 | if((pCurrentPos->Status == INVALID) || (pTargetPos->Status == INVALID)) goto baddata; |
348 | if((pCurrentPos->Status == INVALID) || (pTargetPos->Status == INVALID)) goto baddata; |
349 | 349 | ||
350 | // The deviation from the current to the target position along north and east direction is |
350 | // The deviation from the current to the target position along north and east direction is |
351 | // simple the lat/lon difference. To convert that angular deviation into an |
351 | // simple the lat/lon difference. To convert that angular deviation into an |
352 | // arc length the spherical projection has to be considered. |
352 | // arc length the spherical projection has to be considered. |
353 | // The mean earth radius is 6371km. Therfore the arc length per latitude degree |
353 | // The mean earth radius is 6371km. Therfore the arc length per latitude degree |
354 | // is always 6371km * 2 * Pi / 360deg = 111.2 km/deg. |
354 | // is always 6371km * 2 * Pi / 360deg = 111.2 km/deg. |
355 | // The arc length per longitude degree depends on the correspondig latitude and |
355 | // The arc length per longitude degree depends on the correspondig latitude and |
356 | // is 111.2km * cos(latitude). |
356 | // is 111.2km * cos(latitude). |
357 | 357 | ||
358 | // calculate the shortest longitude deviation from target |
358 | // calculate the shortest longitude deviation from target |
359 | temp1 = DegFromGPS(pCurrentPos->Longitude) - DegFromGPS(pTargetPos->Longitude); |
359 | temp1 = DegFromGPS(pCurrentPos->Longitude) - DegFromGPS(pTargetPos->Longitude); |
360 | // outside an angular difference of -180 deg ... +180 deg its shorter to go the other way around |
360 | // outside an angular difference of -180 deg ... +180 deg its shorter to go the other way around |
361 | // In our application we wont fly more than 20.000 km but along the date line this is important. |
361 | // In our application we wont fly more than 20.000 km but along the date line this is important. |
362 | if(temp1 > 180.0f) temp1 -= 360.0f; |
362 | if(temp1 > 180.0f) temp1 -= 360.0f; |
363 | else if (temp1 < -180.0f) temp1 += 360.0f; |
363 | else if (temp1 < -180.0f) temp1 += 360.0f; |
364 | temp1 *= cos((RadiansFromGPS(pTargetPos->Latitude) + RadiansFromGPS(pCurrentPos->Latitude))/2); |
364 | temp1 *= cos((RadiansFromGPS(pTargetPos->Latitude) + RadiansFromGPS(pCurrentPos->Latitude))/2); |
365 | // calculate latitude deviation from target |
365 | // calculate latitude deviation from target |
366 | // this is allways within -180 deg ... 180 deg |
366 | // this is allways within -180 deg ... 180 deg |
367 | temp2 = DegFromGPS(pCurrentPos->Latitude) - DegFromGPS(pTargetPos->Latitude); |
367 | temp2 = DegFromGPS(pCurrentPos->Latitude) - DegFromGPS(pTargetPos->Latitude); |
368 | // deviation from target position in cm |
368 | // deviation from target position in cm |
369 | // i.e. the distance to walk from the target in northern and eastern direction to reach the current position |
369 | // i.e. the distance to walk from the target in northern and eastern direction to reach the current position |
370 | 370 | ||
371 | pDeviationFromTarget->Status = INVALID; |
371 | pDeviationFromTarget->Status = INVALID; |
372 | pDeviationFromTarget->North = (s32)(11119492.7f * temp2); |
372 | pDeviationFromTarget->North = (s32)(11119492.7f * temp2); |
373 | pDeviationFromTarget->East = (s32)(11119492.7f * temp1); |
373 | pDeviationFromTarget->East = (s32)(11119492.7f * temp1); |
374 | // If the position deviation is small enough to neglect the earth curvature |
374 | // If the position deviation is small enough to neglect the earth curvature |
375 | // (this is for our application always fulfilled) the distance to target |
375 | // (this is for our application always fulfilled) the distance to target |
376 | // can be calculated by the pythagoras of north and east deviation. |
376 | // can be calculated by the pythagoras of north and east deviation. |
377 | pDeviationFromTarget->Distance = (s32)(11119492.7f * hypot(temp1, temp2)); |
377 | pDeviationFromTarget->Distance = (s32)(11119492.7f * hypot(temp1, temp2)); |
378 | if (pDeviationFromTarget->Distance == 0L) pDeviationFromTarget->Bearing = 0L; |
378 | if (pDeviationFromTarget->Distance == 0L) pDeviationFromTarget->Bearing = 0L; |
379 | else pDeviationFromTarget->Bearing = DirectionToTarget_N_E(temp2, temp1); |
379 | else pDeviationFromTarget->Bearing = DirectionToTarget_N_E(temp2, temp1); |
380 | pDeviationFromTarget->Status = NEWDATA; |
380 | pDeviationFromTarget->Status = NEWDATA; |
381 | return TRUE; |
381 | return TRUE; |
382 | 382 | ||
383 | baddata: |
383 | baddata: |
384 | pDeviationFromTarget->North = 0L; |
384 | pDeviationFromTarget->North = 0L; |
385 | pDeviationFromTarget->East = 0L; |
385 | pDeviationFromTarget->East = 0L; |
386 | pDeviationFromTarget->Distance = 0L; |
386 | pDeviationFromTarget->Distance = 0L; |
387 | pDeviationFromTarget->Bearing = 0L; |
387 | pDeviationFromTarget->Bearing = 0L; |
388 | pDeviationFromTarget->Status = INVALID; |
388 | pDeviationFromTarget->Status = INVALID; |
389 | return FALSE; |
389 | return FALSE; |
390 | } |
390 | } |
391 | 391 | ||
392 | //------------------------------------------------------------ |
392 | //------------------------------------------------------------ |
393 | void GPS_Navigation(void) |
393 | void GPS_Navigation(void) |
394 | { |
394 | { |
395 | static u32 beep_rythm; |
395 | static u32 beep_rythm; |
396 | static u32 GPSDataTimeout = 0; |
396 | static u32 GPSDataTimeout = 0; |
397 | float compassheading, sin_h, cos_h; |
397 | float compassheading, sin_h, cos_h; |
398 | 398 | ||
399 | // pointer to current target position |
399 | // pointer to current target position |
400 | static GPS_Pos_t * pTargetPositionOld = NULL; |
400 | static GPS_Pos_t * pTargetPositionOld = NULL; |
401 | static Waypoint_t* GPS_pWaypointOld = NULL; |
401 | static Waypoint_t* GPS_pWaypointOld = NULL; |
402 | 402 | ||
403 | static GPS_Pos_t RangedTargetPosition = {0,0,0, INVALID}; // the limited target position, this is derived from the target position with repect to the operating radius |
403 | static GPS_Pos_t RangedTargetPosition = {0,0,0, INVALID}; // the limited target position, this is derived from the target position with repect to the operating radius |
404 | static s32 OperatingRadiusOld = -1; |
404 | static s32 OperatingRadiusOld = -1; |
405 | static u32 WPTime = 0; |
405 | static u32 WPTime = 0; |
406 | 406 | ||
407 | 407 | ||
408 | 408 | ||
409 | // get current heading from the FC gyro compass heading |
409 | // get current heading from the FC gyro compass heading |
410 | if(abs(FC.StickYaw) > 20 || FromFlightCtrl.GyroHeading > 3600) compassheading = (float)I2C_Heading.Heading * M_PI_180; |
410 | if(abs(FC.StickYaw) > 20 || FromFlightCtrl.GyroHeading > 3600) compassheading = (float)I2C_Heading.Heading * M_PI_180; |
411 | else compassheading = ((float)FromFlightCtrl.GyroHeading * M_PI_180) / 10.0; |
411 | else compassheading = ((float)FromFlightCtrl.GyroHeading * M_PI_180) / 10.0; |
412 | 412 | ||
413 | sin_h = sin(compassheading); |
413 | sin_h = sin(compassheading); |
414 | cos_h = cos(compassheading); |
414 | cos_h = cos(compassheading); |
415 | 415 | ||
416 | 416 | ||
417 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
417 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
418 | //+ Check for new data from GPS-receiver |
418 | //+ Check for new data from GPS-receiver |
419 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
419 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
420 | switch(GPSData.Status) |
420 | switch(GPSData.Status) |
421 | { |
421 | { |
422 | case INVALID: // no gps data available |
422 | case INVALID: // no gps data available |
423 | // do nothing |
423 | // do nothing |
424 | GPS_Parameter.PID_Limit = 0; // disables PID output |
424 | GPS_Parameter.PID_Limit = 0; // disables PID output |
425 | break; |
425 | break; |
426 | 426 | ||
427 | case PROCESSED: // the current data have been allready processed |
427 | case PROCESSED: // the current data have been allready processed |
428 | // if no new data are available within the timeout switch to invalid state. |
428 | // if no new data are available within the timeout switch to invalid state. |
429 | if(CheckDelay(GPSDataTimeout)) GPSData.Status = INVALID; |
429 | if(CheckDelay(GPSDataTimeout)) GPSData.Status = INVALID; |
430 | // wait for new gps data |
430 | // wait for new gps data |
431 | break; |
431 | break; |
432 | 432 | ||
433 | case NEWDATA: // handle new gps data |
433 | case NEWDATA: // handle new gps data |
434 | 434 | ||
435 | // update GPS Parameter from FC-Data via SPI interface |
435 | // update GPS Parameter from FC-Data via SPI interface |
436 | GPS_UpdateParameter(); |
436 | GPS_UpdateParameter(); |
437 | 437 | ||
438 | // wait maximum of 3 times the normal data update time before data timemout |
438 | // wait maximum of 3 times the normal data update time before data timemout |
439 | GPSDataTimeout = SetDelay(3 * GPS_UPDATETIME_MS); |
439 | GPSDataTimeout = SetDelay(3 * GPS_UPDATETIME_MS); |
440 | beep_rythm++; |
440 | beep_rythm++; |
441 | 441 | ||
442 | // debug |
442 | // debug |
443 | DebugOut.Analog[21] = (s16)GPSData.Speed_North; |
443 | DebugOut.Analog[21] = (s16)GPSData.Speed_North; |
444 | DebugOut.Analog[22] = (s16)GPSData.Speed_East; |
444 | DebugOut.Analog[22] = (s16)GPSData.Speed_East; |
445 | DebugOut.Analog[31] = (s16)GPSData.NumOfSats; |
445 | DebugOut.Analog[31] = (s16)GPSData.NumOfSats; |
446 | 446 | ||
447 | // If GPS signal condition is sufficient for a reliable position measurement |
447 | // If GPS signal condition is sufficient for a reliable position measurement |
448 | if(GPS_IsSignalOK()) |
448 | if(GPS_IsSignalOK()) |
449 | { |
449 | { |
450 | // update home deviation info |
450 | // update home deviation info |
451 | GPS_CalculateDeviation(&(GPSData.Position), &GPS_HomePosition, &CurrentHomeDeviation); |
451 | GPS_CalculateDeviation(&(GPSData.Position), &GPS_HomePosition, &CurrentHomeDeviation); |
452 | 452 | ||
453 | // if the MK is starting or the home position is invalid then store the home position |
453 | // if the MK is starting or the home position is invalid then store the home position |
454 | if((FC.MKFlags & MKFLAG_START) || (GPS_HomePosition.Status == INVALID)) |
454 | if((FC.MKFlags & MKFLAG_START) || (GPS_HomePosition.Status == INVALID)) |
455 | { // try to update the home position from the current position |
455 | { // try to update the home position from the current position |
456 | if(GPS_CopyPosition(&(GPSData.Position), &GPS_HomePosition)) |
456 | if(GPS_CopyPosition(&(GPSData.Position), &GPS_HomePosition)) |
457 | { |
457 | { |
458 | BeepTime = 700; // beep on success |
458 | BeepTime = 700; // beep on success |
459 | GPS_CopyPosition(&GPS_HomePosition, &(NaviData.HomePosition)); |
459 | GPS_CopyPosition(&GPS_HomePosition, &(NaviData.HomePosition)); |
460 | } |
460 | } |
461 | GPS_pWaypoint = WPList_Begin(); // go to start of waypoint list, return NULL of the list is empty |
461 | GPS_pWaypoint = WPList_Begin(); // go to start of waypoint list, return NULL of the list is empty |
462 | } |
462 | } |
463 | 463 | ||
464 | /* The selected flight mode influences the target position pointer and therefore the behavior */ |
464 | /* The selected flight mode influences the target position pointer and therefore the behavior */ |
465 | 465 | ||
466 | // check for current flight mode and set the target pointer GPS_pTargetPosition respectively |
466 | // check for current flight mode and set the target pointer GPS_pTargetPosition respectively |
467 | switch(GPS_Parameter.FlightMode) |
467 | switch(GPS_Parameter.FlightMode) |
468 | { |
468 | { |
469 | // the GPS control is deactived |
469 | // the GPS control is deactived |
470 | case GPS_FLIGHT_MODE_FREE: |
470 | case GPS_FLIGHT_MODE_FREE: |
471 | 471 | ||
472 | GPS_Parameter.PID_Limit = 0; // disables PID output |
472 | GPS_Parameter.PID_Limit = 0; // disables PID output |
473 | // update hold position |
473 | // update hold position |
474 | GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); |
474 | GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); |
475 | // no target position |
475 | // no target position |
476 | GPS_pTargetPosition = NULL; |
476 | GPS_pTargetPosition = NULL; |
477 | GPS_TargetRadius = 0; |
477 | GPS_TargetRadius = 0; |
478 | break; |
478 | break; |
479 | 479 | ||
480 | // the GPS supports the position hold, if the pilot takes no action |
480 | // the GPS supports the position hold, if the pilot takes no action |
481 | case GPS_FLIGHT_MODE_AID: |
481 | case GPS_FLIGHT_MODE_AID: |
482 | 482 | ||
483 | if(GPS_IsManuallyControlled()) |
483 | if(GPS_IsManuallyControlled()) |
484 | { |
484 | { |
485 | GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active |
485 | GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active |
486 | GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); |
486 | GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); |
487 | GPS_pTargetPosition = NULL; |
487 | GPS_pTargetPosition = NULL; |
488 | GPS_TargetRadius = 0; |
488 | GPS_TargetRadius = 0; |
489 | } |
489 | } |
490 | else |
490 | else |
491 | { |
491 | { |
492 | #define PH_MOVE_THRESHOLD 8 |
492 | #define PH_MOVE_THRESHOLD 8 |
493 | if( ((abs(FC.StickNick) > PH_MOVE_THRESHOLD) || (abs(FC.StickRoll) > PH_MOVE_THRESHOLD) ) && (FC.RC_Quality > 150)) |
493 | if( ((abs(FC.StickNick) > PH_MOVE_THRESHOLD) || (abs(FC.StickRoll) > PH_MOVE_THRESHOLD) ) && (FC.RC_Quality > 150)) |
494 | { // indirect control by moving the hold position with rc sticks |
494 | { // indirect control by moving the hold position with rc sticks |
495 | // rc sticks have a range of +/-127 counts |
495 | // rc sticks have a range of +/-127 counts |
496 | // GPS Coordinates are in 1-E7 deg, i.e. 1 counts is 1E-7°/360°*40E+8cm = 1.11 cm |
496 | // GPS Coordinates are in 1-E7 deg, i.e. 1 counts is 1E-7°/360°*40E+8cm = 1.11 cm |
497 | // normal manual activtion threshold is 8 counts that results in a change of the hold position |
497 | // normal manual activtion threshold is 8 counts that results in a change of the hold position |
498 | // of min 8*1.11cm * 5Hz = 8.88 = 44.4cm/s an max 127*1.11cm *5Hz = 705cm/s |
498 | // of min 8*1.11cm * 5Hz = 8.88 = 44.4cm/s an max 127*1.11cm *5Hz = 705cm/s |
499 | GPS_HoldPosition.Latitude += (s32)(cos_h*FC.StickNick + sin_h*FC.StickRoll); |
499 | GPS_HoldPosition.Latitude += (s32)(cos_h*FC.StickNick + sin_h*FC.StickRoll); |
500 | GPS_HoldPosition.Longitude += (s32)((sin_h*FC.StickNick - cos_h*FC.StickRoll) / cos(RadiansFromGPS(GPS_HoldPosition.Latitude)) ); |
500 | GPS_HoldPosition.Longitude += (s32)((sin_h*FC.StickNick - cos_h*FC.StickRoll) / cos(RadiansFromGPS(GPS_HoldPosition.Latitude)) ); |
501 | GPS_HoldPosition.Status = NEWDATA; |
- | |
502 | } |
501 | } |
503 | GPS_pTargetPosition = &GPS_HoldPosition; |
502 | GPS_pTargetPosition = &GPS_HoldPosition; |
504 | GPS_TargetRadius = 100; // 1 meter |
503 | GPS_TargetRadius = 100; // 1 meter |
505 | } |
504 | } |
506 | break; |
505 | break; |
507 | 506 | ||
508 | // the GPS control is directed to a target position |
507 | // the GPS control is directed to a target position |
509 | // given by a waypoint or by the home position |
508 | // given by a waypoint or by the home position |
510 | case GPS_FLIGHT_MODE_WAYPOINT: |
509 | case GPS_FLIGHT_MODE_WAYPOINT: |
511 | 510 | ||
512 | if(GPS_IsManuallyControlled()) // the human pilot takes the action |
511 | if(GPS_IsManuallyControlled()) // the human pilot takes the action |
513 | { |
512 | { |
514 | GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active |
513 | GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active |
515 | GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); // update hold position |
514 | GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); // update hold position |
516 | GPS_pTargetPosition = NULL; // set target position invalid |
515 | GPS_pTargetPosition = NULL; // set target position invalid |
517 | GPS_TargetRadius = 0; |
516 | GPS_TargetRadius = 0; |
518 | } |
517 | } |
519 | else // no manual control -> gps position hold active |
518 | else // no manual control -> gps position hold active |
520 | { |
519 | { |
521 | // waypoint trigger logic |
520 | // waypoint trigger logic |
522 | if(GPS_pWaypoint != NULL) // pointer to waypoint exist |
521 | if(GPS_pWaypoint != NULL) // pointer to waypoint exist |
523 | { |
522 | { |
524 | if(GPS_pWaypoint->Position.Status == INVALID) // should never happen |
523 | if(GPS_pWaypoint->Position.Status == INVALID) // should never happen |
525 | { |
524 | { |
526 | GPS_pWaypoint = WPList_Next(); // goto to next WP |
525 | GPS_pWaypoint = WPList_Next(); // goto to next WP |
527 | BeepTime = 255; |
526 | BeepTime = 255; |
528 | } |
527 | } |
529 | else // waypoint position is valid |
528 | else // waypoint position is valid |
530 | { |
529 | { |
531 | // check if the pointer to the waypoint has been changed or the data have been updated |
530 | // check if the pointer to the waypoint has been changed or the data have been updated |
532 | if((GPS_pWaypoint != GPS_pWaypointOld) || (GPS_pWaypoint->Position.Status == NEWDATA)) |
531 | if((GPS_pWaypoint != GPS_pWaypointOld) || (GPS_pWaypoint->Position.Status == NEWDATA)) |
533 | { |
532 | { |
534 | GPS_pWaypointOld = GPS_pWaypoint; |
533 | GPS_pWaypointOld = GPS_pWaypoint; |
535 | } |
534 | } |
536 | // if WP has been reached once, wait hold time before trigger to next one |
535 | // if WP has been reached once, wait hold time before trigger to next one |
537 | if(NCFlags & NC_FLAG_TARGET_REACHED) |
536 | if(NCFlags & NC_FLAG_TARGET_REACHED) |
538 | { |
537 | { |
539 | /* ToDo: Adjust GPS_pWaypoint->Heading, GPS_pWaypoint->Event handling */ |
538 | /* ToDo: Adjust GPS_pWaypoint->Heading, GPS_pWaypoint->Event handling */ |
540 | if(CheckDelay(WPTime)) |
539 | if(CheckDelay(WPTime)) |
541 | { |
540 | { |
542 | GPS_pWaypoint = WPList_Next(); // goto to next waypoint, return NULL if end of list has been reached |
541 | GPS_pWaypoint = WPList_Next(); // goto to next waypoint, return NULL if end of list has been reached |
543 | if(GPS_pWaypoint == NULL) GPS_pWaypoint = WPList_Begin(); // reset WPList to begin |
542 | if(GPS_pWaypoint == NULL) GPS_pWaypoint = WPList_Begin(); // reset WPList to begin |
544 | NCFlags &= ~NC_FLAG_TARGET_REACHED; |
543 | NCFlags &= ~NC_FLAG_TARGET_REACHED; |
545 | } |
544 | } |
546 | } // EOF if(WPArrived) |
545 | } // EOF if(WPArrived) |
547 | else |
546 | else |
548 | { |
547 | { |
549 | WPTime = SetDelay(GPS_pWaypoint->HoldTime * 1000); // set hold time stamp |
548 | WPTime = SetDelay(GPS_pWaypoint->HoldTime * 1000); // set hold time stamp |
550 | } |
549 | } |
551 | } |
550 | } |
552 | } |
551 | } |
553 | else // pointer to waypoint does not exist |
552 | else // pointer to waypoint does not exist |
554 | { |
553 | { |
555 | // try to catch the first waypoint from the list |
554 | // try to catch the first waypoint from the list |
556 | GPS_pWaypoint = WPList_Begin(); |
555 | GPS_pWaypoint = WPList_Begin(); |
557 | } |
556 | } |
558 | // EOF waypoint trigger logic |
557 | // EOF waypoint trigger logic |
559 | 558 | ||
560 | if(GPS_pWaypoint != NULL) // Waypoint exist |
559 | if(GPS_pWaypoint != NULL) // Waypoint exist |
561 | { |
560 | { |
562 | // update the hold position |
561 | // update the hold position |
563 | GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); |
562 | GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); |
564 | GPS_pTargetPosition = &(GPS_pWaypoint->Position); |
563 | GPS_pTargetPosition = &(GPS_pWaypoint->Position); |
565 | GPS_TargetRadius = (s32)(GPS_pWaypoint->ToleranceRadius) * 100L; |
564 | GPS_TargetRadius = (s32)(GPS_pWaypoint->ToleranceRadius) * 100L; |
566 | 565 | ||
567 | } |
566 | } |
568 | else // no waypoint info available, i.e. the WPList is empty or the end of the list has been reached |
567 | else // no waypoint info available, i.e. the WPList is empty or the end of the list has been reached |
569 | { |
568 | { |
570 | // fly back to home postion |
569 | // fly back to home postion |
571 | if(GPS_HomePosition.Status == INVALID) |
570 | if(GPS_HomePosition.Status == INVALID) |
572 | { |
571 | { |
573 | GPS_pTargetPosition = &GPS_HoldPosition; // fall back to hold mode if home position is not available |
572 | GPS_pTargetPosition = &GPS_HoldPosition; // fall back to hold mode if home position is not available |
574 | GPS_TargetRadius = 100; |
573 | GPS_TargetRadius = 100; |
575 | BeepTime = 255; // beep to indicate missin home position |
574 | BeepTime = 255; // beep to indicate missin home position |
576 | } |
575 | } |
577 | else // the home position is valid |
576 | else // the home position is valid |
578 | { |
577 | { |
579 | // update the hold position |
578 | // update the hold position |
580 | GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); |
579 | GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); |
581 | // set target to home position |
580 | // set target to home position |
582 | GPS_pTargetPosition = &GPS_HomePosition; |
581 | GPS_pTargetPosition = &GPS_HomePosition; |
583 | GPS_TargetRadius = 100; |
582 | GPS_TargetRadius = 100; |
584 | } |
583 | } |
585 | } |
584 | } |
586 | } // EOF no manual control |
585 | } // EOF no manual control |
587 | break; |
586 | break; |
588 | 587 | ||
589 | case GPS_FLIGHT_MODE_UNDEF: |
588 | case GPS_FLIGHT_MODE_UNDEF: |
590 | default: |
589 | default: |
591 | GPS_Parameter.PID_Limit = 0; // disables PID output |
590 | GPS_Parameter.PID_Limit = 0; // disables PID output |
592 | // update hold position |
591 | // update hold position |
593 | GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); |
592 | GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); |
594 | // no target position |
593 | // no target position |
595 | GPS_pTargetPosition = NULL; |
594 | GPS_pTargetPosition = NULL; |
596 | GPS_TargetRadius = 0; |
595 | GPS_TargetRadius = 0; |
597 | break; |
596 | break; |
598 | 597 | ||
599 | }// EOF GPS Mode Handling |
598 | }// EOF GPS Mode Handling |
600 | 599 | ||
601 | 600 | ||
602 | /* Calculation of range target based on the real target */ |
601 | /* Calculation of range target based on the real target */ |
603 | 602 | ||
604 | // if no target position exist clear the ranged target position |
603 | // if no target position exist clear the ranged target position |
605 | if(GPS_pTargetPosition == NULL) GPS_ClearPosition(&RangedTargetPosition); |
604 | if(GPS_pTargetPosition == NULL) GPS_ClearPosition(&RangedTargetPosition); |
606 | else |
605 | else |
607 | { // if the target position has been changed or the value has been updated or the OperatingRadius has changed |
606 | { // if the target position has been changed or the value has been updated or the OperatingRadius has changed |
608 | if((GPS_pTargetPosition != pTargetPositionOld) || (GPS_pTargetPosition->Status == NEWDATA) || (GPS_Parameter.OperatingRadius != OperatingRadiusOld) ) |
607 | if((GPS_pTargetPosition != pTargetPositionOld) || (GPS_pTargetPosition->Status == NEWDATA) || (GPS_Parameter.OperatingRadius != OperatingRadiusOld) ) |
609 | { |
608 | { |
610 | BeepTime = 255; // beep to indicate setting of a new target position |
609 | BeepTime = 255; // beep to indicate setting of a new target position |
611 | NCFlags &= ~NC_FLAG_TARGET_REACHED; // clear target reached flag |
610 | NCFlags &= ~NC_FLAG_TARGET_REACHED; // clear target reached flag |
612 | // calculate deviation of new target position from home position |
611 | // calculate deviation of new target position from home position |
613 | if(GPS_CalculateDeviation(GPS_pTargetPosition, &GPS_HomePosition, &TargetHomeDeviation)) |
612 | if(GPS_CalculateDeviation(GPS_pTargetPosition, &GPS_HomePosition, &TargetHomeDeviation)) |
614 | { |
613 | { |
615 | // check distance from home position |
614 | // check distance from home position |
616 | if(TargetHomeDeviation.Distance > GPS_Parameter.OperatingRadius) |
615 | if(TargetHomeDeviation.Distance > GPS_Parameter.OperatingRadius) |
617 | { |
616 | { |
618 | //calculate ranged target position to be within the operation radius area |
617 | //calculate ranged target position to be within the operation radius area |
619 | NCFlags |= NC_FLAG_RANGE_LIMIT; |
618 | NCFlags |= NC_FLAG_RANGE_LIMIT; |
620 | 619 | ||
621 | TargetHomeDeviation.North = (s32)(((float)TargetHomeDeviation.North * (float)GPS_Parameter.OperatingRadius) / (float)TargetHomeDeviation.Distance); |
620 | TargetHomeDeviation.North = (s32)(((float)TargetHomeDeviation.North * (float)GPS_Parameter.OperatingRadius) / (float)TargetHomeDeviation.Distance); |
622 | TargetHomeDeviation.East = (s32)(((float)TargetHomeDeviation.East * (float)GPS_Parameter.OperatingRadius) / (float)TargetHomeDeviation.Distance); |
621 | TargetHomeDeviation.East = (s32)(((float)TargetHomeDeviation.East * (float)GPS_Parameter.OperatingRadius) / (float)TargetHomeDeviation.Distance); |
623 | TargetHomeDeviation.Distance = GPS_Parameter.OperatingRadius; |
622 | TargetHomeDeviation.Distance = GPS_Parameter.OperatingRadius; |
624 | 623 | ||
625 | RangedTargetPosition.Status = INVALID; |
624 | RangedTargetPosition.Status = INVALID; |
626 | RangedTargetPosition.Latitude = GPS_HomePosition.Latitude; |
625 | RangedTargetPosition.Latitude = GPS_HomePosition.Latitude; |
627 | RangedTargetPosition.Latitude += (s32)((float)TargetHomeDeviation.North / 1.11194927f); |
626 | RangedTargetPosition.Latitude += (s32)((float)TargetHomeDeviation.North / 1.11194927f); |
628 | RangedTargetPosition.Longitude = GPS_HomePosition.Longitude; |
627 | RangedTargetPosition.Longitude = GPS_HomePosition.Longitude; |
629 | RangedTargetPosition.Longitude += (s32)((float)TargetHomeDeviation.East / (1.11194927f * cos(RadiansFromGPS(GPS_HomePosition.Latitude))) ); |
628 | RangedTargetPosition.Longitude += (s32)((float)TargetHomeDeviation.East / (1.11194927f * cos(RadiansFromGPS(GPS_HomePosition.Latitude))) ); |
630 | RangedTargetPosition.Altitude = GPS_pTargetPosition->Altitude; |
629 | RangedTargetPosition.Altitude = GPS_pTargetPosition->Altitude; |
631 | RangedTargetPosition.Status = NEWDATA; |
630 | RangedTargetPosition.Status = NEWDATA; |
632 | } |
631 | } |
633 | else |
632 | else |
634 | { // the target is located within the operation radius area |
633 | { // the target is located within the operation radius area |
635 | // simple copy the loaction to the ranged target position |
634 | // simple copy the loaction to the ranged target position |
636 | GPS_CopyPosition(GPS_pTargetPosition, &RangedTargetPosition); |
635 | GPS_CopyPosition(GPS_pTargetPosition, &RangedTargetPosition); |
637 | NCFlags &= ~NC_FLAG_RANGE_LIMIT; |
636 | NCFlags &= ~NC_FLAG_RANGE_LIMIT; |
638 | } |
637 | } |
639 | } |
638 | } |
640 | else |
639 | else |
641 | { // deviation could not be determined |
640 | { // deviation could not be determined |
642 | GPS_ClearPosition(&RangedTargetPosition); |
641 | GPS_ClearPosition(&RangedTargetPosition); |
643 | } |
642 | } |
644 | GPS_pTargetPosition->Status = PROCESSED; // mark current target as processed! |
643 | GPS_pTargetPosition->Status = PROCESSED; // mark current target as processed! |
645 | } |
644 | } |
646 | } |
645 | } |
647 | OperatingRadiusOld = GPS_Parameter.OperatingRadius; |
646 | OperatingRadiusOld = GPS_Parameter.OperatingRadius; |
648 | // remember last target position pointer |
647 | // remember last target position pointer |
649 | pTargetPositionOld = GPS_pTargetPosition; |
648 | pTargetPositionOld = GPS_pTargetPosition; |
650 | 649 | ||
651 | /* Calculate position deviation from ranged target */ |
650 | /* Calculate position deviation from ranged target */ |
652 | 651 | ||
653 | // calculate deviation of current position to ranged target position in cm |
652 | // calculate deviation of current position to ranged target position in cm |
654 | if(GPS_CalculateDeviation(&(GPSData.Position), &RangedTargetPosition, &CurrentTargetDeviation)) |
653 | if(GPS_CalculateDeviation(&(GPSData.Position), &RangedTargetPosition, &CurrentTargetDeviation)) |
655 | { // set target reached flag of we once reached the target point |
654 | { // set target reached flag of we once reached the target point |
656 | if(!(NCFlags & NC_FLAG_TARGET_REACHED) && (CurrentTargetDeviation.Distance < GPS_TargetRadius)) |
655 | if(!(NCFlags & NC_FLAG_TARGET_REACHED) && (CurrentTargetDeviation.Distance < GPS_TargetRadius)) |
657 | { |
656 | { |
658 | NCFlags |= NC_FLAG_TARGET_REACHED; // set target reached flag |
657 | NCFlags |= NC_FLAG_TARGET_REACHED; // set target reached flag |
659 | } |
658 | } |
660 | // implement your control code here based |
659 | // implement your control code here based |
661 | // in the info available in the CurrentTargetDeviation, GPSData and FromFlightCtrl.GyroHeading |
660 | // in the info available in the CurrentTargetDeviation, GPSData and FromFlightCtrl.GyroHeading |
662 | GPS_Stick.Nick = 0; |
661 | GPS_Stick.Nick = 0; |
663 | GPS_Stick.Roll = 0; |
662 | GPS_Stick.Roll = 0; |
664 | GPS_Stick.Yaw = 0; |
663 | GPS_Stick.Yaw = 0; |
665 | } |
664 | } |
666 | else // deviation could not be calculated |
665 | else // deviation could not be calculated |
667 | { // do nothing on gps sticks! |
666 | { // do nothing on gps sticks! |
668 | GPS_Neutral(); |
667 | GPS_Neutral(); |
669 | NCFlags &= ~NC_FLAG_TARGET_REACHED; // clear target reached |
668 | NCFlags &= ~NC_FLAG_TARGET_REACHED; // clear target reached |
670 | } |
669 | } |
671 | 670 | ||
672 | }// eof if GPSSignal is OK |
671 | }// eof if GPSSignal is OK |
673 | else // GPSSignal not OK |
672 | else // GPSSignal not OK |
674 | { |
673 | { |
675 | GPS_Neutral(); |
674 | GPS_Neutral(); |
676 | // beep if signal is not sufficient |
675 | // beep if signal is not sufficient |
677 | if(GPS_Parameter.FlightMode != GPS_FLIGHT_MODE_FREE) |
676 | if(GPS_Parameter.FlightMode != GPS_FLIGHT_MODE_FREE) |
678 | { |
677 | { |
679 | if(!(GPSData.Flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100; |
678 | if(!(GPSData.Flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100; |
680 | else if (GPSData.NumOfSats < GPS_Parameter.MinSat && !(beep_rythm % 5)) BeepTime = 10; |
679 | else if (GPSData.NumOfSats < GPS_Parameter.MinSat && !(beep_rythm % 5)) BeepTime = 10; |
681 | } |
680 | } |
682 | } |
681 | } |
683 | GPSData.Status = PROCESSED; // mark as processed |
682 | GPSData.Status = PROCESSED; // mark as processed |
684 | break; |
683 | break; |
685 | } |
684 | } |
686 | 685 | ||
687 | DebugOut.Analog[6] = NCFlags; |
686 | DebugOut.Analog[6] = NCFlags; |
688 | DebugOut.Analog[27] = (s16)CurrentTargetDeviation.North; |
687 | DebugOut.Analog[27] = (s16)CurrentTargetDeviation.North; |
689 | DebugOut.Analog[28] = (s16)CurrentTargetDeviation.East; |
688 | DebugOut.Analog[28] = (s16)CurrentTargetDeviation.East; |
690 | DebugOut.Analog[29] = GPS_Stick.Nick; |
689 | DebugOut.Analog[29] = GPS_Stick.Nick; |
691 | DebugOut.Analog[30] = GPS_Stick.Roll; |
690 | DebugOut.Analog[30] = GPS_Stick.Roll; |
692 | 691 | ||
693 | // update navi data, send back to ground station |
692 | // update navi data, send back to ground station |
694 | GPS_CopyPosition(&(GPSData.Position), &(NaviData.CurrentPosition)); |
693 | GPS_CopyPosition(&(GPSData.Position), &(NaviData.CurrentPosition)); |
695 | GPS_CopyPosition(&RangedTargetPosition, &(NaviData.TargetPosition)); |
694 | GPS_CopyPosition(&RangedTargetPosition, &(NaviData.TargetPosition)); |
696 | GPS_CopyPosition(&GPS_HomePosition, &(NaviData.HomePosition)); |
695 | GPS_CopyPosition(&GPS_HomePosition, &(NaviData.HomePosition)); |
697 | NaviData.SatsInUse = GPSData.NumOfSats; |
696 | NaviData.SatsInUse = GPSData.NumOfSats; |
698 | NaviData.TargetPositionDeviation.Distance = (u16)CurrentTargetDeviation.Distance/10; // dm |
697 | NaviData.TargetPositionDeviation.Distance = (u16)CurrentTargetDeviation.Distance/10; // dm |
699 | NaviData.TargetPositionDeviation.Bearing = (s16)CurrentTargetDeviation.Bearing; |
698 | NaviData.TargetPositionDeviation.Bearing = (s16)CurrentTargetDeviation.Bearing; |
700 | NaviData.HomePositionDeviation.Distance = (u16)CurrentHomeDeviation.Distance/10; // dm |
699 | NaviData.HomePositionDeviation.Distance = (u16)CurrentHomeDeviation.Distance/10; // dm |
701 | NaviData.HomePositionDeviation.Bearing = (s16)CurrentHomeDeviation.Bearing; |
700 | NaviData.HomePositionDeviation.Bearing = (s16)CurrentHomeDeviation.Bearing; |
702 | NaviData.UBat = FC.UBat; |
701 | NaviData.UBat = FC.UBat; |
703 | NaviData.GroundSpeed = (u16)GPSData.Speed_Ground; |
702 | NaviData.GroundSpeed = (u16)GPSData.Speed_Ground; |
704 | NaviData.Heading = (s16)(GPSData.Heading/100000L); |
703 | NaviData.Heading = (s16)(GPSData.Heading/100000L); |
705 | NaviData.CompassHeading = (s16)FromFlightCtrl.GyroHeading/10; // in deg |
704 | NaviData.CompassHeading = (s16)FromFlightCtrl.GyroHeading/10; // in deg |
706 | NaviData.AngleNick = FromFlightCtrl.AngleNick / 10; // in deg |
705 | NaviData.AngleNick = FromFlightCtrl.AngleNick / 10; // in deg |
707 | NaviData.AngleRoll = FromFlightCtrl.AngleRoll / 10; // in deg |
706 | NaviData.AngleRoll = FromFlightCtrl.AngleRoll / 10; // in deg |
708 | NaviData.RC_Quality = FC.RC_Quality; |
707 | NaviData.RC_Quality = FC.RC_Quality; |
709 | NaviData.RC_RSSI = FC.RC_RSSI; |
708 | NaviData.RC_RSSI = FC.RC_RSSI; |
710 | NaviData.MKFlags = FC.MKFlags; |
709 | NaviData.MKFlags = FC.MKFlags; |
711 | NaviData.NCFlags = NCFlags; |
710 | NaviData.NCFlags = NCFlags; |
712 | NaviData.OperatingRadius = Parameter.NaviOperatingRadius; |
711 | NaviData.OperatingRadius = Parameter.NaviOperatingRadius; |
713 | NaviData.TopSpeed = (s16)GPSData.Speed_Top; // in cm/s |
712 | NaviData.TopSpeed = (s16)GPSData.Speed_Top; // in cm/s |
714 | NaviData.TargetHoldTime = (u8)(GetDelay(WPTime)/1000); // in s |
713 | NaviData.TargetHoldTime = (u8)(GetDelay(WPTime)/1000); // in s |
715 | //+++++++++++++++++++++++++++++++++++++++++++++++++++ |
714 | //+++++++++++++++++++++++++++++++++++++++++++++++++++ |
716 | return; |
715 | return; |
717 | } |
716 | } |
718 | 717 | ||
719 | 718 |