private void CheckForSequence(George george, Vessel vessel, VesselData vdata)
{
if (next == null)
return;
planet = vessel.mainBody;
var nextNext = next.next;
// do not skip over courses that we have never been on
// otherwise it can auto-sequence through a whole series of things that
// the player meant to fly.
if (!courseStatus.beenValid)
{
return;
}
// have to figure out if we have passed next yet
// looking at distance not good enough, should look for
// either reversal of heading neeeded or closer to next course
// -10 to deal with any round-off error
if (courseStatus.distanceTraveled >= (courseStatus.courseDistance-10))
{
if (nextNext != null)
{
Deb.Log("FlightPlan.Update: traveled past current leg, sequence");
SequenceWaypoint(george, vessel, vdata);
return;
}
else
{
next = null;
}
}
else if (nextNext != null &&
!next.HasFlag(WPFlag.FAF) &&
!next.HasFlag(WPFlag.RW) &&
!next.HasFlag(WPFlag.Stop))
{
CourseStatus nextStatus = new CourseStatus();
CrossTrackError(planet,
next.lat, next.lon,
nextNext.lat, nextNext.lon,
position.lat, position.lon,
nextStatus);
var remaining = courseStatus.currentDistance;
var speed = vessel.srfSpeed;
if (speed > 0)
{
var timeToNext = remaining / speed;
var turnAmount = Math.Abs(nextStatus.courseBearing - courseStatus.courseBearing);
// deal with 350-10
if (turnAmount > 180)
{
turnAmount = 360 - turnAmount;
}
// estimate turn time
var turnRate = 3; // 3 degrees per second gives standard rate turn, should get from vessel
var timeToTurn = turnAmount / turnRate;
if (timeToNext < timeToTurn)
{
Deb.Log("FlightPlan.Update: time to turn (speed {5}, remain {0}, timeToNext {1}, amount {2}, rate {3}, timeToTurn {4}",
remaining, timeToNext, turnAmount, turnRate, timeToTurn, speed);
SequenceWaypoint(george, vessel, vdata);
return;
}
}
}
if (next != null)
{
next.bearing = courseStatus.currentBearing;
next.distance = courseStatus.currentDistance;
}
// ok, we know how far off we are, with dXt, what heading do we need?
// can we use the PID controller for this?
}