1
0

module_traffic_light.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <array>
  9. #include "ant.h"
  10. #include "module_traffic_light_common.h"
  11. #include "line.h"
  12. class client;
  13. // 红绿灯
  14. struct traffic_light: point{
  15. int m_light_id; // 红绿灯id
  16. int m_group_id; // 所属红绿灯组
  17. std::string m_ip; // 红绿灯IP
  18. int m_site_id; // 控制分站id
  19. int m_state; // 红绿灯状态,1-初始值,2-红灯,3-绿灯,4-闪烁
  20. int m_port; // 分站控制红绿灯的路数
  21. int m_phy_light_id; // 物理灯号,红绿灯正反面共享此灯号
  22. int m_phy_direction; // 物理灯朝向,1-正面,0-反面
  23. uint64_t m_rec_time; // 红绿灯接收时间
  24. int m_special; // 检查非巷道区域,0-正常,1-特殊
  25. double m_direct_distance; // 红绿灯到灯组坐标的距离
  26. line m_line_group; // 红绿灯与灯组坐标的连线
  27. int m_stream; // 上下行标志,1为上行,2为下行
  28. std::shared_ptr<client> m_clt;
  29. 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), m_stream(0)
  30. {
  31. m_clt = nullptr;
  32. }
  33. 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, int stream): 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), m_stream(stream)
  34. {
  35. m_clt = nullptr;
  36. }
  37. 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, int stream)
  38. {
  39. this->x = x;
  40. this->y = y;
  41. this->z = z;
  42. this->m_light_id = light_id;
  43. this->m_group_id = group_id;
  44. this->m_ip = ip;
  45. this->m_state = state;
  46. this->m_port = port;
  47. this->m_phy_light_id = pli;
  48. this->m_phy_direction = pd;
  49. this->m_special = special;
  50. this->m_stream = stream;
  51. }
  52. void set_state(int value = light_shape::green_all_on)
  53. {
  54. m_state = value;
  55. }
  56. void set_client(const std::shared_ptr<client>& clt)
  57. {
  58. if(m_clt != clt){
  59. m_clt = clt;
  60. }
  61. }
  62. std::shared_ptr<client> get_client()
  63. {
  64. return m_clt;
  65. }
  66. };
  67. using traffic_light_ptr = std::shared_ptr<traffic_light>;
  68. class traffic_light_group;
  69. using traffic_light_group_ptr = std::shared_ptr<traffic_light_group>;
  70. //红绿灯组
  71. struct traffic_light_group: point{
  72. int m_group_id; // 所属红绿灯组ID
  73. double m_scope; // 控制半径
  74. uint16_t m_manual_time; // 人工控制默认时间
  75. uint16_t m_auto_interval; // 断网情况下,自动控制间隔
  76. uint64_t m_card_id; // 如果优先级是1,表示小车id;优先级是2,表示打车id
  77. int m_map_id; // 地图ID
  78. int m_area_id; // 区域ID
  79. time_t m_time_stamp; // 控制时长,单位秒
  80. time_t m_ctrl_time; // 控制时间
  81. bool m_special; // 是否含有特殊红绿灯组
  82. int m_green_light_id; // 灯组被控制后变绿灯的灯ID
  83. std::string m_ctrl_name; // 控制用户
  84. std::atomic<int> m_priority; // 优先级,0-初始值,1-路口控制,2-避让控制;3-手动控制
  85. std::atomic<bool> m_used; // 是否使用
  86. std::atomic_flag m_owner_flag; // 控制权
  87. std::vector<traffic_light_ptr> m_vt_lights; // 组下红绿灯
  88. std::array<path, 2> m_path;
  89. int m_up_stream_idx; // 灯组按上行排序的索引值
  90. move_stream m_stream_state;
  91. 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)
  92. {
  93. m_vt_lights.clear();
  94. m_up_stream_idx = 0;
  95. m_owner_flag.clear();
  96. m_stream_state = move_stream::unknown;
  97. }
  98. 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)
  99. {
  100. m_priority = priority_init;
  101. m_vt_lights.clear();
  102. m_used = false;
  103. m_card_id = 0;
  104. m_up_stream_idx = 0;
  105. m_owner_flag.clear();
  106. m_stream_state = move_stream::unknown;
  107. }
  108. // 给红绿灯下发指令设置其颜色
  109. void send_cmd_light(traffic_light_ptr& ptl);
  110. void set_path(const std::vector<line_v>& vl);
  111. void set_path(const std::vector<line_v>& vl, std::vector<line_v>::const_iterator it);
  112. void set_crossing(const uint64_t& cid, const int& p)
  113. {
  114. m_card_id = cid;
  115. set_priority(p);
  116. }
  117. bool is_at_path(point p);
  118. // 设置避让
  119. void set_avoidance(const int& ld, const int& lc, const int& lcr)
  120. {
  121. m_card_id = 0;
  122. m_time_stamp = 0;
  123. set_priority(priority_avoidance);
  124. set_light(ld, lc, lcr);
  125. }
  126. // 设置优先权
  127. void set_priority(const int& p)
  128. {
  129. m_priority.store(p);
  130. }
  131. void set_up_stream_idx(const int& v)
  132. {
  133. m_up_stream_idx = v;
  134. }
  135. // 获取优先权
  136. int get_priority()
  137. {
  138. return m_priority.load();
  139. }
  140. // 设置状态
  141. void set_status(bool s)
  142. {
  143. m_used.store(s);
  144. }
  145. // 得到状态
  146. bool get_status()
  147. {
  148. return m_used.load();
  149. }
  150. move_stream get_stream_state()
  151. {
  152. return m_stream_state;
  153. }
  154. void set_stream_state(const move_stream& state)
  155. {
  156. m_stream_state = state;
  157. }
  158. move_stream get_stream(const int& cid);
  159. // 控制超时检查
  160. bool is_time_out()
  161. {
  162. if(m_time_stamp >= m_ctrl_time){
  163. return true;
  164. }
  165. return false;
  166. }
  167. void get_turn()
  168. {
  169. while(m_owner_flag.test_and_set(std::memory_order_acquire));
  170. }
  171. void release_turn()
  172. {
  173. m_owner_flag.clear(std::memory_order_release);
  174. }
  175. // 重置
  176. void reset();
  177. //设置灯组内红绿灯颜色,满足条件的设置为指定灯形状,不满足条件3个面的设为其他相同的灯形状
  178. void set_light(const int& lid, const int& lc, const int& lcr);
  179. // 增加红绿灯到灯组
  180. void insert(traffic_light_ptr ptl);
  181. // 检查灯在组内是否存在
  182. bool exist(const int& lid);
  183. //获取两个灯之间的距离
  184. bool is_different(const int& ld, const int& lc, const int& lcr);
  185. // 手动控制
  186. void set_manual_ctrl(const std::string& name, const int& ld, const int& lc);
  187. uint64_t get_green_light_id();
  188. // 上行灯与下行灯之间的距离,带方向
  189. double updown_dist();
  190. void update(double x, double y, double z, int group_id, double scope, int interval, int map_id, int area_id)
  191. {
  192. this->x = x;
  193. this->y = y;
  194. this->z = z;
  195. this->m_group_id = group_id;
  196. this->m_scope = scope;
  197. this->m_auto_interval = interval;
  198. this->m_map_id = map_id;
  199. this->m_area_id = area_id;
  200. }
  201. };
  202. struct traffic_light_data{
  203. bool m_bigger_flag; //大车标识
  204. std::list<traffic_light_ptr> m_traffic_group;
  205. traffic_light_data(): m_bigger_flag(false){
  206. m_traffic_group.clear();
  207. }
  208. };
  209. // 闯红灯
  210. struct run_red_light{
  211. int32_t m_group_id;
  212. int m_light_id;
  213. int m_vid;
  214. int32_t m_area_id;
  215. std::string m_occ_time; // 发生时间
  216. run_red_light(): m_group_id(0), m_light_id(0), m_vid(0), m_area_id(0), m_occ_time(0)
  217. {}
  218. };
  219. #endif