Polished up edge cases so doors close and lock properly.

This commit is contained in:
Anna Rose 2025-02-07 12:08:25 -05:00
parent 643dd04d6b
commit 696b502ae8

View File

@ -23,6 +23,7 @@
MyCommandLine cli; MyCommandLine cli;
List<IEnumerator<bool>> jobs; List<IEnumerator<bool>> jobs;
Dictionary<string, Door> doors; Dictionary<string, Door> doors;
int tickCount;
public class DoorHinge { public class DoorHinge {
public DoorHinge(Program p, IMyMotorStator hinge) { public DoorHinge(Program p, IMyMotorStator hinge) {
@ -47,9 +48,10 @@ public class DoorHinge {
// Process the hinge's movement. // Process the hinge's movement.
// It will return true when the panel has finished moving. // It will return true when the panel has finished moving.
public bool Actuate() { public bool Actuate() {
if (Math.Abs(Hinge.Angle - TargetAngle) < 0.001) { if (Math.Abs(Hinge.Angle - TargetAngle) < 0.1 && Hinge.Angle == LastAngle) {
Hinge.RotorLock = true; Hinge.RotorLock = true;
} }
LastAngle = Hinge.Angle;
return Locked(); return Locked();
} }
@ -60,6 +62,7 @@ public class DoorHinge {
private Program P; // for access to Echo, etc. private Program P; // for access to Echo, etc.
private IMyMotorStator Hinge { get; set; } private IMyMotorStator Hinge { get; set; }
private float TargetAngle { get; set; } private float TargetAngle { get; set; }
private float LastAngle {get; set; }
private float OpenAngle { get; set; } = 90F; private float OpenAngle { get; set; } = 90F;
private float ClosedAngle { get; set; } = 0F; private float ClosedAngle { get; set; } = 0F;
private float Velocity { get; set; } = 5F; private float Velocity { get; set; } = 5F;
@ -117,7 +120,7 @@ public class Door {
public bool Actuate() { public bool Actuate() {
bool done = true; bool done = true;
foreach (DoorHinge hinge in Hinges) { foreach (DoorHinge hinge in Hinges) {
if (hinge.Actuate()) done = false; if (!hinge.Actuate()) done = false;
} }
return done; return done;
} }
@ -134,6 +137,7 @@ public class Door {
} }
public Program() { public Program() {
tickCount = 0;
cli = new MyCommandLine(); cli = new MyCommandLine();
jobs = new List<IEnumerator<bool>>(); jobs = new List<IEnumerator<bool>>();
@ -143,7 +147,7 @@ public Program() {
GridTerminalSystem.GetBlocksOfType(allHinges); GridTerminalSystem.GetBlocksOfType(allHinges);
foreach(IMyMotorStator hinge in allHinges) { foreach(IMyMotorStator hinge in allHinges) {
if (hinge.CustomName.StartsWith("Door")) { if (hinge.CustomName.StartsWith("Door")) {
string doorName = hinge.CustomName; string doorName = hinge.CustomName.Split(' ')[0];
if (!doors.ContainsKey(doorName)) { if (!doors.ContainsKey(doorName)) {
doors[doorName] = new Door(this); doors[doorName] = new Door(this);
} }
@ -155,22 +159,34 @@ public Program() {
} }
public void Main(string argument, UpdateType updateSource) { public void Main(string argument, UpdateType updateSource) {
Echo($"index: {tickCount++}");
if (updateSource == UpdateType.Trigger || updateSource == UpdateType.Terminal) { if (updateSource == UpdateType.Trigger || updateSource == UpdateType.Terminal) {
// Create a new job // Create a new job
cli.TryParse(argument); cli.TryParse(argument);
List<Door> doorsToControl = new List<Door>(); List<Door> doorsToControl = new List<Door>();
if (cli.ArgumentCount == 0) {
Echo("No arguments passed. Controlling all doors.");
foreach (Door door in doors.Values) {
if (!door.Locked()) {
continue;
}
doorsToControl.Add(door);
}
}
for (int i=0; i < cli.ArgumentCount; i++) { for (int i=0; i < cli.ArgumentCount; i++) {
string arg = cli.Argument(i); string key = "Door" + cli.Argument(i);
if (!doors.ContainsKey($"Door{arg}")) { if (!doors.ContainsKey(key)) {
Echo($"Door with identifier {arg} not found. Skipping."); Echo($"Door with identifier {key} not found. Skipping.");
continue; continue;
} }
if (!doors[arg].Locked()) { if (!doors[key].Locked()) {
Echo($"Door {arg} already moving. Skipping."); Echo($"Door {key} already moving. Skipping.");
continue; continue;
} }
doorsToControl.Add(doors[arg]); doorsToControl.Add(doors[key]);
} }
if (doorsToControl.Count == 0) { if (doorsToControl.Count == 0) {
@ -189,6 +205,7 @@ public void Main(string argument, UpdateType updateSource) {
jobs[i].Dispose(); jobs[i].Dispose();
jobs.Remove(jobs[i]); jobs.Remove(jobs[i]);
i--; i--;
Echo("Operation Complete.");
} }
} }