MotionEnablePlugins.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. Log(3, $"轴使能 卡={card.Name} 轴={Model.AxisNo}");
  39. if (!card.SetEnable(Model.AxisNo, true))
  40. return Fail("下发使能失败");
  41. if (Model.AxisNo >= 0)
  42. MotionPluginHelper.FillStatus(res, card.GetAxisStatus(Model.AxisNo));
  43. else
  44. res["使能"] = true;
  45. res["结果"] = true;
  46. return NodeRunStatus.Success;
  47. }
  48. catch (OperationCanceledException) { throw; }
  49. catch (Exception ex) { return Fail(ex.Message); }
  50. }
  51. }
  52. [Serializable]
  53. [Plugin("轴下使能", PluginCategory.硬件模块, typeof(MotionDisableModel), "PowerOff",
  54. Description = "对指定轴或全部轴下使能")]
  55. public class MotionDisablePlugin : BasePlugin<MotionDisableModel>
  56. {
  57. public override List<OutputField> DeclareOutputs() => new List<OutputField>
  58. {
  59. new OutputField("结果", typeof(bool)),
  60. new OutputField("使能", typeof(bool)),
  61. new OutputField("最终位置", typeof(double)),
  62. };
  63. public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary<string, object> results)
  64. {
  65. var res = new Dictionary<string, object>();
  66. results = res;
  67. NodeRunStatus Fail(string m)
  68. {
  69. res["Error"] = m;
  70. res["结果"] = false;
  71. Log(1, $"轴下使能失败: {m}", TeamAAS.LogLevel.Error);
  72. return NodeRunStatus.Failed;
  73. }
  74. try
  75. {
  76. var card = MotionPluginHelper.GetCard(Service<MotionManager>(), Model.MotionCardName, out var err);
  77. if (card == null) return Fail(err);
  78. err = MotionPluginHelper.EnsureConnected(card, Model.AutoConnect);
  79. if (err != null) return Fail(err);
  80. Log(3, $"轴下使能 卡={card.Name} 轴={Model.AxisNo}");
  81. card.Stop(Model.AxisNo);
  82. if (!card.SetEnable(Model.AxisNo, false))
  83. return Fail("下发下使能失败");
  84. if (Model.AxisNo >= 0)
  85. MotionPluginHelper.FillStatus(res, card.GetAxisStatus(Model.AxisNo));
  86. else
  87. res["使能"] = false;
  88. res["结果"] = true;
  89. return NodeRunStatus.Success;
  90. }
  91. catch (OperationCanceledException) { throw; }
  92. catch (Exception ex) { return Fail(ex.Message); }
  93. }
  94. }
  95. }