Rev 813 | Rev 828 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 813 | Rev 818 | ||
---|---|---|---|
1 | #include <inttypes.h> |
1 | #include <inttypes.h> |
2 | #include <stdlib.h> |
2 | #include <stdlib.h> |
3 | #include "fc.h" |
3 | #include "fc.h" |
4 | #include "ubx.h" |
4 | #include "ubx.h" |
5 | #include "mymath.h" |
5 | #include "mymath.h" |
6 | #include "timer0.h" |
6 | #include "timer0.h" |
7 | #include "uart.h" |
7 | #include "uart.h" |
8 | #include "rc.h" |
8 | #include "rc.h" |
9 | #include "eeprom.h" |
9 | #include "eeprom.h" |
10 | 10 | ||
11 | #define TSK_IDLE 0 |
11 | #define TSK_IDLE 0 |
12 | #define TSK_HOLD 1 |
12 | #define TSK_HOLD 1 |
13 | #define TSK_HOME 2 |
13 | #define TSK_HOME 2 |
14 | 14 | ||
15 | #define GPS_STICK_SENSE 20 // must be at least in a range where 90% of the trimming does not switch of the GPS function |
15 | #define GPS_STICK_SENSE 20 // must be at least in a range where 90% of the trimming does not switch of the GPS function |
16 | #define GPS_STICK_LIMIT 45 // limit of gps stick control to avoid critical flight attitudes |
16 | #define GPS_STICK_LIMIT 45 // limit of gps stick control to avoid critical flight attitudes |
17 | #define GPS_POSDEV_INTEGRAL_LIMIT 32000 // limit for the position error integral |
17 | #define GPS_POSDEV_INTEGRAL_LIMIT 32000 // limit for the position error integral |
18 | 18 | ||
19 | 19 | ||
20 | int16_t GPS_Pitch = 0, GPS_Roll = 0; |
20 | int16_t GPS_Pitch = 0, GPS_Roll = 0; |
21 | int32_t GPS_P_Factor = 0, GPS_I_Factor = 0, GPS_D_Factor = 0; |
21 | int32_t GPS_P_Factor = 0, GPS_I_Factor = 0, GPS_D_Factor = 0; |
22 | 22 | ||
23 | 23 | ||
24 | 24 | ||
25 | typedef struct |
25 | typedef struct |
26 | { |
26 | { |
27 | int32_t Longitude; |
27 | int32_t Longitude; |
28 | int32_t Latitude; |
28 | int32_t Latitude; |
29 | int32_t Altitude; |
29 | int32_t Altitude; |
30 | uint8_t Status; |
30 | uint8_t Status; |
31 | } GPS_Pos_t; |
31 | } GPS_Pos_t; |
32 | 32 | ||
33 | // GPS coordinates for hold position |
33 | // GPS coordinates for hold position |
34 | GPS_Pos_t HoldPosition = {0,0,0,INVALID}; |
34 | GPS_Pos_t HoldPosition = {0,0,0,INVALID}; |
35 | // GPS coordinates for home position |
35 | // GPS coordinates for home position |
36 | GPS_Pos_t HomePosition = {0,0,0,INVALID}; |
36 | GPS_Pos_t HomePosition = {0,0,0,INVALID}; |
37 | 37 | ||
38 | 38 | ||
39 | // --------------------------------------------------------------------------------- |
39 | // --------------------------------------------------------------------------------- |
40 | 40 | ||
41 | // checks pitch and roll sticks for manual control |
41 | // checks pitch and roll sticks for manual control |
42 | uint8_t IsManualControlled(void) |
42 | uint8_t IsManualControlled(void) |
43 | { |
43 | { |
44 | if ( (abs(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]]) < GPS_STICK_SENSE) && (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < GPS_STICK_SENSE)) return 0; |
44 | if ( (abs(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]]) < GPS_STICK_SENSE) && (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < GPS_STICK_SENSE)) return 0; |
45 | else return 1; |
45 | else return 1; |
46 | } |
46 | } |
47 | 47 | ||
48 | // set home position to current positon |
48 | // set home position to current positon |
49 | void GPS_SetHomePosition(void) |
49 | void GPS_SetHomePosition(void) |
50 | { |
50 | { |
51 | if( ((GPSInfo.status == VALID) || (GPSInfo.status == PROCESSED)) && GPSInfo.satfix == SATFIX_3D) |
51 | if( ((GPSInfo.status == VALID) || (GPSInfo.status == PROCESSED)) && GPSInfo.satfix == SATFIX_3D) |
52 | { |
52 | { |
53 | HomePosition.Longitude = GPSInfo.longitude; |
53 | HomePosition.Longitude = GPSInfo.longitude; |
54 | HomePosition.Latitude = GPSInfo.latitude; |
54 | HomePosition.Latitude = GPSInfo.latitude; |
55 | HomePosition.Altitude = GPSInfo.altitude; |
55 | HomePosition.Altitude = GPSInfo.altitude; |
56 | HomePosition.Status = VALID; |
56 | HomePosition.Status = VALID; |
57 | BeepTime = 1000; // signal if new home position was set |
57 | BeepTime = 1000; // signal if new home position was set |
58 | } |
58 | } |
59 | else |
59 | else |
60 | { |
60 | { |
61 | HomePosition.Status = INVALID; |
61 | HomePosition.Status = INVALID; |
62 | } |
62 | } |
63 | } |
63 | } |
64 | 64 | ||
65 | // set hold position to current positon |
65 | // set hold position to current positon |
66 | void GPS_SetHoldPosition(void) |
66 | void GPS_SetHoldPosition(void) |
67 | { |
67 | { |
68 | if( ((GPSInfo.status == VALID) || (GPSInfo.status == PROCESSED)) && GPSInfo.satfix == SATFIX_3D) |
68 | if( ((GPSInfo.status == VALID) || (GPSInfo.status == PROCESSED)) && GPSInfo.satfix == SATFIX_3D) |
69 | { |
69 | { |
70 | HoldPosition.Longitude = GPSInfo.longitude; |
70 | HoldPosition.Longitude = GPSInfo.longitude; |
71 | HoldPosition.Latitude = GPSInfo.latitude; |
71 | HoldPosition.Latitude = GPSInfo.latitude; |
72 | HoldPosition.Altitude = GPSInfo.altitude; |
72 | HoldPosition.Altitude = GPSInfo.altitude; |
73 | HoldPosition.Status = VALID; |
73 | HoldPosition.Status = VALID; |
74 | } |
74 | } |
75 | else |
75 | else |
76 | { |
76 | { |
77 | HoldPosition.Status = INVALID; |
77 | HoldPosition.Status = INVALID; |
78 | } |
78 | } |
79 | } |
79 | } |
80 | 80 | ||
81 | // clear home position |
81 | // clear home position |
82 | void GPS_ClearHomePosition(void) |
82 | void GPS_ClearHomePosition(void) |
83 | { |
83 | { |
84 | HomePosition.Status = INVALID; |
84 | HomePosition.Status = INVALID; |
85 | } |
85 | } |
86 | 86 | ||
87 | // disable GPS control sticks |
87 | // disable GPS control sticks |
88 | void GPS_Neutral(void) |
88 | void GPS_Neutral(void) |
89 | { |
89 | { |
90 | GPS_Pitch = 0; |
90 | GPS_Pitch = 0; |
91 | GPS_Roll = 0; |
91 | GPS_Roll = 0; |
92 | } |
92 | } |
93 | 93 | ||
94 | // calculates the GPS control stick values from the deviation to target position |
94 | // calculates the GPS control stick values from the deviation to target position |
95 | // if the pointer to the target positin is NULL or is the target position invalid |
95 | // if the pointer to the target positin is NULL or is the target position invalid |
96 | // then the P part of the controller is deactivated. |
96 | // then the P part of the controller is deactivated. |
97 | void GPS_PIDController(GPS_Pos_t *pTargetPos) |
97 | void GPS_PIDController(GPS_Pos_t *pTargetPos) |
98 | { |
98 | { |
99 | int32_t coscompass, sincompass; |
99 | int32_t coscompass, sincompass; |
100 | int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm |
100 | int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm |
101 | int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0; |
101 | int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0; |
102 | int32_t PID_North = 0, PID_East = 0; |
102 | int32_t PID_North = 0, PID_East = 0; |
103 | uint8_t GPS_Stick_Limited = 0; |
103 | uint8_t GPS_Stick_Limited = 0; |
104 | static int32_t cos_target_latitude = 1; |
104 | static int32_t cos_target_latitude = 1; |
105 | static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0; |
105 | static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0; |
106 | static GPS_Pos_t *pLastTargetPos = 0; |
106 | static GPS_Pos_t *pLastTargetPos = 0; |
107 | 107 | ||
108 | // if GPS data and Compass are ok |
108 | // if GPS data and Compass are ok |
109 | if((GPSInfo.status == VALID) && (GPSInfo.satfix == SATFIX_3D) && (CompassHeading >= 0) ) |
109 | if((GPSInfo.status == VALID) && (GPSInfo.satfix == SATFIX_3D) && (CompassHeading >= 0) ) |
110 | { |
110 | { |
111 | 111 | ||
112 | if(pTargetPos != NULL) // if there is a target position |
112 | if(pTargetPos != NULL) // if there is a target position |
113 | { |
113 | { |
114 | if(pTargetPos->Status != INVALID) // and the position data are valid |
114 | if(pTargetPos->Status != INVALID) // and the position data are valid |
115 | { |
115 | { |
116 | // if the target data are updated or the target pointer has changed |
116 | // if the target data are updated or the target pointer has changed |
117 | if ((pTargetPos->Status != PROCESSED) || (pTargetPos != pLastTargetPos) ) |
117 | if ((pTargetPos->Status != PROCESSED) || (pTargetPos != pLastTargetPos) ) |
118 | { |
118 | { |
119 | // reset error integral |
119 | // reset error integral |
120 | GPSPosDevIntegral_North = 0; |
120 | GPSPosDevIntegral_North = 0; |
121 | GPSPosDevIntegral_East = 0; |
121 | GPSPosDevIntegral_East = 0; |
122 | // recalculate latitude projection |
122 | // recalculate latitude projection |
123 | cos_target_latitude = (int32_t)c_cos_8192((int16_t)(pTargetPos->Latitude/10000000L)); |
123 | cos_target_latitude = (int32_t)c_cos_8192((int16_t)(pTargetPos->Latitude/10000000L)); |
124 | // remember last target pointer |
124 | // remember last target pointer |
125 | pLastTargetPos = pTargetPos; |
125 | pLastTargetPos = pTargetPos; |
126 | // mark data as processed |
126 | // mark data as processed |
127 | pTargetPos->Status = PROCESSED; |
127 | pTargetPos->Status = PROCESSED; |
128 | } |
128 | } |
129 | // calculate position deviation from latitude and longitude differences |
129 | // calculate position deviation from latitude and longitude differences |
130 | GPSPosDev_North = (GPSInfo.latitude - pTargetPos->Latitude); // to calculate real cm we would need *111/100 additionally |
130 | GPSPosDev_North = (GPSInfo.latitude - pTargetPos->Latitude); // to calculate real cm we would need *111/100 additionally |
131 | GPSPosDev_East = (GPSInfo.longitude - pTargetPos->Longitude); // to calculate real cm we would need *111/100 additionally |
131 | GPSPosDev_East = (GPSInfo.longitude - pTargetPos->Longitude); // to calculate real cm we would need *111/100 additionally |
132 | // calculate latitude projection |
132 | // calculate latitude projection |
133 | GPSPosDev_East *= cos_target_latitude; |
133 | GPSPosDev_East *= cos_target_latitude; |
134 | GPSPosDev_East /= 8192; |
134 | GPSPosDev_East /= 8192; |
135 | 135 | ||
136 | DebugOut.Analog[12] = GPSPosDev_North; |
136 | DebugOut.Analog[12] = GPSPosDev_North; |
137 | DebugOut.Analog[13] = GPSPosDev_East; |
137 | DebugOut.Analog[13] = GPSPosDev_East; |
138 | //DebugOut.Analog[12] = GPSInfo.velnorth; |
138 | //DebugOut.Analog[12] = GPSInfo.velnorth; |
139 | //DebugOut.Analog[13] = GPSInfo.veleast; |
139 | //DebugOut.Analog[13] = GPSInfo.veleast; |
140 | } |
140 | } |
141 | else // no valid target position available |
141 | else // no valid target position available |
142 | { |
142 | { |
143 | // reset error |
143 | // reset error |
144 | GPSPosDev_North = 0; |
144 | GPSPosDev_North = 0; |
145 | GPSPosDev_East = 0; |
145 | GPSPosDev_East = 0; |
146 | // reset error integral |
146 | // reset error integral |
147 | GPSPosDevIntegral_North = 0; |
147 | GPSPosDevIntegral_North = 0; |
148 | GPSPosDevIntegral_East = 0; |
148 | GPSPosDevIntegral_East = 0; |
149 | } |
149 | } |
150 | } |
150 | } |
151 | else // no target position available |
151 | else // no target position available |
152 | { |
152 | { |
153 | // reset error |
153 | // reset error |
154 | GPSPosDev_North = 0; |
154 | GPSPosDev_North = 0; |
155 | GPSPosDev_East = 0; |
155 | GPSPosDev_East = 0; |
156 | // reset error integral |
156 | // reset error integral |
157 | GPSPosDevIntegral_North = 0; |
157 | GPSPosDevIntegral_North = 0; |
158 | GPSPosDevIntegral_East = 0; |
158 | GPSPosDevIntegral_East = 0; |
159 | } |
159 | } |
160 | 160 | ||
161 | //Calculate PID-components of the controller (negative sign for compensation) |
161 | //Calculate PID-components of the controller (negative sign for compensation) |
162 | P_North = -(GPS_P_Factor * GPSPosDev_North)/2048; |
162 | P_North = -(GPS_P_Factor * GPSPosDev_North)/2048; |
163 | P_East = -(GPS_P_Factor * GPSPosDev_East)/2048; |
163 | P_East = -(GPS_P_Factor * GPSPosDev_East)/2048; |
164 | I_North = -(GPS_I_Factor * GPSPosDevIntegral_North)/2048; |
164 | I_North = -(GPS_I_Factor * GPSPosDevIntegral_North)/8192; |
165 | I_East = -(GPS_I_Factor * GPSPosDevIntegral_East)/2048; |
165 | I_East = -(GPS_I_Factor * GPSPosDevIntegral_East)/8192; |
166 | D_North = -(GPS_D_Factor * GPSInfo.velnorth)/512; |
166 | D_North = -(GPS_D_Factor * GPSInfo.velnorth)/512; |
167 | D_East = -(GPS_D_Factor * GPSInfo.veleast)/512; |
167 | D_East = -(GPS_D_Factor * GPSInfo.veleast)/512; |
168 | // PD-controller |
168 | // PD-controller |
169 | PID_North = P_North + I_North + D_North; |
169 | PID_North = P_North + I_North + D_North; |
170 | PID_East = P_East + I_East + D_East; |
170 | PID_East = P_East + I_East + D_East; |
171 | 171 | ||
172 | // GPS to pitch and roll settings |
172 | // GPS to pitch and roll settings |
173 | 173 | ||
174 | // A positive pitch angle moves head downwards (flying forward). |
174 | // A positive pitch angle moves head downwards (flying forward). |
175 | // A positive roll angle tilts left side downwards (flying left). |
175 | // A positive roll angle tilts left side downwards (flying left). |
176 | // If compass heading is 0 the head of the copter is in north direction. |
176 | // If compass heading is 0 the head of the copter is in north direction. |
177 | // A positive pitch angle will fly to north and a positive roll angle will fly to west. |
177 | // A positive pitch angle will fly to north and a positive roll angle will fly to west. |
178 | // In case of a positive north deviation/velocity the |
178 | // In case of a positive north deviation/velocity the |
179 | // copter should fly to south (negative pitch). |
179 | // copter should fly to south (negative pitch). |
180 | // In case of a positive east position deviation and a positive east velocity the |
180 | // In case of a positive east position deviation and a positive east velocity the |
181 | // copter should fly to west (positive roll). |
181 | // copter should fly to west (positive roll). |
182 | // The influence of the GPS_Pitch and GPS_Roll variable is contrarily to the stick values |
182 | // The influence of the GPS_Pitch and GPS_Roll variable is contrarily to the stick values |
183 | // in the fc.c. Therefore a positive north deviation/velocity should result in a positive |
183 | // in the fc.c. Therefore a positive north deviation/velocity should result in a positive |
184 | // GPS_Pitch and a positive east deviation/velocity should result in a negative GPS_Roll. |
184 | // GPS_Pitch and a positive east deviation/velocity should result in a negative GPS_Roll. |
185 | 185 | ||
186 | coscompass = (int32_t)c_cos_8192(CompassHeading); |
186 | coscompass = (int32_t)c_cos_8192(CompassHeading); |
187 | sincompass = (int32_t)c_sin_8192(CompassHeading); |
187 | sincompass = (int32_t)c_sin_8192(CompassHeading); |
188 | GPS_Roll = (int16_t)((coscompass * PID_East - sincompass * PID_North) / 8192); |
188 | GPS_Roll = (int16_t)((coscompass * PID_East - sincompass * PID_North) / 8192); |
189 | GPS_Pitch = -1*(int16_t)((sincompass * PID_East + coscompass * PID_North) / 8192); |
189 | GPS_Pitch = -1*(int16_t)((sincompass * PID_East + coscompass * PID_North) / 8192); |
190 | 190 | ||
191 | // limit GPS controls |
191 | // limit GPS controls |
192 | if (GPS_Pitch > GPS_STICK_LIMIT) |
192 | if (GPS_Pitch > GPS_STICK_LIMIT) |
193 | { |
193 | { |
194 | GPS_Pitch = GPS_STICK_LIMIT; |
194 | GPS_Pitch = GPS_STICK_LIMIT; |
195 | GPS_Stick_Limited = 1; |
195 | GPS_Stick_Limited = 1; |
196 | } |
196 | } |
197 | else if (GPS_Pitch < -GPS_STICK_LIMIT) |
197 | else if (GPS_Pitch < -GPS_STICK_LIMIT) |
198 | { |
198 | { |
199 | GPS_Pitch = -GPS_STICK_LIMIT; |
199 | GPS_Pitch = -GPS_STICK_LIMIT; |
200 | GPS_Stick_Limited = 1; |
200 | GPS_Stick_Limited = 1; |
201 | } |
201 | } |
202 | if (GPS_Roll > GPS_STICK_LIMIT) |
202 | if (GPS_Roll > GPS_STICK_LIMIT) |
203 | { |
203 | { |
204 | GPS_Roll = GPS_STICK_LIMIT; |
204 | GPS_Roll = GPS_STICK_LIMIT; |
205 | GPS_Stick_Limited = 1; |
205 | GPS_Stick_Limited = 1; |
206 | } |
206 | } |
207 | else if (GPS_Roll < -GPS_STICK_LIMIT) |
207 | else if (GPS_Roll < -GPS_STICK_LIMIT) |
208 | { |
208 | { |
209 | GPS_Roll = -GPS_STICK_LIMIT; |
209 | GPS_Roll = -GPS_STICK_LIMIT; |
210 | GPS_Stick_Limited = 1; |
210 | GPS_Stick_Limited = 1; |
211 | } |
211 | } |
212 | 212 | ||
213 | if(!GPS_Stick_Limited) // prevent further growing of error integrals if control limit is reached |
213 | if(!GPS_Stick_Limited) // prevent further growing of error integrals if control limit is reached |
214 | { |
214 | { |
215 | // calculate position error integrals |
215 | // calculate position error integrals |
216 | GPSPosDevIntegral_North += GPSPosDev_North/4; |
216 | GPSPosDevIntegral_North += GPSPosDev_North/16; |
217 | if( GPSPosDevIntegral_North > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = GPS_POSDEV_INTEGRAL_LIMIT; |
217 | if( GPSPosDevIntegral_North > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = GPS_POSDEV_INTEGRAL_LIMIT; |
218 | else if (GPSPosDevIntegral_North < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = -GPS_POSDEV_INTEGRAL_LIMIT; |
218 | else if (GPSPosDevIntegral_North < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = -GPS_POSDEV_INTEGRAL_LIMIT; |
219 | GPSPosDevIntegral_East += GPSPosDev_East/4; |
219 | GPSPosDevIntegral_East += GPSPosDev_East/16; |
220 | if( GPSPosDevIntegral_East > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = GPS_POSDEV_INTEGRAL_LIMIT; |
220 | if( GPSPosDevIntegral_East > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = GPS_POSDEV_INTEGRAL_LIMIT; |
221 | else if (GPSPosDevIntegral_East < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = -GPS_POSDEV_INTEGRAL_LIMIT; |
221 | else if (GPSPosDevIntegral_East < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = -GPS_POSDEV_INTEGRAL_LIMIT; |
222 | } |
222 | } |
223 | 223 | ||
224 | } |
224 | } |
225 | else // invalid GPS data |
225 | else // invalid GPS data or bad compass reading |
226 | { |
226 | { |
227 | GPS_Neutral(); // do nothing |
227 | GPS_Neutral(); // do nothing |
228 | // reset error integral |
228 | // reset error integral |
229 | GPSPosDevIntegral_North = 0; |
229 | GPSPosDevIntegral_North = 0; |
230 | GPSPosDevIntegral_East = 0; |
230 | GPSPosDevIntegral_East = 0; |
231 | } |
231 | } |
232 | } |
232 | } |
233 | 233 | ||
234 | 234 | ||
235 | 235 | ||
236 | 236 | ||
237 | void GPS_Main(uint8_t ctrl) |
237 | void GPS_Main(uint8_t ctrl) |
238 | { |
238 | { |
239 | static uint8_t GPS_Task = TSK_IDLE; |
239 | static uint8_t GPS_Task = TSK_IDLE; |
240 | static uint8_t GPS_P_Delay = 0; |
240 | static uint8_t GPS_P_Delay = 0; |
241 | int16_t satbeep; |
241 | int16_t satbeep; |
242 | 242 | ||
243 | // ctrl enables the gps feature |
243 | // ctrl enables the gps feature |
244 | if(ctrl < 70) GPS_Task = TSK_IDLE; |
244 | if(ctrl < 70) GPS_Task = TSK_IDLE; |
245 | else if (ctrl < 160) GPS_Task = TSK_HOLD; |
245 | else if (ctrl < 160) GPS_Task = TSK_HOLD; |
246 | else GPS_Task = TSK_HOME; // ctrl >= 160 |
246 | else GPS_Task = TSK_HOME; // ctrl >= 160 |
247 | 247 | ||
248 | 248 | ||
249 | switch(GPSInfo.status) |
249 | switch(GPSInfo.status) |
250 | { |
250 | { |
251 | case INVALID: // invalid gps data |
251 | case INVALID: // invalid gps data |
252 | GPS_Neutral(); |
252 | GPS_Neutral(); |
253 | if(GPS_Task != TSK_IDLE) |
253 | if(GPS_Task != TSK_IDLE) |
254 | { |
254 | { |
255 | BeepTime = 100; // beep if signal is neccesary |
255 | BeepTime = 100; // beep if signal is neccesary |
256 | } |
256 | } |
257 | break; |
257 | break; |
258 | case PROCESSED: // if gps data are already processed do nothing |
258 | case PROCESSED: // if gps data are already processed do nothing |
259 | // downcount timeout |
259 | // downcount timeout |
260 | if(GPSTimeout) GPSTimeout--; |
260 | if(GPSTimeout) GPSTimeout--; |
261 | // if no new data arrived within timeout set current data invalid |
261 | // if no new data arrived within timeout set current data invalid |
262 | // and therefore disable GPS |
262 | // and therefore disable GPS |
263 | else |
263 | else |
264 | { |
264 | { |
265 | GPS_Neutral(); |
265 | GPS_Neutral(); |
266 | GPSInfo.status = INVALID; |
266 | GPSInfo.status = INVALID; |
267 | } |
267 | } |
268 | break; |
268 | break; |
269 | case VALID: // new valid data from gps device |
269 | case VALID: // new valid data from gps device |
270 | // if the gps data quality is good |
270 | // if the gps data quality is good |
271 | if (GPSInfo.satfix == SATFIX_3D) |
271 | if (GPSInfo.satfix == SATFIX_3D) |
272 | { |
272 | { |
273 | switch(GPS_Task) // check what's to do |
273 | switch(GPS_Task) // check what's to do |
274 | { |
274 | { |
275 | case TSK_IDLE: |
275 | case TSK_IDLE: |
276 | // update hold position to current gps position |
276 | // update hold position to current gps position |
277 | GPS_SetHoldPosition(); // can get invalid if gps signal is bad |
277 | GPS_SetHoldPosition(); // can get invalid if gps signal is bad |
278 | // disable gps control |
278 | // disable gps control |
279 | GPS_Neutral(); |
279 | GPS_Neutral(); |
280 | break; // eof TSK_IDLE |
280 | break; // eof TSK_IDLE |
281 | case TSK_HOLD: |
281 | case TSK_HOLD: |
282 | if(HoldPosition.Status != INVALID) |
282 | if(HoldPosition.Status != INVALID) |
283 | { |
283 | { |
284 | if( IsManualControlled() ) // MK controlled by user |
284 | if( IsManualControlled() ) // MK controlled by user |
285 | { |
285 | { |
286 | // update hold point to current gps position |
286 | // update hold point to current gps position |
287 | GPS_SetHoldPosition(); |
287 | GPS_SetHoldPosition(); |
288 | // disable gps control |
288 | // disable gps control |
289 | GPS_Neutral(); |
289 | GPS_Neutral(); |
290 | GPS_P_Delay = 0; |
290 | GPS_P_Delay = 0; |
291 | } |
291 | } |
292 | else // GPS control active |
292 | else // GPS control active |
293 | { |
293 | { |
294 | if(GPS_P_Delay<7) |
294 | if(GPS_P_Delay<7) |
295 | { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s) |
295 | { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s) |
296 | GPS_P_Delay++; |
296 | GPS_P_Delay++; |
297 | GPS_SetHoldPosition(); // update hold point to current gps position |
297 | GPS_SetHoldPosition(); // update hold point to current gps position |
298 | GPS_PIDController(NULL); // activates only the D-Part |
298 | GPS_PIDController(NULL); // activates only the D-Part |
299 | } |
299 | } |
300 | else GPS_PIDController(&HoldPosition);// activates the P&D-Part |
300 | else GPS_PIDController(&HoldPosition);// activates the P&D-Part |
301 | } |
301 | } |
302 | } |
302 | } |
303 | else // invalid Hold Position |
303 | else // invalid Hold Position |
304 | { // try to catch a valid hold position from gps data input |
304 | { // try to catch a valid hold position from gps data input |
305 | GPS_SetHoldPosition(); |
305 | GPS_SetHoldPosition(); |
306 | GPS_Neutral(); |
306 | GPS_Neutral(); |
307 | } |
307 | } |
308 | break; // eof TSK_HOLD |
308 | break; // eof TSK_HOLD |
309 | case TSK_HOME: |
309 | case TSK_HOME: |
310 | if(HomePosition.Status != INVALID) |
310 | if(HomePosition.Status != INVALID) |
311 | { |
311 | { |
312 | // update hold point to current gps position |
312 | // update hold point to current gps position |
313 | // to avoid a flight back if home comming is deactivated |
313 | // to avoid a flight back if home comming is deactivated |
314 | GPS_SetHoldPosition(); |
314 | GPS_SetHoldPosition(); |
315 | if( IsManualControlled() ) // MK controlled by user |
315 | if( IsManualControlled() ) // MK controlled by user |
316 | { |
316 | { |
317 | GPS_Neutral(); |
317 | GPS_Neutral(); |
318 | } |
318 | } |
319 | else // GPS control active |
319 | else // GPS control active |
320 | { |
320 | { |
321 | GPS_PIDController(&HomePosition); |
321 | GPS_PIDController(&HomePosition); |
322 | } |
322 | } |
323 | } |
323 | } |
324 | else // bad home position |
324 | else // bad home position |
325 | { |
325 | { |
326 | BeepTime = 50; // signal invalid home position |
326 | BeepTime = 50; // signal invalid home position |
327 | // try to hold at least the position as a fallback option |
327 | // try to hold at least the position as a fallback option |
328 | 328 | ||
329 | if (HoldPosition.Status != INVALID) |
329 | if (HoldPosition.Status != INVALID) |
330 | { |
330 | { |
331 | if( IsManualControlled() ) // MK controlled by user |
331 | if( IsManualControlled() ) // MK controlled by user |
332 | { |
332 | { |
333 | GPS_Neutral(); |
333 | GPS_Neutral(); |
334 | } |
334 | } |
335 | else // GPS control active |
335 | else // GPS control active |
336 | { |
336 | { |
337 | GPS_PIDController(&HoldPosition); |
337 | GPS_PIDController(&HoldPosition); |
338 | } |
338 | } |
339 | } |
339 | } |
340 | else |
340 | else |
341 | { // try to catch a valid hold position |
341 | { // try to catch a valid hold position |
342 | GPS_SetHoldPosition(); |
342 | GPS_SetHoldPosition(); |
343 | GPS_Neutral(); |
343 | GPS_Neutral(); |
344 | } |
344 | } |
345 | } |
345 | } |
346 | break; // eof TSK_HOME |
346 | break; // eof TSK_HOME |
347 | default: // unhandled task |
347 | default: // unhandled task |
348 | GPS_Neutral(); |
348 | GPS_Neutral(); |
349 | break; // eof default |
349 | break; // eof default |
350 | } // eof switch GPS_Task |
350 | } // eof switch GPS_Task |
351 | } // eof 3D-FIX |
351 | } // eof 3D-FIX |
352 | else // no 3D-SATFIX |
352 | else // no 3D-SATFIX |
353 | { // disable gps control |
353 | { // disable gps control |
354 | GPS_Neutral(); |
354 | GPS_Neutral(); |
355 | if(GPS_Task != TSK_IDLE) |
355 | if(GPS_Task != TSK_IDLE) |
356 | { |
356 | { |
357 | satbeep = 1600 - (int16_t)GPSInfo.satnum * 200; // is zero at 8 sats |
357 | satbeep = 1600 - (int16_t)GPSInfo.satnum * 200; // is zero at 8 sats |
358 | if (satbeep < 0) satbeep = 0; |
358 | if (satbeep < 0) satbeep = 0; |
359 | BeepTime = 50 + (uint16_t)satbeep; // max 1650 * 0.1 ms = |
359 | BeepTime = 50 + (uint16_t)satbeep; // max 1650 * 0.1 ms = |
360 | } |
360 | } |
361 | } |
361 | } |
362 | // set current data as processed to avoid further calculations on the same gps data |
362 | // set current data as processed to avoid further calculations on the same gps data |
363 | GPSInfo.status = PROCESSED; |
363 | GPSInfo.status = PROCESSED; |
364 | break; |
364 | break; |
365 | } // eof GPSInfo.status |
365 | } // eof GPSInfo.status |
366 | DebugOut.Analog[14] = GPS_Pitch; |
366 | DebugOut.Analog[14] = GPS_Pitch; |
367 | DebugOut.Analog[15] = GPS_Roll; |
367 | DebugOut.Analog[15] = GPS_Roll; |
368 | } |
368 | } |
369 | 369 | ||
370 | 370 |