Subversion Repositories FlightCtrl

Rev

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

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