Subversion Repositories FlightCtrl

Rev

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

Rev 1612 Rev 1775
Line 49... Line 49...
49
// +  POSSIBILITY OF SUCH DAMAGE.
49
// +  POSSIBILITY OF SUCH DAMAGE.
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
#include <inttypes.h>
51
#include <inttypes.h>
52
#include <stdlib.h>
52
#include <stdlib.h>
53
#include "ubx.h"
53
#include "ubx.h"
54
#include "mymath.h"
54
//#include "mymath.h"
55
#include "timer0.h"
55
#include "timer0.h"
56
#include "uart0.h"
56
#include "uart0.h"
57
#include "rc.h"
57
#include "rc.h"
58
#include "eeprom.h"
58
#include "eeprom.h"
Line 85... Line 85...
85
// the current flight mode
85
// the current flight mode
86
FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF;
86
FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF;
Line 87... Line 87...
87
 
87
 
88
 
88
 
89
// ---------------------------------------------------------------------------------
-
 
90
void GPS_UpdateParameter(void)
89
// ---------------------------------------------------------------------------------
Line 91... Line 90...
91
{
90
void GPS_UpdateParameter(void) {
92
  static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF;
-
 
93
 
91
  static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF;
94
  if((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING))
-
 
95
    {
92
 
96
      FlightMode = GPS_FLIGHT_MODE_FREE;
-
 
97
    }
93
  if((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING)) {
98
  else
94
    FlightMode = GPS_FLIGHT_MODE_FREE;
99
    {
95
  } else {
100
      if     (dynamicParams.NaviGpsModeControl <  50) FlightMode = GPS_FLIGHT_MODE_AID;
96
    if     (dynamicParams.NaviGpsModeControl <  50) FlightMode = GPS_FLIGHT_MODE_AID;
-
 
97
    else if(dynamicParams.NaviGpsModeControl < 180) FlightMode = GPS_FLIGHT_MODE_FREE;
101
      else if(dynamicParams.NaviGpsModeControl < 180) FlightMode = GPS_FLIGHT_MODE_FREE;
98
    else                                          FlightMode = GPS_FLIGHT_MODE_HOME;
102
      else                                                FlightMode = GPS_FLIGHT_MODE_HOME;
-
 
103
    }
99
  }
104
  if (FlightMode != FlightModeOld)
100
 
105
    {
101
  if (FlightMode != FlightModeOld) {
106
      BeepTime = 100;
102
    BeepTime = 100;
Line 107... Line -...
107
    }
-
 
108
  FlightModeOld = FlightMode;
-
 
109
}
103
  }
110
 
104
  FlightModeOld = FlightMode;
111
 
105
}
112
 
-
 
113
// ---------------------------------------------------------------------------------
106
 
114
// This function defines a good GPS signal condition
107
// ---------------------------------------------------------------------------------
115
uint8_t GPS_IsSignalOK(void)
-
 
116
{
108
// This function defines a good GPS signal condition
117
  static uint8_t GPSFix = 0;
109
uint8_t GPS_IsSignalOK(void) {
118
  if( (GPSInfo.status != INVALID)  && (GPSInfo.satfix == SATFIX_3D) && (GPSInfo.flags & FLAG_GPSFIXOK) && ((GPSInfo.satnum >= staticParams.NaviGpsMinSat) || GPSFix))
-
 
119
    {
110
  static uint8_t GPSFix = 0;
120
      GPSFix = 1;
111
  if( (GPSInfo.status != INVALID)  && (GPSInfo.satfix == SATFIX_3D) && (GPSInfo.flags & FLAG_GPSFIXOK) && ((GPSInfo.satnum >= staticParams.NaviGpsMinSat) || GPSFix)) {
121
      return(1);
-
 
122
 
112
    GPSFix = 1;
-
 
113
    return 1;
123
    }
114
  }
124
  else return (0);
115
  else return (0);
125
 
116
}
126
}
-
 
127
// ---------------------------------------------------------------------------------
117
 
128
// rescale xy-vector length to  limit
118
// ---------------------------------------------------------------------------------
129
uint8_t GPS_LimitXY(int32_t *x, int32_t *y, int32_t limit)
119
// rescale xy-vector length to  limit
130
{
120
uint8_t GPS_LimitXY(int32_t *x, int32_t *y, int32_t limit) {
131
  uint8_t retval = 0;
-
 
132
  int32_t len;
121
  uint8_t retval = 0;
133
  len = (int32_t)c_sqrt(*x * *x + *y * *y);
122
  int32_t len;
134
  if (len > limit)
123
  len = (int32_t)c_sqrt(*x * *x + *y * *y);
135
    {
124
  if (len > limit) {
136
      // normalize control vector components to the limit
125
    // normalize control vector components to the limit
137
      *x  = (*x  * limit) / len;
126
    *x  = (*x  * limit) / len;
138
      *y  = (*y  * limit) / len;
127
    *y  = (*y  * limit) / len;
Line 139... Line 128...
139
      retval = 1;
128
    retval = 1;
140
    }
129
  }
141
  return(retval);
-
 
142
}
130
  return(retval);
143
 
131
}
144
// checks nick and roll sticks for manual control
132
 
Line 145... Line 133...
145
uint8_t GPS_IsManualControlled(void)
133
// checks nick and roll sticks for manual control
146
{
134
uint8_t GPS_IsManualControlled(void) {
147
  if ( (abs(PPM_in[staticParams.ChannelAssignment[CH_NICK]]) < staticParams.NaviStickThreshold) && (abs(PPM_in[staticParams.ChannelAssignment[CH_ROLL]]) < staticParams.NaviStickThreshold)) return 0;
-
 
148
  else return 1;
135
  if ((abs(PPM_in[staticParams.ChannelAssignment[CH_NICK]]) < staticParams.NaviStickThreshold) && (abs(PPM_in[staticParams.ChannelAssignment[CH_ROLL]]) < staticParams.NaviStickThreshold)) return 0;
149
}
136
  else return 1;
Line 150... Line -...
150
 
-
 
151
// set given position to current gps position
137
}
152
uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos)
138
 
153
{
139
// set given position to current gps position
154
  uint8_t retval = 0;
140
uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos) {
155
  if(pGPSPos == NULL) return(retval);   // bad pointer
141
  uint8_t retval = 0;
156
 
142
  if(pGPSPos == NULL) return(retval);   // bad pointer
157
  if(GPS_IsSignalOK())
-
 
158
    {   // is GPS signal condition is fine
-
 
159
      pGPSPos->Longitude        = GPSInfo.longitude;
143
 
160
      pGPSPos->Latitude = GPSInfo.latitude;
144
  if(GPS_IsSignalOK()) {        // is GPS signal condition is fine
161
      pGPSPos->Altitude = GPSInfo.altitude;
145
    pGPSPos->Longitude  = GPSInfo.longitude;
162
      pGPSPos->Status   = NEWDATA;
146
    pGPSPos->Latitude   = GPSInfo.latitude;
163
      retval = 1;
147
    pGPSPos->Altitude   = GPSInfo.altitude;
164
    }
148
    pGPSPos->Status     = NEWDATA;
Line 165... Line 149...
165
  else
149
    retval = 1;
166
    {   // bad GPS signal condition
150
  } else {      // bad GPS signal condition
167
      pGPSPos->Status = INVALID;
-
 
168
      retval = 0;
151
    pGPSPos->Status = INVALID;
-
 
152
    retval = 0;
169
    }
153
  }
170
  return(retval);
154
  return(retval);
171
}
-
 
172
 
155
}
173
// clear position
156
 
174
uint8_t GPS_ClearPosition(GPS_Pos_t * pGPSPos)
157
// clear position
175
{
158
uint8_t GPS_ClearPosition(GPS_Pos_t * pGPSPos) {
176
  uint8_t retval = 0;
159
  uint8_t retval = 0;
177
  if(pGPSPos == NULL) return(retval);   // bad pointer
160
  if(pGPSPos == NULL)
178
  else
161
    return retval;      // bad pointer
179
    {
162
  else {
Line 180... Line 163...
180
      pGPSPos->Longitude        = 0;
163
    pGPSPos->Longitude  = 0;
181
      pGPSPos->Latitude = 0;
164
    pGPSPos->Latitude   = 0;
182
      pGPSPos->Altitude = 0;
-
 
183
      pGPSPos->Status   = INVALID;
165
    pGPSPos->Altitude   = 0;
184
      retval = 1;
166
    pGPSPos->Status     = INVALID;
185
    }
167
    retval = 1;
Line 186... Line 168...
186
  return (retval);
168
  }
187
}
169
  return (retval);
188
 
170
}
189
// disable GPS control sticks
171
 
190
void GPS_Neutral(void)
-
 
191
{
172
// disable GPS control sticks
192
  GPSStickNick = 0;
173
void GPS_Neutral(void) {
193
  GPSStickRoll = 0;
174
  GPSStickNick = 0;
194
}
175
  GPSStickRoll = 0;
195
 
176
}
196
// calculates the GPS control stick values from the deviation to target position
177
 
197
// if the pointer to the target positin is NULL or is the target position invalid
178
// calculates the GPS control stick values from the deviation to target position
198
// then the P part of the controller is deactivated.
179
// if the pointer to the target positin is NULL or is the target position invalid
Line 199... Line 180...
199
void GPS_PIDController(GPS_Pos_t *pTargetPos)
180
// then the P part of the controller is deactivated.
200
{
181
void GPS_PIDController(GPS_Pos_t *pTargetPos) {
201
  static int32_t PID_Nick, PID_Roll;
-
 
202
  int32_t coscompass, sincompass;
-
 
203
  int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm
182
  static int32_t PID_Nick, PID_Roll;
204
  int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0;
183
  int32_t coscompass, sincompass;
205
  int32_t PID_North = 0, PID_East = 0;
184
  int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm
206
  static int32_t cos_target_latitude = 1;
185
  int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0;
207
  static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0;
186
  int32_t PID_North = 0, PID_East = 0;
Line 267... Line 246...
267
 
246
   
268
      // I-Part
247
    // I-Part
269
      I_North = ((int32_t)dynamicParams.NaviGpsI * GPSPosDevIntegral_North)/8192;
248
    I_North = ((int32_t)dynamicParams.NaviGpsI * GPSPosDevIntegral_North)/8192;
Line 270... Line -...
270
      I_East =  ((int32_t)dynamicParams.NaviGpsI * GPSPosDevIntegral_East)/8192;
-
 
271
 
249
    I_East =  ((int32_t)dynamicParams.NaviGpsI * GPSPosDevIntegral_East)/8192;
272
 
250
   
273
      // combine P & I
251
    // combine P & I
274
      PID_North = P_North + I_North;
252
    PID_North = P_North + I_North;
275
      PID_East  = P_East  + I_East;
253
    PID_East  = P_East  + I_East;
Line 282... Line 260...
282
 
260
   
283
      // combine PI- and D-Part
261
    // combine PI- and D-Part
284
      PID_North += D_North;
262
    PID_North += D_North;
Line 285... Line -...
285
      PID_East  += D_East;
-
 
286
 
263
    PID_East  += D_East;
287
 
264
   
288
      // scale combination with gain.
265
    // scale combination with gain.
Line 289... Line 266...
289
      PID_North = (PID_North * (int32_t)dynamicParams.NaviGpsGain) / 100;
266
    PID_North = (PID_North * (int32_t)dynamicParams.NaviGpsGain) / 100;
290
      PID_East  = (PID_East  * (int32_t)dynamicParams.NaviGpsGain) / 100;
-
 
291
 
267
    PID_East  = (PID_East  * (int32_t)dynamicParams.NaviGpsGain) / 100;
292
      // GPS to nick and roll settings
268
 
293
 
269
    // GPS to nick and roll settings
294
      // A positive nick angle moves head downwards (flying forward).
270
    // A positive nick angle moves head downwards (flying forward).
295
      // A positive roll angle tilts left side downwards (flying left).
271
    // A positive roll angle tilts left side downwards (flying left).
Line 306... Line 282...
306
      coscompass = (int32_t)c_cos_8192(YawGyroHeading / GYRO_DEG_FACTOR);
282
    coscompass = (int32_t)c_cos_8192(YawGyroHeading / GYRO_DEG_FACTOR);
307
      sincompass = (int32_t)c_sin_8192(YawGyroHeading / GYRO_DEG_FACTOR);
283
    sincompass = (int32_t)c_sin_8192(YawGyroHeading / GYRO_DEG_FACTOR);
308
      PID_Nick =   (coscompass * PID_North + sincompass * PID_East) / 8192;
284
    PID_Nick =   (coscompass * PID_North + sincompass * PID_East) / 8192;
309
      PID_Roll  =  (sincompass * PID_North - coscompass * PID_East) / 8192;
285
    PID_Roll  =  (sincompass * PID_North - coscompass * PID_East) / 8192;
Line 310... Line -...
310
 
-
 
311
 
286
   
312
      // limit resulting GPS control vector
287
    // limit resulting GPS control vector
Line 313... Line 288...
313
      GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT);
288
    GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT);
314
 
289
   
Line 322... Line 297...
322
      GPSPosDevIntegral_North = 0;
297
      GPSPosDevIntegral_North = 0;
323
      GPSPosDevIntegral_East = 0;
298
      GPSPosDevIntegral_East = 0;
324
    }
299
    }
325
}
300
}
Line 326... Line -...
326
 
-
 
327
 
-
 
328
 
-
 
329
 
301
 
330
void GPS_Main(void)
-
 
331
{
302
void GPS_Main(void) {
332
  static uint8_t GPS_P_Delay = 0;
303
  static uint8_t GPS_P_Delay = 0;
Line 333... Line 304...
333
  static uint16_t beep_rythm = 0;
304
  static uint16_t beep_rythm = 0;
Line 334... Line 305...
334
 
305
 
335
  GPS_UpdateParameter();
306
  GPS_UpdateParameter();
336
 
-
 
337
  // store home position if start of flight flag is set
307
 
338
  if(MKFlags & MKFLAG_CALIBRATE)
308
  // store home position if start of flight flag is set
Line 339... Line 309...
339
    {
309
  if(MKFlags & MKFLAG_CALIBRATE) {
340
      if(GPS_SetCurrPosition(&HomePosition)) BeepTime = 700;
-
 
341
    }
310
    if(GPS_SetCurrPosition(&HomePosition)) BeepTime = 700;
342
 
311
  }
343
  switch(GPSInfo.status)
312
 
344
    {
-
 
345
    case INVALID:  // invalid gps data
313
  switch(GPSInfo.status) {
346
      GPS_Neutral();
314
  case INVALID:  // invalid gps data
347
      if(FlightMode != GPS_FLIGHT_MODE_FREE)
315
    GPS_Neutral();
348
        {
316
    if(FlightMode != GPS_FLIGHT_MODE_FREE) {
349
          BeepTime = 100; // beep if signal is neccesary
317
      BeepTime = 100; // beep if signal is neccesary