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