Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:CARLA simulator Carla TrafficManagerLocal

From Leeroopedia
Knowledge Sources
Domains Traffic_Management, Autonomous_Driving
Last Updated 2026-02-15 05:00 GMT

Overview

The TrafficManagerLocal class is the concrete local implementation of the Traffic Manager that orchestrates all pipeline stages (localization, collision, traffic light, motion plan, vehicle light) and the ALSM lifecycle manager within a dedicated worker thread.

Description

TrafficManagerLocal extends TrafficManagerBase and serves as the main execution engine when the traffic manager runs in the same process as the CARLA client. It owns and coordinates all internal components:

Construction and Initialization:

  • Accepts PID controller parameter vectors for urban/highway longitudinal and lateral control, a global speed difference percentage, an EpisodeProxy, and an RPC port.
  • Instantiates all pipeline stages in its member initializer list: LocalizationStage, CollisionStage, TrafficLightStage, MotionPlanStage, VehicleLightStage, and ALSM.
  • Creates a TrafficManagerServer bound to the specified RPC port, allowing remote clients to connect.
  • Calls SetupLocalMap() to initialize the InMemoryMap (loading from cache or building from scratch).
  • Calls Start() to launch the worker thread.

Main Execution Loop (Run method):

  • Allocates frame arrays (localization_frame, collision_frame, tl_frame, control_frame) with initial capacity.
  • On each tick: waits for a new simulation frame, synchronizes the registered vehicle list, resizes frame arrays if needed, then executes the pipeline stages in order:
    • alsm.Update() -- refreshes actor state and performs lifecycle management.
    • localization_stage.Update(i) -- per-vehicle waypoint buffer management (parallelizable).
    • collision_stage.Update(i) -- per-vehicle collision detection (parallelizable).
    • traffic_light_stage.Update(i) -- per-vehicle traffic light response (parallelizable).
    • motion_plan_stage.Update(i) -- per-vehicle control command generation (parallelizable).
    • vehicle_light_stage.UpdateWorldInfo() followed by per-vehicle vehicle_light_stage.Update(i).
  • Applies the resulting control commands to the CARLA server via ApplyBatchSync.

Vehicle Management:

  • RegisterVehicles: Adds vehicles to the AtomicActorSet of registered vehicles.
  • UnregisterVehicles: Removes vehicles and cleans up their associated data across all stages.

Lifecycle Methods:

  • Start: Launches the worker thread running Run().
  • Stop: Signals the worker thread to terminate and joins it.
  • Release: Calls Stop() and performs final cleanup.
  • Reset: Reinitializes the local map and restarts the worker thread (used after map changes).

Configuration Delegation:

  • All Set* parameter methods delegate directly to the internal Parameters object.
  • SetSynchronousMode and SynchronousTick manage synchronous execution with condition variable signaling.

Usage

TrafficManagerLocal is instantiated by the TrafficManager facade class when a local TM instance is needed. It is not directly created by user code but runs as the backend when TrafficManager is constructed with a local episode proxy.

Code Reference

Source Location

  • Repository: CARLA
  • File: LibCarla/source/carla/trafficmanager/TrafficManagerLocal.cpp
  • Header: LibCarla/source/carla/trafficmanager/TrafficManagerLocal.h

Signature

class TrafficManagerLocal : public TrafficManagerBase {
public:
  TrafficManagerLocal(
    std::vector<float> longitudinal_PID_parameters,
    std::vector<float> longitudinal_highway_PID_parameters,
    std::vector<float> lateral_PID_parameters,
    std::vector<float> lateral_highway_PID_parameters,
    float perc_difference_from_limit,
    cc::detail::EpisodeProxy &episode_proxy,
    uint16_t &RPCportTM);

  ~TrafficManagerLocal();

  void Start();
  void Stop();
  void Run();
  void Release();
  void Reset();

  void RegisterVehicles(const std::vector<ActorPtr> &actor_list);
  void UnregisterVehicles(const std::vector<ActorPtr> &actor_list);

  void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage) override;
  void SetDesiredSpeed(const ActorPtr &actor, const float value) override;
  void SetLaneOffset(const ActorPtr &actor, const float offset) override;
  void SetHybridPhysicsMode(const bool mode_switch) override;
  void SetSynchronousMode(bool mode) override;
  bool SynchronousTick() override;
  // ... all TrafficManagerBase overrides

private:
  void SetupLocalMap();

  std::vector<float> longitudinal_PID_parameters;
  std::vector<float> longitudinal_highway_PID_parameters;
  std::vector<float> lateral_PID_parameters;
  std::vector<float> lateral_highway_PID_parameters;
  cc::detail::EpisodeProxy episode_proxy;
  cc::World world;
  AtomicActorSet registered_vehicles;
  int registered_vehicles_state;
  std::vector<ActorId> vehicle_id_list;
  LocalMapPtr local_map;
  BufferMap buffer_map;
  TrackTraffic track_traffic;
  SimulationState simulation_state;
  Parameters parameters;
  LocalizationFrame localization_frame;
  CollisionFrame collision_frame;
  TLFrame tl_frame;
  ControlFrame control_frame;
  LocalizationStage localization_stage;
  CollisionStage collision_stage;
  TrafficLightStage traffic_light_stage;
  MotionPlanStage motion_plan_stage;
  VehicleLightStage vehicle_light_stage;
  ALSM alsm;
  TrafficManagerServer server;
  std::atomic<bool> run_traffic_manger;
  std::unique_ptr<std::thread> worker_thread;
};

Import

#include "carla/trafficmanager/TrafficManagerLocal.h"

I/O Contract

Inputs

Name Type Required Description
longitudinal_PID_parameters std::vector<float> Yes PID gains [Kp, Ki, Kd] for urban longitudinal control
longitudinal_highway_PID_parameters std::vector<float> Yes PID gains [Kp, Ki, Kd] for highway longitudinal control
lateral_PID_parameters std::vector<float> Yes PID gains [Kp, Ki, Kd] for urban lateral control
lateral_highway_PID_parameters std::vector<float> Yes PID gains [Kp, Ki, Kd] for highway lateral control
perc_difference_from_limit float Yes Default global percentage speed difference from road speed limits
episode_proxy cc::detail::EpisodeProxy& Yes Proxy to the current CARLA simulation episode
RPCportTM uint16_t& Yes RPC port for the traffic manager server

Outputs

Name Type Description
Control commands ControlFrame Batch of ApplyVehicleControl or ApplyTransform commands applied to the CARLA server each tick
Vehicle light commands ControlFrame Vehicle light state updates included in the control frame

Usage Examples

// TrafficManagerLocal is created internally by the TrafficManager facade:
// (Simplified from TrafficManager::CreateTrafficManagerServer)
uint16_t port = 8000;
auto tm_local = new TrafficManagerLocal(
    {10.0f, 0.0f, 0.1f},    // urban longitudinal PID
    {6.0f, 0.0f, 0.1f},     // highway longitudinal PID
    {10.0f, 0.0f, 0.1f},    // urban lateral PID
    {6.0f, 0.0f, 0.1f},     // highway lateral PID
    0.0f,                     // default speed difference
    episode_proxy,
    port);

// Register vehicles:
tm_local->RegisterVehicles(vehicle_list);

// Configure parameters:
tm_local->SetPercentageSpeedDifference(vehicle, -10.0f);
tm_local->SetSynchronousMode(true);

// Synchronous tick:
bool success = tm_local->SynchronousTick();

// Cleanup:
tm_local->Release();

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment