dispatch_plc.cpp 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  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. #include <stdlib.h>
  8. Dispatch_plc::Dispatch_plc()
  9. {
  10. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_UNKNOW;
  11. m_plc_id = 0;
  12. mp_execute_thread = NULL;
  13. mp_data_storage_thread = NULL;
  14. }
  15. Dispatch_plc::~Dispatch_plc()
  16. {
  17. dispatch_plc_uninit();
  18. }
  19. //调度plc 初始化
  20. Error_manager Dispatch_plc::dispatch_plc_init(int plc_id)
  21. {
  22. m_plc_id = plc_id;
  23. m_last_update_time = std::chrono::system_clock::now();
  24. m_last_heartbeat = 0;
  25. // 线程默认开启
  26. m_execute_condition.reset(false, true, false);
  27. mp_execute_thread = new std::thread(&Dispatch_plc::execute_thread_fun, this);
  28. // m_data_storage_condition.reset(false, true, false);
  29. // mp_data_storage_thread = new std::thread(&Dispatch_plc::data_storage_thread_fun, this);
  30. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY;
  31. return Error_code::SUCCESS;
  32. }
  33. //调度plc 反初始化
  34. Error_manager Dispatch_plc::dispatch_plc_uninit()
  35. {
  36. if (mp_execute_thread)
  37. {
  38. m_execute_condition.kill_all();
  39. }
  40. if (mp_data_storage_thread)
  41. {
  42. m_data_storage_condition.kill_all();
  43. }
  44. if (mp_execute_thread)
  45. {
  46. mp_execute_thread->join();
  47. delete mp_execute_thread;
  48. mp_execute_thread = NULL;
  49. }
  50. if (mp_data_storage_thread)
  51. {
  52. mp_data_storage_thread->join();
  53. delete mp_data_storage_thread;
  54. mp_data_storage_thread = NULL;
  55. }
  56. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_UNKNOW;
  57. return Error_code::SUCCESS;
  58. }
  59. //调度plc 执行请求
  60. //Error_manager Dispatch_plc::execute_for_dispatch_request_msg(message::Dispatch_request_msg& dispatch_request_msg)
  61. //{
  62. //
  63. // if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  64. // {
  65. // std::unique_lock<std::mutex> t_lock(m_lock);
  66. //
  67. // //设定超时时间, 默认比任务指令里面的时间少10秒,
  68. // if ( dispatch_request_msg.base_info().has_timeout_ms() )
  69. // {
  70. // m_timeout_ms = dispatch_request_msg.base_info().timeout_ms() - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
  71. // }
  72. // else
  73. // {
  74. // m_timeout_ms = DISPATCH_PROCESS_TIMEOUT_MS - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
  75. // }
  76. // m_command_key = dispatch_request_msg.command_key();
  77. // m_start_time = std::chrono::system_clock::now();
  78. //
  79. //
  80. //// m_dispatch_request_msg = dispatch_request_msg;
  81. //// if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  82. //// {
  83. //// m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
  84. //// }
  85. //// else if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  86. //// {
  87. //// m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
  88. //// }
  89. //// else
  90. //// {
  91. //// return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  92. //// " dispatch_request_msg.dispatch_motion_direction() PARAMRTER ERROR ");
  93. //// }
  94. //
  95. // m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  96. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST;
  97. // }
  98. // else
  99. // {
  100. // return Error_manager(Error_code::DISPATCH_PLC_STATUS_ERROR, Error_level::MINOR_ERROR,
  101. // " m_dispatch_plc_status error ");
  102. // }
  103. //
  104. // return Error_code::SUCCESS;
  105. //}
  106. //新版调度plc 执行请求 存车
  107. 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)
  108. {
  109. if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  110. {
  111. std::unique_lock<std::mutex> t_lock(m_lock);
  112. //超时默认一小时.
  113. m_timeout_ms = DISPATCH_PROCESS_TIMEOUT_MS - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
  114. //合成唯一码.
  115. // char t_buf[256] = {0};
  116. // 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());
  117. // m_command_key = park_table_msg.primary_key()+"_park";
  118. m_start_time = std::chrono::system_clock::now();
  119. m_park_table_msg = park_table_msg;
  120. m_dispatch_process_type = dispatch_process_type;
  121. m_car_type = car_type;
  122. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
  123. {
  124. m_command_key = park_table_msg.primary_key()+"_store";
  125. }
  126. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
  127. {
  128. char buf[256] = {0};
  129. sprintf(buf, "_%d", Dispatch_manager::get_instance_references().m_dispatch_command.m_dispatch_space_info_count);
  130. m_command_key = park_table_msg.primary_key()+"_reallocate"+buf;
  131. }
  132. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST;
  133. }
  134. else
  135. {
  136. return Error_manager(Error_code::DISPATCH_PLC_STATUS_ERROR, Error_level::MINOR_ERROR,
  137. " m_dispatch_plc_status error ");
  138. }
  139. return Error_code::SUCCESS;
  140. }
  141. //新版调度plc 执行请求 取车
  142. 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)
  143. {
  144. if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  145. {
  146. std::unique_lock<std::mutex> t_lock(m_lock);
  147. //超时默认一小时.
  148. m_timeout_ms = DISPATCH_PROCESS_TIMEOUT_MS - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
  149. //合成唯一码.
  150. // char t_buf[256] = {0};
  151. // 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());
  152. // m_command_key = pick_table_msg.primary_key()+"_pick";
  153. m_start_time = std::chrono::system_clock::now();
  154. m_pick_table_msg = pick_table_msg;
  155. m_dispatch_process_type = dispatch_process_type;
  156. m_car_type = car_type;
  157. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
  158. {
  159. m_command_key = pick_table_msg.primary_key()+"_pickup";
  160. }
  161. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
  162. {
  163. m_command_key = pick_table_msg.primary_key()+"_revocation";
  164. }
  165. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST;
  166. }
  167. else
  168. {
  169. return Error_manager(Error_code::DISPATCH_PLC_STATUS_ERROR, Error_level::MINOR_ERROR,
  170. " m_dispatch_plc_status error ");
  171. }
  172. return Error_code::SUCCESS;
  173. }
  174. Dispatch_plc::Dispatch_plc_status Dispatch_plc::get_dispatch_plc_status()
  175. {
  176. return m_dispatch_plc_status;
  177. }
  178. //message::Dispatch_request_msg Dispatch_plc::get_dispatch_request_msg()
  179. //{
  180. // std::unique_lock<std::mutex> t_lock(m_lock);
  181. // return m_dispatch_request_msg;
  182. //}
  183. Error_manager Dispatch_plc::get_result()
  184. {
  185. return m_result;
  186. }
  187. float Dispatch_plc::get_response_working_total_time()
  188. {
  189. std::unique_lock<std::mutex> t_lock(m_lock);
  190. return m_response_working_total_time;
  191. }
  192. float Dispatch_plc::get_response_working_remaining_time()
  193. {
  194. std::unique_lock<std::mutex> t_lock(m_lock);
  195. return m_response_working_remaining_time;
  196. }
  197. void Dispatch_plc::clear_request_msg()
  198. {
  199. m_request_key.clear(); //请求唯一码, 用作识别
  200. m_request_status = 0; //请求确认标志
  201. // 调度指令的起点,终点,方向
  202. m_request_dispatch_motion_direction = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW; //调度方向 0=未知,1=存车,2=取车
  203. m_request_passageway_id = 0; //出入口id 6个入口和6个出口
  204. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN; //出入口方向 0=未知,1=入口,2=出口
  205. m_request_car_height_level = 0;
  206. m_request_parkingspace_index_id = 0; //楼上车位索引id 1~180(3*6*13)
  207. m_request_parkingspace_unit_id = 0; //楼上车位单元号 1~3
  208. m_request_parkingspace_label_id = 0; //楼上车位标签号 1~78
  209. m_request_parkingspace_floor_id = 0; //楼上车位楼层号 2~11
  210. m_request_parkingspace_room_id = 0; //楼上车位房间号 1~6
  211. m_request_parkingspace_direction = Common_data::Parkingspace_direction::PARKINGSPACE_DIRECTION_UNKNOWN; //楼上车位方向 0=未知,1=朝南,2=朝北
  212. //汽车的定位信息(地面雷达)
  213. m_request_car_center_x = 0; //整车的中心点x值, 四轮的中心, 单位:米 m
  214. m_request_car_center_y = 0; //整车的中心点y值, 四轮的中心, 单位:米 m
  215. m_request_car_angle = 0; //整车的车身旋转角, 单位:度 (-90~90)
  216. m_request_car_front_theta = 0; //整车的前轮的旋转角, 单位:度 (-90~90)
  217. m_request_car_length = 0; //整车的长度, 用于规避碰撞, 单位:米 m
  218. m_request_car_width = 0; //整车的宽度, 用于规避碰撞, 单位:米 m
  219. m_request_car_height = 0; //整车的高度, 用于规避碰撞, 单位:米 m
  220. m_request_car_wheel_base = 0; //整车的轮距, 前后轮的距离, 用于机器人或agv的抓车, 单位:米 m
  221. m_request_car_wheel_width = 0; //整车的轮距, 左右轮的距离, 用于机器人或agv的抓车, 单位:米 m
  222. m_request_car_license.clear(); //车牌号(汽车唯一标示) 例如: 鄂A12345
  223. m_request_car_type = Common_data::Car_type::UNKNOW_CAR_TYPE; //车的大小
  224. m_request_uniformed_car_x = 0; //转角复位后,车辆中心点x
  225. m_request_uniformed_car_y = 0; //转角复位后,车辆中心点y
  226. //防撞雷达
  227. m_request_anticollision_lidar_flag = Common_data::Anticollision_lidar_flag::ANTICOLLISION_LIDAR_UNKNOWN; //汽车是否停到正确的位置, 防撞雷达 预留, 楚天暂时不用,0=未知,1=位置正常,2=位置异常
  228. //轮距雷达
  229. m_request_car_wheel_base_exact_value = 0; //汽车前后轮距,轮距雷达的精确值 预留, 楚天暂时不用,单位:米 m
  230. }
  231. //jiancha qingqiu xiaoxi
  232. //Error_manager Dispatch_plc::check_dispatch_request_msg(message::Dispatch_request_msg & dispatch_request_msg)
  233. //{
  234. // return Error_code::SUCCESS;
  235. // /*
  236. // Error_manager t_error;
  237. //
  238. // if(dispatch_request_msg.has_locate_information() &&
  239. // dispatch_request_msg.has_dispatch_motion_direction() &&
  240. // dispatch_request_msg.has_car_type() &&
  241. // dispatch_request_msg.has_id_struct() &&
  242. // dispatch_request_msg.has_command_key())
  243. // {
  244. // if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  245. // {
  246. // if ( dispatch_request_msg.id_struct().has_unit_id() )
  247. // {
  248. //
  249. // }
  250. // }
  251. // else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  252. //
  253. // return Error_code::SUCCESS;
  254. // }
  255. // else
  256. // {
  257. // return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  258. // " m_dispatch_plc_status error ");
  259. // }
  260. // */
  261. // return Error_code::SUCCESS;
  262. //}
  263. //控制外门, 调度发给plc的状态,
  264. //door_index 1~2是入口外门, 3~4是出口外门
  265. //door_mode 0是无效, 1是开门, 2是关门
  266. Error_manager Dispatch_plc::control_ouside_door(int door_index, int door_mode)
  267. {
  268. switch ( door_index )
  269. {
  270. case 1:
  271. {
  272. m_dispatch_plc_status_from_manager_to_plc.m_inlet_door_control_1 = door_mode;
  273. break;
  274. }
  275. case 2:
  276. {
  277. m_dispatch_plc_status_from_manager_to_plc.m_inlet_door_control_2 = door_mode;
  278. break;
  279. }
  280. case 3:
  281. {
  282. m_dispatch_plc_status_from_manager_to_plc.m_outlet_door_control_3 = door_mode;
  283. break;
  284. }
  285. case 4:
  286. {
  287. m_dispatch_plc_status_from_manager_to_plc.m_outlet_door_control_4 = door_mode;
  288. break;
  289. }
  290. default:
  291. {
  292. break;
  293. }
  294. }
  295. return Error_code::SUCCESS;
  296. }
  297. //获取plc搬运器的状态 0=故障 1=存车 2=取车 3=空闲 4=维护
  298. int Dispatch_plc::get_plc_carrier_status()
  299. {
  300. return m_dispatch_plc_status_from_plc_to_manager.m_plc_carrier_status;
  301. }
  302. //执行外界任务的执行函数
  303. void Dispatch_plc::execute_thread_fun()
  304. {
  305. LOG(INFO) << " Dispatch_plc::execute_thread_fun() start " << this;
  306. Error_manager t_error;
  307. while (m_execute_condition.is_alive())
  308. {
  309. m_execute_condition.wait();
  310. if (m_execute_condition.is_alive())
  311. {
  312. std::this_thread::sleep_for(std::chrono::microseconds(1));
  313. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  314. // std::this_thread::sleep_for(std::chrono::milliseconds(10));
  315. // std::this_thread::sleep_for(std::chrono::seconds(1));
  316. std::this_thread::yield();
  317. // std::unique_lock<std::mutex> t_lock(m_lock);
  318. static int t_count = 0;
  319. if (t_count % 1000 == 0 )
  320. {
  321. std::cout << " huli test :::: " << " m_plc_id = " << m_plc_id << std::endl;
  322. std::cout << " huli test :::: " << " m_dispatch_plc_status = " << m_dispatch_plc_status << std::endl;
  323. }
  324. t_count++;
  325. switch ((Dispatch_plc_status) m_dispatch_plc_status)
  326. {
  327. case DISPATCH_PLC_READY:
  328. {
  329. if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW )
  330. {
  331. update_dispatch_plc_communication();
  332. }
  333. else
  334. {
  335. // m_result = check_dispatch_request_msg(m_dispatch_request_msg);
  336. //收到新指令, 进入工作阶段,强制清空错误码.
  337. m_result.error_manager_clear_all();
  338. if(m_result == Error_code::SUCCESS)
  339. {
  340. m_dispatch_plc_status = DISPATCH_PLC_REQUEST;
  341. }
  342. else
  343. {
  344. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  345. }
  346. }
  347. break;
  348. }
  349. case DISPATCH_PLC_REQUEST://给plc发送请求
  350. {
  351. //收到新指令, 进入工作阶段,强制清空错误码.
  352. m_result.error_manager_clear_all();
  353. #ifdef PLC_OVER_TIME_TO_ERROR
  354. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  355. {
  356. //超时直接报错
  357. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  358. " DISPATCH_PLC_TIME_OUT fun error ");
  359. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  360. }
  361. else
  362. #endif
  363. {
  364. //封装 给plc的调度请求
  365. // m_result = encapsulate_dispatch_request_to_plc();
  366. m_result = encapsulate_dispatch_request_to_plc_new();
  367. test_start_time = std::chrono::system_clock::now();
  368. m_result = Error_code::SUCCESS;
  369. if ( m_result == Error_code::SUCCESS )
  370. {
  371. m_dispatch_plc_status = DISPATCH_PLC_WORKING;
  372. }
  373. else
  374. {
  375. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  376. }
  377. }
  378. break;
  379. }
  380. case DISPATCH_PLC_WORKING:
  381. {
  382. #ifdef PLC_OVER_TIME_TO_ERROR
  383. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  384. {
  385. //超时直接报错
  386. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  387. " DISPATCH_PLC_TIME_OUT fun error ");
  388. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  389. }
  390. else
  391. #endif
  392. {
  393. update_dispatch_plc_communication();
  394. //检查plc答复反馈
  395. // m_result = check_response_from_plc();
  396. m_result = check_response_from_plc_new();
  397. if ( m_result == Error_code::SUCCESS )
  398. {
  399. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  400. }
  401. else if ( m_result != Error_code::NODATA )
  402. {
  403. // m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  404. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  405. }
  406. //else NODATA 原地等待
  407. }
  408. break;
  409. }
  410. case DISPATCH_PLC_RESPONSE:
  411. {
  412. test_end_time = std::chrono::system_clock::now();
  413. auto time_dur = test_end_time - test_start_time;
  414. if ( time_dur < std::chrono::seconds(2) )
  415. {
  416. LOG(INFO) << " 调度完成, 时间太短, 按任意键继续 = "<< time_dur.count() << " --- " << this;
  417. // getchar();
  418. }
  419. //20211213. huli m_request_status = 0 after over process
  420. m_request_status = 0;
  421. clear_request_msg();
  422. //发送调度答复信息给主控
  423. // send_dispatch_response_to_main_control();
  424. // send_dispatch_response_to_main_control_new();
  425. //无论前面的流程是否正确,都要给主控答复, 反馈里面填充错误码即可.
  426. //最新版本不用答复消息, Dispatch_plc 切回 DISPATCH_PLC_READY 后, 由 Dispatch_manager 答复数据库
  427. if ( m_result == Error_code::SUCCESS )
  428. {
  429. /*
  430. //response add to _map
  431. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  432. {
  433. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  434. std::chrono::system_clock::time_point t_time = std::chrono::system_clock::now();
  435. Dispatch_manager::get_instance_references().m_dispatch_response_store_map[t_time] = m_dispatch_response_msg;
  436. Dispatch_manager::get_instance_references().m_store_updata_time = std::chrono::system_clock::now();
  437. }
  438. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  439. {
  440. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  441. int t_terminal_id = m_dispatch_request_msg.id_struct().terminal_id();
  442. Dispatch_manager::get_instance_references().m_dispatch_response_pickup_map[t_terminal_id] = m_dispatch_response_msg;
  443. Dispatch_manager::get_instance_references().m_pickup_updata_time = std::chrono::system_clock::now();
  444. }
  445. */
  446. //流程正常结束, 恢复到待机状态.
  447. m_dispatch_plc_status = DISPATCH_PLC_READY;
  448. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  449. // m_result.error_manager_clear_all();
  450. }
  451. else
  452. {
  453. //流程异常结束, 进入到故障状态.
  454. m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  455. }
  456. break;
  457. }
  458. case DISPATCH_PLC_FAULT:
  459. case DISPATCH_PLC_DISCONNECT:
  460. {
  461. //20211213. huli m_request_status = 0 after over process
  462. m_request_status = 0;
  463. clear_request_msg();
  464. //不在接受新的任务,但是保持基本的通信正常
  465. update_dispatch_plc_communication();
  466. /*
  467. //response add to _map
  468. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  469. {
  470. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  471. std::chrono::system_clock::time_point t_time = std::chrono::system_clock::now();
  472. Dispatch_manager::get_instance_references().m_dispatch_response_store_map[t_time] = m_dispatch_response_msg;
  473. Dispatch_manager::get_instance_references().m_store_updata_time = std::chrono::system_clock::now();
  474. }
  475. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  476. {
  477. std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  478. int t_terminal_id = m_dispatch_request_msg.id_struct().terminal_id();
  479. Dispatch_manager::get_instance_references().m_dispatch_response_pickup_map[t_terminal_id] = m_dispatch_response_msg;
  480. Dispatch_manager::get_instance_references().m_pickup_updata_time = std::chrono::system_clock::now();
  481. }
  482. */
  483. //20211209 huli //流程异常结束, 进入到 ready.
  484. m_dispatch_plc_status = DISPATCH_PLC_READY;
  485. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  486. // m_result.error_manager_clear_all();
  487. LOG(ERROR) << " Dispatch_plc::execute_thread_fun() dispatch_plc is fault, now recover to normal " << this;
  488. break;
  489. }
  490. }
  491. }
  492. }
  493. }
  494. //数据储存线程函数
  495. void Dispatch_plc::data_storage_thread_fun()
  496. {
  497. LOG(INFO) << " Dispatch_plc::data_storage_thread_fun() start " << this;
  498. Error_manager t_error;
  499. while (m_data_storage_condition.is_alive())
  500. {
  501. m_data_storage_condition.wait();
  502. if (m_data_storage_condition.is_alive())
  503. {
  504. std::this_thread::sleep_for(std::chrono::microseconds(1));
  505. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  506. std::this_thread::sleep_for(std::chrono::seconds(1));
  507. std::this_thread::yield();
  508. // std::unique_lock<std::mutex> t_lock(m_lock);
  509. char buf[16] = {0}; //数据转字符串然后存数据库
  510. std::string t_plc_data_string;
  511. float t_heartbeat = Dispatch_communication::get_instance_references().m_data_storage_from_plc_to_manager.m_heartbeat;
  512. {
  513. std::unique_lock<std::mutex> t_lock1(Dispatch_communication::get_instance_references().m_data_lock);
  514. char * tp = buf;
  515. for (int i = 0; i < DATA_STORAGE_PLC_DATA_LENGTH; ++i)
  516. {
  517. sprintf(buf, "%g\t", Dispatch_communication::get_instance_references().m_data_storage_from_plc_to_manager.m_plc_data[i]);
  518. t_plc_data_string += buf;
  519. }
  520. // std::cout << " huli test :::: " << " t_plc_data_string = " << t_plc_data_string << std::endl;
  521. }
  522. Dispatch_manager::get_instance_references().m_dispatch_command.insert_plc_data(t_plc_data_string, t_heartbeat);
  523. }
  524. }
  525. }
  526. //封装 给plc的调度请求
  527. Error_manager Dispatch_plc::encapsulate_dispatch_request_to_plc()
  528. {
  529. // //把m_dispatch_request_msg的信息传给本地的数据缓存.
  530. // std::unique_lock<std::mutex> t_lock(m_lock);
  531. // m_request_key = m_dispatch_request_msg.command_key();
  532. // m_request_status = 1;
  533. // if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  534. // {
  535. // m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
  536. // m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_INLET;
  537. // m_request_passageway_id = m_dispatch_request_msg.mutable_id_struct()->terminal_id()+1;//0~5入口终端号改为1~6
  538. // }
  539. // else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  540. // {
  541. // m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  542. // m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_OUTLET;
  543. // m_request_passageway_id = m_dispatch_request_msg.mutable_id_struct()->terminal_id()+1;//0~5入口终端号改为1~6
  544. //
  545. // }
  546. // else
  547. // {
  548. // m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_UNKNOWN;
  549. // m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN;
  550. // m_request_passageway_id = -1;
  551. // }
  552. //
  553. // if ( m_dispatch_request_msg.locate_information().locate_correct() )
  554. // {
  555. // m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  556. // m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  557. //#ifdef MEASURE_TO_PLC_CORRECTION
  558. // m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.3;//80~100改为-10~+10
  559. //#endif
  560. //#ifndef MEASURE_TO_PLC_CORRECTION
  561. // m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  562. //#endif
  563. // m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  564. // m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  565. // m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  566. // m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  567. // m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  568. // m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  569. // m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_x();
  570. // m_request_uniformed_car_y = m_dispatch_request_msg.locate_information().uniformed_car_y();
  571. // }
  572. // else
  573. // {
  574. // m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  575. // m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  576. //#ifdef MEASURE_TO_PLC_CORRECTION
  577. // m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.3;//80~100改为-10~+10
  578. //#endif
  579. //#ifndef MEASURE_TO_PLC_CORRECTION
  580. // m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  581. //#endif
  582. // m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  583. // m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  584. // m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  585. // m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  586. // m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  587. // m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  588. // m_request_uniformed_car_x = m_dispatch_request_msg.locate_information().uniformed_car_x();
  589. // m_request_uniformed_car_y = m_dispatch_request_msg.locate_information().uniformed_car_y();
  590. // }
  591. //
  592. // if ( m_dispatch_request_msg.parkspace_info_ex_size() ==1 )
  593. // {
  594. // m_request_parkingspace_index_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_index_id();
  595. // m_request_parkingspace_unit_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_unit_id()+1;//0~2单元号改为1~3
  596. // m_request_parkingspace_label_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_label_id();
  597. // m_request_parkingspace_floor_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_floor_id();
  598. // m_request_parkingspace_room_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_room_id();
  599. // //车位朝向, 小号朝前朝南, 大号朝后朝北
  600. // m_request_parkingspace_direction = (Common_data::Parkingspace_direction)m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_direction();
  601. //
  602. //
  603. // //20211220, utf-8 -> GBK
  604. // m_request_car_license = String_convert::utf8_to_gbk( m_dispatch_request_msg.parkspace_info_ex(0).car_info().car_numberplate() );
  605. //
  606. // m_request_car_type = (Common_data::Car_type)m_dispatch_request_msg.parkspace_info_ex(0).car_type();
  607. //
  608. // //20211207, huli add wheel_base for pickup_car
  609. // if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  610. // {
  611. // m_request_car_wheel_base = m_dispatch_request_msg.parkspace_info_ex(0).car_info().car_wheel_base();
  612. // m_request_car_wheel_width = m_dispatch_request_msg.parkspace_info_ex(0).car_info().car_wheel_width();
  613. // }
  614. // }
  615. // else
  616. // {
  617. // return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  618. // " m_dispatch_request_msg.parkspace_info_ex_size() !=1 error ");
  619. // }
  620. return Error_code::SUCCESS;
  621. }
  622. //封装 给plc的调度请求
  623. Error_manager Dispatch_plc::encapsulate_dispatch_request_to_plc_new()
  624. {
  625. //把m_dispatch_request_msg的信息传给本地的数据缓存.
  626. std::unique_lock<std::mutex> t_lock(m_lock);
  627. m_request_key = m_command_key;
  628. m_request_status = 1;
  629. if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE ||
  630. m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_REALLOCATE)
  631. {
  632. //基本信息
  633. m_request_dispatch_motion_direction = m_dispatch_process_type;
  634. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_INLET;
  635. m_request_passageway_id = m_park_table_msg.terminal_id();
  636. // m_request_passageway_id = m_terminal_id;
  637. //感测雷达信息, 存车专有
  638. // if ( m_park_table_msg.entrance_measure_info().border_statu() )
  639. if ( true )
  640. {
  641. m_request_car_center_x = m_park_table_msg.entrance_measure_info().cx();
  642. m_request_car_center_y = m_park_table_msg.entrance_measure_info().cy();
  643. m_request_car_angle = m_park_table_msg.entrance_measure_info().theta();
  644. m_request_car_front_theta = m_park_table_msg.entrance_measure_info().front_theta();
  645. m_request_car_length = m_park_table_msg.entrance_measure_info().length();
  646. m_request_car_width = m_park_table_msg.entrance_measure_info().width();
  647. m_request_car_height = m_park_table_msg.entrance_measure_info().height();
  648. m_request_car_wheel_base = m_park_table_msg.entrance_measure_info().wheelbase();
  649. m_request_car_wheel_width = m_park_table_msg.entrance_measure_info().width();
  650. m_request_uniformed_car_x = m_park_table_msg.entrance_measure_info().cx();
  651. m_request_uniformed_car_y = m_park_table_msg.entrance_measure_info().cy();
  652. }
  653. //车位信息
  654. m_request_parkingspace_index_id = m_park_table_msg.allocated_space_info().id();
  655. m_request_parkingspace_unit_id = m_park_table_msg.allocated_space_info().unit_id();
  656. // int t_parkingspace_label_id = (m_park_table_msg.allocated_space_info().id() - 1 )%78;
  657. // m_request_parkingspace_label_id = t_parkingspace_label_id+1;
  658. m_request_parkingspace_label_id = m_park_table_msg.allocated_space_info().table_id();
  659. // LOG(INFO) << " huli m_request_parkingspace_label_id = " << m_request_parkingspace_label_id<< " "<< this;
  660. m_request_parkingspace_floor_id = m_park_table_msg.allocated_space_info().floor();
  661. m_request_parkingspace_room_id = m_park_table_msg.allocated_space_info().room_id();
  662. if(0 == m_request_car_height)
  663. {
  664. m_request_car_height_level = 0;
  665. }
  666. else if ( 0 < m_request_car_height && m_request_car_height <= CAR_HEIGHT_LIMIT_SMALL +0.000001)
  667. {
  668. m_request_car_height_level = 1;
  669. }
  670. else if ( CAR_HEIGHT_LIMIT_SMALL+0.000001 < m_request_car_height && m_request_car_height <= CAR_HEIGHT_LIMIT_MIDDLE+0.000001 )
  671. {
  672. m_request_car_height_level = 2;
  673. }
  674. else if ( CAR_HEIGHT_LIMIT_MIDDLE+0.000001 < m_request_car_height && m_request_car_height <= CAR_HEIGHT_LIMIT_BIG+0.000001 )
  675. {
  676. m_request_car_height_level = 3;
  677. }
  678. else if ( CAR_HEIGHT_LIMIT_BIG+0.000001 < m_request_car_height )
  679. {
  680. m_request_car_height_level = 4;
  681. }
  682. //车位朝向, 奇数朝南, 偶数朝北
  683. if ( m_request_parkingspace_index_id %2 == 1 )
  684. {
  685. m_request_parkingspace_direction = Common_data::Parkingspace_direction::PARKINGSPACE_DIRECTION_SOUTH;
  686. }
  687. else
  688. {
  689. m_request_parkingspace_direction = Common_data::Parkingspace_direction::PARKINGSPACE_DIRECTION_NORTH;
  690. }
  691. #ifdef CHUTIAN_PROJECT_PROJECT
  692. //20211220, utf-8 -> GBK
  693. m_request_car_license = String_convert::utf8_to_gbk( m_park_table_msg.car_number() );
  694. #endif //CHUTIAN_PROJECT_PROJECT
  695. #ifdef SHANGGUJIE_PROJECT_PROJECT
  696. m_request_car_license = String_convert::utf8_to_gbk( m_park_table_msg.car_number() );
  697. LOG(INFO) << " m_request_car_license = "<< m_request_car_license <<" ---- - - - "<< this;
  698. #endif //SHANGGUJIE_PROJECT_PROJECT
  699. m_request_car_type = Common_data::judge_car_type_with_car_height(m_request_car_height);
  700. }
  701. else if( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP ||
  702. m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_REVOCATION)
  703. {
  704. m_request_dispatch_motion_direction = m_dispatch_process_type;
  705. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_OUTLET;
  706. m_request_passageway_id = m_pick_table_msg.terminal_id();
  707. // m_request_passageway_id = m_terminal_id;
  708. //20211207, 取车专有, 数据库保存的汽车信息, 轮距
  709. m_request_car_center_x = m_pick_table_msg.actually_measure_info().cx();
  710. m_request_car_center_y = m_pick_table_msg.actually_measure_info().cy();
  711. m_request_car_angle = m_pick_table_msg.actually_measure_info().theta();
  712. m_request_car_front_theta = m_pick_table_msg.actually_measure_info().front_theta();
  713. m_request_car_length = m_pick_table_msg.actually_measure_info().length();
  714. m_request_car_width = m_pick_table_msg.actually_measure_info().width();
  715. //huli, 20230609, 存车修改车位后, measure的车高度和space的车高度可能会不同,
  716. //此时用车位的高度为标准
  717. // m_request_car_height = m_pick_table_msg.actually_measure_info().height();
  718. m_request_car_height = m_pick_table_msg.actually_space_info().height();
  719. m_request_car_wheel_base = m_pick_table_msg.actually_measure_info().wheelbase();
  720. m_request_car_wheel_width = m_pick_table_msg.actually_measure_info().width();
  721. m_request_uniformed_car_x = m_pick_table_msg.actually_measure_info().cx();
  722. m_request_uniformed_car_y = m_pick_table_msg.actually_measure_info().cy();
  723. //车位信息
  724. m_request_parkingspace_index_id = m_pick_table_msg.actually_space_info().id();
  725. m_request_parkingspace_unit_id = m_pick_table_msg.actually_space_info().unit_id();
  726. // int t_parkingspace_label_id = (m_pick_table_msg.actually_space_info().id() -1 )%78;
  727. // m_request_parkingspace_label_id = t_parkingspace_label_id +1 ;
  728. m_request_parkingspace_label_id = m_pick_table_msg.actually_space_info().table_id();
  729. m_request_parkingspace_floor_id = m_pick_table_msg.actually_space_info().floor();
  730. m_request_parkingspace_room_id = m_pick_table_msg.actually_space_info().room_id();
  731. if(0 == m_request_car_height)
  732. {
  733. m_request_car_height_level = 0;
  734. }
  735. else if ( 0 < m_request_car_height && m_request_car_height <= CAR_HEIGHT_LIMIT_SMALL +0.000001)
  736. {
  737. m_request_car_height_level = 1;
  738. }
  739. else if ( CAR_HEIGHT_LIMIT_SMALL +0.000001< m_request_car_height && m_request_car_height <= CAR_HEIGHT_LIMIT_MIDDLE +0.000001)
  740. {
  741. m_request_car_height_level = 2;
  742. }
  743. else if ( CAR_HEIGHT_LIMIT_MIDDLE +0.000001< m_request_car_height && m_request_car_height <= CAR_HEIGHT_LIMIT_BIG +0.000001)
  744. {
  745. m_request_car_height_level = 3;
  746. }
  747. else if ( CAR_HEIGHT_LIMIT_BIG +0.000001< m_request_car_height )
  748. {
  749. m_request_car_height_level = 4;
  750. }
  751. //车位朝向, 奇数朝南, 偶数朝北
  752. if ( m_request_parkingspace_index_id %2 == 1 )
  753. {
  754. m_request_parkingspace_direction = Common_data::Parkingspace_direction::PARKINGSPACE_DIRECTION_SOUTH;
  755. }
  756. else
  757. {
  758. m_request_parkingspace_direction = Common_data::Parkingspace_direction::PARKINGSPACE_DIRECTION_NORTH;
  759. }
  760. #ifdef CHUTIAN_PROJECT_PROJECT
  761. //20211220, utf-8 -> GBK
  762. m_request_car_license = String_convert::utf8_to_gbk( m_park_table_msg.car_number() );
  763. #endif //CHUTIAN_PROJECT_PROJECT
  764. #ifdef SHANGGUJIE_PROJECT_PROJECT
  765. m_request_car_license = String_convert::utf8_to_gbk( m_pick_table_msg.car_number() );
  766. LOG(INFO) << " m_request_car_license = "<< m_request_car_license <<" ---- - - - "<< this;
  767. #endif //SHANGGUJIE_PROJECT_PROJECT
  768. m_request_car_type = Common_data::judge_car_type_with_car_height(m_request_car_height);
  769. }
  770. else
  771. {
  772. m_request_dispatch_motion_direction = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  773. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN;
  774. m_request_passageway_id = -1;
  775. }
  776. return Error_code::SUCCESS;
  777. }
  778. //更新plc通信
  779. Error_manager Dispatch_plc::update_dispatch_plc_communication()
  780. {
  781. std::unique_lock<std::mutex> t_lock(m_lock);
  782. std::unique_lock<std::mutex> t_lock1(Dispatch_communication::get_instance_references().m_data_lock);
  783. /*
  784. //请求消息, 调度->plc
  785. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  786. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  787. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  788. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  789. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  790. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  791. m_request_key = "123456789";
  792. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  793. static unsigned char index = 0;
  794. index++;
  795. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = index;
  796. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = 02;
  797. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = 03;
  798. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = 04;
  799. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = 05;
  800. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = 6;
  801. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = 7;
  802. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = 8;
  803. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = 11;
  804. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = 12;
  805. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = 13;
  806. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = 14;
  807. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = 15;
  808. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = 16;
  809. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = 17;
  810. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = 18;
  811. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = 19;
  812. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  813. int t_size2 = m_request_key.size()<=20 ? m_request_key.size() : 20 ;
  814. m_request_car_license = "ABCDEF";
  815. memcpy(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, m_request_car_license.c_str(), t_size2);
  816. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = 33;
  817. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = 44;
  818. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = 55;
  819. */
  820. //AB单元的9000和9001特殊处理, 删除168字节后面的
  821. if ( m_plc_id == 31 || m_plc_id == 32)
  822. {
  823. //请求消息, 调度->plc
  824. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  825. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  826. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  827. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  828. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  829. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  830. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  831. tp_dispatch_request_from_manager_to_plc_for_data->m_request_status = m_request_status;
  832. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = m_request_dispatch_motion_direction;
  833. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = m_request_passageway_id;
  834. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = m_request_passageway_direction;
  835. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height_level = m_request_car_height_level;
  836. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = m_request_parkingspace_index_id;
  837. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = m_request_parkingspace_unit_id;
  838. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_label_id = m_request_parkingspace_label_id;
  839. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = m_request_parkingspace_floor_id;
  840. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = m_request_parkingspace_room_id;
  841. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = m_request_parkingspace_direction;
  842. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = m_request_car_center_x;
  843. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = m_request_car_center_y;
  844. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = m_request_car_angle;
  845. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = m_request_car_front_theta;
  846. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = m_request_car_length;
  847. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = m_request_car_width;
  848. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = m_request_car_height;
  849. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = m_request_car_wheel_base;
  850. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = m_request_car_wheel_width;
  851. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  852. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license[0] = 18;
  853. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license[1] = m_request_car_license.length();
  854. unsigned char * p_ch = tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license;
  855. memcpy(p_ch+2, m_request_car_license.c_str(), m_request_car_license.length());
  856. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = m_request_car_type;
  857. tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_x = m_request_uniformed_car_x;
  858. tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_y = m_request_uniformed_car_y;
  859. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = m_request_anticollision_lidar_flag;
  860. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = m_request_car_wheel_base_exact_value;
  861. //答复消息, plc->调度
  862. Dispatch_communication::Dispatch_response_from_plc_to_manager * tp_dispatch_response_from_plc_to_manager =
  863. & Dispatch_communication::get_instance_references().m_dispatch_response_from_plc_to_manager;
  864. m_response_key = (char*) tp_dispatch_response_from_plc_to_manager->m_response_key;
  865. m_response_status = (Dispatch_plc::Response_status)tp_dispatch_response_from_plc_to_manager->m_response_status;
  866. m_response_working_total_time = tp_dispatch_response_from_plc_to_manager->m_response_working_total_time;
  867. m_response_working_remaining_time = tp_dispatch_response_from_plc_to_manager->m_response_working_remaining_time;
  868. m_response_dispatch_motion_direction = (Common_data::Dispatch_process_type)tp_dispatch_response_from_plc_to_manager->m_response_dispatch_motion_direction;
  869. m_response_passageway_id = tp_dispatch_response_from_plc_to_manager->m_response_passageway_id;
  870. m_response_passageway_direction = (Common_data::Passageway_direction)tp_dispatch_response_from_plc_to_manager->m_response_passageway_direction;
  871. m_response_car_height_level = tp_dispatch_response_from_plc_to_manager->m_response_car_height_level;
  872. m_response_parkingspace_index_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_index_id;
  873. m_response_parkingspace_unit_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_unit_id;
  874. m_response_parkingspace_label_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_label_id;
  875. m_response_parkingspace_floor_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_floor_id;
  876. m_response_parkingspace_room_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_room_id;
  877. m_response_parkingspace_direction = (Common_data::Parkingspace_direction)tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_direction;
  878. m_response_car_center_x = tp_dispatch_response_from_plc_to_manager->m_response_car_center_x;
  879. m_response_car_center_y = tp_dispatch_response_from_plc_to_manager->m_response_car_center_y;
  880. m_response_car_angle = tp_dispatch_response_from_plc_to_manager->m_response_car_angle;
  881. m_response_car_front_theta = tp_dispatch_response_from_plc_to_manager->m_response_car_front_theta;
  882. m_response_car_length = tp_dispatch_response_from_plc_to_manager->m_response_car_length;
  883. m_response_car_width = tp_dispatch_response_from_plc_to_manager->m_response_car_width;
  884. m_response_car_height = tp_dispatch_response_from_plc_to_manager->m_response_car_height;
  885. m_response_car_wheel_base = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base;
  886. m_response_car_wheel_width = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_width;
  887. m_response_car_license = (char*) (tp_dispatch_response_from_plc_to_manager->m_response_car_license+2);
  888. m_response_car_type = (Common_data::Car_type) tp_dispatch_response_from_plc_to_manager->m_response_car_type;
  889. m_response_uniformed_car_x = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_x;
  890. m_response_uniformed_car_y = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_y;
  891. m_response_anticollision_lidar_flag = (Common_data::Anticollision_lidar_flag)tp_dispatch_response_from_plc_to_manager->m_response_anticollision_lidar_flag;
  892. m_response_car_wheel_base_exact_value = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base_exact_value;
  893. }
  894. else
  895. {
  896. //请求消息, 调度->plc
  897. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data_ab * tp_dispatch_request_from_manager_to_plc_for_data =
  898. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data_ab;
  899. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  900. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  901. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  902. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  903. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  904. tp_dispatch_request_from_manager_to_plc_for_data->m_request_status = m_request_status;
  905. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = m_request_dispatch_motion_direction;
  906. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = m_request_passageway_id;
  907. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = m_request_passageway_direction;
  908. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height_level = m_request_car_height_level;
  909. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = m_request_parkingspace_index_id;
  910. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = m_request_parkingspace_unit_id;
  911. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_label_id = m_request_parkingspace_label_id;
  912. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = m_request_parkingspace_floor_id;
  913. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = m_request_parkingspace_room_id;
  914. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = m_request_parkingspace_direction;
  915. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = m_request_car_center_x;
  916. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = m_request_car_center_y;
  917. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = m_request_car_angle;
  918. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = m_request_car_front_theta;
  919. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = m_request_car_length;
  920. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = m_request_car_width;
  921. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = m_request_car_height;
  922. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = m_request_car_wheel_base;
  923. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = m_request_car_wheel_width;
  924. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  925. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license[0] = 18;
  926. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license[1] = m_request_car_license.length();
  927. unsigned char * p_ch = tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license;
  928. memcpy(p_ch+2, m_request_car_license.c_str(), m_request_car_license.length());
  929. // tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = m_request_car_type;
  930. // tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_x = m_request_uniformed_car_x;
  931. // tp_dispatch_request_from_manager_to_plc_for_data->m_request_uniformed_car_y = m_request_uniformed_car_y;
  932. // tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = m_request_anticollision_lidar_flag;
  933. // tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = m_request_car_wheel_base_exact_value;
  934. //答复消息, plc->调度
  935. Dispatch_communication::Dispatch_response_from_plc_to_manager_ab * tp_dispatch_response_from_plc_to_manager =
  936. & Dispatch_communication::get_instance_references().m_dispatch_response_from_plc_to_manager_ab;
  937. m_response_key = (char*) tp_dispatch_response_from_plc_to_manager->m_response_key;
  938. m_response_status = (Dispatch_plc::Response_status)tp_dispatch_response_from_plc_to_manager->m_response_status;
  939. m_response_working_total_time = tp_dispatch_response_from_plc_to_manager->m_response_working_total_time;
  940. m_response_working_remaining_time = tp_dispatch_response_from_plc_to_manager->m_response_working_remaining_time;
  941. m_response_dispatch_motion_direction = (Common_data::Dispatch_process_type)tp_dispatch_response_from_plc_to_manager->m_response_dispatch_motion_direction;
  942. m_response_passageway_id = tp_dispatch_response_from_plc_to_manager->m_response_passageway_id;
  943. m_response_passageway_direction = (Common_data::Passageway_direction)tp_dispatch_response_from_plc_to_manager->m_response_passageway_direction;
  944. m_response_car_height_level = tp_dispatch_response_from_plc_to_manager->m_response_car_height_level;
  945. m_response_parkingspace_index_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_index_id;
  946. m_response_parkingspace_unit_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_unit_id;
  947. m_response_parkingspace_label_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_label_id;
  948. m_response_parkingspace_floor_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_floor_id;
  949. m_response_parkingspace_room_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_room_id;
  950. m_response_parkingspace_direction = (Common_data::Parkingspace_direction)tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_direction;
  951. m_response_car_center_x = tp_dispatch_response_from_plc_to_manager->m_response_car_center_x;
  952. m_response_car_center_y = tp_dispatch_response_from_plc_to_manager->m_response_car_center_y;
  953. m_response_car_angle = tp_dispatch_response_from_plc_to_manager->m_response_car_angle;
  954. m_response_car_front_theta = tp_dispatch_response_from_plc_to_manager->m_response_car_front_theta;
  955. m_response_car_length = tp_dispatch_response_from_plc_to_manager->m_response_car_length;
  956. m_response_car_width = tp_dispatch_response_from_plc_to_manager->m_response_car_width;
  957. m_response_car_height = tp_dispatch_response_from_plc_to_manager->m_response_car_height;
  958. m_response_car_wheel_base = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base;
  959. m_response_car_wheel_width = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_width;
  960. m_response_car_license = (char*) (tp_dispatch_response_from_plc_to_manager->m_response_car_license+2);
  961. // m_response_car_type = (Common_data::Car_type) tp_dispatch_response_from_plc_to_manager->m_response_car_type;
  962. // m_response_uniformed_car_x = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_x;
  963. // m_response_uniformed_car_y = tp_dispatch_response_from_plc_to_manager->m_response_uniformed_car_y;
  964. // m_response_anticollision_lidar_flag = (Common_data::Anticollision_lidar_flag)tp_dispatch_response_from_plc_to_manager->m_response_anticollision_lidar_flag;
  965. // m_response_car_wheel_base_exact_value = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base_exact_value;
  966. }
  967. #ifdef CHUTIAN_PROJECT_PROJECT
  968. //状态消息, 调度->plc
  969. Dispatch_communication::Dispatch_plc_status_from_manager_to_plc * tp_dispatch_plc_status_from_manager_to_plc =
  970. & Dispatch_communication::get_instance_references().m_dispatch_plc_status_from_manager_to_plc;
  971. m_dispatch_heartbeat++;
  972. tp_dispatch_plc_status_from_manager_to_plc->m_dispatch_heartbeat = m_dispatch_heartbeat;
  973. //状态消息, plc->调度
  974. Dispatch_communication::Dispatch_plc_status_from_plc_to_manager * tp_dispatch_plc_status_from_plc_to_manager =
  975. & Dispatch_communication::get_instance_references().m_dispatch_plc_status_from_plc_to_manager;
  976. m_plc_heartbeat = tp_dispatch_plc_status_from_plc_to_manager->m_plc_heartbeat;
  977. m_plc_status_info = tp_dispatch_plc_status_from_plc_to_manager->m_plc_status_info;
  978. m_turnplate_angle_min1 = tp_dispatch_plc_status_from_plc_to_manager->m_turnplate_angle_min1;
  979. m_turnplate_angle_max1 = tp_dispatch_plc_status_from_plc_to_manager->m_turnplate_angle_max1;
  980. m_turnplate_angle_min2 = tp_dispatch_plc_status_from_plc_to_manager->m_turnplate_angle_min2;
  981. m_turnplate_angle_max2 = tp_dispatch_plc_status_from_plc_to_manager->m_turnplate_angle_max2;
  982. #endif //CHUTIAN_PROJECT_PROJECT
  983. #ifdef SHANGGUJIE_PROJECT_PROJECT
  984. //状态消息, plc->调度
  985. Dispatch_communication::Dispatch_plc_status_from_plc_to_manager * tp_dispatch_plc_status_from_plc_to_manager =
  986. & Dispatch_communication::get_instance_references().m_dispatch_plc_status_from_plc_to_manager;
  987. //判断心跳
  988. if ( tp_dispatch_plc_status_from_plc_to_manager->m_plc_heartbeat == m_last_heartbeat )
  989. {
  990. if ( std::chrono::system_clock::now() - m_last_update_time > std::chrono::seconds(3) )
  991. {
  992. m_plc_communication_status = false;
  993. std::cout << " huli test :::: " << "---------------------------------------------------------- tp_dispatch_plc_status_from_plc_to_manager->m_plc_heartbeat = " << tp_dispatch_plc_status_from_plc_to_manager->m_plc_heartbeat << std::endl;
  994. Dispatch_communication::get_instance_references().set_snap7_communication_statu(Snap7_communication_base::SNAP7_COMMUNICATION_DISCONNECT);
  995. m_last_update_time = std::chrono::system_clock::now();
  996. }
  997. //else 继续等待
  998. }
  999. else //plc状态 数据正常保存
  1000. {
  1001. m_plc_communication_status = true;
  1002. m_last_heartbeat = tp_dispatch_plc_status_from_plc_to_manager->m_plc_heartbeat;
  1003. m_last_update_time = std::chrono::system_clock::now();
  1004. memcpy(&m_dispatch_plc_status_from_plc_to_manager, tp_dispatch_plc_status_from_plc_to_manager, sizeof(Dispatch_communication::Dispatch_plc_status_from_plc_to_manager));
  1005. }
  1006. //状态消息, 调度->plc
  1007. Dispatch_communication::Dispatch_plc_status_from_manager_to_plc * tp_dispatch_plc_status_from_manager_to_plc =
  1008. & Dispatch_communication::get_instance_references().m_dispatch_plc_status_from_manager_to_plc;
  1009. m_dispatch_plc_status_from_manager_to_plc.m_dispatch_heartbeat++;
  1010. //20230527, 新增入口控制逻辑,
  1011. //入口终端发送存车指令后, execute_rabbitmq_message_new 会触发关闭入口外门.
  1012. //实时检查入口是否有车, 如果无车, 撤销关门指令,改为无效, 控制权限返回plc
  1013. if ( (m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[0].m_sensor_2 & 0x04) == 0 )
  1014. {
  1015. m_dispatch_plc_status_from_manager_to_plc.m_inlet_door_control_1 = 0;
  1016. }
  1017. if ( (m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[1].m_sensor_2 & 0x04) == 0 )
  1018. {
  1019. m_dispatch_plc_status_from_manager_to_plc.m_inlet_door_control_2 = 0;
  1020. }
  1021. //这里涉及到数据库操作, 放慢一些
  1022. static int t_count_2 = 0;
  1023. if (t_count_2 % 1000 == 0 )
  1024. {
  1025. //检查出口取车是否完成, 如果取车完成 并且出口无车,那么删除取车指令
  1026. if ( Dispatch_manager::get_instance_references().m_dispatch_command.check_pickup_is_finish(m_plc_id, 3) == Error_code::SUCCESS )
  1027. {
  1028. //检测出口是否有车, 如果没有车, 直接清除数据库的指令
  1029. if ( (m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[2].m_sensor_2 & 0x04) == 0 )
  1030. {
  1031. //delete
  1032. Dispatch_manager::get_instance_references().m_dispatch_command.delete_command_queue_for_export_id(m_plc_id, 3);
  1033. }
  1034. //else 什么也不做
  1035. }
  1036. if ( Dispatch_manager::get_instance_references().m_dispatch_command.check_pickup_is_finish(m_plc_id, 4) == Error_code::SUCCESS )
  1037. {
  1038. //检测出口是否有车, 如果没有车, 直接清除数据库的指令
  1039. if ( (m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[3].m_sensor_2 & 0x04) == 0 )
  1040. {
  1041. //delete
  1042. Dispatch_manager::get_instance_references().m_dispatch_command.delete_command_queue_for_export_id(m_plc_id, 4);
  1043. }
  1044. //else 什么也不做
  1045. }
  1046. int t_small_space_count = 0;
  1047. Dispatch_manager::get_instance_references().m_dispatch_command.query_specify_height_unit_parkspace_info(m_plc_id, CAR_HEIGHT_LIMIT_SMALL, CAR_WHEEL_BASE_LIMIT_SHORT,t_small_space_count);
  1048. m_dispatch_plc_status_from_manager_to_plc.m_ready_small_space_count = t_small_space_count - m_dispatch_plc_status_from_manager_to_plc.m_wait_small_park_count;
  1049. int t_middle_space_count = 0;
  1050. Dispatch_manager::get_instance_references().m_dispatch_command.query_specify_height_unit_parkspace_info(m_plc_id, CAR_HEIGHT_LIMIT_MIDDLE, CAR_WHEEL_BASE_LIMIT_SHORT, t_middle_space_count);
  1051. m_dispatch_plc_status_from_manager_to_plc.m_ready_middle_space_count = t_middle_space_count - m_dispatch_plc_status_from_manager_to_plc.m_wait_middle_park_count;
  1052. int t_big_space_count = 0;
  1053. Dispatch_manager::get_instance_references().m_dispatch_command.query_specify_height_unit_parkspace_info(m_plc_id, CAR_HEIGHT_LIMIT_BIG, CAR_WHEEL_BASE_LIMIT_SHORT, t_big_space_count);
  1054. m_dispatch_plc_status_from_manager_to_plc.m_ready_big_space_count = t_big_space_count - m_dispatch_plc_status_from_manager_to_plc.m_wait_big_park_count;
  1055. m_dispatch_plc_status_from_manager_to_plc.m_ready_total_space_count = m_dispatch_plc_status_from_manager_to_plc.m_ready_small_space_count +
  1056. m_dispatch_plc_status_from_manager_to_plc.m_ready_middle_space_count +
  1057. m_dispatch_plc_status_from_manager_to_plc.m_ready_big_space_count;
  1058. // LOG(INFO) << " 444444444444444444444444444444444444 ccc= "<<t_small_space_count<<" "<< this;
  1059. // LOG(INFO) << " 555555555555555555555555555555555555 ccc= "<<t_middle_space_count<<" "<< this;
  1060. // LOG(INFO) << " 666666666666666666666666666666666666 ccc= "<<t_big_space_count<<" "<< this;
  1061. }
  1062. t_count_2++;
  1063. memcpy(tp_dispatch_plc_status_from_manager_to_plc, &m_dispatch_plc_status_from_manager_to_plc, sizeof(Dispatch_communication::Dispatch_plc_status_from_manager_to_plc));
  1064. // std::cout << " huli test :::: " << " m_dispatch_plc_status_from_manager_to_plc 456= " << m_dispatch_plc_status_from_manager_to_plc.m_dispatch_heartbeat << std::endl;
  1065. // std::cout << " huli test :::: " << " tp_dispatch_plc_status_from_manager_to_plc 789= " << tp_dispatch_plc_status_from_manager_to_plc->m_dispatch_heartbeat << std::endl;
  1066. #ifndef PLC_S7_COMMUNICATION
  1067. //屏蔽plc后, 将plc的状态修改为正常
  1068. m_dispatch_plc_status_from_plc_to_manager.m_plc_heartbeat++;
  1069. m_dispatch_plc_status_from_plc_to_manager.m_plc_status_info = 0xF2;
  1070. m_dispatch_plc_status_from_plc_to_manager.m_plc_carrier_status = 3;
  1071. m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[0].m_car_height = 1;
  1072. m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[0].m_sensor_1 = 0xFE;
  1073. m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[0].m_sensor_2 = 0xFF;
  1074. m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[1].m_car_height = 1;
  1075. m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[1].m_sensor_1 = 0xFE;
  1076. m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[1].m_sensor_2 = 0xFF;
  1077. m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[2].m_car_height = 0;
  1078. m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[2].m_sensor_1 = 0xFE;
  1079. m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[2].m_sensor_2 = 0x03;
  1080. m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[3].m_car_height = 1;
  1081. m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[3].m_sensor_1 = 0xFE;
  1082. m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[3].m_sensor_2 = 0x03;
  1083. #endif// WAIT_PLC_RESPONSE
  1084. #endif //SHANGGUJIE_PROJECT_PROJECT
  1085. //need
  1086. //通过心跳帧来判断通信是否正常
  1087. // if ( m_last_heartbeat != tp_dispatch_plc_status_from_plc_to_manager->m_plc_heartbeat )
  1088. // {
  1089. // m_last_heartbeat = tp_dispatch_plc_status_from_plc_to_manager->m_plc_heartbeat;
  1090. // m_status_updata_time = std::chrono::system_clock::now();
  1091. //
  1092. // if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT )
  1093. // {
  1094. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY;
  1095. // }
  1096. // }
  1097. // else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(COMMUNICATION_OVER_TIME_MS))
  1098. // {
  1099. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT;
  1100. // }
  1101. //else 继续等待,直到消息刷新或者超时.
  1102. return Error_code::SUCCESS;
  1103. }
  1104. //检查plc答复反馈
  1105. Error_manager Dispatch_plc::check_response_from_plc()
  1106. {
  1107. // std::unique_lock<std::mutex> t_lock(m_lock);
  1108. //
  1109. //
  1110. //#ifndef WAIT_PLC_RESPONSE
  1111. // //need, 调试代码, 延时10s, 直接返回成功
  1112. // if ( std::chrono::system_clock::now() - m_start_time > std::chrono::seconds(20) )
  1113. // {
  1114. // return Error_code::SUCCESS;
  1115. // }
  1116. // //返回没有收到数据
  1117. // else
  1118. // {
  1119. // return Error_code::NODATA;
  1120. // }
  1121. //#endif
  1122. //
  1123. //
  1124. //#ifdef WAIT_PLC_RESPONSE
  1125. //
  1126. // static bool s_update_flag = false;
  1127. // //检查唯一码
  1128. // if ( m_response_key == m_request_key )
  1129. // {
  1130. // //第一次同步时,刷新时间
  1131. // if ( s_update_flag == false )
  1132. // {
  1133. // s_update_flag = true;
  1134. // if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  1135. // {
  1136. // std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  1137. // Dispatch_manager::get_instance_references().m_store_updata_time = std::chrono::system_clock::now();
  1138. // }
  1139. // else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  1140. // {
  1141. // std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  1142. // Dispatch_manager::get_instance_references().m_pickup_updata_time = std::chrono::system_clock::now();
  1143. // }
  1144. // }
  1145. //
  1146. //
  1147. // if(m_response_status == RESPONS_WORKING)
  1148. // {
  1149. // return Error_code::NODATA;//返回没有收到数据
  1150. // }
  1151. // else
  1152. // {
  1153. // std::cout<< "m_response_status = " << m_response_status << std::endl;
  1154. // //如果故障,则添加错误码
  1155. // if ( m_response_status != RESPONS_OVER )
  1156. // {
  1157. // //添加错误码
  1158. // Error_manager t_error(DISPATCH_PLC_RESPONS_ERROR, MINOR_ERROR, " Dispatch_plc::check_response_from_plc() m_respons_status is error");
  1159. // return t_error;
  1160. // }
  1161. //
  1162. // return Error_code::SUCCESS;
  1163. // }
  1164. //
  1165. // }
  1166. // else
  1167. // {
  1168. // s_update_flag = false;
  1169. // return Error_code::NODATA;//返回没有收到数据
  1170. // }
  1171. //#endif
  1172. return Error_code::SUCCESS;
  1173. }
  1174. //检查plc答复反馈
  1175. Error_manager Dispatch_plc::check_response_from_plc_new()
  1176. {
  1177. std::unique_lock<std::mutex> t_lock(m_lock);
  1178. #ifndef WAIT_PLC_RESPONSE
  1179. //need, 调试代码, 延时10s, 直接返回成功
  1180. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::seconds(10) )
  1181. {
  1182. return Error_code::SUCCESS;
  1183. }
  1184. //返回没有收到数据
  1185. else
  1186. {
  1187. return Error_code::NODATA;
  1188. }
  1189. #endif
  1190. #ifdef WAIT_PLC_RESPONSE
  1191. static bool s_update_flag = false;
  1192. // LOG(INFO) << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++m_request_key = "<< m_request_key << " --- " << this;
  1193. // LOG(INFO) << " ++++++++++++++++++++++++++++++++++++++++++++++++++++++++m_response_key = "<< m_response_key << " --- " << this;
  1194. // LOG(INFO) << " +++++++++++++++++++++++++++++++++++++++++++++++++++++m_response_status = "<< m_response_status << " --- " << this;
  1195. //检查唯一码
  1196. if ( m_response_key == m_request_key )
  1197. {
  1198. //第一次同步时,刷新时间
  1199. if ( s_update_flag == false )
  1200. {
  1201. s_update_flag = true;
  1202. // if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  1203. // {
  1204. // std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  1205. // Dispatch_manager::get_instance_references().m_store_updata_time = std::chrono::system_clock::now();
  1206. // }
  1207. // else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  1208. // {
  1209. // std::unique_lock<std::mutex> t_lock(Dispatch_manager::get_instance_references().m_lock);
  1210. // Dispatch_manager::get_instance_references().m_pickup_updata_time = std::chrono::system_clock::now();
  1211. // }
  1212. }
  1213. if(m_response_status == RESPONS_WORKING)
  1214. {
  1215. return Error_code::NODATA;//返回没有收到数据
  1216. }
  1217. else
  1218. {
  1219. std::cout<< "m_response_status = " << m_response_status << std::endl;
  1220. //如果故障,则添加错误码
  1221. if ( m_response_status == RESPONS_OVER )
  1222. {
  1223. return Error_code::SUCCESS;
  1224. }
  1225. else if(m_response_status == RESPONS_REALLOCATE_MIN_SHORT_CAR)
  1226. {
  1227. //重新分配小车
  1228. return DISPATCH_PLC_REALLOCATE_MIN_SHORT_CAR;
  1229. }
  1230. else if(m_response_status == RESPONS_REALLOCATE_MID_SHORT_CAR)
  1231. {
  1232. //重新分配中车
  1233. return DISPATCH_PLC_REALLOCATE_MID_SHORT_CAR;
  1234. }
  1235. else if(m_response_status == RESPONS_REALLOCATE_BIG_SHORT_CAR)
  1236. {
  1237. //重新分配大车
  1238. return DISPATCH_PLC_REALLOCATE_BIG_SHORT_CAR;
  1239. }
  1240. else if(m_response_status == RESPONS_REALLOCATE_HUGE_SHORT_CAR)
  1241. {
  1242. //重新分配fault
  1243. return DISPATCH_PLC_REALLOCATE_HUGE_SHORT_CAR;
  1244. }
  1245. else if(m_response_status == RESPONS_REALLOCATE_FAULT_SHORT_CAR)
  1246. {
  1247. //重新分配fault
  1248. return DISPATCH_PLC_REALLOCATE_FAULT_SHORT_CAR;
  1249. }
  1250. else if(m_response_status == RESPONS_REALLOCATE_MIN_LONG_CAR)
  1251. {
  1252. //重新分配小车
  1253. return DISPATCH_PLC_REALLOCATE_MIN_LONG_CAR;
  1254. }
  1255. else if(m_response_status == RESPONS_REALLOCATE_MID_LONG_CAR)
  1256. {
  1257. //重新分配中车
  1258. return DISPATCH_PLC_REALLOCATE_MID_LONG_CAR;
  1259. }
  1260. else if(m_response_status == RESPONS_REALLOCATE_BIG_LONG_CAR)
  1261. {
  1262. //重新分配大车
  1263. return DISPATCH_PLC_REALLOCATE_BIG_LONG_CAR;
  1264. }
  1265. else if(m_response_status == RESPONS_REALLOCATE_HUGE_LONG_CAR)
  1266. {
  1267. //重新分配fault
  1268. return DISPATCH_PLC_REALLOCATE_HUGE_LONG_CAR;
  1269. }
  1270. else if(m_response_status == RESPONS_REALLOCATE_FAULT_LONG_CAR)
  1271. {
  1272. //重新分配fault
  1273. return DISPATCH_PLC_REALLOCATE_FAULT_LONG_CAR;
  1274. }
  1275. else //如果故障,则添加错误码
  1276. {
  1277. //添加错误码
  1278. Error_manager t_error(DISPATCH_PLC_RESPONS_ERROR, MINOR_ERROR, " Dispatch_plc::check_response_from_plc() m_respons_status is error");
  1279. return t_error;
  1280. }
  1281. }
  1282. }
  1283. else
  1284. {
  1285. s_update_flag = false;
  1286. return Error_code::NODATA;//返回没有收到数据
  1287. }
  1288. #endif
  1289. return Error_code::SUCCESS;
  1290. }
  1291. //发送调度答复信息给主控
  1292. Error_manager Dispatch_plc::send_dispatch_response_to_main_control()
  1293. {
  1294. // std::unique_lock<std::mutex> t_lock(m_lock);
  1295. // m_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  1296. // m_dispatch_response_msg.mutable_base_info()->set_timeout_ms(m_dispatch_request_msg.base_info().timeout_ms());
  1297. // m_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  1298. // m_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  1299. //
  1300. // m_dispatch_response_msg.set_command_key(m_dispatch_request_msg.command_key());
  1301. // m_dispatch_response_msg.set_dispatch_motion_direction(m_dispatch_request_msg.dispatch_motion_direction());
  1302. // m_dispatch_response_msg.mutable_id_struct()->set_terminal_id(m_dispatch_request_msg.mutable_id_struct()->terminal_id());
  1303. // m_dispatch_response_msg.mutable_id_struct()->set_unit_id(m_dispatch_request_msg.mutable_id_struct()->unit_id());
  1304. // m_dispatch_response_msg.mutable_locate_information()->CopyFrom(m_dispatch_request_msg.locate_information());
  1305. // m_dispatch_response_msg.mutable_parkspace_info_ex()->CopyFrom(m_dispatch_request_msg.parkspace_info_ex());
  1306. // m_dispatch_response_msg.set_car_type(m_dispatch_request_msg.car_type());
  1307. //
  1308. // if ( m_dispatch_response_msg.dispatch_motion_direction() == message::E_STORE_CAR )
  1309. // {
  1310. // for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  1311. // {
  1312. // if ( m_result == Error_code::SUCCESS )
  1313. // {
  1314. // m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  1315. // }
  1316. // else
  1317. // {
  1318. // m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  1319. // }
  1320. // }
  1321. // }
  1322. // else if( m_dispatch_response_msg.dispatch_motion_direction() == message::E_PICKUP_CAR )
  1323. // {
  1324. // for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  1325. // {
  1326. // if ( m_result == Error_code::SUCCESS )
  1327. // {
  1328. // m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  1329. // }
  1330. // else
  1331. // {
  1332. // m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  1333. // }
  1334. // }
  1335. // }
  1336. //
  1337. //
  1338. // m_dispatch_response_msg.mutable_error_manager()->set_error_code(m_result.get_error_code());
  1339. // m_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)m_result.get_error_level());
  1340. // m_dispatch_response_msg.mutable_error_manager()->set_error_description(m_result.get_error_description());
  1341. //
  1342. //
  1343. // std::cout << " huli test :::: " << " m_dispatch_response_msg = " << m_dispatch_response_msg.DebugString()<< std::endl;
  1344. //
  1345. // std::string t_msg = m_dispatch_response_msg.SerializeAsString();
  1346. // System_communication::get_instance_references().encapsulate_task_msg(t_msg);
  1347. return Error_code::SUCCESS;
  1348. }
  1349. //发送调度答复信息给主控
  1350. Error_manager Dispatch_plc::send_dispatch_response_to_main_control_new()
  1351. {
  1352. // std::unique_lock<std::mutex> t_lock(m_lock);
  1353. //
  1354. // std::string t_msg;
  1355. // if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE )
  1356. // {
  1357. // measure_info t_measure_info = m_park_table_msg.entrance_measure_info();
  1358. // m_park_table_msg.mutable_actually_measure_info()->CopyFrom(t_measure_info);
  1359. // parkspace_info t_parkspace_info = m_park_table_msg.allocated_space_info();
  1360. // m_park_table_msg.mutable_actually_space_info()->CopyFrom(t_parkspace_info);
  1361. // //存车真实轴距
  1362. // m_park_table_msg.mutable_actually_measure_info()->set_wheelbase(m_response_car_wheel_base_exact_value);
  1363. //
  1364. // if ( m_result != Error_code::SUCCESS )
  1365. // {
  1366. // std::string t_error_string = m_result.to_string();
  1367. // m_park_table_msg.mutable_statu()->set_execute_statu(eError);
  1368. // m_park_table_msg.mutable_statu()->set_statu_description(t_error_string);
  1369. // }
  1370. // t_msg = m_park_table_msg.DebugString();
  1371. // }
  1372. // else if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP )
  1373. // {
  1374. // m_pick_table_msg.set_export_id(1);
  1375. // if ( m_result != Error_code::SUCCESS )
  1376. // {
  1377. // std::string t_error_string = m_result.to_string();
  1378. // m_pick_table_msg.mutable_statu()->set_execute_statu(eError);
  1379. // m_pick_table_msg.mutable_statu()->set_statu_description(t_error_string);
  1380. // }
  1381. // t_msg = m_pick_table_msg.DebugString();
  1382. // }
  1383. //
  1384. // System_communication::get_instance_references().encapsulate_task_msg(t_msg, 1);
  1385. // std::cout << " huli test :::: " << " m_command_completed_msg = " << t_msg << std::endl;
  1386. // Dispatch_manager::get_instance_references().ack_rabbitmq_message_new();
  1387. return Error_code::SUCCESS;
  1388. }