dispatch_plc.cpp 28 KB

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