|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Lebai.SDK;
- using Lebai.SDK.Dtos;
- using Robotc;
- using BPASmartClient.Message;
- using BPASmartClient.Helper;
- using TaskStatus = Lebai.SDK.Dtos.TaskStatus;
- using BPASmartClient.Peripheral;
- using BPASmartClient.EventBus;
- using static BPASmartClient.EventBus.EventBus;
- using BPASmartClient.Model;
-
- namespace BPASmartClient.Lebai
- {
- public class LebaiRobot: BasePeripheral
- {
-
- //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 且时且多设备
- //取咖啡杯 set:=1 get:=101 10031
- //取冰淇淋杯 set:=1 get:=101 10032
- //咖啡杯检测 set:=1 get:=101 10033
- //冰淇淋杯检测 set:=1 get:=101 10034
- //二次取咖啡杯 set:=1 get:=101 10035
- //二次取冰淇淋杯 set:=1 get:=101 10036
- //接咖啡 set:=1 get:=101 10037
- //接冰淇淋公共点 set:=1 get:=101 10038
- //接1号冰淇淋 set:=1 get:=101 10039
- //接2号冰淇淋 set:=1 get:=101 10040
- //接3号冰淇淋 set:=1 get:=101 10041
- //放咖啡位置 set:=1 get:=101 10042
- //放冰淇淋位置 set:=1 get:=101 10043
-
- 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;
- #endregion
-
- /// <summary>
- /// IP地址
- /// </summary>
- public string IpAddress { get; set; }
- /// <summary>
- /// 抓手上传感器输入位索引
- /// </summary>
- public int HandSensor { get; set; }
- /// <summary>
- /// 输出信号量索引
- /// </summary>
- public int OutputSingalValue { get; set; }
-
- 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 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>
- /// <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>
- /// 运行指定的场景
- /// </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 != 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 != TaskStatus.Running && first.Status != 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}");
- }
-
- protected override void InitStatus()
- {
- status["Connected"] = false;
- status["HandSensor"] = 0;
- status["OutputSingalValue"] = 0;
- }
-
- public override void Start()
- {
- Connect(IpAddress);
-
- }
-
- public override void Stop()
- {
- }
-
- public override void Init()
- {
- EventBus.EventBus.GetInstance().Subscribe<Demo_MakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack )
- {
-
- });
- InitStatus();
- }
- }
- }
|