module_traffic_light.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifndef module_traffic_light_h
  2. #define module_traffic_light_h
  3. #include <memory>
  4. #include <ctime>
  5. #include <atomic>
  6. #include <vector>
  7. #include <list>
  8. #include "module_traffic_light_common.h"
  9. #include "line.h"
  10. // 红绿灯
  11. struct traffic_light: point{
  12. int m_light_id; // 红绿灯id
  13. int m_group_id; // 所属红绿灯组
  14. std::string m_ip; // 红绿灯IP
  15. int m_site_id; // 控制分站id
  16. int m_state; // 红绿灯状态,1-初始值,2-红灯,3-绿灯,4-闪烁
  17. int m_port; // 分站控制红绿灯的路数
  18. int m_phy_light_id; // 物理灯号,红绿灯正反面共享此灯号
  19. int m_phy_direction; // 物理灯朝向,1-正面,0-反面
  20. uint64_t m_rec_time; // 红绿灯接收时间
  21. int m_special; // 检查非巷道区域,0-正常,1-特殊
  22. double m_direct_distance; // 红绿灯到灯组坐标的距离
  23. line m_line_group; // 红绿灯与灯组坐标的连线
  24. traffic_light(): m_light_id(0), m_group_id(0), m_ip(""), m_state(0), m_port(0), m_phy_light_id(0), m_phy_direction(0), m_rec_time(0), m_special(0), m_direct_distance(0.0)
  25. {}
  26. traffic_light(double x, double y, double z, int light_id, int group_id, std::string ip, int state, int port, int pli, int pd, int special): point(x, y, z), m_light_id(light_id), m_group_id(group_id), m_ip(ip), m_state(state), m_port(port), m_phy_light_id(pli), m_phy_direction(pd), m_special(special)
  27. {
  28. }
  29. void update(double x, double y, double z, int light_id, int group_id, std::string ip, int state, int port, int pli, int pd, int special)
  30. {
  31. this->x = x;
  32. this->y = y;
  33. this->z = z;
  34. this->m_light_id = light_id;
  35. this->m_group_id = group_id;
  36. this->m_ip = ip;
  37. this->m_state = state;
  38. this->m_port = port;
  39. this->m_phy_light_id = pli;
  40. this->m_phy_direction = pd;
  41. this->m_special = special;
  42. }
  43. void set_state(int value = green)
  44. {
  45. m_state = value;
  46. }
  47. };
  48. using traffic_light_ptr = std::shared_ptr<traffic_light>;
  49. class traffic_light_group;
  50. using traffic_light_group_ptr = std::shared_ptr<traffic_light_group>;
  51. //红绿灯组
  52. struct traffic_light_group: point{
  53. int m_group_id; // 所属红绿灯组ID
  54. double m_scope; // 控制半径
  55. uint16_t m_manual_time; // 人工控制默认时间
  56. uint16_t m_auto_interval; // 断网情况下,自动控制间隔
  57. uint64_t m_card_id; // 如果优先级是1,表示小车id;优先级是2,表示打车id
  58. int m_map_id; // 地图ID
  59. int m_area_id; // 区域ID
  60. time_t m_time_stamp; // 控制时长,单位秒
  61. time_t m_ctrl_time; // 控制时间
  62. bool m_special; // 是否含有特殊红绿灯组
  63. int m_green_light_id; // 灯组被控制后变绿灯的灯ID
  64. std::string m_ctrl_name; // 控制用户
  65. std::atomic<int> m_priority; // 优先级,0-初始值,1-路口控制,2-避让控制;3-手动控制
  66. std::atomic<bool> m_used; // 是否使用
  67. std::atomic_flag m_owner_flag; // 控制权
  68. std::vector<traffic_light_ptr> m_vt_lights; // 组下红绿灯
  69. traffic_light_group(): point(0,0), m_group_id(0), m_scope(0.0), m_manual_time(0), m_auto_interval(0), m_card_id(0), m_map_id(0), m_area_id(0), m_time_stamp(0), m_ctrl_time(0),m_special(false), m_green_light_id(0), m_ctrl_name(""), m_priority(priority_init), m_used(false)
  70. {
  71. m_vt_lights.clear();
  72. }
  73. traffic_light_group(double x, double y, double z, int group_id, double scope, int interval, int map_id, int area_id): point(x, y, z), m_group_id(group_id), m_scope(scope), m_auto_interval(interval), m_map_id(map_id), m_area_id(area_id)
  74. {
  75. m_priority = priority_init;
  76. m_vt_lights.clear();
  77. m_used = false;
  78. }
  79. // 给红绿灯下发指令设置其颜色
  80. void send_cmd_light(traffic_light_ptr& ptl);
  81. void set_crossing(const uint64_t& cid, const int& p)
  82. {
  83. m_card_id = cid;
  84. set_priority(p);
  85. }
  86. // 设置避让
  87. void set_avoidance(const int& ld, const int& lc, const int& lcr)
  88. {
  89. m_card_id = 0;
  90. m_time_stamp = 0;
  91. set_priority(priority_avoidance);
  92. set_light(ld, lc, lcr);
  93. }
  94. // 设置优先权
  95. void set_priority(const int& p)
  96. {
  97. m_priority.store(p);
  98. }
  99. // 获取优先权
  100. int get_priority()
  101. {
  102. return m_priority.load();
  103. }
  104. // 设置状态
  105. void set_status(bool s)
  106. {
  107. m_used.store(s);
  108. }
  109. // 得到状态
  110. bool get_status()
  111. {
  112. return m_used.load();
  113. }
  114. // 控制超时检查
  115. bool is_time_out()
  116. {
  117. if(m_time_stamp >= m_ctrl_time){
  118. return true;
  119. }
  120. return false;
  121. }
  122. void get_turn()
  123. {
  124. while(m_owner_flag.test_and_set(std::memory_order_acquire))
  125. {}
  126. }
  127. void release_turn()
  128. {
  129. m_owner_flag.clear(std::memory_order_release);
  130. }
  131. // 重置
  132. void reset();
  133. //设置灯组内红绿灯颜色,满足条件的设置为指定灯形状,不满足条件3个面的设为其他相同的灯形状
  134. void set_light(const int& lid, const int& lc, const int& lcr);
  135. // 增加红绿灯到灯组
  136. void insert(traffic_light_ptr ptl);
  137. //获取两个灯之间的距离
  138. bool is_different(const int& ld, const int& lc, const int& lcr);
  139. // 手动控制
  140. void set_manual_ctrl(const std::string& name, const int& ld, const int& lc);
  141. uint64_t get_green_light_id();
  142. void update(double x, double y, double z, int group_id, double scope, int interval, int map_id, int area_id)
  143. {
  144. this->x = x;
  145. this->y = y;
  146. this->z = z;
  147. this->m_group_id = group_id;
  148. this->m_scope = scope;
  149. this->m_auto_interval = interval;
  150. this->m_map_id = map_id;
  151. this->m_area_id = area_id;
  152. }
  153. };
  154. struct traffic_light_data{
  155. bool m_bigger_flag; //大车标识
  156. std::list<traffic_light_ptr> m_traffic_group;
  157. traffic_light_data(): m_bigger_flag(false){
  158. m_traffic_group.clear();
  159. }
  160. };
  161. #endif