module_traffic_light.h 8.2 KB

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