Rev 909 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 909 | Rev 916 | ||
---|---|---|---|
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 15 // must be at least in a range where 90% of the trimming does not switch of the GPS function |
15 | #define GPS_STICK_SENSE 15 // 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 | #define GPS_P_LIMIT 25 |
18 | #define GPS_P_LIMIT 25 |
19 | 19 | ||
20 | 20 | ||
21 | uint8_t GPS_P_Factor = 0, GPS_I_Factor = 0, GPS_D_Factor = 0; |
21 | uint8_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 nick 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_NICK]]) < 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_Nick = 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 temp, temp1, PID_Pitch, PID_Roll; |
99 | int32_t temp, temp1, PID_Nick, PID_Roll; |
100 | int32_t coscompass, sincompass; |
100 | int32_t coscompass, sincompass; |
101 | int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm |
101 | int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm |
102 | int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0; |
102 | int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0; |
103 | int32_t PID_North = 0, PID_East = 0; |
103 | int32_t PID_North = 0, PID_East = 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 | else // no valid target position available |
136 | else // no valid target position available |
137 | { |
137 | { |
138 | // reset error |
138 | // reset error |
139 | GPSPosDev_North = 0; |
139 | GPSPosDev_North = 0; |
140 | GPSPosDev_East = 0; |
140 | GPSPosDev_East = 0; |
141 | // reset error integral |
141 | // reset error integral |
142 | GPSPosDevIntegral_North = 0; |
142 | GPSPosDevIntegral_North = 0; |
143 | GPSPosDevIntegral_East = 0; |
143 | GPSPosDevIntegral_East = 0; |
144 | } |
144 | } |
145 | } |
145 | } |
146 | else // no target position available |
146 | else // no target position available |
147 | { |
147 | { |
148 | // reset error |
148 | // reset error |
149 | GPSPosDev_North = 0; |
149 | GPSPosDev_North = 0; |
150 | GPSPosDev_East = 0; |
150 | GPSPosDev_East = 0; |
151 | // reset error integral |
151 | // reset error integral |
152 | GPSPosDevIntegral_North = 0; |
152 | GPSPosDevIntegral_North = 0; |
153 | GPSPosDevIntegral_East = 0; |
153 | GPSPosDevIntegral_East = 0; |
154 | } |
154 | } |
155 | 155 | ||
156 | //Calculate PID-components of the controller (negative sign for compensation) |
156 | //Calculate PID-components of the controller (negative sign for compensation) |
157 | 157 | ||
158 | // P-Part |
158 | // P-Part |
159 | P_North = -((int32_t)GPS_P_Factor * GPSPosDev_North)/2048; |
159 | P_North = -((int32_t)GPS_P_Factor * GPSPosDev_North)/2048; |
160 | P_East = -((int32_t)GPS_P_Factor * GPSPosDev_East)/2048; |
160 | P_East = -((int32_t)GPS_P_Factor * GPSPosDev_East)/2048; |
161 | 161 | ||
162 | // I-Part |
162 | // I-Part |
163 | I_North = -((int32_t)GPS_I_Factor * GPSPosDevIntegral_North)/8192; |
163 | I_North = -((int32_t)GPS_I_Factor * GPSPosDevIntegral_North)/8192; |
164 | I_East = -((int32_t)GPS_I_Factor * GPSPosDevIntegral_East)/8192; |
164 | I_East = -((int32_t)GPS_I_Factor * GPSPosDevIntegral_East)/8192; |
165 | 165 | ||
166 | // combine P- & I-Part |
166 | // combine P- & I-Part |
167 | PID_North = P_North + I_North; |
167 | PID_North = P_North + I_North; |
168 | PID_East = P_East + I_East; |
168 | PID_East = P_East + I_East; |
169 | 169 | ||
170 | //limit PI-Part to limit the max velocity |
170 | //limit PI-Part to limit the max velocity |
171 | //temp1 = ((int32_t)GPS_D_Factor * MAX_VELOCITY)/512; // the PI-Part limit |
171 | //temp1 = ((int32_t)GPS_D_Factor * MAX_VELOCITY)/512; // the PI-Part limit |
172 | temp = (int32_t)c_sqrt(PID_North*PID_North + PID_East*PID_East); // the current PI-Part |
172 | temp = (int32_t)c_sqrt(PID_North*PID_North + PID_East*PID_East); // the current PI-Part |
173 | if(temp > GPS_P_LIMIT) // P-Part limit is reached |
173 | if(temp > GPS_P_LIMIT) // P-Part limit is reached |
174 | { |
174 | { |
175 | // normalize P-part components to the P-Part limit |
175 | // normalize P-part components to the P-Part limit |
176 | PID_North = (PID_North * GPS_P_LIMIT)/temp; |
176 | PID_North = (PID_North * GPS_P_LIMIT)/temp; |
177 | PID_East = (PID_East * GPS_P_LIMIT)/temp; |
177 | PID_East = (PID_East * GPS_P_LIMIT)/temp; |
178 | } |
178 | } |
179 | else // PI-Part under its limit |
179 | else // PI-Part under its limit |
180 | { |
180 | { |
181 | // update position error integrals |
181 | // update position error integrals |
182 | GPSPosDevIntegral_North += GPSPosDev_North/16; |
182 | GPSPosDevIntegral_North += GPSPosDev_North/16; |
183 | if( GPSPosDevIntegral_North > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = GPS_POSDEV_INTEGRAL_LIMIT; |
183 | if( GPSPosDevIntegral_North > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = GPS_POSDEV_INTEGRAL_LIMIT; |
184 | else if (GPSPosDevIntegral_North < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = -GPS_POSDEV_INTEGRAL_LIMIT; |
184 | else if (GPSPosDevIntegral_North < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = -GPS_POSDEV_INTEGRAL_LIMIT; |
185 | GPSPosDevIntegral_East += GPSPosDev_East/16; |
185 | GPSPosDevIntegral_East += GPSPosDev_East/16; |
186 | if( GPSPosDevIntegral_East > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = GPS_POSDEV_INTEGRAL_LIMIT; |
186 | if( GPSPosDevIntegral_East > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = GPS_POSDEV_INTEGRAL_LIMIT; |
187 | else if (GPSPosDevIntegral_East < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = -GPS_POSDEV_INTEGRAL_LIMIT; |
187 | else if (GPSPosDevIntegral_East < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = -GPS_POSDEV_INTEGRAL_LIMIT; |
188 | } |
188 | } |
189 | 189 | ||
190 | // D-Part |
190 | // D-Part |
191 | D_North = -((int32_t)GPS_D_Factor * GPSInfo.velnorth)/512; |
191 | D_North = -((int32_t)GPS_D_Factor * GPSInfo.velnorth)/512; |
192 | D_East = -((int32_t)GPS_D_Factor * GPSInfo.veleast)/512; |
192 | D_East = -((int32_t)GPS_D_Factor * GPSInfo.veleast)/512; |
193 | 193 | ||
194 | 194 | ||
195 | // combine PI- and D-Part |
195 | // combine PI- and D-Part |
196 | PID_North += D_North; |
196 | PID_North += D_North; |
197 | PID_East += D_East; |
197 | PID_East += D_East; |
198 | 198 | ||
199 | // GPS to pitch and roll settings |
199 | // GPS to nick and roll settings |
200 | 200 | ||
201 | // A positive pitch angle moves head downwards (flying forward). |
201 | // A positive nick angle moves head downwards (flying forward). |
202 | // A positive roll angle tilts left side downwards (flying left). |
202 | // A positive roll angle tilts left side downwards (flying left). |
203 | // If compass heading is 0 the head of the copter is in north direction. |
203 | // If compass heading is 0 the head of the copter is in north direction. |
204 | // A positive pitch angle will fly to north and a positive roll angle will fly to west. |
204 | // A positive nick angle will fly to north and a positive roll angle will fly to west. |
205 | // In case of a positive north deviation/velocity the |
205 | // In case of a positive north deviation/velocity the |
206 | // copter should fly to south (negative pitch). |
206 | // copter should fly to south (negative nick). |
207 | // In case of a positive east position deviation and a positive east velocity the |
207 | // In case of a positive east position deviation and a positive east velocity the |
208 | // copter should fly to west (positive roll). |
208 | // copter should fly to west (positive roll). |
209 | // The influence of the GPS_Pitch and GPS_Roll variable is contrarily to the stick values |
209 | // The influence of the GPS_Nick and GPS_Roll variable is contrarily to the stick values |
210 | // in the fc.c. Therefore a positive north deviation/velocity should result in a positive |
210 | // in the fc.c. Therefore a positive north deviation/velocity should result in a positive |
211 | // GPS_Pitch and a positive east deviation/velocity should result in a negative GPS_Roll. |
211 | // GPS_Nick and a positive east deviation/velocity should result in a negative GPS_Roll. |
212 | 212 | ||
213 | coscompass = (int32_t)c_cos_8192(CompassHeading); |
213 | coscompass = (int32_t)c_cos_8192(CompassHeading); |
214 | sincompass = (int32_t)c_sin_8192(CompassHeading); |
214 | sincompass = (int32_t)c_sin_8192(CompassHeading); |
215 | PID_Roll = (coscompass * PID_East - sincompass * PID_North) / 8192; |
215 | PID_Roll = (coscompass * PID_East - sincompass * PID_North) / 8192; |
216 | PID_Pitch = -1*((sincompass * PID_East + coscompass * PID_North) / 8192); |
216 | PID_Nick = -1*((sincompass * PID_East + coscompass * PID_North) / 8192); |
217 | 217 | ||
218 | // limit resulting GPS control vector |
218 | // limit resulting GPS control vector |
219 | temp = (int32_t)c_sqrt(PID_Roll*PID_Roll + PID_Pitch*PID_Pitch); |
219 | temp = (int32_t)c_sqrt(PID_Roll*PID_Roll + PID_Nick*PID_Nick); |
220 | if (temp > GPS_STICK_LIMIT) |
220 | if (temp > GPS_STICK_LIMIT) |
221 | { |
221 | { |
222 | // normalize control vector components to the limit |
222 | // normalize control vector components to the limit |
223 | PID_Roll = (PID_Roll * GPS_STICK_LIMIT)/temp; |
223 | PID_Roll = (PID_Roll * GPS_STICK_LIMIT)/temp; |
224 | PID_Pitch = (PID_Pitch * GPS_STICK_LIMIT)/temp; |
224 | PID_Nick = (PID_Nick * GPS_STICK_LIMIT)/temp; |
225 | } |
225 | } |
226 | 226 | ||
227 | GPS_Roll = (int16_t)PID_Roll; |
227 | GPS_Roll = (int16_t)PID_Roll; |
228 | GPS_Pitch = (int16_t)PID_Pitch; |
228 | GPS_Nick = (int16_t)PID_Nick; |
229 | 229 | ||
230 | } |
230 | } |
231 | else // invalid GPS data or bad compass reading |
231 | else // invalid GPS data or bad compass reading |
232 | { |
232 | { |
233 | GPS_Neutral(); // do nothing |
233 | GPS_Neutral(); // do nothing |
234 | // reset error integral |
234 | // reset error integral |
235 | GPSPosDevIntegral_North = 0; |
235 | GPSPosDevIntegral_North = 0; |
236 | GPSPosDevIntegral_East = 0; |
236 | GPSPosDevIntegral_East = 0; |
237 | } |
237 | } |
238 | } |
238 | } |
239 | 239 | ||
240 | 240 | ||
241 | 241 | ||
242 | 242 | ||
243 | void GPS_Main(uint8_t ctrl) |
243 | void GPS_Main(uint8_t ctrl) |
244 | { |
244 | { |
245 | static uint8_t GPS_Task = TSK_IDLE; |
245 | static uint8_t GPS_Task = TSK_IDLE; |
246 | static uint8_t GPS_P_Delay = 0; |
246 | static uint8_t GPS_P_Delay = 0; |
247 | int16_t satbeep; |
247 | int16_t satbeep; |
248 | 248 | ||
249 | // ctrl enables the gps feature |
249 | // ctrl enables the gps feature |
250 | if(ctrl < 70) GPS_Task = TSK_IDLE; |
250 | if(ctrl < 70) GPS_Task = TSK_IDLE; |
251 | else if (ctrl < 160) GPS_Task = TSK_HOLD; |
251 | else if (ctrl < 160) GPS_Task = TSK_HOLD; |
252 | else GPS_Task = TSK_HOME; // ctrl >= 160 |
252 | else GPS_Task = TSK_HOME; // ctrl >= 160 |
253 | 253 | ||
254 | 254 | ||
255 | switch(GPSInfo.status) |
255 | switch(GPSInfo.status) |
256 | { |
256 | { |
257 | case INVALID: // invalid gps data |
257 | case INVALID: // invalid gps data |
258 | GPS_Neutral(); |
258 | GPS_Neutral(); |
259 | if(GPS_Task != TSK_IDLE) |
259 | if(GPS_Task != TSK_IDLE) |
260 | { |
260 | { |
261 | BeepTime = 100; // beep if signal is neccesary |
261 | BeepTime = 100; // beep if signal is neccesary |
262 | } |
262 | } |
263 | break; |
263 | break; |
264 | case PROCESSED: // if gps data are already processed do nothing |
264 | case PROCESSED: // if gps data are already processed do nothing |
265 | // downcount timeout |
265 | // downcount timeout |
266 | if(GPSTimeout) GPSTimeout--; |
266 | if(GPSTimeout) GPSTimeout--; |
267 | // if no new data arrived within timeout set current data invalid |
267 | // if no new data arrived within timeout set current data invalid |
268 | // and therefore disable GPS |
268 | // and therefore disable GPS |
269 | else |
269 | else |
270 | { |
270 | { |
271 | GPS_Neutral(); |
271 | GPS_Neutral(); |
272 | GPSInfo.status = INVALID; |
272 | GPSInfo.status = INVALID; |
273 | } |
273 | } |
274 | break; |
274 | break; |
275 | case VALID: // new valid data from gps device |
275 | case VALID: // new valid data from gps device |
276 | // if the gps data quality is good |
276 | // if the gps data quality is good |
277 | if (GPSInfo.satfix == SATFIX_3D) |
277 | if (GPSInfo.satfix == SATFIX_3D) |
278 | { |
278 | { |
279 | switch(GPS_Task) // check what's to do |
279 | switch(GPS_Task) // check what's to do |
280 | { |
280 | { |
281 | case TSK_IDLE: |
281 | case TSK_IDLE: |
282 | // update hold position to current gps position |
282 | // update hold position to current gps position |
283 | GPS_SetHoldPosition(); // can get invalid if gps signal is bad |
283 | GPS_SetHoldPosition(); // can get invalid if gps signal is bad |
284 | // disable gps control |
284 | // disable gps control |
285 | GPS_Neutral(); |
285 | GPS_Neutral(); |
286 | break; // eof TSK_IDLE |
286 | break; // eof TSK_IDLE |
287 | case TSK_HOLD: |
287 | case TSK_HOLD: |
288 | if(HoldPosition.Status != INVALID) |
288 | if(HoldPosition.Status != INVALID) |
289 | { |
289 | { |
290 | if( IsManualControlled() ) // MK controlled by user |
290 | if( IsManualControlled() ) // MK controlled by user |
291 | { |
291 | { |
292 | // update hold point to current gps position |
292 | // update hold point to current gps position |
293 | GPS_SetHoldPosition(); |
293 | GPS_SetHoldPosition(); |
294 | // disable gps control |
294 | // disable gps control |
295 | GPS_Neutral(); |
295 | GPS_Neutral(); |
296 | GPS_P_Delay = 0; |
296 | GPS_P_Delay = 0; |
297 | } |
297 | } |
298 | else // GPS control active |
298 | else // GPS control active |
299 | { |
299 | { |
300 | if(GPS_P_Delay<7) |
300 | if(GPS_P_Delay<7) |
301 | { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s) |
301 | { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s) |
302 | GPS_P_Delay++; |
302 | GPS_P_Delay++; |
303 | GPS_SetHoldPosition(); // update hold point to current gps position |
303 | GPS_SetHoldPosition(); // update hold point to current gps position |
304 | GPS_PIDController(NULL); // activates only the D-Part |
304 | GPS_PIDController(NULL); // activates only the D-Part |
305 | } |
305 | } |
306 | else GPS_PIDController(&HoldPosition);// activates the P&D-Part |
306 | else GPS_PIDController(&HoldPosition);// activates the P&D-Part |
307 | } |
307 | } |
308 | } |
308 | } |
309 | else // invalid Hold Position |
309 | else // invalid Hold Position |
310 | { // try to catch a valid hold position from gps data input |
310 | { // try to catch a valid hold position from gps data input |
311 | GPS_SetHoldPosition(); |
311 | GPS_SetHoldPosition(); |
312 | GPS_Neutral(); |
312 | GPS_Neutral(); |
313 | } |
313 | } |
314 | break; // eof TSK_HOLD |
314 | break; // eof TSK_HOLD |
315 | case TSK_HOME: |
315 | case TSK_HOME: |
316 | if(HomePosition.Status != INVALID) |
316 | if(HomePosition.Status != INVALID) |
317 | { |
317 | { |
318 | // update hold point to current gps position |
318 | // update hold point to current gps position |
319 | // to avoid a flight back if home comming is deactivated |
319 | // to avoid a flight back if home comming is deactivated |
320 | GPS_SetHoldPosition(); |
320 | GPS_SetHoldPosition(); |
321 | if( IsManualControlled() ) // MK controlled by user |
321 | if( IsManualControlled() ) // MK controlled by user |
322 | { |
322 | { |
323 | GPS_Neutral(); |
323 | GPS_Neutral(); |
324 | } |
324 | } |
325 | else // GPS control active |
325 | else // GPS control active |
326 | { |
326 | { |
327 | GPS_PIDController(&HomePosition); |
327 | GPS_PIDController(&HomePosition); |
328 | } |
328 | } |
329 | } |
329 | } |
330 | else // bad home position |
330 | else // bad home position |
331 | { |
331 | { |
332 | BeepTime = 50; // signal invalid home position |
332 | BeepTime = 50; // signal invalid home position |
333 | // try to hold at least the position as a fallback option |
333 | // try to hold at least the position as a fallback option |
334 | 334 | ||
335 | if (HoldPosition.Status != INVALID) |
335 | if (HoldPosition.Status != INVALID) |
336 | { |
336 | { |
337 | if( IsManualControlled() ) // MK controlled by user |
337 | if( IsManualControlled() ) // MK controlled by user |
338 | { |
338 | { |
339 | GPS_Neutral(); |
339 | GPS_Neutral(); |
340 | } |
340 | } |
341 | else // GPS control active |
341 | else // GPS control active |
342 | { |
342 | { |
343 | GPS_PIDController(&HoldPosition); |
343 | GPS_PIDController(&HoldPosition); |
344 | } |
344 | } |
345 | } |
345 | } |
346 | else |
346 | else |
347 | { // try to catch a valid hold position |
347 | { // try to catch a valid hold position |
348 | GPS_SetHoldPosition(); |
348 | GPS_SetHoldPosition(); |
349 | GPS_Neutral(); |
349 | GPS_Neutral(); |
350 | } |
350 | } |
351 | } |
351 | } |
352 | break; // eof TSK_HOME |
352 | break; // eof TSK_HOME |
353 | default: // unhandled task |
353 | default: // unhandled task |
354 | GPS_Neutral(); |
354 | GPS_Neutral(); |
355 | break; // eof default |
355 | break; // eof default |
356 | } // eof switch GPS_Task |
356 | } // eof switch GPS_Task |
357 | } // eof 3D-FIX |
357 | } // eof 3D-FIX |
358 | else // no 3D-SATFIX |
358 | else // no 3D-SATFIX |
359 | { // disable gps control |
359 | { // disable gps control |
360 | GPS_Neutral(); |
360 | GPS_Neutral(); |
361 | if(GPS_Task != TSK_IDLE) |
361 | if(GPS_Task != TSK_IDLE) |
362 | { |
362 | { |
363 | satbeep = 1600 - (int16_t)GPSInfo.satnum * 200; // is zero at 8 sats |
363 | satbeep = 1600 - (int16_t)GPSInfo.satnum * 200; // is zero at 8 sats |
364 | if (satbeep < 0) satbeep = 0; |
364 | if (satbeep < 0) satbeep = 0; |
365 | BeepTime = 50 + (uint16_t)satbeep; // max 1650 * 0.1 ms = |
365 | BeepTime = 50 + (uint16_t)satbeep; // max 1650 * 0.1 ms = |
366 | } |
366 | } |
367 | } |
367 | } |
368 | // set current data as processed to avoid further calculations on the same gps data |
368 | // set current data as processed to avoid further calculations on the same gps data |
369 | GPSInfo.status = PROCESSED; |
369 | GPSInfo.status = PROCESSED; |
370 | break; |
370 | break; |
371 | } // eof GPSInfo.status |
371 | } // eof GPSInfo.status |
372 | } |
372 | } |
373 | 373 | ||
374 | 374 |