19 namespace traffic_manager {
21 using namespace constants::MotionPlan;
22 using namespace constants::WaypointSelection;
23 using 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);
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);
const LocalMapPtr & local_map
TargetWPInfo GetTargetWaypoint(const Buffer &waypoint_buffer, const float &target_point_distance)
static const float CRITICAL_BRAKING_MARGIN
std::vector< carla::rpc::Command > ControlFrame
static const float FOLLOW_LEAD_FACTOR
TrackTraffic & track_traffic
static const float EPSILON
float GetVehicleTargetVelocity(const ActorId &actor_id, const float speed_limit) const
Method to query target velocity for a vehicle.
const std::vector< float > highway_longitudinal_parameters
void Update(const unsigned long index)
SimpleWaypointPtr safe_point
static const float EPSILON_RELATIVE_SPEED
float GetLaneOffset(const ActorId &actor_id) const
Method to query lane offset for a vehicle.
const std::vector< ActorId > & vehicle_id_list
static const float MAX_JUNCTION_BLOCK_DISTANCE
float available_distance_margin
bool is_at_junction_entrance
cc::Timestamp current_timestamp
static const float TL_TARGET_VELOCITY
static const float LANDMARK_DETECTION_TIME
static const uint16_t ATTEMPTS_TO_TELEPORT
std::vector< bool > TLFrame
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...
static const float HYBRID_MODE_DT_FL
float SquaredLength() const
float GetLandmarkTargetVelocity(const SimpleWaypoint &waypoint, const cg::Location vehicle_location, const ActorId actor_id, float max_target_velocity)
RandomGenerator & random_device
static const float YIELD_TARGET_VELOCITY
std::shared_ptr< InMemoryMap > LocalMapPtr
This class holds the state of all the vehicles in the simlation.
This file contains definitions of common data structures used in traffic manager. ...
std::unordered_set< ActorId > ActorIdSet
static const float MIN_TARGET_WAYPOINT_DISTANCE
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
void RemoveActor(const ActorId actor_id)
static auto DistanceSquared(const Vector3D &a, const Vector3D &b)
std::unordered_map< ActorId, cc::Timestamp > teleportation_instance
static const float TARGET_WAYPOINT_TIME_HORIZON
std::unordered_map< carla::ActorId, Buffer > BufferMap
static const float STOP_TARGET_VELOCITY
void UpdateKinematicState(ActorId actor_id, KinematicState state)
static const float FRICTION
cg::Location GetLocation(const ActorId actor_id) const
bool GetRespawnDormantVehicles() const
Method to retrieve if we are automatically respawning vehicles.
const BufferMap & buffer_map
const Timestamp & GetTimestamp() const
Get timestamp of this snapshot.
std::unordered_map< ActorId, StateEntry > pid_state_map
Structure to hold the actuation signals.
static const float RELATIVE_APPROACH_SPEED
bool IsDormant(const ActorId actor_id) const
static const float PERC_MAX_SLOWDOWN
static const float AFTER_JUNCTION_MIN_SPEED
cg::Rotation GetRotation(const ActorId actor_id) const
static const float HIGHWAY_SPEED
float GetSpeedLimit(const ActorId actor_id) const
float GetTurnTargetVelocity(const Buffer &waypoint_buffer, float max_target_velocity)
#define SQUARE(a)
This file contains various constants used in traffic manager arranged into sensible namespaces for re...
double min(double v1, double v2)
This is a simple wrapper class on Carla's waypoint object.
SimpleWaypointPtr junction_end_point
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...
float GetThreePointCircleRadius(cg::Location first_location, cg::Location middle_location, cg::Location last_location)
const CollisionFrame & collision_frame
std::vector< SimpleWaypointPtr > NodeList
bool GetSynchronousMode() const
Method to get synchronous mode.
static const float MIN_FOLLOW_LEAD_DISTANCE
const std::vector< float > highway_lateral_parameters
const LocalizationFrame & localization_frame
static const float MIN_SAFE_INTERVAL_LENGTH
static const double HYBRID_MODE_DT
const std::vector< float > urban_longitudinal_parameters
bool SafeAfterJunction(const LocalizationData &localization, const bool tl_hazard, const bool collision_emergency_stop)
cg::Vector3D GetHeading(const ActorId actor_id) const
float GetUpperBoundaryRespawnDormantVehicles() const
Method to retrieve maximum distance from hero vehicle when respawning vehicles.
bool IsPhysicsEnabled(const ActorId actor_id) const
ControlFrame & output_array
static auto Dot(const Vector3D &a, const Vector3D &b)
std::vector< LocalizationData > LocalizationFrame
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
auto Distance(const Location &loc) const
float GetLowerBoundaryRespawnDormantVehicles() const
Method to retrieve minimum distance from hero vehicle when respawning vehicles.
Structure to hold the controller state.
void UpdateKinematicHybridEndLocation(ActorId actor_id, cg::Location location)
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...
cg::Location GetHeroLocation() const
bool IsGeoGridFree(const GeoGridId geogrid_id) const
ActorIdSet GetPassingVehicles(uint64_t waypoint_id) const
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)
cg::Vector3D GetVelocity(const ActorId actor_id) const
Vector3D MakeSafeUnitVector(const float epsilon) const
WorldSnapshot GetSnapshot() const
Return a snapshot of the world at this moment.
const std::vector< float > urban_lateral_parameters
SimulationState & simulation_state
double elapsed_seconds
Simulated seconds elapsed since the beginning of the current episode.
static const float GRAVITY
geom::Transform Transform
WaypointPtr GetWaypoint() const
Returns a carla::shared_ptr to carla::waypoint.
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)
static constexpr double EPSILON
We use this epsilon to shift the waypoints away from the edges of the lane sections to avoid floating...
std::vector< CollisionHazardData > CollisionFrame
const Parameters & parameters