dispatch_plc.cpp 36 KB

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