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.

Workflow:CARLA simulator Carla Autonomous Navigation

From Leeroopedia
Knowledge Sources
Domains Autonomous_Driving, Motion_Planning, Navigation
Last Updated 2026-02-15 12:00 GMT

Overview

End-to-end process for navigating a vehicle autonomously through a CARLA simulation using the built-in agent navigation stack with global route planning, local trajectory following, and PID-based vehicle control.

Description

This workflow covers autonomous vehicle navigation from destination selection through real-time driving execution. The process uses a hierarchical navigation stack: a Global Route Planner computes the optimal path as a sequence of waypoints using A* graph search over the road network, a Local Planner tracks these waypoints using a buffer queue, and dual PID controllers (longitudinal for speed, lateral for steering) generate low-level vehicle control commands. Three agent behaviors are available: BasicAgent (simple obstacle avoidance), BehaviorAgent (intelligent car-following with lane changes), and ConstantVelocityAgent (maintains fixed speed). The agents handle traffic light compliance, vehicle obstacle detection, and emergency braking.

Usage

Execute this workflow when you need to drive a vehicle autonomously between two locations in the simulation, test navigation algorithms against CARLA traffic, or generate driving behavior data. The ego vehicle must already be spawned in the world (see the Simulation Setup workflow).

Execution Steps

Step 1: Initialize the Navigation Agent

Instantiate the desired agent type with the ego vehicle actor. The agent constructor initializes internal components: a GlobalRoutePlanner that builds a graph representation of the road network, a LocalPlanner with PID controllers, and perception data structures for hazard detection. Configure agent parameters such as target speed, safety distance, and behavior profile.

Key considerations:

  • BasicAgent is suitable for straightforward point-to-point navigation
  • BehaviorAgent supports three profiles: Cautious, Normal, and Aggressive with different safety margins
  • ConstantVelocityAgent maintains a fixed speed regardless of traffic conditions
  • PID controller gains (Kp, Ki, Kd) can be tuned via the opt_dict parameter

Step 2: Build the Road Network Graph

The GlobalRoutePlanner converts the map topology into a directed graph. Road segments are sampled at a fixed resolution (default 2 meters) to create graph nodes. Edges represent lane-following connections and lane-change links. Lane change edges are added with zero cost where road markings permit.

Key considerations:

  • The graph is built once per map and cached for subsequent route queries
  • Sampling resolution affects path smoothness and computation cost
  • Lane change links enable the planner to find routes that require lane changes
  • Junction nodes are marked for special handling during turn detection

Step 3: Compute the Global Route

Set a destination location and invoke the route planner. The planner localizes both origin and destination on the graph, then uses A* search with Euclidean distance heuristic to find the shortest path. The result is a sequence of (waypoint, RoadOption) pairs where RoadOption indicates the maneuver type at each point (LANEFOLLOW, LEFT, RIGHT, STRAIGHT, CHANGELANELEFT, CHANGELANERIGHT).

Key considerations:

  • The route is computed as a list of fine-grained waypoints with turn annotations
  • A* guarantees optimal paths in the weighted graph
  • RoadOption annotations guide the local planner through intersections
  • Routes can be set as a pre-computed list or computed from a destination location

Step 4: Execute the Perception Loop

Each simulation frame, the agent gathers perception data: current vehicle state (speed, location, transform), nearby vehicles via bounding box queries, and traffic light states. The BehaviorAgent extends this with pedestrian detection, time-to-collision estimation, and tailgating analysis.

Key considerations:

  • Vehicle detection uses bounding box polygon intersection on the planned path
  • Traffic light detection checks for red lights within a configurable distance ahead
  • BehaviorAgent computes time-to-collision (TTC) for adaptive speed control
  • Perception range and sensitivity depend on the agent behavior profile

Step 5: Generate Vehicle Control via PID

The Local Planner maintains a rolling buffer of target waypoints from the global route. Each frame, it selects the next target waypoint from the queue and feeds it to the VehiclePIDController. The longitudinal PID controls throttle and brake to match the target speed, while the lateral PID computes steering angle to track the target waypoint. The outputs are combined into a VehicleControl command.

Key considerations:

  • The longitudinal controller uses a 10-sample error history for derivative and integral terms
  • The lateral controller computes cross-track error using vehicle-to-waypoint heading difference
  • Steering is smoothed by limiting changes to 0.1 per frame to prevent oscillation
  • Throttle is capped at 0.75 and brake at 0.3 for smooth driving
  • The waypoint queue auto-generates random waypoints at intersections when no global plan is set

Step 6: Apply Hazard Overrides

If the perception system detects a hazard (blocking vehicle or red traffic light), the agent overrides the PID output with an emergency stop command. The BehaviorAgent additionally applies graduated responses: car-following with adaptive speed, lane change maneuvers to avoid slow vehicles, and intersection speed reduction.

Key considerations:

  • Emergency stop applies brake=0.6 while maintaining the current steering angle
  • BehaviorAgent car-following adjusts speed based on TTC thresholds
  • Lane changes are only attempted when markings permit and adjacent lanes are clear
  • Intersection behavior reduces speed proportionally to turn angle

Step 7: Check Destination Arrival

After each control step, verify whether the vehicle has reached its destination by checking if the waypoint queue is nearly empty. If the destination is reached, the agent signals completion. A new destination can then be set for continued navigation.

Key considerations:

  • done() returns True when the waypoint buffer has fewer than 2 remaining waypoints
  • The agent can be given a new destination without re-initialization
  • In continuous operation, random waypoints are generated to keep the vehicle moving

Execution Diagram

GitHub URL

Workflow Repository