using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using HBLConsole.Service; using Lebai.SDK; using Lebai.SDK.Dtos; using Robotc; using HBLConsole.Model; namespace HBLConsole.Communication { public class LebaiHelper { public const int SENCE_自嗨 = 10008; //public const int SENCE_接冰淇淋1 = 10004; //public const int SENCE_接冰淇淋2 = 10003; //public const int SENCE_接冰淇淋3 = 10005; //public const int SENCE_咖啡 = 10006; //#region 新增场景 //public const int SENCE_取咖啡杯 = 10013; //public const int SENCE_咖啡杯检测 = 10014; //public const int SENCE_再次取杯 = 10017; //public const int SENCE_接咖啡 = 10015; //public const int SENCE_送咖啡 = 10016; //无取走咖啡检测配合场景 //public const int SENCE_New送咖啡 = 10021;//有取走咖啡检测配合场景 //public const int SENCE_有无咖啡检测 = 10019; //public const int SENCE_再送咖啡 = 10020; //public const int SENCE_复位 = 10018; //#endregion //public const int SENCE_欢迎 = 10002; #region 且时且多设备 public const int SENCE_取咖啡杯 = 10031; public const int SENCE_取冰淇淋杯 = 10032; public const int SENCE_咖啡杯检测 = 10033; public const int SENCE_冰淇淋杯检测 = 10034; public const int SENCE_二次取咖啡杯 = 10035; public const int SENCE_二次取冰淇淋杯 = 10036; public const int SENCE_接咖啡 = 10037; public const int SENCE_接冰淇淋公共点 = 10038; public const int SENCE_接1号冰淇淋 = 10039; public const int SENCE_接2号冰淇淋 = 10040; public const int SENCE_接3号冰淇淋 = 10041; public const int SENCE_放咖啡位置 = 10042; public const int SENCE_放冰淇淋位置 = 10043; //add 新加场景 public const int SENCE_接咖啡_新 = 10051; public const int SENCE_咖啡杯回原点 = 10050; public const int SENCE_冰淇淋杯回原点 = 10049; public const int SENCE_取咖啡出餐 = 10052; #endregion #region 果汁机设备 MORKT1 JAKA机器人 public const int JUICE_初始位 = 20000; public const int JUICE_取杯 = 20001; public const int JUICE_取杯检测 = 20010; public const int JUICE_接咖啡 = 20020; public const int JUICE_接1号果汁 = 20030; public const int JUICE_接2号果汁 = 20040; public const int JUICE_接3号果汁 = 20050; public const int JUICE_接4号果汁 = 20060; public const int JUICE_接茶 = 20070; public const int JUICE_接茶水 = 20075; public const int JUICE_接水 = 20080; public const int JUICE_放咖啡杯 = 20090; public const int JUICE_放果汁杯 = 20100; public const int JUICE_放茶水杯 = 20110; public const int JUICE_放杯 = 20120; public const int JUICE_放杯检测 = 20120; #endregion #region 果汁机设备MORKT2 Lebai机器人 public const int JUICE2_初始位 = 10021; public const int JUICE2_取纸杯 = 10006; public const int JUICE2_二次取杯 = 10008; public const int JUICE2_纸杯检测 = 10007; public const int JUICE2_接咖啡 = 10009; public const int JUICE2_放咖啡杯 = 10010; public const int JUICE2_接茶叶 = 10017; public const int JUICE2_接茶水 = 10018; public const int JUICE2_接水 = 10020; public const int JUICE2_放水杯 = 10019; public const int JUICE2_接果汁公共位 = 10011; public const int JUICE2_接1号果汁 = 10012; public const int JUICE2_接2号果汁 = 10013; public const int JUICE2_接3号果汁 = 10014; public const int JUICE2_接4号果汁 = 10015; public const int JUICE2_放果汁杯 = 10016; #endregion private volatile static LebaiHelper _Instance; public static LebaiHelper GetInstance => _Instance ?? (_Instance = new LebaiHelper()); private LebaiHelper() { } private LebaiRobotClient client; private RobotData robotData; public bool IsIdle { get; set; } = false; public bool IsConnected { get { return null != robotData; } } public void Connect(string ip) { bool ErrorFlag = false; while (robotData == null) { try { client = new LebaiRobotClient(ip); robotData = client.GetRobotData().Result; } catch (Exception ex) { if (!ErrorFlag) { MessageLog.GetInstance.ShowEx(ex.ToString()); ErrorFlag = true; } Thread.Sleep(3000); } } StartRobot(); MessageLog.GetInstance.Show("乐百机器人连接成功!"); } /// /// 机器人重连检测 /// /// public void Reconnect(string ip) { if (client == null) { try { client = new LebaiRobotClient(ip); } catch (Exception) { // throw; } } check: while (robotData!=null) { try { robotData = client.GetRobotData().Result; } catch (Exception) { robotData = null; // throw; } } MessageLog.GetInstance.Show("乐白机器人断开连接,准备重连"); int num = 0; while (num < 6 && robotData == null) { try { robotData = client.GetRobotData().Result; } catch (Exception ex) { if (num == 5) { MessageLog.GetInstance.ShowEx(ex.ToString()); } } if (num < 5&& robotData==null) { Thread.Sleep(1000); MessageLog.GetInstance.Show($"乐白机器人正在尝试第{num+1}次重连...."); } else if(num>=5&&robotData==null) { MessageLog.GetInstance.Show("乐白机器人重连失败,请检查硬件连接"); //return; } num++; } if (robotData!=null) { StartRobot(); MessageLog.GetInstance.Show("乐白机器人重连成功"); } goto check; } /// /// 获取机器人模式状态 /// public void GetRobotModeStatus() { try { if (robotData == null) return; int mode = robotData.RobotMode.Mode; IsIdle = mode == 5; for (int i = 0; i < 14; i++) { if (RTrig.GetInstance(((ELebaiRModel)i).ToString()).Start(mode == i)) MessageLog.GetInstance.Show(((ELebaiRModel)i).ToString()); } } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } } /// /// 获取机器人速度因子 /// public void GetSpeed() { //client.StartSys().GetAwaiter(); var aa = client.GetActualTcpSpeed().GetAwaiter(); //robotData.RobotMode.Mode } /// /// 启动机器人 /// public async void StartRobot() { try { if (robotData != null) { await client.StartSys(); await client.Sync(); MessageLog.GetInstance.Show("机器人启动成功"); } } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } } /// /// 启动示教 /// public async void StartTeachMode() { try { if (robotData != null) { await client.TeachMode(); MessageLog.GetInstance.Show("机器人切换为示教模式."); } } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } } /// /// 结束示教 /// public async void EndtTeachMode() { try { if (robotData != null) { await client.EndTeachMode(); MessageLog.GetInstance.Show("机器人切换为停止示教模式."); } } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } } /// /// 机器人急停 /// public async void EStopRobot() { try { if (robotData != null) { await client.EStop(); MessageLog.GetInstance.Show("机器人急停"); } } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } } /// /// 获取抓手重量 /// /// public double GetClawWdight() { if (robotData != null) { return client.GetClawWeight().Result.Weight_; } return 0; } /// /// 获取信号量 /// /// /// public SignalResult GetValueAsync(int index = 0) { try { if (robotData == null) return default(SignalResult); SignalValue signalValue = new SignalValue(); signalValue.Index = index; return client?.GetSignal(signalValue).Result; } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } return default(SignalResult); } /// /// 设置信号量 /// /// /// /// public SignalResult SetValue(int value, int index = 0) { try { if (robotData == null) return default(SignalResult); SignalValue signalValue = new SignalValue(); signalValue.Index = index; signalValue.Value = value; return client.SetSignal(signalValue).Result; } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } return default(SignalResult); } /// /// 获取输入DI状态 /// /// /// public bool GetInput(int pin = 0) { try { if (client == null) return false; var res = client.GetDIO(new IOPin() { Pin = pin }).Result; if (res != null) { return res.Value == 1 ? true : false; } } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } return false; } public bool GetTcpInput(int pin =1) { try { if (client == null) return false; var res = client.GetTcpDIO(new IOPin() { Pin = pin }).Result; if (res != null) { return res.Value == 1 ? true : false; } } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } return false; } /// /// 设置DO输出 /// /// /// public void SetOutput(bool bDO, int pin = 0) { try { if (client == null) { return; } var res = client.SetDIO(new DIO() { Pin = pin , Value=bDO?1:0}); } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } } /// /// 运行指定的场景 /// /// public async void Scene(int id) { try { CancellationToken cancellationToken = default(CancellationToken); var result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken); var first = result?.Items?.FirstOrDefault(); var r = first == null || first.Status != Lebai.SDK.Dtos.TaskStatus.Running && first.Status != Lebai.SDK.Dtos.TaskStatus.Pause; while (!r) { Thread.Sleep(5); result = await client.GetTasks(new GetTasksInput { PageIndex = 1, PageSize = 1 }, cancellationToken); first = result?.Items?.FirstOrDefault(); r = first == null || first.Status != Lebai.SDK.Dtos.TaskStatus.Running && first.Status != Lebai.SDK.Dtos.TaskStatus.Pause; } while (GetValueAsync().Value != 0) { Thread.Sleep(500); } Thread.Sleep(1000); if (robotData == null || client == null) return; //if (!client.GetIsCanRunTask().Result) return; await client?.RunScene(id); MessageLog.GetInstance.Show($"调用场景:{id}"); } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } } } }