Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 935 → Rev 936

/branches/V0.70d Code Redesign killagreg/gps.c
8,26 → 8,25
#include "rc.h"
#include "eeprom.h"
 
#define TSK_IDLE 0
#define TSK_HOLD 1
#define TSK_HOME 2
typedef enum
{
GPS_FLIGHT_MODE_UNDEF,
GPS_FLIGHT_MODE_FREE,
GPS_FLIGHT_MODE_AID,
GPS_FLIGHT_MODE_HOME,
} FlightMode_t;
 
#define GPS_STICK_SENSE 15 // must be at least in a range where 90% of the trimming does not switch of the GPS function
#define GPS_STICK_LIMIT 45 // limit of gps stick control to avoid critical flight attitudes
#define GPS_STICK_LIMIT 200 // limit of gps stick control to avoid critical flight attitudes
#define GPS_POSDEV_INTEGRAL_LIMIT 32000 // limit for the position error integral
#define GPS_P_LIMIT 25
#define GPS_P_LIMIT 3200
 
 
uint8_t GPS_P_Factor = 0, GPS_I_Factor = 0, GPS_D_Factor = 0;
 
 
 
typedef struct
{
int32_t Longitude;
int32_t Latitude;
int32_t Altitude;
uint8_t Status;
Status_t Status;
} GPS_Pos_t;
 
// GPS coordinates for hold position
34,56 → 33,110
GPS_Pos_t HoldPosition = {0,0,0,INVALID};
// GPS coordinates for home position
GPS_Pos_t HomePosition = {0,0,0,INVALID};
// the current flight mode
FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF;
 
 
// ---------------------------------------------------------------------------------
 
// checks nick and roll sticks for manual control
uint8_t IsManualControlled(void)
void GPS_UpdateParameter(void)
{
if ( (abs(PPM_in[ParamSet.ChannelAssignment[CH_NICK]]) < GPS_STICK_SENSE) && (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < GPS_STICK_SENSE)) return 0;
else return 1;
}
static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF;
 
// set home position to current positon
void GPS_SetHomePosition(void)
{
if( ((GPSInfo.status == VALID) || (GPSInfo.status == PROCESSED)) && GPSInfo.satfix == SATFIX_3D)
if((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING))
{
HomePosition.Longitude = GPSInfo.longitude;
HomePosition.Latitude = GPSInfo.latitude;
HomePosition.Altitude = GPSInfo.altitude;
HomePosition.Status = VALID;
BeepTime = 1000; // signal if new home position was set
FlightMode = GPS_FLIGHT_MODE_FREE;
}
else
{
HomePosition.Status = INVALID;
if (FCParam.NaviGpsModeControl < 50) FlightMode = GPS_FLIGHT_MODE_AID;
else if(FCParam.NaviGpsModeControl < 180) FlightMode = GPS_FLIGHT_MODE_FREE;
else FlightMode = GPS_FLIGHT_MODE_HOME;
}
if (FlightMode != FlightModeOld)
{
BeepTime = 100;
}
FlightModeOld = FlightMode;
}
 
// set hold position to current positon
void GPS_SetHoldPosition(void)
 
 
// ---------------------------------------------------------------------------------
// This function defines a good GPS signal condition
uint8_t GPS_IsSignalOK(void)
{
if( ((GPSInfo.status == VALID) || (GPSInfo.status == PROCESSED)) && GPSInfo.satfix == SATFIX_3D)
static uint8_t GPSFix = 0;
if( (GPSInfo.status != INVALID) && (GPSInfo.satfix == SATFIX_3D) && (GPSInfo.flags & FLAG_GPSFIXOK) && ((GPSInfo.satnum >= ParamSet.NaviGpsMinSat) || GPSFix))
{
HoldPosition.Longitude = GPSInfo.longitude;
HoldPosition.Latitude = GPSInfo.latitude;
HoldPosition.Altitude = GPSInfo.altitude;
HoldPosition.Status = VALID;
GPSFix = 1;
return(1);
 
}
else
else return (0);
 
}
// ---------------------------------------------------------------------------------
// rescale xy-vector length to limit
uint8_t GPS_LimitXY(int32_t *x, int32_t *y, int32_t limit)
{
uint8_t retval = 0;
int32_t len;
len = (int32_t)c_sqrt(*x * *x + *y * *y);
if (len > limit)
{
HoldPosition.Status = INVALID;
// normalize control vector components to the limit
*x = (*x * limit) / len;
*y = (*y * limit) / len;
retval = 1;
}
return(retval);
}
 
// clear home position
void GPS_ClearHomePosition(void)
// checks nick and roll sticks for manual control
uint8_t GPS_IsManualControlled(void)
{
HomePosition.Status = INVALID;
if ( (abs(PPM_in[ParamSet.ChannelAssignment[CH_NICK]]) < ParamSet.NaviStickThreshold) && (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < ParamSet.NaviStickThreshold)) return 0;
else return 1;
}
 
// set given position to current gps position
uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos)
{
uint8_t retval = 0;
if(pGPSPos == NULL) return(retval); // bad pointer
 
if(GPS_IsSignalOK())
{ // is GPS signal condition is fine
pGPSPos->Longitude = GPSInfo.longitude;
pGPSPos->Latitude = GPSInfo.latitude;
pGPSPos->Altitude = GPSInfo.altitude;
pGPSPos->Status = NEWDATA;
retval = 1;
}
else
{ // bad GPS signal condition
pGPSPos->Status = INVALID;
retval = 0;
}
return(retval);
}
 
// clear position
uint8_t GPS_ClearPosition(GPS_Pos_t * pGPSPos)
{
uint8_t retval = 0;
if(pGPSPos == NULL) return(retval); // bad pointer
else
{
pGPSPos->Longitude = 0;
pGPSPos->Latitude = 0;
pGPSPos->Altitude = 0;
pGPSPos->Status = INVALID;
retval = 1;
}
return (retval);
}
 
// disable GPS control sticks
void GPS_Neutral(void)
{
96,7 → 149,7
// then the P part of the controller is deactivated.
void GPS_PIDController(GPS_Pos_t *pTargetPos)
{
int32_t temp, temp1, PID_Nick, PID_Roll;
int32_t PID_Nick, PID_Roll;
int32_t coscompass, sincompass;
int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm
int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0;
106,7 → 159,7
static GPS_Pos_t *pLastTargetPos = 0;
 
// if GPS data and Compass are ok
if((GPSInfo.status == VALID) && (GPSInfo.satfix == SATFIX_3D) && (CompassHeading >= 0) )
if( GPS_IsSignalOK() && (CompassHeading >= 0) )
{
 
if(pTargetPos != NULL) // if there is a target position
156,28 → 209,21
//Calculate PID-components of the controller (negative sign for compensation)
 
// P-Part
P_North = -((int32_t)GPS_P_Factor * GPSPosDev_North)/2048;
P_East = -((int32_t)GPS_P_Factor * GPSPosDev_East)/2048;
P_North = -((int32_t)FCParam.NaviGpsP * GPSPosDev_North)/16;
P_East = -((int32_t)FCParam.NaviGpsP * GPSPosDev_East)/16;
 
// I-Part
I_North = -((int32_t)GPS_I_Factor * GPSPosDevIntegral_North)/8192;
I_East = -((int32_t)GPS_I_Factor * GPSPosDevIntegral_East)/8192;
I_North = -((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_North)/64;
I_East = -((int32_t)FCParam.NaviGpsI * GPSPosDevIntegral_East)/64;
 
// combine P- & I-Part
PID_North = P_North + I_North;
PID_East = P_East + I_East;
 
//limit PI-Part to limit the max velocity
//temp1 = ((int32_t)GPS_D_Factor * MAX_VELOCITY)/512; // the PI-Part limit
temp = (int32_t)c_sqrt(PID_North*PID_North + PID_East*PID_East); // the current PI-Part
if(temp > GPS_P_LIMIT) // P-Part limit is reached
//limit PI-Part
if(!GPS_LimitXY(&PID_North, &PID_East, GPS_P_LIMIT))
{
// normalize P-part components to the P-Part limit
PID_North = (PID_North * GPS_P_LIMIT)/temp;
PID_East = (PID_East * GPS_P_LIMIT)/temp;
}
else // PI-Part under its limit
{
// P-Part limit is not reached
// update position error integrals
GPSPosDevIntegral_North += GPSPosDev_North/16;
if( GPSPosDevIntegral_North > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = GPS_POSDEV_INTEGRAL_LIMIT;
188,14 → 234,17
}
 
// D-Part
D_North = -((int32_t)GPS_D_Factor * GPSInfo.velnorth)/512;
D_East = -((int32_t)GPS_D_Factor * GPSInfo.veleast)/512;
D_North = -((int32_t)FCParam.NaviGpsD * GPSInfo.velnorth)/4;
D_East = -((int32_t)FCParam.NaviGpsD * GPSInfo.veleast)/4;
 
 
// combine PI- and D-Part
PID_North += D_North;
PID_East += D_East;
 
// scale combination with gain.
PID_North = (PID_North * (int32_t)FCParam.NaviGpsGain) / (100 * 32);
PID_East = (PID_East * (int32_t)FCParam.NaviGpsGain) / (100 * 32);
 
// GPS to nick and roll settings
 
// A positive nick angle moves head downwards (flying forward).
210,23 → 259,16
// in the fc.c. Therefore a positive north deviation/velocity should result in a positive
// GPS_Nick and a positive east deviation/velocity should result in a negative GPS_Roll.
 
coscompass = (int32_t)c_cos_8192(CompassHeading);
sincompass = (int32_t)c_sin_8192(CompassHeading);
coscompass = (int32_t)c_cos_8192(YawGyroHeading / YAW_GYRO_DEG_FACTOR);
sincompass = (int32_t)c_sin_8192(YawGyroHeading / YAW_GYRO_DEG_FACTOR);
PID_Roll = (coscompass * PID_East - sincompass * PID_North) / 8192;
PID_Nick = -1*((sincompass * PID_East + coscompass * PID_North) / 8192);
 
// limit resulting GPS control vector
temp = (int32_t)c_sqrt(PID_Roll*PID_Roll + PID_Nick*PID_Nick);
if (temp > GPS_STICK_LIMIT)
{
// normalize control vector components to the limit
PID_Roll = (PID_Roll * GPS_STICK_LIMIT)/temp;
PID_Nick = (PID_Nick * GPS_STICK_LIMIT)/temp;
}
GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT);
 
GPS_Roll = (int16_t)PID_Roll;
GPS_Nick = (int16_t)PID_Nick;
 
GPS_Roll = (int16_t)PID_Roll;
}
else // invalid GPS data or bad compass reading
{
240,23 → 282,24
 
 
 
void GPS_Main(uint8_t ctrl)
void GPS_Main(void)
{
static uint8_t GPS_Task = TSK_IDLE;
static uint8_t GPS_P_Delay = 0;
int16_t satbeep;
uint16_t beep_rythm = 0;
 
// ctrl enables the gps feature
if(ctrl < 70) GPS_Task = TSK_IDLE;
else if (ctrl < 160) GPS_Task = TSK_HOLD;
else GPS_Task = TSK_HOME; // ctrl >= 160
GPS_UpdateParameter();
 
// store home position if start of flight flag is set
if(MKFlags & MKFLAG_CALIBRATE)
{
GPS_SetCurrPosition(&HomePosition);
}
 
switch(GPSInfo.status)
{
case INVALID: // invalid gps data
GPS_Neutral();
if(GPS_Task != TSK_IDLE)
if(FlightMode != GPS_FLIGHT_MODE_FREE)
{
BeepTime = 100; // beep if signal is neccesary
}
272,25 → 315,28
GPSInfo.status = INVALID;
}
break;
case VALID: // new valid data from gps device
case NEWDATA: // new valid data from gps device
// if the gps data quality is good
if (GPSInfo.satfix == SATFIX_3D)
beep_rythm++;
 
if (GPS_IsSignalOK())
{
switch(GPS_Task) // check what's to do
switch(FlightMode) // check what's to do
{
case TSK_IDLE:
case GPS_FLIGHT_MODE_FREE:
// update hold position to current gps position
GPS_SetHoldPosition(); // can get invalid if gps signal is bad
GPS_SetCurrPosition(&HoldPosition); // can get invalid if gps signal is bad
// disable gps control
GPS_Neutral();
break; // eof TSK_IDLE
case TSK_HOLD:
break;
 
case GPS_FLIGHT_MODE_AID:
if(HoldPosition.Status != INVALID)
{
if( IsManualControlled() ) // MK controlled by user
if( GPS_IsManualControlled() ) // MK controlled by user
{
// update hold point to current gps position
GPS_SetHoldPosition();
GPS_SetCurrPosition(&HoldPosition);
// disable gps control
GPS_Neutral();
GPS_P_Delay = 0;
297,10 → 343,10
}
else // GPS control active
{
if(GPS_P_Delay<7)
if(GPS_P_Delay < 7)
{ // delayed activation of P-Part for 8 cycles (8*0.25s = 2s)
GPS_P_Delay++;
GPS_SetHoldPosition(); // update hold point to current gps position
GPS_SetCurrPosition(&HoldPosition); // update hold point to current gps position
GPS_PIDController(NULL); // activates only the D-Part
}
else GPS_PIDController(&HoldPosition);// activates the P&D-Part
308,17 → 354,18
}
else // invalid Hold Position
{ // try to catch a valid hold position from gps data input
GPS_SetHoldPosition();
GPS_SetCurrPosition(&HoldPosition);
GPS_Neutral();
}
break; // eof TSK_HOLD
case TSK_HOME:
break;
 
case GPS_FLIGHT_MODE_HOME:
if(HomePosition.Status != INVALID)
{
// update hold point to current gps position
// to avoid a flight back if home comming is deactivated
GPS_SetHoldPosition();
if( IsManualControlled() ) // MK controlled by user
GPS_SetCurrPosition(&HoldPosition);
if( GPS_IsManualControlled() ) // MK controlled by user
{
GPS_Neutral();
}
334,7 → 381,7
 
if (HoldPosition.Status != INVALID)
{
if( IsManualControlled() ) // MK controlled by user
if( GPS_IsManualControlled() ) // MK controlled by user
{
GPS_Neutral();
}
345,7 → 392,7
}
else
{ // try to catch a valid hold position
GPS_SetHoldPosition();
GPS_SetCurrPosition(&HoldPosition);
GPS_Neutral();
}
}
354,16 → 401,13
GPS_Neutral();
break; // eof default
} // eof switch GPS_Task
} // eof 3D-FIX
else // no 3D-SATFIX
} // eof gps data quality is good
else // gps data quality is bad
{ // disable gps control
GPS_Neutral();
if(GPS_Task != TSK_IDLE)
{
satbeep = 1600 - (int16_t)GPSInfo.satnum * 200; // is zero at 8 sats
if (satbeep < 0) satbeep = 0;
BeepTime = 50 + (uint16_t)satbeep; // max 1650 * 0.1 ms =
}
// beep if signal is not sufficient
if(!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100;
else if (GPSInfo.satnum < ParamSet.NaviGpsMinSat && !(beep_rythm % 5)) BeepTime = 10;
}
// set current data as processed to avoid further calculations on the same gps data
GPSInfo.status = PROCESSED;