module_traffic_light_rule.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. #include <cfloat>
  2. #include "module_traffic_light_rule.h"
  3. #include "module_traffic_light_manager.h"
  4. // 车辆与指定灯组g距离比较,lhs近则返回true,否则返回false
  5. bool rule::sort_vehicle(const uint64_t lhs, const uint64_t rhs, const traffic_light_group_ptr& g)
  6. {
  7. pos_data* v1 = traffic_light_manager::instance()->get_position(lhs);
  8. pos_data* v2 = traffic_light_manager::instance()->get_position(rhs);
  9. double d1 = DBL_MAX, d2 = DBL_MAX;
  10. if(nullptr != v2){
  11. d2 = g->dist(v2->x, v2->y);
  12. }
  13. if(nullptr != v1){
  14. d1 = g->dist(v1->x, v1->y);
  15. }
  16. return d1 < d2;
  17. }
  18. // 根据车辆等级进行排序
  19. bool rule::sort_vehicle_by_level(const uint64_t lhs, const uint64_t rhs, const traffic_light_group_ptr& g)
  20. {
  21. //pos_data* lp = traffic_light_manager::instance()->get_position(lhs);
  22. //pos_data* rp = traffic_light_manager::instance()->get_position(rhs);
  23. //return lp->
  24. return true;
  25. }
  26. // 从灯组g中找到最近的灯
  27. traffic_light_ptr rule::find_nearby_light(const point& p, traffic_light_group_ptr g)
  28. {
  29. traffic_light_ptr pl = nullptr;
  30. double max = DBL_MAX;
  31. //查找最近的红绿灯
  32. for(auto it = g->m_vt_lights.begin(); it != g->m_vt_lights.end(); ++it)
  33. {
  34. traffic_light_ptr tmp = traffic_light_manager::instance()->find_light((*it)->m_light_id);
  35. double dt = tmp->dist(p.x, p.y);
  36. if(dt < max){
  37. max = dt;
  38. pl = tmp;
  39. }
  40. }
  41. return pl;
  42. }
  43. // 设置灯的颜色
  44. void crossing_rule::change_state(const uint64_t vid, traffic_light_group_ptr g)
  45. {
  46. pos_data* cp = traffic_light_manager::instance()->get_position(vid);
  47. if(nullptr != cp){
  48. // 查找离车辆最近的红绿灯
  49. traffic_light_ptr pl = find_nearby_light(point(cp->x, cp->y), g);
  50. // 设置来车方向为绿灯,其他方向为红灯
  51. if(nullptr != pl){
  52. log_info("[traffic_light] card_id=%lld, light_id=%d", vid, pl->m_light_id);
  53. g->set_light(pl->m_light_id, light_shape::green_all_on, light_shape::red_all_on);
  54. }
  55. }else{
  56. log_warn("[traffic_light] it can't get vehicle position. vid=%lld, lg's id=%d", vid, g->m_group_id);
  57. }
  58. }
  59. // 获得车辆是否大车的标记
  60. bool crossing_rule::get_vehicle_state(const uint64_t& vid)
  61. {
  62. bool s = false;
  63. pos_data* v = traffic_light_manager::instance()->get_position(vid);
  64. if(nullptr != v){
  65. s = v->m_bigger;
  66. }
  67. return s;
  68. }
  69. // 从红绿灯组中找距离最近的红绿灯
  70. // 后续改为找上行或下行灯
  71. bool crossing_rule::find_light(const uint64_t& vid, traffic_light_group_ptr g, bool a)
  72. {
  73. pos_data* cp = traffic_light_manager::instance()->get_position(vid);
  74. if(nullptr != cp){
  75. line l;
  76. traffic_light_ptr pl = find_nearby_light(point(cp->x, cp->y), g);
  77. if(nullptr == pl){
  78. return false;
  79. }
  80. if(!a){
  81. if(1 == pl->m_special){
  82. // 判断是否在线上
  83. if(pl->m_line_group.contain(cp->x, cp->y, 0.5)){
  84. // 判断行车方向
  85. double d = cp->m_speed * pl->m_direct_distance;
  86. if(d < 0){
  87. return true;
  88. }
  89. }
  90. }
  91. }else{
  92. // 判断是否在线上
  93. if(pl->m_line_group.contain(cp->x, cp->y, 1.0)){
  94. double d = cp->m_speed * pl->m_direct_distance;
  95. if(d < 0){
  96. return true;
  97. }
  98. }
  99. }
  100. }
  101. return false;
  102. }
  103. // 手工控制
  104. bool crossing_rule::handle_manual_ctrl(traffic_light_group_ptr g)
  105. {
  106. if(g->is_time_out()){
  107. g->reset();
  108. return true;
  109. }
  110. return false;
  111. }
  112. bool crossing_rule::handle_crossing(traffic_light_group_ptr group)
  113. {
  114. if(group->m_scope <= 0){
  115. return false;
  116. }
  117. // 查找一定范围的车辆
  118. std::vector<uint64_t> vtl = traffic_light_manager::instance()->find_nearby_vehicle(point(static_cast<int>(group->x), static_cast<int>(group->y)), group->m_scope, group->m_group_id);
  119. log_info("[traffic_light] handle crossing rule.gid=%d, group_scope=%.2f, vehicle's num=%d", group->m_group_id, group->m_scope, vtl.size());
  120. if(vtl.empty()){
  121. if(group->m_card_id > 0 && group->m_priority == priority_crossing)
  122. {
  123. // 释放红绿灯组
  124. group->reset();
  125. return true;
  126. }
  127. return false;
  128. }
  129. // 是否已经有原控制车辆记录
  130. if(group->m_card_id > 0)
  131. {
  132. // check stream
  133. if(group->get_stream_state() != group->get_stream(group->m_card_id)){
  134. log_info("[traffic_light] %d's vehicle is passed crossing that is gid=%d, old_stream=%d, cur_stream=%d",group->m_card_id, group->m_group_id, group->get_stream_state(), group->get_stream(group->m_card_id));
  135. traffic_light_manager::instance()->vtlg_erase(group->m_card_id, group);
  136. return false;
  137. }
  138. auto it = std::find(vtl.begin(), vtl.end(), group->m_card_id);
  139. if(it != vtl.end()){
  140. log_info("[traffic_light] handle crossing rule, there has vehicle that control the light group, card_id=%d", group->m_card_id);
  141. return false;
  142. }
  143. }
  144. std::sort(vtl.begin(), vtl.end(), [&](const uint64_t& lhs, const uint64_t& rhs){
  145. return sort_vehicle(lhs, rhs, group);
  146. });
  147. move_stream ms = group->get_stream(vtl[0]);
  148. pos_data* p = traffic_light_manager::instance()->get_position(vtl[0]);
  149. if((group->m_card_id == 0) && (p->m_speed * p->dist_direct(*group) < 0)){
  150. log_info("[traffic_light] gid=%d is not same to vehicle=%d direction", group->m_group_id, p->m_card_id);
  151. return false;
  152. }
  153. // 对需要控制的N个灯组进行控制
  154. std::vector<traffic_light_group_ptr> vt_groups = traffic_light_manager::instance()->find_stream_groups(group, ms);
  155. if(vt_groups.empty()){
  156. log_info("[traffic_light] 找不到灯组");
  157. return false;
  158. }
  159. std::string s = "[";
  160. for_each(vt_groups.begin(), vt_groups.end(), [&s](traffic_light_group_ptr g){ s.append(std::to_string(g->m_group_id));});
  161. s.append("]");
  162. log_info("[traffic_light] the %d's vehicle control light group of %s", vtl[0], s.c_str());
  163. for(auto it : vt_groups){
  164. traffic_light_manager::instance()->vtlg_insert(vtl[0], it);
  165. it->set_crossing(vtl[0], priority_crossing);
  166. it->set_stream_state(ms);
  167. change_state(vtl[0], it);
  168. }
  169. return true;
  170. }
  171. //避让规则处理
  172. //1)车辆等级规则
  173. //2)上下行规则
  174. bool crossing_rule::handle_avoidance(traffic_light_group_ptr g)
  175. {
  176. if(g->m_card_id == 0){
  177. return false;
  178. }
  179. bool ret = false;
  180. ret = avoidance_by_level(g);
  181. if(ret){
  182. return ret;
  183. }else{
  184. return avoidance_by_updown(g);
  185. }
  186. }
  187. // 根据车辆等级处理避让
  188. bool crossing_rule::avoidance_by_level(traffic_light_group_ptr& group)
  189. {
  190. // 根据车辆等级处理避让规则
  191. // 查找红绿灯组附近车辆
  192. auto vt = traffic_light_manager::instance()->find_nearby_vehicle(point(static_cast<int>(group->x), static_cast<int>(group->y)), group->m_scope, group->m_group_id);
  193. // 红绿灯组附近无车辆
  194. if(vt.empty()){
  195. if(priority_avoidance == group->get_priority() && group->m_card_id > 0){
  196. bool exist = traffic_light_manager::instance()->vtlg_exist(group->m_card_id, group->m_group_id);
  197. if(!exist){
  198. // 重置灯组控制权
  199. group->reset();
  200. }
  201. }
  202. return false;
  203. }
  204. if(vt.size() == 1 && vt[0] == group->m_card_id){
  205. log_info("[traffic_light] group is controlled, gid=%d, cid=%d", group->m_group_id, group->m_card_id);
  206. return false;
  207. }
  208. log_info("[traffic_light] handle avoidance_by_level, gid=%d, x=%.2f, y=%.2f, scope=%.2f, ctrl_cid=%d, ivehicle's size=%d", group->m_group_id, group->x, group->y, group->m_scope, group->m_card_id, vt.size());
  209. if(group->m_card_id > 0 && get_vehicle_state(group->m_card_id)){
  210. //检查是否释放控制权
  211. if(group->get_stream_state() != move_stream::unknown)
  212. {
  213. // 如果车辆运行方向与灯组上下行方向不一致,则重置
  214. if(group->get_stream_state() != group->get_stream(group->m_card_id))
  215. {
  216. traffic_light_manager::instance()->vtlg_erase(group->m_card_id, group);
  217. }
  218. }
  219. return false;
  220. }
  221. bool exist_bigger_car = false;
  222. if(group->m_card_id == 0){
  223. for(size_t i = 0;i < vt.size(); ++i){
  224. if(get_vehicle_state(vt[i])){
  225. exist_bigger_car = true;
  226. break;
  227. }
  228. }
  229. }
  230. if(exist_bigger_car || (group->m_card_id > 0 && !get_vehicle_state(group->m_card_id))){
  231. // only keep vehicle's state that is bigger flag
  232. vt.erase(std::remove_if(vt.begin(), vt.end(), [&](const uint64_t& vid){
  233. if(get_vehicle_state(vid)){
  234. return false;
  235. }else{
  236. return true;
  237. }
  238. }),
  239. vt.end());
  240. if(vt.empty()){
  241. return false;
  242. }
  243. }
  244. // 删除不在红绿灯组所在路径上的车辆
  245. vt.erase(std::remove_if(vt.begin(), vt.end(), [&](const uint64_t& vid){
  246. pos_data* p = traffic_light_manager::instance()->get_position(vid);
  247. if(group->is_at_path(*p)){
  248. return false;
  249. }else{
  250. return true;
  251. }
  252. }),
  253. vt.end());
  254. if(vt.empty()){
  255. return false;
  256. }
  257. // 排序,找到离之最近的大车
  258. std::sort(vt.begin(), vt.end(), [&](const uint64_t& vid, const uint64_t& cid){
  259. return sort_vehicle(vid, cid, group);
  260. });
  261. move_stream ms = group->get_stream(vt[0]);
  262. if(ms == move_stream::unknown){
  263. return false;
  264. }
  265. // 对需要控制的N个灯组进行控制
  266. std::vector<traffic_light_group_ptr> vt_groups = traffic_light_manager::instance()->find_stream_groups(group, ms);
  267. if(vt_groups.empty()){
  268. log_info("[traffic_light] 找不到灯组");
  269. return false;
  270. }
  271. std::string s = "[";
  272. for_each(vt_groups.begin(), vt_groups.end(), [&s](traffic_light_group_ptr g){ s.append(std::to_string(g->m_group_id));});
  273. s.append("]");
  274. log_info("[traffic_light] the %d's vehicle control light group of %s", vt[0], s.c_str());
  275. for(auto& it : vt_groups){
  276. traffic_light_manager::instance()->vtlg_insert(vt[0], it);
  277. it->set_crossing(vt[0], priority_avoidance);
  278. it->set_stream_state(ms);
  279. change_state(vt[0], it);
  280. }
  281. return true;
  282. }
  283. // 根据车辆上下行处理避让
  284. bool crossing_rule::avoidance_by_updown(traffic_light_group_ptr& group)
  285. {
  286. // 1.检查控制灯组的车辆是否为上行车,如果是,则直接返回,表示上行车辆已控制灯组
  287. // 2.如果是下行车,则查看新车辆是否为上行,如果是,则控制
  288. if(group->m_card_id){
  289. if(group->get_stream(group->m_card_id) == move_stream::up_stream){
  290. return false;
  291. }
  292. //检查是否释放控制权
  293. if(group->get_stream_state() != group->get_stream(group->m_card_id))
  294. {
  295. traffic_light_manager::instance()->vtlg_erase(group->m_card_id, group);
  296. return false;
  297. }
  298. }
  299. auto vt = traffic_light_manager::instance()->find_nearby_vehicle(point(static_cast<int>(group->x), static_cast<int>(group->y)), group->m_scope, group->m_group_id);
  300. if(vt.empty()){
  301. if(priority_avoidance == group->get_priority() && group->m_card_id > 0){
  302. bool exist = traffic_light_manager::instance()->vtlg_exist(group->m_card_id, group->m_group_id);
  303. if(!exist){
  304. // 重置灯组控制权
  305. group->reset();
  306. }
  307. }
  308. return false;
  309. }
  310. log_info("[traffic_light] handle avoidance_by_updown, gid=%d, x=%.2f, y=%.2f, scope=%.2f, vehicle's size=%d", group->m_group_id, group->x, group->y, group->m_scope, vt.size());
  311. bool exist_up_stream = false;
  312. if(group->m_card_id == 0){
  313. for(size_t i = 0; i < vt.size(); ++i){
  314. if(group->get_stream(vt[i]) == move_stream::up_stream){
  315. exist_up_stream = true;
  316. break;
  317. }
  318. }
  319. }
  320. if(exist_up_stream || group->m_card_id > 0){
  321. // erase vehicles that's state of down_stream
  322. vt.erase(std::remove_if(vt.begin(), vt.end(), [&](uint64_t cid){
  323. if(group->get_stream(cid) == move_stream::down_stream){
  324. return true;
  325. }else{
  326. return false;
  327. }
  328. }));
  329. if(vt.empty()){
  330. return false;
  331. }
  332. }
  333. log_info("[traffic_light] the number of vehicle that's state of up_stream = %d", vt.size());
  334. for(size_t i = 0;i < group->m_path.size();i++){
  335. log_info("%s", group->m_path[i].to_str().c_str());
  336. }
  337. // 根据车辆远近进行排序
  338. std::sort(vt.begin(), vt.end(), [&](const uint64_t& vid, const uint64_t& cid){
  339. return sort_vehicle(vid, cid, group);
  340. });
  341. // 每辆车上下行处理
  342. // 根据点与灯组的带方向距离和灯组中上行灯与下行灯之间的带方向距离信息,来判断车辆行径方向是上行or下行
  343. // 判断位置与红绿灯组的方向与速度得方向是否一致
  344. // 如果车辆静止不动的话,不考虑
  345. move_stream ms = group->get_stream(vt[0]);
  346. // 对需要控制的N个灯组进行控制
  347. std::vector<traffic_light_group_ptr> vt_groups = traffic_light_manager::instance()->find_stream_groups(group, ms);
  348. if(vt_groups.empty()){
  349. log_info("[traffic_light] 找不到灯组");
  350. return false;
  351. }
  352. std::string s = "[";
  353. for_each(vt_groups.begin(), vt_groups.end(), [&s](traffic_light_group_ptr g){ s.append(std::to_string(g->m_group_id));});
  354. s.append("]");
  355. log_info("[traffic_light] the %d's vehicle control light group of %s", vt[0], s.c_str());
  356. for(auto& it : vt_groups){
  357. if(it->m_card_id == 0
  358. || (ms == move_stream::up_stream && it->get_stream_state() == move_stream::down_stream)
  359. )
  360. {
  361. // 以下两种情况,改变灯组的控制权
  362. // 1.车辆为上行;
  363. // 2.灯组无控制权
  364. traffic_light_manager::instance()->vtlg_insert(vt[0], it);
  365. it->get_turn();
  366. it->set_crossing(vt[0], priority_avoidance);
  367. it->set_stream_state(ms);
  368. change_state(vt[0], it);
  369. log_info("[traffic_light] vehicle get group control, cid = %d, group_id=%d", vt[0], it->m_group_id);
  370. }
  371. }
  372. return true;
  373. }
  374. void crossing_rule::handle_rule(pos_data& p)
  375. {
  376. //log_info("[traffic_light] crossing_rule::handle_rule, group's size=%d", m_vt_group.size());
  377. for(auto& it : m_vt_group){
  378. // 获取控制权
  379. it->get_turn();
  380. int priority = it->get_priority();
  381. //log_info("[traffic_light] crossing_rule::handle_rule, gid=%d, priority=%d, special=%d", it->m_group_id, priority, it->m_special);
  382. bool flag = false;
  383. while(true){
  384. if(priority_manual_ctrl == priority){
  385. flag = handle_manual_ctrl(it);
  386. log_info("[traffic_light] crossing_rule::handle_rule, after call handle_manual_ctrl, flag=%d", flag);
  387. if(flag){
  388. break;
  389. }
  390. }
  391. if(priority <= priority_avoidance){
  392. flag = handle_avoidance(it);
  393. log_info("[traffic_light] crossing_rule::handle_rule, after call handle_avoidance, flag=%d", flag);
  394. if(flag){
  395. break;
  396. }
  397. }
  398. if(priority <= priority_crossing){
  399. flag = handle_crossing(it);
  400. log_info("[traffic_light] crossing_rule::handle_rule, after call handle_crossing, flag=%d", flag);
  401. if(flag){
  402. break;
  403. }
  404. }
  405. break;
  406. };
  407. it->release_turn();
  408. }
  409. }
  410. std::shared_ptr<run_red_light> crossing_rule::check_run_red_light(pos_data& p)
  411. {
  412. return nullptr;
  413. }
  414. // 查找灯组
  415. vt_traffic_group avoidance_rule::find_group(const pos_data& p)
  416. {
  417. return find_group(point(p.x, p.y), p.m_area_id, p.m_speed, p);
  418. }
  419. vt_traffic_group avoidance_rule::find_group(const point& po , int area_id, double speed, const pos_data& pd)
  420. {
  421. vt_traffic_group llist;
  422. if(0 == pd.m_card_id || 0 == pd.m_type){
  423. std::move(llist);
  424. }
  425. // 获取车辆所在地图区域内的所有红绿灯组
  426. vt_traffic_group vg = m_map_area_group[area_id];
  427. double dist = 0.0;
  428. for(std::size_t i = 0; i < vg.size(); ++i){
  429. traffic_light_group_ptr pg = vg[i];
  430. if(nullptr == pg){
  431. continue;
  432. }
  433. // 如果此灯组被控制了,则不允许使用
  434. if(vg[i]->get_status() && vg[i]->m_card_id != pd.m_card_id){
  435. log_info("[traffic_light] card_id=%lld was control light group, gid=%d", pd.m_card_id, vg[i]->m_group_id);
  436. continue;
  437. }
  438. // 求灯组到车辆的距离(车辆在灯组的左下方,则距离值取反)
  439. dist = vg[i]->dist_direct(po.x, po.y);
  440. // 车辆在灯组左边或下边
  441. log_info("[traffic_light] avoidance_rule::find_group, area_id=%d, area_group=%d, dist=%.2f", area_id, vg.size(), dist);
  442. if(dist <= 0){
  443. // 速度方向与前进方向一致
  444. if(speed > 0){
  445. // 车辆到灯组的距离小于指定距离
  446. if(vg[i]->dist(po.x, po.y) <= g_max_scope){
  447. llist.push_back(vg[i]);
  448. }
  449. // 紧挨着的一个灯组到车距离小于指定阈值
  450. if(i + 1 < vg.size()){
  451. if(vg[i+1]->dist(po.x, po.y) <= g_max_scope){
  452. llist.push_back(vg[i+1]);
  453. }
  454. }
  455. }else if(speed < 0){
  456. // 速度方向与前进方向不一致,判断运动方向的灯组是否在可控范围
  457. if(i >= 1 && vg[i - 1]->dist(po.x, po.y) <= g_max_scope){
  458. llist.push_back(vg[i-1]);
  459. }
  460. if(i >= 2 && vg[i-2]->dist(po.x, po.y) <= g_max_scope){
  461. llist.push_back(vg[i-2]);
  462. }
  463. }
  464. break;
  465. }
  466. log_info("[traffic_light] the vehicle is in light group's left or down side.");
  467. }
  468. // 车辆在灯组右边或上边
  469. if(dist > 0 && speed < 0){
  470. std::size_t i = vg.size();
  471. if(i >= 1 && vg[i-1]->dist(po.x, po.y) <= g_max_scope){
  472. llist.push_back(vg[i-1]);
  473. }
  474. if(i >= 2 && vg[i-2]->dist(po.x, po.y) <= g_max_scope){
  475. llist.push_back(vg[i-2]);
  476. }
  477. log_info("[traffic_light] the vehicle is in light group's right or up side");
  478. }
  479. return std::move(llist);
  480. }
  481. bool avoidance_rule::find_vehicle_in_group(vt_traffic_group& vg, std::vector<pos_data>& vv)
  482. {
  483. if(vv.size() < 2){
  484. log_error("[traffic_light] vv's size less than 2");
  485. return false;
  486. }
  487. bool flag = true;
  488. double d = vg[0]->dist(vv[1].x, vv[1].y);
  489. line tl(*vg[0], *vg[1]);
  490. if(tl.contain(vv[1].x, vv[1].y, 1))
  491. {
  492. if(vv[1].m_speed * vv[0].m_speed < 0 && d > g_mid_vehicle_length_group){
  493. flag = false;
  494. }
  495. }
  496. return flag;
  497. }
  498. bool avoidance_rule::get_vehicle(const pos_data& p, vt_traffic_group& vg)
  499. {
  500. double d = vg[0]->dist(*vg[1]);
  501. std::vector<uint64_t> vt1 = traffic_light_manager::instance()->find_nearby_vehicle(point(static_cast<int>(vg[0]->x), static_cast<int>(vg[0]->y)), static_cast<int>(d), vg[0]->m_card_id);
  502. std::vector<uint64_t> vt2 = traffic_light_manager::instance()->find_nearby_vehicle(point(static_cast<int>(vg[1]->x), static_cast<int>(vg[1]->y)), static_cast<int>(d), vg[1]->m_card_id);
  503. std::sort(vt1.begin(), vt1.end());
  504. std::sort(vt2.begin(), vt2.end());
  505. std::vector<uint64_t> vti;
  506. std::set_intersection(vt1.begin(), vt1.end(), vt2.begin(), vt2.end(), std::back_inserter(vti));
  507. if(vti.empty()){
  508. return false;
  509. }
  510. // 筛选出符合条件的车辆,离第一个红绿灯距离进行排序
  511. vti.erase(std::remove_if(vti.begin(), vti.end(), [&](const uint64_t& vid) -> bool {
  512. pos_data* tp = traffic_light_manager::instance()->get_position(vid);
  513. if(tp == nullptr){
  514. return true;
  515. }
  516. std::vector<pos_data> vv;
  517. vv.push_back(p);
  518. vv.push_back(*tp);
  519. return find_vehicle_in_group(vg, vv);
  520. }));
  521. return vti.empty()?false:true;
  522. }
  523. void avoidance_rule::insert(traffic_light_group_ptr g, pos_data& p, vt_traffic_group& vg, const int lc, const int lcr)
  524. {
  525. p.m_light_group.push_back(g->m_group_id);
  526. if(g->get_priority() < priority_avoidance){
  527. // 红绿灯组优先级取初始值0或路口控制1
  528. change_group(g, point(p.x, p.y), lc, lcr);
  529. vg.push_back(g);
  530. }
  531. traffic_light_ptr pl = find_nearby_light(point(p.x, p.y), g);
  532. g->m_green_light_id = pl->m_light_id;
  533. // 灯组绑定车辆的卡号
  534. g->m_card_id = p.m_card_id;
  535. g->set_status(true);
  536. }
  537. void avoidance_rule::erase(bool a, pos_data& p, vt_traffic_group& vg)
  538. {
  539. if(p.m_light_group.size() <= 0){
  540. return;
  541. }
  542. traffic_light_group_ptr pg = nullptr;
  543. bool flag = false;
  544. if(a){
  545. pg = traffic_light_manager::instance()->find_group(p.m_light_group.front());
  546. p.m_light_group.pop_front();
  547. }else{
  548. pg = traffic_light_manager::instance()->find_group(p.m_light_group.back());
  549. p.m_light_group.pop_back();
  550. }
  551. if(nullptr == pg){
  552. return;
  553. }
  554. pg->get_turn();
  555. if(pg->get_priority() <= priority_avoidance){
  556. // 重置灯组
  557. pg->reset();
  558. flag = true;
  559. }
  560. pg->release_turn();
  561. pg->set_status(false);
  562. if(flag){
  563. vg.push_back(pg);
  564. }
  565. }
  566. void avoidance_rule::change_group(const traffic_light_group_ptr& g, const point& p, const int& lc, const int& lcr)
  567. {
  568. // 1.取离p最近的灯组信息
  569. traffic_light_ptr pl = find_nearby_light(point(p.x, p.y), g);
  570. if(nullptr == pl){
  571. return;
  572. }
  573. log_info("[traffic_light] change light's status in %d group", g->m_group_id);
  574. // 2.获取灯组中临近车辆的灯id
  575. g->m_green_light_id = pl->m_light_id;
  576. g->get_turn();
  577. // 3.设置灯组中灯号为ld的灯颜色为lc,其余灯颜色为lcr
  578. g->set_avoidance(pl->m_light_id, lc, lcr);
  579. g->release_turn();
  580. }
  581. bool avoidance_rule::is_different(traffic_light_group_ptr g, const point& p, const int& lc, const int& lcr)
  582. {
  583. bool b = false;
  584. traffic_light_ptr pl = find_nearby_light(p, g);
  585. if(g->is_different(pl->m_light_id, lc, lcr)){
  586. b = true;
  587. }
  588. return b;
  589. }
  590. void avoidance_rule::handle_rule(pos_data& p)
  591. {
  592. vt_traffic_group llist;
  593. if(p.m_card_id == 0|| p.m_type == 0){
  594. return;
  595. }
  596. log_info("[traffic_light] avoidance_rule::handle_rule, group's size=%d, area_id=%d", m_map_area_group.size(), p.m_area_id);
  597. auto it = m_map_area_group.find(p.m_area_id);
  598. if(it != m_map_area_group.end()){
  599. // 根据车卡的定位坐标以及定位区域信息获取红绿灯组
  600. vt_traffic_group vtg = find_group(point(p.x, p.y), p.m_area_id, p.m_speed, p);
  601. log_info("[traffic_light] handle avoidance rule, area has light, area_id=%d, card_id=%d, light_group's size=%d", p.m_area_id, p.m_card_id, vtg.size());
  602. if(vtg.size() == 0){
  603. // 所在区域没有可以控制的红绿灯组
  604. switch(p.m_light_group.size())
  605. {
  606. case 1:
  607. // 当前车辆控制的路口为1个,释放该炉口控制
  608. erase(true, p, llist);
  609. break;
  610. case 2:
  611. // 当前车辆控制的路口为两个,释放这两个路口的控制
  612. erase(true, p, llist);
  613. erase(false, p, llist);
  614. break;
  615. }
  616. }else if(1 == vtg.size()){
  617. // 所在区域存在1个红绿灯组
  618. switch(p.m_light_group.size())
  619. {
  620. case 0:
  621. // 当前卡没有绑定红绿灯组,绑定此灯组,并设置灯组颜色为绿色
  622. insert(vtg[0], p, llist, green, red);
  623. break;
  624. case 1:
  625. //当前卡绑定了1个红绿灯组
  626. if(p.m_light_group.front() != vtg[0]->m_group_id){
  627. // 检查当前卡绑定的红绿灯组与区域找到的是不是同一个
  628. // 不是,释放之前的绑定,再绑定新灯组
  629. erase(true, p, llist);
  630. insert(vtg[0], p, llist, green, red);
  631. }else{
  632. // 当前卡绑定的红绿灯组与区域找到的是同一个
  633. traffic_light_ptr pl = find_nearby_light(point(p.x, p.y), vtg[0]);
  634. if(nullptr != pl && pl->m_light_id != vtg[0]->m_green_light_id){
  635. erase(true, p, llist);
  636. }
  637. }
  638. break;
  639. }
  640. }else if(2 == p.m_light_group.size()){
  641. // 当前卡绑定了2个红绿灯组
  642. traffic_light_group_ptr g = vtg[0];
  643. if(p.m_light_group.front() == g->m_group_id){
  644. // 第一个绑定的红绿灯组与区域找到的是同一个,删除第二个
  645. erase(false, p, llist);
  646. if(g->get_priority() <= priority_avoidance){
  647. change_group(g, point(p.x, p.y), green, red);
  648. llist.push_back(g);
  649. }
  650. }else if(p.m_light_group.back() == g->m_group_id){
  651. // 第二个绑定的红绿灯组与区域找到的是同一个,删除第一个
  652. erase(true, p, llist);
  653. if(g->get_priority() <= priority_avoidance){
  654. change_group(g, point(p.x, p.y), green, red);
  655. llist.push_back(g);
  656. }
  657. }else{
  658. // 两个都不是,全部删除
  659. erase(true, p, llist);
  660. erase(false, p, llist);
  661. insert(g, p, llist, green, red);
  662. }
  663. }else if(2 == vtg.size()){
  664. // 所在区域存在两个红绿灯组,巷道相遇逻辑
  665. // 判断两个灯组中是否有车
  666. bool b = get_vehicle(p, vtg);
  667. light_color la, lb;
  668. if(b){
  669. la = red;
  670. lb = spark;
  671. }else{
  672. la = green;
  673. lb = red;
  674. }
  675. switch(p.m_light_group.size()){
  676. case 0:
  677. // 当前卡没有绑定红绿灯组,绑定2个灯组
  678. insert(vtg[0], p, llist, la, lb);
  679. insert(vtg[1], p, llist, green, spark);
  680. break;
  681. case 1:
  682. // 当前卡有绑定1个红绿灯组,更新
  683. if(p.m_light_group.front() == vtg[0]->m_group_id){
  684. if(vtg[0]->get_priority() <= priority_avoidance){
  685. change_group(vtg[0], point(p.x, p.y), la, lb);
  686. llist.push_back(vtg[0]);
  687. }
  688. insert(vtg[1], p, llist, green, spark);
  689. }else if(p.m_light_group.front() == vtg[1]->m_group_id){
  690. if(vtg[1]->get_priority() <= priority_avoidance)
  691. {
  692. change_group(vtg[1], point(p.x, p.y), green, spark);
  693. llist.push_back(vtg[1]);
  694. }
  695. p.m_light_group.push_front(vtg[0]->m_group_id);
  696. if(vtg[0]->get_priority() < priority_avoidance)
  697. {
  698. change_group(vtg[0], point(p.x, p.y), la, lb);
  699. llist.push_back(vtg[0]);
  700. }
  701. }else{
  702. // 两个都不相等
  703. erase(false, p, llist);
  704. insert(vtg[0], p, llist, la, lb);
  705. insert(vtg[1], p, llist, green, spark);
  706. }
  707. break;
  708. case 2:
  709. // 当前卡有绑定2个红绿灯组,更新
  710. {
  711. std::list<int>::iterator ito = std::find(p.m_light_group.begin(), p.m_light_group.end(), vtg[0]->m_group_id);
  712. std::list<int>::iterator itt = std::find(p.m_light_group.begin(), p.m_light_group.end(), vtg[1]->m_group_id);
  713. if(ito != p.m_light_group.end())
  714. {
  715. if(itt != p.m_light_group.end())
  716. {
  717. if(p.m_light_group.front() == vtg[0]->m_group_id){
  718. if(is_different(vtg[0], point(p.x, p.y), la, lb)){
  719. llist.push_back(vtg[0]);
  720. }
  721. if(is_different(vtg[1], point(p.x, p.y), green, spark)){
  722. llist.push_back(vtg[1]);
  723. }
  724. }else{
  725. p.m_light_group.clear();
  726. if(vtg[0]->get_priority() <= priority_avoidance){
  727. change_group(vtg[0], point(p.x, p.y), la, lb);
  728. llist.push_back(vtg[0]);
  729. }
  730. if(vtg[1]->get_priority() <= priority_avoidance){
  731. change_group(vtg[1], point(p.x, p.y), green, spark);
  732. llist.push_back(vtg[1]);
  733. }
  734. p.m_light_group.push_back(vtg[0]->m_group_id);
  735. p.m_light_group.push_back(vtg[1]->m_group_id);
  736. }
  737. if(vtg[0]->get_priority() <= priority_avoidance){
  738. change_group(vtg[0], point(p.x, p.y), la, lb);
  739. llist.push_back(vtg[0]);
  740. }
  741. insert(vtg[1], p, llist, green, spark);
  742. }
  743. }
  744. else
  745. {
  746. if(itt != p.m_light_group.end()){
  747. if(p.m_light_group.front() == vtg[1]->m_group_id){
  748. erase(false, p, llist);
  749. }else{
  750. erase(true, p, llist);
  751. }
  752. erase(true, p, llist);
  753. erase(true, p, llist);
  754. insert(vtg[0], p, llist, la, lb);
  755. insert(vtg[1], p, llist, green, spark);
  756. }
  757. }
  758. }
  759. break;
  760. }
  761. }
  762. }else{
  763. log_info("[traffic_light] the %d area has no light group", p.m_area_id);
  764. }
  765. }
  766. std::shared_ptr<run_red_light> avoidance_rule::check_run_red_light(pos_data& p)
  767. {
  768. return nullptr;
  769. }