终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

383 lignes
11 KiB

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