initial commit
This commit is contained in:
47
Lab505.Script/Sim/SimAmr.cs
Normal file
47
Lab505.Script/Sim/SimAmr.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Lab505.Script.Facades;
|
||||
|
||||
namespace Lab505.Script.Sim;
|
||||
|
||||
/// <summary>Per-user in-memory AMR simulation. All operations complete after a fixed delay.</summary>
|
||||
public sealed class SimAmr : IAmrFacade
|
||||
{
|
||||
private const int MoveDelayMs = 1500;
|
||||
private const int ActionDelayMs = 800;
|
||||
|
||||
private string _position = "Cap1";
|
||||
|
||||
public string Position => _position;
|
||||
public event Action<string>? OnMoved;
|
||||
|
||||
public bool IsAt(string position, double tol = 0.2) =>
|
||||
string.Equals(_position, position, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public async Task GoTo(string position, CancellationToken ct = default)
|
||||
{
|
||||
await Task.Delay(MoveDelayMs, ct);
|
||||
_position = position;
|
||||
OnMoved?.Invoke(_position);
|
||||
}
|
||||
|
||||
public async Task Undock(CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task Load(CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task Unload(CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task Pause(CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task Resume(CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task InitPosition(double x, double y, double theta, CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task Cancel(CancellationToken ct = default) =>
|
||||
await Task.Delay(200, ct);
|
||||
|
||||
}
|
||||
43
Lab505.Script/Sim/SimAsrs.cs
Normal file
43
Lab505.Script/Sim/SimAsrs.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Lab505.Script.Facades;
|
||||
|
||||
namespace Lab505.Script.Sim;
|
||||
|
||||
/// <summary>Per-user in-memory ASRS simulation. All operations complete after a fixed delay.</summary>
|
||||
public sealed class SimAsrs : IAsrsFacade
|
||||
{
|
||||
private const int OpDelayMs = 1200;
|
||||
|
||||
public bool IsBusy { get; private set; }
|
||||
public event Action? OnOperationStateChanged;
|
||||
|
||||
public bool IsLocked(string key) => false;
|
||||
|
||||
private async Task RunOp(CancellationToken token)
|
||||
{
|
||||
IsBusy = true;
|
||||
OnOperationStateChanged?.Invoke();
|
||||
try { await Task.Delay(OpDelayMs, token); }
|
||||
finally
|
||||
{
|
||||
IsBusy = false;
|
||||
OnOperationStateChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public Task SYSTEM_HOMING(CancellationToken token = default) => RunOp(token);
|
||||
public Task X1_S12(CancellationToken token = default) => RunOp(token);
|
||||
public Task X2_S12(CancellationToken token = default) => RunOp(token);
|
||||
public Task X3_S12(CancellationToken token = default) => RunOp(token);
|
||||
public Task X1_S34(CancellationToken token = default) => RunOp(token);
|
||||
public Task X2_S34(CancellationToken token = default) => RunOp(token);
|
||||
public Task X3_S34(CancellationToken token = default) => RunOp(token);
|
||||
public Task X1_R12(CancellationToken token = default) => RunOp(token);
|
||||
public Task X2_R12(CancellationToken token = default) => RunOp(token);
|
||||
public Task X3_R12(CancellationToken token = default) => RunOp(token);
|
||||
public Task X1_R43(CancellationToken token = default) => RunOp(token);
|
||||
public Task X2_R43(CancellationToken token = default) => RunOp(token);
|
||||
public Task X3_R43(CancellationToken token = default) => RunOp(token);
|
||||
|
||||
public Task<bool> WaitCoil(Func<bool?> selector, bool target, int timeoutMs = 10000) =>
|
||||
Task.FromResult(true);
|
||||
}
|
||||
26
Lab505.Script/Sim/SimContext.cs
Normal file
26
Lab505.Script/Sim/SimContext.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace Lab505.Script.Sim;
|
||||
|
||||
/// <summary>
|
||||
/// Isolated per-user simulation context.
|
||||
/// Each FlowContext built from this context has its own independent robot state.
|
||||
/// </summary>
|
||||
public sealed class SimContext
|
||||
{
|
||||
public SimConveyor Conveyor { get; } = new();
|
||||
public SimImr IMR { get; } = new();
|
||||
public SimAmr AMR { get; } = new();
|
||||
public SimAsrs ASRS { get; } = new();
|
||||
public SimDobot Dobot { get; } = new();
|
||||
public SimFlow Workflow { get; }
|
||||
|
||||
/// <summary>Fires whenever any simulated robot/device changes state.</summary>
|
||||
public event Action? OnChanged;
|
||||
|
||||
public SimContext()
|
||||
{
|
||||
Workflow = new SimFlow(this);
|
||||
IMR.OnMoved += _ => OnChanged?.Invoke();
|
||||
AMR.OnMoved += _ => OnChanged?.Invoke();
|
||||
Conveyor.OnChanged += () => OnChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
68
Lab505.Script/Sim/SimConveyor.cs
Normal file
68
Lab505.Script/Sim/SimConveyor.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Lab505.Script.Facades;
|
||||
|
||||
namespace Lab505.Script.Sim;
|
||||
|
||||
/// <summary>
|
||||
/// Per-user in-memory conveyor simulation.
|
||||
/// Coil commands set state immediately then fire "feedback" after a short delay
|
||||
/// (simulating the physical conveyor responding). WaitCx methods watch for feedback.
|
||||
/// </summary>
|
||||
public sealed class SimConveyor : IConveyorFacade
|
||||
{
|
||||
// Feedback state (what WaitCx methods watch)
|
||||
private bool? _c1In, _c1Out, _c1IMRin, _c1IMRout, _c1Ready, _c2Ready;
|
||||
private bool? _c2In, _c2Out;
|
||||
private bool? _c1HasGoods, _c1NotGoods, _c2HasGoods, _c2NotGoods;
|
||||
|
||||
// Read-only accessors for state snapshot
|
||||
public bool? StateC1In => _c1In;
|
||||
public bool? StateC1Out => _c1Out;
|
||||
public bool? StateC1IMRin => _c1IMRin;
|
||||
public bool? StateC1IMRout => _c1IMRout;
|
||||
public bool? StateC1Ready => _c1Ready;
|
||||
public bool? StateC2Ready => _c2Ready;
|
||||
public bool? StateC2In => _c2In;
|
||||
public bool? StateC2Out => _c2Out;
|
||||
public bool? StateC1HasGoods => _c1HasGoods;
|
||||
public bool? StateC1NotGoods => _c1NotGoods;
|
||||
public bool? StateC2HasGoods => _c2HasGoods;
|
||||
public bool? StateC2NotGoods => _c2NotGoods;
|
||||
|
||||
public event Action? OnChanged;
|
||||
|
||||
private const int FeedbackDelayMs = 600;
|
||||
|
||||
// ── Coil commands ──────────────────────────────────────────────────────────
|
||||
|
||||
public bool C1In(bool v) => FireFeedback(v, x => _c1In = x);
|
||||
public bool C1Out(bool v) => FireFeedback(v, x => _c1Out = x);
|
||||
public bool C1IMRin(bool v) => FireFeedback(v, x => _c1IMRin = x);
|
||||
public bool C1IMRout(bool v) => FireFeedback(v, x => _c1IMRout = x);
|
||||
public bool C1Ready(bool v) => FireFeedback(v, x => _c1Ready = x);
|
||||
public bool C2Ready(bool v) => FireFeedback(v, x => _c2Ready = x);
|
||||
public bool C2In(bool v) => FireFeedback(v, x => _c2In = x);
|
||||
public bool C2Out(bool v) => FireFeedback(v, x => _c2Out = x);
|
||||
public bool C1HasGoods(bool v) => FireFeedback(v, x => _c1HasGoods = x);
|
||||
public bool C1NotGoods(bool v) => FireFeedback(v, x => _c1NotGoods = x);
|
||||
public bool C2HasGoods(bool v) => FireFeedback(v, x => _c2HasGoods = x);
|
||||
public bool C2NotGoods(bool v) => FireFeedback(v, x => _c2NotGoods = x);
|
||||
|
||||
public void StopAll()
|
||||
{
|
||||
C1In(false); C1Out(false); C1IMRin(false); C1IMRout(false);
|
||||
C2In(false); C2Out(false);
|
||||
}
|
||||
|
||||
// ── Internal helpers ───────────────────────────────────────────────────────
|
||||
|
||||
private bool FireFeedback(bool value, Action<bool> setter)
|
||||
{
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(FeedbackDelayMs);
|
||||
setter(value);
|
||||
OnChanged?.Invoke();
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
15
Lab505.Script/Sim/SimDobot.cs
Normal file
15
Lab505.Script/Sim/SimDobot.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Lab505.Script.Facades;
|
||||
|
||||
namespace Lab505.Script.Sim;
|
||||
|
||||
/// <summary>Per-user in-memory Dobot simulation. All operations complete after a fixed delay.</summary>
|
||||
public sealed class SimDobot : IDobotFacade
|
||||
{
|
||||
private const int OpDelayMs = 800;
|
||||
|
||||
public Task PowerOn(CancellationToken t = default) => Task.Delay(OpDelayMs, t);
|
||||
public Task GoHome(CancellationToken t = default) => Task.Delay(OpDelayMs, t);
|
||||
public Task Continue(CancellationToken t = default) => Task.Delay(OpDelayMs, t);
|
||||
public Task Cancel(CancellationToken t = default) => Task.Delay(200, t);
|
||||
public Task Pickup(int ok, int ng, CancellationToken t = default) => Task.Delay(OpDelayMs, t);
|
||||
}
|
||||
126
Lab505.Script/Sim/SimFlow.cs
Normal file
126
Lab505.Script/Sim/SimFlow.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using Lab505.Script;
|
||||
|
||||
namespace Lab505.Script.Sim;
|
||||
|
||||
/// <summary>
|
||||
/// Per-user in-memory workflow simulation.
|
||||
/// Each X_A/X_B call runs a full 8-step cycle using SimContext robots with fixed delays.
|
||||
/// </summary>
|
||||
public sealed class SimFlow : IFlowFacade
|
||||
{
|
||||
private readonly SimImr _imr;
|
||||
private readonly SimAmr _amr;
|
||||
private readonly SimConveyor _conv;
|
||||
private readonly SimAsrs _asrs;
|
||||
private readonly SimDobot _dobot;
|
||||
|
||||
public bool IsRunning { get; private set; }
|
||||
public string? LastError { get; private set; }
|
||||
public int CompletedCycles { get; private set; }
|
||||
public event Action? RunningStateChanged;
|
||||
|
||||
internal SimFlow(SimContext ctx)
|
||||
{
|
||||
_imr = ctx.IMR;
|
||||
_amr = ctx.AMR;
|
||||
_conv = ctx.Conveyor;
|
||||
_asrs = ctx.ASRS;
|
||||
_dobot = ctx.Dobot;
|
||||
}
|
||||
|
||||
// ── X_A: S34 pick → workflow → S34 return ────────────────────────────────
|
||||
|
||||
public Task X1A(int times, int ng, CancellationToken ct = default)
|
||||
=> RunCycle(times, ng, ct => _asrs.X1_S34(ct), ct => _asrs.X1_R43(ct), ct);
|
||||
public Task X2A(int times, int ng, CancellationToken ct = default)
|
||||
=> RunCycle(times, ng, ct => _asrs.X2_S34(ct), ct => _asrs.X2_R43(ct), ct);
|
||||
public Task X3A(int times, int ng, CancellationToken ct = default)
|
||||
=> RunCycle(times, ng, ct => _asrs.X3_S34(ct), ct => _asrs.X3_R43(ct), ct);
|
||||
|
||||
// ── X_B: S12 pick → workflow → S12 return ────────────────────────────────
|
||||
|
||||
public Task X1B(int times, int ng, CancellationToken ct = default)
|
||||
=> RunCycle(times, ng, ct => _asrs.X1_S12(ct), ct => _asrs.X1_R12(ct), ct);
|
||||
public Task X2B(int times, int ng, CancellationToken ct = default)
|
||||
=> RunCycle(times, ng, ct => _asrs.X2_S12(ct), ct => _asrs.X2_R12(ct), ct);
|
||||
public Task X3B(int times, int ng, CancellationToken ct = default)
|
||||
=> RunCycle(times, ng, ct => _asrs.X3_S12(ct), ct => _asrs.X3_R12(ct), ct);
|
||||
|
||||
// ── Run cycle (no ASRS) ───────────────────────────────────────────────────
|
||||
|
||||
public Task RunCycle(int times, int ng, CancellationToken ct = default)
|
||||
=> RunCycle(times, ng, _ => Task.CompletedTask, _ => Task.CompletedTask, ct);
|
||||
|
||||
// ── Core cycle loop ───────────────────────────────────────────────────────
|
||||
|
||||
private async Task RunCycle(
|
||||
int times, int ng,
|
||||
Func<CancellationToken, Task> startOp,
|
||||
Func<CancellationToken, Task> endOp,
|
||||
CancellationToken ct)
|
||||
{
|
||||
IsRunning = true;
|
||||
LastError = null;
|
||||
RunningStateChanged?.Invoke();
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < times; i++)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
// Step 1: Init — IMR→C1 + AMR→Cap2 + ASRS pick (parallel)
|
||||
await Task.WhenAll(_imr.GoTo("C1", ct), _amr.GoTo("Cap2", ct), startOp(ct));
|
||||
|
||||
// Step 2: AMR load from C2
|
||||
_conv.C2Out(true);
|
||||
await _amr.Load(ct);
|
||||
_conv.C2Out(false);
|
||||
|
||||
// Step 3: AMR undock → Cap1
|
||||
await _amr.Undock(ct);
|
||||
await _amr.GoTo("Cap1", ct);
|
||||
|
||||
// Step 4: AMR→IMR exchange via C1
|
||||
_conv.C1In(true);
|
||||
await _imr.Load(ct);
|
||||
_conv.C1In(false);
|
||||
_conv.C1Out(true);
|
||||
await Task.Delay(300, ct);
|
||||
_conv.C1Out(false);
|
||||
|
||||
// Step 5: IMR process @ ABB
|
||||
await _imr.GoTo("ABB", ct);
|
||||
await _dobot.Pickup(1, ng, ct);
|
||||
await _imr.GoTo("C1", ct);
|
||||
|
||||
// Step 6: IMR→AMR exchange via C1
|
||||
_conv.C1IMRin(true);
|
||||
await _imr.Unload(ct);
|
||||
_conv.C1IMRin(false);
|
||||
await _amr.Load(ct);
|
||||
|
||||
// Step 7: AMR → Cap2
|
||||
await _amr.GoTo("Cap2", ct);
|
||||
|
||||
// Step 8: AMR unload to C2 + ASRS return
|
||||
_conv.C2In(true);
|
||||
await _amr.Unload(ct);
|
||||
_conv.C2In(false);
|
||||
await endOp(ct);
|
||||
|
||||
CompletedCycles++;
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (Exception ex)
|
||||
{
|
||||
LastError = ex.Message;
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsRunning = false;
|
||||
RunningStateChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
50
Lab505.Script/Sim/SimImr.cs
Normal file
50
Lab505.Script/Sim/SimImr.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Lab505.Script.Facades;
|
||||
|
||||
namespace Lab505.Script.Sim;
|
||||
|
||||
/// <summary>Per-user in-memory IMR simulation. All operations complete after a fixed delay.</summary>
|
||||
public sealed class SimImr : IImrFacade
|
||||
{
|
||||
private const int MoveDelayMs = 1500;
|
||||
private const int ActionDelayMs = 800;
|
||||
|
||||
private string _position = "C1";
|
||||
|
||||
public string Position => _position;
|
||||
public event Action<string>? OnMoved;
|
||||
|
||||
public bool IsAt(string position, double tol = 0.2) =>
|
||||
string.Equals(_position, position, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public async Task GoTo(string position, CancellationToken ct = default)
|
||||
{
|
||||
await Task.Delay(MoveDelayMs, ct);
|
||||
_position = position;
|
||||
OnMoved?.Invoke(_position);
|
||||
}
|
||||
|
||||
public async Task Undock(string fromPosition, CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task Load(CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task Unload(CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task PickUp(int ok, int ng = 0, CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task Pause(CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task Resume(CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task InitPosition(double x, double y, double theta, CancellationToken ct = default) =>
|
||||
await Task.Delay(ActionDelayMs, ct);
|
||||
|
||||
public async Task Cancel(CancellationToken ct = default) =>
|
||||
await Task.Delay(200, ct);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user