Skip to content

Commit

Permalink
Use Autopilot state when setting AP
Browse files Browse the repository at this point in the history
Check if AP is already in appropriate state before setting. THis should
help with some of the "bouncing" altitude issues. Not sure if update is
fast enough yet.
  • Loading branch information
AR-AR-AR committed Feb 5, 2017
1 parent b7a6ff3 commit c5920bd
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions IF_FMS/FMS.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,7 @@ public IFConnect.IFConnectorClient Client {
set { client = value; }
}

private bool APUpdateReceived = false;
private APIAutopilotState pAPState = new APIAutopilotState();
public APIAutopilotState APState
{
get {
System.Threading.Tasks.Task tsk = System.Threading.Tasks.Task.Run(async () =>
{
client.ExecuteCommand("Autopilot.GetState");
while (!APUpdateReceived) { await System.Threading.Tasks.Task.Delay(25); }
APUpdateReceived = false;
});
return pAPState; }
set { pAPState = value; APUpdateReceived = true; }
}


private APIAircraftState pAircraftState = new APIAircraftState();
public bool autoFplDirectActive = false;
Expand Down Expand Up @@ -164,6 +151,7 @@ private void btnInitFlightDir_Click(object sender, RoutedEventArgs e)
{
if (!autoFplDirectActive) //AutoNAV not active. Turn it on.
{
getAPState();
if (pFplState==null || pFplState.fpl == null)
{
MessageBox.Show("Must get or set FPL first.");
Expand Down Expand Up @@ -475,6 +463,24 @@ public void handleAtcMessage(APIATCMessage AtcMsg, APIAircraftState acState)
#endregion

#region "Autopilot settings"
private bool APUpdateReceived = false;
private APIAutopilotState pAPState = new APIAutopilotState();
public APIAutopilotState APState
{
get
{
getAPState();
return pAPState;
}
set { pAPState = value; APUpdateReceived = true; }
}

private void getAPState()
{
APUpdateReceived = false;
client.ExecuteCommand("Autopilot.GetState");
}

private void setHeading(double heading)
{
if (APState.TargetHeading != heading) { client.ExecuteCommand("Commands.Autopilot.SetHeading", new CallParameter[] { new CallParameter { Value = heading.ToString(CultureInfo.InvariantCulture.NumberFormat) } }); }
Expand All @@ -491,12 +497,13 @@ private void setAltitude(double altitude, double vs)

private void setSpeed(double speed)
{
if(APState.TargetSpeed != speed) { client.ExecuteCommand("Commands.Autopilot.SetSpeed", new CallParameter[] { new CallParameter { Value = speed.ToString(CultureInfo.InvariantCulture.NumberFormat) } }); }
if (APState.TargetSpeed != speed) { client.ExecuteCommand("Commands.Autopilot.SetSpeed", new CallParameter[] { new CallParameter { Value = speed.ToString(CultureInfo.InvariantCulture.NumberFormat) } }); }
if (!APState.EnableSpeed){ client.ExecuteCommand("Commands.Autopilot.SetSpeedState", new CallParameter[] { new CallParameter { Value = "True" } }); }
}

private void setAutoPilotParams(double altitude, double heading, double vs, double speed)
{
getAPState();
//Send parameters
if (speed > 0 && APState.TargetSpeed!=speed) { client.ExecuteCommand("Commands.Autopilot.SetSpeed", new CallParameter[] { new CallParameter { Value = speed.ToString(CultureInfo.InvariantCulture.NumberFormat) } }); }
if (altitude > 0 && pAPState.TargetAltitude != altitude) { client.ExecuteCommand("Commands.Autopilot.SetAltitude", new CallParameter[] { new CallParameter { Value = altitude.ToString(CultureInfo.InvariantCulture.NumberFormat) } }); }
Expand Down

0 comments on commit c5920bd

Please # to comment.