|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- using BPASmartClient.Helper;
- using BPASmartClient.Message;
- using BPASmartClient.Model;
- using Lebai.SDK;
- using Lebai.SDK.Dtos;
- using Robotc;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using TaskStatus = Lebai.SDK.Dtos.TaskStatus;
-
- namespace BPASmartClient.LebaiRobot
- {
- public class LebaiHelper:Singleton<LebaiHelper>
- {
-
-
- private LebaiRobotClient client;
- public RobotData robotData;
- public bool IsIdle { get; set; } = false;
- public bool IsConnected { get { return null != robotData; } }
- public void Connect(string ip)
- {
- MessageLog.GetInstance.Show($"机器人IP地址:{ip}");
- bool ErrorFlag = false;
- while (robotData == null)
- {
- try
- {
- client = new LebaiRobotClient(ip);
- robotData = client.GetRobotData().Result;
- ActionManage.GetInstance.Send("乐白机器人数据");
- }
- catch (Exception ex)
- {
- if (!ErrorFlag)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- ErrorFlag = true;
- }
- Thread.Sleep(3000);
- }
- }
- StartRobot();
- Thread.Sleep(500);
- MessageLog.GetInstance.Show("乐百机器人连接成功!");
- }
- /// <summary>
- /// 机器人重连检测
- /// </summary>
- /// <param name="ip"></param>
- 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;
- }
- Thread.Sleep(10);
- }
- 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()
- {
- 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());
- }
-
- }
- public void GetSpeed()
- {
- //client.StartSys().GetAwaiter();
- var aa = client.GetActualTcpSpeed().GetAwaiter();
- //robotData.RobotMode.Mode
- }
-
- /// <summary>
- /// 启动机器人
- /// </summary>
- public async void StartRobot()
- {
- if (robotData != null)
- {
- await client.StartSys();
- await client.Sync();
- MessageLog.GetInstance.Show("机器人启动成功");
- }
- }
-
- /// <summary>
- /// 启动示教
- /// </summary>
- public async void StartTeachMode()
- {
- try
- {
- if (robotData != null)
- {
- await client.TeachMode();
- MessageLog.GetInstance.Show("机器人切换为示教模式.");
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- }
- }
-
- /// <summary>
- /// 结束示教
- /// </summary>
- public async void EndtTeachMode()
- {
- try
- {
- if (robotData != null)
- {
- await client.EndTeachMode();
- MessageLog.GetInstance.Show("机器人切换为停止示教模式.");
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- }
- }
- /// <summary>
- /// 暂停
- /// </summary>
- public async void pauseMode()
- {
- try
- {
- if (robotData != null)
- {
- await client.Pause();
- MessageLog.GetInstance.Show("机器人切换为暂停模式.");
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- }
- }
- /// <summary>
- /// 恢复
- /// </summary>
- public async void resumeMode()
- {
- try
- {
- if (robotData != null)
- {
- await client.Resume();
- MessageLog.GetInstance.Show("机器人切换为暂停后恢复模式.");
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- }
- }
-
- /// <summary>
- /// 机器人急停
- /// </summary>
- public async void EStopRobot()
- {
- try
- {
- if (robotData != null)
- {
- await client.EStop();
- MessageLog.GetInstance.Show("机器人急停");
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- }
- }
-
- /// <summary>
- /// 获取抓手重量
- /// </summary>
- /// <returns></returns>
- public double GetClawWdight()
- {
- if (robotData != null)
- {
- return client.GetClawWeight().Result.Weight_;
- }
- return 0;
- }
-
- /// <summary>
- /// 获取信号量
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- public SignalResult? GetValueAsync(int index = 0)
- {
- if (robotData == null) return default(SignalResult);
- SignalValue signalValue = new SignalValue();
- signalValue.Index = index;
- return client?.GetSignal(signalValue).Result;
- }
-
- /// <summary>
- /// 设置信号量
- /// </summary>
- /// <param name="index"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public SignalResult? SetValue(int value, int index = 0)
- {
- if (robotData == null) return default;
- SignalValue signalValue = new SignalValue();
- signalValue.Index = index;
- signalValue.Value = value;
- return client.SetSignal(signalValue).Result;
-
- }
-
- /// <summary>
- /// 获取输入DI状态
- /// </summary>
- /// <param name="pin"></param>
- /// <returns></returns>
- public bool GetInput(int pin = 0)
- {
- if (client == null) return false;
- var res = client.GetDIO(new IOPin() { Pin = pin }).Result;
- if (res != null)
- {
- return res.Value == 1 ? true : false;
- }
- return false;
- }
- /// <summary>
- /// 设置DO输出
- /// </summary>
- /// <param name="pin"></param>
- /// <returns></returns>
- 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 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;
- }
-
- /// <summary>
- /// 运行指定的场景
- /// </summary>
- /// <param name="id"></param>
- public async void Scene(int id)
- {
- 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 != 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}");
- }
-
- //public async void ExecuteLua()
- // {
- // try
- // {
- // string lua = ""
- // if (client == null) return;
- // await client?.ExecuteLua()
-
- // }
- // catch (Exception ex)
-
- // }
- }
- }
|