CARLA
Loading...
Searching...
No Matches
Constants.h
Go to the documentation of this file.
1
2/// This file contains various constants used in traffic manager
3/// arranged into sensible namespaces for re-usability across
4/// different files.
5
6#pragma once
7
8#include <chrono>
9#include <limits>
10#include <stdint.h>
11#include <iostream>
12#include <vector>
13
14#define SQUARE(a) ((a) * (a))
15#define RATE(MaxY, MinY, DiffX) (((MaxY) - (MinY)) / (DiffX))
16
17namespace carla {
18namespace traffic_manager {
19namespace constants {
20
21namespace Networking {
22static const uint64_t MIN_TRY_COUNT = 20u;
23static const unsigned short TM_DEFAULT_PORT = 8000u;
24static const int64_t TM_TIMEOUT = 2000; // ms
25} // namespace Networking
26
27namespace VehicleRemoval {
28static const float STOPPED_VELOCITY_THRESHOLD = 0.8f;
29static const double BLOCKED_TIME_THRESHOLD = 90.0;
30static const double RED_TL_BLOCKED_TIME_THRESHOLD = 180.0;
31static const double DELTA_TIME_BETWEEN_DESTRUCTIONS = 10.0;
32} // namespace VehicleRemoval
33
34namespace HybridMode {
35static const float HYBRID_MODE_DT_FL = 0.05f;
36static const double HYBRID_MODE_DT = 0.05;
37static const double INV_HYBRID_DT = 1.0 / HYBRID_MODE_DT;
38static const float PHYSICS_RADIUS = 50.0f;
39} // namespace HybridMode
40
41// Refresh periods (simulation seconds) for world queries read over synchronous
42// RPCs. In asynchronous mode each such RPC costs a full server frame of
43// latency; synchronous mode still reads them every step.
44namespace WorldInfoRefresh {
45static const double EPISODE_SETTINGS_REFRESH_PERIOD = 1.0;
46static const double VEHICLE_LIGHT_STATES_REFRESH_PERIOD = 0.25;
47static const double WEATHER_REFRESH_PERIOD = 1.0;
48// Bounds how many control batches can queue ahead of the server's game thread.
49// Counted in batches, not simulation time: with a fixed delta a time period
50// would let a slow server queue proportionally more of them.
51static const uint64_t MAX_UNWAITED_CONTROL_BATCHES = 4u;
52// Keeps the asynchronous worker from spinning on the episode state.
53static const std::chrono::milliseconds SNAPSHOT_POLL_PERIOD {1};
54
55/// Also true when the clock jumps backwards (new episode) or on NaN, so an
56/// unusable clock refreshes rather than pins the cache for ever.
57inline bool IsRefreshDue(const double now, const double last, const double period) {
58 return !(now >= last && (now - last) < period);
59}
60
61/// A vehicle the server already omitted from a fresh list (dormant on a large
62/// map) must not re-arm the unscheduled read on every other step.
63[[nodiscard]] inline bool IsEarlyRefreshDue(
64 const bool refreshed_this_step,
65 const bool missing_from_the_last_refresh) {
66 return !refreshed_this_step && !missing_from_the_last_refresh;
67}
68
69[[nodiscard]] inline bool IsBatchSyncDue(const uint64_t unwaited_batches, const uint64_t limit) {
70 return unwaited_batches >= limit;
71}
72} // namespace WorldInfoRefresh
73
74namespace SpeedThreshold {
75static const float HIGHWAY_SPEED = 60.0f / 3.6f;
76static const float AFTER_JUNCTION_MIN_SPEED = 5.0f / 3.6f;
77static const float INITIAL_PERCENTAGE_SPEED_DIFFERENCE = 0.0f;
78} // namespace SpeedThreshold
79
80namespace PathBufferUpdate {
81static const float MAX_START_DISTANCE = 20.0f;
82static const float MINIMUM_HORIZON_LENGTH = 15.0f;
83static const float HORIZON_RATE = 2.0f;
84static const float HIGH_SPEED_HORIZON_RATE = 4.0f;
85} // namespace PathBufferUpdate
86
87namespace WaypointSelection {
88// Lookahead time for the steering target. ue5-dev halved this to 0.5 s,
89// which at 30 km/h puts the target ~4 m ahead: any lateral transient (lane
90// change, resume offset) then reads as a huge angular error, saturates the
91// PID and produces an underdamped weave. 1.0 s matches upstream and keeps
92// the pure-pursuit geometry well damped.
93static const float TARGET_WAYPOINT_TIME_HORIZON = 1.0f;
94static const float MIN_TARGET_WAYPOINT_DISTANCE = 3.0f;
95// Cap on the pursuit-target distance. Chasing a point d ahead cuts a curve
96// of radius R by the chord sagitta ~d^2/(8R): uncapped at 60 km/h (16.7 m)
97// on Town10's R~25 m corners that is a ~1.4 m apex cut, which lands the
98// vehicle in the oncoming lane on left curves. 10 m keeps the cut under
99// ~0.6 m on the tightest urban corners.
100static const float MAX_TARGET_WAYPOINT_DISTANCE = 10.0f;
101// Curvature-aware bound on the same chord cut. The fixed 10 m cap encodes the
102// R~25 m / 60 km/h case, but junction connecting roads on Town10 are R~11 m
103// taken at ~8 m/s where the surviving d = speed * 1 s chord still cuts
104// d^2/(8R) ~ 0.8 m (measured ~1.4 m at the apex with vehicle dynamics): the
105// vehicle turns in before the lane does and sweeps the adjacent lane. Capping
106// d = sqrt(8 * R_local * MAX_PURSUIT_CHORD_SAGITTA) keeps the geometric cut
107// below ~0.2 m at any local path radius; straight paths (R -> inf) are
108// unaffected. R_local is measured over the buffer up to the pursuit target.
109static const float MAX_PURSUIT_CHORD_SAGITTA = 0.2f;
110static const float JUNCTION_LOOK_AHEAD = 5.0f;
111static const float SAFE_DISTANCE_AFTER_JUNCTION = 4.0f;
112static const float MIN_JUNCTION_LENGTH = 8.0f;
114static const float LARGE_VEHICLES_JUNCTION_OFFSET = 1.5f;
115static const float LARGE_VEHICLES_JUNCTION_POINT = 0.3f;
116static const float LARGE_VEHICLES_JUNCTION_MAX_RADIUS = 20.0f;
117// Fraction of the inboard (exit "cut-in") excursion retained for large
118// vehicles. The cut-in is what drives a long vehicle's rear into the inside
119// shoulder; 0.0f removes it entirely, 1.0f keeps the original profile.
120static const float LARGE_VEHICLES_JUNCTION_INBOARD_SCALE = 0.25f;
121// Vehicle length (m) at or below which the wide-turn offset is zero. Above it
122// the offset ramps up linearly at OFFSET_GAIN per metre, capped by
123// LARGE_VEHICLES_JUNCTION_OFFSET. Sim-tuned.
124static const float LARGE_VEHICLES_JUNCTION_REF_LENGTH = 6.0f;
125static const float LARGE_VEHICLES_JUNCTION_OFFSET_GAIN = 0.25f;
126// Lateral clearance (m) added beyond the swing band when checking whether the
127// offset side is occupied, and longitudinal margin (m) added to the vehicle's
128// half-length to define the "alongside" window.
129static const float LARGE_VEHICLES_JUNCTION_CLEARANCE = 1.0f;
130static const float LARGE_VEHICLES_JUNCTION_SIDE_MARGIN = 2.0f;
131} // namespace WaypointSelection
132
133namespace LaneChange {
134static const float MINIMUM_LANE_CHANGE_DISTANCE = 20.0f;
135static const float MAXIMUM_LANE_OBSTACLE_DISTANCE = 50.0f;
136static const float MAXIMUM_LANE_OBSTACLE_CURVATURE = 0.6f;
137static const float INTER_LANE_CHANGE_DISTANCE = 10.0f;
138static const float MIN_WPT_DISTANCE = 5.0f;
139static const float MAX_WPT_DISTANCE = 20.0f;
140static const float MIN_LANE_CHANGE_SPEED = 5.0f;
141static const float FIFTYPERC = 50.0f;
142} // namespace LaneChange
143
144namespace Collision {
145static const float BOUNDARY_EXTENSION_MINIMUM = 2.5f;
146static const float BOUNDARY_EXTENSION_RATE = 4.35f;
147static const float COS_10_DEGREES = 0.9848f;
148static const float OVERLAP_THRESHOLD = 0.1f;
149static const float LOCKING_DISTANCE_PADDING = 4.0f;
150static const float COLLISION_RADIUS_STOP = 8.0f;
151static const float COLLISION_RADIUS_MIN = 20.0f;
152static const float COLLISION_RADIUS_RATE = 2.65f;
153static const float MAX_LOCKING_EXTENSION = 10.0f;
154static const float WALKER_TIME_EXTENSION = 1.5f;
155static const float SQUARE_ROOT_OF_TWO = 1.414f;
156static const float VERTICAL_OVERLAP_THRESHOLD = 4.0f;
157static const float EPSILON = 2.0f * std::numeric_limits<float>::epsilon();
158static const float MIN_REFERENCE_DISTANCE = 0.5f;
159static const float MIN_VELOCITY_COLL_RADIUS = 2.0f;
160static const float VEL_EXT_FACTOR = 0.36f;
161} // namespace Collision
162
163namespace FrameMemory {
164static const uint64_t INITIAL_SIZE = 50u;
165static const uint64_t GROWTH_STEP_SIZE = 50u;
166static const float INV_GROWTH_STEP_SIZE = 1.0f / static_cast<float>(GROWTH_STEP_SIZE);
167} // namespace FrameMemory
168
169namespace Map {
170static const float INFINITE_DISTANCE = std::numeric_limits<float>::max();
171static const float MAX_GEODESIC_GRID_LENGTH = 20.0f;
172static const float MAP_RESOLUTION = 5.0f;
173static const float INV_MAP_RESOLUTION = 1.0f / MAP_RESOLUTION;
175static const float MAX_WPT_RADIANS = 0.087f; // 5ยบ
176static float const DELTA = 25.0f;
177static float const Z_DELTA = 500.0f;
178static float const STRAIGHT_DEG = 19.0f;
179static const double MIN_LANE_WIDTH = 1.0f;
180} // namespace Map
181
182namespace TrafficLight {
183static const double MINIMUM_STOP_TIME = 2.0;
184static const double EXIT_JUNCTION_THRESHOLD = 0; // Dot product of 90ยบ
185} // namespace TrafficLight
186
187namespace MotionPlan {
188static const float RELATIVE_APPROACH_SPEED = 12.0f / 3.6f;
189static const float MIN_FOLLOW_LEAD_DISTANCE = 2.0f;
190static const float CRITICAL_BRAKING_MARGIN = 0.2f;
191static const float EPSILON_RELATIVE_SPEED = 0.001f;
193static const float TWO_KM = 2000.0f;
194static const uint16_t ATTEMPTS_TO_TELEPORT = 5u;
195static const float LANDMARK_DETECTION_TIME = 3.5f;
196static const float TL_TARGET_VELOCITY = 15.0f / 3.6f;
197static const float STOP_TARGET_VELOCITY = 10.0f / 3.6f;
198static const float YIELD_TARGET_VELOCITY = 10.0f / 3.6f;
199// m/s2 (~0.3 g), a comfortable cornering level; still takes the R = 11 m
200// Town10 junction connectors at 20 km/h.
201static const float LATERAL_COMFORT_ACCELERATION = 3.0f;
202// m/s2 used to plan braking ahead of a turn, so vehicles slow before it.
203static const float TURN_BRAKING_DECELERATION = 2.0f;
204// Closer samples land on the stored polyline's chords and read as straight;
205// wider ones miss turns shorter than two spacings.
207static const float PI = 3.1415927f;
208static const float PERC_MAX_SLOWDOWN = 0.08f;
209static const float FOLLOW_LEAD_FACTOR = 2.0f;
210} // namespace MotionPlan
211
212namespace VehicleLight {
213static const float SUN_ALTITUDE_DEGREES_BEFORE_DAWN = 15.0f;
214static const float SUN_ALTITUDE_DEGREES_AFTER_SUNSET = 165.0f;
215static const float SUN_ALTITUDE_DEGREES_JUST_AFTER_DAWN = 35.0f;
216static const float SUN_ALTITUDE_DEGREES_JUST_BEFORE_SUNSET = 145.0f;
217static const float HEAVY_PRECIPITATION_THRESHOLD = 80.0f;
218static const float FOG_DENSITY_THRESHOLD = 20.0f;
219static const float MAX_DISTANCE_LIGHT_CHECK = 225.0f;
220} // namespace VehicleLight
221
222namespace PID {
223static const float MAX_THROTTLE = 0.85f;
224static const float MAX_BRAKE = 0.7f;
225static const float MAX_STEERING = 0.8f;
226static const float MAX_STEERING_DIFF = 0.15f;
227// Speed-scaled steering envelope: |steer| <= STEER_LIMIT_GAIN / speed^2
228// (speed in m/s). Guards against a saturated PID output knifing the vehicle
229// across lanes at speed. Inactive below ~6 m/s (envelope > 0.8).
230// A/B note (Town10 matrix): raising the gain to 45 to let vehicles steer
231// through corners taken at 60 km/h regressed badly (saturated transients
232// became hard swerves into obstacles); the correct fix for running wide in
233// curves is slowing down before them (GetTurnTargetVelocity), not granting
234// more steering authority at speed.
235static const float STEER_LIMIT_GAIN = 30.0f;
236// Vehicle steering-authority normalization. A normalized steer command maps
237// to path curvature as kappa = tan(steer * max_steer_angle) / wheelbase, so
238// for the same command a long vehicle yields proportionally less curvature
239// than the car-class fleet the lateral gains and the STEER_LIMIT_GAIN
240// envelope were tuned on (measured: 8.5 m trucks pinned at the envelope on
241// 45% of junction ticks, ran R~13 m on R~11 m connecting roads and invaded
242// the adjacent lane on every junction turn while cars tracked cleanly).
243// Both the lateral P/D terms and the envelope are therefore scaled by
244// (bbox_length / REF_VEHICLE_LENGTH) * (REF_MAX_STEER_ANGLE / max_steer)
245// which equalizes commanded CURVATURE across the fleet; wheelbase tracks
246// bounding-box length closely, so the unknown wheelbase fraction cancels
247// against the car-anchored reference length. Clamped to
248// [1, MAX_STEER_AUTHORITY_CORRECTION] so car-class vehicles and the
249// physical lateral-acceleration guard stay exactly as tuned.
250static const float REF_VEHICLE_LENGTH = 4.9f; // m, tuning-fleet car bbox length
251static const float REF_MAX_STEER_ANGLE = 70.0f; // deg, tuning-fleet wheel lock
252static const float MAX_STEER_AUTHORITY_CORRECTION = 2.5f;
253// Ceiling for the COMBINED lateral P/D scale (pursuit-distance schedule x
254// steering-authority correction). Both factors approximate the pure-pursuit
255// equivalent gain, but their product on a long vehicle at a short pursuit
256// distance overshoots it (measured: 8.5 m trucks settled ~1.0 m INSIDE their
257// reference through junction turns at a composed scale of ~4.6). The ceiling
258// is the largest scale the schedule alone produces on the tuned car fleet
259// (MAX/MIN pursuit distance = 10/3), which validated cleanly; the authority
260// correction fills toward that ceiling for long vehicles instead of
261// exceeding it. The steering ENVELOPE keeps the full authority correction:
262// it is a physical curvature cap, not a loop gain.
263static const float MAX_LATERAL_GAIN_SCALE =
266// Bounding-box length to wheelbase ratio, stable across the CARLA fleet
267// (0.55-0.62 on the shipped vehicles); used with the physical wheel lock to
268// invert the steering-to-curvature map tan(steer * lock) / wheelbase exactly
269// instead of linearizing it. The linearized P law was tuned at car-scale
270// commands (|steer| ~ 0.1, where tan is linear); long vehicles need
271// |steer| ~ 0.4-0.5 through junctions, where the uncompensated tan() makes
272// the linear law over-curve and settle ~1 m inside its reference.
273static const float WHEELBASE_FRACTION = 0.6f;
274// Margin over the exact pure-pursuit curvature (kappa = 2 sin(alpha) / d).
275// Chosen so the anchor car at the cruise pursuit distance reproduces the
276// validated linear gain P = 2: P_pp = 2 * L_wb * pi / (d * lock_rad) = 1.49
277// at d = 10 m, and 1.32 * 1.49 * dev matches 2 * dev in the small-command
278// regime the cruise tuning was measured in.
279static const float PURSUIT_CURVATURE_MARGIN = 1.32f;
280// Nominal controller period: the sync-mode fixed_delta_seconds the gains were
281// tuned at. In asynchronous mode the real period is one server frame, which
282// under render load (e.g. a path-traced rt_lens sensor halving the frame
283// rate) can be 2-4x this. RunStep therefore takes the measured period and
284// compensates; DT is only the design point and the fallback when no valid
285// measurement exists (first tick after registration or a state reseed).
286static const float DT = 0.05f;
287static const float INV_DT = 1.0f / DT;
288// Valid range for the measured controller period. Below MIN the derivative
289// division gets noisy; above MAX the sim is hitching so badly that reacting
290// to the full elapsed time would command huge one-tick corrections.
291static constexpr float MIN_CONTROL_DT = 0.01f;
292static const float MAX_CONTROL_DT = 0.2f;
293// Steering slew budget, per second of simulation time (0.15 per 0.05 s tick
294// at the design rate). Applying it per second instead of per tick keeps the
295// physical steering rate constant when the tick rate changes.
297// Max change in normalised angular deviation per tick accepted by the
298// lateral derivative term: 0.05 * 180 deg / 0.05 s = 180 deg/s, the upper
299// bound of real vehicle yaw. Larger jumps are target discontinuities.
300static const float MAX_DEVIATION_DELTA = 0.05f;
301// The same bound expressed as a rate, so it scales with the measured tick
302// period: 1.0 normalised units/s = 180 deg/s.
304// Deadbands hold the previous command to remove per-frame dither around the
305// trim point. A slew limit is avoided on purpose: it must be asymmetric to stay
306// safe, and that biases the average command down. The throttle ramp applies
307// only near standstill (m/s); a higher threshold also catches corner exits.
308static const float LAUNCH_RAMP_SPEED = 2.0f;
309static constexpr float MAX_LAUNCH_THROTTLE_RISE_RATE = 1.7f;
310static constexpr float THROTTLE_DEADBAND = 0.01f;
311// Otherwise the deadband snaps every ramp step back and a vehicle at rest is
312// held at zero throttle for ever.
314 "the launch ramp must be able to step out of the throttle deadband");
315static const float BRAKE_DEADBAND = 0.01f;
316static const float STEER_DEADBAND = 0.002f;
317// m/s. The steering deadband is scaled by (ref / speed)^2 above this speed,
318// since the lateral acceleration it hides grows with the square of speed.
319static const float STEER_DEADBAND_REF_SPEED = 8.0f;
320// m/s2 at which the longitudinal reference closes on a stepping target. The
321// proportional gain saturates the throttle above a 7 per cent error (0.85 / 12),
322// so without it a target step commands full throttle in one frame.
323static const float COMFORT_ACCELERATION = 2.0f;
324// Max lead of the reference over the vehicle, as a fraction of the target, so
325// it cannot run away from a blocked vehicle. Must stay above the 7 per cent
326// that saturates the throttle.
327static const float REFERENCE_LEAD_FRACTION = 0.15f;
328static const std::vector<float> LONGITUDIAL_PARAM = {12.0f, 0.05f, 0.02f};
329static const std::vector<float> LONGITUDIAL_HIGHWAY_PARAM = {20.0f, 0.05f, 0.01f};
330// Lateral gains, step-response tuned against the LINEAR Chaos steering
331// mapping (the previous values were tuned while the engine still applied a
332// squared input curve, which hid a ~2-5x effective gain increase at small
333// commands). Key constraint: at cruise speeds the P term must stay inside
334// the STEER_LIMIT_GAIN/v^2 envelope for typical errors (~1.5 m offset ->
335// deviation ~0.05); P=2 keeps the loop linear at 60 km/h where P>=3
336// saturates the envelope and hunts around the lane center without settling
337// (measured: settle 1.1 s vs >6 s). D=0.24 damps the recovery to <=0.25 m
338// overshoot at 30-60 km/h.
339static const std::vector<float> LATERAL_PARAM = {2.0f, 0.03f, 0.24f};
340static const std::vector<float> LATERAL_HIGHWAY_PARAM = {2.0f, 0.02f, 0.24f};
341} // namespace PID
342
343namespace StuckRecovery {
344// A vehicle commanded to move that stays below STUCK_SPEED for STUCK_TIME
345// while no hazard justifies the stop is considered wedged (curb, wall,
346// fence); a vehicle whose target sits more than ALIGN_ENTER_DEVIATION
347// off-heading at low speed is facing the wrong way (spun, wrong-way
348// resume). Both run the same K-turn maneuver: alternate reverse and
349// forward phases with full steering toward the target until the heading
350// deviation drops below ALIGN_EXIT_DEVIATION, then resume the PID.
351static const float STUCK_SPEED = 0.3f; // m/s
352static const double STUCK_TIME = 3.0; // s immobile before recovery
353static const double PHASE_DURATION = 1.5; // s per K-turn phase
354static const float REVERSE_THROTTLE = 0.5f;
355static const float FORWARD_THROTTLE = 0.3f;
356static const float RECOVERY_STEER = 0.8f;
357static const float ALIGN_ENTER_DEVIATION = 0.6f; // normalised angle (~108 deg)
358static const float ALIGN_EXIT_DEVIATION = 0.1f; // normalised angle (~18 deg)
359static const float MISALIGN_MAX_SPEED = 3.5f; // m/s
360} // namespace StuckRecovery
361
362namespace TrackTraffic {
363static const uint64_t BUFFER_STEP_THROUGH = 5;
364static const float INV_BUFFER_STEP_THROUGH = 1.0f / static_cast<float>(BUFFER_STEP_THROUGH);
365} // namespace TrackTraffic
366
367} // namespace constants
368} // namespace traffic_manager
369} // namespace carla
#define SQUARE(a)
This file contains various constants used in traffic manager arranged into sensible namespaces for re...
Definition Constants.h:14
long long last
Definition LaneGraph.cpp:35
static const float MAX_GEODESIC_GRID_LENGTH
Definition Constants.h:171
static const unsigned short TM_DEFAULT_PORT
Definition Constants.h:23
static const float MAX_STEER_AUTHORITY_CORRECTION
Definition Constants.h:252
static const float COMFORT_ACCELERATION
Definition Constants.h:323
static constexpr float MIN_CONTROL_DT
Definition Constants.h:291
static const std::vector< float > LATERAL_HIGHWAY_PARAM
Definition Constants.h:340
static const float REFERENCE_LEAD_FRACTION
Definition Constants.h:327
static const std::vector< float > LONGITUDIAL_PARAM
Definition Constants.h:328
static const std::vector< float > LONGITUDIAL_HIGHWAY_PARAM
Definition Constants.h:329
static constexpr float MAX_LAUNCH_THROTTLE_RISE_RATE
Definition Constants.h:309
static constexpr float THROTTLE_DEADBAND
Definition Constants.h:310
static const float PURSUIT_CURVATURE_MARGIN
Definition Constants.h:279
static const float MAX_LATERAL_GAIN_SCALE
Definition Constants.h:263
static const float STEER_DEADBAND_REF_SPEED
Definition Constants.h:319
static const std::vector< float > LATERAL_PARAM
Definition Constants.h:339
static const float SUN_ALTITUDE_DEGREES_JUST_BEFORE_SUNSET
Definition Constants.h:216
bool IsRefreshDue(const double now, const double last, const double period)
Also true when the clock jumps backwards (new episode) or on NaN, so an unusable clock refreshes rath...
Definition Constants.h:57
bool IsBatchSyncDue(const uint64_t unwaited_batches, const uint64_t limit)
Definition Constants.h:69
static const std::chrono::milliseconds SNAPSHOT_POLL_PERIOD
Definition Constants.h:53
bool IsEarlyRefreshDue(const bool refreshed_this_step, const bool missing_from_the_last_refresh)
A vehicle the server already omitted from a fresh list (dormant on a large map) must not re-arm the u...
Definition Constants.h:63
This file contains definitions of common data structures used in traffic manager.