module_traffic_light.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. void set_state(int value = green)
  27. {
  28. m_state = value;
  29. }
  30. };
  31. using traffic_light_ptr = std::shared_ptr<traffic_light>;
  32. class traffic_light_group;
  33. using traffic_light_group_ptr = std::shared_ptr<traffic_light_group>;
  34. //红绿灯组
  35. struct traffic_light_group: point{
  36. int m_group_id; // 所属红绿灯组ID
  37. double m_scope; // 控制半径
  38. uint16_t m_manual_time; // 人工控制默认时间
  39. uint16_t m_auto_interval; // 断网情况下,自动控制间隔
  40. uint64_t m_card_id; // 如果优先级是1,表示小车id;优先级是2,表示打车id
  41. int m_map_id; // 地图ID
  42. int m_area_id; // 区域ID
  43. time_t m_time_stamp; // 控制时长,单位秒
  44. time_t m_ctrl_time; // 控制时间
  45. bool m_special; // 是否含有特殊红绿灯组
  46. int m_green_light_id; // 灯组被控制后变绿灯的灯ID
  47. std::string m_ctrl_name; // 控制用户
  48. std::atomic<int> m_priority; // 优先级,0-初始值,1-路口控制,2-避让控制;3-手动控制
  49. std::atomic<bool> m_used; // 是否使用
  50. std::atomic_flag m_owner_flag; // 控制权
  51. std::vector<traffic_light_ptr> m_vt_lights; // 组下红绿灯
  52. 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)
  53. {
  54. m_vt_lights.clear();
  55. }
  56. // 给红绿灯下发指令设置其颜色
  57. void send_cmd_light(traffic_light_ptr& ptl);
  58. void set_crossing(const uint64_t& cid, const int& p)
  59. {
  60. m_card_id = cid;
  61. set_priority(p);
  62. }
  63. // 设置避让
  64. void set_avoidance(const int& ld, const int& lc, const int& lcr)
  65. {
  66. m_card_id = 0;
  67. m_time_stamp = 0;
  68. set_priority(priority_avoidance);
  69. set_light(ld, lc, lcr);
  70. }
  71. // 设置优先权
  72. void set_priority(const int& p)
  73. {
  74. m_priority.store(p);
  75. }
  76. // 获取优先权
  77. int get_priority()
  78. {
  79. return m_priority.load();
  80. }
  81. // 设置状态
  82. void set_status(bool s)
  83. {
  84. m_used.store(s);
  85. }
  86. // 得到状态
  87. bool get_status()
  88. {
  89. return m_used.load();
  90. }
  91. // 控制超时检查
  92. bool is_time_out()
  93. {
  94. if(m_time_stamp >= m_ctrl_time){
  95. return true;
  96. }
  97. return false;
  98. }
  99. void get_turn()
  100. {
  101. while(m_owner_flag.test_and_set(std::memory_order_acquire))
  102. {}
  103. }
  104. void release_turn()
  105. {
  106. m_owner_flag.clear(std::memory_order_release);
  107. }
  108. // 重置
  109. void reset();
  110. //设置灯组内红绿灯颜色,满足条件的设置为指定灯形状,不满足条件3个面的设为其他相同的灯形状
  111. void set_light(const int& lid, const int& lc, const int& lcr);
  112. // 增加红绿灯到灯组
  113. void insert(traffic_light_ptr ptl);
  114. //获取两个灯之间的距离
  115. bool is_different(const int& ld, const int& lc, const int& lcr);
  116. // 手动控制
  117. void set_manual_ctrl(const std::string& name, const int& ld, const int& lc);
  118. uint64_t get_green_light_id();
  119. };
  120. struct traffic_light_data{
  121. bool m_bigger_flag; //大车标识
  122. std::list<traffic_light_ptr> m_traffic_group;
  123. traffic_light_data(): m_bigger_flag(false){
  124. m_traffic_group.clear();
  125. }
  126. };
  127. #endif