Subversion Repositories FlightCtrl

Rev

Rev 942 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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