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