Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update IAS definition #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions AtmosphereAutopilot/AtmosphereAutopilot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,15 @@ public enum AerodinamycsModel
/// Current aerodynamics model.
/// </summary>
public static AerodinamycsModel AeroModel { get; private set; }
Assembly far_assembly;
public FARReflections farReflections;

void determine_aerodynamics()
{
AeroModel = AerodinamycsModel.Stock;
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
farReflections = new FARReflections();
if (farReflections.isFarFound)
{
if (a.GetName().Name.Equals("FerramAerospaceResearch"))
{
far_assembly = a;
AeroModel = AerodinamycsModel.FAR;
Debug.Log("[AtmosphereAutopilot]: FAR aerodynamics detected");
return;
}
AeroModel = AerodinamycsModel.FAR;
}
}

Expand All @@ -111,11 +106,7 @@ void get_csurf_module()
if (AeroModel == AerodinamycsModel.Stock)
control_surface_module_type = typeof(SyncModuleControlSurface);
else
{
control_surface_module_type = far_assembly.GetTypes().First(t => t.Name.Equals("FARControllableSurface"));
if (control_surface_module_type == null)
throw new Exception("AtmosphereAutopilot could not bind to FAR FARControllableSurface class");
}
control_surface_module_type = farReflections.FARControllableSurfaceType;
}

public static Dictionary<Type, ConstructorInfo> gimbal_module_wrapper_map = new Dictionary<Type, ConstructorInfo>(4);
Expand Down
2 changes: 2 additions & 0 deletions AtmosphereAutopilot/AtmosphereAutopilot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AutoHotkey.cs" />
<Compile Include="Reflections\FARReflections.cs" />
<Compile Include="GimbalWrappers.cs" />
<Compile Include="AppLauncher.cs" />
<Compile Include="AtmosphereAutopilot.cs" />
Expand Down Expand Up @@ -157,6 +158,7 @@
<Compile Include="Modules\TopModuleManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="MessageManager.cs" />
<Compile Include="Reflections\ReflectionUtils.cs" />
<Compile Include="SyncModuleControlSurface.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
83 changes: 83 additions & 0 deletions AtmosphereAutopilot/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,89 @@ public static string ToString(this Vector3d v, string format)
{
return '(' + v.x.ToString(format) + ", " + v.y.ToString(format) + ", " + v.z.ToString(format) + ')';
}

public enum AlgoStatus
{
Success,
InvalidArgument,
ConvergenceFailure,
MaxIterReached,
NaN,
OutOfBounds,
}

/// <summary>
/// Try to find a root for a target result of a function using secant method.
/// </summary>
/// <param name="function">Function to find root of</param>
/// <param name="target">Target result of the function</param>
/// <param name="x0">Initial first guess</param>
/// <param name="x1">Initial second guess</param>
/// <param name="epsilon">Convergence tolerance</param>
/// <param name="maxIter">Maximum iterations allowed</param>
/// <param name="min">Minimum value of the root</param>
/// <param name="max">Maximum value of the root</param>
/// <returns>A tuple of algorithm status and a possible root</returns>
public static Tuple<AlgoStatus, double> Secant(Func<double, double> function,
double target,
double x0,
double x1,
double epsilon,
int maxIter,
double min = double.NegativeInfinity,
double max = double.PositiveInfinity)
{
if (x0 == x1)
return new Tuple<AlgoStatus, double>(AlgoStatus.InvalidArgument, x0);

double slope, xnew;
int boundFlag = 0;
double v0 = function(x0) - target;
double v1 = function(x1) - target;

if (double.IsNaN(v0) || double.IsNaN(v1))
return new Tuple<AlgoStatus, double>(AlgoStatus.NaN, x0);

for (int i = 0; i < maxIter; i++)
{
if (Math.Abs(x1 - x0) <= epsilon)
return new Tuple<AlgoStatus, double>(AlgoStatus.Success, x1);

slope = (x1 - x0) / (v1 - v0);
if (double.IsNaN(slope) || double.IsInfinity(slope))
return new Tuple<AlgoStatus, double>(AlgoStatus.ConvergenceFailure, x1);
xnew = x1 - slope * v1;
if (xnew < min)
{
if (boundFlag == -1)
return new Tuple<AlgoStatus, double>(AlgoStatus.OutOfBounds, min);
else
{
xnew = min;
boundFlag = -1;
}
}
if (xnew > max)
{
if (boundFlag == 1)
return new Tuple<AlgoStatus, double>(AlgoStatus.OutOfBounds, max);
else
{
xnew = max;
boundFlag = 1;
}
}
x0 = x1;
x1 = xnew;
v0 = v1;
v1 = function(x1) - target;
if (double.IsNaN(v1))
return new Tuple<AlgoStatus, double>(AlgoStatus.NaN, x1);
}
if (Math.Abs(x0 - x1) < epsilon)
return new Tuple<AlgoStatus, double>(AlgoStatus.Success, x1);
return new Tuple<AlgoStatus, double>(AlgoStatus.MaxIterReached, x1);
}
}

public static class VesselExtensions
Expand Down
4 changes: 2 additions & 2 deletions AtmosphereAutopilot/GUI/NeoGUIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ public float speed {
get {
if (speedAP == null)
return 0f;
return speedAP.setpoint.mps();
return speedAP.setpoint.sameSystemMps();
}

set {
if (parent.ActiveVessel != null) {
speedAP.setpoint_field.Value = value;
speedAP.chosen_spd_mode = 1;
speedAP.type = SpeedType.MetersPerSecond;
speedAP.setpoint = new SpeedSetpoint(SpeedType.MetersPerSecond, value, parent.ActiveVessel);
}
}
Expand Down
2 changes: 1 addition & 1 deletion AtmosphereAutopilot/Modules/AoAHoldController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public override void ApplyControl(FlightCtrlState cntrl)
return;

if (thrust_c.spd_control_enabled)
thrust_c.ApplyControl(cntrl, thrust_c.setpoint.mps());
thrust_c.ApplyControl(cntrl);

if (use_keys)
ControlUtils.neutralize_user_input(cntrl, PITCH);
Expand Down
9 changes: 5 additions & 4 deletions AtmosphereAutopilot/Modules/CruiseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public override void ApplyControl(FlightCtrlState cntrl)
return;

if (thrust_c.spd_control_enabled)
thrust_c.ApplyControl(cntrl, thrust_c.setpoint.mps());
thrust_c.ApplyControl(cntrl);

desired_velocity = Vector3d.zero;
planet2ves = vessel.ReferenceTransform.position - vessel.mainBody.position;
Expand Down Expand Up @@ -320,14 +320,15 @@ Vector3d account_for_height(Vector3d desired_direction)
else
effective_max_climb_angle = 1.0;

double spd_diff = (imodel.surface_v_magnitude - thrust_c.setpoint.mps());
double spd_diff_magnitude;
double spd_diff = thrust_c.speedDiff(out spd_diff_magnitude);
if (spd_diff < -flc_margin)
effective_max_climb_angle *= 0.0;
else if (spd_diff < 0.0)
effective_max_climb_angle *= (spd_diff + flc_margin) / flc_margin;
effective_max_climb_angle *= (spd_diff * spd_diff_magnitude + flc_margin) / flc_margin;
}
else
effective_max_climb_angle *= Math.Max(0.0, Math.Min(1.0, vessel.srfSpeed / thrust_c.setpoint.mps()));
effective_max_climb_angle *= Common.Clamp(thrust_c.currentSpeedOfSameSystem() / thrust_c.setpoint.sameSystemMps(), 0.0, 1.0);
}

double max_vert_speed = vessel.horizontalSrfSpeed * Math.Tan(effective_max_climb_angle * dgr2rad);
Expand Down
2 changes: 1 addition & 1 deletion AtmosphereAutopilot/Modules/MouseDirector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public override void ApplyControl(FlightCtrlState cntrl)
dir_c.ApplyControl(cntrl, camera_direction, Vector3d.zero);

if (thrust_c.spd_control_enabled)
thrust_c.ApplyControl(cntrl, thrust_c.setpoint.mps());
thrust_c.ApplyControl(cntrl);
}

//bool camera_correct = false;
Expand Down
Loading