You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

275 lines
9.0 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. public void GetRobotModeStatus()
  82. {
  83. try
  84. {
  85. if (robotData == null) return;
  86. int mode = robotData.RobotMode.Mode;
  87. IsIdle = mode == 5;
  88. for (int i = 0; i < 14; i++)
  89. {
  90. if (RTrig.GetInstance(((ELebaiRModel)i).ToString()).Start(mode == i))
  91. MessageLog.GetInstance.Show(((ELebaiRModel)i).ToString());
  92. }
  93. }
  94. catch (Exception ex)
  95. {
  96. MessageLog.GetInstance.ShowEx(ex.ToString());
  97. }
  98. }
  99. /// <summary>
  100. /// 获取机器人速度因子
  101. /// </summary>
  102. public void GetSpeed()
  103. {
  104. //client.StartSys().GetAwaiter();
  105. var aa = client.GetActualTcpSpeed().GetAwaiter();
  106. //robotData.RobotMode.Mode
  107. }
  108. /// <summary>
  109. /// 启动机器人
  110. /// </summary>
  111. public async void StartRobot()
  112. {
  113. try
  114. {
  115. if (robotData != null)
  116. {
  117. await client.StartSys();
  118. await client.Sync();
  119. MessageLog.GetInstance.Show("机器人启动成功");
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. MessageLog.GetInstance.ShowEx(ex.ToString());
  125. }
  126. }
  127. /// <summary>
  128. /// 获取抓手重量
  129. /// </summary>
  130. /// <returns></returns>
  131. public double GetClawWdight()
  132. {
  133. if (robotData != null)
  134. {
  135. return client.GetClawWeight().Result.Weight_;
  136. }
  137. return 0;
  138. }
  139. /// <summary>
  140. /// 获取信号量
  141. /// </summary>
  142. /// <param name="index"></param>
  143. /// <returns></returns>
  144. public SignalResult GetValueAsync(int index = 0)
  145. {
  146. try
  147. {
  148. if (robotData == null) return default(SignalResult);
  149. SignalValue signalValue = new SignalValue();
  150. signalValue.Index = index;
  151. return client?.GetSignal(signalValue).Result;
  152. }
  153. catch (Exception ex)
  154. {
  155. MessageLog.GetInstance.ShowEx(ex.ToString());
  156. }
  157. return default(SignalResult);
  158. }
  159. /// <summary>
  160. /// 设置信号量
  161. /// </summary>
  162. /// <param name="index"></param>
  163. /// <param name="value"></param>
  164. /// <returns></returns>
  165. public SignalResult SetValue(int value, int index = 0)
  166. {
  167. try
  168. {
  169. if (robotData == null) return default(SignalResult);
  170. SignalValue signalValue = new SignalValue();
  171. signalValue.Index = index;
  172. signalValue.Value = value;
  173. return client.SetSignal(signalValue).Result;
  174. }
  175. catch (Exception ex)
  176. {
  177. MessageLog.GetInstance.ShowEx(ex.ToString());
  178. }
  179. return default(SignalResult);
  180. }
  181. /// <summary>
  182. /// 获取输入DI状态
  183. /// </summary>
  184. /// <param name="pin"></param>
  185. /// <returns></returns>
  186. public bool GetInput(int pin = 0)
  187. {
  188. try
  189. {
  190. if (client == null) return false;
  191. var res = client.GetDIO(new IOPin() { Pin = pin }).Result;
  192. if (res != null)
  193. {
  194. return res.Value == 1 ? true : false;
  195. }
  196. }
  197. catch (Exception ex)
  198. {
  199. MessageLog.GetInstance.ShowEx(ex.ToString());
  200. }
  201. return false;
  202. }
  203. public bool GetTcpInput(int pin =1)
  204. {
  205. try
  206. {
  207. if (client == null) return false;
  208. var res = client.GetTcpDIO(new IOPin() { Pin = pin }).Result;
  209. if (res != null)
  210. {
  211. return res.Value == 1 ? true : false;
  212. }
  213. }
  214. catch (Exception ex)
  215. {
  216. MessageLog.GetInstance.ShowEx(ex.ToString());
  217. }
  218. return false;
  219. }
  220. /// <summary>
  221. /// 运行指定的场景
  222. /// </summary>
  223. /// <param name="id"></param>
  224. public async void Scene(int id)
  225. {
  226. try
  227. {
  228. CancellationToken cancellationToken = default(CancellationToken);
  229. var result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken);
  230. var first = result?.Items?.FirstOrDefault();
  231. var r = first == null || first.Status != Lebai.SDK.Dtos.TaskStatus.Running && first.Status != Lebai.SDK.Dtos.TaskStatus.Pause;
  232. while (!r)
  233. {
  234. Thread.Sleep(5);
  235. result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken);
  236. first = result?.Items?.FirstOrDefault();
  237. r = first == null || first.Status != Lebai.SDK.Dtos.TaskStatus.Running && first.Status != Lebai.SDK.Dtos.TaskStatus.Pause;
  238. }
  239. while (GetValueAsync().Value != 0)
  240. {
  241. Thread.Sleep(500);
  242. }
  243. Thread.Sleep(1000);
  244. if (robotData == null || client == null) return;
  245. //if (!client.GetIsCanRunTask().Result) return;
  246. await client?.RunScene(id);
  247. MessageLog.GetInstance.Show($"调用场景:{id}");
  248. }
  249. catch (Exception ex)
  250. {
  251. MessageLog.GetInstance.ShowEx(ex.ToString());
  252. }
  253. }
  254. }
  255. }