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.
 
 

135 lines
3.9 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. private volatile static LebaiHelper _Instance;
  17. public static LebaiHelper GetInstance => _Instance ?? (_Instance = new LebaiHelper());
  18. private LebaiHelper() { }
  19. private LebaiRobotClient client;
  20. private RobotData robotData;
  21. public bool IsIdle { get; set; } = false;
  22. public bool IsConnected { get { return null != robotData; } }
  23. public void Connect(string ip)
  24. {
  25. bool ErrorFlag = false;
  26. while (robotData == null)
  27. {
  28. try
  29. {
  30. client = new LebaiRobotClient(ip);
  31. robotData = client.GetRobotData().Result;
  32. }
  33. catch (Exception ex)
  34. {
  35. if (!ErrorFlag)
  36. {
  37. MessageLog.GetInstance.Show(ex.ToString());
  38. ErrorFlag = true;
  39. }
  40. Thread.Sleep(3000);
  41. }
  42. }
  43. StartRobot();
  44. MessageLog.GetInstance.Show("乐百机器人连接成功!");
  45. }
  46. public void GetRobotModeStatus()
  47. {
  48. if (robotData == null) return;
  49. int mode = robotData.RobotMode.Mode;
  50. IsIdle = mode == 5;
  51. for (int i = 0; i < 14; i++)
  52. {
  53. if (RTrig.GetInstance(((ELebaiRModel)i).ToString()).Start(mode == i))
  54. MessageLog.GetInstance.Show(((ELebaiRModel)i).ToString());
  55. }
  56. }
  57. public void GetSpeed()
  58. {
  59. //client.StartSys().GetAwaiter();
  60. var aa = client.GetActualTcpSpeed().GetAwaiter();
  61. //robotData.RobotMode.Mode
  62. }
  63. /// <summary>
  64. /// 启动机器人
  65. /// </summary>
  66. public async void StartRobot()
  67. {
  68. if (robotData != null)
  69. {
  70. await client.StartSys();
  71. await client.Sync();
  72. MessageLog.GetInstance.Show("机器人启动成功");
  73. }
  74. }
  75. /// <summary>
  76. /// 获取抓手重量
  77. /// </summary>
  78. /// <returns></returns>
  79. public double GetClawWdight()
  80. {
  81. if (robotData != null)
  82. {
  83. return client.GetClawWeight().Result.Weight_;
  84. }
  85. return 0;
  86. }
  87. /// <summary>
  88. /// 获取信号量
  89. /// </summary>
  90. /// <param name="index"></param>
  91. /// <returns></returns>
  92. public SignalResult GetValueAsync(int index = 0)
  93. {
  94. if (robotData == null) return default(SignalResult);
  95. SignalValue signalValue = new SignalValue();
  96. signalValue.Index = index;
  97. return client?.GetSignal(signalValue).Result;
  98. }
  99. /// <summary>
  100. /// 设置信号量
  101. /// </summary>
  102. /// <param name="index"></param>
  103. /// <param name="value"></param>
  104. /// <returns></returns>
  105. public SignalResult SetValue(int value, int index = 0)
  106. {
  107. if (robotData == null) return default(SignalResult);
  108. SignalValue signalValue = new SignalValue();
  109. signalValue.Index = index;
  110. signalValue.Value = value;
  111. return client.SetSignal(signalValue).Result;
  112. }
  113. /// <summary>
  114. /// 运行指定的场景
  115. /// </summary>
  116. /// <param name="id"></param>
  117. public void Scene(int id)
  118. {
  119. if (robotData == null) return;
  120. client?.RunScene(id);
  121. }
  122. }
  123. }