dispatch_plc.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. //
  2. // Created by huli on 2021/8/3.
  3. //
  4. #include "dispatch_plc.h"
  5. #include "../system/system_communication.h"
  6. Dispatch_plc::Dispatch_plc()
  7. {
  8. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_UNKNOW;
  9. m_plc_id = 0;
  10. mp_execute_thread = NULL;
  11. }
  12. Dispatch_plc::~Dispatch_plc()
  13. {
  14. dispatch_plc_uninit();
  15. }
  16. //调度plc 初始化
  17. Error_manager Dispatch_plc::dispatch_plc_init(int plc_id)
  18. {
  19. m_plc_id = plc_id;
  20. m_status_updata_time = std::chrono::system_clock::now();
  21. m_last_heartbeat = 0;
  22. // 线程默认开启
  23. m_execute_condition.reset(false, true, false);
  24. mp_execute_thread = new std::thread(&Dispatch_plc::execute_thread_fun, this);
  25. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY;
  26. return Error_code::SUCCESS;
  27. }
  28. //调度plc 反初始化
  29. Error_manager Dispatch_plc::dispatch_plc_uninit()
  30. {
  31. if (mp_execute_thread)
  32. {
  33. m_execute_condition.kill_all();
  34. }
  35. if (mp_execute_thread)
  36. {
  37. mp_execute_thread->join();
  38. delete mp_execute_thread;
  39. mp_execute_thread = NULL;
  40. }
  41. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_UNKNOW;
  42. return Error_code::SUCCESS;
  43. }
  44. //调度plc 执行请求
  45. Error_manager Dispatch_plc::execute_for_dispatch_request_msg(message::Dispatch_request_msg& dispatch_request_msg)
  46. {
  47. if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  48. {
  49. std::unique_lock<std::mutex> t_lock(m_lock);
  50. //设定超时时间, 默认比任务指令里面的时间少10秒,
  51. if ( dispatch_request_msg.base_info().has_timeout_ms() )
  52. {
  53. m_timeout_ms = dispatch_request_msg.base_info().timeout_ms() - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
  54. }
  55. else
  56. {
  57. m_timeout_ms = DISPATCH_PROCESS_TIMEOUT_MS - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
  58. }
  59. m_command_key = dispatch_request_msg.command_key();
  60. m_start_time = std::chrono::system_clock::now();
  61. m_dispatch_request_msg = dispatch_request_msg;
  62. if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  63. {
  64. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
  65. }
  66. else if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  67. {
  68. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
  69. }
  70. else
  71. {
  72. return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  73. " dispatch_request_msg.dispatch_motion_direction() PARAMRTER ERROR ");
  74. }
  75. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  76. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST;
  77. }
  78. else
  79. {
  80. return Error_manager(Error_code::DISPATCH_PLC_STATUS_ERROR, Error_level::MINOR_ERROR,
  81. " m_dispatch_plc_status error ");
  82. }
  83. return Error_code::SUCCESS;
  84. }
  85. Dispatch_plc::Dispatch_plc_status Dispatch_plc::get_dispatch_plc_status()
  86. {
  87. return m_dispatch_plc_status;
  88. }
  89. //执行外界任务的执行函数
  90. void Dispatch_plc::execute_thread_fun()
  91. {
  92. LOG(INFO) << " Dispatch_plc::execute_thread_fun() start " << this;
  93. Error_manager t_error;
  94. while (m_execute_condition.is_alive())
  95. {
  96. m_execute_condition.wait();
  97. if (m_execute_condition.is_alive())
  98. {
  99. std::this_thread::sleep_for(std::chrono::microseconds(1));
  100. std::this_thread::sleep_for(std::chrono::seconds(1));
  101. std::this_thread::yield();
  102. // std::unique_lock<std::mutex> t_lock(m_lock);
  103. std::cout << " huli test :::: " << " m_plc_id = " << m_plc_id << std::endl;
  104. std::cout << " huli test :::: " << " m_dispatch_plc_status = " << m_dispatch_plc_status << std::endl;
  105. switch ((Dispatch_plc_status) m_dispatch_plc_status)
  106. {
  107. case DISPATCH_PLC_READY:
  108. {
  109. if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW )
  110. {
  111. update_dispatch_plc_communication();
  112. }
  113. else
  114. {
  115. m_dispatch_plc_status = DISPATCH_PLC_REQUEST;
  116. }
  117. break;
  118. }
  119. case DISPATCH_PLC_REQUEST://给plc发送请求
  120. {
  121. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  122. {
  123. //超时直接报错
  124. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  125. " DISPATCH_PLC_TIME_OUT fun error ");
  126. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  127. }
  128. else
  129. {
  130. //封装 给plc的调度请求
  131. m_result = encapsulate_dispatch_request_to_plc();
  132. if ( m_result == Error_code::SUCCESS )
  133. {
  134. m_dispatch_plc_status = DISPATCH_PLC_WORKING;
  135. }
  136. else
  137. {
  138. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  139. }
  140. }
  141. break;
  142. }
  143. case DISPATCH_PLC_WORKING:
  144. {
  145. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  146. {
  147. //超时直接报错
  148. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  149. " DISPATCH_PLC_TIME_OUT fun error ");
  150. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  151. }
  152. else
  153. {
  154. update_dispatch_plc_communication();
  155. //检查plc答复反馈
  156. m_result = check_response_from_plc();
  157. if ( m_result == Error_code::SUCCESS )
  158. {
  159. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  160. }
  161. else if ( m_result != Error_code::NODATA )
  162. {
  163. m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  164. }
  165. //else 原地等待
  166. }
  167. break;
  168. }
  169. case DISPATCH_PLC_RESPONSE:
  170. {
  171. //发送调度答复信息给主控
  172. send_dispatch_response_to_main_control();
  173. //无论前面的流程是否正确,都要给主控答复, 反馈里面填充错误码即可.
  174. if ( m_result == Error_code::SUCCESS )
  175. {
  176. //流程正常结束, 恢复到待机状态.
  177. m_dispatch_plc_status = DISPATCH_PLC_READY;
  178. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  179. m_result.error_manager_clear_all();
  180. }
  181. else
  182. {
  183. //流程异常结束, 进入到故障状态.
  184. m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  185. }
  186. break;
  187. }
  188. case DISPATCH_PLC_FAULT:
  189. {
  190. //不在接受新的任务,但是保持基本的通信正常
  191. update_dispatch_plc_communication();
  192. break;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. //封装 给plc的调度请求
  199. Error_manager Dispatch_plc::encapsulate_dispatch_request_to_plc()
  200. {
  201. //把m_dispatch_request_msg的信息传给本地的数据缓存.
  202. std::unique_lock<std::mutex> t_lock(m_lock);
  203. m_request_key = m_dispatch_request_msg.command_key();
  204. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  205. {
  206. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
  207. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_INLET;
  208. }
  209. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  210. {
  211. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  212. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_OUTLET;
  213. }
  214. else
  215. {
  216. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_UNKNOWN;
  217. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN;
  218. }
  219. m_request_passageway_id = m_dispatch_request_msg.terminal_id()+1;//0~5改为1~6
  220. if ( m_dispatch_request_msg.locate_information().locate_correct() )
  221. {
  222. m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  223. m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  224. #ifdef MEASURE_TO_PLC_CORRECTION
  225. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.3;//80~100改为-10~+10
  226. #endif
  227. #ifndef MEASURE_TO_PLC_CORRECTION
  228. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  229. #endif
  230. m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  231. m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  232. m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  233. m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  234. m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  235. m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  236. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_x();
  237. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_y();
  238. }
  239. else
  240. {
  241. m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  242. m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  243. #ifdef MEASURE_TO_PLC_CORRECTION
  244. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.3;//80~100改为-10~+10
  245. #endif
  246. #ifndef MEASURE_TO_PLC_CORRECTION
  247. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  248. #endif
  249. m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  250. m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  251. m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  252. m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  253. m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  254. m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  255. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_x();
  256. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_y();
  257. }
  258. if ( m_dispatch_request_msg.parkspace_info_ex_size() ==1 )
  259. {
  260. m_request_parkingspace_index_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_index_id();
  261. m_request_parkingspace_unit_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_unit_id();
  262. m_request_parkingspace_floor_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_floor_id();
  263. m_request_parkingspace_room_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_room_id();
  264. //车位朝向, 小号朝前朝南, 大号朝后朝北
  265. m_request_parkingspace_direction = (Common_data::Parkingspace_direction)m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_direction();
  266. m_request_car_license = m_dispatch_request_msg.parkspace_info_ex(0).car_info().license();
  267. m_request_car_type = (Common_data::Car_type)m_dispatch_request_msg.parkspace_info_ex(0).car_type();
  268. }
  269. else
  270. {
  271. return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  272. " m_dispatch_request_msg.parkspace_info_ex_size() !=1 error ");
  273. }
  274. return Error_code::SUCCESS;
  275. }
  276. //更新plc通信
  277. Error_manager Dispatch_plc::update_dispatch_plc_communication()
  278. {
  279. std::unique_lock<std::mutex> t_lock(m_lock);
  280. std::unique_lock<std::mutex> t_lock1(Dispatch_communication::get_instance_references().m_data_lock);
  281. /*
  282. //请求消息, 调度->plc
  283. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  284. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  285. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  286. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  287. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  288. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  289. m_request_key = "123456789";
  290. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  291. static unsigned char index = 0;
  292. index++;
  293. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = index;
  294. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = 02;
  295. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = 03;
  296. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = 04;
  297. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = 05;
  298. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = 6;
  299. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = 7;
  300. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = 8;
  301. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = 11;
  302. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = 12;
  303. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = 13;
  304. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = 14;
  305. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = 15;
  306. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = 16;
  307. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = 17;
  308. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = 18;
  309. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = 19;
  310. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  311. int t_size2 = m_request_key.size()<=20 ? m_request_key.size() : 20 ;
  312. m_request_car_license = "ABCDEF";
  313. memcpy(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, m_request_car_license.c_str(), t_size2);
  314. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = 33;
  315. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = 44;
  316. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = 55;
  317. */
  318. //请求消息, 调度->plc
  319. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  320. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  321. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  322. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  323. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  324. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  325. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  326. tp_dispatch_request_from_manager_to_plc_for_data->m_request_status = 1;
  327. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = m_request_dispatch_motion_direction;
  328. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = m_request_passageway_id;
  329. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = m_request_passageway_direction;
  330. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = m_request_parkingspace_index_id;
  331. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = m_request_parkingspace_unit_id;
  332. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = m_request_parkingspace_floor_id;
  333. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = m_request_parkingspace_room_id;
  334. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = m_request_parkingspace_direction;
  335. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = m_request_car_center_x;
  336. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = m_request_car_center_y;
  337. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = m_request_car_angle;
  338. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = m_request_car_front_theta;
  339. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = m_request_car_length;
  340. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = m_request_car_width;
  341. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = m_request_car_height;
  342. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = m_request_car_wheel_base;
  343. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = m_request_car_wheel_width;
  344. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  345. int t_size2 = m_request_key.size()<=20 ? m_request_key.size() : 20 ;
  346. memcpy(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, m_request_car_license.c_str(), t_size2);
  347. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = m_request_car_type;
  348. tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_x = m_request_uniformed_car_x;
  349. tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_y = m_request_uniformed_car_y;
  350. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = m_request_anticollision_lidar_flag;
  351. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = m_request_car_wheel_base_exact_value;
  352. //答复消息, plc->调度
  353. Dispatch_communication::Dispatch_response_from_plc_to_manager * tp_dispatch_response_from_plc_to_manager =
  354. & Dispatch_communication::get_instance_references().m_dispatch_response_from_plc_to_manager;
  355. m_response_key = (char*) tp_dispatch_response_from_plc_to_manager->m_response_key;
  356. m_response_status = (Dispatch_plc::Response_status)tp_dispatch_response_from_plc_to_manager->m_response_status;
  357. m_response_dispatch_motion_direction = (Common_data::Dispatch_motion_direction)tp_dispatch_response_from_plc_to_manager->m_response_dispatch_motion_direction;
  358. m_response_passageway_id = tp_dispatch_response_from_plc_to_manager->m_response_passageway_id;
  359. m_response_passageway_direction = (Common_data::Passageway_direction)tp_dispatch_response_from_plc_to_manager->m_response_passageway_direction;
  360. m_response_parkingspace_index_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_index_id;
  361. m_response_parkingspace_unit_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_unit_id;
  362. m_response_parkingspace_floor_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_floor_id;
  363. m_response_parkingspace_room_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_room_id;
  364. m_response_parkingspace_direction = (Common_data::Parkingspace_direction)tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_direction;
  365. m_response_car_center_x = tp_dispatch_response_from_plc_to_manager->m_response_car_center_x;
  366. m_response_car_center_y = tp_dispatch_response_from_plc_to_manager->m_response_car_center_y;
  367. m_response_car_angle = tp_dispatch_response_from_plc_to_manager->m_response_car_angle;
  368. m_response_car_front_theta = tp_dispatch_response_from_plc_to_manager->m_response_car_front_theta;
  369. m_response_car_length = tp_dispatch_response_from_plc_to_manager->m_response_car_length;
  370. m_response_car_width = tp_dispatch_response_from_plc_to_manager->m_response_car_width;
  371. m_response_car_height = tp_dispatch_response_from_plc_to_manager->m_response_car_height;
  372. m_response_car_wheel_base = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base;
  373. m_response_car_wheel_width = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_width;
  374. m_response_car_license = (char*) tp_dispatch_response_from_plc_to_manager->m_response_car_license;
  375. m_response_car_type = (Common_data::Car_type) tp_dispatch_response_from_plc_to_manager->m_response_car_type;
  376. m_response_uniformed_car_x = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_x;
  377. m_response_uniformed_car_y = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_y;
  378. m_response_anticollision_lidar_flag = (Common_data::Anticollision_lidar_flag)tp_dispatch_response_from_plc_to_manager->m_response_anticollision_lidar_flag;
  379. m_response_car_wheel_base_exact_value = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base_exact_value;
  380. /*
  381. std::cout << " huli test :::: " << " ------------------------------------------------------ = " << std::endl;
  382. std::cout << " huli test :::: " << " m_response_key = " << m_response_key << std::endl;
  383. std::cout << " huli test :::: " << " m_response_status = " << (int)m_response_status << std::endl;
  384. std::cout << " huli test :::: " << " m_response_dispatch_motion_direction = " << (int)m_response_dispatch_motion_direction << std::endl;
  385. std::cout << " huli test :::: " << " m_response_passageway_id = " << m_response_passageway_id << std::endl;
  386. std::cout << " huli test :::: " << " m_response_passageway_direction = " << (int)m_response_passageway_direction << std::endl;
  387. std::cout << " huli test :::: " << " m_response_parkingspace_index_id = " << m_response_parkingspace_index_id << std::endl;
  388. std::cout << " huli test :::: " << " m_response_parkingspace_unit_id = " << m_response_parkingspace_unit_id << std::endl;
  389. std::cout << " huli test :::: " << " m_response_parkingspace_floor_id = " << m_response_parkingspace_floor_id << std::endl;
  390. std::cout << " huli test :::: " << " m_response_parkingspace_room_id = " << m_response_parkingspace_room_id << std::endl;
  391. std::cout << " huli test :::: " << " m_response_parkingspace_direction = " << (int)m_response_parkingspace_direction << std::endl;
  392. std::cout << " huli test :::: " << " m_response_car_center_x = " << m_response_car_center_x << std::endl;
  393. std::cout << " huli test :::: " << " m_response_car_center_y = " << m_response_car_center_y << std::endl;
  394. std::cout << " huli test :::: " << " m_response_car_license = " << m_response_car_license << std::endl;
  395. */
  396. //状态消息, plc->调度
  397. Dispatch_communication::Dispatch_plc_status_from_plc_to_manager * tp_dispatch_plc_status_from_plc_to_manager =
  398. & Dispatch_communication::get_instance_references().m_dispatch_plc_status_from_plc_to_manager;
  399. //need
  400. // //通过心跳帧来判断通信是否正常
  401. // if ( m_last_heartbeat != tp_dispatch_plc_status_from_plc_to_manager->m_heartbeat )
  402. // {
  403. // m_last_heartbeat = tp_dispatch_plc_status_from_plc_to_manager->m_heartbeat;
  404. // m_status_updata_time = std::chrono::system_clock::now();
  405. //
  406. // if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT )
  407. // {
  408. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY;
  409. // }
  410. // }
  411. // else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(COMMUNICATION_OVER_TIME_MS))
  412. // {
  413. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT;
  414. // }
  415. // //else 继续等待,直到消息刷新或者超时.
  416. return Error_code::SUCCESS;
  417. }
  418. //检查plc答复反馈
  419. Error_manager Dispatch_plc::check_response_from_plc()
  420. {
  421. std::unique_lock<std::mutex> t_lock(m_lock);
  422. #ifndef WAIT_PLC_RESPONSE
  423. //need, 调试代码, 延时10s, 直接返回成功
  424. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::seconds(5) )
  425. {
  426. return Error_code::SUCCESS;
  427. }
  428. //返回没有收到数据
  429. else
  430. {
  431. return Error_code::NODATA;
  432. }
  433. #endif
  434. #ifdef WAIT_PLC_RESPONSE
  435. //检查唯一码
  436. if ( m_response_key == m_request_key )
  437. {
  438. //如果故障,则添加错误码
  439. if ( m_response_status != RESPONS_WORKING )
  440. {
  441. //添加错误码
  442. Error_manager t_error(DISPATCH_PLC_RESPONS_ERROR, MINOR_ERROR, " Dispatch_plc::check_response_from_plc() m_respons_status is error");
  443. return t_error;
  444. }
  445. return Error_code::SUCCESS;
  446. }
  447. //返回没有收到数据
  448. else
  449. {
  450. return Error_code::NODATA;
  451. }
  452. #endif
  453. return Error_code::SUCCESS;
  454. }
  455. //发送调度答复信息给主控
  456. Error_manager Dispatch_plc::send_dispatch_response_to_main_control()
  457. {
  458. std::unique_lock<std::mutex> t_lock(m_lock);
  459. m_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  460. m_dispatch_response_msg.mutable_base_info()->set_timeout_ms(m_dispatch_request_msg.base_info().timeout_ms());
  461. m_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  462. m_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  463. m_dispatch_response_msg.set_command_key(m_dispatch_request_msg.command_key());
  464. m_dispatch_response_msg.set_dispatch_motion_direction(m_dispatch_request_msg.dispatch_motion_direction());
  465. m_dispatch_response_msg.set_terminal_id(m_dispatch_request_msg.terminal_id());
  466. m_dispatch_response_msg.mutable_locate_information()->CopyFrom(m_dispatch_request_msg.locate_information());
  467. m_dispatch_response_msg.mutable_parkspace_info_ex()->CopyFrom(m_dispatch_request_msg.parkspace_info_ex());
  468. m_dispatch_response_msg.set_car_type(m_dispatch_request_msg.car_type());
  469. if ( m_dispatch_response_msg.dispatch_motion_direction() == message::E_STORE_CAR )
  470. {
  471. for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  472. {
  473. if ( m_result == Error_code::SUCCESS )
  474. {
  475. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  476. }
  477. else
  478. {
  479. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  480. }
  481. }
  482. }
  483. else if( m_dispatch_response_msg.dispatch_motion_direction() == message::E_PICKUP_CAR )
  484. {
  485. for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  486. {
  487. if ( m_result == Error_code::SUCCESS )
  488. {
  489. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  490. }
  491. else
  492. {
  493. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  494. }
  495. }
  496. }
  497. m_dispatch_response_msg.mutable_error_manager()->set_error_code(m_result.get_error_code());
  498. m_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)m_result.get_error_level());
  499. m_dispatch_response_msg.mutable_error_manager()->set_error_description(m_result.get_error_description());
  500. std::cout << " huli test :::: " << " m_dispatch_response_msg = " << m_dispatch_response_msg.DebugString()<< std::endl;
  501. std::string t_msg = m_dispatch_response_msg.SerializeAsString();
  502. System_communication::get_instance_references().encapsulate_msg(t_msg);
  503. return Error_code::SUCCESS;
  504. }