终端一体化运控平台
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.
 
 
 

457 lines
16 KiB

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