Subversion Repositories FlightCtrl

Rev

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

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