dispatch_plc.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  200. {
  201. //超时直接报错
  202. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  203. " DISPATCH_PLC_TIME_OUT fun error ");
  204. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  205. }
  206. else
  207. {
  208. //封装 给plc的调度请求
  209. m_result = encapsulate_dispatch_request_to_plc();
  210. if ( m_result == Error_code::SUCCESS )
  211. {
  212. m_dispatch_plc_status = DISPATCH_PLC_WORKING;
  213. }
  214. else
  215. {
  216. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  217. }
  218. }
  219. break;
  220. }
  221. case DISPATCH_PLC_WORKING:
  222. {
  223. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  224. {
  225. //超时直接报错
  226. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  227. " DISPATCH_PLC_TIME_OUT fun error ");
  228. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  229. }
  230. else
  231. {
  232. update_dispatch_plc_communication();
  233. //检查plc答复反馈
  234. m_result = check_response_from_plc();
  235. if ( m_result == Error_code::SUCCESS )
  236. {
  237. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  238. }
  239. else if ( m_result != Error_code::NODATA )
  240. {
  241. // m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  242. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  243. }
  244. //else 原地等待
  245. }
  246. break;
  247. }
  248. case DISPATCH_PLC_RESPONSE:
  249. {
  250. //20211213. huli m_request_status = 0 after over process
  251. m_request_status = 0;
  252. clear_request_msg();
  253. //发送调度答复信息给主控
  254. send_dispatch_response_to_main_control();
  255. //无论前面的流程是否正确,都要给主控答复, 反馈里面填充错误码即可.
  256. if ( m_result == Error_code::SUCCESS )
  257. {
  258. //response add to _map
  259. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  260. {
  261. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  262. std::chrono::system_clock::time_point t_time = std::chrono::system_clock::now();
  263. Dispatch_manager::get_instance_references().m_dispatch_response_store_map[t_time] = m_dispatch_response_msg;
  264. Dispatch_manager::get_instance_references().m_store_updata_time = std::chrono::system_clock::now();
  265. }
  266. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  267. {
  268. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  269. int t_terminal_id = m_dispatch_request_msg.id_struct().terminal_id();
  270. Dispatch_manager::get_instance_references().m_dispatch_response_pickup_map[t_terminal_id] = m_dispatch_response_msg;
  271. Dispatch_manager::get_instance_references().m_pickup_updata_time = std::chrono::system_clock::now();
  272. }
  273. //流程正常结束, 恢复到待机状态.
  274. m_dispatch_plc_status = DISPATCH_PLC_READY;
  275. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  276. m_result.error_manager_clear_all();
  277. }
  278. else
  279. {
  280. //流程异常结束, 进入到故障状态.
  281. m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  282. }
  283. break;
  284. }
  285. case DISPATCH_PLC_FAULT:
  286. {
  287. //20211213. huli m_request_status = 0 after over process
  288. m_request_status = 0;
  289. clear_request_msg();
  290. //不在接受新的任务,但是保持基本的通信正常
  291. update_dispatch_plc_communication();
  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. int t_terminal_id = m_dispatch_request_msg.id_struct().terminal_id();
  304. Dispatch_manager::get_instance_references().m_dispatch_response_pickup_map[t_terminal_id] = m_dispatch_response_msg;
  305. Dispatch_manager::get_instance_references().m_pickup_updata_time = std::chrono::system_clock::now();
  306. }
  307. //20211209 huli //流程异常结束, 进入到 ready.
  308. m_dispatch_plc_status = DISPATCH_PLC_READY;
  309. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  310. m_result.error_manager_clear_all();
  311. LOG(ERROR) << " Dispatch_plc::execute_thread_fun() dispatch_plc is fault, now recover to normal " << this;
  312. break;
  313. }
  314. }
  315. }
  316. }
  317. }
  318. //封装 给plc的调度请求
  319. Error_manager Dispatch_plc::encapsulate_dispatch_request_to_plc()
  320. {
  321. //把m_dispatch_request_msg的信息传给本地的数据缓存.
  322. std::unique_lock<std::mutex> t_lock(m_lock);
  323. m_request_key = m_dispatch_request_msg.command_key();
  324. m_request_status = 1;
  325. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  326. {
  327. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
  328. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_INLET;
  329. m_request_passageway_id = m_dispatch_request_msg.mutable_id_struct()->terminal_id()+1;//0~5入口终端号改为1~6
  330. }
  331. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  332. {
  333. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  334. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_OUTLET;
  335. m_request_passageway_id = m_dispatch_request_msg.mutable_id_struct()->terminal_id()+1;//0~5入口终端号改为1~6
  336. }
  337. else
  338. {
  339. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_UNKNOWN;
  340. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN;
  341. m_request_passageway_id = -1;
  342. }
  343. if ( m_dispatch_request_msg.locate_information().locate_correct() )
  344. {
  345. m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  346. m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  347. #ifdef MEASURE_TO_PLC_CORRECTION
  348. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.3;//80~100改为-10~+10
  349. #endif
  350. #ifndef MEASURE_TO_PLC_CORRECTION
  351. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  352. #endif
  353. m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  354. m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  355. m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  356. m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  357. m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  358. m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  359. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_x();
  360. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_y();
  361. }
  362. else
  363. {
  364. m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  365. m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  366. #ifdef MEASURE_TO_PLC_CORRECTION
  367. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.3;//80~100改为-10~+10
  368. #endif
  369. #ifndef MEASURE_TO_PLC_CORRECTION
  370. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  371. #endif
  372. m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  373. m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  374. m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  375. m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  376. m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  377. m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  378. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_x();
  379. m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_y();
  380. }
  381. if ( m_dispatch_request_msg.parkspace_info_ex_size() ==1 )
  382. {
  383. m_request_parkingspace_index_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_index_id();
  384. m_request_parkingspace_unit_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_unit_id()+1;//0~2单元号改为1~3
  385. m_request_parkingspace_label_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_label_id();
  386. m_request_parkingspace_floor_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_floor_id();
  387. m_request_parkingspace_room_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_room_id();
  388. //车位朝向, 小号朝前朝南, 大号朝后朝北
  389. m_request_parkingspace_direction = (Common_data::Parkingspace_direction)m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_direction();
  390. //20211220, utf-8 -> GBK
  391. m_request_car_license = String_convert::utf8_to_gbk( m_dispatch_request_msg.parkspace_info_ex(0).car_info().car_numberplate() );
  392. m_request_car_type = (Common_data::Car_type)m_dispatch_request_msg.parkspace_info_ex(0).car_type();
  393. //20211207, huli add wheel_base for pickup_car
  394. if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  395. {
  396. m_request_car_wheel_base = m_dispatch_request_msg.parkspace_info_ex(0).car_info().car_wheel_base();
  397. m_request_car_wheel_width = m_dispatch_request_msg.parkspace_info_ex(0).car_info().car_wheel_width();
  398. }
  399. }
  400. else
  401. {
  402. return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  403. " m_dispatch_request_msg.parkspace_info_ex_size() !=1 error ");
  404. }
  405. return Error_code::SUCCESS;
  406. }
  407. //更新plc通信
  408. Error_manager Dispatch_plc::update_dispatch_plc_communication()
  409. {
  410. std::unique_lock<std::mutex> t_lock(m_lock);
  411. std::unique_lock<std::mutex> t_lock1(Dispatch_communication::get_instance_references().m_data_lock);
  412. /*
  413. //请求消息, 调度->plc
  414. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  415. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  416. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  417. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  418. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  419. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  420. m_request_key = "123456789";
  421. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  422. static unsigned char index = 0;
  423. index++;
  424. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = index;
  425. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = 02;
  426. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = 03;
  427. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = 04;
  428. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = 05;
  429. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = 6;
  430. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = 7;
  431. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = 8;
  432. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = 11;
  433. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = 12;
  434. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = 13;
  435. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = 14;
  436. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = 15;
  437. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = 16;
  438. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = 17;
  439. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = 18;
  440. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = 19;
  441. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  442. int t_size2 = m_request_key.size()<=20 ? m_request_key.size() : 20 ;
  443. m_request_car_license = "ABCDEF";
  444. memcpy(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, m_request_car_license.c_str(), t_size2);
  445. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = 33;
  446. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = 44;
  447. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = 55;
  448. */
  449. //请求消息, 调度->plc
  450. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  451. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  452. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  453. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  454. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  455. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  456. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  457. tp_dispatch_request_from_manager_to_plc_for_data->m_request_status = m_request_status;
  458. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = m_request_dispatch_motion_direction;
  459. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = m_request_passageway_id;
  460. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = m_request_passageway_direction;
  461. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = m_request_parkingspace_index_id;
  462. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = m_request_parkingspace_unit_id;
  463. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_label_id = m_request_parkingspace_label_id;
  464. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = m_request_parkingspace_floor_id;
  465. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = m_request_parkingspace_room_id;
  466. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = m_request_parkingspace_direction;
  467. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = m_request_car_center_x;
  468. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = m_request_car_center_y;
  469. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = m_request_car_angle;
  470. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = m_request_car_front_theta;
  471. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = m_request_car_length;
  472. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = m_request_car_width;
  473. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = m_request_car_height;
  474. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = m_request_car_wheel_base;
  475. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = m_request_car_wheel_width;
  476. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  477. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license[0] = 18;
  478. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license[1] = m_request_car_license.length();
  479. unsigned char * p_ch = tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license;
  480. memcpy(p_ch+2, m_request_car_license.c_str(), m_request_car_license.length());
  481. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = m_request_car_type;
  482. tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_x = m_request_uniformed_car_x;
  483. tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_y = m_request_uniformed_car_y;
  484. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = m_request_anticollision_lidar_flag;
  485. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = m_request_car_wheel_base_exact_value;
  486. //答复消息, plc->调度
  487. Dispatch_communication::Dispatch_response_from_plc_to_manager * tp_dispatch_response_from_plc_to_manager =
  488. & Dispatch_communication::get_instance_references().m_dispatch_response_from_plc_to_manager;
  489. m_response_key = (char*) tp_dispatch_response_from_plc_to_manager->m_response_key;
  490. m_response_status = (Dispatch_plc::Response_status)tp_dispatch_response_from_plc_to_manager->m_response_status;
  491. m_response_dispatch_motion_direction = (Common_data::Dispatch_motion_direction)tp_dispatch_response_from_plc_to_manager->m_response_dispatch_motion_direction;
  492. m_response_passageway_id = tp_dispatch_response_from_plc_to_manager->m_response_passageway_id;
  493. m_response_passageway_direction = (Common_data::Passageway_direction)tp_dispatch_response_from_plc_to_manager->m_response_passageway_direction;
  494. m_response_parkingspace_index_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_index_id;
  495. m_response_parkingspace_unit_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_unit_id;
  496. m_response_parkingspace_label_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_label_id;
  497. m_response_parkingspace_floor_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_floor_id;
  498. m_response_parkingspace_room_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_room_id;
  499. m_response_parkingspace_direction = (Common_data::Parkingspace_direction)tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_direction;
  500. m_response_car_center_x = tp_dispatch_response_from_plc_to_manager->m_response_car_center_x;
  501. m_response_car_center_y = tp_dispatch_response_from_plc_to_manager->m_response_car_center_y;
  502. m_response_car_angle = tp_dispatch_response_from_plc_to_manager->m_response_car_angle;
  503. m_response_car_front_theta = tp_dispatch_response_from_plc_to_manager->m_response_car_front_theta;
  504. m_response_car_length = tp_dispatch_response_from_plc_to_manager->m_response_car_length;
  505. m_response_car_width = tp_dispatch_response_from_plc_to_manager->m_response_car_width;
  506. m_response_car_height = tp_dispatch_response_from_plc_to_manager->m_response_car_height;
  507. m_response_car_wheel_base = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base;
  508. m_response_car_wheel_width = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_width;
  509. m_response_car_license = (char*) (tp_dispatch_response_from_plc_to_manager->m_response_car_license+2);
  510. m_response_car_type = (Common_data::Car_type) tp_dispatch_response_from_plc_to_manager->m_response_car_type;
  511. m_response_uniformed_car_x = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_x;
  512. m_response_uniformed_car_y = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_y;
  513. m_response_anticollision_lidar_flag = (Common_data::Anticollision_lidar_flag)tp_dispatch_response_from_plc_to_manager->m_response_anticollision_lidar_flag;
  514. m_response_car_wheel_base_exact_value = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base_exact_value;
  515. /*
  516. std::cout << " huli test :::: " << " ------------------------------------------------------ = " << std::endl;
  517. std::cout << " huli test :::: " << " m_response_key = " << m_response_key << std::endl;
  518. std::cout << " huli test :::: " << " m_response_status = " << (int)m_response_status << std::endl;
  519. std::cout << " huli test :::: " << " m_response_dispatch_motion_direction = " << (int)m_response_dispatch_motion_direction << std::endl;
  520. std::cout << " huli test :::: " << " m_response_passageway_id = " << m_response_passageway_id << std::endl;
  521. std::cout << " huli test :::: " << " m_response_passageway_direction = " << (int)m_response_passageway_direction << std::endl;
  522. std::cout << " huli test :::: " << " m_response_parkingspace_index_id = " << m_response_parkingspace_index_id << std::endl;
  523. std::cout << " huli test :::: " << " m_response_parkingspace_unit_id = " << m_response_parkingspace_unit_id << std::endl;
  524. std::cout << " huli test :::: " << " m_response_parkingspace_floor_id = " << m_response_parkingspace_floor_id << std::endl;
  525. std::cout << " huli test :::: " << " m_response_parkingspace_room_id = " << m_response_parkingspace_room_id << std::endl;
  526. std::cout << " huli test :::: " << " m_response_parkingspace_direction = " << (int)m_response_parkingspace_direction << std::endl;
  527. std::cout << " huli test :::: " << " m_response_car_center_x = " << m_response_car_center_x << std::endl;
  528. std::cout << " huli test :::: " << " m_response_car_center_y = " << m_response_car_center_y << std::endl;
  529. std::cout << " huli test :::: " << " m_response_car_license = " << m_response_car_license << std::endl;
  530. */
  531. //状态消息, plc->调度
  532. Dispatch_communication::Dispatch_plc_status_from_plc_to_manager * tp_dispatch_plc_status_from_plc_to_manager =
  533. & Dispatch_communication::get_instance_references().m_dispatch_plc_status_from_plc_to_manager;
  534. //need
  535. // //通过心跳帧来判断通信是否正常
  536. // if ( m_last_heartbeat != tp_dispatch_plc_status_from_plc_to_manager->m_heartbeat )
  537. // {
  538. // m_last_heartbeat = tp_dispatch_plc_status_from_plc_to_manager->m_heartbeat;
  539. // m_status_updata_time = std::chrono::system_clock::now();
  540. //
  541. // if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT )
  542. // {
  543. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY;
  544. // }
  545. // }
  546. // else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(COMMUNICATION_OVER_TIME_MS))
  547. // {
  548. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT;
  549. // }
  550. // //else 继续等待,直到消息刷新或者超时.
  551. return Error_code::SUCCESS;
  552. }
  553. //检查plc答复反馈
  554. Error_manager Dispatch_plc::check_response_from_plc()
  555. {
  556. std::unique_lock<std::mutex> t_lock(m_lock);
  557. #ifndef WAIT_PLC_RESPONSE
  558. //need, 调试代码, 延时10s, 直接返回成功
  559. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::seconds(20) )
  560. {
  561. return Error_code::SUCCESS;
  562. }
  563. //返回没有收到数据
  564. else
  565. {
  566. return Error_code::NODATA;
  567. }
  568. #endif
  569. #ifdef WAIT_PLC_RESPONSE
  570. //检查唯一码
  571. if ( m_response_key == m_request_key )
  572. {
  573. std::cout<< "m_response_status = " << m_response_status << std::endl;
  574. //如果故障,则添加错误码
  575. if ( m_response_status != RESPONS_OVER )
  576. {
  577. //添加错误码
  578. Error_manager t_error(DISPATCH_PLC_RESPONS_ERROR, MINOR_ERROR, " Dispatch_plc::check_response_from_plc() m_respons_status is error");
  579. return t_error;
  580. }
  581. return Error_code::SUCCESS;
  582. }
  583. //返回没有收到数据
  584. else
  585. {
  586. return Error_code::NODATA;
  587. }
  588. #endif
  589. return Error_code::SUCCESS;
  590. }
  591. //发送调度答复信息给主控
  592. Error_manager Dispatch_plc::send_dispatch_response_to_main_control()
  593. {
  594. std::unique_lock<std::mutex> t_lock(m_lock);
  595. m_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  596. m_dispatch_response_msg.mutable_base_info()->set_timeout_ms(m_dispatch_request_msg.base_info().timeout_ms());
  597. m_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  598. m_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  599. m_dispatch_response_msg.set_command_key(m_dispatch_request_msg.command_key());
  600. m_dispatch_response_msg.set_dispatch_motion_direction(m_dispatch_request_msg.dispatch_motion_direction());
  601. m_dispatch_response_msg.mutable_id_struct()->set_terminal_id(m_dispatch_request_msg.mutable_id_struct()->terminal_id());
  602. m_dispatch_response_msg.mutable_id_struct()->set_unit_id(m_dispatch_request_msg.mutable_id_struct()->unit_id());
  603. m_dispatch_response_msg.mutable_locate_information()->CopyFrom(m_dispatch_request_msg.locate_information());
  604. m_dispatch_response_msg.mutable_parkspace_info_ex()->CopyFrom(m_dispatch_request_msg.parkspace_info_ex());
  605. m_dispatch_response_msg.set_car_type(m_dispatch_request_msg.car_type());
  606. if ( m_dispatch_response_msg.dispatch_motion_direction() == message::E_STORE_CAR )
  607. {
  608. for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  609. {
  610. if ( m_result == Error_code::SUCCESS )
  611. {
  612. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  613. }
  614. else
  615. {
  616. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  617. }
  618. }
  619. }
  620. else if( m_dispatch_response_msg.dispatch_motion_direction() == message::E_PICKUP_CAR )
  621. {
  622. for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  623. {
  624. if ( m_result == Error_code::SUCCESS )
  625. {
  626. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  627. }
  628. else
  629. {
  630. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  631. }
  632. }
  633. }
  634. m_dispatch_response_msg.mutable_error_manager()->set_error_code(m_result.get_error_code());
  635. m_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)m_result.get_error_level());
  636. m_dispatch_response_msg.mutable_error_manager()->set_error_description(m_result.get_error_description());
  637. std::cout << " huli test :::: " << " m_dispatch_response_msg = " << m_dispatch_response_msg.DebugString()<< std::endl;
  638. std::string t_msg = m_dispatch_response_msg.SerializeAsString();
  639. System_communication::get_instance_references().encapsulate_msg(t_msg);
  640. return Error_code::SUCCESS;
  641. }