Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 72 → Rev 73

/trunk/GPS.c
95,11 → 95,15
float D;
float A;
float ACC;
s32 P_Limit;
s32 I_Limit;
s32 D_Limit;
s32 PID_Limit;
u32 BrakingDuration;
u8 MinSat;
s8 StickThreshold;
float WindCorrection;
s32 OperatingRadius;
u32 NaviAngleLimitation;
GPS_FlightMode_t FlightMode;
} __attribute__((packed)) GPS_Parameter_t;
 
111,7 → 115,9
s32 Bearing; // in deg
s32 Distance; // in cm
} __attribute__((packed)) GPS_Deviation_t;
GPS_Deviation_t TargetDeviation;
GPS_Deviation_t CurrentTargetDeviation; // Deviation from Target
GPS_Deviation_t CurrentHomeDeviation; // Deviation from Home
GPS_Deviation_t TargetHomeDeviation; // Deviation from Target to Home
 
GPS_Stick_t GPS_Stick;
GPS_Parameter_t GPS_Parameter;
137,11 → 143,16
GPS_Parameter.D = (float) 90;
GPS_Parameter.A = (float) 90;
GPS_Parameter.ACC = (float) 0;
GPS_Parameter.P_Limit = 90;
GPS_Parameter.I_Limit = 90;
GPS_Parameter.D_Limit = 90;
GPS_Parameter.PID_Limit = 200;
GPS_Parameter.BrakingDuration = 0;
GPS_Parameter.MinSat = 6;
GPS_Parameter.StickThreshold = 8;
GPS_Parameter.WindCorrection = 0.0;
GPS_Parameter.OperatingRadius = 0; // forces the aircraft to fly to home positon
GPS_Parameter.NaviAngleLimitation = 125;
 
}
else
{
159,11 → 170,15
GPS_Parameter.D = (float)Parameter.NaviGpsD;
GPS_Parameter.A = (float)Parameter.NaviGpsD;
GPS_Parameter.ACC = (float)Parameter.NaviGpsACC;
GPS_Parameter.P_Limit = (s32)Parameter.NaviGpsPLimit;
GPS_Parameter.I_Limit = (s32)Parameter.NaviGpsILimit;
GPS_Parameter.D_Limit = (s32)Parameter.NaviGpsDLimit;
GPS_Parameter.PID_Limit = 2* (s32)Parameter.NaviAngleLimitation;
GPS_Parameter.BrakingDuration = (u32)Parameter.NaviPH_LoginTime;
GPS_Parameter.MinSat = (u8)Parameter.NaviGpsMinSat;
GPS_Parameter.StickThreshold = (s8)Parameter.NaviStickThreshold;
GPS_Parameter.WindCorrection = (float)Parameter.NaviWindCorrection;
GPS_Parameter.OperatingRadius = (s32)Parameter.NaviOperatingRadius * 100; // conversion of m to cm
GPS_Parameter.NaviAngleLimitation = (u32) Parameter.NaviAngleLimitation * 2;
}
// FlightMode changed?
if(GPS_Parameter.FlightMode != FlightMode_Old) BeepTime = 100; // beep to indicate that mode has switched
263,10 → 278,15
{
s32 dist;
dist = (s32)hypot(*x,*y); // the length of the vector
if ((dist != 0L) && (dist > limit))
if (dist == 0)
{
*x = 0;
*y = 0;
}
else if (dist > limit)
// if vector length is larger than the given limit
{ // scale vector compontents so that the length is cut off to limit
*x = (*x * limit) / dist;
*x = (*x * limit) / dist;
*y = (*y * limit) / dist;
}
}
287,9 → 307,9
 
//------------------------------------------------------------
// calculate the deviation from the current position to the target position
u8 GPS_CalculateDeviation(GPS_Pos_t * pCurrentPos, GPS_Pos_t * pTargetPos)
u8 GPS_CalculateDeviation(GPS_Pos_t * pCurrentPos, GPS_Pos_t * pTargetPos, GPS_Deviation_t* pDeviationFromTarget)
{
double temp1, temp2;
double temp1, temp2;
// if given pointer is NULL
if((pCurrentPos == NULL) || (pTargetPos == NULL)) goto baddata;
// if positions are invalid
309,7 → 329,7
// In our application we wont fly more than 20.000 km but along the date line this is important.
if(temp1 > 180.0f) temp1 -= 360.0f;
else if (temp1 < -180.0f) temp1 += 360.0f;
temp1 *= cos(RadiansFromGPS(pTargetPos->Latitude));
temp1 *= cos((RadiansFromGPS(pTargetPos->Latitude) + RadiansFromGPS(pCurrentPos->Latitude))/2);
// calculate latitude deviation from target
// this is allways within -180 deg ... 180 deg
temp2 = DegFromGPS(pCurrentPos->Latitude) - DegFromGPS(pTargetPos->Latitude);
316,24 → 336,24
// deviation from target position in cm
// i.e. the distance to walk from the target in northern and eastern direction to reach the current position
 
TargetDeviation.Status = INVALID;
TargetDeviation.North = (s32)(11119492.7f * temp2);
TargetDeviation.East = (s32)(11119492.7f * temp1);
pDeviationFromTarget->Status = INVALID;
pDeviationFromTarget->North = (s32)(11119492.7f * temp2);
pDeviationFromTarget->East = (s32)(11119492.7f * temp1);
// If the position deviation is small enough to neglect the earth curvature
// (this is for our application always fulfilled) the distance to target
// can be calculated by the pythagoras of north and east deviation.
TargetDeviation.Distance = (s32)(11119492.7f * hypot(temp1, temp2));
if (TargetDeviation.Distance == 0L) TargetDeviation.Bearing = 0L;
else TargetDeviation.Bearing = DirectionToTarget_N_E(temp2, temp1);
TargetDeviation.Status = NEWDATA;
pDeviationFromTarget->Distance = (s32)(11119492.7f * hypot(temp1, temp2));
if (pDeviationFromTarget->Distance == 0L) pDeviationFromTarget->Bearing = 0L;
else pDeviationFromTarget->Bearing = DirectionToTarget_N_E(temp2, temp1);
pDeviationFromTarget->Status = NEWDATA;
return TRUE;
 
baddata:
TargetDeviation.North = 0L;
TargetDeviation.East = 0L;
TargetDeviation.Distance = 0L;
TargetDeviation.Bearing = 0L;
TargetDeviation.Status = INVALID;
pDeviationFromTarget->North = 0L;
pDeviationFromTarget->East = 0L;
pDeviationFromTarget->Distance = 0L;
pDeviationFromTarget->Bearing = 0L;
pDeviationFromTarget->Status = INVALID;
return FALSE;
}
 
359,6 → 379,7
{
case INVALID: // no gps data available
// do nothing
GPS_Parameter.PID_Limit = 0; // disables PID output
break;
 
case PROCESSED: // the current data have been allready processed
384,6 → 405,9
// If GPS signal condition is sufficient for a reliable position measurement
if(GPS_IsSignalOK())
{
// update home deviation info
GPS_CalculateDeviation(&(GPSData.Position), &GPS_HomePosition, &CurrentHomeDeviation);
// if the MK is starting or the home position is invalid then store the home position
if((FC.MKFlags & MKFLAG_START) || (GPS_HomePosition.Status == INVALID))
{ // try to update the home position from the current position
404,7 → 428,7
case GPS_FLIGHT_MODE_FREE:
NaviData.NCFlags &= ~(NC_FLAG_PH | NC_FLAG_CH);
NaviData.NCFlags |= NC_FLAG_FREE;
 
GPS_Parameter.PID_Limit = 0; // disables PID output
// update hold position
GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
// no target position
420,6 → 444,7
 
if(GPS_IsManuallyControlled())
{
GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active
GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
GPS_pTargetPosition = NULL;
}
434,73 → 459,83
case GPS_FLIGHT_MODE_WAYPOINT:
NaviData.NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_PH);
NaviData.NCFlags |= NC_FLAG_CH;
 
// waypoint trigger logic
if(GPS_pWaypoint != NULL) // waypoint exist
if(GPS_IsManuallyControlled()) // the human pilot takes the action
{
if(GPS_pWaypoint->Position.Status == INVALID) // should never happen
GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active
GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition); // update hold position
GPS_pTargetPosition = NULL; // set target position invalid
}
else // no manual control -> gps position hold active
{
// waypoint trigger logic
if(GPS_pWaypoint != NULL) // waypoint exist
{
GPS_pWaypoint = WPList_Next(); // goto to next WP
WPArrived = FALSE;
BeepTime = 255;
}
else // waypoint position is valid
{
// check if the pointer to the waypoint has been changed or the data have been updated
if((GPS_pWaypoint != GPS_pWaypointOld) || (GPS_pWaypoint->Position.Status == NEWDATA))
if(GPS_pWaypoint->Position.Status == INVALID) // should never happen
{
GPS_pWaypointOld = GPS_pWaypoint;
// reset the arrived bit to break a pending HoldTime of the old WP
GPS_pWaypoint = WPList_Next(); // goto to next WP
WPArrived = FALSE;
BeepTime = 255;
}
 
if(TargetDeviation.Status != INVALID)
{ // if the waypoint was not catched and the target area has been reached
if(!WPArrived && (TargetDeviation.Distance < (GPS_pWaypoint->ToleranceRadius * 100)))
else // waypoint position is valid
{
// check if the pointer to the waypoint has been changed or the data have been updated
if((GPS_pWaypoint != GPS_pWaypointOld) || (GPS_pWaypoint->Position.Status == NEWDATA))
{
WPArrived = TRUE;
WPTime = SetDelay(GPS_pWaypoint->HoldTime * 1000); // set hold time stamp
GPS_pWaypointOld = GPS_pWaypoint;
// reset the arrived bit to break a pending HoldTime of the old WP
WPArrived = FALSE;
}
if(CurrentTargetDeviation.Status != INVALID)
{ // if the waypoint was not catched and the target area has been reached
if(!WPArrived && (CurrentTargetDeviation.Distance < (GPS_pWaypoint->ToleranceRadius * 100)))
{
WPArrived = TRUE;
WPTime = SetDelay(GPS_pWaypoint->HoldTime * 1000); // set hold time stamp
}
}
// if WP has been reached once, wait hold time before trigger to next one
if(WPArrived)
{
/* ToDo: Adjust GPS_pWaypoint->Heading, GPS_pWaypoint->Event handling */
if(CheckDelay(WPTime))
{
GPS_pWaypoint = WPList_Next(); // goto to next waypoint, return NULL if end of list has been reached
WPArrived = FALSE; // which is not arrived
}
} // EOF if(WPArrived)
}
// if WP has been reached once, wait hold time before trigger to next one
if(WPArrived)
{
/* ToDo: Adjust GPS_pWaypoint->Heading, GPS_pWaypoint->Event handling */
if(CheckDelay(WPTime))
{
GPS_pWaypoint = WPList_Next(); // goto to next waypoint, return NULL if end of list has been reached
WPArrived = FALSE; // which is not arrived
}
} // EOF if(WPArrived)
}
} // EOF waypoint trigger logic
 
if(GPS_pWaypoint != NULL) // Waypoint exist
{
// update the hold position
GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
GPS_pTargetPosition = &(GPS_pWaypoint->Position);
}
else // no waypoint info available, i.e. the WPList is empty or the end of the list has been reached
{
// fly back to home postion
if(GPS_HomePosition.Status == INVALID)
} // EOF waypoint trigger logic
if(GPS_pWaypoint != NULL) // Waypoint exist
{
GPS_pTargetPosition = &GPS_HoldPosition; // fall back to hold mode if home position is not available
BeepTime = 255; // beep to indicate missin home position
}
else // the home position is valid
{
// update the hold position
GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
// set target to home position
GPS_pTargetPosition = &GPS_HomePosition;
GPS_pTargetPosition = &(GPS_pWaypoint->Position);
}
}
else // no waypoint info available, i.e. the WPList is empty or the end of the list has been reached
{
// fly back to home postion
if(GPS_HomePosition.Status == INVALID)
{
GPS_pTargetPosition = &GPS_HoldPosition; // fall back to hold mode if home position is not available
BeepTime = 255; // beep to indicate missin home position
}
else // the home position is valid
{
// update the hold position
GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
// set target to home position
GPS_pTargetPosition = &GPS_HomePosition;
}
}
} // EOF no manual control
break;
 
case GPS_FLIGHT_MODE_UNDEF:
default:
GPS_Parameter.PID_Limit = 0; // disables PID output
// update hold position
GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
// no target position
518,22 → 553,27
{ // if the target position has been changed or the value has been updated or the OperatingRadius has changed
if((GPS_pTargetPosition != pTargetPositionOld) || (GPS_pTargetPosition->Status == NEWDATA) || (GPS_Parameter.OperatingRadius != OperatingRadiusOld) )
{
BeepTime = 255; // beep to indicate setting of a new target position
// calculate deviation of new target position from home position
if(GPS_CalculateDeviation(GPS_pTargetPosition, &GPS_HomePosition))
if(GPS_CalculateDeviation(GPS_pTargetPosition, &GPS_HomePosition, &TargetHomeDeviation))
{
// check distance from home position
if(TargetDeviation.Distance > GPS_Parameter.OperatingRadius)
if(TargetHomeDeviation.Distance > GPS_Parameter.OperatingRadius)
{
//calculate ranged target position to be within the operation radius area
NaviData.NCFlags |= NC_FLAG_RANGE_LIMIT;
TargetDeviation.Distance = GPS_Parameter.OperatingRadius;
GPS_LimitXY(&(TargetDeviation.East), &(TargetDeviation.North), TargetDeviation.Distance);
 
TargetHomeDeviation.North *= GPS_Parameter.OperatingRadius;
TargetHomeDeviation.North /= TargetHomeDeviation.Distance;
TargetHomeDeviation.East *= GPS_Parameter.OperatingRadius;
TargetHomeDeviation.East /= TargetHomeDeviation.Distance;
TargetHomeDeviation.Distance = GPS_Parameter.OperatingRadius;
 
RangedTargetPosition.Status = INVALID;
RangedTargetPosition.Latitude = GPS_HomePosition.Latitude;
RangedTargetPosition.Latitude += (s32)((float)TargetDeviation.North / 1.11194927f);
RangedTargetPosition.Latitude += (s32)((float)TargetHomeDeviation.North / 1.11194927f);
RangedTargetPosition.Longitude = GPS_HomePosition.Longitude;
RangedTargetPosition.Longitude += (s32)((float)TargetDeviation.East / (1.11194927f * cos(RadiansFromGPS(GPS_HomePosition.Latitude))) );
RangedTargetPosition.Longitude += (s32)((float)TargetHomeDeviation.East / (1.11194927f * cos(RadiansFromGPS(GPS_HomePosition.Latitude))) );
RangedTargetPosition.Altitude = GPS_pTargetPosition->Altitude;
RangedTargetPosition.Status = NEWDATA;
}
558,10 → 598,10
/* Calculate position deviation from ranged target */
 
// calculate deviation of current position to ranged target position in cm
if(GPS_CalculateDeviation(&(GPSData.Position), &RangedTargetPosition))
if(GPS_CalculateDeviation(&(GPSData.Position), &RangedTargetPosition, &CurrentTargetDeviation))
{
// implement your control code here based
// in the info available in the TargetDeviation, GPSData and FromFlightCtrl.GyroHeading
// in the info available in the CurrentTargetDeviation, GPSData and FromFlightCtrl.GyroHeading
GPS_Stick.Nick = 0;
GPS_Stick.Roll = 0;
GPS_Stick.Yaw = 0;
586,25 → 626,29
break;
}
 
DebugOut.Analog[27] = (s16)TargetDeviation.North;
DebugOut.Analog[28] = (s16)TargetDeviation.East;
DebugOut.Analog[27] = (s16)CurrentTargetDeviation.North;
DebugOut.Analog[28] = (s16)CurrentTargetDeviation.East;
DebugOut.Analog[29] = GPS_Stick.Nick;
DebugOut.Analog[30] = GPS_Stick.Roll;
 
// update navi data, send back to ground station
GPS_CopyPosition(&(GPSData.Position), &(NaviData.CurrentPosition));
GPS_CopyPosition(&(GPSData.Position), &(NaviData.CurrentPosition));
GPS_CopyPosition(&RangedTargetPosition, &(NaviData.TargetPosition));
GPS_CopyPosition(&GPS_HomePosition, &(NaviData.HomePosition));
NaviData.SatsInUse = GPSData.NumOfSats;
NaviData.TargetPositionDeviation.Distance = TargetDeviation.Distance;
NaviData.TargetPositionDeviation.Bearing = TargetDeviation.Bearing;
NaviData.TargetPositionDeviation.Distance = CurrentTargetDeviation.Distance;
NaviData.TargetPositionDeviation.Bearing = CurrentTargetDeviation.Bearing;
NaviData.HomePositionDeviation.Distance = CurrentHomeDeviation.Distance;
NaviData.HomePositionDeviation.Bearing = CurrentHomeDeviation.Bearing;
NaviData.UBat = FC.UBat;
NaviData.GroundSpeed = (u16)GPSData.Speed_Ground;
NaviData.Heading = (s16)GPSData.Heading;
NaviData.CompassHeading = FromFlightCtrl.GyroHeading / 10; // in deg
NaviData.AngleNick = FromFlightCtrl.AngleNick / 10; // in deg
NaviData.AngleRoll = FromFlightCtrl.AngleRoll / 10; // in deg
NaviData.CompassHeading = (s16)FromFlightCtrl.GyroHeading/10; // in deg
NaviData.AngleNick = FromFlightCtrl.AngleNick / 10; // in deg
NaviData.AngleRoll = FromFlightCtrl.AngleRoll / 10; // in deg
NaviData.RC_Quality = (u8) FC.RC_Quality;
NaviData.MKFlags = (u8)FC.MKFlags;
NaviData.OperatingRadius = Parameter.NaviOperatingRadius;
 
//+++++++++++++++++++++++++++++++++++++++++++++++++++
return;
/trunk/main.h
4,16 → 4,16
 
#define VERSION_MAJOR 0
#define VERSION_MINOR 14
#define VERSION_PATCH 1
#define VERSION_PATCH 2
 
#define VERSION_SERIAL_MAJOR 10
#define VERSION_SERIAL_MINOR 0
#define VERSION_SERIAL_MINOR 0
 
#define FC_SPI_COMPATIBLE 5
#define MK3MAG_I2C_COMPATIBLE 2
 
// sends data on uart0 (without MK-OSD the MKGPS is connect there)
//#define SEND_MKOSD_DATA
//#define SEND_MKOSD_DATA
 
extern u32 TimerCompassUpdate;
extern u16 BeepTime;
/trunk/uart1.h
81,7 → 81,8
u8 MKFlags; // Flags from FC
u8 NCFlags; // Flags from NC
u8 Errorcode; // 0 --> okay
u8 Reserve[8]; // for future use
u8 OperatingRadius; // current operation radius around the Home Position in m
u8 Reserve[7]; // for future use
} __attribute__((packed)) NaviData_t;
 
extern NaviData_t NaviData;