carrier.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //
  2. // Created by huli on 2021/3/12.
  3. //
  4. #include "carrier.h"
  5. Carrier::Carrier()
  6. {
  7. m_request_x = 0; //搬运器坐标x轴,
  8. m_request_y = 0; //搬运器坐标y轴,
  9. m_request_z = 0; //搬运器坐标z轴,
  10. m_request_y1 = 0; //搬运器坐标y1轴, 搬运器抓车杆纵向移动(前轮抓杆)
  11. m_request_y2 = 0; //搬运器坐标y2轴, 搬运器抓车杆纵向移动(后轮抓杆)
  12. m_request_clamp_motion = E_CLAMP_NO_ACTION; //搬运器夹车杆. 0=无动作,1=夹紧,2=松开
  13. m_request_joint_motion_x = E_JOINT_NO_ACTION; //电梯与X轴的横向轨道对接,0=无动作,1=进行对接, 2=松开对接
  14. m_request_joint_motion_y = E_JOINT_NO_ACTION; //小跑车与Y轴的横向轨道对接,0=无动作,1=进行对接, 2=松开对接
  15. m_request_space_id = 0; //搬运器空间位置的id.
  16. m_request_floor_id = 0; //搬运器楼层位置的id.
  17. m_request_wheelbase = 0; //搬运器抓车杆前后轮距.
  18. m_respons_status = RESPONS_WORKING; //指令完成状态, 搬运器答复指令, 返回任务完成的情况
  19. m_respons_x = 0; //搬运器坐标x轴,
  20. m_respons_y = 0; //搬运器坐标y轴,
  21. m_respons_z = 0; //搬运器坐标z轴,
  22. m_respons_y1 = 0; //搬运器坐标y1轴, 搬运器抓车杆纵向移动(前轮抓杆)
  23. m_respons_y2 = 0; //搬运器坐标y2轴, 搬运器抓车杆纵向移动(后轮抓杆)
  24. m_respons_clamp_motion = E_CLAMP_NO_ACTION; //搬运器夹车杆. 0=无动作,1=夹紧,2=松开
  25. m_respons_joint_motion_x = E_JOINT_NO_ACTION; //电梯与X轴的横向轨道对接,0=无动作,1=进行对接, 2=松开对接
  26. m_respons_joint_motion_y = E_JOINT_NO_ACTION; //小跑车与Y轴的横向轨道对接,0=无动作,1=进行对接, 2=松开对接
  27. m_respons_space_id = 0; //搬运器空间位置的id.
  28. m_respons_floor_id = 0; //搬运器楼层位置的id.
  29. m_respons_wheelbase = 0; //搬运器抓车杆前后轮距.
  30. m_status_updata_time = std::chrono::system_clock::now();
  31. m_last_heartbeat = 0; //上一次的心跳
  32. m_actual_device_status = DEVICE_UNKNOWN; //搬运器的硬件设备状态
  33. m_actual_load_status = LOAD_UNKNOWN; //搬运器的负载状态, 小跑车上面是否有车.
  34. m_actual_x = 0; //搬运器坐标x轴,
  35. m_actual_y = 0; //搬运器坐标y轴,
  36. m_actual_z = 0; //搬运器坐标z轴,
  37. m_actual_y1 = 0; //搬运器坐标y1轴, 搬运器抓车杆纵向移动(前轮抓杆)
  38. m_actual_y2 = 0; //搬运器坐标y2轴, 搬运器抓车杆纵向移动(后轮抓杆)
  39. m_actual_clamp_motion1 = E_CLAMP_NO_ACTION; //小跑车夹车杆. 0=无动作,1=夹紧,2=松开
  40. m_actual_clamp_motion2 = E_CLAMP_NO_ACTION; //小跑车夹车杆. 0=无动作,1=夹紧,2=松开
  41. m_actual_small_sports_car_motion = E_SMALL_SPORTS_NO_ACTION;
  42. m_actual_joint_motion_x1 = E_JOINT_NO_ACTION; //电梯与X轴的横向轨道对接,0=无动作,1=进行对接, 2=松开对接
  43. m_actual_joint_motion_x2 = E_JOINT_NO_ACTION; //电梯与X轴的横向轨道对接,0=无动作,1=进行对接, 2=松开对接
  44. memset(m_actual_error_code, 0, 50); //搬运器设备的报警信息位
  45. memset(m_actual_warning_code, 0, 50); //升降机设备的报警信息位
  46. }
  47. Carrier::~Carrier()
  48. {
  49. }
  50. //检查任务类型, 子类必须重载, 用来检查输入的任务是否为子类所需的.
  51. Error_manager Carrier::check_task_type(std::shared_ptr<Task_Base> p_task)
  52. {
  53. //检查任务类型,
  54. if (p_task->get_task_type() != Task_Base::Task_type::CARRIER_TASK)
  55. {
  56. return Error_manager(Error_code::CARRIER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  57. "Carrier::check_task_type get_task_type() != CARRIER_TASK ");
  58. }
  59. return Error_code::SUCCESS;
  60. }
  61. //获取硬件设备的状态, 必须子类继承
  62. Carrier::Device_status Carrier::get_actual_device_status()
  63. {
  64. return m_actual_device_status;
  65. }
  66. //把任务单写入到内存中, 子类必须重载
  67. Error_manager Carrier::write_task_to_memory(std::shared_ptr<Task_Base> p_task)
  68. {
  69. //检查任务类型,
  70. if (p_task->get_task_type() != Task_Base::Task_type::CARRIER_TASK)
  71. {
  72. return Error_manager(Error_code::CARRIER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  73. "Carrier::check_task_type get_task_type() != CARRIER_TASK ");
  74. }
  75. else
  76. {
  77. std::unique_lock<std::mutex> t_lock(m_lock);
  78. Carrier_task* tp_carrier_task = (Carrier_task*)p_task.get();
  79. std::unique_lock<std::mutex> t_lock1(tp_carrier_task->m_lock);
  80. m_request_key = tp_carrier_task->m_request_key;
  81. m_request_x = tp_carrier_task->m_request_x;
  82. m_request_y = tp_carrier_task->m_request_y;
  83. m_request_z = tp_carrier_task->m_request_z;
  84. m_request_y1 = tp_carrier_task->m_request_y1;
  85. m_request_y2 = tp_carrier_task->m_request_y2;
  86. m_request_clamp_motion = (Dispatch_device_base::Clamp_motion)tp_carrier_task->m_request_clamp_motion;
  87. m_request_joint_motion_x = (Dispatch_device_base::Joint_motion)tp_carrier_task->m_request_joint_motion_x;
  88. m_request_joint_motion_y = (Dispatch_device_base::Joint_motion)tp_carrier_task->m_request_joint_motion_y;
  89. m_request_space_id = tp_carrier_task->m_request_space_id;
  90. m_request_floor_id = tp_carrier_task->m_request_floor_id;
  91. m_request_wheelbase = tp_carrier_task->m_request_wheelbase;
  92. return Error_code::SUCCESS;
  93. }
  94. return Error_code::SUCCESS;
  95. }
  96. //更新设备底层通信数据, 子类必须重载
  97. Error_manager Carrier::update_device_communication()
  98. {
  99. std::unique_lock<std::mutex> t_lock1(Dispatch_communication::get_instance_references().m_data_lock);
  100. std::unique_lock<std::mutex> t_lock(m_lock);
  101. //请求消息, 调度->plc
  102. Dispatch_communication::Carrier_request_from_dispatch_to_plc_for_data * tp_carrier_request_from_dispatch_to_plc_for_data =
  103. & Dispatch_communication::get_instance_references().m_carrier_request_from_dispatch_to_plc_for_data[m_device_id];
  104. Dispatch_communication::Carrier_request_from_dispatch_to_plc_for_key * tp_carrier_request_from_dispatch_to_plc_for_key =
  105. & Dispatch_communication::get_instance_references().m_carrier_request_from_dispatch_to_plc_for_key[m_device_id];
  106. memset(tp_carrier_request_from_dispatch_to_plc_for_key->m_request_key, 0, 50);
  107. int t_size = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  108. memcpy(tp_carrier_request_from_dispatch_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size);
  109. tp_carrier_request_from_dispatch_to_plc_for_data->m_request_x = m_request_x;
  110. tp_carrier_request_from_dispatch_to_plc_for_data->m_request_y = m_request_y;
  111. tp_carrier_request_from_dispatch_to_plc_for_data->m_request_z = m_request_z;
  112. tp_carrier_request_from_dispatch_to_plc_for_data->m_request_y1 = m_request_y1;
  113. tp_carrier_request_from_dispatch_to_plc_for_data->m_request_y2 = m_request_y2;
  114. tp_carrier_request_from_dispatch_to_plc_for_data->m_request_clamp_motion = m_request_clamp_motion;
  115. tp_carrier_request_from_dispatch_to_plc_for_data->m_request_joint_motion_x = m_request_joint_motion_x;
  116. tp_carrier_request_from_dispatch_to_plc_for_data->m_request_joint_motion_y = m_request_joint_motion_y;
  117. tp_carrier_request_from_dispatch_to_plc_for_data->m_request_space_id = m_request_space_id;
  118. tp_carrier_request_from_dispatch_to_plc_for_data->m_request_floor_id = m_request_floor_id;
  119. tp_carrier_request_from_dispatch_to_plc_for_data->m_request_wheelbase = m_request_wheelbase;
  120. //答复消息, plc->调度
  121. Dispatch_communication::Carrier_response_from_plc_to_dispatch * tp_carrier_response_from_plc_to_dispatch =
  122. & Dispatch_communication::get_instance_references().m_carrier_response_from_plc_to_dispatch[m_device_id];
  123. m_respons_key = (char*) tp_carrier_response_from_plc_to_dispatch->m_respons_key;
  124. m_respons_status = (Dispatch_device_base::Respons_status)tp_carrier_response_from_plc_to_dispatch->m_respons_status;
  125. m_respons_x = tp_carrier_response_from_plc_to_dispatch->m_respons_x;
  126. m_respons_y = tp_carrier_response_from_plc_to_dispatch->m_respons_y;
  127. m_respons_z = tp_carrier_response_from_plc_to_dispatch->m_respons_z;
  128. m_respons_y1 = tp_carrier_response_from_plc_to_dispatch->m_respons_y1;
  129. m_respons_y2 = tp_carrier_response_from_plc_to_dispatch->m_respons_y2;
  130. m_respons_clamp_motion = (Dispatch_device_base::Clamp_motion)tp_carrier_response_from_plc_to_dispatch->m_respons_clamp_motion;
  131. m_respons_joint_motion_x = (Dispatch_device_base::Joint_motion)tp_carrier_response_from_plc_to_dispatch->m_respons_joint_motion_x;
  132. m_respons_joint_motion_y = (Dispatch_device_base::Joint_motion)tp_carrier_response_from_plc_to_dispatch->m_respons_joint_motion_y;
  133. m_respons_space_id = tp_carrier_response_from_plc_to_dispatch->m_respons_space_id;
  134. m_respons_floor_id = tp_carrier_response_from_plc_to_dispatch->m_respons_floor_id;
  135. m_respons_wheelbase = tp_carrier_response_from_plc_to_dispatch->m_respons_wheelbase;
  136. //状态消息, plc->调度
  137. Dispatch_communication::Carrier_status_from_plc_to_dispatch *tp_carrier_status_from_plc_to_dispatch =
  138. & Dispatch_communication::get_instance_references().m_carrier_status_from_plc_to_dispatch[m_device_id];
  139. // int heartbeat = tp_carrier_status_from_plc_to_dispatch->m_heartbeat;
  140. // std::cout << " huli test :::: " << " heartbeat = " << heartbeat << std::endl;
  141. //通过心跳帧来判断通信是否正常
  142. if ( m_last_heartbeat != tp_carrier_status_from_plc_to_dispatch->m_heartbeat )
  143. {
  144. m_last_heartbeat = tp_carrier_status_from_plc_to_dispatch->m_heartbeat;
  145. m_status_updata_time = std::chrono::system_clock::now();
  146. // int safe_status = tp_carrier_status_from_plc_to_dispatch->m_safe_status;
  147. // std::cout << " huli test :::: " << " m_safe_status = " << safe_status << std::endl;
  148. // int work_status = tp_carrier_status_from_plc_to_dispatch->m_work_status;
  149. // std::cout << " huli test :::: " << " m_work_status = " << work_status << std::endl;
  150. //设备异常 //注注注注注注注注意了, ==的优先级比&要高.
  151. if ( (tp_carrier_status_from_plc_to_dispatch->m_safe_status & 0x02) == 0 )
  152. {
  153. m_actual_device_status = Dispatch_device_base::DEVICE_EMERGENCY_STOP;
  154. m_dispatch_device_status = Dispatch_device_base::E_FAULT;
  155. }
  156. else if ( (tp_carrier_status_from_plc_to_dispatch->m_safe_status & 0x01) == 0 )
  157. {
  158. m_actual_device_status = Dispatch_device_base::DEVICE_FAULT;
  159. m_dispatch_device_status = Dispatch_device_base::E_FAULT;
  160. }
  161. else if ( (tp_carrier_status_from_plc_to_dispatch->m_safe_status & 0x08) == 0 )
  162. {
  163. m_actual_device_status = Dispatch_device_base::DEVICE_COLLISION;
  164. m_dispatch_device_status = Dispatch_device_base::E_FAULT;
  165. }
  166. //正常状态
  167. else
  168. {
  169. if (tp_carrier_status_from_plc_to_dispatch->m_work_status == 1)
  170. {
  171. m_actual_device_status = Dispatch_device_base::DEVICE_WORKING;
  172. }
  173. else if(tp_carrier_status_from_plc_to_dispatch->m_work_status == 2)
  174. {
  175. m_actual_device_status = Dispatch_device_base::DEVICE_READY;
  176. }
  177. else if(tp_carrier_status_from_plc_to_dispatch->m_work_status == 0)
  178. {
  179. m_actual_device_status = Dispatch_device_base::DEVICE_UNKNOWN;
  180. }
  181. //故障恢复之后 E_FAULT ->> E_THREE_LEVEL_WORK
  182. if ( m_dispatch_device_status == Dispatch_device_base::E_FAULT )
  183. {
  184. m_dispatch_device_status = Dispatch_device_base::E_THREE_LEVEL_WORK;
  185. }
  186. //else 流程状态维持不变
  187. }
  188. m_actual_load_status = (Dispatch_device_base::Load_status)tp_carrier_status_from_plc_to_dispatch->m_actual_load_status;
  189. m_actual_x = tp_carrier_status_from_plc_to_dispatch->m_actual_x;
  190. m_actual_y = tp_carrier_status_from_plc_to_dispatch->m_actual_y;
  191. m_actual_z = tp_carrier_status_from_plc_to_dispatch->m_actual_z;
  192. m_actual_y1 = tp_carrier_status_from_plc_to_dispatch->m_actual_y1;
  193. m_actual_y2 = tp_carrier_status_from_plc_to_dispatch->m_actual_y2;
  194. m_actual_clamp_motion1 = (Dispatch_device_base::Clamp_motion)tp_carrier_status_from_plc_to_dispatch->m_actual_clamp_motion1;
  195. m_actual_clamp_motion2 = (Dispatch_device_base::Clamp_motion)tp_carrier_status_from_plc_to_dispatch->m_actual_clamp_motion2;
  196. m_actual_small_sports_car_motion = (Dispatch_device_base::Small_sports_car_motion)tp_carrier_status_from_plc_to_dispatch->m_actual_small_sports_car_motion;
  197. m_actual_joint_motion_x1 = (Dispatch_device_base::Joint_motion)tp_carrier_status_from_plc_to_dispatch->m_actual_joint_motion_x1;
  198. m_actual_joint_motion_x2 = (Dispatch_device_base::Joint_motion)tp_carrier_status_from_plc_to_dispatch->m_actual_joint_motion_x2;
  199. memcpy(m_actual_error_code, tp_carrier_status_from_plc_to_dispatch->m_actual_error_code, 50);
  200. memcpy(m_actual_warning_code, tp_carrier_status_from_plc_to_dispatch->m_actual_warning_code, 50);
  201. m_actual_error_description = (char*)(tp_carrier_status_from_plc_to_dispatch->m_actual_error_description-2);
  202. //重连之后,搬运器状态 E_DISCONNECT ->> E_THREE_LEVEL_WORK
  203. if ( m_dispatch_device_status == Dispatch_device_base::E_DISCONNECT )
  204. {
  205. m_dispatch_device_status = Dispatch_device_base::E_THREE_LEVEL_WORK;
  206. }
  207. }
  208. else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(COMMUNICATION_OVER_TIME_MS))
  209. {
  210. m_dispatch_device_status = Dispatch_device_base::E_DISCONNECT;
  211. }
  212. //else 继续等待,直到消息刷新或者超时.
  213. return Error_code::SUCCESS;
  214. }
  215. //从内存中读数据到任务单, 子类必须重载
  216. Error_manager Carrier::check_and_read_memory_to_task(std::shared_ptr<Task_Base> p_task)
  217. {
  218. std::cout << " huli test :::: " << " ((((((((((((((((((((((((((333333333333333333 = " << 333 << std::endl;
  219. std::cout << " huli test :::: " << " m_request_key = " << m_request_key << std::endl;
  220. std::cout << " huli test :::: " << " m_respons_key = " << m_respons_key << std::endl;
  221. std::cout << " huli test :::: " << " m_respons_status = " << m_respons_status << std::endl;
  222. std::cout << " huli test :::: " << " )))))))))))))))))))))))))))))))4444444444444 = " << 444 << std::endl;
  223. Dispatch_communication::get_instance_references().communication_start();
  224. //检查任务类型,
  225. if (p_task->get_task_type() != Task_Base::Task_type::CARRIER_TASK)
  226. {
  227. return Error_manager(Error_code::CARRIER_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  228. "Carrier::check_task_type get_task_type() != CARRIER_TASK ");
  229. }
  230. else
  231. {
  232. std::unique_lock<std::mutex> t_lock(m_lock);
  233. if ( m_respons_key == m_request_key && m_respons_status != RESPONS_WORKING )
  234. {
  235. Carrier_task* tp_carrier_task = (Carrier_task*)p_task.get();
  236. std::unique_lock<std::mutex> t_lock1(tp_carrier_task->m_lock);
  237. tp_carrier_task->m_respons_key = m_respons_key;
  238. tp_carrier_task->m_respons_status = (Carrier_task::Respons_status)m_respons_status;
  239. tp_carrier_task->m_respons_x = m_respons_x;
  240. tp_carrier_task->m_respons_y = m_respons_y;
  241. tp_carrier_task->m_respons_z = m_respons_z;
  242. tp_carrier_task->m_respons_y1 = m_respons_y1;
  243. tp_carrier_task->m_respons_y2 = m_respons_y2;
  244. tp_carrier_task->m_respons_clamp_motion = (Carrier_task::Clamp_motion)m_respons_clamp_motion;
  245. tp_carrier_task->m_respons_joint_motion_x = (Carrier_task::Joint_motion)m_respons_joint_motion_x;
  246. tp_carrier_task->m_respons_joint_motion_y = (Carrier_task::Joint_motion)m_respons_joint_motion_y;
  247. tp_carrier_task->m_respons_space_id = m_respons_space_id;
  248. tp_carrier_task->m_respons_floor_id = m_respons_floor_id;
  249. tp_carrier_task->m_respons_wheelbase = m_respons_wheelbase;
  250. //如果故障,则添加错误码
  251. if ( m_respons_status == RESPONS_MINOR_ERROR || m_respons_status == RESPONS_CRITICAL_ERROR )
  252. {
  253. //添加错误码
  254. Error_manager t_error(CARRIER_RESPONS_ERROR, MINOR_ERROR, "m_respons_status is error");
  255. tp_carrier_task->set_task_error_manager(t_error);
  256. }
  257. std::cout << " huli test :::: " << " {{{{{{{{{{{{{{{{{{{{{ 33333333333333333333333333333333333333333 }}}}}}}}}}}}}}}}}}}}}}}}}}= " << 333 << std::endl;
  258. std::cout << " huli test :::: " << " m_request_key = " << m_request_key << std::endl;
  259. std::cout << " huli test :::: " << " m_request_x = " << m_request_x << std::endl;
  260. std::cout << " huli test :::: " << " m_request_y = " << m_request_y << std::endl;
  261. std::cout << " huli test :::: " << " m_request_z = " << m_request_z << std::endl;
  262. std::cout << " huli test :::: " << " m_request_clamp_motion = " << m_request_clamp_motion << std::endl;
  263. std::cout << " huli test :::: " << " m_request_wheelbase = " << m_request_wheelbase << std::endl;
  264. std::cout << " huli test :::: " << " m_request_y1 = " << m_request_y1 << std::endl;
  265. std::cout << " huli test :::: " << " m_request_y2 = " << m_request_y2 << std::endl;
  266. std::cout << " huli test :::: " << " m_respons_key = " << m_respons_key << std::endl;
  267. std::cout << " huli test :::: " << " m_respons_status = " << m_respons_status << std::endl;
  268. std::cout << " huli test :::: " << " m_respons_x = " << m_respons_x << std::endl;
  269. std::cout << " huli test :::: " << " m_respons_y = " << m_respons_y << std::endl;
  270. std::cout << " huli test :::: " << " m_respons_z = " << m_respons_z << std::endl;
  271. std::cout << " huli test :::: " << " m_respons_clamp_motion = " << m_respons_clamp_motion << std::endl;
  272. std::cout << " huli test :::: " << " m_respons_wheelbase = " << m_respons_wheelbase << std::endl;
  273. std::cout << " huli test :::: " << " m_respons_y1 = " << m_respons_y1 << std::endl;
  274. std::cout << " huli test :::: " << " m_respons_y2 = " << m_respons_y2 << std::endl;
  275. std::cout << " huli test :::: " << " m_actual_x = " << m_actual_x << std::endl;
  276. std::cout << " huli test :::: " << " m_actual_y = " << m_actual_y << std::endl;
  277. std::cout << " huli test :::: " << " m_actual_z = " << m_actual_z << std::endl;
  278. std::cout << " huli test :::: " << " m_actual_y1 = " << m_actual_y1 << std::endl;
  279. std::cout << " huli test :::: " << " m_actual_y2 = " << m_actual_y2 << std::endl;
  280. std::cout << " huli test :::: " << " m_actual_clamp_motion1 = " << m_actual_clamp_motion1 << std::endl;
  281. std::cout << " huli test :::: " << " {{{{{{{{{{{{{{{{{{{{{{{{{{{{ 44444444444444444444444444444444444444444 }}}}}}}}}}}}}}}}}}}}}}}}= " << 444 << std::endl;
  282. return Error_code::SUCCESS;
  283. }
  284. //返回没有收到数据
  285. else
  286. {
  287. return Error_code::NODATA;
  288. }
  289. }
  290. return Error_code::SUCCESS;
  291. }
  292. //取消下发的指令
  293. Error_manager Carrier::cancel_command()
  294. {
  295. //以后再写 need programe
  296. //目前调度和plc的通信指令做的很简单,没有暂停和急停 复位等操作.
  297. //这里先空着,以后再写.
  298. //调度模块单方面销毁任务, 不管底层plc的执行情况, 也不去告知plc任务取消.
  299. return Error_code::SUCCESS;
  300. }