Subversion Repositories FlightCtrl

Rev

Rev 1796 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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