dispatch_plc.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. message::Dispatch_request_msg Dispatch_plc::get_dispatch_request_msg()
  91. {
  92. std::unique_lock<std::mutex> t_lock(m_lock);
  93. return m_dispatch_request_msg;
  94. }
  95. void Dispatch_plc::clear_request_msg()
  96. {
  97. m_request_key.clear(); //请求唯一码, 用作识别
  98. m_request_status = 0; //请求确认标志
  99. // 调度指令的起点,终点,方向
  100. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_UNKNOWN; //调度方向 0=未知,1=存车,2=取车
  101. m_request_passageway_id = 0; //出入口id 6个入口和6个出口
  102. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN; //出入口方向 0=未知,1=入口,2=出口
  103. m_request_parkingspace_index_id = 0; //楼上车位索引id 1~180(3*6*13)
  104. m_request_parkingspace_unit_id = 0; //楼上车位单元号 1~3
  105. m_request_parkingspace_label_id = 0; //楼上车位标签号 1~78
  106. m_request_parkingspace_floor_id = 0; //楼上车位楼层号 2~11
  107. m_request_parkingspace_room_id = 0; //楼上车位房间号 1~6
  108. m_request_parkingspace_direction = Common_data::Parkingspace_direction::PARKINGSPACE_DIRECTION_UNKNOWN; //楼上车位方向 0=未知,1=朝南,2=朝北
  109. //汽车的定位信息(地面雷达)
  110. m_request_car_center_x = 0; //整车的中心点x值, 四轮的中心, 单位:米 m
  111. m_request_car_center_y = 0; //整车的中心点y值, 四轮的中心, 单位:米 m
  112. m_request_car_angle = 0; //整车的车身旋转角, 单位:度 (-90~90)
  113. m_request_car_front_theta = 0; //整车的前轮的旋转角, 单位:度 (-90~90)
  114. m_request_car_length = 0; //整车的长度, 用于规避碰撞, 单位:米 m
  115. m_request_car_width = 0; //整车的宽度, 用于规避碰撞, 单位:米 m
  116. m_request_car_height = 0; //整车的高度, 用于规避碰撞, 单位:米 m
  117. m_request_car_wheel_base = 0; //整车的轮距, 前后轮的距离, 用于机器人或agv的抓车, 单位:米 m
  118. m_request_car_wheel_width = 0; //整车的轮距, 左右轮的距离, 用于机器人或agv的抓车, 单位:米 m
  119. m_request_car_license.clear(); //车牌号(汽车唯一标示) 例如: 鄂A12345
  120. m_request_car_type = Common_data::Car_type::UNKNOW_CAR_TYPE; //车的大小
  121. m_request_uniformed_car_x = 0; //转角复位后,车辆中心点x
  122. m_request_uniformed_car_y = 0; //转角复位后,车辆中心点y
  123. //防撞雷达
  124. m_request_anticollision_lidar_flag = Common_data::Anticollision_lidar_flag::ANTICOLLISION_LIDAR_UNKNOWN; //汽车是否停到正确的位置, 防撞雷达 预留, 楚天暂时不用,0=未知,1=位置正常,2=位置异常
  125. //轮距雷达
  126. m_request_car_wheel_base_exact_value = 0; //汽车前后轮距,轮距雷达的精确值 预留, 楚天暂时不用,单位:米 m
  127. }
  128. //jiancha qingqiu xiaoxi
  129. Error_manager Dispatch_plc::check_dispatch_request_msg(message::Dispatch_request_msg & dispatch_request_msg)
  130. {
  131. Error_manager t_error;
  132. if(dispatch_request_msg.has_locate_information() &&
  133. dispatch_request_msg.has_dispatch_motion_direction() &&
  134. dispatch_request_msg.has_car_type() &&
  135. dispatch_request_msg.has_id_struct() &&
  136. dispatch_request_msg.has_command_key())
  137. {
  138. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  139. {
  140. if ( dispatch_request_msg.id_struct().has_unit_id() )
  141. {
  142. }
  143. }
  144. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  145. return Error_code::SUCCESS;
  146. }
  147. else
  148. {
  149. return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  150. " m_dispatch_plc_status error ");
  151. }
  152. return Error_code::SUCCESS;
  153. }
  154. //执行外界任务的执行函数
  155. void Dispatch_plc::execute_thread_fun()
  156. {
  157. LOG(INFO) << " Dispatch_plc::execute_thread_fun() start " << this;
  158. Error_manager t_error;
  159. while (m_execute_condition.is_alive())
  160. {
  161. m_execute_condition.wait();
  162. if (m_execute_condition.is_alive())
  163. {
  164. std::this_thread::sleep_for(std::chrono::microseconds(1));
  165. std::this_thread::sleep_for(std::chrono::seconds(1));
  166. std::this_thread::yield();
  167. // std::unique_lock<std::mutex> t_lock(m_lock);
  168. static int t_count = 0;
  169. if (t_count % 10 == 0 )
  170. {
  171. std::cout << " huli test :::: " << " m_plc_id = " << m_plc_id << std::endl;
  172. std::cout << " huli test :::: " << " m_dispatch_plc_status = " << m_dispatch_plc_status << std::endl;
  173. }
  174. t_count++;
  175. switch ((Dispatch_plc_status) m_dispatch_plc_status)
  176. {
  177. case DISPATCH_PLC_READY:
  178. {
  179. if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW )
  180. {
  181. update_dispatch_plc_communication();
  182. }
  183. else
  184. {
  185. m_result = check_dispatch_request_msg(m_dispatch_request_msg);
  186. if(m_result == Error_code::SUCCESS)
  187. {
  188. m_dispatch_plc_status = DISPATCH_PLC_REQUEST;
  189. }
  190. else
  191. {
  192. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  193. }
  194. }
  195. break;
  196. }
  197. case DISPATCH_PLC_REQUEST://给plc发送请求
  198. {
  199. #ifdef PLC_OVER_TIME_TO_ERROR
  200. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  201. {
  202. //超时直接报错
  203. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  204. " DISPATCH_PLC_TIME_OUT fun error ");
  205. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  206. }
  207. else
  208. #endif
  209. {
  210. //封装 给plc的调度请求
  211. m_result = encapsulate_dispatch_request_to_plc();
  212. if ( m_result == Error_code::SUCCESS )
  213. {
  214. m_dispatch_plc_status = DISPATCH_PLC_WORKING;
  215. }
  216. else
  217. {
  218. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  219. }
  220. }
  221. break;
  222. }
  223. case DISPATCH_PLC_WORKING:
  224. {
  225. #ifdef PLC_OVER_TIME_TO_ERROR
  226. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  227. {
  228. //超时直接报错
  229. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  230. " DISPATCH_PLC_TIME_OUT fun error ");
  231. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  232. }
  233. else
  234. #endif
  235. {
  236. update_dispatch_plc_communication();
  237. //检查plc答复反馈
  238. m_result = check_response_from_plc();
  239. if ( m_result == Error_code::SUCCESS )
  240. {
  241. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  242. }
  243. else if ( m_result != Error_code::NODATA )
  244. {
  245. // m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  246. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  247. }
  248. //else 原地等待
  249. }
  250. break;
  251. }
  252. case DISPATCH_PLC_RESPONSE:
  253. {
  254. //20211213. huli m_request_status = 0 after over process
  255. m_request_status = 0;
  256. clear_request_msg();
  257. //发送调度答复信息给主控
  258. send_dispatch_response_to_main_control();
  259. //无论前面的流程是否正确,都要给主控答复, 反馈里面填充错误码即可.
  260. if ( m_result == Error_code::SUCCESS )
  261. {
  262. //response add to _map
  263. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  264. {
  265. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  266. std::chrono::system_clock::time_point t_time = std::chrono::system_clock::now();
  267. Dispatch_manager::get_instance_references().m_dispatch_response_store_map[t_time] = m_dispatch_response_msg;
  268. Dispatch_manager::get_instance_references().m_store_updata_time = std::chrono::system_clock::now();
  269. }
  270. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  271. {
  272. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  273. int t_terminal_id = m_dispatch_request_msg.id_struct().terminal_id();
  274. Dispatch_manager::get_instance_references().m_dispatch_response_pickup_map[t_terminal_id] = m_dispatch_response_msg;
  275. Dispatch_manager::get_instance_references().m_pickup_updata_time = std::chrono::system_clock::now();
  276. }
  277. //流程正常结束, 恢复到待机状态.
  278. m_dispatch_plc_status = DISPATCH_PLC_READY;
  279. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  280. m_result.error_manager_clear_all();
  281. }
  282. else
  283. {
  284. //流程异常结束, 进入到故障状态.
  285. m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  286. }
  287. break;
  288. }
  289. case DISPATCH_PLC_FAULT:
  290. {
  291. //20211213. huli m_request_status = 0 after over process
  292. m_request_status = 0;
  293. clear_request_msg();
  294. //不在接受新的任务,但是保持基本的通信正常
  295. update_dispatch_plc_communication();
  296. //response add to _map
  297. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  298. {
  299. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  300. std::chrono::system_clock::time_point t_time = std::chrono::system_clock::now();
  301. Dispatch_manager::get_instance_references().m_dispatch_response_store_map[t_time] = m_dispatch_response_msg;
  302. Dispatch_manager::get_instance_references().m_store_updata_time = std::chrono::system_clock::now();
  303. }
  304. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  305. {
  306. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  307. int t_terminal_id = m_dispatch_request_msg.id_struct().terminal_id();
  308. Dispatch_manager::get_instance_references().m_dispatch_response_pickup_map[t_terminal_id] = m_dispatch_response_msg;
  309. Dispatch_manager::get_instance_references().m_pickup_updata_time = std::chrono::system_clock::now();
  310. }
  311. //20211209 huli //流程异常结束, 进入到 ready.
  312. m_dispatch_plc_status = DISPATCH_PLC_READY;
  313. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  314. m_result.error_manager_clear_all();
  315. LOG(ERROR) << " Dispatch_plc::execute_thread_fun() dispatch_plc is fault, now recover to normal " << this;
  316. break;
  317. }
  318. }
  319. }
  320. }
  321. }
  322. //封装 给plc的调度请求
  323. Error_manager Dispatch_plc::encapsulate_dispatch_request_to_plc()
  324. {
  325. //把m_dispatch_request_msg的信息传给本地的数据缓存.
  326. std::unique_lock<std::mutex> t_lock(m_lock);
  327. m_request_key = m_dispatch_request_msg.command_key();
  328. m_request_status = 1;
  329. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  330. {
  331. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
  332. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_INLET;
  333. m_request_passageway_id = m_dispatch_request_msg.mutable_id_struct()->terminal_id()+1;//0~5入口终端号改为1~6
  334. }
  335. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  336. {
  337. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  338. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_OUTLET;
  339. m_request_passageway_id = m_dispatch_request_msg.mutable_id_struct()->terminal_id()+1;//0~5入口终端号改为1~6
  340. }
  341. else
  342. {
  343. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_UNKNOWN;
  344. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN;
  345. m_request_passageway_id = -1;
  346. }
  347. if ( m_dispatch_request_msg.locate_information().locate_correct() )
  348. {
  349. m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  350. m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  351. #ifdef MEASURE_TO_PLC_CORRECTION
  352. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.3;//80~100改为-10~+10
  353. #endif
  354. #ifndef MEASURE_TO_PLC_CORRECTION
  355. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  356. #endif
  357. m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  358. m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  359. m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  360. m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  361. m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  362. m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  363. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_x();
  364. m_request_uniformed_car_y = m_dispatch_request_msg.locate_information().uniformed_car_y();
  365. }
  366. else
  367. {
  368. m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  369. m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  370. #ifdef MEASURE_TO_PLC_CORRECTION
  371. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.3;//80~100改为-10~+10
  372. #endif
  373. #ifndef MEASURE_TO_PLC_CORRECTION
  374. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  375. #endif
  376. m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  377. m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  378. m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  379. m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  380. m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  381. m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  382. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_x();
  383. m_request_uniformed_car_y = m_dispatch_request_msg.locate_information().uniformed_car_y();
  384. }
  385. if ( m_dispatch_request_msg.parkspace_info_ex_size() ==1 )
  386. {
  387. m_request_parkingspace_index_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_index_id();
  388. m_request_parkingspace_unit_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_unit_id()+1;//0~2单元号改为1~3
  389. m_request_parkingspace_label_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_label_id();
  390. m_request_parkingspace_floor_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_floor_id();
  391. m_request_parkingspace_room_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_room_id();
  392. //车位朝向, 小号朝前朝南, 大号朝后朝北
  393. m_request_parkingspace_direction = (Common_data::Parkingspace_direction)m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_direction();
  394. //20211220, utf-8 -> GBK
  395. m_request_car_license = String_convert::utf8_to_gbk( m_dispatch_request_msg.parkspace_info_ex(0).car_info().car_numberplate() );
  396. m_request_car_type = (Common_data::Car_type)m_dispatch_request_msg.parkspace_info_ex(0).car_type();
  397. //20211207, huli add wheel_base for pickup_car
  398. if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  399. {
  400. m_request_car_wheel_base = m_dispatch_request_msg.parkspace_info_ex(0).car_info().car_wheel_base();
  401. m_request_car_wheel_width = m_dispatch_request_msg.parkspace_info_ex(0).car_info().car_wheel_width();
  402. }
  403. }
  404. else
  405. {
  406. return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  407. " m_dispatch_request_msg.parkspace_info_ex_size() !=1 error ");
  408. }
  409. return Error_code::SUCCESS;
  410. }
  411. //更新plc通信
  412. Error_manager Dispatch_plc::update_dispatch_plc_communication()
  413. {
  414. std::unique_lock<std::mutex> t_lock(m_lock);
  415. std::unique_lock<std::mutex> t_lock1(Dispatch_communication::get_instance_references().m_data_lock);
  416. /*
  417. //请求消息, 调度->plc
  418. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  419. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  420. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  421. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  422. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  423. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  424. m_request_key = "123456789";
  425. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  426. static unsigned char index = 0;
  427. index++;
  428. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = index;
  429. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = 02;
  430. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = 03;
  431. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = 04;
  432. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = 05;
  433. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = 6;
  434. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = 7;
  435. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = 8;
  436. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = 11;
  437. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = 12;
  438. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = 13;
  439. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = 14;
  440. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = 15;
  441. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = 16;
  442. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = 17;
  443. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = 18;
  444. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = 19;
  445. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  446. int t_size2 = m_request_key.size()<=20 ? m_request_key.size() : 20 ;
  447. m_request_car_license = "ABCDEF";
  448. memcpy(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, m_request_car_license.c_str(), t_size2);
  449. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = 33;
  450. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = 44;
  451. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = 55;
  452. */
  453. //请求消息, 调度->plc
  454. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  455. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  456. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  457. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  458. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  459. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  460. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  461. tp_dispatch_request_from_manager_to_plc_for_data->m_request_status = m_request_status;
  462. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = m_request_dispatch_motion_direction;
  463. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = m_request_passageway_id;
  464. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = m_request_passageway_direction;
  465. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = m_request_parkingspace_index_id;
  466. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = m_request_parkingspace_unit_id;
  467. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_label_id = m_request_parkingspace_label_id;
  468. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = m_request_parkingspace_floor_id;
  469. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = m_request_parkingspace_room_id;
  470. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = m_request_parkingspace_direction;
  471. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = m_request_car_center_x;
  472. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = m_request_car_center_y;
  473. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = m_request_car_angle;
  474. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = m_request_car_front_theta;
  475. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = m_request_car_length;
  476. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = m_request_car_width;
  477. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = m_request_car_height;
  478. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = m_request_car_wheel_base;
  479. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = m_request_car_wheel_width;
  480. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  481. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license[0] = 18;
  482. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license[1] = m_request_car_license.length();
  483. unsigned char * p_ch = tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license;
  484. memcpy(p_ch+2, m_request_car_license.c_str(), m_request_car_license.length());
  485. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = m_request_car_type;
  486. tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_x = m_request_uniformed_car_x;
  487. tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_y = m_request_uniformed_car_y;
  488. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = m_request_anticollision_lidar_flag;
  489. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = m_request_car_wheel_base_exact_value;
  490. //答复消息, plc->调度
  491. Dispatch_communication::Dispatch_response_from_plc_to_manager * tp_dispatch_response_from_plc_to_manager =
  492. & Dispatch_communication::get_instance_references().m_dispatch_response_from_plc_to_manager;
  493. m_response_key = (char*) tp_dispatch_response_from_plc_to_manager->m_response_key;
  494. m_response_status = (Dispatch_plc::Response_status)tp_dispatch_response_from_plc_to_manager->m_response_status;
  495. m_response_dispatch_motion_direction = (Common_data::Dispatch_motion_direction)tp_dispatch_response_from_plc_to_manager->m_response_dispatch_motion_direction;
  496. m_response_passageway_id = tp_dispatch_response_from_plc_to_manager->m_response_passageway_id;
  497. m_response_passageway_direction = (Common_data::Passageway_direction)tp_dispatch_response_from_plc_to_manager->m_response_passageway_direction;
  498. m_response_parkingspace_index_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_index_id;
  499. m_response_parkingspace_unit_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_unit_id;
  500. m_response_parkingspace_label_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_label_id;
  501. m_response_parkingspace_floor_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_floor_id;
  502. m_response_parkingspace_room_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_room_id;
  503. m_response_parkingspace_direction = (Common_data::Parkingspace_direction)tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_direction;
  504. m_response_car_center_x = tp_dispatch_response_from_plc_to_manager->m_response_car_center_x;
  505. m_response_car_center_y = tp_dispatch_response_from_plc_to_manager->m_response_car_center_y;
  506. m_response_car_angle = tp_dispatch_response_from_plc_to_manager->m_response_car_angle;
  507. m_response_car_front_theta = tp_dispatch_response_from_plc_to_manager->m_response_car_front_theta;
  508. m_response_car_length = tp_dispatch_response_from_plc_to_manager->m_response_car_length;
  509. m_response_car_width = tp_dispatch_response_from_plc_to_manager->m_response_car_width;
  510. m_response_car_height = tp_dispatch_response_from_plc_to_manager->m_response_car_height;
  511. m_response_car_wheel_base = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base;
  512. m_response_car_wheel_width = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_width;
  513. m_response_car_license = (char*) (tp_dispatch_response_from_plc_to_manager->m_response_car_license+2);
  514. m_response_car_type = (Common_data::Car_type) tp_dispatch_response_from_plc_to_manager->m_response_car_type;
  515. m_response_uniformed_car_x = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_x;
  516. m_response_uniformed_car_y = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_y;
  517. m_response_anticollision_lidar_flag = (Common_data::Anticollision_lidar_flag)tp_dispatch_response_from_plc_to_manager->m_response_anticollision_lidar_flag;
  518. m_response_car_wheel_base_exact_value = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base_exact_value;
  519. /*
  520. std::cout << " huli test :::: " << " ------------------------------------------------------ = " << std::endl;
  521. std::cout << " huli test :::: " << " m_response_key = " << m_response_key << std::endl;
  522. std::cout << " huli test :::: " << " m_response_status = " << (int)m_response_status << std::endl;
  523. std::cout << " huli test :::: " << " m_response_dispatch_motion_direction = " << (int)m_response_dispatch_motion_direction << std::endl;
  524. std::cout << " huli test :::: " << " m_response_passageway_id = " << m_response_passageway_id << std::endl;
  525. std::cout << " huli test :::: " << " m_response_passageway_direction = " << (int)m_response_passageway_direction << std::endl;
  526. std::cout << " huli test :::: " << " m_response_parkingspace_index_id = " << m_response_parkingspace_index_id << std::endl;
  527. std::cout << " huli test :::: " << " m_response_parkingspace_unit_id = " << m_response_parkingspace_unit_id << std::endl;
  528. std::cout << " huli test :::: " << " m_response_parkingspace_floor_id = " << m_response_parkingspace_floor_id << std::endl;
  529. std::cout << " huli test :::: " << " m_response_parkingspace_room_id = " << m_response_parkingspace_room_id << std::endl;
  530. std::cout << " huli test :::: " << " m_response_parkingspace_direction = " << (int)m_response_parkingspace_direction << std::endl;
  531. std::cout << " huli test :::: " << " m_response_car_center_x = " << m_response_car_center_x << std::endl;
  532. std::cout << " huli test :::: " << " m_response_car_center_y = " << m_response_car_center_y << std::endl;
  533. std::cout << " huli test :::: " << " m_response_car_license = " << m_response_car_license << std::endl;
  534. */
  535. //状态消息, plc->调度
  536. Dispatch_communication::Dispatch_plc_status_from_plc_to_manager * tp_dispatch_plc_status_from_plc_to_manager =
  537. & Dispatch_communication::get_instance_references().m_dispatch_plc_status_from_plc_to_manager;
  538. //need
  539. // //通过心跳帧来判断通信是否正常
  540. // if ( m_last_heartbeat != tp_dispatch_plc_status_from_plc_to_manager->m_heartbeat )
  541. // {
  542. // m_last_heartbeat = tp_dispatch_plc_status_from_plc_to_manager->m_heartbeat;
  543. // m_status_updata_time = std::chrono::system_clock::now();
  544. //
  545. // if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT )
  546. // {
  547. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY;
  548. // }
  549. // }
  550. // else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(COMMUNICATION_OVER_TIME_MS))
  551. // {
  552. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT;
  553. // }
  554. // //else 继续等待,直到消息刷新或者超时.
  555. return Error_code::SUCCESS;
  556. }
  557. //检查plc答复反馈
  558. Error_manager Dispatch_plc::check_response_from_plc()
  559. {
  560. std::unique_lock<std::mutex> t_lock(m_lock);
  561. #ifndef WAIT_PLC_RESPONSE
  562. //need, 调试代码, 延时10s, 直接返回成功
  563. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::seconds(20) )
  564. {
  565. return Error_code::SUCCESS;
  566. }
  567. //返回没有收到数据
  568. else
  569. {
  570. return Error_code::NODATA;
  571. }
  572. #endif
  573. #ifdef WAIT_PLC_RESPONSE
  574. //检查唯一码
  575. if ( m_response_key == m_request_key )
  576. {
  577. std::cout<< "m_response_status = " << m_response_status << std::endl;
  578. //如果故障,则添加错误码
  579. if ( m_response_status != RESPONS_OVER )
  580. {
  581. //添加错误码
  582. Error_manager t_error(DISPATCH_PLC_RESPONS_ERROR, MINOR_ERROR, " Dispatch_plc::check_response_from_plc() m_respons_status is error");
  583. return t_error;
  584. }
  585. return Error_code::SUCCESS;
  586. }
  587. //返回没有收到数据
  588. else
  589. {
  590. return Error_code::NODATA;
  591. }
  592. #endif
  593. return Error_code::SUCCESS;
  594. }
  595. //发送调度答复信息给主控
  596. Error_manager Dispatch_plc::send_dispatch_response_to_main_control()
  597. {
  598. std::unique_lock<std::mutex> t_lock(m_lock);
  599. m_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  600. m_dispatch_response_msg.mutable_base_info()->set_timeout_ms(m_dispatch_request_msg.base_info().timeout_ms());
  601. m_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  602. m_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  603. m_dispatch_response_msg.set_command_key(m_dispatch_request_msg.command_key());
  604. m_dispatch_response_msg.set_dispatch_motion_direction(m_dispatch_request_msg.dispatch_motion_direction());
  605. m_dispatch_response_msg.mutable_id_struct()->set_terminal_id(m_dispatch_request_msg.mutable_id_struct()->terminal_id());
  606. m_dispatch_response_msg.mutable_id_struct()->set_unit_id(m_dispatch_request_msg.mutable_id_struct()->unit_id());
  607. m_dispatch_response_msg.mutable_locate_information()->CopyFrom(m_dispatch_request_msg.locate_information());
  608. m_dispatch_response_msg.mutable_parkspace_info_ex()->CopyFrom(m_dispatch_request_msg.parkspace_info_ex());
  609. m_dispatch_response_msg.set_car_type(m_dispatch_request_msg.car_type());
  610. if ( m_dispatch_response_msg.dispatch_motion_direction() == message::E_STORE_CAR )
  611. {
  612. for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  613. {
  614. if ( m_result == Error_code::SUCCESS )
  615. {
  616. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  617. }
  618. else
  619. {
  620. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  621. }
  622. }
  623. }
  624. else if( m_dispatch_response_msg.dispatch_motion_direction() == message::E_PICKUP_CAR )
  625. {
  626. for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  627. {
  628. if ( m_result == Error_code::SUCCESS )
  629. {
  630. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  631. }
  632. else
  633. {
  634. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  635. }
  636. }
  637. }
  638. m_dispatch_response_msg.mutable_error_manager()->set_error_code(m_result.get_error_code());
  639. m_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)m_result.get_error_level());
  640. m_dispatch_response_msg.mutable_error_manager()->set_error_description(m_result.get_error_description());
  641. std::cout << " huli test :::: " << " m_dispatch_response_msg = " << m_dispatch_response_msg.DebugString()<< std::endl;
  642. std::string t_msg = m_dispatch_response_msg.SerializeAsString();
  643. System_communication::get_instance_references().encapsulate_msg(t_msg);
  644. return Error_code::SUCCESS;
  645. }