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.
 
 

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