Rev 2048 | Rev 2052 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2048 | Rev 2051 | ||
---|---|---|---|
1 | // Navigation with a GPS directly attached to the FC's UART1. |
1 | // Navigation with a GPS directly attached to the FC's UART1. |
2 | 2 | ||
3 | #include <inttypes.h> |
3 | #include <inttypes.h> |
4 | #include <stdlib.h> |
4 | #include <stdlib.h> |
5 | #include <stddef.h> |
5 | #include <stddef.h> |
6 | //#include "mymath.h" |
6 | //#include "mymath.h" |
7 | //#include "timer0.h" |
7 | //#include "timer0.h" |
8 | //#include "uart1.h" |
8 | //#include "uart1.h" |
9 | //#include "rc.h" |
9 | //#include "rc.h" |
10 | //#include "eeprom.h" |
10 | //#include "eeprom.h" |
11 | #include "ubx.h" |
11 | #include "ubx.h" |
12 | #include "configuration.h" |
12 | #include "configuration.h" |
13 | #include "controlMixer.h" |
13 | #include "controlMixer.h" |
14 | #include "output.h" |
14 | #include "output.h" |
15 | #include "isqrt.h" |
15 | #include "isqrt.h" |
16 | #include "attitude.h" |
16 | #include "attitude.h" |
17 | #include "dongfangMath.h" |
17 | #include "dongfangMath.h" |
18 | #include "attitude.h" |
18 | #include "attitude.h" |
19 | 19 | ||
20 | typedef enum { |
20 | typedef enum { |
21 | GPS_FLIGHT_MODE_UNDEF, |
21 | GPS_FLIGHT_MODE_UNDEF, |
22 | GPS_FLIGHT_MODE_FREE, |
22 | GPS_FLIGHT_MODE_FREE, |
23 | GPS_FLIGHT_MODE_AID, |
23 | GPS_FLIGHT_MODE_AID, |
24 | GPS_FLIGHT_MODE_HOME, |
24 | GPS_FLIGHT_MODE_HOME, |
25 | } FlightMode_t; |
25 | } FlightMode_t; |
26 | 26 | ||
27 | #define GPS_POSINTEGRAL_LIMIT 32000 |
27 | #define GPS_POSINTEGRAL_LIMIT 32000 |
28 | #define LOG_NAVI_STICK_GAIN 2 |
28 | #define LOG_NAVI_STICK_GAIN 2 |
29 | #define GPS_P_LIMIT 100 |
29 | #define GPS_P_LIMIT 100 |
30 | 30 | ||
31 | typedef struct { |
31 | typedef struct { |
32 | int32_t longitude; |
32 | int32_t longitude; |
33 | int32_t latitude; |
33 | int32_t latitude; |
34 | int32_t altitude; |
34 | int32_t altitude; |
35 | Status_t status; |
35 | Status_t status; |
36 | } GPS_Pos_t; |
36 | } GPS_Pos_t; |
37 | 37 | ||
38 | // GPS coordinates for hold position |
38 | // GPS coordinates for hold position |
39 | GPS_Pos_t holdPosition = { 0, 0, 0, INVALID }; |
39 | GPS_Pos_t holdPosition = { 0, 0, 0, INVALID }; |
40 | // GPS coordinates for home position |
40 | // GPS coordinates for home position |
41 | GPS_Pos_t homePosition = { 0, 0, 0, INVALID }; |
41 | GPS_Pos_t homePosition = { 0, 0, 0, INVALID }; |
42 | // the current flight mode |
42 | // the current flight mode |
43 | FlightMode_t flightMode = GPS_FLIGHT_MODE_UNDEF; |
43 | FlightMode_t flightMode = GPS_FLIGHT_MODE_UNDEF; |
44 | 44 | ||
45 | // --------------------------------------------------------------------------------- |
45 | // --------------------------------------------------------------------------------- |
46 | void navi_updateFlightMode(void) { |
46 | void navi_updateFlightMode(void) { |
47 | static FlightMode_t flightModeOld = GPS_FLIGHT_MODE_UNDEF; |
47 | static FlightMode_t flightModeOld = GPS_FLIGHT_MODE_UNDEF; |
48 | 48 | ||
49 | if (controlMixer_getSignalQuality() <= SIGNAL_BAD |
49 | if (controlMixer_getSignalQuality() <= SIGNAL_BAD |
50 | || MKFlags & MKFLAG_EMERGENCY_FLIGHT) { |
50 | || MKFlags & MKFLAG_EMERGENCY_FLIGHT) { |
51 | flightMode = GPS_FLIGHT_MODE_HOME; |
51 | flightMode = GPS_FLIGHT_MODE_HOME; |
52 | } else { |
52 | } else { |
53 | if (dynamicParams.naviMode < 50) |
53 | if (dynamicParams.naviMode < 50) |
54 | flightMode = GPS_FLIGHT_MODE_HOME; |
54 | flightMode = GPS_FLIGHT_MODE_HOME; |
55 | else if (dynamicParams.naviMode < 180) |
55 | else if (dynamicParams.naviMode < 180) |
56 | flightMode = GPS_FLIGHT_MODE_AID; |
56 | flightMode = GPS_FLIGHT_MODE_AID; |
57 | else |
57 | else |
58 | flightMode = GPS_FLIGHT_MODE_FREE; |
58 | flightMode = GPS_FLIGHT_MODE_FREE; |
59 | } |
59 | } |
60 | 60 | ||
61 | if (flightMode != flightModeOld) { |
61 | if (flightMode != flightModeOld) { |
62 | beep(100); |
62 | beep(100); |
63 | flightModeOld = flightMode; |
63 | flightModeOld = flightMode; |
64 | } |
64 | } |
65 | 65 | ||
66 | debugOut.analog[31] = flightMode; |
66 | //debugOut.analog[31] = flightMode; |
67 | } |
67 | } |
68 | 68 | ||
69 | // --------------------------------------------------------------------------------- |
69 | // --------------------------------------------------------------------------------- |
70 | // This function defines a good GPS signal condition |
70 | // This function defines a good GPS signal condition |
71 | uint8_t navi_isGPSSignalOK(void) { |
71 | uint8_t navi_isGPSSignalOK(void) { |
72 | static uint8_t GPSFix = 0; |
72 | static uint8_t GPSFix = 0; |
73 | if ((GPSInfo.status != INVALID) && (GPSInfo.satfix == SATFIX_3D) |
73 | if ((GPSInfo.status != INVALID) && (GPSInfo.satfix == SATFIX_3D) |
74 | && (GPSInfo.flags & FLAG_GPSFIXOK) |
74 | && (GPSInfo.flags & FLAG_GPSFIXOK) |
75 | && ((GPSInfo.satnum >= staticParams.GPSMininumSatellites) || GPSFix)) { |
75 | && ((GPSInfo.satnum >= staticParams.GPSMininumSatellites) || GPSFix)) { |
76 | GPSFix = 1; |
76 | GPSFix = 1; |
77 | return 1; |
77 | return 1; |
78 | } else |
78 | } else |
79 | return (0); |
79 | return (0); |
80 | } |
80 | } |
81 | 81 | ||
82 | // --------------------------------------------------------------------------------- |
82 | // --------------------------------------------------------------------------------- |
83 | // rescale xy-vector length to limit |
83 | // rescale xy-vector length to limit |
84 | uint8_t navi_limitXY(int32_t *x, int32_t *y, int32_t limit) { |
84 | uint8_t navi_limitXY(int32_t *x, int32_t *y, int32_t limit) { |
85 | int32_t len; |
85 | int32_t len; |
86 | len = isqrt32(*x * *x + *y * *y); |
86 | len = isqrt32(*x * *x + *y * *y); |
87 | if (len > limit) { |
87 | if (len > limit) { |
88 | // normalize control vector components to the limit |
88 | // normalize control vector components to the limit |
89 | *x = (*x * limit) / len; |
89 | *x = (*x * limit) / len; |
90 | *y = (*y * limit) / len; |
90 | *y = (*y * limit) / len; |
91 | return 1; |
91 | return 1; |
92 | } |
92 | } |
93 | return 0; |
93 | return 0; |
94 | } |
94 | } |
95 | 95 | ||
96 | // checks nick and roll sticks for manual control |
96 | // checks nick and roll sticks for manual control |
97 | uint8_t navi_isManuallyControlled(int16_t* PRTY) { |
97 | uint8_t navi_isManuallyControlled(int16_t* PRTY) { |
98 | if (PRTY[CONTROL_PITCH] < staticParams.naviStickThreshold |
98 | if (PRTY[CONTROL_PITCH] < staticParams.naviStickThreshold |
99 | && PRTY[CONTROL_ROLL] < staticParams.naviStickThreshold) |
99 | && PRTY[CONTROL_ROLL] < staticParams.naviStickThreshold) |
100 | return 0; |
100 | return 0; |
101 | else |
101 | else |
102 | return 1; |
102 | return 1; |
103 | } |
103 | } |
104 | 104 | ||
105 | // set given position to current gps position |
105 | // set given position to current gps position |
106 | uint8_t navi_writeCurrPositionTo(GPS_Pos_t * pGPSPos) { |
106 | uint8_t navi_writeCurrPositionTo(GPS_Pos_t * pGPSPos) { |
107 | if (pGPSPos == NULL) |
107 | if (pGPSPos == NULL) |
108 | return 0; // bad pointer |
108 | return 0; // bad pointer |
109 | 109 | ||
110 | if (navi_isGPSSignalOK()) { // is GPS signal condition is fine |
110 | if (navi_isGPSSignalOK()) { // is GPS signal condition is fine |
111 | pGPSPos->longitude = GPSInfo.longitude; |
111 | pGPSPos->longitude = GPSInfo.longitude; |
112 | pGPSPos->latitude = GPSInfo.latitude; |
112 | pGPSPos->latitude = GPSInfo.latitude; |
113 | pGPSPos->altitude = GPSInfo.altitude; |
113 | pGPSPos->altitude = GPSInfo.altitude; |
114 | pGPSPos->status = NEWDATA; |
114 | pGPSPos->status = NEWDATA; |
115 | return 1; |
115 | return 1; |
116 | } else { // bad GPS signal condition |
116 | } else { // bad GPS signal condition |
117 | pGPSPos->status = INVALID; |
117 | pGPSPos->status = INVALID; |
118 | return 0; |
118 | return 0; |
119 | } |
119 | } |
120 | } |
120 | } |
121 | 121 | ||
122 | // clear position |
122 | // clear position |
123 | uint8_t navi_clearPosition(GPS_Pos_t * pGPSPos) { |
123 | uint8_t navi_clearPosition(GPS_Pos_t * pGPSPos) { |
124 | if (pGPSPos == NULL) |
124 | if (pGPSPos == NULL) |
125 | return 0; // bad pointer |
125 | return 0; // bad pointer |
126 | else { |
126 | else { |
127 | pGPSPos->longitude = 0; |
127 | pGPSPos->longitude = 0; |
128 | pGPSPos->latitude = 0; |
128 | pGPSPos->latitude = 0; |
129 | pGPSPos->altitude = 0; |
129 | pGPSPos->altitude = 0; |
130 | pGPSPos->status = INVALID; |
130 | pGPSPos->status = INVALID; |
131 | } |
131 | } |
132 | return 1; |
132 | return 1; |
133 | } |
133 | } |
134 | 134 | ||
135 | // calculates the GPS control stick values from the deviation to target position |
135 | // calculates the GPS control stick values from the deviation to target position |
136 | // if the pointer to the target positin is NULL or is the target position invalid |
136 | // if the pointer to the target positin is NULL or is the target position invalid |
137 | // then the P part of the controller is deactivated. |
137 | // then the P part of the controller is deactivated. |
138 | void navi_PIDController(GPS_Pos_t *pTargetPos, int16_t *PRTY) { |
138 | void navi_PIDController(GPS_Pos_t *pTargetPos, int16_t *PRTY) { |
139 | static int32_t PID_Nick, PID_Roll; |
139 | static int32_t PID_Nick, PID_Roll; |
140 | int32_t coscompass, sincompass; |
140 | int32_t coscompass, sincompass; |
141 | int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm |
141 | int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm |
142 | int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0; |
142 | int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0; |
143 | int32_t PID_North = 0, PID_East = 0; |
143 | int32_t PID_North = 0, PID_East = 0; |
144 | static int32_t cos_target_latitude = 1; |
144 | static int32_t cos_target_latitude = 1; |
145 | static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0; |
145 | static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0; |
146 | static GPS_Pos_t *pLastTargetPos = 0; |
146 | static GPS_Pos_t *pLastTargetPos = 0; |
147 | 147 | ||
148 | // if GPS data and Compass are ok |
148 | // if GPS data and Compass are ok |
149 | if (navi_isGPSSignalOK() && (magneticHeading >= 0)) { |
149 | if (navi_isGPSSignalOK() && (magneticHeading >= 0)) { |
150 | if (pTargetPos != NULL) { // if there is a target position |
150 | if (pTargetPos != NULL) { // if there is a target position |
151 | if (pTargetPos->status != INVALID) { // and the position data are valid |
151 | if (pTargetPos->status != INVALID) { // and the position data are valid |
152 | // if the target data are updated or the target pointer has changed |
152 | // if the target data are updated or the target pointer has changed |
153 | if ((pTargetPos->status != PROCESSED) |
153 | if ((pTargetPos->status != PROCESSED) |
154 | || (pTargetPos != pLastTargetPos)) { |
154 | || (pTargetPos != pLastTargetPos)) { |
155 | // reset error integral |
155 | // reset error integral |
156 | GPSPosDevIntegral_North = 0; |
156 | GPSPosDevIntegral_North = 0; |
157 | GPSPosDevIntegral_East = 0; |
157 | GPSPosDevIntegral_East = 0; |
158 | // recalculate latitude projection |
158 | // recalculate latitude projection |
159 | cos_target_latitude = cos_360(pTargetPos->latitude / 10000000L); |
159 | cos_target_latitude = cos_360(pTargetPos->latitude / 10000000L); |
160 | // remember last target pointer |
160 | // remember last target pointer |
161 | pLastTargetPos = pTargetPos; |
161 | pLastTargetPos = pTargetPos; |
162 | // mark data as processed |
162 | // mark data as processed |
163 | pTargetPos->status = PROCESSED; |
163 | pTargetPos->status = PROCESSED; |
164 | } |
164 | } |
165 | // calculate position deviation from latitude and longitude differences |
165 | // calculate position deviation from latitude and longitude differences |
166 | GPSPosDev_North = (GPSInfo.latitude - pTargetPos->latitude); // to calculate real cm we would need *111/100 additionally |
166 | GPSPosDev_North = (GPSInfo.latitude - pTargetPos->latitude); // to calculate real cm we would need *111/100 additionally |
167 | GPSPosDev_East = (GPSInfo.longitude - pTargetPos->longitude); // to calculate real cm we would need *111/100 additionally |
167 | GPSPosDev_East = (GPSInfo.longitude - pTargetPos->longitude); // to calculate real cm we would need *111/100 additionally |
168 | // calculate latitude projection |
168 | // calculate latitude projection |
169 | GPSPosDev_East *= cos_target_latitude; |
169 | GPSPosDev_East *= cos_target_latitude; |
170 | GPSPosDev_East >>= LOG_MATH_UNIT_FACTOR; |
170 | GPSPosDev_East >>= LOG_MATH_UNIT_FACTOR; |
171 | } else { // no valid target position available |
171 | } else { // no valid target position available |
172 | // reset error |
172 | // reset error |
173 | GPSPosDev_North = 0; |
173 | GPSPosDev_North = 0; |
174 | GPSPosDev_East = 0; |
174 | GPSPosDev_East = 0; |
175 | // reset error integral |
175 | // reset error integral |
176 | GPSPosDevIntegral_North = 0; |
176 | GPSPosDevIntegral_North = 0; |
177 | GPSPosDevIntegral_East = 0; |
177 | GPSPosDevIntegral_East = 0; |
178 | } |
178 | } |
179 | } else { // no target position available |
179 | } else { // no target position available |
180 | // reset error |
180 | // reset error |
181 | GPSPosDev_North = 0; |
181 | GPSPosDev_North = 0; |
182 | GPSPosDev_East = 0; |
182 | GPSPosDev_East = 0; |
183 | // reset error integral |
183 | // reset error integral |
184 | GPSPosDevIntegral_North = 0; |
184 | GPSPosDevIntegral_North = 0; |
185 | GPSPosDevIntegral_East = 0; |
185 | GPSPosDevIntegral_East = 0; |
186 | } |
186 | } |
187 | 187 | ||
188 | //Calculate PID-components of the controller |
188 | //Calculate PID-components of the controller |
189 | 189 | ||
190 | // D-Part |
190 | // D-Part |
191 | D_North = ((int32_t) staticParams.naviD * GPSInfo.velnorth) >> 9; |
191 | D_North = ((int32_t) staticParams.naviD * GPSInfo.velnorth) >> 9; |
192 | D_East = ((int32_t) staticParams.naviD * GPSInfo.veleast) >> 9; |
192 | D_East = ((int32_t) staticParams.naviD * GPSInfo.veleast) >> 9; |
193 | 193 | ||
194 | // P-Part |
194 | // P-Part |
195 | P_North = ((int32_t) staticParams.naviP * GPSPosDev_North) >> 11; |
195 | P_North = ((int32_t) staticParams.naviP * GPSPosDev_North) >> 11; |
196 | P_East = ((int32_t) staticParams.naviP * GPSPosDev_East) >> 11; |
196 | P_East = ((int32_t) staticParams.naviP * GPSPosDev_East) >> 11; |
197 | 197 | ||
198 | // I-Part |
198 | // I-Part |
199 | I_North = ((int32_t) staticParams.naviI * GPSPosDevIntegral_North) >> 13; |
199 | I_North = ((int32_t) staticParams.naviI * GPSPosDevIntegral_North) >> 13; |
200 | I_East = ((int32_t) staticParams.naviI * GPSPosDevIntegral_East) >> 13; |
200 | I_East = ((int32_t) staticParams.naviI * GPSPosDevIntegral_East) >> 13; |
201 | 201 | ||
202 | // combine P & I |
202 | // combine P & I |
203 | PID_North = P_North + I_North; |
203 | PID_North = P_North + I_North; |
204 | PID_East = P_East + I_East; |
204 | PID_East = P_East + I_East; |
205 | if (!navi_limitXY(&PID_North, &PID_East, GPS_P_LIMIT)) { |
205 | if (!navi_limitXY(&PID_North, &PID_East, GPS_P_LIMIT)) { |
206 | // within limit |
206 | // within limit |
207 | GPSPosDevIntegral_North += GPSPosDev_North >> 4; |
207 | GPSPosDevIntegral_North += GPSPosDev_North >> 4; |
208 | GPSPosDevIntegral_East += GPSPosDev_East >> 4; |
208 | GPSPosDevIntegral_East += GPSPosDev_East >> 4; |
209 | navi_limitXY(&GPSPosDevIntegral_North, &GPSPosDevIntegral_East, GPS_POSINTEGRAL_LIMIT); |
209 | navi_limitXY(&GPSPosDevIntegral_North, &GPSPosDevIntegral_East, GPS_POSINTEGRAL_LIMIT); |
210 | } |
210 | } |
211 | 211 | ||
212 | // combine PI- and D-Part |
212 | // combine PI- and D-Part |
213 | PID_North += D_North; |
213 | PID_North += D_North; |
214 | PID_East += D_East; |
214 | PID_East += D_East; |
215 | 215 | ||
216 | // scale combination with gain. |
216 | // scale combination with gain. |
217 | // dongfang: Lets not do that. P I and D can be scaled instead. |
217 | // dongfang: Lets not do that. P I and D can be scaled instead. |
218 | // PID_North = (PID_North * (int32_t) staticParams.NaviGpsGain) / 100; |
218 | // PID_North = (PID_North * (int32_t) staticParams.NaviGpsGain) / 100; |
219 | // PID_East = (PID_East * (int32_t) staticParams.NaviGpsGain) / 100; |
219 | // PID_East = (PID_East * (int32_t) staticParams.NaviGpsGain) / 100; |
220 | 220 | ||
221 | // GPS to nick and roll settings |
221 | // GPS to nick and roll settings |
222 | // A positive nick angle moves head downwards (flying forward). |
222 | // A positive nick angle moves head downwards (flying forward). |
223 | // A positive roll angle tilts left side downwards (flying left). |
223 | // A positive roll angle tilts left side downwards (flying left). |
224 | // If compass heading is 0 the head of the copter is in north direction. |
224 | // If compass heading is 0 the head of the copter is in north direction. |
225 | // A positive nick angle will fly to north and a positive roll angle will fly to west. |
225 | // A positive nick angle will fly to north and a positive roll angle will fly to west. |
226 | // In case of a positive north deviation/velocity the |
226 | // In case of a positive north deviation/velocity the |
227 | // copter should fly to south (negative nick). |
227 | // copter should fly to south (negative nick). |
228 | // In case of a positive east position deviation and a positive east velocity the |
228 | // In case of a positive east position deviation and a positive east velocity the |
229 | // copter should fly to west (positive roll). |
229 | // copter should fly to west (positive roll). |
230 | // The influence of the GPSStickNick and GPSStickRoll variable is contrarily to the stick values |
230 | // The influence of the GPSStickNick and GPSStickRoll variable is contrarily to the stick values |
231 | // in the flight.c. Therefore a positive north deviation/velocity should result in a positive |
231 | // in the flight.c. Therefore a positive north deviation/velocity should result in a positive |
232 | // GPSStickNick and a positive east deviation/velocity should result in a negative GPSStickRoll. |
232 | // GPSStickNick and a positive east deviation/velocity should result in a negative GPSStickRoll. |
233 | 233 | ||
234 | coscompass = -cos_360(heading / GYRO_DEG_FACTOR_YAW); |
234 | coscompass = -cos_360(heading / GYRO_DEG_FACTOR_YAW); |
235 | sincompass = -sin_360(heading / GYRO_DEG_FACTOR_YAW); |
235 | sincompass = -sin_360(heading / GYRO_DEG_FACTOR_YAW); |
236 | 236 | ||
237 | PID_Nick = (coscompass * PID_North + sincompass * PID_East) >> (LOG_MATH_UNIT_FACTOR-LOG_NAVI_STICK_GAIN); |
237 | PID_Nick = (coscompass * PID_North + sincompass * PID_East) >> (LOG_MATH_UNIT_FACTOR-LOG_NAVI_STICK_GAIN); |
238 | PID_Roll = (sincompass * PID_North - coscompass * PID_East) >> (LOG_MATH_UNIT_FACTOR-LOG_NAVI_STICK_GAIN); |
238 | PID_Roll = (sincompass * PID_North - coscompass * PID_East) >> (LOG_MATH_UNIT_FACTOR-LOG_NAVI_STICK_GAIN); |
239 | 239 | ||
240 | // limit resulting GPS control vector |
240 | // limit resulting GPS control vector |
241 | navi_limitXY(&PID_Nick, &PID_Roll, staticParams.naviStickLimit << LOG_NAVI_STICK_GAIN); |
241 | navi_limitXY(&PID_Nick, &PID_Roll, staticParams.naviStickLimit << LOG_NAVI_STICK_GAIN); |
242 | 242 | ||
243 | PRTY[CONTROL_PITCH] += PID_Nick; |
243 | PRTY[CONTROL_PITCH] += PID_Nick; |
244 | PRTY[CONTROL_ROLL] += PID_Roll; |
244 | PRTY[CONTROL_ROLL] += PID_Roll; |
245 | 245 | ||
246 | } else { // invalid GPS data or bad compass reading |
246 | } else { // invalid GPS data or bad compass reading |
247 | // reset error integral |
247 | // reset error integral |
248 | GPSPosDevIntegral_North = 0; |
248 | GPSPosDevIntegral_North = 0; |
249 | GPSPosDevIntegral_East = 0; |
249 | GPSPosDevIntegral_East = 0; |
250 | } |
250 | } |
251 | } |
251 | } |
252 | 252 | ||
253 | void navigation_periodicTaskAndPRTY(int16_t* PRTY) { |
253 | void navigation_periodicTaskAndPRTY(int16_t* PRTY) { |
254 | static uint8_t GPS_P_Delay = 0; |
254 | static uint8_t GPS_P_Delay = 0; |
255 | static uint16_t beep_rythm = 0; |
255 | static uint16_t beep_rythm = 0; |
256 | 256 | ||
257 | navi_updateFlightMode(); |
257 | navi_updateFlightMode(); |
258 | 258 | ||
259 | // store home position if start of flight flag is set |
259 | // store home position if start of flight flag is set |
260 | if (MKFlags & MKFLAG_CALIBRATE) { |
260 | if (MKFlags & MKFLAG_CALIBRATE) { |
261 | MKFlags &= ~(MKFLAG_CALIBRATE); |
261 | MKFlags &= ~(MKFLAG_CALIBRATE); |
262 | if (navi_writeCurrPositionTo(&homePosition)) { |
262 | if (navi_writeCurrPositionTo(&homePosition)) { |
263 | homePosition.latitude += 10000L; |
263 | homePosition.latitude += 10000L; |
264 | beep(500); |
264 | beep(500); |
265 | } |
265 | } |
266 | } |
266 | } |
267 | 267 | ||
268 | switch (GPSInfo.status) { |
268 | switch (GPSInfo.status) { |
269 | case INVALID: // invalid gps data |
269 | case INVALID: // invalid gps data |
270 | if (flightMode != GPS_FLIGHT_MODE_FREE) { |
270 | if (flightMode != GPS_FLIGHT_MODE_FREE) { |
271 | beep(100); // beep if signal is neccesary |
271 | beep(100); // beep if signal is neccesary |
272 | } |
272 | } |
273 | break; |
273 | break; |
274 | case PROCESSED: // if gps data are already processed do nothing |
274 | case PROCESSED: // if gps data are already processed do nothing |
275 | // downcount timeout |
275 | // downcount timeout |
276 | if (GPSTimeout) |
276 | if (GPSTimeout) |
277 | GPSTimeout--; |
277 | GPSTimeout--; |
278 | // if no new data arrived within timeout set current data invalid |
278 | // if no new data arrived within timeout set current data invalid |
279 | // and therefore disable GPS |
279 | // and therefore disable GPS |
280 | else { |
280 | else { |
281 | GPSInfo.status = INVALID; |
281 | GPSInfo.status = INVALID; |
282 | } |
282 | } |
283 | break; |
283 | break; |
284 | case NEWDATA: // new valid data from gps device |
284 | case NEWDATA: // new valid data from gps device |
285 | // if the gps data quality is good |
285 | // if the gps data quality is good |
286 | beep_rythm++; |
286 | beep_rythm++; |
287 | if (navi_isGPSSignalOK()) { |
287 | if (navi_isGPSSignalOK()) { |
288 | switch (flightMode) { // check what's to do |
288 | switch (flightMode) { // check what's to do |
289 | case GPS_FLIGHT_MODE_FREE: |
289 | case GPS_FLIGHT_MODE_FREE: |
290 | // update hold position to current gps position |
290 | // update hold position to current gps position |
291 | navi_writeCurrPositionTo(&holdPosition); // can get invalid if gps signal is bad |
291 | navi_writeCurrPositionTo(&holdPosition); // can get invalid if gps signal is bad |
292 | // disable gps control |
292 | // disable gps control |
293 | break; |
293 | break; |
294 | 294 | ||
295 | case GPS_FLIGHT_MODE_AID: |
295 | case GPS_FLIGHT_MODE_AID: |
296 | if (holdPosition.status != INVALID) { |
296 | if (holdPosition.status != INVALID) { |
297 | if (navi_isManuallyControlled(PRTY)) { // MK controlled by user |
297 | if (navi_isManuallyControlled(PRTY)) { // MK controlled by user |
298 | // update hold point to current gps position |
298 | // update hold point to current gps position |
299 | navi_writeCurrPositionTo(&holdPosition); |
299 | navi_writeCurrPositionTo(&holdPosition); |
300 | // disable gps control |
300 | // disable gps control |
301 | GPS_P_Delay = 0; |
301 | GPS_P_Delay = 0; |
302 | } else { // GPS control active |
302 | } else { // GPS control active |
303 | if (GPS_P_Delay < 7) { |
303 | if (GPS_P_Delay < 7) { |
304 | // delayed activation of P-Part for 8 cycles (8*0.25s = 2s) |
304 | // delayed activation of P-Part for 8 cycles (8*0.25s = 2s) |
305 | GPS_P_Delay++; |
305 | GPS_P_Delay++; |
306 | navi_writeCurrPositionTo(&holdPosition); // update hold point to current gps position |
306 | navi_writeCurrPositionTo(&holdPosition); // update hold point to current gps position |
307 | navi_PIDController(NULL, PRTY); // activates only the D-Part |
307 | navi_PIDController(NULL, PRTY); // activates only the D-Part |
308 | } else |
308 | } else |
309 | navi_PIDController(&holdPosition, PRTY); // activates the P&D-Part |
309 | navi_PIDController(&holdPosition, PRTY); // activates the P&D-Part |
310 | } |
310 | } |
311 | } else // invalid Hold Position |
311 | } else // invalid Hold Position |
312 | { // try to catch a valid hold position from gps data input |
312 | { // try to catch a valid hold position from gps data input |
313 | navi_writeCurrPositionTo(&holdPosition); |
313 | navi_writeCurrPositionTo(&holdPosition); |
314 | } |
314 | } |
315 | break; |
315 | break; |
316 | 316 | ||
317 | case GPS_FLIGHT_MODE_HOME: |
317 | case GPS_FLIGHT_MODE_HOME: |
318 | if (homePosition.status != INVALID) { |
318 | if (homePosition.status != INVALID) { |
319 | // update hold point to current gps position |
319 | // update hold point to current gps position |
320 | // to avoid a flight back if home comming is deactivated |
320 | // to avoid a flight back if home comming is deactivated |
321 | navi_writeCurrPositionTo(&holdPosition); |
321 | navi_writeCurrPositionTo(&holdPosition); |
322 | if (navi_isManuallyControlled(PRTY)) // MK controlled by user |
322 | if (navi_isManuallyControlled(PRTY)) // MK controlled by user |
323 | { |
323 | { |
324 | } else {// GPS control active |
324 | } else {// GPS control active |
325 | navi_PIDController(&homePosition, PRTY); |
325 | navi_PIDController(&homePosition, PRTY); |
326 | } |
326 | } |
327 | } else { |
327 | } else { |
328 | // bad home position |
328 | // bad home position |
329 | beep(50); // signal invalid home position |
329 | beep(50); // signal invalid home position |
330 | // try to hold at least the position as a fallback option |
330 | // try to hold at least the position as a fallback option |
331 | 331 | ||
332 | if (holdPosition.status != INVALID) { |
332 | if (holdPosition.status != INVALID) { |
333 | if (navi_isManuallyControlled(PRTY)) { |
333 | if (navi_isManuallyControlled(PRTY)) { |
334 | // MK controlled by user |
334 | // MK controlled by user |
335 | } else { |
335 | } else { |
336 | // GPS control active |
336 | // GPS control active |
337 | navi_PIDController(&holdPosition, PRTY); |
337 | navi_PIDController(&holdPosition, PRTY); |
338 | } |
338 | } |
339 | } else { // try to catch a valid hold position |
339 | } else { // try to catch a valid hold position |
340 | navi_writeCurrPositionTo(&holdPosition); |
340 | navi_writeCurrPositionTo(&holdPosition); |
341 | } |
341 | } |
342 | } |
342 | } |
343 | break; // eof TSK_HOME |
343 | break; // eof TSK_HOME |
344 | default: // unhandled task |
344 | default: // unhandled task |
345 | break; // eof default |
345 | break; // eof default |
346 | } // eof switch GPS_Task |
346 | } // eof switch GPS_Task |
347 | } // eof gps data quality is good |
347 | } // eof gps data quality is good |
348 | else // gps data quality is bad |
348 | else // gps data quality is bad |
349 | { // disable gps control |
349 | { // disable gps control |
350 | if (flightMode != GPS_FLIGHT_MODE_FREE) { |
350 | if (flightMode != GPS_FLIGHT_MODE_FREE) { |
351 | // beep if signal is not sufficient |
351 | // beep if signal is not sufficient |
352 | if (!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) |
352 | if (!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) |
353 | beep(100); |
353 | beep(100); |
354 | else if (GPSInfo.satnum < staticParams.GPSMininumSatellites |
354 | else if (GPSInfo.satnum < staticParams.GPSMininumSatellites |
355 | && !(beep_rythm % 5)) |
355 | && !(beep_rythm % 5)) |
356 | beep(10); |
356 | beep(10); |
357 | } |
357 | } |
358 | } |
358 | } |
359 | // set current data as processed to avoid further calculations on the same gps data |
359 | // set current data as processed to avoid further calculations on the same gps data |
360 | GPSInfo.status = PROCESSED; |
360 | GPSInfo.status = PROCESSED; |
361 | break; |
361 | break; |
362 | } // eof GPSInfo.status |
362 | } // eof GPSInfo.status |
363 | 363 | ||
364 | debugOut.analog[11] = GPSInfo.satnum; |
364 | debugOut.analog[11] = GPSInfo.satnum; |
365 | } |
365 | } |
366 | 366 |