dispatch_plc.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. m_outlet_id = 0;
  150. //封装 给plc的调度请求
  151. m_result = encapsulate_dispatch_request_to_plc(0);
  152. if ( m_result == Error_code::SUCCESS )
  153. {
  154. m_dispatch_plc_status = DISPATCH_PLC_WORKING;
  155. }
  156. else
  157. {
  158. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  159. }
  160. }
  161. else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
  162. {
  163. m_outlet_id = 1;
  164. //封装 给plc的调度请求
  165. m_result = encapsulate_dispatch_request_to_plc(1);
  166. if ( m_result == Error_code::SUCCESS )
  167. {
  168. m_dispatch_plc_status = DISPATCH_PLC_WORKING;
  169. }
  170. else
  171. {
  172. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  173. }
  174. }
  175. //else无限等待
  176. }
  177. else
  178. {
  179. m_result = Error_manager(Error_code::DISPATCH_PLC_DIRECTION_ERROR, Error_level::MINOR_ERROR,
  180. " DISPATCH_PLC_DIRECTION_ERROR fun error ");
  181. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  182. }
  183. }
  184. break;
  185. }
  186. case DISPATCH_PLC_WORKING:
  187. {
  188. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  189. {
  190. //超时直接报错
  191. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  192. " DISPATCH_PLC_TIME_OUT fun error ");
  193. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  194. }
  195. else
  196. {
  197. update_dispatch_plc_communication();
  198. //检查plc答复反馈
  199. m_result = check_response_from_plc();
  200. if ( m_result == Error_code::SUCCESS )
  201. {
  202. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  203. }
  204. else if ( m_result != Error_code::NODATA )
  205. {
  206. m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  207. }
  208. //else 原地等待
  209. }
  210. break;
  211. }
  212. case DISPATCH_PLC_RESPONSE:
  213. {
  214. //发送调度答复信息给主控
  215. send_dispatch_response_to_main_control();
  216. //无论前面的流程是否正确,都要给主控答复, 反馈里面填充错误码即可.
  217. if ( m_result == Error_code::SUCCESS )
  218. {
  219. //流程正常结束, 恢复到待机状态.
  220. m_dispatch_plc_status = DISPATCH_PLC_READY;
  221. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  222. m_result.error_manager_clear_all();
  223. }
  224. else
  225. {
  226. //流程异常结束, 进入到故障状态.
  227. m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  228. }
  229. break;
  230. }
  231. case DISPATCH_PLC_FAULT:
  232. {
  233. //不在接受新的任务,但是保持基本的通信正常
  234. update_dispatch_plc_communication();
  235. break;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. //封装 给plc的调度请求
  242. Error_manager Dispatch_plc::encapsulate_dispatch_request_to_plc(int outlet_id)
  243. {
  244. //把m_dispatch_request_msg的信息传给本地的数据缓存.
  245. std::unique_lock<std::mutex> t_lock(m_lock);
  246. m_request_key = m_dispatch_request_msg.command_key();
  247. m_request_status = 1;
  248. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  249. {
  250. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
  251. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_INLET;
  252. m_request_passageway_id = m_dispatch_request_msg.mutable_id_struct()->terminal_id()+1;//0~5入口终端号改为1~6
  253. }
  254. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  255. {
  256. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  257. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_OUTLET;
  258. m_request_passageway_id = m_dispatch_request_msg.mutable_id_struct()->unit_id()*2+outlet_id+1;//0~2出口单元号改为1~6
  259. }
  260. else
  261. {
  262. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_UNKNOWN;
  263. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN;
  264. m_request_passageway_id = -1;
  265. }
  266. if ( m_dispatch_request_msg.locate_information().locate_correct() )
  267. {
  268. m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  269. m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  270. #ifdef MEASURE_TO_PLC_CORRECTION
  271. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.3;//80~100改为-10~+10
  272. #endif
  273. #ifndef MEASURE_TO_PLC_CORRECTION
  274. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  275. #endif
  276. m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  277. m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  278. m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  279. m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  280. m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  281. m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  282. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_x();
  283. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_y();
  284. }
  285. else
  286. {
  287. m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  288. m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  289. #ifdef MEASURE_TO_PLC_CORRECTION
  290. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.3;//80~100改为-10~+10
  291. #endif
  292. #ifndef MEASURE_TO_PLC_CORRECTION
  293. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  294. #endif
  295. m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  296. m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  297. m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  298. m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  299. m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  300. m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  301. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_x();
  302. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_y();
  303. }
  304. if ( m_dispatch_request_msg.parkspace_info_ex_size() ==1 )
  305. {
  306. m_request_parkingspace_index_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_index_id();
  307. m_request_parkingspace_unit_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_unit_id();
  308. m_request_parkingspace_floor_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_floor_id();
  309. m_request_parkingspace_room_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_room_id();
  310. //车位朝向, 小号朝前朝南, 大号朝后朝北
  311. m_request_parkingspace_direction = (Common_data::Parkingspace_direction)m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_direction();
  312. m_request_car_license = m_dispatch_request_msg.parkspace_info_ex(0).car_info().license();
  313. m_request_car_type = (Common_data::Car_type)m_dispatch_request_msg.parkspace_info_ex(0).car_type();
  314. }
  315. else
  316. {
  317. return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  318. " m_dispatch_request_msg.parkspace_info_ex_size() !=1 error ");
  319. }
  320. return Error_code::SUCCESS;
  321. }
  322. //更新plc通信
  323. Error_manager Dispatch_plc::update_dispatch_plc_communication()
  324. {
  325. std::unique_lock<std::mutex> t_lock(m_lock);
  326. std::unique_lock<std::mutex> t_lock1(Dispatch_communication::get_instance_references().m_data_lock);
  327. /*
  328. //请求消息, 调度->plc
  329. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  330. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  331. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  332. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  333. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  334. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  335. m_request_key = "123456789";
  336. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  337. static unsigned char index = 0;
  338. index++;
  339. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = index;
  340. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = 02;
  341. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = 03;
  342. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = 04;
  343. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = 05;
  344. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = 6;
  345. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = 7;
  346. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = 8;
  347. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = 11;
  348. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = 12;
  349. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = 13;
  350. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = 14;
  351. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = 15;
  352. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = 16;
  353. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = 17;
  354. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = 18;
  355. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = 19;
  356. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  357. int t_size2 = m_request_key.size()<=20 ? m_request_key.size() : 20 ;
  358. m_request_car_license = "ABCDEF";
  359. memcpy(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, m_request_car_license.c_str(), t_size2);
  360. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = 33;
  361. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = 44;
  362. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = 55;
  363. */
  364. //请求消息, 调度->plc
  365. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  366. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  367. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  368. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  369. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  370. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  371. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  372. tp_dispatch_request_from_manager_to_plc_for_data->m_request_status = m_request_status;
  373. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = m_request_dispatch_motion_direction;
  374. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = m_request_passageway_id;
  375. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = m_request_passageway_direction;
  376. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = m_request_parkingspace_index_id;
  377. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = m_request_parkingspace_unit_id;
  378. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = m_request_parkingspace_floor_id;
  379. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = m_request_parkingspace_room_id;
  380. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = m_request_parkingspace_direction;
  381. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = m_request_car_center_x;
  382. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = m_request_car_center_y;
  383. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = m_request_car_angle;
  384. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = m_request_car_front_theta;
  385. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = m_request_car_length;
  386. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = m_request_car_width;
  387. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = m_request_car_height;
  388. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = m_request_car_wheel_base;
  389. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = m_request_car_wheel_width;
  390. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  391. int t_size2 = m_request_key.size()<=20 ? m_request_key.size() : 20 ;
  392. memcpy(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, m_request_car_license.c_str(), t_size2);
  393. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = m_request_car_type;
  394. tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_x = m_request_uniformed_car_x;
  395. tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_y = m_request_uniformed_car_y;
  396. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = m_request_anticollision_lidar_flag;
  397. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = m_request_car_wheel_base_exact_value;
  398. //答复消息, plc->调度
  399. Dispatch_communication::Dispatch_response_from_plc_to_manager * tp_dispatch_response_from_plc_to_manager =
  400. & Dispatch_communication::get_instance_references().m_dispatch_response_from_plc_to_manager;
  401. m_response_key = (char*) tp_dispatch_response_from_plc_to_manager->m_response_key;
  402. m_response_status = (Dispatch_plc::Response_status)tp_dispatch_response_from_plc_to_manager->m_response_status;
  403. m_response_dispatch_motion_direction = (Common_data::Dispatch_motion_direction)tp_dispatch_response_from_plc_to_manager->m_response_dispatch_motion_direction;
  404. m_response_passageway_id = tp_dispatch_response_from_plc_to_manager->m_response_passageway_id;
  405. m_response_passageway_direction = (Common_data::Passageway_direction)tp_dispatch_response_from_plc_to_manager->m_response_passageway_direction;
  406. m_response_parkingspace_index_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_index_id;
  407. m_response_parkingspace_unit_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_unit_id;
  408. m_response_parkingspace_floor_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_floor_id;
  409. m_response_parkingspace_room_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_room_id;
  410. m_response_parkingspace_direction = (Common_data::Parkingspace_direction)tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_direction;
  411. m_response_car_center_x = tp_dispatch_response_from_plc_to_manager->m_response_car_center_x;
  412. m_response_car_center_y = tp_dispatch_response_from_plc_to_manager->m_response_car_center_y;
  413. m_response_car_angle = tp_dispatch_response_from_plc_to_manager->m_response_car_angle;
  414. m_response_car_front_theta = tp_dispatch_response_from_plc_to_manager->m_response_car_front_theta;
  415. m_response_car_length = tp_dispatch_response_from_plc_to_manager->m_response_car_length;
  416. m_response_car_width = tp_dispatch_response_from_plc_to_manager->m_response_car_width;
  417. m_response_car_height = tp_dispatch_response_from_plc_to_manager->m_response_car_height;
  418. m_response_car_wheel_base = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base;
  419. m_response_car_wheel_width = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_width;
  420. m_response_car_license = (char*) tp_dispatch_response_from_plc_to_manager->m_response_car_license;
  421. m_response_car_type = (Common_data::Car_type) tp_dispatch_response_from_plc_to_manager->m_response_car_type;
  422. m_response_uniformed_car_x = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_x;
  423. m_response_uniformed_car_y = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_y;
  424. m_response_anticollision_lidar_flag = (Common_data::Anticollision_lidar_flag)tp_dispatch_response_from_plc_to_manager->m_response_anticollision_lidar_flag;
  425. m_response_car_wheel_base_exact_value = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base_exact_value;
  426. /*
  427. std::cout << " huli test :::: " << " ------------------------------------------------------ = " << std::endl;
  428. std::cout << " huli test :::: " << " m_response_key = " << m_response_key << std::endl;
  429. std::cout << " huli test :::: " << " m_response_status = " << (int)m_response_status << std::endl;
  430. std::cout << " huli test :::: " << " m_response_dispatch_motion_direction = " << (int)m_response_dispatch_motion_direction << std::endl;
  431. std::cout << " huli test :::: " << " m_response_passageway_id = " << m_response_passageway_id << std::endl;
  432. std::cout << " huli test :::: " << " m_response_passageway_direction = " << (int)m_response_passageway_direction << std::endl;
  433. std::cout << " huli test :::: " << " m_response_parkingspace_index_id = " << m_response_parkingspace_index_id << std::endl;
  434. std::cout << " huli test :::: " << " m_response_parkingspace_unit_id = " << m_response_parkingspace_unit_id << std::endl;
  435. std::cout << " huli test :::: " << " m_response_parkingspace_floor_id = " << m_response_parkingspace_floor_id << std::endl;
  436. std::cout << " huli test :::: " << " m_response_parkingspace_room_id = " << m_response_parkingspace_room_id << std::endl;
  437. std::cout << " huli test :::: " << " m_response_parkingspace_direction = " << (int)m_response_parkingspace_direction << std::endl;
  438. std::cout << " huli test :::: " << " m_response_car_center_x = " << m_response_car_center_x << std::endl;
  439. std::cout << " huli test :::: " << " m_response_car_center_y = " << m_response_car_center_y << std::endl;
  440. std::cout << " huli test :::: " << " m_response_car_license = " << m_response_car_license << std::endl;
  441. */
  442. //状态消息, plc->调度
  443. Dispatch_communication::Dispatch_plc_status_from_plc_to_manager * tp_dispatch_plc_status_from_plc_to_manager =
  444. & Dispatch_communication::get_instance_references().m_dispatch_plc_status_from_plc_to_manager;
  445. //need
  446. // //通过心跳帧来判断通信是否正常
  447. // if ( m_last_heartbeat != tp_dispatch_plc_status_from_plc_to_manager->m_heartbeat )
  448. // {
  449. // m_last_heartbeat = tp_dispatch_plc_status_from_plc_to_manager->m_heartbeat;
  450. // m_status_updata_time = std::chrono::system_clock::now();
  451. //
  452. // if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT )
  453. // {
  454. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY;
  455. // }
  456. // }
  457. // else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(COMMUNICATION_OVER_TIME_MS))
  458. // {
  459. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT;
  460. // }
  461. // //else 继续等待,直到消息刷新或者超时.
  462. return Error_code::SUCCESS;
  463. }
  464. //检查plc答复反馈
  465. Error_manager Dispatch_plc::check_response_from_plc()
  466. {
  467. std::unique_lock<std::mutex> t_lock(m_lock);
  468. #ifndef WAIT_PLC_RESPONSE
  469. //need, 调试代码, 延时10s, 直接返回成功
  470. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::seconds(5) )
  471. {
  472. return Error_code::SUCCESS;
  473. }
  474. //返回没有收到数据
  475. else
  476. {
  477. return Error_code::NODATA;
  478. }
  479. #endif
  480. #ifdef WAIT_PLC_RESPONSE
  481. //检查唯一码
  482. if ( m_response_key == m_request_key )
  483. {
  484. //如果故障,则添加错误码
  485. if ( m_response_status != RESPONS_WORKING )
  486. {
  487. //添加错误码
  488. Error_manager t_error(DISPATCH_PLC_RESPONS_ERROR, MINOR_ERROR, " Dispatch_plc::check_response_from_plc() m_respons_status is error");
  489. return t_error;
  490. }
  491. return Error_code::SUCCESS;
  492. }
  493. //返回没有收到数据
  494. else
  495. {
  496. return Error_code::NODATA;
  497. }
  498. #endif
  499. return Error_code::SUCCESS;
  500. }
  501. //发送调度答复信息给主控
  502. Error_manager Dispatch_plc::send_dispatch_response_to_main_control()
  503. {
  504. std::unique_lock<std::mutex> t_lock(m_lock);
  505. m_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  506. m_dispatch_response_msg.mutable_base_info()->set_timeout_ms(m_dispatch_request_msg.base_info().timeout_ms());
  507. m_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  508. m_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  509. m_dispatch_response_msg.set_command_key(m_dispatch_request_msg.command_key());
  510. m_dispatch_response_msg.set_dispatch_motion_direction(m_dispatch_request_msg.dispatch_motion_direction());
  511. //need,need
  512. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  513. {
  514. m_dispatch_response_msg.mutable_id_struct()->set_terminal_id(m_dispatch_request_msg.mutable_id_struct()->terminal_id());
  515. m_dispatch_response_msg.mutable_id_struct()->set_unit_id(m_dispatch_request_msg.mutable_id_struct()->terminal_id()/2);
  516. }
  517. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  518. {
  519. m_dispatch_response_msg.mutable_id_struct()->set_terminal_id(m_dispatch_request_msg.mutable_id_struct()->unit_id()*2+m_outlet_id);
  520. m_dispatch_response_msg.mutable_id_struct()->set_unit_id(m_dispatch_request_msg.mutable_id_struct()->unit_id());
  521. }
  522. // m_dispatch_response_msg.set_terminal_id(m_dispatch_request_msg.terminal_id());
  523. m_dispatch_response_msg.mutable_locate_information()->CopyFrom(m_dispatch_request_msg.locate_information());
  524. m_dispatch_response_msg.mutable_parkspace_info_ex()->CopyFrom(m_dispatch_request_msg.parkspace_info_ex());
  525. m_dispatch_response_msg.set_car_type(m_dispatch_request_msg.car_type());
  526. if ( m_dispatch_response_msg.dispatch_motion_direction() == message::E_STORE_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_occupied);
  533. }
  534. else
  535. {
  536. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  537. }
  538. }
  539. }
  540. else if( m_dispatch_response_msg.dispatch_motion_direction() == message::E_PICKUP_CAR )
  541. {
  542. for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  543. {
  544. if ( m_result == Error_code::SUCCESS )
  545. {
  546. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  547. }
  548. else
  549. {
  550. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  551. }
  552. }
  553. }
  554. m_dispatch_response_msg.mutable_error_manager()->set_error_code(m_result.get_error_code());
  555. m_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)m_result.get_error_level());
  556. m_dispatch_response_msg.mutable_error_manager()->set_error_description(m_result.get_error_description());
  557. std::cout << " huli test :::: " << " m_dispatch_response_msg = " << m_dispatch_response_msg.DebugString()<< std::endl;
  558. std::string t_msg = m_dispatch_response_msg.SerializeAsString();
  559. System_communication::get_instance_references().encapsulate_msg(t_msg);
  560. return Error_code::SUCCESS;
  561. }