Subversion Repositories FlightCtrl

Rev

Rev 938 | Rev 942 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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