终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

258 行
8.7 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 BPASmartClient.Message;
  11. using BPASmartClient.Helper;
  12. using TaskStatus = Lebai.SDK.Dtos.TaskStatus;
  13. using BPASmartClient.Peripheral;
  14. using BPASmartClient.EventBus;
  15. using static BPASmartClient.EventBus.EventBus;
  16. using BPASmartClient.Model;
  17. namespace BPASmartClient.Lebai
  18. {
  19. public class LebaiRobot: BasePeripheral
  20. {
  21. //public const int SENCE_自嗨 = 10008;
  22. //public const int SENCE_接冰淇淋1 = 10004;
  23. //public const int SENCE_接冰淇淋2 = 10003;
  24. //public const int SENCE_接冰淇淋3 = 10005;
  25. //public const int SENCE_咖啡 = 10006;
  26. //#region 新增场景
  27. //public const int SENCE_取咖啡杯 = 10013;
  28. //public const int SENCE_咖啡杯检测 = 10014;
  29. //public const int SENCE_再次取杯 = 10017;
  30. //public const int SENCE_接咖啡 = 10015;
  31. //public const int SENCE_送咖啡 = 10016; //无取走咖啡检测配合场景
  32. //public const int SENCE_New送咖啡 = 10021;//有取走咖啡检测配合场景
  33. //public const int SENCE_有无咖啡检测 = 10019;
  34. //public const int SENCE_再送咖啡 = 10020;
  35. //public const int SENCE_复位 = 10018;
  36. //#endregion
  37. //public const int SENCE_欢迎 = 10002;
  38. #region 且时且多设备
  39. //取咖啡杯 set:=1 get:=101 10031
  40. //取冰淇淋杯 set:=1 get:=101 10032
  41. //咖啡杯检测 set:=1 get:=101 10033
  42. //冰淇淋杯检测 set:=1 get:=101 10034
  43. //二次取咖啡杯 set:=1 get:=101 10035
  44. //二次取冰淇淋杯 set:=1 get:=101 10036
  45. //接咖啡 set:=1 get:=101 10037
  46. //接冰淇淋公共点 set:=1 get:=101 10038
  47. //接1号冰淇淋 set:=1 get:=101 10039
  48. //接2号冰淇淋 set:=1 get:=101 10040
  49. //接3号冰淇淋 set:=1 get:=101 10041
  50. //放咖啡位置 set:=1 get:=101 10042
  51. //放冰淇淋位置 set:=1 get:=101 10043
  52. public const int SENCE_取咖啡杯 = 10031;
  53. public const int SENCE_取冰淇淋杯 = 10032;
  54. public const int SENCE_咖啡杯检测 = 10033;
  55. public const int SENCE_冰淇淋杯检测 = 10034;
  56. public const int SENCE_二次取咖啡杯 = 10035;
  57. public const int SENCE_二次取冰淇淋杯 = 10036;
  58. public const int SENCE_接咖啡 = 10037;
  59. public const int SENCE_接冰淇淋公共点 = 10038;
  60. public const int SENCE_接1号冰淇淋 = 10039;
  61. public const int SENCE_接2号冰淇淋 = 10040;
  62. public const int SENCE_接3号冰淇淋 = 10041;
  63. public const int SENCE_放咖啡位置 = 10042;
  64. public const int SENCE_放冰淇淋位置 = 10043;
  65. #endregion
  66. /// <summary>
  67. /// IP地址
  68. /// </summary>
  69. public string IpAddress { get; set; }
  70. /// <summary>
  71. /// 抓手上传感器输入位索引
  72. /// </summary>
  73. public int HandSensor { get; set; }
  74. /// <summary>
  75. /// 输出信号量索引
  76. /// </summary>
  77. public int OutputSingalValue { get; set; }
  78. private LebaiRobotClient client;
  79. private RobotData robotData;
  80. public bool IsIdle { get; set; } = false;
  81. public bool IsConnected { get { return null != robotData; } }
  82. public void Connect(string ip)
  83. {
  84. bool ErrorFlag = false;
  85. while (robotData == null)
  86. {
  87. try
  88. {
  89. client = new LebaiRobotClient(ip);
  90. robotData = client.GetRobotData().Result;
  91. }
  92. catch (Exception ex)
  93. {
  94. if (!ErrorFlag)
  95. {
  96. MessageLog.GetInstance.ShowEx(ex.ToString());
  97. ErrorFlag = true;
  98. }
  99. Thread.Sleep(3000);
  100. }
  101. }
  102. StartRobot();
  103. MessageLog.GetInstance.Show("乐百机器人连接成功!");
  104. }
  105. public void GetRobotModeStatus()
  106. {
  107. if (robotData == null) return;
  108. int mode = robotData.RobotMode.Mode;
  109. IsIdle = mode == 5;
  110. for (int i = 0; i < 14; i++)
  111. {
  112. if (RTrig.GetInstance(((ELebaiRModel)i).ToString()).Start(mode == i))
  113. MessageLog.GetInstance.Show(((ELebaiRModel)i).ToString());
  114. }
  115. }
  116. public void GetSpeed()
  117. {
  118. //client.StartSys().GetAwaiter();
  119. var aa = client.GetActualTcpSpeed().GetAwaiter();
  120. //robotData.RobotMode.Mode
  121. }
  122. /// <summary>
  123. /// 启动机器人
  124. /// </summary>
  125. public async void StartRobot()
  126. {
  127. if (robotData != null)
  128. {
  129. await client.StartSys();
  130. await client.Sync();
  131. MessageLog.GetInstance.Show("机器人启动成功");
  132. }
  133. }
  134. /// <summary>
  135. /// 获取抓手重量
  136. /// </summary>
  137. /// <returns></returns>
  138. public double GetClawWdight()
  139. {
  140. if (robotData != null)
  141. {
  142. return client.GetClawWeight().Result.Weight_;
  143. }
  144. return 0;
  145. }
  146. /// <summary>
  147. /// 获取信号量
  148. /// </summary>
  149. /// <param name="index"></param>
  150. /// <returns></returns>
  151. public SignalResult? GetValueAsync(int index = 0)
  152. {
  153. if (robotData == null) return default(SignalResult);
  154. SignalValue signalValue = new SignalValue();
  155. signalValue.Index = index;
  156. return client?.GetSignal(signalValue).Result;
  157. }
  158. /// <summary>
  159. /// 设置信号量
  160. /// </summary>
  161. /// <param name="index"></param>
  162. /// <param name="value"></param>
  163. /// <returns></returns>
  164. public SignalResult? SetValue(int value, int index = 0)
  165. {
  166. if (robotData == null) return default;
  167. SignalValue signalValue = new SignalValue();
  168. signalValue.Index = index;
  169. signalValue.Value = value;
  170. return client.SetSignal(signalValue).Result;
  171. }
  172. /// <summary>
  173. /// 获取输入DI状态
  174. /// </summary>
  175. /// <param name="pin"></param>
  176. /// <returns></returns>
  177. public bool GetInput(int pin = 0)
  178. {
  179. if (client == null) return false;
  180. var res = client.GetDIO(new IOPin() { Pin = pin }).Result;
  181. if (res != null)
  182. {
  183. return res.Value == 1 ? true : false;
  184. }
  185. return false;
  186. }
  187. /// <summary>
  188. /// 运行指定的场景
  189. /// </summary>
  190. /// <param name="id"></param>
  191. public async void Scene(int id)
  192. {
  193. CancellationToken cancellationToken = default(CancellationToken);
  194. var result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken);
  195. var first = result?.Items?.FirstOrDefault();
  196. var r = first == null || first.Status != TaskStatus.Running && first.Status != TaskStatus.Pause;
  197. while (!r)
  198. {
  199. Thread.Sleep(5);
  200. result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken);
  201. first = result?.Items?.FirstOrDefault();
  202. r = first == null || first.Status != TaskStatus.Running && first.Status != TaskStatus.Pause;
  203. }
  204. while (GetValueAsync().Value != 0)
  205. {
  206. Thread.Sleep(500);
  207. }
  208. Thread.Sleep(1000);
  209. if (robotData == null || client == null) return;
  210. //if (!client.GetIsCanRunTask().Result) return;
  211. await client?.RunScene(id);
  212. MessageLog.GetInstance.Show($"调用场景:{id}");
  213. }
  214. protected override void InitStatus()
  215. {
  216. status["Connected"] = false;
  217. status["HandSensor"] = 0;
  218. status["OutputSingalValue"] = 0;
  219. }
  220. public override void Start()
  221. {
  222. Connect(IpAddress);
  223. }
  224. public override void Stop()
  225. {
  226. }
  227. public override void Init()
  228. {
  229. EventBus.EventBus.GetInstance().Subscribe<Demo_MakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack )
  230. {
  231. });
  232. InitStatus();
  233. }
  234. }
  235. }