| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using TeamAAS.Global.Devices;
- using TeamAAS.Motion;
- namespace Plugins.Motion
- {
- internal static class MotionPluginHelper
- {
- public static IMotionCard GetCard(MotionManager mgr, string name, out string error)
- {
- error = null;
- if (string.IsNullOrWhiteSpace(name))
- {
- error = "未选择运动卡";
- return null;
- }
- if (mgr == null)
- {
- error = "MotionManager 未初始化";
- return null;
- }
- var card = mgr.GetMotion(name);
- if (card == null)
- error = $"找不到运动卡: {name}";
- return card;
- }
- public static string EnsureConnected(IMotionCard card, bool autoConnect)
- {
- if (card.IsConnected) return null;
- if (!autoConnect) return $"运动卡未连接: {card.Name}";
- return card.Open() ? null : $"自动连接失败: {card.Name}";
- }
- public static string EnsureEnabled(IMotionCard card, int axis, bool autoEnable)
- {
- if (axis < 0)
- {
- if (!autoEnable) return "未使能且未勾选自动使能";
- return card.SetEnable(-1, true) ? null : "全部轴自动使能失败";
- }
- var st = card.GetAxisStatus(axis);
- if (st == null) return $"轴号超出范围: {axis}";
- if (st.IsEnabled) return null;
- if (!autoEnable) return $"轴 {axis} 未使能(可勾选「未使能时自动使能」,或在流程中加入轴使能节点)";
- return card.SetEnable(axis, true) ? null : $"轴 {axis} 自动使能失败";
- }
- public static string WaitIdle(IMotionCard card, int axis, int timeoutMs, CancellationToken token)
- {
- var start = DateTime.UtcNow;
- while (true)
- {
- token.ThrowIfCancellationRequested();
- var st = card.GetAxisStatus(axis);
- if (st == null) return $"无法读取轴 {axis} 状态";
- if (st.Alarm) return $"轴 {axis} 报警";
- if (!st.IsMoving) return null;
- if (timeoutMs > 0 && (DateTime.UtcNow - start).TotalMilliseconds > timeoutMs)
- return $"等待到位超时 {timeoutMs}ms(轴 {axis})";
- Thread.Sleep(20);
- }
- }
- public static void FillStatus(Dictionary<string, object> res, AxisStatus st)
- {
- if (st == null) return;
- res["最终位置"] = st.Position;
- res["使能"] = st.IsEnabled;
- res["已回零"] = st.IsHomed;
- res["运动中"] = st.IsMoving;
- }
- }
- }
|