MotionEnablePlugins.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using Plugins.Motion.Models;
  5. using TeamAAS.FlowEditor.Models;
  6. using TeamAAS.FlowEditor.Plugins;
  7. using TeamAAS.Motion;
  8. namespace Plugins.Motion
  9. {
  10. [Serializable]
  11. [Plugin("轴使能", PluginCategory.硬件模块, typeof(MotionEnableModel), "Power",
  12. Description = "对指定轴或全部轴上使能")]
  13. public class MotionEnablePlugin : BasePlugin<MotionEnableModel>
  14. {
  15. public override List<OutputField> DeclareOutputs() => new List<OutputField>
  16. {
  17. new OutputField("结果", typeof(bool)),
  18. new OutputField("使能", typeof(bool)),
  19. new OutputField("最终位置", typeof(double)),
  20. };
  21. public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary<string, object> results)
  22. {
  23. var res = new Dictionary<string, object>();
  24. results = res;
  25. NodeRunStatus Fail(string m)
  26. {
  27. res["Error"] = m;
  28. res["结果"] = false;
  29. Log(1, $"轴使能失败: {m}", TeamAAS.LogLevel.Error);
  30. return NodeRunStatus.Failed;
  31. }
  32. try
  33. {
  34. var card = MotionPluginHelper.GetCard(Service<MotionManager>(), Model.MotionCardName, out var err);
  35. if (card == null) return Fail(err);
  36. err = MotionPluginHelper.EnsureConnected(card, Model.AutoConnect);
  37. if (err != null) return Fail(err);
  38. err = MotionPluginHelper.CheckEnableAllowed(card);
  39. if (err != null) return Fail(err);
  40. Log(3, $"轴使能 卡={card.Name} 轴={Model.AxisNo}");
  41. if (!card.SetEnable(Model.AxisNo, true))
  42. return Fail("下发使能失败");
  43. if (Model.AxisNo >= 0)
  44. MotionPluginHelper.FillStatus(res, card.GetAxisStatus(Model.AxisNo));
  45. else
  46. res["使能"] = true;
  47. res["结果"] = true;
  48. return NodeRunStatus.Success;
  49. }
  50. catch (OperationCanceledException) { throw; }
  51. catch (Exception ex) { return Fail(ex.Message); }
  52. }
  53. }
  54. [Serializable]
  55. [Plugin("轴下使能", PluginCategory.硬件模块, typeof(MotionDisableModel), "PowerOff",
  56. Description = "对指定轴或全部轴下使能")]
  57. public class MotionDisablePlugin : BasePlugin<MotionDisableModel>
  58. {
  59. public override List<OutputField> DeclareOutputs() => new List<OutputField>
  60. {
  61. new OutputField("结果", typeof(bool)),
  62. new OutputField("使能", typeof(bool)),
  63. new OutputField("最终位置", typeof(double)),
  64. };
  65. public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary<string, object> results)
  66. {
  67. var res = new Dictionary<string, object>();
  68. results = res;
  69. NodeRunStatus Fail(string m)
  70. {
  71. res["Error"] = m;
  72. res["结果"] = false;
  73. Log(1, $"轴下使能失败: {m}", TeamAAS.LogLevel.Error);
  74. return NodeRunStatus.Failed;
  75. }
  76. try
  77. {
  78. var card = MotionPluginHelper.GetCard(Service<MotionManager>(), Model.MotionCardName, out var err);
  79. if (card == null) return Fail(err);
  80. err = MotionPluginHelper.EnsureConnected(card, Model.AutoConnect);
  81. if (err != null) return Fail(err);
  82. Log(3, $"轴下使能 卡={card.Name} 轴={Model.AxisNo}");
  83. card.Stop(Model.AxisNo);
  84. if (!card.SetEnable(Model.AxisNo, false))
  85. return Fail("下发下使能失败");
  86. if (Model.AxisNo >= 0)
  87. MotionPluginHelper.FillStatus(res, card.GetAxisStatus(Model.AxisNo));
  88. else
  89. res["使能"] = false;
  90. res["结果"] = true;
  91. return NodeRunStatus.Success;
  92. }
  93. catch (OperationCanceledException) { throw; }
  94. catch (Exception ex) { return Fail(ex.Message); }
  95. }
  96. }
  97. }