CARLA
Loading...
Searching...
No Matches
TrafficLightStage.cpp
Go to the documentation of this file.
1
4
6
7namespace carla {
8namespace traffic_manager {
9
10using constants::TrafficLight::EXIT_JUNCTION_THRESHOLD;
11using constants::TrafficLight::MINIMUM_STOP_TIME;
12using constants::WaypointSelection::JUNCTION_LOOK_AHEAD;
13using constants::MotionPlan::EPSILON_RELATIVE_SPEED;
14
30
31void TrafficLightStage::Update(const unsigned long index) {
32 bool traffic_light_hazard = false;
33
34 const ActorId ego_actor_id = vehicle_id_list.at(index);
35
36 JunctionID current_junction_id = -1;
37 if (vehicle_last_junction.find(ego_actor_id) != vehicle_last_junction.end()) {
38 current_junction_id = vehicle_last_junction.at(ego_actor_id);
39 }
40 auto affected_junction_id = GetAffectedJunctionId(ego_actor_id);
41
42 current_timestamp = world.GetSnapshot().GetTimestamp();
43
44 const TrafficLightState tl_state = simulation_state.GetTLS(ego_actor_id);
45 const TLS traffic_light_state = tl_state.tl_state;
46 const bool is_at_traffic_light = tl_state.at_traffic_light;
47
48 // We determine to stop if the vehicle found a traffic light in yellow / red.
49 if (is_at_traffic_light &&
50 traffic_light_state != TLS::Green &&
51 traffic_light_state != TLS::Off &&
53 // Remove actor from non-signalized junction if it is affected by a traffic light.
54 if (current_junction_id != -1) {
55 RemoveActor(ego_actor_id);
56 }
57 traffic_light_hazard = true;
58 }
59 // The vehicle is currently at a non signalized junction, so handle its priorities.
60 // Don't use the next condition as bounding boxes might switch to green
61 else if (current_junction_id != -1)
62 {
63 if ( affected_junction_id == -1 || affected_junction_id != current_junction_id ) {
64 RemoveActor(ego_actor_id);
65 }
66 else {
67 traffic_light_hazard = HandleNonSignalisedJunction(ego_actor_id, affected_junction_id, current_timestamp);
68 }
69 }
70 else if (affected_junction_id != -1 &&
71 !is_at_traffic_light &&
72 traffic_light_state != TLS::Green &&
74
75 AddActorToNonSignalisedJunction(ego_actor_id, affected_junction_id);
76 traffic_light_hazard = true;
77 }
78 output_array.at(index) = traffic_light_hazard;
79}
80
81void TrafficLightStage::AddActorToNonSignalisedJunction(const ActorId ego_actor_id, const JunctionID junction_id) {
82
83 if (entering_vehicles_map.find(junction_id) == entering_vehicles_map.end()) {
84 // Initializing new map entry for the junction with an empty actor deque.
85 std::deque<ActorId> entry_deque;
86 entering_vehicles_map.insert({junction_id, entry_deque});
87 }
88
89 auto& entering_vehicles = entering_vehicles_map.at(junction_id);
90 if (std::find(entering_vehicles.begin(), entering_vehicles.end(), ego_actor_id) == entering_vehicles.end()){
91 // Initializing new actor entry to the junction maps.
92 entering_vehicles.push_back(ego_actor_id);
93 if (vehicle_last_junction.find(ego_actor_id) != vehicle_last_junction.end()) {
94 // The actor was entering another junction, so remove all of its stored data
95 RemoveActor(ego_actor_id);
96 }
97 vehicle_last_junction.insert({ego_actor_id, junction_id});
98 }
99}
100
101
102bool TrafficLightStage::HandleNonSignalisedJunction(const ActorId ego_actor_id, const JunctionID junction_id,
103 cc::Timestamp timestamp) {
104
105 bool traffic_light_hazard = false;
106
107 auto& entering_vehicles = entering_vehicles_map.at(junction_id);
108
109 if (vehicle_stop_time.find(ego_actor_id) == vehicle_stop_time.end()) {
110 // Ensure the vehicle stops before doing anything else
111 if (simulation_state.GetVelocity(ego_actor_id).Length() < EPSILON_RELATIVE_SPEED) {
112 vehicle_stop_time.insert({ego_actor_id, timestamp});
113 }
114 traffic_light_hazard = true;
115 }
116
117 else if (entering_vehicles.front() == ego_actor_id) {
118 auto entry_elapsed_seconds = vehicle_stop_time.at(ego_actor_id).elapsed_seconds;
119 if (timestamp.elapsed_seconds - entry_elapsed_seconds < MINIMUM_STOP_TIME) {
120 // Wait at least the minimum amount of time before entering the junction
121 traffic_light_hazard = true;
122 }
123 } else {
124 // Only one vehicle can be entering the junction, so stop the rest.
125 traffic_light_hazard = true;
126 }
127 return traffic_light_hazard;
128}
129
131 const Buffer &waypoint_buffer = buffer_map.at(ego_actor_id);
132 const SimpleWaypointPtr look_ahead_point = GetTargetWaypoint(waypoint_buffer, JUNCTION_LOOK_AHEAD).first;
133 const auto front_point = waypoint_buffer.front();
134
135 auto look_ahead_junction_id = look_ahead_point->GetJunctionId();
136 auto front_junction_id = front_point->GetJunctionId();
137
138 // Check if the vehicle is currently at a non-signalized junction
139 JunctionID current_junction_id = -1;
140 if (vehicle_last_junction.find(ego_actor_id) != vehicle_last_junction.end()) {
141 current_junction_id = vehicle_last_junction.at(ego_actor_id);
142 }
143
144 if (current_junction_id != -1) {
145 // We are currently processing a junction
146 if (current_junction_id == look_ahead_junction_id) {
147 return look_ahead_junction_id;
148 } else {
149 if (look_ahead_junction_id != -1) {
150 // We are detecting a different junction
151 return look_ahead_junction_id;
152 } else {
153 if (current_junction_id == front_junction_id) {
154 // We are still in the same junction
155 return front_junction_id;
156 } else {
157 return -1;
158 }
159 }
160 }
161 } else {
162 // If we are not processing any junction, return the look ahead detected junction
163 return look_ahead_junction_id;
164 }
165}
166
168 if (vehicle_last_junction.find(actor_id) != vehicle_last_junction.end()) {
169 auto junction_id = vehicle_last_junction.at(actor_id);
170
171 auto& entering_vehicles = entering_vehicles_map.at(junction_id);
172 auto ent_index = std::find(entering_vehicles.begin(), entering_vehicles.end(), actor_id);
173 if (ent_index != entering_vehicles.end()) {
174 entering_vehicles.erase(ent_index);
175 }
176
177 if (vehicle_stop_time.find(actor_id) != vehicle_stop_time.end()) {
178 vehicle_stop_time.erase(actor_id);
179 }
180
181 vehicle_last_junction.erase(actor_id);
182 }
183}
184
190
191} // namespace traffic_manager
192} // namespace carla
float GetPercentageRunningSign(const ActorId &actor_id) const
Method to get % to run any traffic light.
float GetPercentageRunningLight(const ActorId &actor_id) const
Method to get % to run any traffic light.
This class holds the state of all the vehicles in the simlation.
cg::Vector3D GetVelocity(const ActorId actor_id) const
TrafficLightState GetTLS(const ActorId actor_id) const
void AddActorToNonSignalisedJunction(const ActorId ego_actor_id, const JunctionID junction_id)
Initialized the vehicle to the non-signalized junction maps.
void Update(const unsigned long index) override
JunctionID GetAffectedJunctionId(const ActorId ego_actor_id)
Get current affected junction id for the vehicle.
std::unordered_map< ActorId, JunctionID > vehicle_last_junction
Map linking the vehicles with their current junction. Used for easy access to the previous two maps.
std::unordered_map< ActorId, cc::Timestamp > vehicle_stop_time
Map containing the timestamp at which the actor first stopped at a stop sign.
void RemoveActor(const ActorId actor_id) override
const std::vector< ActorId > & vehicle_id_list
std::unordered_map< JunctionID, std::deque< ActorId > > entering_vehicles_map
Variables used to handle non signalized junctions.
bool HandleNonSignalisedJunction(const ActorId ego_actor_id, const JunctionID junction_id, cc::Timestamp timestamp)
This controls all vehicle's interactions at non signalized junctions.
TrafficLightStage(const std::vector< ActorId > &vehicle_id_list, const SimulationState &Simulation_state, const BufferMap &buffer_map, const Parameters &parameters, const cc::World &world, TLFrame &output_array, RandomGenerator &random_device)
carla::rpc::TrafficLightState TLS
std::vector< bool > TLFrame
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
carla::road::JuncId JunctionID
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
TargetWPInfo GetTargetWaypoint(const Buffer &waypoint_buffer, const float &target_point_distance)
std::unordered_map< carla::ActorId, Buffer > BufferMap
This file contains definitions of common data structures used in traffic manager.