From 445c1205f95656d2985cf4bfdc812975d69cba7e Mon Sep 17 00:00:00 2001 From: annabunches Date: Fri, 7 Feb 2025 09:06:42 -0500 Subject: [PATCH] Fix various bugs and inconsistencies. --- docking.cs | 95 +++++++++++++++++++++++++++---------------- maintenance_panels.cs | 32 +++++++++++---- 2 files changed, 83 insertions(+), 44 deletions(-) diff --git a/docking.cs b/docking.cs index c145467..7522fc9 100644 --- a/docking.cs +++ b/docking.cs @@ -3,77 +3,100 @@ // * Connect any Connectors whose names start with "Docking Port" that are ready to connect. // * Turn off all engines. // * Set all O2 and Hydrogen tanks to Stockpile. -// * Set all batteries to Recharge. +// +// Recharging batteries needs to be done manually, since the script can't finish with the +// batteries disabled. // // When you activate the script with "-undock" it will reverse all of these actions. // // TODO: None of the below switches work yet. // You can selectively disable some functionality with switches. // * "-no-refuel" to disable refueling -// * "-no-recharge" to disable battery recharge -// (batteries will be left in whatever state they are already in) MyCommandLine cli; +IEnumerator state; List dockingPorts; List thrusters; -List batteries; List tanks; -public void Program() { - cli = new MyCommandLine(); +public Program() { + cli = new MyCommandLine(); + state = null; - dockingPorts = new List(); - List allConnectors = new List(); - foreach(IMyShipConnector connector in allConnectors) { - if (connector.CustomName.StartsWith("Docking Port")) { - dockingPorts.Add(connector); - } - } + dockingPorts = new List(); + List allConnectors = new List(); + GridTerminalSystem.GetBlocksOfType(allConnectors); + foreach(IMyShipConnector connector in allConnectors) { + if (connector.CustomName.StartsWith("Docking Port")) { + dockingPorts.Add(connector); + } + } - thrusters = new List(); - GridTerminalSystem.GetBlocksOfType(thrusters); + thrusters = new List(); + GridTerminalSystem.GetBlocksOfType(thrusters); - batteries = new List(); - GridTerminalSystem.GetBlocksOfType(batteries); + tanks = new List(); + GridTerminalSystem.GetBlocksOfType(tanks); - tanks = new List(); - GridTerminalSystem.GetBlocksOfType(tanks); + Echo($"Found {dockingPorts.Count} docking ports."); + Echo($"Found {thrusters.Count} thrusters"); + Echo($"Found {tanks.Count} tanks"); } public void Main(string argument, UpdateType updateSource) { - cli.TryParse(argument); - if (cli.Switch("undock")) Undock(); - else Dock(); + if (state == null) { + cli.TryParse(argument); + if (cli.Switch("undock")) state = Undock(); + else state = Dock(); + Runtime.UpdateFrequency = UpdateFrequency.Update100; + return; + } + + if (!state.MoveNext()) { + Runtime.UpdateFrequency = UpdateFrequency.None; + state.Dispose(); + state = null; + } } -private void Dock() { +private IEnumerator Dock() { + Echo("Initiating docking operation."); + foreach (IMyShipConnector dockingPort in dockingPorts) { if (dockingPort.Status == MyShipConnectorStatus.Connectable) { - dockingPort.Connect(); + dockingPort.Connect(); } } + bool docked = false; + while (!docked) { + yield return true; + foreach (IMyShipConnector dockingPort in dockingPorts) { + if (dockingPort.Status == MyShipConnectorStatus.Connected) { + docked = true; + break; + } + } + } + + Echo("Docking clamp engaged. Shutting down systems."); + foreach (IMyThrust thruster in thrusters) { thruster.Enabled = false; } - foreach (IMyBatteryBlock battery in batteries) { - battery.ChargeMode = ChargeMode.Recharge; - } - foreach (IMyGasTank tank in tanks) { tank.Stockpile = true; } + + yield return false; } -private void Undock() { - foreach (IMyBatteryBlock battery in batteries) { - battery.ChargeMode = ChargeMode.Recharge; - } - +private IEnumerator Undock() { + Echo("Initiating undocking operation."); foreach (IMyGasTank tank in tanks) { - tank.Stockpile = true; + tank.Stockpile = false; } foreach (IMyThrust thruster in thrusters) { @@ -82,7 +105,9 @@ private void Undock() { foreach (IMyShipConnector dockingPort in dockingPorts) { if (dockingPort.Status == MyShipConnectorStatus.Connected) { - dockingPort.Disconnect(); + dockingPort.Disconnect(); } } + + yield return false; } \ No newline at end of file diff --git a/maintenance_panels.cs b/maintenance_panels.cs index 19b2f13..15a9744 100644 --- a/maintenance_panels.cs +++ b/maintenance_panels.cs @@ -23,29 +23,32 @@ public class Panel { ParseConfig(); } + // For these two functions, IMyMotorStator.Angle reports radians, but + // IMyMotorStator.RotateToAngle() expects degrees... public void OpenPanel() { Hinge.RotorLock = false; - TargetAngle = OpenAngle; - Hinge.RotateToAngle(MyRotationDirection.AUTO, TargetAngle, Velocity); + TargetAngle = DegToRad(OpenAngle); + Hinge.RotateToAngle(MyRotationDirection.AUTO, OpenAngle, Velocity); } public void ClosePanel() { Hinge.RotorLock = false; - TargetAngle = ClosedAngle; - Hinge.RotateToAngle(MyRotationDirection.AUTO, TargetAngle, Velocity); + TargetAngle = DegToRad(ClosedAngle); + Hinge.RotateToAngle(MyRotationDirection.AUTO, ClosedAngle, Velocity); } // Call this function every tick after OpenPanel or ClosePanel. // It will return true when the panel has finished moving. public bool Monitor() { - if (Math.Abs(Hinge.Angle - TargetAngle) < 0.01) { + if (Math.Abs(Hinge.Angle - TargetAngle) < 0.001) { Hinge.RotorLock = true; } return Hinge.RotorLock; } - private IMyMotorStator Hinge { get; set; } - private float TargetAngle { get; set; } + // TODO: these are public for debugging + public IMyMotorStator Hinge { get; set; } + public float TargetAngle { get; set; } private float OpenAngle { get; set; } = 90F; private float ClosedAngle { get; set; } = 0F; private float Velocity { get; set; } = 5F; @@ -68,6 +71,12 @@ public class Panel { } } } + + // TODO: a utility class or function would be lovely... + // In general, the encapsulation feels a little screwy here. + private float DegToRad(float degrees) { + return degrees * ((float)Math.PI / 180F); + } } public Program() { @@ -76,13 +85,14 @@ public Program() { panels = new List(); List allHinges = new List(); + GridTerminalSystem.GetBlocksOfType(allHinges); foreach(IMyMotorStator hinge in allHinges) { if (hinge.CustomName.StartsWith("Maintenance")) { panels.Add(new Panel(hinge)); } } - Echo($"Found {panels.Length} panels."); + Echo($"Found {panels.Count} panels."); } public void Main(string argument, UpdateType updateSource) { @@ -95,6 +105,7 @@ public void Main(string argument, UpdateType updateSource) { } if (!state.MoveNext()) { + Echo("Operation Complete."); state.Dispose(); state = null; Runtime.UpdateFrequency = UpdateFrequency.None; @@ -122,7 +133,10 @@ private IEnumerator MonitorPanels() { Echo("Monitoring panels."); bool done = true; // assume we've finished, then falsify it below foreach (Panel panel in panels) { - if (!panel.Monitor()) done = false; + if (!panel.Monitor()) { + Echo($"{panel.TargetAngle} {panel.Hinge.Angle}"); + done = false; + } } if (done) yield break; yield return true;