终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

JaKaHelper.cs 12 KiB

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