dispatch_coordinates.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. //
  2. // Created by huli on 2021/3/24.
  3. //
  4. #include "dispatch_coordinates.h"
  5. #include "../tool/proto_tool.h"
  6. Dispatch_coordinates::Dispatch_coordinates()
  7. {
  8. mpp_space_lock = nullptr;
  9. m_space_lock_rows = 0;
  10. m_space_lock_columns = 0;
  11. m_space_lock_total = 0;
  12. }
  13. Dispatch_coordinates::~Dispatch_coordinates()
  14. {
  15. dispatch_coordinates_uninit();
  16. }
  17. //初始化 调度的坐标系 模块。如下三选一
  18. Error_manager Dispatch_coordinates::dispatch_coordinates_init()
  19. {
  20. return dispatch_coordinates_init_from_protobuf(DISPATCH_COORDINATES_PARAMETER_PATH);
  21. }
  22. //初始化 调度的坐标系 模块。从文件读取
  23. Error_manager Dispatch_coordinates::dispatch_coordinates_init_from_protobuf(std::string prototxt_path)
  24. {
  25. Dispatch_proto::Dispatch_coordinates_parameter_all t_dispatch_coordinates_parameter_all;
  26. if(! proto_tool::read_proto_param(prototxt_path,t_dispatch_coordinates_parameter_all) )
  27. {
  28. return Error_manager(DISPATCH_COORDINATES_READ_PROTOBUF_ERROR,MINOR_ERROR,
  29. "Dispatch_coordinates read_proto_param failed");
  30. }
  31. return dispatch_coordinates_init_from_protobuf(t_dispatch_coordinates_parameter_all);
  32. }
  33. //初始化 调度的坐标系 模块。从protobuf读取
  34. Error_manager Dispatch_coordinates::dispatch_coordinates_init_from_protobuf(Dispatch_proto::Dispatch_coordinates_parameter_all& dispatch_coordinates_parameter_all)
  35. {
  36. LOG(INFO) << " ---Dispatch_coordinates::dispatch_coordinates_init_from_protobuf() run--- "<< this;
  37. Error_manager t_error;
  38. m_space_lock_rows = dispatch_coordinates_parameter_all.parkspace_number().rows();
  39. m_space_lock_columns = dispatch_coordinates_parameter_all.parkspace_number().columns();
  40. m_space_lock_total = dispatch_coordinates_parameter_all.parkspace_number().total();
  41. m_liftway_width = dispatch_coordinates_parameter_all.parkspace_number().liftway_width();
  42. mpp_space_lock = new space_device*[m_space_lock_rows];
  43. for (int i = 0; i < m_space_lock_rows; ++i)
  44. {
  45. mpp_space_lock[i] = new space_device[m_space_lock_columns];
  46. for (int j = 0; j < m_space_lock_columns; ++j)
  47. {
  48. //注意了, 这里一定要初始化为-1, 因为设备id=0 是有效设备
  49. mpp_space_lock[i][j].m_catcher_id = -1;
  50. mpp_space_lock[i][j].m_carrier_id = -1;
  51. }
  52. }
  53. int size;
  54. size = dispatch_coordinates_parameter_all.packspace_coordinates_parameters_size();
  55. for (int i = 0; i < size; ++i)
  56. {
  57. Point3D_tool::Point3D t_point3d;
  58. int id = dispatch_coordinates_parameter_all.packspace_coordinates_parameters(i).id();
  59. t_point3d.x = dispatch_coordinates_parameter_all.packspace_coordinates_parameters(i).x();
  60. t_point3d.y = dispatch_coordinates_parameter_all.packspace_coordinates_parameters(i).y();
  61. t_point3d.z = dispatch_coordinates_parameter_all.packspace_coordinates_parameters(i).z();
  62. m_packspace_coordinates_map[id] = t_point3d;
  63. }
  64. size = dispatch_coordinates_parameter_all.passageway_coordinates_parameters_size();
  65. for (int i = 0; i < size; ++i)
  66. {
  67. Point3D_tool::Point3D t_point3d;
  68. int id = dispatch_coordinates_parameter_all.passageway_coordinates_parameters(i).id();
  69. t_point3d.x = dispatch_coordinates_parameter_all.passageway_coordinates_parameters(i).x();
  70. t_point3d.y = dispatch_coordinates_parameter_all.passageway_coordinates_parameters(i).y();
  71. t_point3d.z = dispatch_coordinates_parameter_all.passageway_coordinates_parameters(i).z();
  72. m_passageway_coordinates_map[id] = t_point3d;
  73. }
  74. size = dispatch_coordinates_parameter_all.carrier_coordinates_parameters_size();
  75. for (int i = 0; i < size; ++i)
  76. {
  77. Point3D_tool::Point3D t_point3d;
  78. int id = dispatch_coordinates_parameter_all.carrier_coordinates_parameters(i).id();
  79. t_point3d.x = dispatch_coordinates_parameter_all.carrier_coordinates_parameters(i).x();
  80. t_point3d.y = dispatch_coordinates_parameter_all.carrier_coordinates_parameters(i).y();
  81. t_point3d.z = dispatch_coordinates_parameter_all.carrier_coordinates_parameters(i).z();
  82. m_carrier_coordinates_map[id] = t_point3d;
  83. }
  84. size = dispatch_coordinates_parameter_all.catcher_coordinates_parameters_size();
  85. for (int i = 0; i < size; ++i)
  86. {
  87. Point3D_tool::Point3D t_point3d;
  88. int id = dispatch_coordinates_parameter_all.catcher_coordinates_parameters(i).id();
  89. t_point3d.x = dispatch_coordinates_parameter_all.catcher_coordinates_parameters(i).x();
  90. t_point3d.y = dispatch_coordinates_parameter_all.catcher_coordinates_parameters(i).y();
  91. t_point3d.z = dispatch_coordinates_parameter_all.catcher_coordinates_parameters(i).z();
  92. m_catcher_coordinates_map[id] = t_point3d;
  93. }
  94. m_carrier_box.x_min = dispatch_coordinates_parameter_all.carrier_box_parameters().x_min();
  95. m_carrier_box.x_max = dispatch_coordinates_parameter_all.carrier_box_parameters().x_max();
  96. m_carrier_box.y_min = dispatch_coordinates_parameter_all.carrier_box_parameters().y_min();
  97. m_carrier_box.y_max = dispatch_coordinates_parameter_all.carrier_box_parameters().y_max();
  98. m_carrier_box.z_min = dispatch_coordinates_parameter_all.carrier_box_parameters().z_min();
  99. m_carrier_box.z_max = dispatch_coordinates_parameter_all.carrier_box_parameters().z_max();
  100. m_catcher_box.x_min = dispatch_coordinates_parameter_all.catcher_box_parameters().x_min();
  101. m_catcher_box.x_max = dispatch_coordinates_parameter_all.catcher_box_parameters().x_max();
  102. m_catcher_box.y_min = dispatch_coordinates_parameter_all.catcher_box_parameters().y_min();
  103. m_catcher_box.y_max = dispatch_coordinates_parameter_all.catcher_box_parameters().y_max();
  104. m_catcher_box.z_min = dispatch_coordinates_parameter_all.catcher_box_parameters().z_min();
  105. m_catcher_box.z_max = dispatch_coordinates_parameter_all.catcher_box_parameters().z_max();
  106. m_catcher_b_min = dispatch_coordinates_parameter_all.catcher_b_min();
  107. m_catcher_b_max = dispatch_coordinates_parameter_all.catcher_b_max();
  108. m_catcher_d1_min = dispatch_coordinates_parameter_all.catcher_d1_min();
  109. m_catcher_d1_max = dispatch_coordinates_parameter_all.catcher_d1_max();
  110. m_catcher_d2_min = dispatch_coordinates_parameter_all.catcher_d2_min();
  111. m_catcher_d2_max = dispatch_coordinates_parameter_all.catcher_d2_max();
  112. std::cout << " huli test :::: " << " m_packspace_coordinates_map.size() = " << m_packspace_coordinates_map.size() << std::endl;
  113. std::cout << " huli test :::: " << " m_passageway_coordinates_map.size() = " << m_passageway_coordinates_map.size() << std::endl;
  114. std::cout << " huli test :::: " << " m_carrier_coordinates_map.size() = " << m_carrier_coordinates_map.size() << std::endl;
  115. std::cout << " huli test :::: " << " m_catcher_coordinates_map.size() = " << m_catcher_coordinates_map.size() << std::endl;
  116. m_default_wheelbase = dispatch_coordinates_parameter_all.default_wheelbase();
  117. m_catcher_1th_floor_z = dispatch_coordinates_parameter_all.catcher_1th_floor_z();
  118. m_catcher_2th_floor_z = dispatch_coordinates_parameter_all.catcher_2th_floor_z();
  119. m_catcher_3th_floor_z = dispatch_coordinates_parameter_all.catcher_3th_floor_z();
  120. m_catcher_4th_floor_z = dispatch_coordinates_parameter_all.catcher_4th_floor_z();
  121. m_carrier_1th_floor_z = dispatch_coordinates_parameter_all.carrier_1th_floor_z();
  122. m_carrier_2th_floor_z = dispatch_coordinates_parameter_all.carrier_2th_floor_z();
  123. m_carrier_3th_floor_z = dispatch_coordinates_parameter_all.carrier_3th_floor_z();
  124. m_carrier_4th_floor_z = dispatch_coordinates_parameter_all.carrier_4th_floor_z();
  125. m_catcher_d1_d2_distance = dispatch_coordinates_parameter_all.catcher_d1_d2_distance();
  126. m_catcher_wheel_base_limit = dispatch_coordinates_parameter_all.catcher_wheel_base_limit();
  127. m_carrier_y_y1_distance = dispatch_coordinates_parameter_all.carrier_y_y1_distance();
  128. m_carrier_default_y1_back = dispatch_coordinates_parameter_all.carrier_default_y1_back();
  129. m_carrier_default_y1_leave = dispatch_coordinates_parameter_all.carrier_default_y1_leave();
  130. m_carrier_default_y_back = dispatch_coordinates_parameter_all.carrier_default_y_back();
  131. m_carrier_default_y_leave = dispatch_coordinates_parameter_all.carrier_default_y_leave();
  132. m_carrier_default_x_left = dispatch_coordinates_parameter_all.carrier_default_x_left();
  133. m_carrier_default_x_right = dispatch_coordinates_parameter_all.carrier_default_x_right();
  134. m_passageway_terminal_id_min = dispatch_coordinates_parameter_all.passageway_terminal_id_min();
  135. m_passageway_terminal_id_max = dispatch_coordinates_parameter_all.passageway_terminal_id_max();
  136. m_passageway_terminal_number = dispatch_coordinates_parameter_all.passageway_terminal_number();
  137. size = dispatch_coordinates_parameter_all.passageway_functioning_patterns_size();
  138. for (int i = m_passageway_terminal_id_min; i < size; ++i)
  139. {
  140. m_passageway_functioning_pattern_map[i] = (Passageway_functioning_pattern)dispatch_coordinates_parameter_all.passageway_functioning_patterns(i);
  141. }
  142. if ( m_passageway_terminal_number != m_passageway_terminal_id_max - m_passageway_terminal_id_min +1 ||
  143. m_passageway_terminal_number != size)
  144. {
  145. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  146. " Dispatch_coordinates::dispatch_coordinates_init_from_protobuf PARAMRTER ERROR ");
  147. }
  148. m_parkspace_id_min = dispatch_coordinates_parameter_all.parkspace_id_min();
  149. m_parkspace_id_max = dispatch_coordinates_parameter_all.parkspace_id_max();
  150. m_parkspace_id_total = dispatch_coordinates_parameter_all.parkspace_id_total();
  151. m_third_floor_parkspace_id_min = dispatch_coordinates_parameter_all.third_floor_parkspace_id_min();
  152. m_third_floor_parkspace_id_max = dispatch_coordinates_parameter_all.third_floor_parkspace_id_max();
  153. m_third_floor_parkspace_id_total = dispatch_coordinates_parameter_all.third_floor_parkspace_id_total();
  154. m_car_wheel_base_min = dispatch_coordinates_parameter_all.car_wheel_base_min();
  155. m_car_wheel_base_max = dispatch_coordinates_parameter_all.car_wheel_base_max();
  156. m_separated_distance_z = dispatch_coordinates_parameter_all.m_separated_distance_z();
  157. m_separated_distance_x = dispatch_coordinates_parameter_all.m_separated_distance_x();
  158. return Error_code::SUCCESS;
  159. }
  160. //反初始化
  161. Error_manager Dispatch_coordinates::dispatch_coordinates_uninit()
  162. {
  163. for (int i = 0; i < m_space_lock_rows; ++i)
  164. {
  165. delete [](mpp_space_lock[i]);
  166. }
  167. delete [](mpp_space_lock);
  168. return Error_code::SUCCESS;
  169. }
  170. //获取2点之间的路径长度, 只算x轴和z轴, 只是计算一个大概值
  171. Error_manager Dispatch_coordinates::get_distance(int coordinates_id_1, int coordinates_id_2, Parkspace_path parkspace_path, float & distance)
  172. {
  173. //是否在1楼的标记位,
  174. float t_distance_x = 0;
  175. float t_distance_z = 0;
  176. //检查合法性
  177. if ( m_carrier_coordinates_map.find(coordinates_id_1) != m_carrier_coordinates_map.end() &&
  178. m_carrier_coordinates_map.find(coordinates_id_2) != m_carrier_coordinates_map.end() )
  179. {
  180. //计算距离
  181. if(coordinates_id_1 == coordinates_id_2)
  182. {
  183. distance = 0;
  184. }
  185. else if ( m_carrier_coordinates_map[coordinates_id_1].z == m_carrier_coordinates_map[coordinates_id_2].z )
  186. {
  187. distance = m_carrier_coordinates_map[coordinates_id_1].x - m_carrier_coordinates_map[coordinates_id_2].x;
  188. if ( distance<0 )
  189. {
  190. distance = -distance;
  191. }
  192. }
  193. else if ( parkspace_path == Dispatch_coordinates::Parkspace_path::LEFT_PATH )
  194. {
  195. t_distance_x = (m_carrier_coordinates_map[coordinates_id_1].x - m_carrier_default_x_left) +
  196. (m_carrier_coordinates_map[coordinates_id_2].x - m_carrier_default_x_left);
  197. t_distance_z = m_carrier_coordinates_map[coordinates_id_1].z - m_carrier_coordinates_map[coordinates_id_2].z;
  198. if ( t_distance_z<0 )
  199. {
  200. t_distance_z = -t_distance_z;
  201. }
  202. distance = t_distance_x + t_distance_z;
  203. }
  204. else if ( parkspace_path == Dispatch_coordinates::Parkspace_path::RIGHT_PATH )
  205. {
  206. t_distance_x = (m_carrier_default_x_right - m_carrier_coordinates_map[coordinates_id_1].x ) +
  207. (m_carrier_default_x_right - m_carrier_coordinates_map[coordinates_id_2].x );
  208. t_distance_z = m_carrier_coordinates_map[coordinates_id_1].z - m_carrier_coordinates_map[coordinates_id_2].z;
  209. if ( t_distance_z<0 )
  210. {
  211. t_distance_z = -t_distance_z;
  212. }
  213. distance = t_distance_x + t_distance_z;
  214. }
  215. else if ( parkspace_path == Dispatch_coordinates::Parkspace_path::TEMPORARY_CACHE_PATH )
  216. {
  217. t_distance_x = m_carrier_coordinates_map[coordinates_id_1].x - m_carrier_coordinates_map[coordinates_id_2].x;
  218. if ( t_distance_x<0 )
  219. {
  220. t_distance_x = -t_distance_x;
  221. }
  222. t_distance_z = m_carrier_coordinates_map[coordinates_id_1].z - m_carrier_coordinates_map[coordinates_id_2].z;
  223. if ( t_distance_z<0 )
  224. {
  225. t_distance_z = -t_distance_z;
  226. }
  227. distance = t_distance_x + t_distance_z;
  228. }
  229. else
  230. {
  231. return Error_manager(Error_code::DISPATCH_COORDINATES_PATH_ERROR, Error_level::MINOR_ERROR,
  232. " Dispatch_coordinates::get_distance parkspace_path error ");
  233. }
  234. }
  235. else
  236. {
  237. return Error_manager(Error_code::DISPATCH_COORDINATES_ID_ERROR, Error_level::MINOR_ERROR,
  238. " Dispatch_coordinates::get_distance coordinates_id error ");
  239. }
  240. return Error_code::SUCCESS;
  241. }
  242. //把1楼出入口的坐标修正到2楼.
  243. bool Dispatch_coordinates::amend_coordinates_from_1st_floor_to_2nd_floor(int & coordinates_id)
  244. {
  245. if ( coordinates_id >= PASSAGEWAY_ID_BASE+1 && coordinates_id<= PASSAGEWAY_ID_BASE + m_passageway_terminal_id_max-1)
  246. {
  247. coordinates_id = coordinates_id+100; //例如:把 1103 改为 1203
  248. return true;
  249. }
  250. else
  251. {
  252. return false;
  253. }
  254. return false;
  255. }
  256. //选择最近的搬运器, return true:选左边, return false:选右边
  257. bool Dispatch_coordinates::choice_nearest_carrier(int dispatch_source, int coordinates_id_1, int coordinates_id_2)
  258. {
  259. Error_manager t_error;
  260. //选最近的搬运器
  261. float t_distance_left = 0;
  262. t_error = get_distance(dispatch_source, coordinates_id_1, Dispatch_coordinates::Parkspace_path::LEFT_PATH, t_distance_left );
  263. if ( t_error != Error_code::SUCCESS )
  264. {
  265. LOG(INFO) << " Dispatch_manager::resource_allocation() m_process_pickup_list error = "<< t_error.to_string() << " " << this;
  266. }
  267. float t_distance_right = 0;
  268. t_error = get_distance(dispatch_source, coordinates_id_2, Dispatch_coordinates::Parkspace_path::RIGHT_PATH, t_distance_right );
  269. if ( t_error != Error_code::SUCCESS )
  270. {
  271. LOG(INFO) << " Dispatch_manager::resource_allocation() m_process_pickup_list error = "<< t_error.to_string() << " " << this;
  272. }
  273. if ( t_distance_left <= t_distance_right )
  274. {
  275. return true;
  276. }
  277. else
  278. {
  279. return false;
  280. }
  281. }
  282. //判断取车的路径
  283. Dispatch_coordinates::Parkspace_path Dispatch_coordinates::get_pickup_path(int dispatch_source, int dispatch_destination)
  284. {
  285. if ( dispatch_source >= 1 && dispatch_source <= 165 &&
  286. dispatch_destination >= 1100 && dispatch_destination <= 1107)
  287. {
  288. if ( dispatch_source >= 19 && dispatch_source <= 27 )
  289. {
  290. if ( dispatch_destination >= 1101 && dispatch_destination <= 1106 )
  291. {
  292. return Dispatch_coordinates::Parkspace_path::THIRD_FLOOR_PATH;
  293. }
  294. else
  295. {
  296. return Dispatch_coordinates::Parkspace_path::UNKNOW_PATH;
  297. }
  298. }
  299. else
  300. {
  301. int t_columns = dispatch_source % 15;
  302. if ( (t_columns ==1 || t_columns == 2 || t_columns ==3) ||
  303. (dispatch_source >= 34 && dispatch_source <= 37) )
  304. {
  305. if ( dispatch_destination >= 1100 && dispatch_destination <= 1106 )
  306. {
  307. return Dispatch_coordinates::Parkspace_path::LEFT_PATH;
  308. }
  309. else
  310. {
  311. return Dispatch_coordinates::Parkspace_path::UNKNOW_PATH;
  312. }
  313. }
  314. else if ( (t_columns ==13 || t_columns == 14 || t_columns ==0) ||
  315. (dispatch_source >= 38 && dispatch_source <= 42) )
  316. {
  317. if ( dispatch_destination >= 1101 && dispatch_destination <= 1107 )
  318. {
  319. return Dispatch_coordinates::Parkspace_path::RIGHT_PATH;
  320. }
  321. else
  322. {
  323. return Dispatch_coordinates::Parkspace_path::UNKNOW_PATH;
  324. }
  325. }
  326. else
  327. {
  328. if ( dispatch_destination >= 1101 && dispatch_destination <= 1106 )
  329. {
  330. return Dispatch_coordinates::Parkspace_path::DOUBLE_PATH;
  331. }
  332. else if ( dispatch_destination == 1100 )
  333. {
  334. return Dispatch_coordinates::Parkspace_path::LEFT_PATH;
  335. }
  336. else if ( dispatch_destination == 1107 )
  337. {
  338. return Dispatch_coordinates::Parkspace_path::RIGHT_PATH;
  339. }
  340. else
  341. {
  342. return Dispatch_coordinates::Parkspace_path::UNKNOW_PATH;
  343. }
  344. }
  345. }
  346. }
  347. else
  348. {
  349. return Dispatch_coordinates::Parkspace_path::UNKNOW_PATH;
  350. }
  351. return Dispatch_coordinates::Parkspace_path::UNKNOW_PATH;
  352. }
  353. //检查停车位和取车位, 判断是否能改道
  354. bool Dispatch_coordinates::is_able_redirect(int pickup_parkspace_id, int store_parkspace_id)
  355. {
  356. return true;
  357. }
  358. //搬运器尝试空间加锁
  359. Error_manager Dispatch_coordinates::carrier_try_space_lock(int row_min, int column_min, int row_max, int column_max, int carrier_id, int& avoid_catcher_id )
  360. {
  361. std::unique_lock<std::mutex> t_lock(m_lock);
  362. bool try_flag = true;
  363. avoid_catcher_id = -1;
  364. for (int i = row_min; i <= row_max; ++i)
  365. {
  366. for (int j = column_min; j <= column_max ; ++j)
  367. {
  368. if ( mpp_space_lock[i][j].m_carrier_id != -1 &&
  369. mpp_space_lock[i][j].m_carrier_id != carrier_id )
  370. {
  371. try_flag = false;
  372. break;
  373. }
  374. if ( mpp_space_lock[i][j].m_catcher_id != -1 )
  375. {
  376. try_flag = false;
  377. avoid_catcher_id = mpp_space_lock[i][j].m_catcher_id;
  378. break;
  379. }
  380. }
  381. }
  382. if ( try_flag )
  383. {
  384. for (int i = row_min; i <= row_max; ++i)
  385. {
  386. for (int j = column_min; j <= column_max ; ++j)
  387. {
  388. mpp_space_lock[i][j].m_carrier_id = carrier_id;
  389. }
  390. }
  391. return Error_code::SUCCESS;
  392. }
  393. else
  394. {
  395. return Error_manager(Error_code::DISPATCH_COORDINATES_CAN_NOT_LOCK, Error_level::MINOR_ERROR,
  396. " Dispatch_coordinates::carrier_try_space_lock error ");
  397. }
  398. }
  399. //抓取器尝试空间加锁
  400. Error_manager Dispatch_coordinates::catcher_try_space_lock(int row_min, int column_min, int row_max, int column_max, int catcher_id )
  401. {
  402. std::unique_lock<std::mutex> t_lock(m_lock);
  403. bool try_flag = true;
  404. for (int i = row_min; i <= row_max; ++i)
  405. {
  406. for (int j = column_min; j <= column_max ; ++j)
  407. {
  408. if ( (mpp_space_lock[i][j].m_catcher_id != -1 && mpp_space_lock[i][j].m_catcher_id != catcher_id)
  409. || mpp_space_lock[i][j].m_carrier_id != -1)
  410. {
  411. try_flag = false;
  412. break;
  413. }
  414. }
  415. }
  416. if ( try_flag )
  417. {
  418. for (int i = row_min; i <= row_max; ++i)
  419. {
  420. for (int j = column_min; j <= column_max ; ++j)
  421. {
  422. mpp_space_lock[i][j].m_catcher_id = catcher_id;
  423. }
  424. }
  425. return Error_code::SUCCESS;
  426. }
  427. else
  428. {
  429. return Error_manager(Error_code::DISPATCH_COORDINATES_CAN_NOT_LOCK, Error_level::MINOR_ERROR,
  430. " Dispatch_coordinates::catcher_try_space_lock error ");
  431. }
  432. }
  433. //搬运器强制空间加锁(不做判断了, 直接加锁)
  434. Error_manager Dispatch_coordinates::carrier_force_space_lock(int row_min, int column_min, int row_max, int column_max, int carrier_id )
  435. {
  436. //不做判断了, 直接加锁
  437. std::unique_lock<std::mutex> t_lock(m_lock);
  438. for (int i = row_min; i <= row_max; ++i)
  439. {
  440. for (int j = column_min; j <= column_max ; ++j)
  441. {
  442. mpp_space_lock[i][j].m_carrier_id = carrier_id;
  443. }
  444. }
  445. return Error_code::SUCCESS;
  446. }
  447. //抓取器强制空间加锁(不做判断了, 直接加锁)
  448. Error_manager Dispatch_coordinates::catcher_force_space_lock(int row_min, int column_min, int row_max, int column_max, int catcher_id )
  449. {
  450. //不做判断了, 直接加锁
  451. std::unique_lock<std::mutex> t_lock(m_lock);
  452. for (int i = row_min; i <= row_max; ++i)
  453. {
  454. for (int j = column_min; j <= column_max ; ++j)
  455. {
  456. mpp_space_lock[i][j].m_catcher_id = catcher_id;
  457. }
  458. }
  459. return Error_code::SUCCESS;
  460. }
  461. //搬运器解锁, 退回电梯井, 全部解锁
  462. Error_manager Dispatch_coordinates::carrier_space_unlock(int carrier_id)
  463. {
  464. std::unique_lock<std::mutex> t_lock(m_lock);
  465. for (int i = 0; i < m_space_lock_rows; ++i)
  466. {
  467. for (int j = 0; j < m_space_lock_columns ; ++j)
  468. {
  469. if ( mpp_space_lock[i][j].m_carrier_id == carrier_id )
  470. {
  471. mpp_space_lock[i][j].m_carrier_id = -1;
  472. }
  473. }
  474. }
  475. return Error_code::SUCCESS;
  476. }
  477. //抓取器解锁, 上升到最上层, 全部解锁, 保留4楼的空间锁
  478. Error_manager Dispatch_coordinates::catcher_space_unlock(int catcher_coordinates_id, int catcher_id)
  479. {
  480. std::unique_lock<std::mutex> t_lock(m_lock);
  481. for (int i = 0; i < m_space_lock_rows; ++i)
  482. {
  483. for (int j = 0; j < m_space_lock_columns ; ++j)
  484. {
  485. if ( mpp_space_lock[i][j].m_catcher_id == catcher_id )
  486. {
  487. mpp_space_lock[i][j].m_catcher_id = -1;
  488. }
  489. }
  490. }
  491. //保留4楼的空间锁
  492. if ( catcher_coordinates_id >= 34 && catcher_coordinates_id <= 42 )
  493. {
  494. int t_coordinates_columns = catcher_coordinates_id -30-1;
  495. catcher_3rd_4st_floor_space_lock(t_coordinates_columns, catcher_id);
  496. }
  497. else if ( catcher_coordinates_id >= 1401 && catcher_coordinates_id <= 1406 )
  498. {
  499. int t_coordinates_columns = catcher_coordinates_id - 1400;
  500. int temp = (t_coordinates_columns-1)%2;
  501. t_coordinates_columns = 4 + ((t_coordinates_columns-1)/2)*3 + temp*2 -1;
  502. catcher_3rd_4st_floor_space_lock(t_coordinates_columns, catcher_id);
  503. }
  504. else
  505. {
  506. return Error_manager(Error_code::DISPATCH_COORDINATES_ID_ERROR, Error_level::MINOR_ERROR,
  507. " Dispatch_coordinates::catcher_space_unlock error ");
  508. }
  509. return Error_code::SUCCESS;
  510. }
  511. //抓取器 加锁3楼和4楼的2*3个车位
  512. Error_manager Dispatch_coordinates::catcher_3rd_4st_floor_space_lock(int catcher_4st_floor_coordinates_columns, int catcher_id)
  513. {
  514. //周围2*3个车位 加锁
  515. mpp_space_lock[1][catcher_4st_floor_coordinates_columns-1].m_catcher_id = catcher_id;
  516. mpp_space_lock[1][catcher_4st_floor_coordinates_columns].m_catcher_id = catcher_id;
  517. mpp_space_lock[1][catcher_4st_floor_coordinates_columns+1].m_catcher_id = catcher_id;
  518. mpp_space_lock[2][catcher_4st_floor_coordinates_columns-1].m_catcher_id = catcher_id;
  519. mpp_space_lock[2][catcher_4st_floor_coordinates_columns].m_catcher_id = catcher_id;
  520. mpp_space_lock[2][catcher_4st_floor_coordinates_columns+1].m_catcher_id = catcher_id;
  521. return Error_code::SUCCESS;
  522. }
  523. void Dispatch_coordinates::set_space_lock(int space_rows, int space_columns, int catcher_id, int carrier_id)
  524. {
  525. std::unique_lock<std::mutex> t_lock(m_lock);
  526. mpp_space_lock[space_rows][space_columns].m_catcher_id = catcher_id;
  527. mpp_space_lock[space_rows][space_columns].m_carrier_id = carrier_id;
  528. }
  529. void Dispatch_coordinates::set_space_lock_for_catcher(int space_rows, int space_columns, int catcher_id)
  530. {
  531. std::unique_lock<std::mutex> t_lock(m_lock);
  532. mpp_space_lock[space_rows][space_columns].m_catcher_id = catcher_id;
  533. }
  534. void Dispatch_coordinates::set_space_lock_for_carrier(int space_rows, int space_columns, int carrier_id)
  535. {
  536. std::unique_lock<std::mutex> t_lock(m_lock);
  537. mpp_space_lock[space_rows][space_columns].m_carrier_id = carrier_id;
  538. }
  539. void Dispatch_coordinates::set_space_lock(int space_id, int catcher_id, int carrier_id)
  540. {
  541. int space_rows = (space_id-1) / m_space_lock_columns;
  542. int space_columns = (space_id-1) % m_space_lock_columns;
  543. set_space_lock(space_rows, space_columns, catcher_id, carrier_id);
  544. }
  545. void Dispatch_coordinates::set_space_lock_for_catcher(int space_id, int catcher_id)
  546. {
  547. int space_rows = (space_id-1) / m_space_lock_columns;
  548. int space_columns = (space_id-1) % m_space_lock_columns;
  549. set_space_lock_for_catcher(space_rows, space_columns, catcher_id);
  550. }
  551. void Dispatch_coordinates::set_space_lock_for_carrier(int space_id, int carrier_id)
  552. {
  553. int space_rows = (space_id-1) / m_space_lock_columns;
  554. int space_columns = (space_id-1) % m_space_lock_columns;
  555. set_space_lock_for_carrier(space_rows, space_columns, carrier_id);
  556. }
  557. void Dispatch_coordinates::cout_space_lock()
  558. {
  559. std::unique_lock<std::mutex> t_lock(m_lock);
  560. std::cout << " huli test :::: " << " Dispatch_coordinates::cout_space_lock() = " << "mpp_space_lock" << std::endl;
  561. for (int i = m_space_lock_rows-1; i >=0; --i)
  562. {
  563. for (int j = m_space_lock_columns-1; j >=0 ; --j)
  564. {
  565. if ( mpp_space_lock[i][j].m_carrier_id == -1 )
  566. {
  567. printf("( _");
  568. }
  569. else
  570. {
  571. printf("(%d_", mpp_space_lock[i][j].m_carrier_id);
  572. }
  573. if ( mpp_space_lock[i][j].m_catcher_id == -1 )
  574. {
  575. printf(" ) ");
  576. }
  577. else
  578. {
  579. printf("%d) ", mpp_space_lock[i][j].m_catcher_id);
  580. }
  581. }
  582. std::cout << std::endl;
  583. }
  584. }