Subversion Repositories FlightCtrl

Rev

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

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