终端一体化运控平台
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

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