终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

381 linhas
12 KiB

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