Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HKG Longitudinal Tuning #80

Open
wants to merge 180 commits into
base: master-new
Choose a base branch
from
Open

HKG Longitudinal Tuning #80

wants to merge 180 commits into from

Conversation

Discountchubbs
Copy link

@Discountchubbs Discountchubbs commented Mar 13, 2025

Custom tuning for HKG vehicles, taking into account lower, upper jerk, and comfort bands. When the user selects toggles, these plus interface tuning will be applied. Furthermore, if the user presses the button to enable smoother braking, my custom accel logic will be utilized to greatly filter and smoothen accel/braking transitions for a more chill open pilot experience.

Current issue:
--While CB calculation works excellent to limit rough transitions from acceleration costs, there needs to be changes in the logic.. Zero'd out for now.

Summary by Sourcery

New Features:

  • Adds custom longitudinal tuning for Hyundai/Kia vehicles, allowing adjustment of jerk and comfort band parameters.

Copy link

sourcery-ai bot commented Mar 13, 2025

Reviewer's Guide by Sourcery

This pull request introduces custom longitudinal tuning for HKG vehicles, allowing for adjustments to jerk limits and comfort bands. It also implements a smoother braking feature using custom acceleration logic to filter and smooth acceleration/braking transitions. The changes involve creating new classes and functions for managing tuning parameters, integrating them into the car controller, and modifying CAN message creation to include the new parameters.

Sequence diagram for longitudinal control update with HKGLongitudinalController

sequenceDiagram
  participant CC as CarController
  participant HKGLC as HKGLongitudinalController
  participant HKGLT as HKGLongitudinalTuning

  CC->HKGLC: update(longControlState, actuators, CS, CP_SP)
  activate HKGLC
  HKGLC->HKGLC: calculate_accel(actuators, CS, CP_SP)
  HKGLC->HKGLC: calculate_and_get_jerk(actuators, CS, longControlState)
  alt HKGLC.tuning is not None
    HKGLC->HKGLT: make_jerk(CS, actuators)
    activate HKGLT
    HKGLT-->HKGLC: jerk_output
    deactivate HKGLT
  else
    HKGLC-->HKGLC: jerk_limit
  end
  HKGLC-->CC: None
  deactivate HKGLC
Loading

Updated class diagram for HKGLongitudinalController

classDiagram
  class HKGLongitudinalController {
    -CP: structs.CarParams
    -tuning: HKGLongitudinalTuning
    -jerk: None
    -jerk_upper: float
    -jerk_lower: float
    -cb_upper: float
    -cb_lower: float
    -accel: float
    +__init__(CP: structs.CarParams, CP_SP: structs.CarParamsSP)
    +apply_tune(CP: structs.CarParams, CP_SP: structs.CarParamsSP)
    +get_jerk(): JerkOutput
    +calculate_and_get_jerk(actuators: structs.CarControl.Actuators, CS: structs.CarState, long_control_state: LongCtrlState): JerkOutput
    +calculate_accel(actuators: structs.CarControl.Actuators, CS: structs.CarState, CP_SP: structs.CarParamsSP): float
    +update(long_control_state: LongCtrlState, actuators: structs.CarControl.Actuators, CS: structs.CarState, CP_SP: structs.CarParamsSP): None
  }
  class HKGLongitudinalTuning {
    -CP: structs.CarParams
    -DT_CTRL: float
    -accel_last: float
    -accel_last_jerk: float
    -jerk: float
    -jerk_count: float
    -jerk_upper_limit: float
    -jerk_lower_limit: float
    -cb_upper: float
    -cb_lower: float
    -last_decel_time: float
    -min_cancel_delay: float
    -car_config: CarTuningConfig
    +__init__(CP: structs.CarParams)
    -_setup_controllers(): None
    -_init_state(): None
    -_setup_car_config(): None
    +make_jerk(CS: structs.CarState, actuators: structs.CarControl.Actuators): float
    +handle_cruise_cancel(CS: structs.CarState): bool
    +calculate_limited_accel(actuators: structs.CarControl.Actuators, CS: structs.CarState): float
    +calculate_accel(actuators: structs.CarControl.Actuators, CS: structs.CarState): float
    +apply_tune(CP: structs.CarParams): None
  }
  class CarTuningConfig {
    -vego_stopping: float
    -vego_starting: float
    -stopping_decel_rate: float
    -start_accel: float
    -jerk_limits: tuple[float, float]
    -brake_response: tuple[float, float, float, float]
    -accel_limits: tuple[float, float]
  }
  class Cartuning {
    +get_car_config(CP: structs.CarParams): CarTuningConfig
  }
  class JerkOutput {
    -jerk_upper_limit: float
    -jerk_lower_limit: float
    -cb_upper: float
    -cb_lower: float
    +__init__(jerk_upper_limit: float, jerk_lower_limit: float, cb_upper: float, cb_lower: float)
  }

  HKGLongitudinalController -- HKGLongitudinalTuning : has
  HKGLongitudinalTuning -- CarTuningConfig : uses
  HKGLongitudinalController -- JerkOutput : returns
  Cartuning -- CarTuningConfig : returns

  note for HKGLongitudinalController "This class manages the longitudinal control with custom tuning."
  note for HKGLongitudinalTuning "This class handles the longitudinal tuning logic."
  note for CarTuningConfig "This class holds the tuning parameters for different car types."
  note for Cartuning "This class provides the car-specific tuning configurations."
  note for JerkOutput "This class represents the output of jerk calculation."
Loading

File-Level Changes

Change Details Files
Introduces a new class HKGLongitudinalController to manage longitudinal tuning for HKG vehicles, including jerk limits and comfort bands.
  • Adds HKGLongitudinalController to CarController's inheritance list.
  • Initializes HKGLongitudinalController in CarController's constructor.
  • Calls HKGLongitudinalController.update in CarController.update to apply longitudinal control logic.
  • Passes jerk limits and comfort band values from HKGLongitudinalController to create_acc_commands for CAN message creation.
opendbc/car/hyundai/carcontroller.py
Modifies create_acc_commands in hyundaican.py to accept jerk limits and comfort band parameters.
  • Updates the function signature of create_acc_commands to include jerk_upper, jerk_lower, cb_upper, and cb_lower.
  • Passes these new parameters to get_scc14_values for inclusion in the CAN message.
  • Updates the get_scc14_values function to use the passed-in jerk limit and comfort band values.
opendbc/car/hyundai/hyundaican.py
Updates create_acc_control in hyundaicanfd.py to include jerk limits in the CAN message.
  • Updates the function signature of create_acc_control to include jerk_upper and jerk_lower.
  • Passes these new parameters to the CAN message values.
opendbc/car/hyundai/hyundaicanfd.py
Adds a new flag HKGLONGTUNING to HyundaiFlagsSP and integrates HKGLongitudinalController into the car interface.
  • Adds HKGLONGTUNING and HKGLONGTUNING_BRAKING to the HyundaiFlagsSP enum.
  • Initializes and applies tuning from HKGLongitudinalController in _get_params if HKGLONGTUNING is enabled.
  • Adds CP_SP to CarParams struct.
opendbc/car/hyundai/interface.py
opendbc/car/interfaces.py
opendbc/sunnypilot/car/hyundai/values.py
Introduces a new module longitudinal_config.py to manage car-specific longitudinal tuning parameters.
  • Defines a CarTuningConfig dataclass to hold longitudinal tuning parameters.
  • Creates default tuning configurations for EV, Hybrid, and ICE vehicles.
  • Creates car-specific configurations overriding the defaults.
  • Implements a get_car_config method to retrieve the appropriate configuration based on car fingerprint and flags.
opendbc/sunnypilot/car/hyundai/longitudinal_config.py
Implements HKGLongitudinalTuning and HKGLongitudinalController classes for managing and applying longitudinal tuning.
  • Creates HKGLongitudinalTuning class to handle longitudinal tuning logic, including jerk calculation and adaptive acceleration limiting.
  • Creates HKGLongitudinalController class to integrate the tuning logic into the car controller.
  • Adds methods to calculate and apply jerk limits, comfort bands, and acceleration limits based on car-specific configurations.
  • Adds logic to handle cruise control cancel to prevent faults.
opendbc/sunnypilot/car/hyundai/longitudinal_tuning.py
Adds interpolation utilities for vehicle dynamics.
  • Adds catmull_rom_interp function for 1D Catmull-Rom interpolation.
  • Adds makima_interp function for Modified Akima interpolation.
opendbc/sunnypilot/interpolation_utils.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Discountchubbs Discountchubbs requested a review from a team March 13, 2025 18:53
@Discountchubbs Discountchubbs added the enhancement New feature or request label Mar 13, 2025
@Discountchubbs Discountchubbs marked this pull request as ready for review March 13, 2025 19:00
@sunnyhaibin sunnyhaibin marked this pull request as draft March 15, 2025 16:41
@Discountchubbs Discountchubbs marked this pull request as ready for review March 20, 2025 23:43
This resolves the issues caused by the incorrect sequence.
Moved Hyundai-specific flag handling logic to utilize CP_SP flags instead of CP flags. This ensures better separation of parameters and aligns with the updated parameter structure. Updated related functions and class initializations for consistency.
…e CP_SP parameter to default to `None`, improving compatibility
…e CP_SP parameter to default to `None`, improving compatibility
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants