Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 

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