passageway.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // Created by huli on 2021/3/16.
  3. //
  4. #include "passageway.h"
  5. Passageway::Passageway()
  6. {
  7. m_request_inside_door_motion = DOOR_UNKNOWN;
  8. m_request_outside_door_motion = DOOR_UNKNOWN;
  9. m_request_turntable_direction = TURNTABLE_DIRECTION_UNKNOWN;
  10. m_respons_status = RESPONS_WORKING; //指令完成状态, 搬运器答复指令, 返回任务完成的情况
  11. m_respons_inside_door_motion = DOOR_UNKNOWN;
  12. m_respons_outside_door_motion = DOOR_UNKNOWN;
  13. m_respons_turntable_direction = TURNTABLE_DIRECTION_UNKNOWN;
  14. m_status_updata_time = std::chrono::system_clock::now();
  15. m_last_heartbeat = 0; //上一次的心跳
  16. m_actual_device_status = HARDWARE_DEVICE_UNKNOWN; //通道口的硬件设备状态
  17. m_actual_inside_load_status = LOAD_UNKNOWN; //通道口的内部负载状态, 门内地感是否有车.
  18. m_actual_outside_load_status = LOAD_UNKNOWN; //通道口的外部负载状态, 门外地感是否有车.
  19. m_actual_front_overstep_the_boundary = BOUNDARY_NORMAL; //通道口 汽车前边界
  20. m_actual_back_overstep_the_boundary = BOUNDARY_NORMAL; //通道口 汽车前边界
  21. m_actual_height_overstep_the_boundary = BOUNDARY_NORMAL; //通道口 车辆是否超高
  22. m_actual_outside_door_sensor = LOAD_UNKNOWN; //通道口 的外门处的传感器, 判断是否有车经过外门
  23. //通道口的真实状态, 可能是路径中间的坐标
  24. m_actual_inside_door_motion = DOOR_UNKNOWN; //通道口 内门动作
  25. m_actual_outside_door_motion = DOOR_UNKNOWN; //通道口 外门动作
  26. m_actual_turntable_load_status = LOAD_UNKNOWN; //通道口 转盘负载状态, 是否有车.
  27. m_actual_turntable_direction = TURNTABLE_DIRECTION_UNKNOWN; //通道口 转台方向
  28. memset(m_actual_error_code, 0, 50); //搬运器设备的报警信息位
  29. memset(m_actual_warning_code, 0, 50); //升降机设备的报警信息位
  30. }
  31. Passageway::~Passageway()
  32. {
  33. }
  34. //检查任务类型, 子类必须重载, 用来检查输入的任务是否为子类所需的.
  35. Error_manager Passageway::check_task_type(std::shared_ptr<Task_Base> p_task)
  36. {
  37. //检查任务类型,
  38. if (p_task->get_task_type() != Task_Base::Task_type::PASSAGEWAY_TASK)
  39. {
  40. return Error_manager(Error_code::PASSAGEWAY_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  41. "Passageway::check_task_type get_task_type() != PASSAGEWAY_TASK ");
  42. }
  43. return Error_code::SUCCESS;
  44. }
  45. //获取硬件设备的状态, 必须子类继承
  46. Passageway::Hardware_device_status Passageway::get_actual_device_status()
  47. {
  48. return m_actual_device_status;
  49. }
  50. //把任务单写入到内存中, 子类必须重载
  51. Error_manager Passageway::write_task_to_memory(std::shared_ptr<Task_Base> p_task)
  52. {
  53. //检查任务类型,
  54. if (p_task->get_task_type() != Task_Base::Task_type::PASSAGEWAY_TASK)
  55. {
  56. return Error_manager(Error_code::PASSAGEWAY_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  57. "Passageway::check_task_type get_task_type() != PASSAGEWAY_TASK ");
  58. }
  59. else
  60. {
  61. std::unique_lock<std::mutex> t_lock(m_lock);
  62. Passageway_task* tp_passageway_task = (Passageway_task*)p_task.get();
  63. std::unique_lock<std::mutex> t_lock1(tp_passageway_task->m_lock);
  64. m_request_key = tp_passageway_task->m_request_key;
  65. m_request_inside_door_motion = (Dispatch_device_base::Door_motion)tp_passageway_task->m_request_inside_door_motion;
  66. m_request_outside_door_motion = (Dispatch_device_base::Door_motion)tp_passageway_task->m_request_outside_door_motion;
  67. m_request_turntable_direction = (Dispatch_device_base::Turntable_direction)tp_passageway_task->m_request_turntable_direction;
  68. return Error_code::SUCCESS;
  69. }
  70. return Error_code::SUCCESS;
  71. }
  72. //更新设备底层通信数据, 子类必须重载
  73. Error_manager Passageway::update_device_communication()
  74. {
  75. std::unique_lock<std::mutex> t_lock1(Dispatch_communication::get_instance_references().m_data_lock);
  76. std::unique_lock<std::mutex> t_lock(m_lock);
  77. //请求消息, 调度->plc
  78. Dispatch_communication::Passageway_request_from_dispatch_to_plc_for_data * tp_passageway_request_from_dispatch_to_plc_for_data =
  79. & Dispatch_communication::get_instance_references().m_passageway_request_from_dispatch_to_plc_for_data[m_device_id];
  80. Dispatch_communication::Passageway_request_from_dispatch_to_plc_for_key * tp_passageway_request_from_dispatch_to_plc_for_key =
  81. & Dispatch_communication::get_instance_references().m_passageway_request_from_dispatch_to_plc_for_key[m_device_id];
  82. memset(tp_passageway_request_from_dispatch_to_plc_for_key->m_request_key, 0, 50);
  83. int t_size = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  84. memcpy(tp_passageway_request_from_dispatch_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size);
  85. tp_passageway_request_from_dispatch_to_plc_for_data->m_request_inside_door_motion = m_request_inside_door_motion;
  86. tp_passageway_request_from_dispatch_to_plc_for_data->m_request_outside_door_motion = m_request_outside_door_motion;
  87. tp_passageway_request_from_dispatch_to_plc_for_data->m_request_turntable_direction = m_request_turntable_direction;
  88. //答复消息, plc->调度
  89. Dispatch_communication::Passageway_response_from_plc_to_dispatch * tp_passageway_response_from_plc_to_dispatch =
  90. & Dispatch_communication::get_instance_references().m_passageway_response_from_plc_to_dispatch[m_device_id];
  91. m_respons_key = (char*) tp_passageway_response_from_plc_to_dispatch->m_respons_key;
  92. m_respons_status = (Dispatch_device_base::Respons_status)tp_passageway_response_from_plc_to_dispatch->m_respons_status;
  93. m_respons_inside_door_motion = (Dispatch_device_base::Door_motion)tp_passageway_response_from_plc_to_dispatch->m_respons_inside_door_motion;
  94. m_respons_outside_door_motion = (Dispatch_device_base::Door_motion)tp_passageway_response_from_plc_to_dispatch->m_respons_outside_door_motion;
  95. m_respons_turntable_direction = (Dispatch_device_base::Turntable_direction)tp_passageway_response_from_plc_to_dispatch->m_respons_turntable_direction;
  96. //状态消息, plc->调度
  97. Dispatch_communication::Passageway_status_from_plc_to_dispatch *tp_passageway_status_from_plc_to_dispatch =
  98. & Dispatch_communication::get_instance_references().m_passageway_status_from_plc_to_dispatch[m_device_id];
  99. //设备异常 //注注注注注注注注意了, ==的优先级比&要高.
  100. if ( (tp_passageway_status_from_plc_to_dispatch->m_safe_status & 0x02) == 0 )
  101. {
  102. m_actual_device_status = Dispatch_device_base::HARDWARE_DEVICE_EMERGENCY_STOP;
  103. m_dispatch_device_status = Dispatch_device_base::DISPATCH_DEVICE_FAULT;
  104. }
  105. else if ( (tp_passageway_status_from_plc_to_dispatch->m_safe_status & 0x01) == 0 )
  106. {
  107. m_actual_device_status = Dispatch_device_base::HARDWARE_DEVICE_FAULT;
  108. m_dispatch_device_status = Dispatch_device_base::DISPATCH_DEVICE_FAULT;
  109. }
  110. else//正常状态
  111. {
  112. if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status2 & 0x20)== 1)
  113. {
  114. m_actual_device_status = Dispatch_device_base::HARDWARE_DEVICE_WORKING;
  115. }
  116. else if( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status2 & 0x20)== 0)
  117. {
  118. m_actual_device_status = Dispatch_device_base::HARDWARE_DEVICE_READY;
  119. }
  120. else
  121. {
  122. m_actual_device_status = Dispatch_device_base::HARDWARE_DEVICE_UNKNOWN;
  123. }
  124. //故障恢复之后 E_FAULT ->> E_THREE_LEVEL_WORK
  125. if ( m_dispatch_device_status == Dispatch_device_base::DISPATCH_DEVICE_FAULT )
  126. {
  127. m_dispatch_device_status = Dispatch_device_base::DISPATCH_DEVICE_THREE_LEVEL_WORK;
  128. }
  129. //else 流程状态维持不变
  130. }
  131. //数据解析
  132. if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x01)== 1)
  133. {
  134. m_actual_inside_load_status = Dispatch_device_base::HAVE_CAR;
  135. }
  136. else
  137. {
  138. m_actual_inside_load_status = Dispatch_device_base::NO_CAR;
  139. }
  140. if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x02)== 1)
  141. {
  142. m_actual_outside_load_status = Dispatch_device_base::HAVE_CAR;
  143. }
  144. else
  145. {
  146. m_actual_outside_load_status = Dispatch_device_base::NO_CAR;
  147. }
  148. if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x04)== 1)
  149. {
  150. m_actual_front_overstep_the_boundary = Dispatch_device_base::BOUNDARY_OVERSTEP;
  151. }
  152. else
  153. {
  154. m_actual_front_overstep_the_boundary = Dispatch_device_base::BOUNDARY_NORMAL;
  155. }
  156. if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x08)== 1)
  157. {
  158. m_actual_back_overstep_the_boundary = Dispatch_device_base::BOUNDARY_OVERSTEP;
  159. }
  160. else
  161. {
  162. m_actual_back_overstep_the_boundary = Dispatch_device_base::BOUNDARY_NORMAL;
  163. }
  164. if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x10)== 1)
  165. {
  166. m_actual_height_overstep_the_boundary = Dispatch_device_base::BOUNDARY_OVERSTEP;
  167. }
  168. else
  169. {
  170. m_actual_height_overstep_the_boundary = Dispatch_device_base::BOUNDARY_NORMAL;
  171. }
  172. if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x20)== 1)
  173. {
  174. m_actual_outside_door_sensor = Dispatch_device_base::HAVE_CAR;
  175. }
  176. else
  177. {
  178. m_actual_outside_door_sensor = Dispatch_device_base::NO_CAR;
  179. }
  180. if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x40)== 1 &&
  181. (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x80)== 0)
  182. {
  183. m_actual_inside_door_motion = Dispatch_device_base::DOOR_OPEN;
  184. }
  185. else if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x40)== 0 &&
  186. (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x80)== 1)
  187. {
  188. m_actual_inside_door_motion = Dispatch_device_base::DOOR_CLOSE;
  189. }
  190. else if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x40)== 1 &&
  191. (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x80)== 1)
  192. {
  193. m_actual_inside_door_motion = Dispatch_device_base::DOOR_ERROR;
  194. }
  195. else if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x40)== 0 &&
  196. (tp_passageway_status_from_plc_to_dispatch->m_sensor_status1 & 0x80)== 0)
  197. {
  198. m_actual_inside_door_motion = Dispatch_device_base::DOOR_UNKNOWN;
  199. }
  200. if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status2 & 0x01)== 1 &&
  201. (tp_passageway_status_from_plc_to_dispatch->m_sensor_status2 & 0x02)== 0)
  202. {
  203. m_actual_outside_door_motion = Dispatch_device_base::DOOR_OPEN;
  204. }
  205. else if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status2 & 0x01)== 0 &&
  206. (tp_passageway_status_from_plc_to_dispatch->m_sensor_status2 & 0x02)== 1)
  207. {
  208. m_actual_outside_door_motion = Dispatch_device_base::DOOR_CLOSE;
  209. }
  210. else if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status2 & 0x01)== 1 &&
  211. (tp_passageway_status_from_plc_to_dispatch->m_sensor_status2 & 0x02)== 1)
  212. {
  213. m_actual_outside_door_motion = Dispatch_device_base::DOOR_ERROR;
  214. }
  215. else if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status2 & 0x01)== 0 &&
  216. (tp_passageway_status_from_plc_to_dispatch->m_sensor_status2 & 0x02)== 0)
  217. {
  218. m_actual_outside_door_motion = Dispatch_device_base::DOOR_UNKNOWN;
  219. }
  220. if ( (tp_passageway_status_from_plc_to_dispatch->m_sensor_status2 & 0x03)== 1)
  221. {
  222. m_actual_turntable_load_status = Dispatch_device_base::HAVE_CAR;
  223. }
  224. else
  225. {
  226. m_actual_turntable_load_status = Dispatch_device_base::NO_CAR;
  227. }
  228. m_actual_turntable_direction = (Dispatch_device_base::Turntable_direction)tp_passageway_status_from_plc_to_dispatch->m_actual_turntable_direction;
  229. memcpy(m_actual_error_code, tp_passageway_status_from_plc_to_dispatch->m_actual_error_code, 50);
  230. memcpy(m_actual_warning_code, tp_passageway_status_from_plc_to_dispatch->m_actual_warning_code, 50);
  231. m_actual_error_description = (char*)(tp_passageway_status_from_plc_to_dispatch->m_actual_error_description-2);
  232. //通过心跳帧来判断通信是否正常
  233. if ( m_last_heartbeat != tp_passageway_status_from_plc_to_dispatch->m_heartbeat )
  234. {
  235. m_last_heartbeat = tp_passageway_status_from_plc_to_dispatch->m_heartbeat;
  236. m_status_updata_time = std::chrono::system_clock::now();
  237. //重连之后,搬运器状态 E_DISCONNECT ->> E_THREE_LEVEL_WORK
  238. if ( m_dispatch_device_status == Dispatch_device_base::DISPATCH_DEVICE_DISCONNECT )
  239. {
  240. m_dispatch_device_status = Dispatch_device_base::DISPATCH_DEVICE_THREE_LEVEL_WORK;
  241. }
  242. }
  243. else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(COMMUNICATION_OVER_TIME_MS))
  244. {
  245. m_dispatch_device_status = Dispatch_device_base::DISPATCH_DEVICE_DISCONNECT;
  246. }
  247. //else 继续等待,直到消息刷新或者超时.
  248. return Error_code::SUCCESS;
  249. }
  250. //从内存中读数据到任务单, 子类必须重载
  251. Error_manager Passageway::check_and_read_memory_to_task(std::shared_ptr<Task_Base> p_task)
  252. {
  253. Dispatch_communication::get_instance_references().communication_start();
  254. //检查任务类型,
  255. if (p_task->get_task_type() != Task_Base::Task_type::PASSAGEWAY_TASK)
  256. {
  257. return Error_manager(Error_code::PASSAGEWAY_TASK_TYPE_ERROR, Error_level::MINOR_ERROR,
  258. "Passageway::check_task_type get_task_type() != PASSAGEWAY_TASK ");
  259. }
  260. else
  261. {
  262. std::unique_lock<std::mutex> t_lock(m_lock);
  263. if ( m_respons_key == m_request_key && m_respons_status != RESPONS_WORKING )
  264. {
  265. Passageway_task* tp_passageway_task = (Passageway_task*)p_task.get();
  266. std::unique_lock<std::mutex> t_lock1(tp_passageway_task->m_lock);
  267. tp_passageway_task->m_respons_key = m_respons_key;
  268. tp_passageway_task->m_respons_status = (Passageway_task::Respons_status)m_respons_status;
  269. tp_passageway_task->m_respons_inside_door_motion = (Passageway_task::Door_motion)m_respons_inside_door_motion;
  270. tp_passageway_task->m_respons_outside_door_motion = (Passageway_task::Door_motion)m_respons_outside_door_motion;
  271. tp_passageway_task->m_respons_turntable_direction = (Passageway_task::Turntable_direction)m_respons_turntable_direction;
  272. //如果故障,则添加错误码
  273. if ( m_respons_status == RESPONS_MINOR_ERROR || m_respons_status == RESPONS_CRITICAL_ERROR )
  274. {
  275. //添加错误码
  276. Error_manager t_error(PASSAGEWAY_RESPONS_ERROR, MINOR_ERROR, "m_respons_status is error");
  277. tp_passageway_task->set_task_error_manager(t_error);
  278. }
  279. return Error_code::SUCCESS;
  280. }
  281. //返回没有收到数据
  282. else
  283. {
  284. return Error_code::NODATA;
  285. }
  286. }
  287. return Error_code::SUCCESS;
  288. }
  289. //取消下发的指令
  290. Error_manager Passageway::cancel_command()
  291. {
  292. //以后再写 need programe
  293. //目前调度和plc的通信指令做的很简单,没有暂停和急停 复位等操作.
  294. //这里先空着,以后再写.
  295. //调度模块单方面销毁任务, 不管底层plc的执行情况, 也不去告知plc任务取消.
  296. return Error_code::SUCCESS;
  297. }