Fix various bugs and inconsistencies.
This commit is contained in:
parent
44d161f5c0
commit
445c1205f9
95
docking.cs
95
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<bool> state;
|
||||
|
||||
List<IMyShipConnector> dockingPorts;
|
||||
List<IMyThrust> thrusters;
|
||||
List<IMyBatteryBlock> batteries;
|
||||
List<IMyGasTank> tanks;
|
||||
|
||||
public void Program() {
|
||||
cli = new MyCommandLine();
|
||||
public Program() {
|
||||
cli = new MyCommandLine();
|
||||
state = null;
|
||||
|
||||
dockingPorts = new List<IMyShipConnector>();
|
||||
List<IMyShipConnector> allConnectors = new List<IMyShipConnector>();
|
||||
foreach(IMyShipConnector connector in allConnectors) {
|
||||
if (connector.CustomName.StartsWith("Docking Port")) {
|
||||
dockingPorts.Add(connector);
|
||||
}
|
||||
}
|
||||
dockingPorts = new List<IMyShipConnector>();
|
||||
List<IMyShipConnector> allConnectors = new List<IMyShipConnector>();
|
||||
GridTerminalSystem.GetBlocksOfType(allConnectors);
|
||||
foreach(IMyShipConnector connector in allConnectors) {
|
||||
if (connector.CustomName.StartsWith("Docking Port")) {
|
||||
dockingPorts.Add(connector);
|
||||
}
|
||||
}
|
||||
|
||||
thrusters = new List<IMyThrust>();
|
||||
GridTerminalSystem.GetBlocksOfType(thrusters);
|
||||
thrusters = new List<IMyThrust>();
|
||||
GridTerminalSystem.GetBlocksOfType(thrusters);
|
||||
|
||||
batteries = new List<IMyBatteryBlock>();
|
||||
GridTerminalSystem.GetBlocksOfType(batteries);
|
||||
tanks = new List<IMyGasTank>();
|
||||
GridTerminalSystem.GetBlocksOfType(tanks);
|
||||
|
||||
tanks = new List<IMyGasTank>();
|
||||
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<bool> 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<bool> 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;
|
||||
}
|
|
@ -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<Panel>();
|
||||
List<IMyMotorStator> allHinges = new List<IMyMotorStator>();
|
||||
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<bool> 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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user