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.
 
 

180 lines
5.5 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. private volatile static LebaiHelper _Instance;
  34. public static LebaiHelper GetInstance => _Instance ?? (_Instance = new LebaiHelper());
  35. private LebaiHelper() { }
  36. private LebaiRobotClient client;
  37. private RobotData robotData;
  38. public bool IsIdle { get; set; } = false;
  39. public bool IsConnected { get { return null != robotData; } }
  40. public void Connect(string ip)
  41. {
  42. bool ErrorFlag = false;
  43. while (robotData == null)
  44. {
  45. try
  46. {
  47. client = new LebaiRobotClient(ip);
  48. robotData = client.GetRobotData().Result;
  49. }
  50. catch (Exception ex)
  51. {
  52. if (!ErrorFlag)
  53. {
  54. MessageLog.GetInstance.ShowEx(ex.ToString());
  55. ErrorFlag = true;
  56. }
  57. Thread.Sleep(3000);
  58. }
  59. }
  60. StartRobot();
  61. MessageLog.GetInstance.Show("乐百机器人连接成功!");
  62. }
  63. public void GetRobotModeStatus()
  64. {
  65. if (robotData == null) return;
  66. int mode = robotData.RobotMode.Mode;
  67. IsIdle = mode == 5;
  68. for (int i = 0; i < 14; i++)
  69. {
  70. if (RTrig.GetInstance(((ELebaiRModel)i).ToString()).Start(mode == i))
  71. MessageLog.GetInstance.Show(((ELebaiRModel)i).ToString());
  72. }
  73. }
  74. public void GetSpeed()
  75. {
  76. //client.StartSys().GetAwaiter();
  77. var aa = client.GetActualTcpSpeed().GetAwaiter();
  78. //robotData.RobotMode.Mode
  79. }
  80. /// <summary>
  81. /// 启动机器人
  82. /// </summary>
  83. public async void StartRobot()
  84. {
  85. if (robotData != null)
  86. {
  87. await client.StartSys();
  88. await client.Sync();
  89. MessageLog.GetInstance.Show("机器人启动成功");
  90. }
  91. }
  92. /// <summary>
  93. /// 获取抓手重量
  94. /// </summary>
  95. /// <returns></returns>
  96. public double GetClawWdight()
  97. {
  98. if (robotData != null)
  99. {
  100. return client.GetClawWeight().Result.Weight_;
  101. }
  102. return 0;
  103. }
  104. /// <summary>
  105. /// 获取信号量
  106. /// </summary>
  107. /// <param name="index"></param>
  108. /// <returns></returns>
  109. public SignalResult GetValueAsync(int index = 0)
  110. {
  111. if (robotData == null) return default(SignalResult);
  112. SignalValue signalValue = new SignalValue();
  113. signalValue.Index = index;
  114. return client?.GetSignal(signalValue).Result;
  115. }
  116. /// <summary>
  117. /// 设置信号量
  118. /// </summary>
  119. /// <param name="index"></param>
  120. /// <param name="value"></param>
  121. /// <returns></returns>
  122. public SignalResult SetValue(int value, int index = 0)
  123. {
  124. if (robotData == null) return default(SignalResult);
  125. SignalValue signalValue = new SignalValue();
  126. signalValue.Index = index;
  127. signalValue.Value = value;
  128. return client.SetSignal(signalValue).Result;
  129. }
  130. public bool GetInput(int pin=0)
  131. {
  132. if (client == null) return false;
  133. var res = client.GetDIO(new IOPin() { Pin=pin}).Result;
  134. if (res != null)
  135. {
  136. return res.Value == 1 ? true : false;
  137. }
  138. //int a = res.Value;
  139. return false;
  140. }
  141. /// <summary>
  142. /// 运行指定的场景
  143. /// </summary>
  144. /// <param name="id"></param>
  145. public void Scene(int id)
  146. {
  147. if (robotData == null || client == null) return;
  148. if (!client.GetIsCanRunTask().Result) return;
  149. client?.RunScene(id);
  150. //var r = client.RunSceneUntilDone(id).Result;
  151. //var res = client.GetTask(id).Result;
  152. //var res1 = GetValueAsync().Value;
  153. //while (res1!=id)
  154. //{
  155. // client?.RunScene(id);
  156. // Thread.Sleep(100);
  157. //}
  158. //SetValue(1000);
  159. }
  160. }
  161. }