mazda: add alpha longitudinal support#3355
Open
yummydirtx wants to merge 3 commits intocommaai:masterfrom
Open
mazda: add alpha longitudinal support#3355yummydirtx wants to merge 3 commits intocommaai:masterfrom
yummydirtx wants to merge 3 commits intocommaai:masterfrom
Conversation
Contributor
Car behavior reportReplays driving segments through this PR and compares the behavior to master. Testing 17 segments for: MAZDA_CX5, MAZDA_CX9_2021, MAZDA_CX5_2022 Show changes |
Contributor
There was a problem hiding this comment.
Thanks for contributing to opendbc! In order for us to review your PR as quickly as possible, check the following:
- Convert your PR to a draft unless it's ready to review
- Read the contributing docs
- Before marking as "ready for review", ensure:
- the goal is clearly stated in the description
- all the tests are passing
- include a route or your device' dongle ID if relevant
…improve CAN message handling in tests
yozorakumo
added a commit
to yozorakumo/FrogPilot
that referenced
this pull request
May 8, 2026
… for Mazda 2 DJ MT - Fix hazard recognition: use BLINK_INFO LEFT_BLINK+RIGHT_BLINK instead of TURN_SWITCH HAZARD (momentary signal) - Add longitudinal control support based on PR commaai/opendbc#3355 - New file: selfdrive/car/mazda/longitudinal.py (CRZ_INFO/CRZ_CTRL message builder, radar session management) - Update DBC: add CRZ_INFO, STEER_RELATED, STEER2 messages to mazda_2_dj_mt.dbc - Update interface.py: enable alphaLongitudinalAvailable for MAZDA_2_DJ_MT - Update carstate.py: PEDALS-based cruise state for longitudinal mode - Update carcontroller.py: Stop-and-Go state machine, accel calculation, longitudinal message sending - Update safety_mazda.h: longitudinal safety checks (ACCEL_CMD, CRZ_CTRL, RADAR UDS whitelist) - Add implementation plan document
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mazda Longitudinal (MRCC) Port Writeup
This PR adds alpha longitudinal control for the Mazda CX-5 by taking over the stock MRCC longitudinal path and publishing synthetic ACC control messages that preserve Mazda's expected state transitions, including stop-and-go hold and resume behavior.
The implementation is intentionally conservative: it keeps the stock lateral path structure, reuses OEM-like signal patterns where possible, and aligns software behavior with Panda safety constraints so invalid longitudinal packets are rejected early.
Scope
opendbc/car/mazda/interface.pyopendbc/car/mazda/carstate.pyopendbc/car/mazda/carcontroller.pyopendbc/car/mazda/longitudinal.pyopendbc/safety/modes/mazda.hopendbc/safety/tests/test_mazda.pyopendbc/safety/tests/common.pyopendbc/dbc/mazda_2017.dbcCurrent Availability
Longitudinal is enabled as alpha-long for:
MAZDA_CX5_2022In
CarInterface, this is gated with:ret.alphaLongitudinalAvailable = candidate == CAR.MAZDA_CX5_2022ret.openpilotLongitudinalControl = alpha_long and ret.alphaLongitudinalAvailableIt's entirely possible that this alpha longitudinal method works on CX-9 too, but that needs to be verified with further on-car testing.
Architecture Overview
Mazda stock ACC behavior is radar-owned for longitudinal. The approach in this port is:
CRZ_INFOandCRZ_CTRL) at stock-like timing and state transitions.Radar Session Management
0x764on bus00x10 0x02), with retries.0x3E 0x80) periodically to keep suppression active.Key implementation points:
enter_radar_programming_session(...)inlongitudinal.pycreate_radar_tester_present(...)sent everyTESTER_PRESENT_STEP = 50control framesAssuming
DT_CTRL = 0.01s, tester-present is sent at ~2 Hz.Command Path
Messages and Timing
Longitudinal control publishes:
CRZ_INFO(0x21B) on bus 0CRZ_CTRL(0x21C) on bus 0at
LONG_COMMAND_STEP = 2control frames.Assuming
DT_CTRL = 0.01s, this is ~50 Hz.CRZ_INFO(0x21B)CRZ_INFOcarries the synthetic acceleration request and stop/resume state bits.Fields explicitly controlled:
ACCEL_CMDACC_ACTIVEACC_SET_ALLOWEDCRZ_ENDED(forced 0)STOPPING_MAYBESTOPPING_MAYBE2RESUME_UNLATCHING_MAYBECTR1(mod 16)Accel Command Mapping
ACCEL_CMDis generated as a raw integer command with speed-dependent scaling:m/s):(0.0, 4.2, 11.1, 22.2)(1000, 1000, 950, 800)m/s):(0.0, 1.4, 5.6, 22.2)(1200, 1000, 925, 950)[-2000, 2000]Rationale: one global scale worked but felt too aggressive at higher speed; split maps keep stronger low/mid-speed authority and soften high-speed behavior.
Checksum
CRZ_INFOchecksum is computed as inverted byte-sum (excluding checksum byte), with an additional+0x04bias when active stop bits are asserted (ACTIVE_STOP_CHECKSUM_BIAS).That bias was required to match Mazda's expected stop-phase encoding.
CRZ_CTRL(0x21C)CRZ_CTRLis built from profile templates and then patched with state bits/signals.Profiles:
STANDBY:02010b0000000000ENGAGED_CRUISE:0a018b2000001000ENGAGED_FOLLOW:0a018b4000001000STOP_GO_HOLD:0a018b6000001000STOP_GO_HOLD_LATCHED:0a018b8000001000Patched fields include:
CRZ_ACTIVEACC_ACTIVE_2DISABLE_TIMER_1andDISABLE_TIMER_2(forced 0)RADAR_HAS_LEADRADAR_LEAD_RELATIVE_DISTANCEACC_GAS_MAYBE2The logic explicitly sets lead-distance/gas semantics for OEM-like transitions:
ACC_GAS_MAYBE2 = 0ACC_GAS_MAYBE2 = 1Stop-and-Go Strategy
The core goal is to reproduce Mazda's hold/release behavior without abrupt unlatches.
State Latching
Controller-level latches track stop intent and resume phases:
stop_intent_latchedstandstill_hold_framesresume_release_framesresume_crz_latched_framesresume_phase_framesImportant timing thresholds:
CRZ_CTRL_LATCH_FRAMES: ~2.0sCRZ_CTRL_PASSIVE_FRAMES: ~9.6sHOLD_REQUEST_FRAMES: ~6.0sRESUME_RELEASE_FRAMES: ~0.5sCRZ_INFO_RESUME_PHASE_FRAMES: ~0.2sHold Brake Commands
When stop intent is active:
Raw targets used:
-1024-750(ramped into hold)-1This mirrors observed OEM behavior: braking force is carried through active stop, then relaxed once hold latch is established.
Resume Handling
Resume can be requested physically (wheel RES) or virtually (planner resume), but virtual resume is intentionally delayed until a RES frame is actually transmitted and hold latch is ready.
This avoids releasing synthetic hold from transient planner noise.
Additional safeguards:
CRZ_INFOstop bits are cleared during release to avoid "positive accel + active stop" mismatch.CarState and Engagement Semantics
In alpha-long mode, radar-owned
CRZ_CTRLis suppressed. Because of that:CarStatedoes not rely onCRZ_CTRLfor cruise availability.PEDALSbits:ACC_OFF= armedACC_ACTIVE= engagedCRZ_BTNSparsing includesCANCELand syntheticMAINdecoding (MODE_X && MODE_Y) so button events remain reliable under radar suppression.This keeps higher-level engagement logic stable while stock radar control messages are intentionally absent.
Safety Model
Mazda safety mode now supports a longitudinal parameter:
MAZDA_PARAM_LONGITUDINAL = 1When enabled, TX allowlist extends to:
0x21B(CRZ_INFO)0x21C(CRZ_CTRL)0x764(radar UDS)Longitudinal Safety Checks
CRZ_INFO.ACCEL_CMDis decoded from packed bits and validated with longitudinal limits:max_accel = 2000min_accel = -2000inactive_accel = 0CRZ_CTRLwith active cruise is blocked when controls are not allowed.0x764is strictly whitelisted to:0x3E 0x80)0x10 0x01or0x10 0x02)CRZ_CTRLtoPEDALSbits to avoid false disengage edges during suppression.Test Coverage
Added/extended tests include:
TestMazdaLongitudinalSafetywith Mazda long safety param (1)[-2000, 2000]This ensures Panda safety behavior is aligned with sender-side clipping and signal packing.
My Approach (Design Notes)
The guiding principles for this implementation were:
CX-5 2022) while behavior and logs mature.Known Limitations
MAZDA_CX5_2022only.Future Work
Validation