终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

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