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.
 
 

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