Rev 937 | Rev 939 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 937 | Rev 938 | ||
---|---|---|---|
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 | typedef enum |
11 | typedef enum |
12 | { |
12 | { |
13 | GPS_FLIGHT_MODE_UNDEF, |
13 | GPS_FLIGHT_MODE_UNDEF, |
14 | GPS_FLIGHT_MODE_FREE, |
14 | GPS_FLIGHT_MODE_FREE, |
15 | GPS_FLIGHT_MODE_AID, |
15 | GPS_FLIGHT_MODE_AID, |
16 | GPS_FLIGHT_MODE_HOME, |
16 | GPS_FLIGHT_MODE_HOME, |
17 | } FlightMode_t; |
17 | } FlightMode_t; |
18 | 18 | ||
19 | #define GPS_STICK_LIMIT 200 // limit of gps stick control to avoid critical flight attitudes |
19 | #define GPS_STICK_LIMIT 500 // limit of gps stick control to avoid critical flight attitudes |
20 | #define GPS_POSDEV_INTEGRAL_LIMIT 32000 // limit for the position error integral |
20 | #define GPS_POSDEV_INTEGRAL_LIMIT 32000 // limit for the position error integral |
21 | #define GPS_P_LIMIT 100 |
21 | #define GPS_P_LIMIT 250 |
22 | 22 | ||
23 | 23 | ||
24 | typedef struct |
24 | typedef struct |
25 | { |
25 | { |
26 | int32_t Longitude; |
26 | int32_t Longitude; |
27 | int32_t Latitude; |
27 | int32_t Latitude; |
28 | int32_t Altitude; |
28 | int32_t Altitude; |
29 | Status_t Status; |
29 | Status_t Status; |
30 | } GPS_Pos_t; |
30 | } GPS_Pos_t; |
31 | 31 | ||
32 | // GPS coordinates for hold position |
32 | // GPS coordinates for hold position |
33 | GPS_Pos_t HoldPosition = {0,0,0,INVALID}; |
33 | GPS_Pos_t HoldPosition = {0,0,0,INVALID}; |
34 | // GPS coordinates for home position |
34 | // GPS coordinates for home position |
35 | GPS_Pos_t HomePosition = {0,0,0,INVALID}; |
35 | GPS_Pos_t HomePosition = {0,0,0,INVALID}; |
36 | // the current flight mode |
36 | // the current flight mode |
37 | FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF; |
37 | FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF; |
38 | 38 | ||
39 | 39 | ||
40 | // --------------------------------------------------------------------------------- |
40 | // --------------------------------------------------------------------------------- |
41 | void GPS_UpdateParameter(void) |
41 | void GPS_UpdateParameter(void) |
42 | { |
42 | { |
43 | static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF; |
43 | static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF; |
44 | 44 | ||
45 | if((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING)) |
45 | if((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING)) |
46 | { |
46 | { |
47 | FlightMode = GPS_FLIGHT_MODE_FREE; |
47 | FlightMode = GPS_FLIGHT_MODE_FREE; |
48 | } |
48 | } |
49 | else |
49 | else |
50 | { |
50 | { |
51 | if (FCParam.NaviGpsModeControl < 50) FlightMode = GPS_FLIGHT_MODE_AID; |
51 | if (FCParam.NaviGpsModeControl < 50) FlightMode = GPS_FLIGHT_MODE_AID; |
52 | else if(FCParam.NaviGpsModeControl < 180) FlightMode = GPS_FLIGHT_MODE_FREE; |
52 | else if(FCParam.NaviGpsModeControl < 180) FlightMode = GPS_FLIGHT_MODE_FREE; |
53 | else FlightMode = GPS_FLIGHT_MODE_HOME; |
53 | else FlightMode = GPS_FLIGHT_MODE_HOME; |
54 | } |
54 | } |
55 | if (FlightMode != FlightModeOld) |
55 | if (FlightMode != FlightModeOld) |
56 | { |
56 | { |
57 | BeepTime = 100; |
57 | BeepTime = 100; |
58 | } |
58 | } |
59 | FlightModeOld = FlightMode; |
59 | FlightModeOld = FlightMode; |
60 | } |
60 | } |
61 | 61 | ||
62 | 62 | ||
63 | 63 | ||
64 | // --------------------------------------------------------------------------------- |
64 | // --------------------------------------------------------------------------------- |
65 | // This function defines a good GPS signal condition |
65 | // This function defines a good GPS signal condition |
66 | uint8_t GPS_IsSignalOK(void) |
66 | uint8_t GPS_IsSignalOK(void) |
67 | { |
67 | { |
68 | static uint8_t GPSFix = 0; |
68 | static uint8_t GPSFix = 0; |
69 | if( (GPSInfo.status != INVALID) && (GPSInfo.satfix == SATFIX_3D) && (GPSInfo.flags & FLAG_GPSFIXOK) && ((GPSInfo.satnum >= ParamSet.NaviGpsMinSat) || GPSFix)) |
69 | if( (GPSInfo.status != INVALID) && (GPSInfo.satfix == SATFIX_3D) && (GPSInfo.flags & FLAG_GPSFIXOK) && ((GPSInfo.satnum >= ParamSet.NaviGpsMinSat) || GPSFix)) |
70 | { |
70 | { |
71 | GPSFix = 1; |
71 | GPSFix = 1; |
72 | return(1); |
72 | return(1); |
73 | 73 | ||
74 | } |
74 | } |
75 | else return (0); |
75 | else return (0); |
76 | 76 | ||
77 | } |
77 | } |
78 | // --------------------------------------------------------------------------------- |
78 | // --------------------------------------------------------------------------------- |
79 | // rescale xy-vector length to limit |
79 | // rescale xy-vector length to limit |
80 | uint8_t GPS_LimitXY(int32_t *x, int32_t *y, int32_t limit) |
80 | uint8_t GPS_LimitXY(int32_t *x, int32_t *y, int32_t limit) |
81 | { |
81 | { |
82 | uint8_t retval = 0; |
82 | uint8_t retval = 0; |
83 | int32_t len; |
83 | int32_t len; |
84 | len = (int32_t)c_sqrt(*x * *x + *y * *y); |
84 | len = (int32_t)c_sqrt(*x * *x + *y * *y); |
85 | if (len > limit) |
85 | if (len > limit) |
86 | { |
86 | { |
87 | // normalize control vector components to the limit |
87 | // normalize control vector components to the limit |
88 | *x = (*x * limit) / len; |
88 | *x = (*x * limit) / len; |
89 | *y = (*y * limit) / len; |
89 | *y = (*y * limit) / len; |
90 | retval = 1; |
90 | retval = 1; |
91 | } |
91 | } |
92 | return(retval); |
92 | return(retval); |
93 | } |
93 | } |
94 | 94 | ||
95 | // checks nick and roll sticks for manual control |
95 | // checks nick and roll sticks for manual control |
96 | uint8_t GPS_IsManualControlled(void) |
96 | uint8_t GPS_IsManualControlled(void) |
97 | { |
97 | { |
98 | if ( (abs(PPM_in[ParamSet.ChannelAssignment[CH_NICK]]) < ParamSet.NaviStickThreshold) && (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < ParamSet.NaviStickThreshold)) return 0; |
98 | if ( (abs(PPM_in[ParamSet.ChannelAssignment[CH_NICK]]) < ParamSet.NaviStickThreshold) && (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < ParamSet.NaviStickThreshold)) return 0; |
99 | else return 1; |
99 | else return 1; |
100 | } |
100 | } |
101 | 101 | ||
102 | // set given position to current gps position |
102 | // set given position to current gps position |
103 | uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos) |
103 | uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos) |
104 | { |
104 | { |
105 | uint8_t retval = 0; |
105 | uint8_t retval = 0; |
106 | if(pGPSPos == NULL) return(retval); // bad pointer |
106 | if(pGPSPos == NULL) return(retval); // bad pointer |
107 | 107 | ||
108 | if(GPS_IsSignalOK()) |
108 | if(GPS_IsSignalOK()) |
109 | { // is GPS signal condition is fine |
109 | { // is GPS signal condition is fine |
110 | pGPSPos->Longitude = GPSInfo.longitude; |
110 | pGPSPos->Longitude = GPSInfo.longitude; |
111 | pGPSPos->Latitude = GPSInfo.latitude; |
111 | pGPSPos->Latitude = GPSInfo.latitude; |
112 | pGPSPos->Altitude = GPSInfo.altitude; |
112 | pGPSPos->Altitude = GPSInfo.altitude; |
113 | pGPSPos->Status = NEWDATA; |
113 | pGPSPos->Status = NEWDATA; |
114 | retval = 1; |
114 | retval = 1; |
115 | } |
115 | } |
116 | else |
116 | else |
117 | { // bad GPS signal condition |
117 | { // bad GPS signal condition |
118 | pGPSPos->Status = INVALID; |
118 | pGPSPos->Status = INVALID; |
119 | retval = 0; |
119 | retval = 0; |
120 | } |
120 | } |
121 | return(retval); |
121 | return(retval); |
122 | } |
122 | } |
123 | 123 | ||
124 | // clear position |
124 | // clear position |
125 | uint8_t GPS_ClearPosition(GPS_Pos_t * pGPSPos) |
125 | uint8_t GPS_ClearPosition(GPS_Pos_t * pGPSPos) |
126 | { |
126 | { |
127 | uint8_t retval = 0; |
127 | uint8_t retval = 0; |
128 | if(pGPSPos == NULL) return(retval); // bad pointer |
128 | if(pGPSPos == NULL) return(retval); // bad pointer |
129 | else |
129 | else |
130 | { |
130 | { |
131 | pGPSPos->Longitude = 0; |
131 | pGPSPos->Longitude = 0; |
132 | pGPSPos->Latitude = 0; |
132 | pGPSPos->Latitude = 0; |
133 | pGPSPos->Altitude = 0; |
133 | pGPSPos->Altitude = 0; |
134 | pGPSPos->Status = INVALID; |
134 | pGPSPos->Status = INVALID; |
135 | retval = 1; |
135 | retval = 1; |
136 | } |
136 | } |
137 | return (retval); |
137 | return (retval); |
138 | } |
138 | } |
139 | 139 | ||
140 | // disable GPS control sticks |
140 | // disable GPS control sticks |
141 | void GPS_Neutral(void) |
141 | void GPS_Neutral(void) |
142 | { |
142 | { |
143 | GPS_Nick = 0; |
143 | GPS_Nick = 0; |
144 | GPS_Roll = 0; |
144 | GPS_Roll = 0; |
145 | } |
145 | } |
146 | 146 | ||
147 | // calculates the GPS control stick values from the deviation to target position |
147 | // calculates the GPS control stick values from the deviation to target position |
148 | // if the pointer to the target positin is NULL or is the target position invalid |
148 | // if the pointer to the target positin is NULL or is the target position invalid |
149 | // then the P part of the controller is deactivated. |
149 | // then the P part of the controller is deactivated. |
150 | void GPS_PIDController(GPS_Pos_t *pTargetPos) |
150 | void GPS_PIDController(GPS_Pos_t *pTargetPos) |
151 | { |
151 | { |
152 | int32_t PID_Nick, PID_Roll; |
152 | static int32_t PID_Nick, PID_Roll; |
153 | int32_t coscompass, sincompass; |
153 | int32_t coscompass, sincompass; |
154 | int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm |
154 | int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm |
155 | int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0; |
155 | int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0; |
156 | int32_t PID_North = 0, PID_East = 0; |
156 | int32_t PID_North = 0, PID_East = 0; |
157 | static int32_t cos_target_latitude = 1; |
157 | static int32_t cos_target_latitude = 1; |
158 | static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0; |
158 | static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0; |
159 | static GPS_Pos_t *pLastTargetPos = 0; |
159 | static GPS_Pos_t *pLastTargetPos = 0; |
160 | 160 | ||
161 | // if GPS data and Compass are ok |
161 | // if GPS data and Compass are ok |
162 | if( GPS_IsSignalOK() && (CompassHeading >= 0) ) |
162 | if( GPS_IsSignalOK() && (CompassHeading >= 0) ) |
163 | { |
163 | { |
164 | 164 | ||
165 | if(pTargetPos != NULL) // if there is a target position |
165 | if(pTargetPos != NULL) // if there is a target position |
166 | { |
166 | { |
167 | if(pTargetPos->Status != INVALID) // and the position data are valid |
167 | if(pTargetPos->Status != INVALID) // and the position data are valid |
168 | { |
168 | { |
169 | // if the target data are updated or the target pointer has changed |
169 | // if the target data are updated or the target pointer has changed |
170 | if ((pTargetPos->Status != PROCESSED) || (pTargetPos != pLastTargetPos) ) |
170 | if ((pTargetPos->Status != PROCESSED) || (pTargetPos != pLastTargetPos) ) |
171 | { |
171 | { |
172 | // reset error integral |
172 | // reset error integral |
173 | GPSPosDevIntegral_North = 0; |
173 | GPSPosDevIntegral_North = 0; |
174 | GPSPosDevIntegral_East = 0; |
174 | GPSPosDevIntegral_East = 0; |
175 | // recalculate latitude projection |
175 | // recalculate latitude projection |
176 | cos_target_latitude = (int32_t)c_cos_8192((int16_t)(pTargetPos->Latitude/10000000L)); |
176 | cos_target_latitude = (int32_t)c_cos_8192((int16_t)(pTargetPos->Latitude/10000000L)); |
177 | // remember last target pointer |
177 | // remember last target pointer |
178 | pLastTargetPos = pTargetPos; |
178 | pLastTargetPos = pTargetPos; |
179 | // mark data as processed |
179 | // mark data as processed |
180 | pTargetPos->Status = PROCESSED; |
180 | pTargetPos->Status = PROCESSED; |
181 | } |
181 | } |
182 | // calculate position deviation from latitude and longitude differences |
182 | // calculate position deviation from latitude and longitude differences |
183 | GPSPosDev_North = (GPSInfo.latitude - pTargetPos->Latitude); // to calculate real cm we would need *111/100 additionally |
183 | GPSPosDev_North = (GPSInfo.latitude - pTargetPos->Latitude); // to calculate real cm we would need *111/100 additionally |
184 | GPSPosDev_East = (GPSInfo.longitude - pTargetPos->Longitude); // to calculate real cm we would need *111/100 additionally |
184 | GPSPosDev_East = (GPSInfo.longitude - pTargetPos->Longitude); // to calculate real cm we would need *111/100 additionally |
185 | // calculate latitude projection |
185 | // calculate latitude projection |
186 | GPSPosDev_East *= cos_target_latitude; |
186 | GPSPosDev_East *= cos_target_latitude; |
187 | GPSPosDev_East /= 8192; |
187 | GPSPosDev_East /= 8192; |
188 | } |
188 | } |
189 | else // no valid target position available |
189 | else // no valid target position available |
190 | { |
190 | { |
191 | // reset error |
191 | // reset error |
192 | GPSPosDev_North = 0; |
192 | GPSPosDev_North = 0; |
193 | GPSPosDev_East = 0; |
193 | GPSPosDev_East = 0; |
194 | // reset error integral |
194 | // reset error integral |
195 | GPSPosDevIntegral_North = 0; |
195 | GPSPosDevIntegral_North = 0; |
196 | GPSPosDevIntegral_East = 0; |
196 | GPSPosDevIntegral_East = 0; |
197 | } |
197 | } |
198 | } |
198 | } |
199 | else // no target position available |
199 | else // no target position available |
200 | { |
200 | { |
201 | // reset error |
201 | // reset error |
202 | GPSPosDev_North = 0; |
202 | GPSPosDev_North = 0; |
203 | GPSPosDev_East = 0; |
203 | GPSPosDev_East = 0; |
204 | // reset error integral |
204 | // reset error integral |
205 | GPSPosDevIntegral_North = 0; |
205 | GPSPosDevIntegral_North = 0; |
206 | GPSPosDevIntegral_East = 0; |
206 | GPSPosDevIntegral_East = 0; |
207 | } |
207 | } |
208 | 208 | ||
209 | //Calculate PID-components of the controller (negative sign for compensation) |
209 | //Calculate PID-components of the controller |
210 | 210 | ||
211 | // P-Part |
211 | // P-Part |
212 | P_North = -((int32_t)FCParam.NaviGpsP * GPSPosDev_North)/512; |
212 | P_North = ((int32_t)FCParam.NaviGpsP * GPSPosDev_North)/512; |
213 | P_East = -((int32_t)FCParam.NaviGpsP * GPSPosDev_East)/512; |
213 | P_East = ((int32_t)FCParam.NaviGpsP * GPSPosDev_East)/512; |
214 | 214 | ||
215 | // I-Part |
215 | // I-Part |
216 | I_North = -((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_North)/2048; |
216 | I_North = ((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_North)/2048; |
217 | I_East = -((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_East)/2048; |
217 | I_East = ((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_East)/2048; |
218 | 218 | ||
219 | // combine P- & I-Part |
219 | // combine P- & I-Part |
220 | PID_North = P_North + I_North; |
220 | PID_North = P_North + I_North; |
221 | PID_East = P_East + I_East; |
221 | PID_East = P_East + I_East; |
222 | 222 | ||
223 | //limit PI-Part |
223 | //limit PI-Part |
224 | if(!GPS_LimitXY(&PID_North, &PID_East, GPS_P_LIMIT)) |
224 | if(!GPS_LimitXY(&PID_North, &PID_East, GPS_P_LIMIT)) |
225 | { |
225 | { |
226 | // P-Part limit is not reached |
226 | // P-Part limit is not reached |
227 | // update position error integrals |
227 | // update position error integrals |
228 | GPSPosDevIntegral_North += GPSPosDev_North/16; |
228 | GPSPosDevIntegral_North += GPSPosDev_North/16; |
229 | if( GPSPosDevIntegral_North > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = GPS_POSDEV_INTEGRAL_LIMIT; |
229 | if( GPSPosDevIntegral_North > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = GPS_POSDEV_INTEGRAL_LIMIT; |
230 | else if (GPSPosDevIntegral_North < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = -GPS_POSDEV_INTEGRAL_LIMIT; |
230 | else if (GPSPosDevIntegral_North < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = -GPS_POSDEV_INTEGRAL_LIMIT; |
231 | GPSPosDevIntegral_East += GPSPosDev_East/16; |
231 | GPSPosDevIntegral_East += GPSPosDev_East/16; |
232 | if( GPSPosDevIntegral_East > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = GPS_POSDEV_INTEGRAL_LIMIT; |
232 | if( GPSPosDevIntegral_East > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = GPS_POSDEV_INTEGRAL_LIMIT; |
233 | else if (GPSPosDevIntegral_East < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = -GPS_POSDEV_INTEGRAL_LIMIT; |
233 | else if (GPSPosDevIntegral_East < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = -GPS_POSDEV_INTEGRAL_LIMIT; |
234 | } |
234 | } |
235 | 235 | ||
236 | // D-Part |
236 | // D-Part |
237 | D_North = -((int32_t)FCParam.NaviGpsD * GPSInfo.velnorth)/ 128; |
237 | D_North = ((int32_t)FCParam.NaviGpsD * GPSInfo.velnorth)/ 128; |
238 | D_East = -((int32_t)FCParam.NaviGpsD * GPSInfo.veleast) / 128; |
238 | D_East = ((int32_t)FCParam.NaviGpsD * GPSInfo.veleast) / 128; |
239 | 239 | ||
240 | // combine PI- and D-Part |
240 | // combine PI- and D-Part |
241 | PID_North += D_North; |
241 | PID_North += D_North; |
242 | PID_East += D_East; |
242 | PID_East += D_East; |
243 | 243 | ||
244 | // scale combination with gain. |
244 | // scale combination with gain. |
245 | PID_North = (PID_North * (int32_t)FCParam.NaviGpsGain) / 100; |
245 | PID_North = (PID_North * (int32_t)FCParam.NaviGpsGain) / 100; |
246 | PID_East = (PID_East * (int32_t)FCParam.NaviGpsGain) / 100; |
246 | PID_East = (PID_East * (int32_t)FCParam.NaviGpsGain) / 100; |
247 | 247 | ||
248 | // GPS to nick and roll settings |
248 | // GPS to nick and roll settings |
249 | 249 | ||
250 | // A positive nick angle moves head downwards (flying forward). |
250 | // A positive nick angle moves head downwards (flying forward). |
251 | // A positive roll angle tilts left side downwards (flying left). |
251 | // A positive roll angle tilts left side downwards (flying left). |
252 | // If compass heading is 0 the head of the copter is in north direction. |
252 | // If compass heading is 0 the head of the copter is in north direction. |
253 | // A positive nick angle will fly to north and a positive roll angle will fly to west. |
253 | // A positive nick angle will fly to north and a positive roll angle will fly to west. |
254 | // In case of a positive north deviation/velocity the |
254 | // In case of a positive north deviation/velocity the |
255 | // copter should fly to south (negative nick). |
255 | // copter should fly to south (negative nick). |
256 | // In case of a positive east position deviation and a positive east velocity the |
256 | // In case of a positive east position deviation and a positive east velocity the |
257 | // copter should fly to west (positive roll). |
257 | // copter should fly to west (positive roll). |
258 | // The influence of the GPS_Nick and GPS_Roll variable is contrarily to the stick values |
258 | // The influence of the GPS_Nick and GPS_Roll variable is contrarily to the stick values |
259 | // in the fc.c. Therefore a positive north deviation/velocity should result in a positive |
259 | // in the fc.c. Therefore a positive north deviation/velocity should result in a positive |
260 | // GPS_Nick and a positive east deviation/velocity should result in a negative GPS_Roll. |
260 | // GPS_Nick and a positive east deviation/velocity should result in a negative GPS_Roll. |
261 | 261 | ||
262 | coscompass = (int32_t)c_cos_8192(YawGyroHeading / YAW_GYRO_DEG_FACTOR); |
262 | coscompass = (int32_t)c_cos_8192(YawGyroHeading / YAW_GYRO_DEG_FACTOR); |
263 | sincompass = (int32_t)c_sin_8192(YawGyroHeading / YAW_GYRO_DEG_FACTOR); |
263 | sincompass = (int32_t)c_sin_8192(YawGyroHeading / YAW_GYRO_DEG_FACTOR); |
264 | PID_Roll = (coscompass * PID_East - sincompass * PID_North) / 8192; |
264 | PID_Nick = (PID_Nick + (coscompass * PID_North + sincompass * PID_East) / 8192)/2; |
265 | PID_Nick = -1*((sincompass * PID_East + coscompass * PID_North) / 8192); |
265 | PID_Roll = (PID_Roll + (sincompass * PID_North - coscompass * PID_East) / 8192)/2; |
- | 266 | ||
266 | 267 | ||
267 | // limit resulting GPS control vector |
268 | // limit resulting GPS control vector |
268 | GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT); |
269 | GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT); |
269 | 270 | ||
270 | GPS_Nick = (int16_t)PID_Nick; |
271 | GPS_Nick = (int16_t)PID_Nick; |
271 | GPS_Roll = (int16_t)PID_Roll; |
272 | GPS_Roll = (int16_t)PID_Roll; |
272 | } |
273 | } |
273 | else // invalid GPS data or bad compass reading |
274 | else // invalid GPS data or bad compass reading |
274 | { |
275 | { |
275 | GPS_Neutral(); // do nothing |
276 | GPS_Neutral(); // do nothing |
276 | // reset error integral |
277 | // reset error integral |
277 | GPSPosDevIntegral_North = 0; |
278 | GPSPosDevIntegral_North = 0; |
278 | GPSPosDevIntegral_East = 0; |
279 | GPSPosDevIntegral_East = 0; |
279 | } |
280 | } |
280 | } |
281 | } |
281 | 282 | ||
282 | 283 | ||
283 | 284 | ||
284 | 285 | ||
285 | void GPS_Main(void) |
286 | void GPS_Main(void) |
286 | { |
287 | { |
287 | static uint8_t GPS_P_Delay = 0; |
288 | static uint8_t GPS_P_Delay = 0; |
288 | uint16_t beep_rythm = 0; |
289 | static uint16_t beep_rythm = 0; |
289 | 290 | ||
290 | GPS_UpdateParameter(); |
291 | GPS_UpdateParameter(); |
291 | 292 | ||
292 | // store home position if start of flight flag is set |
293 | // store home position if start of flight flag is set |
293 | if(MKFlags & MKFLAG_CALIBRATE) |
294 | if(MKFlags & MKFLAG_CALIBRATE) |
294 | { |
295 | { |
295 | GPS_SetCurrPosition(&HomePosition); |
296 | if(GPS_SetCurrPosition(&HomePosition)) BeepTime = 700; |
296 | } |
297 | } |
297 | 298 | ||
298 | switch(GPSInfo.status) |
299 | switch(GPSInfo.status) |
299 | { |
300 | { |
300 | case INVALID: // invalid gps data |
301 | case INVALID: // invalid gps data |
301 | GPS_Neutral(); |
302 | GPS_Neutral(); |
302 | if(FlightMode != GPS_FLIGHT_MODE_FREE) |
303 | if(FlightMode != GPS_FLIGHT_MODE_FREE) |
303 | { |
304 | { |
304 | BeepTime = 100; // beep if signal is neccesary |
305 | BeepTime = 100; // beep if signal is neccesary |
305 | } |
306 | } |
306 | break; |
307 | break; |
307 | case PROCESSED: // if gps data are already processed do nothing |
308 | case PROCESSED: // if gps data are already processed do nothing |
308 | // downcount timeout |
309 | // downcount timeout |
309 | if(GPSTimeout) GPSTimeout--; |
310 | if(GPSTimeout) GPSTimeout--; |
310 | // if no new data arrived within timeout set current data invalid |
311 | // if no new data arrived within timeout set current data invalid |
311 | // and therefore disable GPS |
312 | // and therefore disable GPS |
312 | else |
313 | else |
313 | { |
314 | { |
314 | GPS_Neutral(); |
315 | GPS_Neutral(); |
315 | GPSInfo.status = INVALID; |
316 | GPSInfo.status = INVALID; |
316 | } |
317 | } |
317 | break; |
318 | break; |
318 | case NEWDATA: // new valid data from gps device |
319 | case NEWDATA: // new valid data from gps device |
319 | // if the gps data quality is good |
320 | // if the gps data quality is good |
320 | beep_rythm++; |
321 | beep_rythm++; |
321 | 322 | ||
322 | if (GPS_IsSignalOK()) |
323 | if (GPS_IsSignalOK()) |
323 | { |
324 | { |
324 | switch(FlightMode) // check what's to do |
325 | switch(FlightMode) // check what's to do |
325 | { |
326 | { |
326 | case GPS_FLIGHT_MODE_FREE: |
327 | case GPS_FLIGHT_MODE_FREE: |
327 | // update hold position to current gps position |
328 | // update hold position to current gps position |
328 | GPS_SetCurrPosition(&HoldPosition); // can get invalid if gps signal is bad |
329 | GPS_SetCurrPosition(&HoldPosition); // can get invalid if gps signal is bad |
329 | // disable gps control |
330 | // disable gps control |
330 | GPS_Neutral(); |
331 | GPS_Neutral(); |
331 | break; |
332 | break; |
332 | 333 | ||
333 | case GPS_FLIGHT_MODE_AID: |
334 | case GPS_FLIGHT_MODE_AID: |
334 | if(HoldPosition.Status != INVALID) |
335 | if(HoldPosition.Status != INVALID) |
335 | { |
336 | { |
336 | if( GPS_IsManualControlled() ) // MK controlled by user |
337 | if( GPS_IsManualControlled() ) // MK controlled by user |
337 | { |
338 | { |
338 | // update hold point to current gps position |
339 | // update hold point to current gps position |
339 | GPS_SetCurrPosition(&HoldPosition); |
340 | GPS_SetCurrPosition(&HoldPosition); |
340 | // disable gps control |
341 | // disable gps control |
341 | GPS_Neutral(); |
342 | GPS_Neutral(); |
342 | GPS_P_Delay = 0; |
343 | GPS_P_Delay = 0; |
343 | } |
344 | } |
344 | else // GPS control active |
345 | else // GPS control active |
345 | { |
346 | { |
346 | if(GPS_P_Delay < 7) |
347 | if(GPS_P_Delay < 7) |
347 | { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s) |
348 | { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s) |
348 | GPS_P_Delay++; |
349 | GPS_P_Delay++; |
349 | GPS_SetCurrPosition(&HoldPosition); // update hold point to current gps position |
350 | GPS_SetCurrPosition(&HoldPosition); // update hold point to current gps position |
350 | GPS_PIDController(NULL); // activates only the D-Part |
351 | GPS_PIDController(NULL); // activates only the D-Part |
351 | } |
352 | } |
352 | else GPS_PIDController(&HoldPosition);// activates the P&D-Part |
353 | else GPS_PIDController(&HoldPosition);// activates the P&D-Part |
353 | } |
354 | } |
354 | } |
355 | } |
355 | else // invalid Hold Position |
356 | else // invalid Hold Position |
356 | { // try to catch a valid hold position from gps data input |
357 | { // try to catch a valid hold position from gps data input |
357 | GPS_SetCurrPosition(&HoldPosition); |
358 | GPS_SetCurrPosition(&HoldPosition); |
358 | GPS_Neutral(); |
359 | GPS_Neutral(); |
359 | } |
360 | } |
360 | break; |
361 | break; |
361 | 362 | ||
362 | case GPS_FLIGHT_MODE_HOME: |
363 | case GPS_FLIGHT_MODE_HOME: |
363 | if(HomePosition.Status != INVALID) |
364 | if(HomePosition.Status != INVALID) |
364 | { |
365 | { |
365 | // update hold point to current gps position |
366 | // update hold point to current gps position |
366 | // to avoid a flight back if home comming is deactivated |
367 | // to avoid a flight back if home comming is deactivated |
367 | GPS_SetCurrPosition(&HoldPosition); |
368 | GPS_SetCurrPosition(&HoldPosition); |
368 | if( GPS_IsManualControlled() ) // MK controlled by user |
369 | if( GPS_IsManualControlled() ) // MK controlled by user |
369 | { |
370 | { |
370 | GPS_Neutral(); |
371 | GPS_Neutral(); |
371 | } |
372 | } |
372 | else // GPS control active |
373 | else // GPS control active |
373 | { |
374 | { |
374 | GPS_PIDController(&HomePosition); |
375 | GPS_PIDController(&HomePosition); |
375 | } |
376 | } |
376 | } |
377 | } |
377 | else // bad home position |
378 | else // bad home position |
378 | { |
379 | { |
379 | BeepTime = 50; // signal invalid home position |
380 | BeepTime = 50; // signal invalid home position |
380 | // try to hold at least the position as a fallback option |
381 | // try to hold at least the position as a fallback option |
381 | 382 | ||
382 | if (HoldPosition.Status != INVALID) |
383 | if (HoldPosition.Status != INVALID) |
383 | { |
384 | { |
384 | if( GPS_IsManualControlled() ) // MK controlled by user |
385 | if( GPS_IsManualControlled() ) // MK controlled by user |
385 | { |
386 | { |
386 | GPS_Neutral(); |
387 | GPS_Neutral(); |
387 | } |
388 | } |
388 | else // GPS control active |
389 | else // GPS control active |
389 | { |
390 | { |
390 | GPS_PIDController(&HoldPosition); |
391 | GPS_PIDController(&HoldPosition); |
391 | } |
392 | } |
392 | } |
393 | } |
393 | else |
394 | else |
394 | { // try to catch a valid hold position |
395 | { // try to catch a valid hold position |
395 | GPS_SetCurrPosition(&HoldPosition); |
396 | GPS_SetCurrPosition(&HoldPosition); |
396 | GPS_Neutral(); |
397 | GPS_Neutral(); |
397 | } |
398 | } |
398 | } |
399 | } |
399 | break; // eof TSK_HOME |
400 | break; // eof TSK_HOME |
400 | default: // unhandled task |
401 | default: // unhandled task |
401 | GPS_Neutral(); |
402 | GPS_Neutral(); |
402 | break; // eof default |
403 | break; // eof default |
403 | } // eof switch GPS_Task |
404 | } // eof switch GPS_Task |
404 | } // eof gps data quality is good |
405 | } // eof gps data quality is good |
405 | else // gps data quality is bad |
406 | else // gps data quality is bad |
406 | { // disable gps control |
407 | { // disable gps control |
407 | GPS_Neutral(); |
408 | GPS_Neutral(); |
408 | // beep if signal is not sufficient |
409 | // beep if signal is not sufficient |
409 | if(!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100; |
410 | if(!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100; |
410 | else if (GPSInfo.satnum < ParamSet.NaviGpsMinSat && !(beep_rythm % 5)) BeepTime = 10; |
411 | else if (GPSInfo.satnum < ParamSet.NaviGpsMinSat && !(beep_rythm % 5)) BeepTime = 10; |
411 | } |
412 | } |
412 | // set current data as processed to avoid further calculations on the same gps data |
413 | // set current data as processed to avoid further calculations on the same gps data |
413 | GPSInfo.status = PROCESSED; |
414 | GPSInfo.status = PROCESSED; |
414 | break; |
415 | break; |
415 | } // eof GPSInfo.status |
416 | } // eof GPSInfo.status |
416 | } |
417 | } |
417 | 418 | ||
418 | 419 |