catcher.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // Created by huli on 2021/3/2.
  3. //
  4. #include "catcher.h"
  5. Catcher::Catcher()
  6. {
  7. m_request_x = 0; //机器人坐标x轴,
  8. m_request_y = 0; //机器人坐标y轴,
  9. m_request_b = 0; //机器人坐标b轴, 旋转范围80~280
  10. m_request_z = 0; //机器人坐标z轴,
  11. m_request_space_id = 0; //机器人空间位置的id.
  12. m_request_d1 = 0; //机器人坐标d1轴, 机器人抓车杆纵向移动(前轮抓杆)
  13. m_request_d2 = 0; //机器人坐标d2轴, 机器人抓车杆纵向移动(后轮抓杆)
  14. m_request_wheelbase = 0; //机器人抓车杆前后轮距.
  15. m_request_clamp_motion = E_CLAMP_NO_ACTION; //机器人夹车杆. 0=无动作,1=夹紧,2=松开
  16. m_respons_status = RESPONS_WORKING; //指令完成状态, 机器人答复指令, 返回任务完成的情况
  17. m_respons_x = 0; //机器人坐标x轴,
  18. m_respons_y = 0; //机器人坐标y轴,
  19. m_respons_b = 0; //机器人坐标b轴, 旋转范围80~280
  20. m_respons_z = 0; //机器人坐标z轴,
  21. m_respons_space_id = 0; //机器人空间位置的id.
  22. m_respons_d1 = 0; //机器人坐标d1轴, 机器人抓车杆纵向移动(前轮抓杆)
  23. m_respons_d2 = 0; //机器人坐标d2轴, 机器人抓车杆纵向移动(后轮抓杆)
  24. m_respons_wheelbase = 0; //机器人抓车杆前后轮距.
  25. m_respons_clamp_motion = E_CLAMP_NO_ACTION; //机器人夹车杆. 0=无动作,1=夹紧,2=松开
  26. m_status_updata_time = std::chrono::system_clock::now();
  27. m_last_heartbeat = 0; //上一次的心跳
  28. m_actual_device_status = DEVICE_UNKNOWN; //机器人的硬件设备状态
  29. m_actual_load_status = LOAD_UNKNOWN; //机器人的负载状态, 小跑车上面是否有车.
  30. m_actual_x = 0; //机器人坐标x轴,
  31. m_actual_y = 0; //机器人坐标y轴,
  32. m_actual_b = 0; //机器人坐标b轴, 旋转范围80~280
  33. m_actual_z = 0; //机器人坐标z轴,
  34. m_actual_d1 = 0; //机器人坐标d1轴, 机器人抓车杆纵向移动(前轮抓杆)
  35. m_actual_d2 = 0; //机器人坐标d2轴, 机器人抓车杆纵向移动(后轮抓杆)
  36. m_actual_clamp_motion1 = E_CLAMP_NO_ACTION; //小跑车夹车杆. 0=无动作,1=夹紧,2=松开
  37. m_actual_clamp_motion2 = E_CLAMP_NO_ACTION; //小跑车夹车杆. 0=无动作,1=夹紧,2=松开
  38. m_actual_clamp_motion3 = E_CLAMP_NO_ACTION; //小跑车夹车杆. 0=无动作,1=夹紧,2=松开
  39. m_actual_clamp_motion4 = E_CLAMP_NO_ACTION; //小跑车夹车杆. 0=无动作,1=夹紧,2=松开
  40. memset(m_actual_error_code, 0, 50); //搬运器设备的报警信息位
  41. memset(m_actual_warning_code, 0, 50); //升降机设备的报警信息位
  42. }
  43. Catcher::~Catcher()
  44. {
  45. }
  46. //检查任务类型, 子类必须重载, 用来检查输入的任务是否为子类所需的.
  47. Error_manager Catcher::check_task_type(std::shared_ptr<Task_Base> p_task)
  48. {
  49. //检查任务类型,
  50. if (p_task->get_task_type() != CATCHER_TASK)
  51. {
  52. return Error_manager(Error_code::CATCHER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  53. "Catcher::check_task_type get_task_type() != CATCHER_TASK ");
  54. }
  55. return Error_code::SUCCESS;
  56. }
  57. //获取硬件设备的状态, 必须子类继承
  58. Catcher::Device_status Catcher::get_actual_device_status()
  59. {
  60. return m_actual_device_status;
  61. }
  62. //把任务单写入到内存中, 子类必须重载
  63. Error_manager Catcher::write_task_to_memory(std::shared_ptr<Task_Base> p_task)
  64. {
  65. //检查任务类型,
  66. if (p_task->get_task_type() != CATCHER_TASK)
  67. {
  68. return Error_manager(Error_code::CATCHER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  69. "Catcher::check_task_type get_task_type() != CATCHER_TASK ");
  70. }
  71. else
  72. {
  73. std::unique_lock<std::mutex> t_lock(m_lock);
  74. Catcher_task* tp_catcher_task = (Catcher_task*)p_task.get();
  75. std::unique_lock<std::mutex> t_lock1(tp_catcher_task->m_lock);
  76. m_request_key = tp_catcher_task->m_request_key;
  77. m_request_x = tp_catcher_task->m_request_x;
  78. m_request_y = tp_catcher_task->m_request_y;
  79. m_request_b = tp_catcher_task->m_request_b;
  80. m_request_z = tp_catcher_task->m_request_z;
  81. m_request_space_id = tp_catcher_task->m_request_space_id;
  82. m_request_d1 = tp_catcher_task->m_request_d1;
  83. m_request_d2 = tp_catcher_task->m_request_d2;
  84. m_request_wheelbase = tp_catcher_task->m_request_wheelbase;
  85. m_request_clamp_motion = (Dispatch_device_base::Clamp_motion)tp_catcher_task->m_request_clamp_motion;
  86. return Error_code::SUCCESS;
  87. }
  88. return Error_code::SUCCESS;
  89. }
  90. //更新设备底层通信数据, 子类必须重载
  91. Error_manager Catcher::update_device_communication()
  92. {
  93. std::unique_lock<std::mutex> t_lock1(Dispatch_communication::get_instance_references().m_data_lock);
  94. std::unique_lock<std::mutex> t_lock(m_lock);
  95. //请求消息, 调度->plc
  96. Dispatch_communication::Catcher_request_from_dispatch_to_plc_for_data * tp_catcher_request_from_dispatch_to_plc_for_data =
  97. & Dispatch_communication::get_instance_references().m_catcher_request_from_dispatch_to_plc_for_data[m_device_id];
  98. Dispatch_communication::Catcher_request_from_dispatch_to_plc_for_key * tp_catcher_request_from_dispatch_to_plc_for_key =
  99. & Dispatch_communication::get_instance_references().m_catcher_request_from_dispatch_to_plc_for_key[m_device_id];
  100. memset(tp_catcher_request_from_dispatch_to_plc_for_key->m_request_key, 0, 50);
  101. memcpy(tp_catcher_request_from_dispatch_to_plc_for_key->m_request_key, m_request_key.c_str(), m_request_key.size());
  102. tp_catcher_request_from_dispatch_to_plc_for_data->m_request_x = m_request_x;
  103. tp_catcher_request_from_dispatch_to_plc_for_data->m_request_y = m_request_y;
  104. tp_catcher_request_from_dispatch_to_plc_for_data->m_request_b = m_request_b;
  105. tp_catcher_request_from_dispatch_to_plc_for_data->m_request_z = m_request_z;
  106. tp_catcher_request_from_dispatch_to_plc_for_data->m_request_clamp_motion = m_request_clamp_motion;
  107. tp_catcher_request_from_dispatch_to_plc_for_data->m_request_wheelbase = m_request_wheelbase;
  108. tp_catcher_request_from_dispatch_to_plc_for_data->m_request_d1 = m_request_d1;
  109. tp_catcher_request_from_dispatch_to_plc_for_data->m_request_d2 = m_request_d2;
  110. //答复消息, plc->调度
  111. Dispatch_communication::Catcher_response_from_plc_to_dispatch * tp_catcher_response_from_plc_to_dispatch =
  112. & Dispatch_communication::get_instance_references().m_catcher_response_from_plc_to_dispatch[m_device_id];
  113. m_respons_key = (char*) tp_catcher_response_from_plc_to_dispatch->m_respons_key;
  114. m_respons_status = (Dispatch_device_base::Respons_status)tp_catcher_response_from_plc_to_dispatch->m_respons_status;
  115. m_respons_x = tp_catcher_response_from_plc_to_dispatch->m_respons_x;
  116. m_respons_y = tp_catcher_response_from_plc_to_dispatch->m_respons_y;
  117. m_respons_b = tp_catcher_response_from_plc_to_dispatch->m_respons_b;
  118. m_respons_z = tp_catcher_response_from_plc_to_dispatch->m_respons_z;
  119. m_respons_clamp_motion = (Dispatch_device_base::Clamp_motion)tp_catcher_response_from_plc_to_dispatch->m_respons_clamp_motion;
  120. m_respons_wheelbase = tp_catcher_response_from_plc_to_dispatch->m_respons_wheelbase;
  121. m_respons_d1 = tp_catcher_response_from_plc_to_dispatch->m_respons_d1;
  122. m_respons_d2 = tp_catcher_response_from_plc_to_dispatch->m_respons_d2;
  123. //状态消息, plc->调度
  124. Dispatch_communication::Catcher_status_from_plc_to_dispatch *tp_catcher_status_from_plc_to_dispatch =
  125. & Dispatch_communication::get_instance_references().m_catcher_status_from_plc_to_dispatch[m_device_id];
  126. //通过心跳帧来判断通信是否正常
  127. if ( m_last_heartbeat != tp_catcher_status_from_plc_to_dispatch->m_heartbeat )
  128. {
  129. m_last_heartbeat = tp_catcher_status_from_plc_to_dispatch->m_heartbeat;
  130. m_status_updata_time = std::chrono::system_clock::now();
  131. //设备异常 //注注注注注注注注意了, ==的优先级比&要高.
  132. if ( (tp_catcher_status_from_plc_to_dispatch->m_safe_status & 0x02) == 0 )
  133. {
  134. m_actual_device_status = Dispatch_device_base::DEVICE_EMERGENCY_STOP;
  135. m_dispatch_device_status = Dispatch_device_base::E_FAULT;
  136. }
  137. else if ( (tp_catcher_status_from_plc_to_dispatch->m_safe_status & 0x01) == 0 )
  138. {
  139. m_actual_device_status = Dispatch_device_base::DEVICE_FAULT;
  140. m_dispatch_device_status = Dispatch_device_base::E_FAULT;
  141. }
  142. else if ( (tp_catcher_status_from_plc_to_dispatch->m_safe_status & 0x08) == 0 )
  143. {
  144. m_actual_device_status = Dispatch_device_base::DEVICE_COLLISION;
  145. m_dispatch_device_status = Dispatch_device_base::E_FAULT;
  146. }
  147. //正常状态
  148. else
  149. {
  150. if (tp_catcher_status_from_plc_to_dispatch->m_work_status == 1)
  151. {
  152. m_actual_device_status = Dispatch_device_base::DEVICE_WORKING;
  153. }
  154. else if(tp_catcher_status_from_plc_to_dispatch->m_work_status == 2)
  155. {
  156. m_actual_device_status = Dispatch_device_base::DEVICE_READY;
  157. }
  158. else if(tp_catcher_status_from_plc_to_dispatch->m_work_status == 0)
  159. {
  160. m_actual_device_status = Dispatch_device_base::DEVICE_UNKNOWN;
  161. }
  162. //故障恢复之后 E_FAULT ->> E_THREE_LEVEL_WORK
  163. if ( m_dispatch_device_status == Dispatch_device_base::E_FAULT )
  164. {
  165. m_dispatch_device_status = Dispatch_device_base::E_THREE_LEVEL_WORK;
  166. }
  167. //else 流程状态维持不变
  168. }
  169. m_actual_load_status = (Dispatch_device_base::Load_status)tp_catcher_status_from_plc_to_dispatch->m_actual_load_status;
  170. m_actual_x = tp_catcher_status_from_plc_to_dispatch->m_actual_x;
  171. m_actual_y = tp_catcher_status_from_plc_to_dispatch->m_actual_y;
  172. m_actual_b = tp_catcher_status_from_plc_to_dispatch->m_actual_b;
  173. m_actual_z = tp_catcher_status_from_plc_to_dispatch->m_actual_z;
  174. m_actual_d1 = tp_catcher_status_from_plc_to_dispatch->m_actual_d1;
  175. m_actual_d2 = tp_catcher_status_from_plc_to_dispatch->m_actual_d2;
  176. m_actual_clamp_motion1 = (Dispatch_device_base::Clamp_motion)tp_catcher_status_from_plc_to_dispatch->m_actual_clamp_motion1;
  177. m_actual_clamp_motion2 = (Dispatch_device_base::Clamp_motion)tp_catcher_status_from_plc_to_dispatch->m_actual_clamp_motion2;
  178. m_actual_clamp_motion3 = (Dispatch_device_base::Clamp_motion)tp_catcher_status_from_plc_to_dispatch->m_actual_clamp_motion3;
  179. m_actual_clamp_motion4 = (Dispatch_device_base::Clamp_motion)tp_catcher_status_from_plc_to_dispatch->m_actual_clamp_motion4;
  180. memcpy(m_actual_error_code, tp_catcher_status_from_plc_to_dispatch->m_actual_error_code, 50);
  181. memcpy(m_actual_warning_code, tp_catcher_status_from_plc_to_dispatch->m_actual_warning_code, 50);
  182. m_actual_error_description = (char*)(tp_catcher_status_from_plc_to_dispatch->m_actual_error_description-2);
  183. //重连之后,搬运器状态 E_DISCONNECT ->> E_THREE_LEVEL_WORK
  184. if ( m_dispatch_device_status == Dispatch_device_base::E_DISCONNECT )
  185. {
  186. m_dispatch_device_status = Dispatch_device_base::E_THREE_LEVEL_WORK;
  187. }
  188. }
  189. else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(COMMUNICATION_OVER_TIME_MS))
  190. {
  191. m_dispatch_device_status = Dispatch_device_base::E_DISCONNECT;
  192. }
  193. //else 继续等待,直到消息刷新或者超时.
  194. return Error_code::SUCCESS;
  195. }
  196. //从内存中读数据到任务单, 子类必须重载
  197. Error_manager Catcher::check_and_read_memory_to_task(std::shared_ptr<Task_Base> p_task)
  198. {
  199. Dispatch_communication::get_instance_references().communication_start();
  200. //检查任务类型,
  201. if (p_task->get_task_type() != CATCHER_TASK)
  202. {
  203. return Error_manager(Error_code::CATCHER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  204. "Catcher::check_task_type get_task_type() != CATCHER_TASK ");
  205. }
  206. else
  207. {
  208. std::unique_lock<std::mutex> t_lock(m_lock);
  209. if ( m_respons_key == m_request_key && m_respons_status != RESPONS_WORKING )
  210. {
  211. Catcher_task* tp_catcher_task = (Catcher_task*)p_task.get();
  212. std::unique_lock<std::mutex> t_lock1(tp_catcher_task->m_lock);
  213. tp_catcher_task->m_respons_key = m_respons_key;
  214. tp_catcher_task->m_respons_status = (Catcher_task::Respons_status)m_respons_status;
  215. tp_catcher_task->m_respons_x = m_respons_x;
  216. tp_catcher_task->m_respons_y = m_respons_y;
  217. tp_catcher_task->m_respons_b = m_respons_b;
  218. tp_catcher_task->m_respons_z = m_respons_z;
  219. tp_catcher_task->m_respons_space_id = m_respons_space_id;
  220. tp_catcher_task->m_respons_d1 = m_respons_d1;
  221. tp_catcher_task->m_respons_d2 = m_respons_d2;
  222. tp_catcher_task->m_respons_wheelbase = m_respons_wheelbase;
  223. tp_catcher_task->m_respons_clamp_motion = (Catcher_task::Clamp_motion)m_respons_clamp_motion;
  224. //如果故障,则添加错误码
  225. if ( m_respons_status == RESPONS_MINOR_ERROR || m_respons_status == RESPONS_CRITICAL_ERROR )
  226. {
  227. //添加错误码
  228. Error_manager t_error(CATCHER_RESPONS_ERROR, MINOR_ERROR, "m_respons_status is error");
  229. tp_catcher_task->set_task_error_manager(t_error);
  230. }
  231. return Error_code::SUCCESS;
  232. }
  233. //返回没有收到数据
  234. else
  235. {
  236. return Error_code::NODATA;
  237. }
  238. }
  239. return Error_code::SUCCESS;
  240. }
  241. //取消下发的指令
  242. Error_manager Catcher::cancel_command()
  243. {
  244. //以后再写 need programe
  245. //目前调度和plc的通信指令做的很简单,没有暂停和急停 复位等操作.
  246. //这里先空着,以后再写.
  247. //调度模块单方面销毁任务, 不管底层plc的执行情况, 也不去告知plc任务取消.
  248. return Error_code::SUCCESS;
  249. }