The Planning and Localization stack in openpilot is responsible for determining the vehicle's precise state in the world and calculating the optimal trajectories for both longitudinal (acceleration/braking) and lateral (steering) control. This system bridges the gap between raw perception (vision/radar) and the control actuation layer.
plannerd)The plannerd daemon serves as the central orchestration point for high-level driving logic. It primarily manages longitudinal planning and safety features like Lane Departure Warning (LDW).
The LongitudinalPlanner class calculates the desired acceleration and velocity profile. It uses a Model Predictive Control (MPC) approach to balance multiple objectives: following a lead car, maintaining cruise speed, and adhering to comfort/safety limits.
modelV2 (vision-based paths and leads), radarState (physical lead measurements), and carState (current vehicle speed) openpilot/selfdrive/controls/plannerd.py22-23LongitudinalMpc to solve for the optimal trajectory over a 10-second horizon openpilot/selfdrive/controls/lib/longitudinal_planner.py51plannerd runs the LaneDepartureWarning logic, which monitors the vehicle's position relative to lane lines and triggers alerts if a departure is imminent without driver intent (e.g., no turn signal) openpilot/selfdrive/controls/plannerd.py31-36
Sources: openpilot/selfdrive/controls/plannerd.py11-41 openpilot/selfdrive/controls/lib/longitudinal_planner.py48-160 openpilot/selfdrive/controls/lib/longitudinal_mpc_lib/long_mpc.py25-87
long_mpc)The longitudinal trajectory is optimized using the acados solver, which handles a non-linear least squares problem.
The system defines a state vector $x = [x_{ego}, v_{ego}, a_{ego}]^T$ and control input $u = [j_{ego}]$ (jerk) openpilot/selfdrive/controls/lib/longitudinal_mpc_lib/long_mpc.py94-99
| Parameter | Role |
|---|---|
T_IDXS | Non-uniform time steps for the prediction horizon (up to 10s) openpilot/selfdrive/controls/lib/longitudinal_mpc_lib/long_mpc.py51-53 |
T_FOLLOW | Desired time gap to the lead vehicle, varying by LongitudinalPersonality openpilot/selfdrive/controls/lib/longitudinal_mpc_lib/long_mpc.py73-81 |
DANGER_ZONE_COST | Penalty for entering the unsafe distance buffer behind a lead openpilot/selfdrive/controls/lib/longitudinal_mpc_lib/long_mpc.py41 |
A_CHANGE_COST | Cost associated with rapid changes in acceleration to ensure smoothness openpilot/selfdrive/controls/lib/longitudinal_mpc_lib/long_mpc.py40 |
J_EGO_COST | Penalty on jerk to maintain passenger comfort openpilot/selfdrive/controls/lib/longitudinal_mpc_lib/long_mpc.py39 |
Sources: openpilot/selfdrive/controls/lib/longitudinal_mpc_lib/long_mpc.py89-213 openpilot/selfdrive/controls/lib/longitudinal_planner.py130-149
Localization and vehicle parameter estimation are handled by a fusion of IMU, GPS, and vehicle CAN data across several specialized daemons.
calibrationd)This daemon determines the extrinsic orientation of the camera relative to the vehicle frame (Pitch, Yaw, and Roll).
HEIGHT_INIT for camera height openpilot/selfdrive/locationd/calibrationd.py9ModelRenderer in the UI consumes liveCalibration data to correctly project 3D points (paths, lane lines) into the 2D screen space openpilot/selfdrive/ui/onroad/model_renderer.py100-101paramsd)paramsd learns vehicle-specific physical properties while driving.
angleOffsetDeg which is used by the LongitudinalPlanner to calculate steering-angle-without-offset for turn-based acceleration limiting openpilot/selfdrive/controls/lib/longitudinal_planner.py89-90torqued)torqued estimates parameters required for lateral torque control.
ModelRenderer uses torque output from carOutput to visualize steering effort openpilot/selfdrive/ui/mici/onroad/model_renderer.py100lagd)lagd estimates the mechanical and electrical delay in the steering actuator.
masked_normalized_cross_correlation to find the time shift between the planner's desiredCurvature (from controlsState) and the IMU's angularVelocityDevice.z (actual yaw rate from livePose) openpilot/selfdrive/locationd/lagd.py55-109 openpilot/selfdrive/locationd/test/test_lagd.py37-39BlockAverage and filtered to provide a stable lateralDelay value openpilot/selfdrive/locationd/lagd.py137-170MIN_VEGO) and yaw rate to ensure signal quality openpilot/selfdrive/locationd/lagd.py24-25locationd)locationd fuses IMU data with camera odometry to provide a high-frequency livePose message.
radard uses the vehicle speed (vEgo) and vision leads to match radar tracks to model-predicted objects openpilot/selfdrive/controls/radard.py119-140Sources: openpilot/selfdrive/locationd/lagd.py173-182 openpilot/selfdrive/locationd/test/test_lagd.py8-46 openpilot/selfdrive/controls/radard.py186-196 openpilot/selfdrive/ui/onroad/model_renderer.py84-132
The following table and diagram map system responsibilities to specific code entities.
| Concept | Code Entity | File Path |
|---|---|---|
| Longitudinal Plan | LongitudinalPlanner | openpilot/selfdrive/controls/lib/longitudinal_planner.py48 |
| MPC Solver | LongitudinalMpc | openpilot/selfdrive/controls/lib/longitudinal_mpc_lib/long_mpc.py216 |
| Actuator Delay | LateralLagEstimator | openpilot/selfdrive/locationd/lagd.py173 |
| Radar Processing | RadarD | openpilot/selfdrive/controls/radard.py186 |
| Path Rendering | ModelRenderer | openpilot/selfdrive/ui/onroad/model_renderer.py45 |
Sources: openpilot/selfdrive/controls/plannerd.py21-36 openpilot/selfdrive/locationd/lagd.py173-182 openpilot/selfdrive/controls/radard.py186-196 openpilot/selfdrive/controls/lib/longitudinal_planner.py48-65
Refresh this wiki