dispatch_plc.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. //
  49. // if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  50. // {
  51. // std::unique_lock<std::mutex> t_lock(m_lock);
  52. //
  53. // //设定超时时间, 默认比任务指令里面的时间少10秒,
  54. // if ( dispatch_request_msg.base_info().has_timeout_ms() )
  55. // {
  56. // m_timeout_ms = dispatch_request_msg.base_info().timeout_ms() - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
  57. // }
  58. // else
  59. // {
  60. // m_timeout_ms = DISPATCH_PROCESS_TIMEOUT_MS - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
  61. // }
  62. // m_command_key = dispatch_request_msg.command_key();
  63. // m_start_time = std::chrono::system_clock::now();
  64. //
  65. //
  66. //// m_dispatch_request_msg = dispatch_request_msg;
  67. //// if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  68. //// {
  69. //// m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
  70. //// }
  71. //// else if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  72. //// {
  73. //// m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
  74. //// }
  75. //// else
  76. //// {
  77. //// return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  78. //// " dispatch_request_msg.dispatch_motion_direction() PARAMRTER ERROR ");
  79. //// }
  80. //
  81. // m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  82. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST;
  83. // }
  84. // else
  85. // {
  86. // return Error_manager(Error_code::DISPATCH_PLC_STATUS_ERROR, Error_level::MINOR_ERROR,
  87. // " m_dispatch_plc_status error ");
  88. // }
  89. //
  90. // return Error_code::SUCCESS;
  91. //}
  92. //新版调度plc 执行请求 存车
  93. Error_manager Dispatch_plc::execute_for_dispatch_request_msg(park_table &park_table_msg, Common_data::Dispatch_process_type dispatch_process_type, Common_data::Car_type car_type)
  94. {
  95. if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  96. {
  97. std::unique_lock<std::mutex> t_lock(m_lock);
  98. //超时默认一小时.
  99. m_timeout_ms = DISPATCH_PROCESS_TIMEOUT_MS - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
  100. //合成唯一码.
  101. // char t_buf[256] = {0};
  102. // sprintf(t_buf, "%d+%s+%s", park_table_msg.queue_id(), park_table_msg.car_number().c_str(), park_table_msg.primary_key().c_str());
  103. // m_command_key = park_table_msg.primary_key()+"_park";
  104. m_start_time = std::chrono::system_clock::now();
  105. m_park_table_msg = park_table_msg;
  106. m_dispatch_process_type = dispatch_process_type;
  107. m_car_type = car_type;
  108. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
  109. {
  110. m_command_key = park_table_msg.primary_key()+"_store";
  111. }
  112. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
  113. {
  114. m_command_key = park_table_msg.primary_key()+"_reallocate";
  115. }
  116. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST;
  117. }
  118. else
  119. {
  120. return Error_manager(Error_code::DISPATCH_PLC_STATUS_ERROR, Error_level::MINOR_ERROR,
  121. " m_dispatch_plc_status error ");
  122. }
  123. return Error_code::SUCCESS;
  124. }
  125. //新版调度plc 执行请求 取车
  126. Error_manager Dispatch_plc::execute_for_dispatch_request_msg(pick_table &pick_table_msg, Common_data::Dispatch_process_type dispatch_process_type, Common_data::Car_type car_type)
  127. {
  128. if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  129. {
  130. std::unique_lock<std::mutex> t_lock(m_lock);
  131. //超时默认一小时.
  132. m_timeout_ms = DISPATCH_PROCESS_TIMEOUT_MS - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
  133. //合成唯一码.
  134. // char t_buf[256] = {0};
  135. // sprintf(t_buf, "%d+%s+%s", pick_table_msg.queue_id(), pick_table_msg.car_number().c_str(), pick_table_msg.primary_key().c_str());
  136. // m_command_key = pick_table_msg.primary_key()+"_pick";
  137. m_start_time = std::chrono::system_clock::now();
  138. m_pick_table_msg = pick_table_msg;
  139. m_dispatch_process_type = dispatch_process_type;
  140. m_car_type = car_type;
  141. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
  142. {
  143. m_command_key = pick_table_msg.primary_key()+"_pickup";
  144. }
  145. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
  146. {
  147. m_command_key = pick_table_msg.primary_key()+"_revocation";
  148. }
  149. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST;
  150. }
  151. else
  152. {
  153. return Error_manager(Error_code::DISPATCH_PLC_STATUS_ERROR, Error_level::MINOR_ERROR,
  154. " m_dispatch_plc_status error ");
  155. }
  156. return Error_code::SUCCESS;
  157. }
  158. Dispatch_plc::Dispatch_plc_status Dispatch_plc::get_dispatch_plc_status()
  159. {
  160. return m_dispatch_plc_status;
  161. }
  162. //message::Dispatch_request_msg Dispatch_plc::get_dispatch_request_msg()
  163. //{
  164. // std::unique_lock<std::mutex> t_lock(m_lock);
  165. // return m_dispatch_request_msg;
  166. //}
  167. Error_manager Dispatch_plc::get_result()
  168. {
  169. return m_result;
  170. }
  171. float Dispatch_plc::get_response_working_total_time()
  172. {
  173. std::unique_lock<std::mutex> t_lock(m_lock);
  174. return m_response_working_total_time;
  175. }
  176. float Dispatch_plc::get_response_working_remaining_time()
  177. {
  178. std::unique_lock<std::mutex> t_lock(m_lock);
  179. return m_response_working_remaining_time;
  180. }
  181. void Dispatch_plc::clear_request_msg()
  182. {
  183. m_request_key.clear(); //请求唯一码, 用作识别
  184. m_request_status = 0; //请求确认标志
  185. // 调度指令的起点,终点,方向
  186. m_request_dispatch_motion_direction = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW; //调度方向 0=未知,1=存车,2=取车
  187. m_request_passageway_id = 0; //出入口id 6个入口和6个出口
  188. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN; //出入口方向 0=未知,1=入口,2=出口
  189. m_request_parkingspace_index_id = 0; //楼上车位索引id 1~180(3*6*13)
  190. m_request_parkingspace_unit_id = 0; //楼上车位单元号 1~3
  191. m_request_parkingspace_label_id = 0; //楼上车位标签号 1~78
  192. m_request_parkingspace_floor_id = 0; //楼上车位楼层号 2~11
  193. m_request_parkingspace_room_id = 0; //楼上车位房间号 1~6
  194. m_request_parkingspace_direction = Common_data::Parkingspace_direction::PARKINGSPACE_DIRECTION_UNKNOWN; //楼上车位方向 0=未知,1=朝南,2=朝北
  195. //汽车的定位信息(地面雷达)
  196. m_request_car_center_x = 0; //整车的中心点x值, 四轮的中心, 单位:米 m
  197. m_request_car_center_y = 0; //整车的中心点y值, 四轮的中心, 单位:米 m
  198. m_request_car_angle = 0; //整车的车身旋转角, 单位:度 (-90~90)
  199. m_request_car_front_theta = 0; //整车的前轮的旋转角, 单位:度 (-90~90)
  200. m_request_car_length = 0; //整车的长度, 用于规避碰撞, 单位:米 m
  201. m_request_car_width = 0; //整车的宽度, 用于规避碰撞, 单位:米 m
  202. m_request_car_height = 0; //整车的高度, 用于规避碰撞, 单位:米 m
  203. m_request_car_wheel_base = 0; //整车的轮距, 前后轮的距离, 用于机器人或agv的抓车, 单位:米 m
  204. m_request_car_wheel_width = 0; //整车的轮距, 左右轮的距离, 用于机器人或agv的抓车, 单位:米 m
  205. m_request_car_license.clear(); //车牌号(汽车唯一标示) 例如: 鄂A12345
  206. m_request_car_type = Common_data::Car_type::UNKNOW_CAR_TYPE; //车的大小
  207. m_request_uniformed_car_x = 0; //转角复位后,车辆中心点x
  208. m_request_uniformed_car_y = 0; //转角复位后,车辆中心点y
  209. //防撞雷达
  210. m_request_anticollision_lidar_flag = Common_data::Anticollision_lidar_flag::ANTICOLLISION_LIDAR_UNKNOWN; //汽车是否停到正确的位置, 防撞雷达 预留, 楚天暂时不用,0=未知,1=位置正常,2=位置异常
  211. //轮距雷达
  212. m_request_car_wheel_base_exact_value = 0; //汽车前后轮距,轮距雷达的精确值 预留, 楚天暂时不用,单位:米 m
  213. }
  214. //jiancha qingqiu xiaoxi
  215. //Error_manager Dispatch_plc::check_dispatch_request_msg(message::Dispatch_request_msg & dispatch_request_msg)
  216. //{
  217. // return Error_code::SUCCESS;
  218. // /*
  219. // Error_manager t_error;
  220. //
  221. // if(dispatch_request_msg.has_locate_information() &&
  222. // dispatch_request_msg.has_dispatch_motion_direction() &&
  223. // dispatch_request_msg.has_car_type() &&
  224. // dispatch_request_msg.has_id_struct() &&
  225. // dispatch_request_msg.has_command_key())
  226. // {
  227. // if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  228. // {
  229. // if ( dispatch_request_msg.id_struct().has_unit_id() )
  230. // {
  231. //
  232. // }
  233. // }
  234. // else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  235. //
  236. // return Error_code::SUCCESS;
  237. // }
  238. // else
  239. // {
  240. // return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  241. // " m_dispatch_plc_status error ");
  242. // }
  243. // */
  244. // return Error_code::SUCCESS;
  245. //}
  246. //执行外界任务的执行函数
  247. void Dispatch_plc::execute_thread_fun()
  248. {
  249. LOG(INFO) << " Dispatch_plc::execute_thread_fun() start " << this;
  250. Error_manager t_error;
  251. while (m_execute_condition.is_alive())
  252. {
  253. m_execute_condition.wait();
  254. if (m_execute_condition.is_alive())
  255. {
  256. std::this_thread::sleep_for(std::chrono::microseconds(1));
  257. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  258. // std::this_thread::sleep_for(std::chrono::seconds(1));
  259. std::this_thread::yield();
  260. // std::unique_lock<std::mutex> t_lock(m_lock);
  261. static int t_count = 0;
  262. if (t_count % 1000 == 0 )
  263. {
  264. std::cout << " huli test :::: " << " m_plc_id = " << m_plc_id << std::endl;
  265. std::cout << " huli test :::: " << " m_dispatch_plc_status = " << m_dispatch_plc_status << std::endl;
  266. }
  267. t_count++;
  268. switch ((Dispatch_plc_status) m_dispatch_plc_status)
  269. {
  270. case DISPATCH_PLC_READY:
  271. {
  272. if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW )
  273. {
  274. update_dispatch_plc_communication();
  275. }
  276. else
  277. {
  278. // m_result = check_dispatch_request_msg(m_dispatch_request_msg);
  279. //收到新指令, 进入工作阶段,强制清空错误码.
  280. m_result.error_manager_clear_all();
  281. if(m_result == Error_code::SUCCESS)
  282. {
  283. m_dispatch_plc_status = DISPATCH_PLC_REQUEST;
  284. }
  285. else
  286. {
  287. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  288. }
  289. }
  290. break;
  291. }
  292. case DISPATCH_PLC_REQUEST://给plc发送请求
  293. {
  294. //收到新指令, 进入工作阶段,强制清空错误码.
  295. m_result.error_manager_clear_all();
  296. #ifdef PLC_OVER_TIME_TO_ERROR
  297. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  298. {
  299. //超时直接报错
  300. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  301. " DISPATCH_PLC_TIME_OUT fun error ");
  302. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  303. }
  304. else
  305. #endif
  306. {
  307. //封装 给plc的调度请求
  308. // m_result = encapsulate_dispatch_request_to_plc();
  309. m_result = encapsulate_dispatch_request_to_plc_new();
  310. test_start_time = std::chrono::system_clock::now();
  311. m_result = Error_code::SUCCESS;
  312. if ( m_result == Error_code::SUCCESS )
  313. {
  314. m_dispatch_plc_status = DISPATCH_PLC_WORKING;
  315. }
  316. else
  317. {
  318. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  319. }
  320. }
  321. break;
  322. }
  323. case DISPATCH_PLC_WORKING:
  324. {
  325. #ifdef PLC_OVER_TIME_TO_ERROR
  326. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  327. {
  328. //超时直接报错
  329. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  330. " DISPATCH_PLC_TIME_OUT fun error ");
  331. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  332. }
  333. else
  334. #endif
  335. {
  336. update_dispatch_plc_communication();
  337. //检查plc答复反馈
  338. // m_result = check_response_from_plc();
  339. m_result = check_response_from_plc_new();
  340. if ( m_result == Error_code::SUCCESS )
  341. {
  342. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  343. }
  344. else if ( m_result != Error_code::NODATA )
  345. {
  346. // m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  347. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  348. }
  349. //else NODATA 原地等待
  350. }
  351. break;
  352. }
  353. case DISPATCH_PLC_RESPONSE:
  354. {
  355. test_end_time = std::chrono::system_clock::now();
  356. auto time_dur = test_end_time - test_start_time;
  357. if ( time_dur < std::chrono::seconds(2) )
  358. {
  359. LOG(INFO) << " 调度完成, 时间太短, 按任意键继续 = "<< time_dur.count() << " --- " << this;
  360. getchar();
  361. }
  362. //20211213. huli m_request_status = 0 after over process
  363. m_request_status = 0;
  364. clear_request_msg();
  365. //发送调度答复信息给主控
  366. // send_dispatch_response_to_main_control();
  367. // send_dispatch_response_to_main_control_new();
  368. //无论前面的流程是否正确,都要给主控答复, 反馈里面填充错误码即可.
  369. //最新版本不用答复消息, Dispatch_plc 切回 DISPATCH_PLC_READY 后, 由 Dispatch_manager 答复数据库
  370. if ( m_result == Error_code::SUCCESS )
  371. {
  372. /*
  373. //response add to _map
  374. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  375. {
  376. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  377. std::chrono::system_clock::time_point t_time = std::chrono::system_clock::now();
  378. Dispatch_manager::get_instance_references().m_dispatch_response_store_map[t_time] = m_dispatch_response_msg;
  379. Dispatch_manager::get_instance_references().m_store_updata_time = std::chrono::system_clock::now();
  380. }
  381. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  382. {
  383. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  384. int t_terminal_id = m_dispatch_request_msg.id_struct().terminal_id();
  385. Dispatch_manager::get_instance_references().m_dispatch_response_pickup_map[t_terminal_id] = m_dispatch_response_msg;
  386. Dispatch_manager::get_instance_references().m_pickup_updata_time = std::chrono::system_clock::now();
  387. }
  388. */
  389. //流程正常结束, 恢复到待机状态.
  390. m_dispatch_plc_status = DISPATCH_PLC_READY;
  391. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  392. // m_result.error_manager_clear_all();
  393. }
  394. else
  395. {
  396. //流程异常结束, 进入到故障状态.
  397. m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  398. }
  399. break;
  400. }
  401. case DISPATCH_PLC_FAULT:
  402. case DISPATCH_PLC_DISCONNECT:
  403. {
  404. //20211213. huli m_request_status = 0 after over process
  405. m_request_status = 0;
  406. clear_request_msg();
  407. //不在接受新的任务,但是保持基本的通信正常
  408. update_dispatch_plc_communication();
  409. /*
  410. //response add to _map
  411. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  412. {
  413. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  414. std::chrono::system_clock::time_point t_time = std::chrono::system_clock::now();
  415. Dispatch_manager::get_instance_references().m_dispatch_response_store_map[t_time] = m_dispatch_response_msg;
  416. Dispatch_manager::get_instance_references().m_store_updata_time = std::chrono::system_clock::now();
  417. }
  418. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  419. {
  420. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  421. int t_terminal_id = m_dispatch_request_msg.id_struct().terminal_id();
  422. Dispatch_manager::get_instance_references().m_dispatch_response_pickup_map[t_terminal_id] = m_dispatch_response_msg;
  423. Dispatch_manager::get_instance_references().m_pickup_updata_time = std::chrono::system_clock::now();
  424. }
  425. */
  426. //20211209 huli //流程异常结束, 进入到 ready.
  427. m_dispatch_plc_status = DISPATCH_PLC_READY;
  428. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  429. // m_result.error_manager_clear_all();
  430. LOG(ERROR) << " Dispatch_plc::execute_thread_fun() dispatch_plc is fault, now recover to normal " << this;
  431. break;
  432. }
  433. }
  434. }
  435. }
  436. }
  437. //封装 给plc的调度请求
  438. Error_manager Dispatch_plc::encapsulate_dispatch_request_to_plc()
  439. {
  440. // //把m_dispatch_request_msg的信息传给本地的数据缓存.
  441. // std::unique_lock<std::mutex> t_lock(m_lock);
  442. // m_request_key = m_dispatch_request_msg.command_key();
  443. // m_request_status = 1;
  444. // if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  445. // {
  446. // m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
  447. // m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_INLET;
  448. // m_request_passageway_id = m_dispatch_request_msg.mutable_id_struct()->terminal_id()+1;//0~5入口终端号改为1~6
  449. // }
  450. // else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  451. // {
  452. // m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  453. // m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_OUTLET;
  454. // m_request_passageway_id = m_dispatch_request_msg.mutable_id_struct()->terminal_id()+1;//0~5入口终端号改为1~6
  455. //
  456. // }
  457. // else
  458. // {
  459. // m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_UNKNOWN;
  460. // m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN;
  461. // m_request_passageway_id = -1;
  462. // }
  463. //
  464. // if ( m_dispatch_request_msg.locate_information().locate_correct() )
  465. // {
  466. // m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  467. // m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  468. //#ifdef MEASURE_TO_PLC_CORRECTION
  469. // m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.3;//80~100改为-10~+10
  470. //#endif
  471. //#ifndef MEASURE_TO_PLC_CORRECTION
  472. // m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  473. //#endif
  474. // m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  475. // m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  476. // m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  477. // m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  478. // m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  479. // m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  480. // m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_x();
  481. // m_request_uniformed_car_y = m_dispatch_request_msg.locate_information().uniformed_car_y();
  482. // }
  483. // else
  484. // {
  485. // m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  486. // m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  487. //#ifdef MEASURE_TO_PLC_CORRECTION
  488. // m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.3;//80~100改为-10~+10
  489. //#endif
  490. //#ifndef MEASURE_TO_PLC_CORRECTION
  491. // m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  492. //#endif
  493. // m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  494. // m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  495. // m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  496. // m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  497. // m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  498. // m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  499. // m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_x();
  500. // m_request_uniformed_car_y = m_dispatch_request_msg.locate_information().uniformed_car_y();
  501. // }
  502. //
  503. // if ( m_dispatch_request_msg.parkspace_info_ex_size() ==1 )
  504. // {
  505. // m_request_parkingspace_index_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_index_id();
  506. // m_request_parkingspace_unit_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_unit_id()+1;//0~2单元号改为1~3
  507. // m_request_parkingspace_label_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_label_id();
  508. // m_request_parkingspace_floor_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_floor_id();
  509. // m_request_parkingspace_room_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_room_id();
  510. // //车位朝向, 小号朝前朝南, 大号朝后朝北
  511. // m_request_parkingspace_direction = (Common_data::Parkingspace_direction)m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_direction();
  512. //
  513. //
  514. // //20211220, utf-8 -> GBK
  515. // m_request_car_license = String_convert::utf8_to_gbk( m_dispatch_request_msg.parkspace_info_ex(0).car_info().car_numberplate() );
  516. //
  517. // m_request_car_type = (Common_data::Car_type)m_dispatch_request_msg.parkspace_info_ex(0).car_type();
  518. //
  519. // //20211207, huli add wheel_base for pickup_car
  520. // if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  521. // {
  522. // m_request_car_wheel_base = m_dispatch_request_msg.parkspace_info_ex(0).car_info().car_wheel_base();
  523. // m_request_car_wheel_width = m_dispatch_request_msg.parkspace_info_ex(0).car_info().car_wheel_width();
  524. // }
  525. // }
  526. // else
  527. // {
  528. // return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  529. // " m_dispatch_request_msg.parkspace_info_ex_size() !=1 error ");
  530. // }
  531. return Error_code::SUCCESS;
  532. }
  533. //封装 给plc的调度请求
  534. Error_manager Dispatch_plc::encapsulate_dispatch_request_to_plc_new()
  535. {
  536. //把m_dispatch_request_msg的信息传给本地的数据缓存.
  537. std::unique_lock<std::mutex> t_lock(m_lock);
  538. m_request_key = m_command_key;
  539. m_request_status = 1;
  540. if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE ||
  541. m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_REALLOCATE)
  542. {
  543. //基本信息
  544. m_request_dispatch_motion_direction = m_dispatch_process_type;
  545. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_INLET;
  546. m_request_passageway_id = m_park_table_msg.terminal_id();
  547. // m_request_passageway_id = m_terminal_id;
  548. //感测雷达信息, 存车专有
  549. // if ( m_park_table_msg.entrance_measure_info().border_statu() )
  550. if ( true )
  551. {
  552. m_request_car_center_x = m_park_table_msg.entrance_measure_info().cx();
  553. m_request_car_center_y = m_park_table_msg.entrance_measure_info().cy();
  554. m_request_car_angle = m_park_table_msg.entrance_measure_info().theta();
  555. m_request_car_front_theta = m_park_table_msg.entrance_measure_info().front_theta();
  556. m_request_car_length = m_park_table_msg.entrance_measure_info().length();
  557. m_request_car_width = m_park_table_msg.entrance_measure_info().width();
  558. m_request_car_height = m_park_table_msg.entrance_measure_info().height();
  559. m_request_car_wheel_base = m_park_table_msg.entrance_measure_info().wheelbase();
  560. m_request_car_wheel_width = m_park_table_msg.entrance_measure_info().width();
  561. m_request_uniformed_car_x = m_park_table_msg.entrance_measure_info().cx();
  562. m_request_uniformed_car_y = m_park_table_msg.entrance_measure_info().cy();
  563. }
  564. //车位信息
  565. m_request_parkingspace_index_id = m_park_table_msg.allocated_space_info().id();
  566. m_request_parkingspace_unit_id = m_park_table_msg.allocated_space_info().unit_id();
  567. int t_parkingspace_label_id = m_park_table_msg.allocated_space_info().id()%78;
  568. m_request_parkingspace_label_id = t_parkingspace_label_id;
  569. m_request_parkingspace_floor_id = m_park_table_msg.allocated_space_info().floor();
  570. m_request_parkingspace_room_id = m_park_table_msg.allocated_space_info().room_id();
  571. //车位朝向, 奇数朝南, 偶数朝北
  572. if ( m_request_parkingspace_index_id %2 == 1 )
  573. {
  574. m_request_parkingspace_direction = Common_data::Parkingspace_direction::PARKINGSPACE_DIRECTION_SOUTH;
  575. }
  576. else
  577. {
  578. m_request_parkingspace_direction = Common_data::Parkingspace_direction::PARKINGSPACE_DIRECTION_NORTH;
  579. }
  580. //20211220, utf-8 -> GBK
  581. m_request_car_license = String_convert::utf8_to_gbk( m_park_table_msg.car_number() );
  582. m_request_car_type = Common_data::judge_car_type_with_car_height(m_request_car_height);
  583. }
  584. else if( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP ||
  585. m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_REVOCATION)
  586. {
  587. m_request_dispatch_motion_direction = m_dispatch_process_type;
  588. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_OUTLET;
  589. m_request_passageway_id = m_pick_table_msg.terminal_id();
  590. // m_request_passageway_id = m_terminal_id;
  591. //20211207, 取车专有, 数据库保存的汽车信息, 轮距
  592. m_request_car_center_x = m_pick_table_msg.actually_measure_info().cx();
  593. m_request_car_center_y = m_pick_table_msg.actually_measure_info().cy();
  594. m_request_car_angle = m_pick_table_msg.actually_measure_info().theta();
  595. m_request_car_front_theta = m_pick_table_msg.actually_measure_info().front_theta();
  596. m_request_car_length = m_pick_table_msg.actually_measure_info().length();
  597. m_request_car_width = m_pick_table_msg.actually_measure_info().width();
  598. m_request_car_height = m_pick_table_msg.actually_measure_info().height();
  599. m_request_car_wheel_base = m_pick_table_msg.actually_measure_info().wheelbase();
  600. m_request_car_wheel_width = m_pick_table_msg.actually_measure_info().width();
  601. m_request_uniformed_car_x = m_pick_table_msg.actually_measure_info().cx();
  602. m_request_uniformed_car_y = m_pick_table_msg.actually_measure_info().cy();
  603. //车位信息
  604. m_request_parkingspace_index_id = m_pick_table_msg.actually_space_info().id();
  605. m_request_parkingspace_unit_id = m_pick_table_msg.actually_space_info().unit_id();
  606. int t_parkingspace_label_id = m_pick_table_msg.actually_space_info().id()%78;
  607. m_request_parkingspace_label_id = t_parkingspace_label_id;
  608. m_request_parkingspace_floor_id = m_pick_table_msg.actually_space_info().floor();
  609. m_request_parkingspace_room_id = m_pick_table_msg.actually_space_info().room_id();
  610. //车位朝向, 奇数朝南, 偶数朝北
  611. if ( m_request_parkingspace_index_id %2 == 1 )
  612. {
  613. m_request_parkingspace_direction = Common_data::Parkingspace_direction::PARKINGSPACE_DIRECTION_SOUTH;
  614. }
  615. else
  616. {
  617. m_request_parkingspace_direction = Common_data::Parkingspace_direction::PARKINGSPACE_DIRECTION_NORTH;
  618. }
  619. //20211220, utf-8 -> GBK
  620. m_request_car_license = String_convert::utf8_to_gbk( m_pick_table_msg.car_number() );
  621. m_request_car_type = Common_data::judge_car_type_with_car_height(m_request_car_height);
  622. }
  623. else
  624. {
  625. m_request_dispatch_motion_direction = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  626. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN;
  627. m_request_passageway_id = -1;
  628. }
  629. return Error_code::SUCCESS;
  630. }
  631. //更新plc通信
  632. Error_manager Dispatch_plc::update_dispatch_plc_communication()
  633. {
  634. std::unique_lock<std::mutex> t_lock(m_lock);
  635. std::unique_lock<std::mutex> t_lock1(Dispatch_communication::get_instance_references().m_data_lock);
  636. /*
  637. //请求消息, 调度->plc
  638. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  639. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  640. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  641. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  642. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  643. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  644. m_request_key = "123456789";
  645. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  646. static unsigned char index = 0;
  647. index++;
  648. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = index;
  649. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = 02;
  650. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = 03;
  651. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = 04;
  652. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = 05;
  653. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = 6;
  654. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = 7;
  655. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = 8;
  656. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = 11;
  657. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = 12;
  658. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = 13;
  659. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = 14;
  660. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = 15;
  661. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = 16;
  662. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = 17;
  663. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = 18;
  664. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = 19;
  665. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  666. int t_size2 = m_request_key.size()<=20 ? m_request_key.size() : 20 ;
  667. m_request_car_license = "ABCDEF";
  668. memcpy(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, m_request_car_license.c_str(), t_size2);
  669. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = 33;
  670. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = 44;
  671. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = 55;
  672. */
  673. //请求消息, 调度->plc
  674. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  675. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  676. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  677. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  678. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  679. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  680. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  681. tp_dispatch_request_from_manager_to_plc_for_data->m_request_status = m_request_status;
  682. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = m_request_dispatch_motion_direction;
  683. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = m_request_passageway_id;
  684. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = m_request_passageway_direction;
  685. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = m_request_parkingspace_index_id;
  686. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = m_request_parkingspace_unit_id;
  687. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_label_id = m_request_parkingspace_label_id;
  688. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = m_request_parkingspace_floor_id;
  689. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = m_request_parkingspace_room_id;
  690. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = m_request_parkingspace_direction;
  691. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = m_request_car_center_x;
  692. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = m_request_car_center_y;
  693. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = m_request_car_angle;
  694. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = m_request_car_front_theta;
  695. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = m_request_car_length;
  696. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = m_request_car_width;
  697. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = m_request_car_height;
  698. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = m_request_car_wheel_base;
  699. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = m_request_car_wheel_width;
  700. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  701. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license[0] = 18;
  702. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license[1] = m_request_car_license.length();
  703. unsigned char * p_ch = tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license;
  704. memcpy(p_ch+2, m_request_car_license.c_str(), m_request_car_license.length());
  705. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = m_request_car_type;
  706. tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_x = m_request_uniformed_car_x;
  707. tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_y = m_request_uniformed_car_y;
  708. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = m_request_anticollision_lidar_flag;
  709. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = m_request_car_wheel_base_exact_value;
  710. //答复消息, plc->调度
  711. Dispatch_communication::Dispatch_response_from_plc_to_manager * tp_dispatch_response_from_plc_to_manager =
  712. & Dispatch_communication::get_instance_references().m_dispatch_response_from_plc_to_manager;
  713. m_response_key = (char*) tp_dispatch_response_from_plc_to_manager->m_response_key;
  714. m_response_status = (Dispatch_plc::Response_status)tp_dispatch_response_from_plc_to_manager->m_response_status;
  715. m_response_working_total_time = tp_dispatch_response_from_plc_to_manager->m_response_working_total_time;
  716. m_response_working_remaining_time = tp_dispatch_response_from_plc_to_manager->m_response_working_remaining_time;
  717. m_response_dispatch_motion_direction = (Common_data::Dispatch_process_type)tp_dispatch_response_from_plc_to_manager->m_response_dispatch_motion_direction;
  718. m_response_passageway_id = tp_dispatch_response_from_plc_to_manager->m_response_passageway_id;
  719. m_response_passageway_direction = (Common_data::Passageway_direction)tp_dispatch_response_from_plc_to_manager->m_response_passageway_direction;
  720. m_response_parkingspace_index_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_index_id;
  721. m_response_parkingspace_unit_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_unit_id;
  722. m_response_parkingspace_label_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_label_id;
  723. m_response_parkingspace_floor_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_floor_id;
  724. m_response_parkingspace_room_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_room_id;
  725. m_response_parkingspace_direction = (Common_data::Parkingspace_direction)tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_direction;
  726. m_response_car_center_x = tp_dispatch_response_from_plc_to_manager->m_response_car_center_x;
  727. m_response_car_center_y = tp_dispatch_response_from_plc_to_manager->m_response_car_center_y;
  728. m_response_car_angle = tp_dispatch_response_from_plc_to_manager->m_response_car_angle;
  729. m_response_car_front_theta = tp_dispatch_response_from_plc_to_manager->m_response_car_front_theta;
  730. m_response_car_length = tp_dispatch_response_from_plc_to_manager->m_response_car_length;
  731. m_response_car_width = tp_dispatch_response_from_plc_to_manager->m_response_car_width;
  732. m_response_car_height = tp_dispatch_response_from_plc_to_manager->m_response_car_height;
  733. m_response_car_wheel_base = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base;
  734. m_response_car_wheel_width = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_width;
  735. m_response_car_license = (char*) (tp_dispatch_response_from_plc_to_manager->m_response_car_license+2);
  736. m_response_car_type = (Common_data::Car_type) tp_dispatch_response_from_plc_to_manager->m_response_car_type;
  737. m_response_uniformed_car_x = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_x;
  738. m_response_uniformed_car_y = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_y;
  739. m_response_anticollision_lidar_flag = (Common_data::Anticollision_lidar_flag)tp_dispatch_response_from_plc_to_manager->m_response_anticollision_lidar_flag;
  740. m_response_car_wheel_base_exact_value = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base_exact_value;
  741. /*
  742. std::cout << " huli test :::: " << " ------------------------------------------------------ = " << std::endl;
  743. std::cout << " huli test :::: " << " m_response_key = " << m_response_key << std::endl;
  744. std::cout << " huli test :::: " << " m_response_status = " << (int)m_response_status << std::endl;
  745. std::cout << " huli test :::: " << " m_response_dispatch_motion_direction = " << (int)m_response_dispatch_motion_direction << std::endl;
  746. std::cout << " huli test :::: " << " m_response_passageway_id = " << m_response_passageway_id << std::endl;
  747. std::cout << " huli test :::: " << " m_response_passageway_direction = " << (int)m_response_passageway_direction << std::endl;
  748. std::cout << " huli test :::: " << " m_response_parkingspace_index_id = " << m_response_parkingspace_index_id << std::endl;
  749. std::cout << " huli test :::: " << " m_response_parkingspace_unit_id = " << m_response_parkingspace_unit_id << std::endl;
  750. std::cout << " huli test :::: " << " m_response_parkingspace_floor_id = " << m_response_parkingspace_floor_id << std::endl;
  751. std::cout << " huli test :::: " << " m_response_parkingspace_room_id = " << m_response_parkingspace_room_id << std::endl;
  752. std::cout << " huli test :::: " << " m_response_parkingspace_direction = " << (int)m_response_parkingspace_direction << std::endl;
  753. std::cout << " huli test :::: " << " m_response_car_center_x = " << m_response_car_center_x << std::endl;
  754. std::cout << " huli test :::: " << " m_response_car_center_y = " << m_response_car_center_y << std::endl;
  755. std::cout << " huli test :::: " << " m_response_car_license = " << m_response_car_license << std::endl;
  756. */
  757. //状态消息, 调度->plc
  758. Dispatch_communication::Dispatch_plc_status_from_manager_to_plc * tp_dispatch_plc_status_from_manager_to_plc =
  759. & Dispatch_communication::get_instance_references().m_dispatch_plc_status_from_manager_to_plc;
  760. m_dispatch_heartbeat++;
  761. tp_dispatch_plc_status_from_manager_to_plc->m_dispatch_heartbeat = m_dispatch_heartbeat;
  762. //状态消息, plc->调度
  763. Dispatch_communication::Dispatch_plc_status_from_plc_to_manager * tp_dispatch_plc_status_from_plc_to_manager =
  764. & Dispatch_communication::get_instance_references().m_dispatch_plc_status_from_plc_to_manager;
  765. m_plc_heartbeat = tp_dispatch_plc_status_from_plc_to_manager->m_plc_heartbeat;
  766. m_plc_status_info = tp_dispatch_plc_status_from_plc_to_manager->m_plc_status_info;
  767. m_turnplate_angle_min1 = tp_dispatch_plc_status_from_plc_to_manager->m_turnplate_angle_min1;
  768. m_turnplate_angle_max1 = tp_dispatch_plc_status_from_plc_to_manager->m_turnplate_angle_max1;
  769. m_turnplate_angle_min2 = tp_dispatch_plc_status_from_plc_to_manager->m_turnplate_angle_min2;
  770. m_turnplate_angle_max2 = tp_dispatch_plc_status_from_plc_to_manager->m_turnplate_angle_max2;
  771. //need
  772. //通过心跳帧来判断通信是否正常
  773. // if ( m_last_heartbeat != tp_dispatch_plc_status_from_plc_to_manager->m_plc_heartbeat )
  774. // {
  775. // m_last_heartbeat = tp_dispatch_plc_status_from_plc_to_manager->m_plc_heartbeat;
  776. // m_status_updata_time = std::chrono::system_clock::now();
  777. //
  778. // if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT )
  779. // {
  780. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY;
  781. // }
  782. // }
  783. // else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(COMMUNICATION_OVER_TIME_MS))
  784. // {
  785. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT;
  786. // }
  787. //else 继续等待,直到消息刷新或者超时.
  788. return Error_code::SUCCESS;
  789. }
  790. //检查plc答复反馈
  791. Error_manager Dispatch_plc::check_response_from_plc()
  792. {
  793. // std::unique_lock<std::mutex> t_lock(m_lock);
  794. //
  795. //
  796. //#ifndef WAIT_PLC_RESPONSE
  797. // //need, 调试代码, 延时10s, 直接返回成功
  798. // if ( std::chrono::system_clock::now() - m_start_time > std::chrono::seconds(20) )
  799. // {
  800. // return Error_code::SUCCESS;
  801. // }
  802. // //返回没有收到数据
  803. // else
  804. // {
  805. // return Error_code::NODATA;
  806. // }
  807. //#endif
  808. //
  809. //
  810. //#ifdef WAIT_PLC_RESPONSE
  811. //
  812. // static bool s_update_flag = false;
  813. // //检查唯一码
  814. // if ( m_response_key == m_request_key )
  815. // {
  816. // //第一次同步时,刷新时间
  817. // if ( s_update_flag == false )
  818. // {
  819. // s_update_flag = true;
  820. // if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  821. // {
  822. // std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  823. // Dispatch_manager::get_instance_references().m_store_updata_time = std::chrono::system_clock::now();
  824. // }
  825. // else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  826. // {
  827. // std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  828. // Dispatch_manager::get_instance_references().m_pickup_updata_time = std::chrono::system_clock::now();
  829. // }
  830. // }
  831. //
  832. //
  833. // if(m_response_status == RESPONS_WORKING)
  834. // {
  835. // return Error_code::NODATA;//返回没有收到数据
  836. // }
  837. // else
  838. // {
  839. // std::cout<< "m_response_status = " << m_response_status << std::endl;
  840. // //如果故障,则添加错误码
  841. // if ( m_response_status != RESPONS_OVER )
  842. // {
  843. // //添加错误码
  844. // Error_manager t_error(DISPATCH_PLC_RESPONS_ERROR, MINOR_ERROR, " Dispatch_plc::check_response_from_plc() m_respons_status is error");
  845. // return t_error;
  846. // }
  847. //
  848. // return Error_code::SUCCESS;
  849. // }
  850. //
  851. // }
  852. // else
  853. // {
  854. // s_update_flag = false;
  855. // return Error_code::NODATA;//返回没有收到数据
  856. // }
  857. //#endif
  858. return Error_code::SUCCESS;
  859. }
  860. //检查plc答复反馈
  861. Error_manager Dispatch_plc::check_response_from_plc_new()
  862. {
  863. std::unique_lock<std::mutex> t_lock(m_lock);
  864. #ifndef WAIT_PLC_RESPONSE
  865. //need, 调试代码, 延时10s, 直接返回成功
  866. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::seconds(10) )
  867. {
  868. return Error_code::SUCCESS;
  869. }
  870. //返回没有收到数据
  871. else
  872. {
  873. return Error_code::NODATA;
  874. }
  875. #endif
  876. #ifdef WAIT_PLC_RESPONSE
  877. static bool s_update_flag = false;
  878. //检查唯一码
  879. if ( m_response_key == m_request_key )
  880. {
  881. //第一次同步时,刷新时间
  882. if ( s_update_flag == false )
  883. {
  884. s_update_flag = true;
  885. // if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  886. // {
  887. // std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  888. // Dispatch_manager::get_instance_references().m_store_updata_time = std::chrono::system_clock::now();
  889. // }
  890. // else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  891. // {
  892. // std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  893. // Dispatch_manager::get_instance_references().m_pickup_updata_time = std::chrono::system_clock::now();
  894. // }
  895. }
  896. if(m_response_status == RESPONS_WORKING)
  897. {
  898. return Error_code::NODATA;//返回没有收到数据
  899. }
  900. else
  901. {
  902. std::cout<< "m_response_status = " << m_response_status << std::endl;
  903. //如果故障,则添加错误码
  904. if ( m_response_status == RESPONS_OVER )
  905. {
  906. return Error_code::SUCCESS;
  907. }
  908. else if(m_response_status == RESPONS_REALLOCATE_MIN_CAR)
  909. {
  910. //重新分配小车
  911. return DISPATCH_PLC_REALLOCATE_MIN_CAR;
  912. }
  913. else if(m_response_status == RESPONS_REALLOCATE_MID_CAR)
  914. {
  915. //重新分配中车
  916. return DISPATCH_PLC_REALLOCATE_MID_CAR;
  917. }
  918. else if(m_response_status == RESPONS_REALLOCATE_BIG_CAR)
  919. {
  920. //重新分配大车
  921. return DISPATCH_PLC_REALLOCATE_BIG_CAR;
  922. }
  923. else if(m_response_status == RESPONS_REALLOCATE_HUGE_CAR)
  924. {
  925. //重新分配fault
  926. return DISPATCH_PLC_REALLOCATE_HUGE_CAR;
  927. }
  928. else if(m_response_status == RESPONS_REALLOCATE_FAULT_CAR)
  929. {
  930. //重新分配fault
  931. return DISPATCH_PLC_REALLOCATE_FAULT_CAR;
  932. }
  933. else //如果故障,则添加错误码
  934. {
  935. //添加错误码
  936. Error_manager t_error(DISPATCH_PLC_RESPONS_ERROR, MINOR_ERROR, " Dispatch_plc::check_response_from_plc() m_respons_status is error");
  937. return t_error;
  938. }
  939. }
  940. }
  941. else
  942. {
  943. s_update_flag = false;
  944. return Error_code::NODATA;//返回没有收到数据
  945. }
  946. #endif
  947. return Error_code::SUCCESS;
  948. }
  949. //发送调度答复信息给主控
  950. Error_manager Dispatch_plc::send_dispatch_response_to_main_control()
  951. {
  952. // std::unique_lock<std::mutex> t_lock(m_lock);
  953. // m_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  954. // m_dispatch_response_msg.mutable_base_info()->set_timeout_ms(m_dispatch_request_msg.base_info().timeout_ms());
  955. // m_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  956. // m_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  957. //
  958. // m_dispatch_response_msg.set_command_key(m_dispatch_request_msg.command_key());
  959. // m_dispatch_response_msg.set_dispatch_motion_direction(m_dispatch_request_msg.dispatch_motion_direction());
  960. // m_dispatch_response_msg.mutable_id_struct()->set_terminal_id(m_dispatch_request_msg.mutable_id_struct()->terminal_id());
  961. // m_dispatch_response_msg.mutable_id_struct()->set_unit_id(m_dispatch_request_msg.mutable_id_struct()->unit_id());
  962. // m_dispatch_response_msg.mutable_locate_information()->CopyFrom(m_dispatch_request_msg.locate_information());
  963. // m_dispatch_response_msg.mutable_parkspace_info_ex()->CopyFrom(m_dispatch_request_msg.parkspace_info_ex());
  964. // m_dispatch_response_msg.set_car_type(m_dispatch_request_msg.car_type());
  965. //
  966. // if ( m_dispatch_response_msg.dispatch_motion_direction() == message::E_STORE_CAR )
  967. // {
  968. // for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  969. // {
  970. // if ( m_result == Error_code::SUCCESS )
  971. // {
  972. // m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  973. // }
  974. // else
  975. // {
  976. // m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  977. // }
  978. // }
  979. // }
  980. // else if( m_dispatch_response_msg.dispatch_motion_direction() == message::E_PICKUP_CAR )
  981. // {
  982. // for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  983. // {
  984. // if ( m_result == Error_code::SUCCESS )
  985. // {
  986. // m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  987. // }
  988. // else
  989. // {
  990. // m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  991. // }
  992. // }
  993. // }
  994. //
  995. //
  996. // m_dispatch_response_msg.mutable_error_manager()->set_error_code(m_result.get_error_code());
  997. // m_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)m_result.get_error_level());
  998. // m_dispatch_response_msg.mutable_error_manager()->set_error_description(m_result.get_error_description());
  999. //
  1000. //
  1001. // std::cout << " huli test :::: " << " m_dispatch_response_msg = " << m_dispatch_response_msg.DebugString()<< std::endl;
  1002. //
  1003. // std::string t_msg = m_dispatch_response_msg.SerializeAsString();
  1004. // System_communication::get_instance_references().encapsulate_task_msg(t_msg);
  1005. return Error_code::SUCCESS;
  1006. }
  1007. //发送调度答复信息给主控
  1008. Error_manager Dispatch_plc::send_dispatch_response_to_main_control_new()
  1009. {
  1010. // std::unique_lock<std::mutex> t_lock(m_lock);
  1011. //
  1012. // std::string t_msg;
  1013. // if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE )
  1014. // {
  1015. // measure_info t_measure_info = m_park_table_msg.entrance_measure_info();
  1016. // m_park_table_msg.mutable_actually_measure_info()->CopyFrom(t_measure_info);
  1017. // parkspace_info t_parkspace_info = m_park_table_msg.allocated_space_info();
  1018. // m_park_table_msg.mutable_actually_space_info()->CopyFrom(t_parkspace_info);
  1019. // //存车真实轴距
  1020. // m_park_table_msg.mutable_actually_measure_info()->set_wheelbase(m_response_car_wheel_base_exact_value);
  1021. //
  1022. // if ( m_result != Error_code::SUCCESS )
  1023. // {
  1024. // std::string t_error_string = m_result.to_string();
  1025. // m_park_table_msg.mutable_statu()->set_execute_statu(eError);
  1026. // m_park_table_msg.mutable_statu()->set_statu_description(t_error_string);
  1027. // }
  1028. // t_msg = m_park_table_msg.DebugString();
  1029. // }
  1030. // else if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP )
  1031. // {
  1032. // m_pick_table_msg.set_export_id(1);
  1033. // if ( m_result != Error_code::SUCCESS )
  1034. // {
  1035. // std::string t_error_string = m_result.to_string();
  1036. // m_pick_table_msg.mutable_statu()->set_execute_statu(eError);
  1037. // m_pick_table_msg.mutable_statu()->set_statu_description(t_error_string);
  1038. // }
  1039. // t_msg = m_pick_table_msg.DebugString();
  1040. // }
  1041. //
  1042. // System_communication::get_instance_references().encapsulate_task_msg(t_msg, 1);
  1043. // std::cout << " huli test :::: " << " m_command_completed_msg = " << t_msg << std::endl;
  1044. // Dispatch_manager::get_instance_references().ack_rabbitmq_message_new();
  1045. return Error_code::SUCCESS;
  1046. }