|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using HBLConsole.Service;
- using jkType;
- using Robotc;
- using HBLConsole.Model;
-
- namespace HBLConsole.Communication
- {
- public class JaKaHelper
- {
-
- #region 果汁机设备
- /*public const string SENCE_取咖啡杯 = "10000";
- public const string SENCE_取果汁杯 = "10001";
- public const string SENCE_取茶水杯 = "10002";
- public const string SENCE_取水杯 = "10003";
- public const string SENCE_取咖啡杯检测 = "11000";
- public const string SENCE_取果汁杯检测 = "11001";
- public const string SENCE_取茶水杯检测 = "11002";
- public const string SENCE_取水杯检测 = "11003";
-
- public const string SENCE_接咖啡 = "12000";
- public const string SENCE_接果汁1 = "12001";
- public const string SENCE_接果汁2 = "12002";
- public const string SENCE_接果汁3 = "12003";
- public const string SENCE_接果汁4 = "12004";
- public const string SENCE_接茶 = "12005";
- public const string SENCE_接水 = "12006";
-
- public const string SENCE_放咖啡杯 = "13000";
- public const string SENCE_放果汁杯1 = "13001";
- public const string SENCE_放果汁杯2 = "13002";
- public const string SENCE_放果汁杯3 = "13003";
- public const string SENCE_放果汁杯4 = "13004";
- public const string SENCE_放茶水杯 = "13005";
- public const string SENCE_放水杯 = "13006";*/
-
- public const string SENCE_取杯 = "10000";
- public const string SENCE_放杯位检测 = "10100";
- public const string SENCE_取杯检测 = "11000";
-
- public const string SENCE_接咖啡 = "12000";
- public const string SENCE_接果汁1 = "12001";
- public const string SENCE_接果汁2 = "12002";
- public const string SENCE_接果汁3 = "12003";
- public const string SENCE_接果汁4 = "12004";
- public const string SENCE_接茶 = "12005";
- public const string SENCE_接水 = "12006";
- public const string SENCE_接茶_接水 = "12007";
-
- public const string SENCE_放咖啡杯 = "13000";//接饮料位---放饮料过渡位
- public const string SENCE_放果汁杯1 = "13001";
- public const string SENCE_放果汁杯2 = "13002";
- public const string SENCE_放果汁杯3 = "13003";
- public const string SENCE_放果汁杯4 = "13004";
- public const string SENCE_放茶水杯 = "13005";
-
- public const string SENCE_放杯 = "14000";//放杯过渡位到-放杯
-
- public const string SENCE_放杯检测 = "15000";
-
- public const string SENCE_初始位 = "20000";
-
- #endregion
- public int rshd = -1;
- public bool login = false;
-
- /// <summary>
- /// 机器人的状态
- /// </summary>
- public JKTYPE.RobotStatus robot_Status;
- public JKTYPE.ProgramState program_status;
- private volatile static JaKaHelper _Instance;
- public static JaKaHelper GetInstance => _Instance ?? (_Instance = new JaKaHelper());
- private JaKaHelper() { }
- public bool IsIdle { get; set; } = false;
- public bool IsConnected { get { return login; } }
-
- public void Connect(string ip)
- {
- bool ErrorFlag = false;
- while (rshd == -1 || login == false)
- {
- try
- {
- jakaAPI.create_handler(ip.ToCharArray(), ref rshd);
-
- login = true;
-
- }
- catch (Exception ex)
- {
- if (!ErrorFlag)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- ErrorFlag = true;
- login = false;
- }
- Thread.Sleep(3000);
- }
- }
- if (login)
- {
- try
- {
- Power_On();//打开机器人电源
- Thread.Sleep(1000);
- Enable_robot();//机器人上使能
- MessageLog.GetInstance.Show("机器人已上电使能");
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx("机器人未完成上电和使能");
- }
- }
- }
- public void Power_On()
- {
- if (login)
- {
- try
- {
- jakaAPI.power_on(ref rshd);
- }
- catch (System.AccessViolationException ave)
- {
- MessageLog.GetInstance.ShowEx(ave.ToString());
- }
- //catch (Exception ex)
- //{
- // MessageLog.GetInstance.ShowEx(ex.ToString());
- //}
- }
- else
- {
- MessageLog.GetInstance.Show("jaka机器人未连接成功!");
- }
- }
-
- public void Power_Off()
- {
- if (login)
- {
- try
- {
- jakaAPI.power_off(ref rshd);
- }
- catch (Exception ex)
- {
-
- }
- }
- else
- {
- MessageLog.GetInstance.Show("jaka机器人未连接成功!");
- }
- }
-
- public void Enable_robot()
- {
- if (!(rshd == -1))
- {
- try
- {
- jakaAPI.enable_robot(ref rshd);
- }
- catch (Exception ex)
- {
-
- }
- }
- else
- {
- MessageLog.GetInstance.Show("jaka机器人未连接成功!");
- }
- }
-
- public void disEnable_robot()
- {
- if (!(rshd == -1))
- {
- try
- {
- jakaAPI.disable_robot(ref rshd);
- }
- catch (Exception ex)
- {
-
- }
- }
- else
- {
- MessageLog.GetInstance.Show("jaka机器人未连接成功!");
- }
- }
-
- /// <summary>
- /// 设置机器人的DO值
- /// </summary>
- /// <param name="DOIndex"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public bool Set_RobotDO(int DOIndex,bool value)
- {
- try
- {
- jakaAPI.set_digital_output(ref rshd, JKTYPE.IOType.IO_CABINET, DOIndex, value);
- return value;
-
- }
- catch (Exception ex)
- {
- return false;
- }
- }
- /// <summary>
- /// 设置机器人的AO值
- /// </summary>
- /// <param name="value">设置值</param>
- /// <returns></returns>
- public int Set_RobotAO1(int Value)
- {
- try
- {
- return jakaAPI.set_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, 0, Value);
-
- }
- catch (Exception ex)
- {
- return 0;
- }
- }
- /// <summary>
- /// 设置机器人的AO值
- /// </summary>
- /// <param name="Index">索引值</param>
- /// <param name="value">设置值</param>
- /// <returns></returns>
- public int Set_RobotAO(int Index, int Value)
- {
- try
- {
- return jakaAPI.set_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, Index, Value);
- }
- catch (Exception ex)
- {
- return 0;
- }
- }
-
- /// <summary>
- /// 获取机器人的AO值
- /// </summary>
- /// <param name="Index">索引值</param>
- /// <param name="value">设置值</param>
- /// <returns></returns>
- public float Get_RobotAO(int Index, int value)
- {
- try
- {
- float bResult = 0;
- jakaAPI.get_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, Index, ref bResult);
- return bResult;
- }
- catch (Exception ex)
- {
- return 0;
- }
- }
- /// <summary>
- /// 获取机器人的AO值
- /// </summary>
- /// <param name="Index">索引值</param>
- /// <param name="value">设置值</param>
- /// <returns></returns>
- public float Get_RobotAO1()
- {
- try
- {
- float bResult = 0;
- jakaAPI.get_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, 0, ref bResult);
- return bResult;
- }
- catch (Exception ex)
- {
- return 0;
- }
- }
-
- /// <summary>
- /// 获取机器人的DI输入
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- public bool Get_RobotDI(int index)
- {
- bool bResult = false;
- jakaAPI.get_digital_input(ref rshd, JKTYPE.IOType.IO_CABINET, index, ref bResult);
- return bResult;
- }
-
- /// <summary>
- /// 获取机器人的状态
- /// </summary>
- public void GetRobotStatus()
- {
- jakaAPI.get_robot_status(ref rshd,ref robot_Status);
- }
-
- public JKTYPE.ProgramState GetProgramStatus()
- {
- jakaAPI.get_program_state(ref rshd, ref program_status);
- return program_status;
-
- }
-
- StringBuilder jakafile;
- JKTYPE.ProgramState status;
- char[] file;
- public void JaKaProgramName(String Programname)
- {
- try
- {
- //加载
- file = Programname.ToCharArray();
- MessageLog.GetInstance.Show($"调用文件名:{Programname}");
- status = new JKTYPE.ProgramState();
- jakaAPI.get_program_state(ref rshd, ref status);
- if (status != JKTYPE.ProgramState.PROGRAM_IDLE)
- {
- MessageLog.GetInstance.Show($"程序运行中,无法加载程序!!!");
- }
- else
- {
- if (jakaAPI.program_load(ref rshd, file) == 0)
- {
- MessageLog.GetInstance.Show($"加载程序完成");
- }
- else
- {
- MessageLog.GetInstance.Show($"加载程序失败!!!");
- }
- }
- if (status == JKTYPE.ProgramState.PROGRAM_RUNNING)
- {
- MessageLog.GetInstance.Show($"程序处于已处于启动状态!!!");
- }
- else if (status == JKTYPE.ProgramState.PROGRAM_IDLE)
- {
- jakafile = new StringBuilder();
- jakaAPI.get_loaded_program(ref rshd, jakafile);
- if (jakafile.Length == 0)
- {
- MessageLog.GetInstance.Show($"未加载程序,无法运行!!!");
- }
- else
- {
- MessageLog.GetInstance.Show($"当前加载程序为:" + jakafile);
- jakaAPI.program_run(ref rshd);
- MessageLog.GetInstance.Show($"程序启动");
- }
- }
- else
- {
- MessageLog.GetInstance.Show($"程序处于暂停状态,无法重新启动程序!!!");
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- }
- }
- }
- }
|