1
0

module_traffic_light_rule.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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) * traffic_light_manager::instance()->get_scale();
  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, light_shape=%d", vid, pl->m_light_id, light_shape::green_all_on);
  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, 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, 5.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] crossing_rule::avoidance_by_level, group is controlled, gid=%d, cid=%d", group->m_group_id, group->m_card_id);
  206. return false;
  207. }
  208. log_info("[traffic_light] crossing_rule::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. log_info("[traffic_light] handle avoidance_by_updown, gid=%d, control_card_id=%d, exist_up_stream_vehicle=%s", group->m_group_id, group->m_card_id, (exist_up_stream?"true":"false"));
  321. if(exist_up_stream || group->m_card_id > 0){
  322. // erase vehicles that's state of down_stream
  323. vt.erase(std::remove_if(vt.begin(), vt.end(), [&](uint64_t cid){
  324. if(group->get_stream(cid) == move_stream::down_stream){
  325. return true;
  326. }else{
  327. return false;
  328. }
  329. }), vt.end());
  330. if(vt.empty()){
  331. //log_info("[traffic_light] handle avoidance_by_updown")
  332. return false;
  333. }
  334. }
  335. log_info("[traffic_light] the number of vehicle that's state of up_stream = %d", vt.size());
  336. for(size_t i = 0;i < group->m_path.size();i++){
  337. log_info("%s", group->m_path[i].to_str().c_str());
  338. }
  339. // 根据车辆远近进行排序
  340. std::sort(vt.begin(), vt.end(), [&](const uint64_t& vid, const uint64_t& cid){
  341. return sort_vehicle(vid, cid, group);
  342. });
  343. // 每辆车上下行处理
  344. // 根据点与灯组的带方向距离和灯组中上行灯与下行灯之间的带方向距离信息,来判断车辆行径方向是上行or下行
  345. // 判断位置与红绿灯组的方向与速度得方向是否一致
  346. // 如果车辆静止不动的话,不考虑
  347. move_stream ms = group->get_stream(vt[0]);
  348. // 对需要控制的N个灯组进行控制
  349. std::vector<traffic_light_group_ptr> vt_groups = traffic_light_manager::instance()->find_stream_groups(group, ms);
  350. if(vt_groups.empty()){
  351. log_info("[traffic_light] 找不到灯组");
  352. return false;
  353. }
  354. std::string s = "[";
  355. for_each(vt_groups.begin(), vt_groups.end(), [&s](traffic_light_group_ptr g){ s.append(std::to_string(g->m_group_id));});
  356. s.append("]");
  357. log_info("[traffic_light] the %d's vehicle control light group of %s", vt[0], s.c_str());
  358. for(auto& it : vt_groups){
  359. if(it->m_card_id == 0
  360. || (ms == move_stream::up_stream && it->get_stream_state() == move_stream::down_stream)
  361. )
  362. {
  363. // 以下两种情况,改变灯组的控制权
  364. // 1.车辆为上行;
  365. // 2.灯组无控制权
  366. traffic_light_manager::instance()->vtlg_insert(vt[0], it);
  367. it->get_turn();
  368. it->set_crossing(vt[0], priority_avoidance);
  369. it->set_stream_state(ms);
  370. change_state(vt[0], it);
  371. log_info("[traffic_light] vehicle get group control, cid = %d, group_id=%d", vt[0], it->m_group_id);
  372. }
  373. }
  374. return true;
  375. }
  376. void crossing_rule::handle_rule(pos_data& p)
  377. {
  378. //log_info("[traffic_light] crossing_rule::handle_rule, group's size=%d", m_vt_group.size());
  379. if(m_pos.size()<2){
  380. log_warn("[traffic_light] pos_data's size = %d", m_pos.size());
  381. return;
  382. }
  383. for(auto& it : m_vt_group){
  384. // 获取控制权
  385. it->get_turn();
  386. int priority = it->get_priority();
  387. //log_info("[traffic_light] crossing_rule::handle_rule, gid=%d, priority=%d, special=%d", it->m_group_id, priority, it->m_special);
  388. bool flag = false;
  389. while(true){
  390. if(priority_manual_ctrl == priority){
  391. flag = handle_manual_ctrl(it);
  392. //log_info("[traffic_light] crossing_rule::handle_rule, after call handle_manual_ctrl, flag=%d", flag);
  393. if(flag){
  394. break;
  395. }
  396. }
  397. if(priority <= priority_avoidance){
  398. flag = handle_avoidance(it);
  399. //log_info("[traffic_light] crossing_rule::handle_rule, after call handle_avoidance, flag=%d", flag);
  400. if(flag){
  401. break;
  402. }
  403. }
  404. if(priority <= priority_crossing){
  405. flag = handle_crossing(it);
  406. //log_info("[traffic_light] crossing_rule::handle_rule, after call handle_crossing, flag=%d", flag);
  407. if(flag){
  408. break;
  409. }
  410. }
  411. break;
  412. };
  413. it->release_turn();
  414. }
  415. }
  416. std::shared_ptr<run_red_light> crossing_rule::check_run_red_light(pos_data& p)
  417. {
  418. return nullptr;
  419. }
  420. // 查找灯组
  421. vt_traffic_group avoidance_rule::find_group(const pos_data& p)
  422. {
  423. return find_group(point(p.x, p.y), p.m_area_id, p.m_speed, p);
  424. }
  425. vt_traffic_group avoidance_rule::find_group(const point& po , int area_id, double speed, const pos_data& pd)
  426. {
  427. vt_traffic_group llist;
  428. if(0 == pd.m_card_id || 0 == pd.m_type){
  429. std::move(llist);
  430. }
  431. // 获取车辆所在地图区域内的所有红绿灯组
  432. vt_traffic_group vg = m_map_area_group[area_id];
  433. double dist = 0.0;
  434. double scale = traffic_light_manager::instance()->get_scale();
  435. double alpha = 0.0;
  436. int gid = 0;
  437. for(std::size_t i = 0; i < vg.size(); ++i){
  438. traffic_light_group_ptr pg = vg[i];
  439. if(nullptr == pg){
  440. continue;
  441. }
  442. // 如果此灯组被控制了,则不允许使用
  443. if(vg[i]->get_status() && vg[i]->m_card_id != pd.m_card_id){
  444. log_info("[traffic_light] card_id=%lld was control light group, gid=%d", pd.m_card_id, vg[i]->m_group_id);
  445. continue;
  446. }
  447. // 求灯组到车辆的距离(车辆在灯组的左下方,则距离值取反)
  448. dist = m_pos[1].dist_direct(vg[i]->x, vg[i]->y) * scale;
  449. point vp1(vg[i]->x - m_pos[0].x, vg[i]->y - m_pos[0].y), vp2(m_pos[1].x - m_pos[0].x, m_pos[1].y - m_pos[0].y);
  450. int f1 = (vg[i]->x > m_pos[0].x ? 1 : -1);
  451. int f2 = (m_pos[1].x > m_pos[0].x ? 1: -1);
  452. if(f1*f2 > 0 && fabs(dist) < vg[i]->m_scope)
  453. {
  454. double tmp = (vp1.x*vp2.x + vp1.y*vp2.y)/(sqrt(vp1.x*vp1.x + vp1.y+vp1.y) * sqrt(vp2.x*vp2.x + vp2.y*vp2.y));
  455. tmp = (tmp>0?(tmp>1?fabs(tmp-1):tmp):(tmp>-1?fabs(tmp):fabs(tmp+1)));
  456. if(tmp < alpha){
  457. alpha = tmp;
  458. gid = vg[i]->m_group_id;
  459. }
  460. llist.push_back(vg[i]);
  461. log_info("[traffic_light] avoidance_rule::find_group, possiable light_group_id=%d, alpha=%.2f, area_id=%d, area_light_group_size=%d, scope=%.2f, dist=%.2f, min_alpha=%.2f, speed=%.2f, direction=%d", vg[i]->m_group_id, tmp, area_id, vg.size(), vg[i]->m_scope, dist, alpha, speed, f1*f2);
  462. }
  463. }
  464. if(gid > 0){
  465. llist.erase(std::remove_if(llist.begin(), llist.end(), [&](const traffic_light_group_ptr& g){
  466. if(g->m_group_id != gid){
  467. return true;
  468. }else{
  469. return false;
  470. }
  471. }), llist.end());
  472. }
  473. //检查接收基站所在路径有没有此灯组,没有则删除
  474. std::vector<pos_data> vtp;
  475. vtp.insert(vtp.begin(), m_pos.begin(), m_pos.end());
  476. llist.erase(std::remove_if(llist.begin(), llist.end(), [&](const traffic_light_group_ptr& g){
  477. if(!traffic_light_manager::instance()->on_path(vtp, point(g->x, g->y))){
  478. log_info("[traffic_light] the %d light group is not on %d site path.", g->m_group_id, vtp[0].m_site_id);
  479. return true;
  480. }else{
  481. log_info("[traffic_light] the %d light group is on %d site path.", g->m_group_id, vtp[0].m_site_id);
  482. return false;
  483. }
  484. }),
  485. llist.end()
  486. );
  487. return std::move(llist);
  488. }
  489. bool avoidance_rule::find_vehicle_in_group(vt_traffic_group& vg, std::vector<pos_data>& vv)
  490. {
  491. if(vv.size() < 2){
  492. log_error("[traffic_light] vv's size less than 2");
  493. return false;
  494. }
  495. bool flag = true;
  496. double d = vg[0]->dist(vv[1].x, vv[1].y);
  497. line tl(*vg[0], *vg[1]);
  498. if(tl.contain(vv[1].x, vv[1].y, 1))
  499. {
  500. if(vv[1].m_speed * vv[0].m_speed < 0 && d > g_mid_vehicle_length_group){
  501. flag = false;
  502. }
  503. }
  504. return flag;
  505. }
  506. bool avoidance_rule::get_vehicle(const pos_data& p, vt_traffic_group& vg)
  507. {
  508. double d = vg[0]->dist(*vg[1]);
  509. 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);
  510. 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);
  511. std::sort(vt1.begin(), vt1.end());
  512. std::sort(vt2.begin(), vt2.end());
  513. std::vector<uint64_t> vti;
  514. std::set_intersection(vt1.begin(), vt1.end(), vt2.begin(), vt2.end(), std::back_inserter(vti));
  515. // 筛选出符合条件的车辆,离第一个红绿灯距离进行排序
  516. vti.erase(std::remove_if(vti.begin(), vti.end(), [&](const uint64_t& vid) -> bool {
  517. pos_data* tp = traffic_light_manager::instance()->get_position(vid);
  518. if(tp == nullptr){
  519. return true;
  520. }
  521. std::vector<pos_data> vv;
  522. vv.push_back(p);
  523. vv.push_back(*tp);
  524. return find_vehicle_in_group(vg, vv);
  525. }));
  526. return vti.empty()?false:true;
  527. }
  528. void avoidance_rule::insert(traffic_light_group_ptr g, pos_data& p, vt_traffic_group& vg, const int lc, const int lcr)
  529. {
  530. p.m_light_group.push_back(g->m_group_id);
  531. if(g->get_priority() < priority_avoidance){
  532. // 红绿灯组优先级取初始值0或路口控制1
  533. change_group(g, point(p.x, p.y), lc, lcr);
  534. vg.push_back(g);
  535. }
  536. traffic_light_ptr pl = find_nearby_light(point(p.x, p.y), g);
  537. g->m_green_light_id = pl->m_light_id;
  538. // 灯组绑定车辆的卡号
  539. g->m_card_id = p.m_card_id;
  540. g->set_status(true);
  541. }
  542. void avoidance_rule::erase(bool a, pos_data& p, vt_traffic_group& vg)
  543. {
  544. if(p.m_light_group.size() <= 0){
  545. return;
  546. }
  547. traffic_light_group_ptr pg = nullptr;
  548. bool flag = false;
  549. if(a){
  550. pg = traffic_light_manager::instance()->find_group(p.m_light_group.front());
  551. p.m_light_group.pop_front();
  552. }else{
  553. pg = traffic_light_manager::instance()->find_group(p.m_light_group.back());
  554. p.m_light_group.pop_back();
  555. }
  556. if(nullptr == pg){
  557. return;
  558. }
  559. pg->get_turn();
  560. if(pg->get_priority() <= priority_avoidance){
  561. // 重置灯组
  562. pg->reset();
  563. flag = true;
  564. }
  565. pg->release_turn();
  566. pg->set_status(false);
  567. if(flag){
  568. vg.push_back(pg);
  569. }
  570. }
  571. void avoidance_rule::change_group(const traffic_light_group_ptr& g, const point& p, const int& lc, const int& lcr)
  572. {
  573. // 1.取离p最近的灯组信息
  574. traffic_light_ptr pl = find_nearby_light(point(p.x, p.y), g);
  575. if(nullptr == pl){
  576. return;
  577. }
  578. log_info("[traffic_light] change light's status in the %d's light group", g->m_group_id);
  579. // 2.获取灯组中临近车辆的灯id
  580. g->m_green_light_id = pl->m_light_id;
  581. g->get_turn();
  582. // 3.设置灯组中灯号为ld的灯颜色为lc,其余灯颜色为lcr
  583. g->set_avoidance(pl->m_light_id, lc, lcr);
  584. g->release_turn();
  585. }
  586. void avoidance_rule::change_group(const traffic_light_group_ptr& g, const move_stream& s, const int& lc, const int& lcr)
  587. {
  588. g->get_turn();
  589. for_each(g->m_vt_lights.begin(), g->m_vt_lights.end(), [&](traffic_light_ptr& l){
  590. if(l->m_stream == s){
  591. g->m_green_light_id = l->m_light_id;
  592. g->set_light(l->m_light_id, lc, lcr);
  593. }
  594. });
  595. g->release_turn();
  596. }
  597. bool avoidance_rule::is_different(traffic_light_group_ptr g, const point& p, const int& lc, const int& lcr)
  598. {
  599. bool b = false;
  600. traffic_light_ptr pl = find_nearby_light(p, g);
  601. if(g->is_different(pl->m_light_id, lc, lcr)){
  602. b = true;
  603. }
  604. return b;
  605. }
  606. void avoidance_rule::handle_rule(pos_data& p)
  607. {
  608. if(m_pos.size() < 2){
  609. log_warn("[traffic_light] pos_data's size = %d", m_pos.size());
  610. return;
  611. }
  612. vt_traffic_group llist;
  613. if(p.m_card_id == 0|| p.m_type == 0){
  614. return;
  615. }
  616. log_info("[traffic_light] avoidance_rule::handle_rule, group's size=%d, area_id=%d his_pos's size=%d, pos0.x=%.2f, pos0.y=%.2f, pos1.x=%.2f, pos2.y=%.2f", m_map_area_group.size(), p.m_area_id, m_pos.size(), m_pos[0].x, m_pos[0].y, m_pos[1].x, m_pos[1].y);
  617. auto it = m_map_area_group.find(p.m_area_id);
  618. if(it != m_map_area_group.end()){
  619. // 根据车卡的定位坐标以及定位区域信息获取红绿灯组
  620. vt_traffic_group vtg = find_group(point(p.x, p.y), p.m_area_id, p.m_speed, p);
  621. //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());
  622. if(vtg.size() == 0){
  623. // 所在区域没有可以控制的红绿灯组
  624. switch(p.m_light_group.size())
  625. {
  626. case 1:
  627. // 当前车辆控制的路口为1个,释放该路口控制
  628. erase(true, p, llist);
  629. break;
  630. case 2:
  631. // 当前车辆控制的路口为两个,释放这两个路口的控制
  632. erase(true, p, llist);
  633. erase(false, p, llist);
  634. break;
  635. }
  636. }else if(1 == vtg.size()){
  637. // 所在区域存在1个红绿灯组
  638. switch(p.m_light_group.size())
  639. {
  640. case 0:
  641. // 当前卡没有绑定红绿灯组,绑定此灯组,并设置灯组颜色为绿色
  642. insert(vtg[0], p, llist, light_shape::green_all_on, light_shape::red_all_on);
  643. break;
  644. case 1:
  645. //当前卡绑定了1个红绿灯组
  646. if(p.m_light_group.front() != vtg[0]->m_group_id){
  647. // 检查当前卡绑定的红绿灯组与区域找到的是不是同一个
  648. // 不是,释放之前的绑定,再绑定新灯组
  649. erase(true, p, llist);
  650. insert(vtg[0], p, llist, light_shape::green_all_on, light_shape::red_all_on);
  651. }else{
  652. // 当前卡绑定的红绿灯组与区域找到的是同一个
  653. traffic_light_ptr pl = find_nearby_light(point(p.x, p.y), vtg[0]);
  654. if(nullptr != pl && pl->m_light_id != vtg[0]->m_green_light_id){
  655. erase(true, p, llist);
  656. }
  657. }
  658. break;
  659. }
  660. }else if(2 == p.m_light_group.size()){
  661. // 当前卡绑定了2个红绿灯组
  662. traffic_light_group_ptr g = vtg[0];
  663. if(p.m_light_group.front() == g->m_group_id){
  664. // 第一个绑定的红绿灯组与区域找到的是同一个,删除第二个
  665. erase(false, p, llist);
  666. if(g->get_priority() <= priority_avoidance){
  667. change_group(g, point(p.x, p.y), light_shape::green_all_on, light_shape::red_all_on);
  668. llist.push_back(g);
  669. }
  670. }else if(p.m_light_group.back() == g->m_group_id){
  671. // 第二个绑定的红绿灯组与区域找到的是同一个,删除第一个
  672. erase(true, p, llist);
  673. if(g->get_priority() <= priority_avoidance){
  674. change_group(g, point(p.x, p.y), light_shape::green_all_on, light_shape::red_all_on);
  675. llist.push_back(g);
  676. }
  677. }else{
  678. // 两个都不是,全部删除
  679. erase(true, p, llist);
  680. erase(false, p, llist);
  681. insert(g, p, llist, light_shape::green_all_on, light_shape::red_all_on);
  682. }
  683. }else if(2 == vtg.size()){
  684. // 所在区域存在两个红绿灯组,巷道相遇逻辑
  685. // 判断两个灯组中是否有车
  686. bool b = get_vehicle(p, vtg);
  687. int la, lb;
  688. if(b){
  689. la = light_shape::red_all_on;
  690. lb = light_shape::red_spark;
  691. }else{
  692. la = light_shape::green_all_on;
  693. lb = light_shape::red_all_on;
  694. }
  695. switch(p.m_light_group.size()){
  696. case 0:
  697. // 当前卡没有绑定红绿灯组,绑定2个灯组
  698. insert(vtg[0], p, llist, la, lb);
  699. insert(vtg[1], p, llist, light_shape::green_all_on, light_shape::green_spark);
  700. break;
  701. case 1:
  702. // 当前卡有绑定1个红绿灯组,更新
  703. if(p.m_light_group.front() == vtg[0]->m_group_id){
  704. if(vtg[0]->get_priority() <= priority_avoidance){
  705. change_group(vtg[0], point(p.x, p.y), la, lb);
  706. llist.push_back(vtg[0]);
  707. }
  708. insert(vtg[1], p, llist, light_shape::green_all_on, light_shape::green_spark);
  709. }else if(p.m_light_group.front() == vtg[1]->m_group_id){
  710. if(vtg[1]->get_priority() <= priority_avoidance)
  711. {
  712. change_group(vtg[1], point(p.x, p.y), light_shape::green_all_on, light_shape::green_spark);
  713. llist.push_back(vtg[1]);
  714. }
  715. p.m_light_group.push_front(vtg[0]->m_group_id);
  716. if(vtg[0]->get_priority() < priority_avoidance)
  717. {
  718. change_group(vtg[0], point(p.x, p.y), la, lb);
  719. llist.push_back(vtg[0]);
  720. }
  721. }else{
  722. // 两个都不相等
  723. erase(false, p, llist);
  724. insert(vtg[0], p, llist, la, lb);
  725. insert(vtg[1], p, llist, light_shape::green_all_on, light_shape::green_spark);
  726. }
  727. break;
  728. case 2:
  729. // 当前卡有绑定2个红绿灯组,更新
  730. {
  731. std::list<int>::iterator ito = std::find(p.m_light_group.begin(), p.m_light_group.end(), vtg[0]->m_group_id);
  732. std::list<int>::iterator itt = std::find(p.m_light_group.begin(), p.m_light_group.end(), vtg[1]->m_group_id);
  733. if(ito != p.m_light_group.end())
  734. {
  735. if(itt != p.m_light_group.end())
  736. {
  737. if(p.m_light_group.front() == vtg[0]->m_group_id){
  738. if(is_different(vtg[0], point(p.x, p.y), la, lb)){
  739. llist.push_back(vtg[0]);
  740. }
  741. if(is_different(vtg[1], point(p.x, p.y), light_shape::green_all_on, light_shape::red_all_on)){
  742. llist.push_back(vtg[1]);
  743. }
  744. }else{
  745. p.m_light_group.clear();
  746. if(vtg[0]->get_priority() <= priority_avoidance){
  747. change_group(vtg[0], point(p.x, p.y), la, lb);
  748. llist.push_back(vtg[0]);
  749. }
  750. if(vtg[1]->get_priority() <= priority_avoidance){
  751. change_group(vtg[1], point(p.x, p.y), light_shape::green_all_on, light_shape::green_spark);
  752. llist.push_back(vtg[1]);
  753. }
  754. p.m_light_group.push_back(vtg[0]->m_group_id);
  755. p.m_light_group.push_back(vtg[1]->m_group_id);
  756. }
  757. if(vtg[0]->get_priority() <= priority_avoidance){
  758. change_group(vtg[0], point(p.x, p.y), la, lb);
  759. llist.push_back(vtg[0]);
  760. }
  761. insert(vtg[1], p, llist, light_shape::green_all_on, light_shape::green_spark);
  762. }
  763. else
  764. {
  765. if(itt != p.m_light_group.end()){
  766. if(p.m_light_group.front() == vtg[1]->m_group_id){
  767. erase(false, p, llist);
  768. }else{
  769. erase(true, p, llist);
  770. }
  771. erase(true, p, llist);
  772. erase(true, p, llist);
  773. insert(vtg[0], p, llist, la, lb);
  774. insert(vtg[1], p, llist, light_shape::green_all_on, light_shape::green_spark);
  775. }
  776. }
  777. }
  778. break;
  779. }
  780. }
  781. }
  782. else if(vtg.size() > 2){
  783. log_info("[traffic_light] control light group's size = %d", vtg.size());
  784. for(auto it : vtg){
  785. move_stream s = it->get_stream(p.m_card_id);
  786. change_group(it, s, light_shape::green_all_on, light_shape::green_spark);
  787. }
  788. }
  789. }else{
  790. log_info("[traffic_light] the %d area has no light group", p.m_area_id);
  791. }
  792. }
  793. std::shared_ptr<run_red_light> avoidance_rule::check_run_red_light(pos_data& p)
  794. {
  795. return nullptr;
  796. }