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.
 
 

380 lines
12 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 jkType;
  9. using Robotc;
  10. using HBLConsole.Model;
  11. namespace HBLConsole.Communication
  12. {
  13. public class JaKaHelper
  14. {
  15. #region 果汁机设备
  16. /*public const string SENCE_取咖啡杯 = "10000";
  17. public const string SENCE_取果汁杯 = "10001";
  18. public const string SENCE_取茶水杯 = "10002";
  19. public const string SENCE_取水杯 = "10003";
  20. public const string SENCE_取咖啡杯检测 = "11000";
  21. public const string SENCE_取果汁杯检测 = "11001";
  22. public const string SENCE_取茶水杯检测 = "11002";
  23. public const string SENCE_取水杯检测 = "11003";
  24. public const string SENCE_接咖啡 = "12000";
  25. public const string SENCE_接果汁1 = "12001";
  26. public const string SENCE_接果汁2 = "12002";
  27. public const string SENCE_接果汁3 = "12003";
  28. public const string SENCE_接果汁4 = "12004";
  29. public const string SENCE_接茶 = "12005";
  30. public const string SENCE_接水 = "12006";
  31. public const string SENCE_放咖啡杯 = "13000";
  32. public const string SENCE_放果汁杯1 = "13001";
  33. public const string SENCE_放果汁杯2 = "13002";
  34. public const string SENCE_放果汁杯3 = "13003";
  35. public const string SENCE_放果汁杯4 = "13004";
  36. public const string SENCE_放茶水杯 = "13005";
  37. public const string SENCE_放水杯 = "13006";*/
  38. public const string SENCE_取杯 = "10000";
  39. public const string SENCE_放杯位检测 = "10100";
  40. public const string SENCE_取杯检测 = "11000";
  41. public const string SENCE_接咖啡 = "12000";
  42. public const string SENCE_接果汁1 = "12001";
  43. public const string SENCE_接果汁2 = "12002";
  44. public const string SENCE_接果汁3 = "12003";
  45. public const string SENCE_接果汁4 = "12004";
  46. public const string SENCE_接茶 = "12005";
  47. public const string SENCE_接水 = "12006";
  48. public const string SENCE_接茶_接水 = "12007";
  49. public const string SENCE_放咖啡杯 = "13000";//接饮料位---放饮料过渡位
  50. public const string SENCE_放果汁杯1 = "13001";
  51. public const string SENCE_放果汁杯2 = "13002";
  52. public const string SENCE_放果汁杯3 = "13003";
  53. public const string SENCE_放果汁杯4 = "13004";
  54. public const string SENCE_放茶水杯 = "13005";
  55. public const string SENCE_放杯 = "14000";//放杯过渡位到-放杯
  56. public const string SENCE_放杯检测 = "15000";
  57. public const string SENCE_初始位 = "20000";
  58. #endregion
  59. public int rshd = -1;
  60. public bool login = false;
  61. /// <summary>
  62. /// 机器人的状态
  63. /// </summary>
  64. public JKTYPE.RobotStatus robot_Status;
  65. public JKTYPE.ProgramState program_status;
  66. private volatile static JaKaHelper _Instance;
  67. public static JaKaHelper GetInstance => _Instance ?? (_Instance = new JaKaHelper());
  68. private JaKaHelper() { }
  69. public bool IsIdle { get; set; } = false;
  70. public bool IsConnected { get { return login; } }
  71. public void Connect(string ip)
  72. {
  73. bool ErrorFlag = false;
  74. while (rshd == -1 || login == false)
  75. {
  76. try
  77. {
  78. jakaAPI.create_handler(ip.ToCharArray(), ref rshd);
  79. login = true;
  80. }
  81. catch (Exception ex)
  82. {
  83. if (!ErrorFlag)
  84. {
  85. MessageLog.GetInstance.ShowEx(ex.ToString());
  86. ErrorFlag = true;
  87. login = false;
  88. }
  89. Thread.Sleep(3000);
  90. }
  91. }
  92. if (login)
  93. {
  94. try
  95. {
  96. Power_On();//打开机器人电源
  97. Thread.Sleep(1000);
  98. Enable_robot();//机器人上使能
  99. MessageLog.GetInstance.Show("机器人已上电使能");
  100. }
  101. catch (Exception ex)
  102. {
  103. MessageLog.GetInstance.ShowEx("机器人未完成上电和使能");
  104. }
  105. }
  106. }
  107. public void Power_On()
  108. {
  109. if (login)
  110. {
  111. try
  112. {
  113. jakaAPI.power_on(ref rshd);
  114. }
  115. catch (System.AccessViolationException ave)
  116. {
  117. MessageLog.GetInstance.ShowEx(ave.ToString());
  118. }
  119. //catch (Exception ex)
  120. //{
  121. // MessageLog.GetInstance.ShowEx(ex.ToString());
  122. //}
  123. }
  124. else
  125. {
  126. MessageLog.GetInstance.Show("jaka机器人未连接成功!");
  127. }
  128. }
  129. public void Power_Off()
  130. {
  131. if (login)
  132. {
  133. try
  134. {
  135. jakaAPI.power_off(ref rshd);
  136. }
  137. catch (Exception ex)
  138. {
  139. }
  140. }
  141. else
  142. {
  143. MessageLog.GetInstance.Show("jaka机器人未连接成功!");
  144. }
  145. }
  146. public void Enable_robot()
  147. {
  148. if (!(rshd == -1))
  149. {
  150. try
  151. {
  152. jakaAPI.enable_robot(ref rshd);
  153. }
  154. catch (Exception ex)
  155. {
  156. }
  157. }
  158. else
  159. {
  160. MessageLog.GetInstance.Show("jaka机器人未连接成功!");
  161. }
  162. }
  163. public void disEnable_robot()
  164. {
  165. if (!(rshd == -1))
  166. {
  167. try
  168. {
  169. jakaAPI.disable_robot(ref rshd);
  170. }
  171. catch (Exception ex)
  172. {
  173. }
  174. }
  175. else
  176. {
  177. MessageLog.GetInstance.Show("jaka机器人未连接成功!");
  178. }
  179. }
  180. /// <summary>
  181. /// 设置机器人的DO值
  182. /// </summary>
  183. /// <param name="DOIndex"></param>
  184. /// <param name="value"></param>
  185. /// <returns></returns>
  186. public bool Set_RobotDO(int DOIndex,bool value)
  187. {
  188. try
  189. {
  190. jakaAPI.set_digital_output(ref rshd, JKTYPE.IOType.IO_CABINET, DOIndex, value);
  191. return value;
  192. }
  193. catch (Exception ex)
  194. {
  195. return false;
  196. }
  197. }
  198. /// <summary>
  199. /// 设置机器人的AO值
  200. /// </summary>
  201. /// <param name="value">设置值</param>
  202. /// <returns></returns>
  203. public int Set_RobotAO1(int Value)
  204. {
  205. try
  206. {
  207. return jakaAPI.set_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, 0, Value);
  208. }
  209. catch (Exception ex)
  210. {
  211. return 0;
  212. }
  213. }
  214. /// <summary>
  215. /// 设置机器人的AO值
  216. /// </summary>
  217. /// <param name="Index">索引值</param>
  218. /// <param name="value">设置值</param>
  219. /// <returns></returns>
  220. public int Set_RobotAO(int Index, int Value)
  221. {
  222. try
  223. {
  224. return jakaAPI.set_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, Index, Value);
  225. }
  226. catch (Exception ex)
  227. {
  228. return 0;
  229. }
  230. }
  231. /// <summary>
  232. /// 获取机器人的AO值
  233. /// </summary>
  234. /// <param name="Index">索引值</param>
  235. /// <param name="value">设置值</param>
  236. /// <returns></returns>
  237. public float Get_RobotAO(int Index, int value)
  238. {
  239. try
  240. {
  241. float bResult = 0;
  242. jakaAPI.get_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, Index, ref bResult);
  243. return bResult;
  244. }
  245. catch (Exception ex)
  246. {
  247. return 0;
  248. }
  249. }
  250. /// <summary>
  251. /// 获取机器人的AO值
  252. /// </summary>
  253. /// <param name="Index">索引值</param>
  254. /// <param name="value">设置值</param>
  255. /// <returns></returns>
  256. public float Get_RobotAO1()
  257. {
  258. try
  259. {
  260. float bResult = 0;
  261. jakaAPI.get_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, 0, ref bResult);
  262. return bResult;
  263. }
  264. catch (Exception ex)
  265. {
  266. return 0;
  267. }
  268. }
  269. /// <summary>
  270. /// 获取机器人的DI输入
  271. /// </summary>
  272. /// <param name="index"></param>
  273. /// <returns></returns>
  274. public bool Get_RobotDI(int index)
  275. {
  276. bool bResult = false;
  277. jakaAPI.get_digital_input(ref rshd, JKTYPE.IOType.IO_CABINET, index, ref bResult);
  278. return bResult;
  279. }
  280. /// <summary>
  281. /// 获取机器人的状态
  282. /// </summary>
  283. public void GetRobotStatus()
  284. {
  285. jakaAPI.get_robot_status(ref rshd,ref robot_Status);
  286. }
  287. public JKTYPE.ProgramState GetProgramStatus()
  288. {
  289. jakaAPI.get_program_state(ref rshd, ref program_status);
  290. return program_status;
  291. }
  292. StringBuilder jakafile;
  293. JKTYPE.ProgramState status;
  294. char[] file;
  295. public void JaKaProgramName(String Programname)
  296. {
  297. try
  298. {
  299. //加载
  300. file = Programname.ToCharArray();
  301. MessageLog.GetInstance.Show($"调用文件名:{Programname}");
  302. status = new JKTYPE.ProgramState();
  303. jakaAPI.get_program_state(ref rshd, ref status);
  304. if (status != JKTYPE.ProgramState.PROGRAM_IDLE)
  305. {
  306. MessageLog.GetInstance.Show($"程序运行中,无法加载程序!!!");
  307. }
  308. else
  309. {
  310. if (jakaAPI.program_load(ref rshd, file) == 0)
  311. {
  312. MessageLog.GetInstance.Show($"加载程序完成");
  313. }
  314. else
  315. {
  316. MessageLog.GetInstance.Show($"加载程序失败!!!");
  317. }
  318. }
  319. if (status == JKTYPE.ProgramState.PROGRAM_RUNNING)
  320. {
  321. MessageLog.GetInstance.Show($"程序处于已处于启动状态!!!");
  322. }
  323. else if (status == JKTYPE.ProgramState.PROGRAM_IDLE)
  324. {
  325. jakafile = new StringBuilder();
  326. jakaAPI.get_loaded_program(ref rshd, jakafile);
  327. if (jakafile.Length == 0)
  328. {
  329. MessageLog.GetInstance.Show($"未加载程序,无法运行!!!");
  330. }
  331. else
  332. {
  333. MessageLog.GetInstance.Show($"当前加载程序为:" + jakafile);
  334. jakaAPI.program_run(ref rshd);
  335. MessageLog.GetInstance.Show($"程序启动");
  336. }
  337. }
  338. else
  339. {
  340. MessageLog.GetInstance.Show($"程序处于暂停状态,无法重新启动程序!!!");
  341. }
  342. }
  343. catch (Exception ex)
  344. {
  345. MessageLog.GetInstance.ShowEx(ex.ToString());
  346. }
  347. }
  348. }
  349. }