终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

393 satır
12 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 Lebai.SDK;
  8. using Lebai.SDK.Dtos;
  9. using Robotc;
  10. using BPASmartClient.Message;
  11. using BPASmartClient.Helper;
  12. using TaskStatus = Lebai.SDK.Dtos.TaskStatus;
  13. using BPASmartClient.Peripheral;
  14. using BPASmartClient.EventBus;
  15. using static BPASmartClient.EventBus.EventBus;
  16. using BPASmartClient.Model;
  17. namespace BPASmartClient.Lebai
  18. {
  19. public class LebaiRobot: BasePeripheral
  20. {
  21. //public const int SENCE_自嗨 = 10008;
  22. //public const int SENCE_接冰淇淋1 = 10004;
  23. //public const int SENCE_接冰淇淋2 = 10003;
  24. //public const int SENCE_接冰淇淋3 = 10005;
  25. //public const int SENCE_咖啡 = 10006;
  26. //#region 新增场景
  27. //public const int SENCE_取咖啡杯 = 10013;
  28. //public const int SENCE_咖啡杯检测 = 10014;
  29. //public const int SENCE_再次取杯 = 10017;
  30. //public const int SENCE_接咖啡 = 10015;
  31. //public const int SENCE_送咖啡 = 10016; //无取走咖啡检测配合场景
  32. //public const int SENCE_New送咖啡 = 10021;//有取走咖啡检测配合场景
  33. //public const int SENCE_有无咖啡检测 = 10019;
  34. //public const int SENCE_再送咖啡 = 10020;
  35. //public const int SENCE_复位 = 10018;
  36. //#endregion
  37. //public const int SENCE_欢迎 = 10002;
  38. #region 且时且多设备
  39. //取咖啡杯 set:=1 get:=101 10031
  40. //取冰淇淋杯 set:=1 get:=101 10032
  41. //咖啡杯检测 set:=1 get:=101 10033
  42. //冰淇淋杯检测 set:=1 get:=101 10034
  43. //二次取咖啡杯 set:=1 get:=101 10035
  44. //二次取冰淇淋杯 set:=1 get:=101 10036
  45. //接咖啡 set:=1 get:=101 10037
  46. //接冰淇淋公共点 set:=1 get:=101 10038
  47. //接1号冰淇淋 set:=1 get:=101 10039
  48. //接2号冰淇淋 set:=1 get:=101 10040
  49. //接3号冰淇淋 set:=1 get:=101 10041
  50. //放咖啡位置 set:=1 get:=101 10042
  51. //放冰淇淋位置 set:=1 get:=101 10043
  52. public const int SENCE_取咖啡杯 = 10031;
  53. public const int SENCE_取冰淇淋杯 = 10032;
  54. public const int SENCE_咖啡杯检测 = 10033;
  55. public const int SENCE_冰淇淋杯检测 = 10034;
  56. public const int SENCE_二次取咖啡杯 = 10035;
  57. public const int SENCE_二次取冰淇淋杯 = 10036;
  58. public const int SENCE_接咖啡 = 10037;
  59. public const int SENCE_接冰淇淋公共点 = 10038;
  60. public const int SENCE_接1号冰淇淋 = 10039;
  61. public const int SENCE_接2号冰淇淋 = 10040;
  62. public const int SENCE_接3号冰淇淋 = 10041;
  63. public const int SENCE_放咖啡位置 = 10042;
  64. public const int SENCE_放冰淇淋位置 = 10043;
  65. //add 新加场景
  66. public const int SENCE_接咖啡后回原点 = 10051;
  67. public const int SENCE_咖啡杯回原点 = 10050;
  68. public const int SENCE_冰淇淋杯回原点 = 10049;
  69. public const int SENCE_取咖啡出餐 = 10052;
  70. #endregion
  71. /// <summary>
  72. /// IP地址
  73. /// </summary>
  74. public string IpAddress { get; set; }
  75. /// <summary>
  76. /// 抓手上传感器输入位索引
  77. /// </summary>
  78. public int HandSensor { get; set; }
  79. /// <summary>
  80. /// 输出信号量索引
  81. /// </summary>
  82. public int OutputSingalValue { get; set; }
  83. private volatile static LebaiRobot _Instance;
  84. public static LebaiRobot GetInstance => _Instance ?? (_Instance = new LebaiRobot());
  85. private LebaiRobot() { }
  86. private LebaiRobotClient client;
  87. private RobotData robotData;
  88. public bool IsIdle { get; set; } = false;
  89. public bool IsConnected { get { return null != robotData; } }
  90. public void Connect(string ip)
  91. {
  92. bool ErrorFlag = false;
  93. while (robotData == null)
  94. {
  95. try
  96. {
  97. client = new LebaiRobotClient(ip);
  98. robotData = client.GetRobotData().Result;
  99. }
  100. catch (Exception ex)
  101. {
  102. if (!ErrorFlag)
  103. {
  104. MessageLog.GetInstance.ShowEx(ex.ToString());
  105. ErrorFlag = true;
  106. }
  107. Thread.Sleep(3000);
  108. }
  109. }
  110. StartRobot();
  111. MessageLog.GetInstance.Show("乐百机器人连接成功!");
  112. }
  113. /// <summary>
  114. /// 机器人重连检测
  115. /// </summary>
  116. /// <param name="ip"></param>
  117. public void Reconnect(string ip)
  118. {
  119. if (client == null)
  120. {
  121. try
  122. {
  123. client = new LebaiRobotClient(ip);
  124. }
  125. catch (Exception)
  126. {
  127. // throw;
  128. }
  129. }
  130. check:
  131. while (robotData != null)
  132. {
  133. try
  134. {
  135. robotData = client.GetRobotData().Result;
  136. }
  137. catch (Exception)
  138. {
  139. robotData = null;
  140. // throw;
  141. }
  142. }
  143. MessageLog.GetInstance.Show("乐白机器人断开连接,准备重连");
  144. int num = 0;
  145. while (num < 6 && robotData == null)
  146. {
  147. try
  148. {
  149. robotData = client.GetRobotData().Result;
  150. }
  151. catch (Exception ex)
  152. {
  153. if (num == 5)
  154. {
  155. MessageLog.GetInstance.ShowEx(ex.ToString());
  156. }
  157. }
  158. if (num < 5 && robotData == null)
  159. {
  160. Thread.Sleep(1000);
  161. MessageLog.GetInstance.Show($"乐白机器人正在尝试第{num + 1}次重连....");
  162. }
  163. else if (num >= 5 && robotData == null)
  164. {
  165. MessageLog.GetInstance.Show("乐白机器人重连失败,请检查硬件连接");
  166. //return;
  167. }
  168. num++;
  169. }
  170. if (robotData != null)
  171. {
  172. StartRobot();
  173. MessageLog.GetInstance.Show("乐白机器人重连成功");
  174. }
  175. goto check;
  176. }
  177. public void GetRobotModeStatus()
  178. {
  179. if (robotData == null) return;
  180. int mode = robotData.RobotMode.Mode;
  181. IsIdle = mode == 5;
  182. for (int i = 0; i < 14; i++)
  183. {
  184. if (RTrig.GetInstance(((ELebaiRModel)i).ToString()).Start(mode == i))
  185. MessageLog.GetInstance.Show(((ELebaiRModel)i).ToString());
  186. }
  187. }
  188. public void GetSpeed()
  189. {
  190. //client.StartSys().GetAwaiter();
  191. var aa = client.GetActualTcpSpeed().GetAwaiter();
  192. //robotData.RobotMode.Mode
  193. }
  194. /// <summary>
  195. /// 启动机器人
  196. /// </summary>
  197. public async void StartRobot()
  198. {
  199. if (robotData != null)
  200. {
  201. await client.StartSys();
  202. await client.Sync();
  203. MessageLog.GetInstance.Show("机器人启动成功");
  204. }
  205. }
  206. /// <summary>
  207. /// 启动示教
  208. /// </summary>
  209. public async void StartTeachMode()
  210. {
  211. try
  212. {
  213. if (robotData != null)
  214. {
  215. await client.TeachMode();
  216. MessageLog.GetInstance.Show("机器人切换为示教模式.");
  217. }
  218. }
  219. catch (Exception ex)
  220. {
  221. MessageLog.GetInstance.ShowEx(ex.ToString());
  222. }
  223. }
  224. /// <summary>
  225. /// 结束示教
  226. /// </summary>
  227. public async void EndtTeachMode()
  228. {
  229. try
  230. {
  231. if (robotData != null)
  232. {
  233. await client.EndTeachMode();
  234. MessageLog.GetInstance.Show("机器人切换为停止示教模式.");
  235. }
  236. }
  237. catch (Exception ex)
  238. {
  239. MessageLog.GetInstance.ShowEx(ex.ToString());
  240. }
  241. }
  242. /// <summary>
  243. /// 机器人急停
  244. /// </summary>
  245. public async void EStopRobot()
  246. {
  247. try
  248. {
  249. if (robotData != null)
  250. {
  251. await client.EStop();
  252. MessageLog.GetInstance.Show("机器人急停");
  253. }
  254. }
  255. catch (Exception ex)
  256. {
  257. MessageLog.GetInstance.ShowEx(ex.ToString());
  258. }
  259. }
  260. /// <summary>
  261. /// 获取抓手重量
  262. /// </summary>
  263. /// <returns></returns>
  264. public double GetClawWdight()
  265. {
  266. if (robotData != null)
  267. {
  268. return client.GetClawWeight().Result.Weight_;
  269. }
  270. return 0;
  271. }
  272. /// <summary>
  273. /// 获取信号量
  274. /// </summary>
  275. /// <param name="index"></param>
  276. /// <returns></returns>
  277. public SignalResult? GetValueAsync(int index = 0)
  278. {
  279. if (robotData == null) return default(SignalResult);
  280. SignalValue signalValue = new SignalValue();
  281. signalValue.Index = index;
  282. return client?.GetSignal(signalValue).Result;
  283. }
  284. /// <summary>
  285. /// 设置信号量
  286. /// </summary>
  287. /// <param name="index"></param>
  288. /// <param name="value"></param>
  289. /// <returns></returns>
  290. public SignalResult? SetValue(int value, int index = 0)
  291. {
  292. if (robotData == null) return default;
  293. SignalValue signalValue = new SignalValue();
  294. signalValue.Index = index;
  295. signalValue.Value = value;
  296. return client.SetSignal(signalValue).Result;
  297. }
  298. /// <summary>
  299. /// 获取输入DI状态
  300. /// </summary>
  301. /// <param name="pin"></param>
  302. /// <returns></returns>
  303. public bool GetInput(int pin = 0)
  304. {
  305. if (client == null) return false;
  306. var res = client.GetDIO(new IOPin() { Pin = pin }).Result;
  307. if (res != null)
  308. {
  309. return res.Value == 1 ? true : false;
  310. }
  311. return false;
  312. }
  313. /// <summary>
  314. /// 运行指定的场景
  315. /// </summary>
  316. /// <param name="id"></param>
  317. public async void Scene(int id)
  318. {
  319. CancellationToken cancellationToken = default(CancellationToken);
  320. var result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken);
  321. var first = result?.Items?.FirstOrDefault();
  322. var r = first == null || first.Status != TaskStatus.Running && first.Status != TaskStatus.Pause;
  323. while (!r)
  324. {
  325. Thread.Sleep(5);
  326. result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken);
  327. first = result?.Items?.FirstOrDefault();
  328. r = first == null || first.Status != TaskStatus.Running && first.Status != TaskStatus.Pause;
  329. }
  330. while (GetValueAsync().Value != 0)
  331. {
  332. Thread.Sleep(500);
  333. }
  334. Thread.Sleep(1000);
  335. if (robotData == null || client == null) return;
  336. //if (!client.GetIsCanRunTask().Result) return;
  337. await client?.RunScene(id);
  338. MessageLog.GetInstance.Show($"调用场景:{id}");
  339. }
  340. protected override void InitStatus()
  341. {
  342. status["Connected"] = false;
  343. status["HandSensor"] = 0;
  344. status["OutputSingalValue"] = 0;
  345. }
  346. public override void Start()
  347. {
  348. Connect(IpAddress);
  349. }
  350. public override void Stop()
  351. {
  352. }
  353. public override void Init()
  354. {
  355. EventBus.EventBus.GetInstance().Subscribe<Demo_MakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack )
  356. {
  357. });
  358. InitStatus();
  359. }
  360. }
  361. }