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.
 
 

464 linhas
15 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using HBLConsole.Service;
  8. using Lebai.SDK;
  9. using Lebai.SDK.Dtos;
  10. using Robotc;
  11. using HBLConsole.Model;
  12. namespace HBLConsole.Communication
  13. {
  14. public class LebaiHelper
  15. {
  16. public const int SENCE_自嗨 = 10008;
  17. //public const int SENCE_接冰淇淋1 = 10004;
  18. //public const int SENCE_接冰淇淋2 = 10003;
  19. //public const int SENCE_接冰淇淋3 = 10005;
  20. //public const int SENCE_咖啡 = 10006;
  21. //#region 新增场景
  22. //public const int SENCE_取咖啡杯 = 10013;
  23. //public const int SENCE_咖啡杯检测 = 10014;
  24. //public const int SENCE_再次取杯 = 10017;
  25. //public const int SENCE_接咖啡 = 10015;
  26. //public const int SENCE_送咖啡 = 10016; //无取走咖啡检测配合场景
  27. //public const int SENCE_New送咖啡 = 10021;//有取走咖啡检测配合场景
  28. //public const int SENCE_有无咖啡检测 = 10019;
  29. //public const int SENCE_再送咖啡 = 10020;
  30. //public const int SENCE_复位 = 10018;
  31. //#endregion
  32. //public const int SENCE_欢迎 = 10002;
  33. #region 且时且多设备
  34. public const int SENCE_取咖啡杯 = 10031;
  35. public const int SENCE_取冰淇淋杯 = 10032;
  36. public const int SENCE_咖啡杯检测 = 10033;
  37. public const int SENCE_冰淇淋杯检测 = 10034;
  38. public const int SENCE_二次取咖啡杯 = 10035;
  39. public const int SENCE_二次取冰淇淋杯 = 10036;
  40. public const int SENCE_接咖啡 = 10037;
  41. public const int SENCE_接冰淇淋公共点 = 10038;
  42. public const int SENCE_接1号冰淇淋 = 10039;
  43. public const int SENCE_接2号冰淇淋 = 10040;
  44. public const int SENCE_接3号冰淇淋 = 10041;
  45. public const int SENCE_放咖啡位置 = 10042;
  46. public const int SENCE_放冰淇淋位置 = 10043;
  47. //add 新加场景
  48. public const int SENCE_接咖啡_新 = 10051;
  49. public const int SENCE_咖啡杯回原点 = 10050;
  50. public const int SENCE_冰淇淋杯回原点 = 10049;
  51. public const int SENCE_取咖啡出餐 = 10052;
  52. #endregion
  53. #region 果汁机设备 MORKT1 JAKA机器人
  54. public const int JUICE_初始位 = 20000;
  55. public const int JUICE_取杯 = 20001;
  56. public const int JUICE_取杯检测 = 20010;
  57. public const int JUICE_接咖啡 = 20020;
  58. public const int JUICE_接1号果汁 = 20030;
  59. public const int JUICE_接2号果汁 = 20040;
  60. public const int JUICE_接3号果汁 = 20050;
  61. public const int JUICE_接4号果汁 = 20060;
  62. public const int JUICE_接茶 = 20070;
  63. public const int JUICE_接茶水 = 20075;
  64. public const int JUICE_接水 = 20080;
  65. public const int JUICE_放咖啡杯 = 20090;
  66. public const int JUICE_放果汁杯 = 20100;
  67. public const int JUICE_放茶水杯 = 20110;
  68. public const int JUICE_放杯 = 20120;
  69. public const int JUICE_放杯检测 = 20120;
  70. #endregion
  71. #region 果汁机设备MORKT2 Lebai机器人
  72. public const int JUICE2_初始位 = 10021;
  73. public const int JUICE2_取纸杯 = 10006;
  74. public const int JUICE2_二次取杯 = 10008;
  75. public const int JUICE2_纸杯检测 = 10007;
  76. public const int JUICE2_接咖啡 = 10009;
  77. public const int JUICE2_放咖啡杯 = 10010;
  78. public const int JUICE2_接茶叶 = 10017;
  79. public const int JUICE2_接茶水 = 10018;
  80. public const int JUICE2_接水 = 10020;
  81. public const int JUICE2_放水杯 = 10019;
  82. public const int JUICE2_接果汁公共位 = 10011;
  83. public const int JUICE2_接1号果汁 = 10012;
  84. public const int JUICE2_接2号果汁 = 10013;
  85. public const int JUICE2_接3号果汁 = 10014;
  86. public const int JUICE2_接4号果汁 = 10015;
  87. public const int JUICE2_放果汁杯 = 10016;
  88. #endregion
  89. private volatile static LebaiHelper _Instance;
  90. public static LebaiHelper GetInstance => _Instance ?? (_Instance = new LebaiHelper());
  91. private LebaiHelper() { }
  92. private LebaiRobotClient client;
  93. private RobotData robotData;
  94. public bool IsIdle { get; set; } = false;
  95. public bool IsConnected { get { return null != robotData; } }
  96. public void Connect(string ip)
  97. {
  98. bool ErrorFlag = false;
  99. while (robotData == null)
  100. {
  101. try
  102. {
  103. client = new LebaiRobotClient(ip);
  104. robotData = client.GetRobotData().Result;
  105. }
  106. catch (Exception ex)
  107. {
  108. if (!ErrorFlag)
  109. {
  110. MessageLog.GetInstance.ShowEx(ex.ToString());
  111. ErrorFlag = true;
  112. }
  113. Thread.Sleep(3000);
  114. }
  115. }
  116. StartRobot();
  117. MessageLog.GetInstance.Show("乐百机器人连接成功!");
  118. }
  119. /// <summary>
  120. /// 机器人重连检测
  121. /// </summary>
  122. /// <param name="ip"></param>
  123. public void Reconnect(string ip)
  124. {
  125. if (client == null)
  126. {
  127. try
  128. {
  129. client = new LebaiRobotClient(ip);
  130. }
  131. catch (Exception)
  132. {
  133. // throw;
  134. }
  135. }
  136. check:
  137. while (robotData!=null)
  138. {
  139. try
  140. {
  141. robotData = client.GetRobotData().Result;
  142. }
  143. catch (Exception)
  144. {
  145. robotData = null;
  146. // throw;
  147. }
  148. }
  149. MessageLog.GetInstance.Show("乐白机器人断开连接,准备重连");
  150. int num = 0;
  151. while (num < 6 && robotData == null)
  152. {
  153. try
  154. {
  155. robotData = client.GetRobotData().Result;
  156. }
  157. catch (Exception ex)
  158. {
  159. if (num == 5)
  160. {
  161. MessageLog.GetInstance.ShowEx(ex.ToString());
  162. }
  163. }
  164. if (num < 5&& robotData==null)
  165. {
  166. Thread.Sleep(1000);
  167. MessageLog.GetInstance.Show($"乐白机器人正在尝试第{num+1}次重连....");
  168. }
  169. else if(num>=5&&robotData==null)
  170. {
  171. MessageLog.GetInstance.Show("乐白机器人重连失败,请检查硬件连接");
  172. //return;
  173. }
  174. num++;
  175. }
  176. if (robotData!=null)
  177. {
  178. StartRobot();
  179. MessageLog.GetInstance.Show("乐白机器人重连成功");
  180. }
  181. goto check;
  182. }
  183. /// <summary>
  184. /// 获取机器人模式状态
  185. /// </summary>
  186. public void GetRobotModeStatus()
  187. {
  188. try
  189. {
  190. if (robotData == null) return;
  191. int mode = robotData.RobotMode.Mode;
  192. IsIdle = mode == 5;
  193. for (int i = 0; i < 14; i++)
  194. {
  195. if (RTrig.GetInstance(((ELebaiRModel)i).ToString()).Start(mode == i))
  196. MessageLog.GetInstance.Show(((ELebaiRModel)i).ToString());
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201. MessageLog.GetInstance.ShowEx(ex.ToString());
  202. }
  203. }
  204. /// <summary>
  205. /// 获取机器人速度因子
  206. /// </summary>
  207. public void GetSpeed()
  208. {
  209. //client.StartSys().GetAwaiter();
  210. var aa = client.GetActualTcpSpeed().GetAwaiter();
  211. //robotData.RobotMode.Mode
  212. }
  213. /// <summary>
  214. /// 启动机器人
  215. /// </summary>
  216. public async void StartRobot()
  217. {
  218. try
  219. {
  220. if (robotData != null)
  221. {
  222. await client.StartSys();
  223. await client.Sync();
  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. public async void StartTeachMode()
  236. {
  237. try
  238. {
  239. if (robotData != null)
  240. {
  241. await client.TeachMode();
  242. MessageLog.GetInstance.Show("机器人切换为示教模式.");
  243. }
  244. }
  245. catch (Exception ex)
  246. {
  247. MessageLog.GetInstance.ShowEx(ex.ToString());
  248. }
  249. }
  250. /// <summary>
  251. /// 结束示教
  252. /// </summary>
  253. public async void EndtTeachMode()
  254. {
  255. try
  256. {
  257. if (robotData != null)
  258. {
  259. await client.EndTeachMode();
  260. MessageLog.GetInstance.Show("机器人切换为停止示教模式.");
  261. }
  262. }
  263. catch (Exception ex)
  264. {
  265. MessageLog.GetInstance.ShowEx(ex.ToString());
  266. }
  267. }
  268. /// <summary>
  269. /// 机器人急停
  270. /// </summary>
  271. public async void EStopRobot()
  272. {
  273. try
  274. {
  275. if (robotData != null)
  276. {
  277. await client.EStop();
  278. MessageLog.GetInstance.Show("机器人急停");
  279. }
  280. }
  281. catch (Exception ex)
  282. {
  283. MessageLog.GetInstance.ShowEx(ex.ToString());
  284. }
  285. }
  286. /// <summary>
  287. /// 获取抓手重量
  288. /// </summary>
  289. /// <returns></returns>
  290. public double GetClawWdight()
  291. {
  292. if (robotData != null)
  293. {
  294. return client.GetClawWeight().Result.Weight_;
  295. }
  296. return 0;
  297. }
  298. /// <summary>
  299. /// 获取信号量
  300. /// </summary>
  301. /// <param name="index"></param>
  302. /// <returns></returns>
  303. public SignalResult GetValueAsync(int index = 0)
  304. {
  305. try
  306. {
  307. if (robotData == null) return default(SignalResult);
  308. SignalValue signalValue = new SignalValue();
  309. signalValue.Index = index;
  310. return client?.GetSignal(signalValue).Result;
  311. }
  312. catch (Exception ex)
  313. {
  314. MessageLog.GetInstance.ShowEx(ex.ToString());
  315. }
  316. return default(SignalResult);
  317. }
  318. /// <summary>
  319. /// 设置信号量
  320. /// </summary>
  321. /// <param name="index"></param>
  322. /// <param name="value"></param>
  323. /// <returns></returns>
  324. public SignalResult SetValue(int value, int index = 0)
  325. {
  326. try
  327. {
  328. if (robotData == null) return default(SignalResult);
  329. SignalValue signalValue = new SignalValue();
  330. signalValue.Index = index;
  331. signalValue.Value = value;
  332. return client.SetSignal(signalValue).Result;
  333. }
  334. catch (Exception ex)
  335. {
  336. MessageLog.GetInstance.ShowEx(ex.ToString());
  337. }
  338. return default(SignalResult);
  339. }
  340. /// <summary>
  341. /// 获取输入DI状态
  342. /// </summary>
  343. /// <param name="pin"></param>
  344. /// <returns></returns>
  345. public bool GetInput(int pin = 0)
  346. {
  347. try
  348. {
  349. if (client == null) return false;
  350. var res = client.GetDIO(new IOPin() { Pin = pin }).Result;
  351. if (res != null)
  352. {
  353. return res.Value == 1 ? true : false;
  354. }
  355. }
  356. catch (Exception ex)
  357. {
  358. MessageLog.GetInstance.ShowEx(ex.ToString());
  359. }
  360. return false;
  361. }
  362. public bool GetTcpInput(int pin =1)
  363. {
  364. try
  365. {
  366. if (client == null) return false;
  367. var res = client.GetTcpDIO(new IOPin() { Pin = pin }).Result;
  368. if (res != null)
  369. {
  370. return res.Value == 1 ? true : false;
  371. }
  372. }
  373. catch (Exception ex)
  374. {
  375. MessageLog.GetInstance.ShowEx(ex.ToString());
  376. }
  377. return false;
  378. }
  379. /// <summary>
  380. /// 设置DO输出
  381. /// </summary>
  382. /// <param name="pin"></param>
  383. /// <returns></returns>
  384. public void SetOutput(bool bDO, int pin = 0)
  385. {
  386. try
  387. {
  388. if (client == null) { return; }
  389. var res = client.SetDIO(new DIO() { Pin = pin , Value=bDO?1:0});
  390. }
  391. catch (Exception ex)
  392. {
  393. MessageLog.GetInstance.ShowEx(ex.ToString());
  394. }
  395. }
  396. /// <summary>
  397. /// 运行指定的场景
  398. /// </summary>
  399. /// <param name="id"></param>
  400. public async void Scene(int id)
  401. {
  402. try
  403. {
  404. CancellationToken cancellationToken = default(CancellationToken);
  405. var result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken);
  406. var first = result?.Items?.FirstOrDefault();
  407. var r = first == null || first.Status != Lebai.SDK.Dtos.TaskStatus.Running && first.Status != Lebai.SDK.Dtos.TaskStatus.Pause;
  408. while (!r)
  409. {
  410. Thread.Sleep(5);
  411. result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken);
  412. first = result?.Items?.FirstOrDefault();
  413. r = first == null || first.Status != Lebai.SDK.Dtos.TaskStatus.Running && first.Status != Lebai.SDK.Dtos.TaskStatus.Pause;
  414. }
  415. while (GetValueAsync().Value != 0)
  416. {
  417. Thread.Sleep(500);
  418. }
  419. Thread.Sleep(1000);
  420. if (robotData == null || client == null) return;
  421. //if (!client.GetIsCanRunTask().Result) return;
  422. await client?.RunScene(id);
  423. MessageLog.GetInstance.Show($"调用场景:{id}");
  424. }
  425. catch (Exception ex)
  426. {
  427. MessageLog.GetInstance.ShowEx(ex.ToString());
  428. }
  429. }
  430. }
  431. }