19namespace traffic_manager {
21using namespace constants::MotionPlan;
22using namespace constants::WaypointSelection;
23using namespace constants::SpeedThreshold;
30 const std::vector<ActorId> &vehicle_id_list,
35 const std::vector<float> &urban_longitudinal_parameters,
36 const std::vector<float> &highway_longitudinal_parameters,
37 const std::vector<float> &urban_lateral_parameters,
38 const std::vector<float> &highway_lateral_parameters,
46 : vehicle_id_list(vehicle_id_list),
47 simulation_state(simulation_state),
48 parameters(parameters),
49 buffer_map(buffer_map),
50 track_traffic(track_traffic),
51 urban_longitudinal_parameters(urban_longitudinal_parameters),
52 highway_longitudinal_parameters(highway_longitudinal_parameters),
53 urban_lateral_parameters(urban_lateral_parameters),
54 highway_lateral_parameters(highway_lateral_parameters),
55 localization_frame(localization_frame),
56 collision_frame(collision_frame),
59 output_array(output_array),
60 random_device(random_device),
61 local_map(local_map) {}
68 const float vehicle_speed = vehicle_velocity.
Length();
75 const bool &tl_hazard =
tl_frame.at(index);
84 bool is_hero_alive = hero_location !=
cg::Location(0, 0, 0);
100 float dilate_factor = (upper_bound-lower_bound)/100.0f;
106 float random_sample = (
static_cast<float>(
random_device.
next())*dilate_factor) + lower_bound;
108 if (!teleport_waypoint_list.empty()) {
109 for (
auto &teleport_waypoint : teleport_waypoint_list) {
110 GeoGridId geogrid_id = teleport_waypoint->GetGeodesicGridId();
112 teleportation_transform = teleport_waypoint->GetTransform();
113 teleportation_transform.
location.
z += 0.5f;
125 vehicle_velocity, vehicle_speed_limit,
137 float max_landmark_target_velocity =
GetLandmarkTargetVelocity(*(waypoint_buffer.at(0)), vehicle_location, actor_id, max_target_velocity);
141 max_target_velocity = std::min(std::min(max_target_velocity, max_landmark_target_velocity), max_turn_target_velocity);
144 std::pair<bool, float> collision_response =
CollisionHandling(collision_hazard, tl_hazard, vehicle_velocity,
145 vehicle_heading, max_target_velocity);
146 bool collision_emergency_stop = collision_response.first;
147 float dynamic_target_velocity = collision_response.second;
150 bool safe_after_junction =
SafeAfterJunction(localization, tl_hazard, collision_emergency_stop);
153 bool emergency_stop = tl_hazard || collision_emergency_stop || !safe_after_junction;
161 cg::Location target_location = target_waypoint->GetLocation();
164 auto right_vector = target_waypoint->GetTransform().GetRightVector();
166 target_location = target_location + offset_location;
168 float dot_product =
DeviationDotProduct(vehicle_location, vehicle_heading, target_location);
170 dot_product = acos(dot_product) /
PI;
171 if (cross_product < 0.0f) {
172 dot_product *= -1.0f;
174 const float angular_deviation = dot_product;
175 const float velocity_deviation = (dynamic_target_velocity - vehicle_speed) / dynamic_target_velocity;
187 std::vector<float> longitudinal_parameters;
188 std::vector<float> lateral_parameters;
199 current_state = {
current_timestamp, angular_deviation, velocity_deviation, 0.0f};
202 actuation_signal =
PID::RunStep(current_state, previous_state,
203 longitudinal_parameters, lateral_parameters);
205 if (emergency_stop) {
206 actuation_signal.throttle = 0.0f;
207 actuation_signal.brake = 1.0f;
213 vehicle_control.
throttle = actuation_signal.throttle;
214 vehicle_control.
brake = actuation_signal.brake;
215 vehicle_control.
steer = actuation_signal.steer;
220 current_state.
steer = actuation_signal.steer;
222 state = current_state;
245 cg::Transform target_base_transform = teleport_target->GetTransform();
248 cg::Vector3D correct_heading = (target_base_location - vehicle_location).MakeSafeUnitVector(EPSILON);
250 if (vehicle_location.
Distance(target_base_location) < target_displacement) {
271 const bool tl_hazard,
272 const bool collision_emergency_stop) {
277 bool safe_after_junction =
true;
278 if (!tl_hazard && !collision_emergency_stop
280 && junction_end_point !=
nullptr && safe_point !=
nullptr
285 cg::Location mid_point = (junction_end_point->GetLocation() + safe_point->GetLocation())/2.0f;
290 std::set_difference(passing_safe_point.begin(), passing_safe_point.end(),
291 passing_junction_end_point.begin(), passing_junction_end_point.end(),
292 std::inserter(difference, difference.begin()));
293 if (difference.size() > 0) {
294 for (
const ActorId &blocking_id: difference) {
298 safe_after_junction =
false;
305 return safe_after_junction;
309 const bool tl_hazard,
312 const float max_target_velocity) {
313 bool collision_emergency_stop =
false;
314 float dynamic_target_velocity = max_target_velocity;
315 const float vehicle_speed = vehicle_velocity.
Length();
317 if (collision_hazard.
hazard && !tl_hazard) {
320 const float vehicle_relative_speed = (vehicle_velocity - other_velocity).Length();
323 const float other_speed_along_heading = cg::Math::Dot(other_velocity, vehicle_heading);
327 if (vehicle_relative_speed > EPSILON_RELATIVE_SPEED) {
331 if (available_distance_margin > follow_lead_distance) {
334 dynamic_target_velocity = other_speed_along_heading;
343 collision_emergency_stop =
true;
347 collision_emergency_stop =
true;
352 if (dynamic_target_velocity < vehicle_speed - max_gradual_velocity) {
354 dynamic_target_velocity = vehicle_speed - max_gradual_velocity;
356 dynamic_target_velocity = std::min(max_target_velocity, dynamic_target_velocity);
358 return {collision_emergency_stop, dynamic_target_velocity};
364 float max_target_velocity) {
368 float landmark_target_velocity = std::numeric_limits<float>::max();
370 auto all_landmarks = waypoint.
GetWaypoint()->GetAllLandmarksInDistance(max_distance,
false);
372 for (
auto &landmark: all_landmarks) {
374 auto landmark_location = landmark->GetWaypoint()->GetTransform().location;
375 auto landmark_type = landmark->GetType();
376 auto distance = landmark_location.Distance(vehicle_location);
378 if (distance > max_distance) {
382 float minimum_velocity = max_target_velocity;
383 if (landmark_type ==
"1000001") {
385 }
else if (landmark_type ==
"206") {
387 }
else if (landmark_type ==
"205") {
389 }
else if (landmark_type ==
"274") {
390 float value =
static_cast<float>(landmark->GetValue()) / 3.6f;
392 minimum_velocity = (value < max_target_velocity) ? value : max_target_velocity;
397 float v = std::max(((max_target_velocity - minimum_velocity) / max_distance) * distance + minimum_velocity, minimum_velocity);
398 landmark_target_velocity = std::min(landmark_target_velocity, v);
401 return landmark_target_velocity;
405 float max_target_velocity) {
407 if (waypoint_buffer.size() < 3) {
408 return max_target_velocity;
413 const SimpleWaypointPtr middle_waypoint = waypoint_buffer.at(
static_cast<uint16_t
>(waypoint_buffer.size() / 2));
416 middle_waypoint->GetLocation(),
417 last_waypoint->GetLocation());
428 float x1 = first_location.
x;
429 float y1 = first_location.
y;
430 float x2 = middle_location.
x;
431 float y2 = middle_location.
y;
432 float x3 = last_location.
x;
433 float y3 = last_location.
y;
444 float sx13 = x1 * x1 - x3 * x3;
445 float sy13 = y1 * y1 - y3 * y3;
446 float sx21 = x2 * x2 - x1 * x1;
447 float sy21 = y2 * y2 - y1 * y1;
449 float f_denom = 2 * (y31 * x12 - y21 * x13);
451 return std::numeric_limits<float>::max();
453 float f = (sx13 * x12 + sy13 * x12 + sx21 * x13 + sy21 * x13) / f_denom;
455 float g_denom = 2 * (x31 * y12 - x21 * y13);
457 return std::numeric_limits<float>::max();
459 float g = (sx13 * y12 + sy13 * y12 + sx21 * y13 + sy21 * y13) / g_denom;
461 float c = - (x1 * x1 + y1 * y1) - 2 * g * x1 - 2 * f * y1;
465 return std::sqrt(h * h + k * k - c);
#define SQUARE(a)
This file contains various constants used in traffic manager arranged into sensible namespaces for re...
double elapsed_seconds
Simulated seconds elapsed since the beginning of the current episode.
const Timestamp & GetTimestamp() const
Get timestamp of this snapshot.
WorldSnapshot GetSnapshot() const
Return a snapshot of the world at this moment.
auto Distance(const Location &loc) const
float SquaredLength() const
Vector3D MakeSafeUnitVector(const float epsilon) const
const LocalMapPtr & local_map
const std::vector< float > urban_longitudinal_parameters
cc::Timestamp current_timestamp
const LocalizationFrame & localization_frame
const BufferMap & buffer_map
const CollisionFrame & collision_frame
const std::vector< float > highway_lateral_parameters
std::unordered_map< ActorId, cc::Timestamp > teleportation_instance
TrackTraffic & track_traffic
float GetThreePointCircleRadius(cg::Location first_location, cg::Location middle_location, cg::Location last_location)
bool SafeAfterJunction(const LocalizationData &localization, const bool tl_hazard, const bool collision_emergency_stop)
ControlFrame & output_array
RandomGenerator & random_device
void Update(const unsigned long index)
const Parameters & parameters
MotionPlanStage(const std::vector< ActorId > &vehicle_id_list, SimulationState &simulation_state, const Parameters ¶meters, const BufferMap &buffer_map, TrackTraffic &track_traffic, const std::vector< float > &urban_longitudinal_parameters, const std::vector< float > &highway_longitudinal_parameters, const std::vector< float > &urban_lateral_parameters, const std::vector< float > &highway_lateral_parameters, const LocalizationFrame &localization_frame, const CollisionFrame &collision_frame, const TLFrame &tl_frame, const cc::World &world, ControlFrame &output_array, RandomGenerator &random_device, const LocalMapPtr &local_map)
const std::vector< float > highway_longitudinal_parameters
SimulationState & simulation_state
const std::vector< ActorId > & vehicle_id_list
float GetLandmarkTargetVelocity(const SimpleWaypoint &waypoint, const cg::Location vehicle_location, const ActorId actor_id, float max_target_velocity)
float GetTurnTargetVelocity(const Buffer &waypoint_buffer, float max_target_velocity)
void RemoveActor(const ActorId actor_id)
const std::vector< float > urban_lateral_parameters
std::unordered_map< ActorId, StateEntry > pid_state_map
std::pair< bool, float > CollisionHandling(const CollisionHazardData &collision_hazard, const bool tl_hazard, const cg::Vector3D ego_velocity, const cg::Vector3D ego_heading, const float max_target_velocity)
float GetLaneOffset(const ActorId &actor_id) const
Method to query lane offset for a vehicle.
float GetVehicleTargetVelocity(const ActorId &actor_id, const float speed_limit) const
Method to query target velocity for a vehicle.
float GetUpperBoundaryRespawnDormantVehicles() const
Method to retrieve maximum distance from hero vehicle when respawning vehicles.
float GetLowerBoundaryRespawnDormantVehicles() const
Method to retrieve minimum distance from hero vehicle when respawning vehicles.
bool GetRespawnDormantVehicles() const
Method to retrieve if we are automatically respawning vehicles.
bool GetSynchronousMode() const
Method to get synchronous mode.
This is a simple wrapper class on Carla's waypoint object.
WaypointPtr GetWaypoint() const
Returns a carla::shared_ptr to carla::waypoint.
This class holds the state of all the vehicles in the simlation.
cg::Location GetLocation(const ActorId actor_id) const
void UpdateKinematicHybridEndLocation(ActorId actor_id, cg::Location location)
cg::Vector3D GetHeading(const ActorId actor_id) const
void UpdateKinematicState(ActorId actor_id, KinematicState state)
float GetSpeedLimit(const ActorId actor_id) const
cg::Rotation GetRotation(const ActorId actor_id) const
cg::Vector3D GetVelocity(const ActorId actor_id) const
bool IsPhysicsEnabled(const ActorId actor_id) const
bool IsDormant(const ActorId actor_id) const
cg::Location GetHeroLocation() const
ActorIdSet GetPassingVehicles(uint64_t waypoint_id) const
bool IsGeoGridFree(const GeoGridId geogrid_id) const
void AddTakenGrid(const GeoGridId geogrid_id, const ActorId actor_id)
ActuationSignal RunStep(StateEntry present_state, StateEntry previous_state, const std::vector< float > &longitudinal_parameters, const std::vector< float > &lateral_parameters)
This function calculates the actuation signals based on the resent state change of the vehicle to min...
static const float EPSILON
static const double HYBRID_MODE_DT
static const float HYBRID_MODE_DT_FL
static const float PERC_MAX_SLOWDOWN
static const float LANDMARK_DETECTION_TIME
static const float FRICTION
static const float CRITICAL_BRAKING_MARGIN
static const float FOLLOW_LEAD_FACTOR
static const float MAX_JUNCTION_BLOCK_DISTANCE
static const float RELATIVE_APPROACH_SPEED
static const float TL_TARGET_VELOCITY
static const uint16_t ATTEMPTS_TO_TELEPORT
static const float GRAVITY
static const float MIN_FOLLOW_LEAD_DISTANCE
static const float STOP_TARGET_VELOCITY
static const float YIELD_TARGET_VELOCITY
static const float AFTER_JUNCTION_MIN_SPEED
static const float HIGHWAY_SPEED
static const float TARGET_WAYPOINT_TIME_HORIZON
static const float MIN_SAFE_INTERVAL_LENGTH
static const float MIN_TARGET_WAYPOINT_DISTANCE
std::vector< SimpleWaypointPtr > NodeList
std::vector< carla::rpc::Command > ControlFrame
std::vector< bool > TLFrame
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
std::vector< CollisionHazardData > CollisionFrame
float DeviationCrossProduct(const cg::Location &reference_location, const cg::Vector3D &heading_vector, const cg::Location &target_location)
Returns the cross product (z component value) between the vehicle's heading vector and the vector alo...
std::shared_ptr< InMemoryMap > LocalMapPtr
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
TargetWPInfo GetTargetWaypoint(const Buffer &waypoint_buffer, const float &target_point_distance)
std::unordered_set< ActorId > ActorIdSet
std::vector< LocalizationData > LocalizationFrame
std::unordered_map< carla::ActorId, Buffer > BufferMap
float DeviationDotProduct(const cg::Location &reference_location, const cg::Vector3D &heading_vector, const cg::Location &target_location)
Returns the dot product between the vehicle's heading vector and the vector along the direction to th...
This file contains definitions of common data structures used in traffic manager.
Structure to hold the actuation signals.
float available_distance_margin
SimpleWaypointPtr safe_point
bool is_at_junction_entrance
SimpleWaypointPtr junction_end_point
Structure to hold the controller state.