module_traffic_light.h 8.1 KB

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