终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

166 righe
6.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Lebai.SDK;
  8. using Lebai.SDK.Dtos;
  9. using Robotc;
  10. using BPA.Helper;
  11. using TaskStatus = Lebai.SDK.Dtos.TaskStatus;
  12. using BPASmartClient.Peripheral;
  13. using BPASmartClient.Model;
  14. using BPASmartClient.LebaiRobot;
  15. using BPASmartClient.Model.乐白机器人;
  16. using static BPA.Helper.EventBus;
  17. namespace BPASmartClient.Lebai
  18. {
  19. public class LebaiRobot : BasePeripheral
  20. {
  21. /// <summary>
  22. /// 抓手上传感器输入位索引
  23. /// </summary>
  24. public int HandSensor { get; set; }
  25. /// <summary>
  26. /// 输出信号量索引
  27. /// </summary>
  28. public int OutputSingalValue { get; set; }
  29. protected override void InitStatus()
  30. {
  31. status["Connected"] = false;
  32. status["HandSensor"] = 0;
  33. status["OutputSingalValue"] = 0;
  34. }
  35. public override void Start()
  36. {
  37. LebaiHelper.GetInstance().Connect(communicationPar.IPAddress);
  38. TaskManage.GetInstance.StartLong(() => { LebaiHelper.GetInstance().Reconnect(communicationPar.IPAddress); Thread.Sleep(5000); }, "机器人重连检测");
  39. TaskManage.GetInstance.StartLong(() =>
  40. {
  41. IsConnected = LebaiHelper.GetInstance().IsConnected;
  42. status["RobotIsConnected"] = LebaiHelper.GetInstance().IsConnected;
  43. status["RobotOK"] = LebaiHelper.GetInstance().GetValueAsync().Ok;
  44. status["RobotValue"] = LebaiHelper.GetInstance().GetValueAsync().Value;
  45. if (LebaiHelper.GetInstance().robotData != null) status["RobotMode"] = (ELebaiRModel)LebaiHelper.GetInstance().robotData.RobotMode.Mode;
  46. status["RobotValue1"] = LebaiHelper.GetInstance().GetValueAsync(1).Value;
  47. status["LeibaiGetTcpInput"] = LebaiHelper.GetInstance().GetTcpInput(0);
  48. status["LeibaiGetInput1"] = LebaiHelper.GetInstance().GetInput(0);
  49. status["LeibaiGetInput2"] = LebaiHelper.GetInstance().GetInput(1);
  50. status["LeibaiGetInput3"] = LebaiHelper.GetInstance().GetInput(2);
  51. status["LeibaiGetInput4"] = LebaiHelper.GetInstance().GetInput(3);
  52. if (LebaiHelper.GetInstance().robotData != null) status["RobotMode"] = LebaiHelper.GetInstance().robotData.RobotMode.Mode;
  53. LebaiHelper.GetInstance().GetRobotModeStatus();
  54. Thread.Sleep(10);
  55. }, "获取乐白机器人数据");
  56. }
  57. public override void Stop()
  58. {
  59. }
  60. public override void Init()
  61. {
  62. EventBus.GetInstance().Subscribe<Demo_MakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  63. {
  64. });
  65. //获取机器人信号
  66. EventBus.GetInstance().Subscribe<LebaiRobot_GetInputEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  67. {
  68. if (@event == null) return;
  69. if (@event is LebaiRobot_GetInputEvent getInput)
  70. {
  71. callBack.Invoke(LebaiHelper.GetInstance().GetInput(getInput.Pin));
  72. }
  73. });
  74. //获取Tcp信号
  75. EventBus.GetInstance().Subscribe<LebaiRobot_GetTCPInputEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  76. {
  77. if (@event == null) return;
  78. if (@event is LebaiRobot_GetTCPInputEvent getTCPInput)
  79. {
  80. callBack?.Invoke(LebaiHelper.GetInstance().GetTcpInput(getTCPInput.Pin));
  81. }
  82. });
  83. //机器人输入信号
  84. EventBus.GetInstance().Subscribe<LebaiRobot_SetValueEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  85. {
  86. if (@event == null) return;
  87. if (@event is LebaiRobot_SetValueEvent SetValueEvent)
  88. {
  89. callBack?.Invoke(LebaiHelper.GetInstance().SetValue(SetValueEvent.RobotSetValue));
  90. }
  91. });
  92. //控制机器人
  93. EventBus.GetInstance().Subscribe<LebaiRobot_LebaiControlEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  94. {
  95. if (@event == null) return;
  96. if (@event is LebaiRobot_LebaiControlEvent lebaiControlEvent)
  97. {
  98. switch (lebaiControlEvent.LebaiControl)
  99. {
  100. case "启动":
  101. LebaiHelper.GetInstance().StartRobot();
  102. break;
  103. case "启动示教":
  104. LebaiHelper.GetInstance().StartTeachMode();
  105. break;
  106. case "停止示教":
  107. LebaiHelper.GetInstance().EndtTeachMode();
  108. break;
  109. case "急停":
  110. LebaiHelper.GetInstance().EStopRobot();
  111. break;
  112. case "暂停":
  113. LebaiHelper.GetInstance().pauseMode();
  114. break;
  115. case "恢复":
  116. LebaiHelper.GetInstance().resumeMode();
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. });
  123. //选择机器人场景
  124. EventBus.GetInstance().Subscribe<LebaiRobot_LebaiSenceEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  125. {
  126. if (@event == null) return;
  127. if (@event is LebaiRobot_LebaiSenceEvent lebaiSenceEvent)
  128. {
  129. LebaiHelper.GetInstance().Scene(lebaiSenceEvent.LebaiSence);
  130. }
  131. });
  132. EventBus.GetInstance().Subscribe<LebaiRobot_SetOutPutEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  133. {
  134. if (@event == null) return;
  135. if (@event is LebaiRobot_SetOutPutEvent lebaiOutputEvent)
  136. {
  137. LebaiHelper.GetInstance().SetOutput(lebaiOutputEvent.Value, lebaiOutputEvent.Pin);
  138. }
  139. });
  140. InitStatus();
  141. Start();
  142. MessageLog.GetInstance.Show("乐白机器人初始化完成");
  143. }
  144. public override void WriteData(string address, object value)
  145. {
  146. }
  147. }
  148. }