MotionPluginHelper.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using TeamAAS.Global.Devices;
  5. using TeamAAS.Motion;
  6. namespace Plugins.Motion
  7. {
  8. internal static class MotionPluginHelper
  9. {
  10. public static IMotionCard GetCard(MotionManager mgr, string name, out string error)
  11. {
  12. error = null;
  13. if (string.IsNullOrWhiteSpace(name))
  14. {
  15. error = "未选择运动卡";
  16. return null;
  17. }
  18. if (mgr == null)
  19. {
  20. error = "MotionManager 未初始化";
  21. return null;
  22. }
  23. var card = mgr.GetMotion(name);
  24. if (card == null)
  25. error = $"找不到运动卡: {name}";
  26. return card;
  27. }
  28. public static string EnsureConnected(IMotionCard card, bool autoConnect)
  29. {
  30. if (card.IsConnected) return null;
  31. if (!autoConnect) return $"运动卡未连接: {card.Name}";
  32. return card.Open() ? null : $"自动连接失败: {card.Name}";
  33. }
  34. public static string EnsureEnabled(IMotionCard card, int axis, bool autoEnable)
  35. {
  36. var lockErr = CheckEnableAllowed(card);
  37. if (lockErr != null) return lockErr;
  38. if (axis < 0)
  39. {
  40. if (!autoEnable) return "未使能且未勾选自动使能";
  41. return card.SetEnable(-1, true) ? null : "全部轴自动使能失败";
  42. }
  43. var st = card.GetAxisStatus(axis);
  44. if (st == null) return $"轴号超出范围: {axis}";
  45. if (st.IsEnabled) return null;
  46. if (!autoEnable) return $"轴 {axis} 未使能(可勾选「未使能时自动使能」,或在流程中加入轴使能节点)";
  47. return card.SetEnable(axis, true) ? null : $"轴 {axis} 自动使能失败";
  48. }
  49. public static string CheckEnableAllowed(IMotionCard card)
  50. {
  51. if (card is IMotionSafetyHost s && !s.AllowEnable(out var reason))
  52. return reason;
  53. return null;
  54. }
  55. public static string CheckMotionAllowed(IMotionCard card)
  56. {
  57. if (card is IMotionSafetyHost s && !s.AllowMotion(out var reason))
  58. return reason;
  59. return null;
  60. }
  61. public static string WaitIdle(IMotionCard card, int axis, int timeoutMs, CancellationToken token)
  62. {
  63. token.ThrowIfCancellationRequested();
  64. // 仿真点到点:按剩余路程一次拨完时钟,与等待超时无关。
  65. (card as IMotionSimulation)?.FinishDiscreteMoves();
  66. var start = DateTime.UtcNow;
  67. while (true)
  68. {
  69. token.ThrowIfCancellationRequested();
  70. var st = card.GetAxisStatus(axis);
  71. if (st == null) return $"无法读取轴 {axis} 状态";
  72. if (st.Alarm) return $"轴 {axis} 报警";
  73. if (!st.IsMoving) return null;
  74. if (timeoutMs > 0 && (DateTime.UtcNow - start).TotalMilliseconds > timeoutMs)
  75. return $"等待到位超时 {timeoutMs}ms(轴 {axis})";
  76. Thread.Sleep(20);
  77. }
  78. }
  79. public static void FillStatus(Dictionary<string, object> res, AxisStatus st)
  80. {
  81. if (st == null) return;
  82. res["最终位置"] = st.Position;
  83. res["使能"] = st.IsEnabled;
  84. res["已回零"] = st.IsHomed;
  85. res["运动中"] = st.IsMoving;
  86. }
  87. }
  88. }