终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

334 lines
11 KiB

  1. using BPASmartClient.Helper;
  2. using BPASmartClient.Message;
  3. using BPASmartClient.Model;
  4. using Lebai.SDK;
  5. using Lebai.SDK.Dtos;
  6. using Robotc;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using TaskStatus = Lebai.SDK.Dtos.TaskStatus;
  12. namespace BPASmartClient.LebaiRobot
  13. {
  14. public class LebaiHelper:Singleton<LebaiHelper>
  15. {
  16. #region 且时且多设备
  17. //取咖啡杯 set:=1 get:=101 10031
  18. //取冰淇淋杯 set:=1 get:=101 10032
  19. //咖啡杯检测 set:=1 get:=101 10033
  20. //冰淇淋杯检测 set:=1 get:=101 10034
  21. //二次取咖啡杯 set:=1 get:=101 10035
  22. //二次取冰淇淋杯 set:=1 get:=101 10036
  23. //接咖啡 set:=1 get:=101 10037
  24. //接冰淇淋公共点 set:=1 get:=101 10038
  25. //接1号冰淇淋 set:=1 get:=101 10039
  26. //接2号冰淇淋 set:=1 get:=101 10040
  27. //接3号冰淇淋 set:=1 get:=101 10041
  28. //放咖啡位置 set:=1 get:=101 10042
  29. //放冰淇淋位置 set:=1 get:=101 10043
  30. public const int SENCE_取咖啡杯 = 10031;
  31. public const int SENCE_取冰淇淋杯 = 10032;
  32. public const int SENCE_咖啡杯检测 = 10033;
  33. public const int SENCE_冰淇淋杯检测 = 10034;
  34. public const int SENCE_二次取咖啡杯 = 10035;
  35. public const int SENCE_二次取冰淇淋杯 = 10036;
  36. public const int SENCE_接咖啡 = 10037;
  37. public const int SENCE_接冰淇淋公共点 = 10038;
  38. public const int SENCE_接1号冰淇淋 = 10039;
  39. public const int SENCE_接2号冰淇淋 = 10040;
  40. public const int SENCE_接3号冰淇淋 = 10041;
  41. public const int SENCE_放咖啡位置 = 10042;
  42. public const int SENCE_放冰淇淋位置 = 10043;
  43. //add 新加场景
  44. public const int SENCE_接咖啡后回原点 = 10051;
  45. public const int SENCE_咖啡杯回原点 = 10050;
  46. public const int SENCE_冰淇淋杯回原点 = 10049;
  47. public const int SENCE_取咖啡出餐 = 10052;
  48. #endregion
  49. private LebaiRobotClient client;
  50. public RobotData robotData;
  51. public bool IsIdle { get; set; } = false;
  52. public bool IsConnected { get { return null != robotData; } }
  53. public void Connect(string ip)
  54. {
  55. MessageLog.GetInstance.Show($"机器人IP地址:{ip}");
  56. bool ErrorFlag = false;
  57. while (robotData == null)
  58. {
  59. try
  60. {
  61. client = new LebaiRobotClient(ip);
  62. robotData = client.GetRobotData().Result;
  63. ActionManage.GetInstance.Send("乐白机器人数据");
  64. }
  65. catch (Exception ex)
  66. {
  67. if (!ErrorFlag)
  68. {
  69. MessageLog.GetInstance.ShowEx(ex.ToString());
  70. ErrorFlag = true;
  71. }
  72. Thread.Sleep(3000);
  73. }
  74. }
  75. StartRobot();
  76. MessageLog.GetInstance.Show("乐百机器人连接成功!");
  77. }
  78. /// <summary>
  79. /// 机器人重连检测
  80. /// </summary>
  81. /// <param name="ip"></param>
  82. public void Reconnect(string ip)
  83. {
  84. if (client == null)
  85. {
  86. try
  87. {
  88. client = new LebaiRobotClient(ip);
  89. }
  90. catch (Exception)
  91. {
  92. // throw;
  93. }
  94. }
  95. check:
  96. while (robotData != null)
  97. {
  98. try
  99. {
  100. robotData = client.GetRobotData().Result; }
  101. catch (Exception)
  102. {
  103. robotData = null;
  104. // throw;
  105. }
  106. }
  107. MessageLog.GetInstance.Show("乐白机器人断开连接,准备重连");
  108. int num = 0;
  109. while (num < 6 && robotData == null)
  110. {
  111. try
  112. {
  113. robotData = client.GetRobotData().Result;
  114. }
  115. catch (Exception ex)
  116. {
  117. if (num == 5)
  118. {
  119. MessageLog.GetInstance.ShowEx(ex.ToString());
  120. }
  121. }
  122. if (num < 5 && robotData == null)
  123. {
  124. Thread.Sleep(1000);
  125. MessageLog.GetInstance.Show($"乐白机器人正在尝试第{num + 1}次重连....");
  126. }
  127. else if (num >= 5 && robotData == null)
  128. {
  129. MessageLog.GetInstance.Show("乐白机器人重连失败,请检查硬件连接");
  130. //return;
  131. }
  132. num++;
  133. }
  134. if (robotData != null)
  135. {
  136. StartRobot();
  137. MessageLog.GetInstance.Show("乐白机器人重连成功");
  138. }
  139. goto check;
  140. }
  141. public void GetRobotModeStatus()
  142. {
  143. if (robotData == null)
  144. {
  145. return;
  146. }
  147. int mode = robotData.RobotMode.Mode;
  148. IsIdle = mode == 5;
  149. for (int i = 0; i < 14; i++)
  150. {
  151. if (RTrig.GetInstance(((ELebaiRModel)i).ToString()).Start(mode == i))
  152. MessageLog.GetInstance.Show(((ELebaiRModel)i).ToString());
  153. }
  154. }
  155. public void GetSpeed()
  156. {
  157. //client.StartSys().GetAwaiter();
  158. var aa = client.GetActualTcpSpeed().GetAwaiter();
  159. //robotData.RobotMode.Mode
  160. }
  161. /// <summary>
  162. /// 启动机器人
  163. /// </summary>
  164. public async void StartRobot()
  165. {
  166. if (robotData != null)
  167. {
  168. await client.StartSys();
  169. await client.Sync();
  170. MessageLog.GetInstance.Show("机器人启动成功");
  171. }
  172. }
  173. /// <summary>
  174. /// 启动示教
  175. /// </summary>
  176. public async void StartTeachMode()
  177. {
  178. try
  179. {
  180. if (robotData != null)
  181. {
  182. await client.TeachMode();
  183. MessageLog.GetInstance.Show("机器人切换为示教模式.");
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. MessageLog.GetInstance.ShowEx(ex.ToString());
  189. }
  190. }
  191. /// <summary>
  192. /// 结束示教
  193. /// </summary>
  194. public async void EndtTeachMode()
  195. {
  196. try
  197. {
  198. if (robotData != null)
  199. {
  200. await client.EndTeachMode();
  201. MessageLog.GetInstance.Show("机器人切换为停止示教模式.");
  202. }
  203. }
  204. catch (Exception ex)
  205. {
  206. MessageLog.GetInstance.ShowEx(ex.ToString());
  207. }
  208. }
  209. /// <summary>
  210. /// 机器人急停
  211. /// </summary>
  212. public async void EStopRobot()
  213. {
  214. try
  215. {
  216. if (robotData != null)
  217. {
  218. await client.EStop();
  219. MessageLog.GetInstance.Show("机器人急停");
  220. }
  221. }
  222. catch (Exception ex)
  223. {
  224. MessageLog.GetInstance.ShowEx(ex.ToString());
  225. }
  226. }
  227. /// <summary>
  228. /// 获取抓手重量
  229. /// </summary>
  230. /// <returns></returns>
  231. public double GetClawWdight()
  232. {
  233. if (robotData != null)
  234. {
  235. return client.GetClawWeight().Result.Weight_;
  236. }
  237. return 0;
  238. }
  239. /// <summary>
  240. /// 获取信号量
  241. /// </summary>
  242. /// <param name="index"></param>
  243. /// <returns></returns>
  244. public SignalResult? GetValueAsync(int index = 0)
  245. {
  246. if (robotData == null) return default(SignalResult);
  247. SignalValue signalValue = new SignalValue();
  248. signalValue.Index = index;
  249. return client?.GetSignal(signalValue).Result;
  250. }
  251. /// <summary>
  252. /// 设置信号量
  253. /// </summary>
  254. /// <param name="index"></param>
  255. /// <param name="value"></param>
  256. /// <returns></returns>
  257. public SignalResult? SetValue(int value, int index = 0)
  258. {
  259. if (robotData == null) return default;
  260. SignalValue signalValue = new SignalValue();
  261. signalValue.Index = index;
  262. signalValue.Value = value;
  263. return client.SetSignal(signalValue).Result;
  264. }
  265. /// <summary>
  266. /// 获取输入DI状态
  267. /// </summary>
  268. /// <param name="pin"></param>
  269. /// <returns></returns>
  270. public bool GetInput(int pin = 0)
  271. {
  272. if (client == null) return false;
  273. var res = client.GetDIO(new IOPin() { Pin = pin }).Result;
  274. if (res != null)
  275. {
  276. return res.Value == 1 ? true : false;
  277. }
  278. return false;
  279. }
  280. /// <summary>
  281. /// 运行指定的场景
  282. /// </summary>
  283. /// <param name="id"></param>
  284. public async void Scene(int id)
  285. {
  286. CancellationToken cancellationToken = default(CancellationToken);
  287. var result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken);
  288. var first = result?.Items?.FirstOrDefault();
  289. var r = first == null || first.Status != Lebai.SDK.Dtos.TaskStatus.Running && first.Status != TaskStatus.Pause;
  290. while (!r)
  291. {
  292. Thread.Sleep(5);
  293. result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken);
  294. first = result?.Items?.FirstOrDefault();
  295. r = first == null || first.Status != Lebai.SDK.Dtos.TaskStatus.Running && first.Status != Lebai.SDK.Dtos.TaskStatus.Pause;
  296. }
  297. while (GetValueAsync().Value != 0)
  298. {
  299. Thread.Sleep(500);
  300. }
  301. Thread.Sleep(1000);
  302. if (robotData == null || client == null) return;
  303. //if (!client.GetIsCanRunTask().Result) return;
  304. await client?.RunScene(id);
  305. MessageLog.GetInstance.Show($"调用场景:{id}");
  306. }
  307. }
  308. }