终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

LebaiHelper.cs 11 KiB

2年前
2年前
2年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 pauseMode()
  183. {
  184. try
  185. {
  186. if (robotData != null)
  187. {
  188. await client.Pause();
  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. public async void resumeMode()
  201. {
  202. try
  203. {
  204. if (robotData != null)
  205. {
  206. await client.Resume();
  207. MessageLog.GetInstance.Show("机器人切换为暂停后恢复模式.");
  208. }
  209. }
  210. catch (Exception ex)
  211. {
  212. MessageLog.GetInstance.ShowEx(ex.ToString());
  213. }
  214. }
  215. /// <summary>
  216. /// 机器人急停
  217. /// </summary>
  218. public async void EStopRobot()
  219. {
  220. try
  221. {
  222. if (robotData != null)
  223. {
  224. await client.EStop();
  225. MessageLog.GetInstance.Show("机器人急停");
  226. }
  227. }
  228. catch (Exception ex)
  229. {
  230. MessageLog.GetInstance.ShowEx(ex.ToString());
  231. }
  232. }
  233. /// <summary>
  234. /// 获取抓手重量
  235. /// </summary>
  236. /// <returns></returns>
  237. public double GetClawWdight()
  238. {
  239. if (robotData != null)
  240. {
  241. return client.GetClawWeight().Result.Weight_;
  242. }
  243. return 0;
  244. }
  245. /// <summary>
  246. /// 获取信号量
  247. /// </summary>
  248. /// <param name="index"></param>
  249. /// <returns></returns>
  250. public SignalResult? GetValueAsync(int index = 0)
  251. {
  252. if (robotData == null) return default(SignalResult);
  253. SignalValue signalValue = new SignalValue();
  254. signalValue.Index = index;
  255. return client?.GetSignal(signalValue).Result;
  256. }
  257. /// <summary>
  258. /// 设置信号量
  259. /// </summary>
  260. /// <param name="index"></param>
  261. /// <param name="value"></param>
  262. /// <returns></returns>
  263. public SignalResult? SetValue(int value, int index = 0)
  264. {
  265. if (robotData == null) return default;
  266. SignalValue signalValue = new SignalValue();
  267. signalValue.Index = index;
  268. signalValue.Value = value;
  269. return client.SetSignal(signalValue).Result;
  270. }
  271. /// <summary>
  272. /// 获取输入DI状态
  273. /// </summary>
  274. /// <param name="pin"></param>
  275. /// <returns></returns>
  276. public bool GetInput(int pin = 0)
  277. {
  278. if (client == null) return false;
  279. var res = client.GetDIO(new IOPin() { Pin = pin }).Result;
  280. if (res != null)
  281. {
  282. return res.Value == 1 ? true : false;
  283. }
  284. return false;
  285. }
  286. /// <summary>
  287. /// 设置DO输出
  288. /// </summary>
  289. /// <param name="pin"></param>
  290. /// <returns></returns>
  291. public void SetOutput(bool bDO, int pin = 0)
  292. {
  293. try
  294. {
  295. if (client == null) { return; }
  296. var res = client.SetDIO(new DIO() { Pin = pin, Value = bDO ? 1 : 0 });
  297. }
  298. catch (Exception ex)
  299. {
  300. MessageLog.GetInstance.ShowEx(ex.ToString());
  301. }
  302. }
  303. public bool GetTcpInput(int pin = 1)
  304. {
  305. try
  306. {
  307. if (client == null) return false;
  308. var res = client.GetTcpDIO(new IOPin() { Pin = pin }).Result;
  309. if (res != null)
  310. {
  311. return res.Value == 1 ? true : false;
  312. }
  313. }
  314. catch (Exception ex)
  315. {
  316. MessageLog.GetInstance.ShowEx(ex.ToString());
  317. }
  318. return false;
  319. }
  320. /// <summary>
  321. /// 运行指定的场景
  322. /// </summary>
  323. /// <param name="id"></param>
  324. public async void Scene(int id)
  325. {
  326. CancellationToken cancellationToken = default(CancellationToken);
  327. var result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken);
  328. var first = result?.Items?.FirstOrDefault();
  329. var r = first == null || first.Status != Lebai.SDK.Dtos.TaskStatus.Running && first.Status != TaskStatus.Pause;
  330. while (!r)
  331. {
  332. Thread.Sleep(5);
  333. result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken);
  334. first = result?.Items?.FirstOrDefault();
  335. r = first == null || first.Status != Lebai.SDK.Dtos.TaskStatus.Running && first.Status != Lebai.SDK.Dtos.TaskStatus.Pause;
  336. }
  337. while (GetValueAsync().Value != 0)
  338. {
  339. Thread.Sleep(500);
  340. }
  341. Thread.Sleep(1000);
  342. if (robotData == null || client == null) return;
  343. //if (!client.GetIsCanRunTask().Result) return;
  344. await client?.RunScene(id);
  345. MessageLog.GetInstance.Show($"调用场景:{id}");
  346. }
  347. //public async void ExecuteLua()
  348. // {
  349. // try
  350. // {
  351. // string lua = ""
  352. // if (client == null) return;
  353. // await client?.ExecuteLua()
  354. // }
  355. // catch (Exception ex)
  356. // }
  357. }
  358. }