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: Add ESCC (Enhanced Smart Cruise Control) support #70

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

Conversation

devtekve
Copy link

@devtekve devtekve commented Feb 1, 2025

@sourcery-ai

supersedes #28

Summary by Sourcery

Add support for Enhanced Smart Cruise Control (ESCC) on Hyundai vehicles. This includes sending ESCC messages over CAN.

New Features:

  • Support Enhanced Smart Cruise Control (ESCC) for Hyundai vehicles.

Tests:

  • Added a test script to flash ESCC firmware over CAN.

@devtekve devtekve added the enhancement New feature or request label Feb 1, 2025
Copy link

sourcery-ai bot commented Feb 1, 2025

Reviewer's Guide by Sourcery

This pull request introduces support for Enhanced Smart Cruise Control (ESCC) in Hyundai vehicles. It includes changes to the main control logic, safety configurations, and build process to accommodate the new ESCC functionality. Additionally, a test script for flashing ESCC firmware over CAN has been added.

Sequence diagram for ESCC message handling

sequenceDiagram
    participant Car as Car CAN Bus
    participant Radar as Radar CAN Bus
    participant ESCC as ESCC Module
    participant SP as SunnyPilot

    Note over ESCC: Initialize ESCC message structure
    Radar->>ESCC: SCC11 (0x420)
    activate ESCC
    ESCC->>ESCC: Update radar points data
    ESCC->>Car: Send ESCC message (0x2AB)
    deactivate ESCC

    Radar->>ESCC: SCC12 (0x421)
    activate ESCC
    ESCC->>ESCC: Update AEB data
    ESCC->>Car: Send ESCC message (0x2AB)
    deactivate ESCC

    Radar->>ESCC: FCA11 (0x38D)
    activate ESCC
    ESCC->>ESCC: Update FCA data
    ESCC->>Car: Send ESCC message (0x2AB)
    deactivate ESCC
Loading

Class diagram for ESCC message structure

classDiagram
    class ESCC_Msg {
        +uint8_t fca_cmd_act
        +uint8_t aeb_cmd_act
        +uint8_t cf_vsm_warn_fca11
        +uint8_t cf_vsm_warn_scc12
        +uint8_t cf_vsm_deccmdact_scc12
        +uint8_t cf_vsm_deccmdact_fca11
        +uint8_t cr_vsm_deccmd_scc12
        +uint8_t cr_vsm_deccmd_fca11
        +uint8_t obj_valid
        +uint8_t acc_objstatus
        +uint8_t acc_obj_lat_pos_1
        +uint8_t acc_obj_lat_pos_2
        +uint8_t acc_obj_dist_1
        +uint8_t acc_obj_dist_2
        +uint8_t acc_obj_rel_spd_1
        +uint8_t acc_obj_rel_spd_2
    }

    class safety_hooks {
        +init()
        +rx()
        +tx()
        +fwd()
        +get_counter()
        +get_checksum()
        +compute_checksum()
    }

    safety_hooks <|-- hyundai_escc_hooks : implements
Loading

File-Level Changes

Change Details Files
Added ESCC support with a new safety mode and message handling.
  • Introduced a new SAFETY_HYUNDAI_ESCC mode.
  • Added a new is_car_safety_mode_escc function.
  • Defined CAN IDs for ESCC input and output.
  • Implemented send_escc_msg to transmit ESCC messages.
  • Added logic to handle ESCC messages and forward them to the appropriate bus.
  • Added a new ESCC_Msg struct to hold ESCC data.
  • Added a new escc_rx_hook, escc_tx_hook, and escc_fwd_hook to handle ESCC messages.
  • Modified the main loop to disable heartbeat when ESCC is enabled.
  • Initialized the safety mode to SAFETY_HYUNDAI_ESCC when ESCC is enabled.
board/main.c
board/safety.h
board/safety/safety_hyundai_escc.h
board/safety_sunnypilot_common.h
Modified the build process to support ESCC.
  • Added a new --escc option to the build system.
  • Added a new escc SConscript to build the ESCC firmware.
  • Modified the main SConscript to conditionally build the ESCC firmware.
  • Added a new CPPDEFINES option to the build system.
SConscript
SConstruct
board/escc/SConscript
Added test scripts for ESCC.
  • Added a script to flash ESCC firmware over CAN.
  • Added a script to flash ESCC firmware over DFU.
  • Added a script to recover ESCC firmware over DFU.
  • Added a script to flash ESCC firmware over CAN.
  • Added a .gitignore file for the escc directory.
tests/escc/enter_canloader.py
board/escc/flash.py
board/escc/recover.sh
board/escc/flash_can.sh
board/escc/.gitignore
Added documentation for ESCC.
  • Added a README file with documentation for ESCC.
board/escc/README.MD

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

@sourcery-ai sourcery-ai bot changed the title @sourcery-ai feat: Add ESCC support Feb 1, 2025
Add initial README for ESCC board

This commit introduces a new README for the ESCC board detailing the pinout configurations and connections between the Molex connector and IONIQ harness and radar components. The documentation provides a clear, structured guide to assist with proper wiring and setup.

Improving comments for clarity and explicitness

IDE is behaving now. Wd don't need this hack anymore

Add ESCC build option and refine ESCC specific code

Introduce --escc option in SCons to conditionally build ESCC firmware. Update build_project to support CPPDEFINES and modify main.c to handle ESCC-specific safety modes and behaviors more cleanly.

Remove unnecessary send_escc_msg calls in Hyundai safety code

The updates in the code remove redundant send_escc_msg calls in the Hyundai safety code. We don't really need to send the FCA or AEB data through the ESCC packet because we don't block it at all. So we don't really need to "package" it anymore on the ESCC message

Add send_escc_msg call in case blocks

The send_escc_msg function is now called within the individual case blocks for SCC11, SCC12, and FCA11, enhancing code readability. Before, it was called on any message that the radar sends, but this strategy was inefficient as it was piggybacking on their frequencies.

Refactor `escc_tx_hook` function in Hyundai safety code

The commented-out `escc_tx_hook` function in Hyundai ESCC safety code has been refactored and is now in use again, replacing the formerly used `alloutput_tx_hook`. Debugging functionality has been preserved and conditions for when the function is unused have also been handled. The resulting code is now cleaner and more efficient.

Add ESCC firmware and build files, and safety mode for Hyundai ESCC

This commit introduces ESCC firmware and the corresponding build files. We have also defined a new safety mode for Hyundai ESCC. The necessary scripts for flashing and recovery have been added, alongside the gitignore file for the ESCC board. Additionally, we have implemented new message processing and forwarding logic to handle ESCC specific messages in main and safety files.
@devtekve devtekve force-pushed the master-new-escc-reworked branch from 3cf59d6 to afd5d9f Compare February 1, 2025 10:10
@devtekve devtekve changed the title feat: Add ESCC support feat: Add ESCC (Enhances Smart Cruise Control) support Feb 1, 2025
@devtekve devtekve changed the title feat: Add ESCC (Enhances Smart Cruise Control) support feat: Add ESCC (Enhanced Smart Cruise Control) support Feb 1, 2025
@devtekve devtekve changed the title feat: Add ESCC (Enhanced Smart Cruise Control) support hkg: Add ESCC (Enhanced Smart Cruise Control) support Feb 1, 2025
@devtekve
Copy link
Author

devtekve commented Feb 1, 2025

@sourcery-ai reviewer's guide again (ESCC is Enhanced Smart Cruise Control)

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @devtekve - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider documenting the ESCC message protocol and bit layout in safety_hyundai_escc.h to make the code more maintainable
  • The debug logging could be made more consistent - consider using a structured logging approach rather than scattered print statements
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟡 Security: 1 issue found
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

board/main.c Show resolved Hide resolved
board/safety/safety_hyundai_escc.h Show resolved Hide resolved
tests/escc/enter_canloader.py Show resolved Hide resolved
@devtekve devtekve force-pushed the master-new-escc-reworked branch from 6db7693 to f62a079 Compare February 1, 2025 10:59
@devtekve devtekve marked this pull request as ready for review February 1, 2025 11:07
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @devtekve - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.


#define DEVNULL_BUS (-1)
#define CAR_BUS 0
#define RADAR_BUS 2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider extracting the message parsing logic into separate functions to improve maintainability and testability.

The message handling could be simplified by extracting parsing logic into message-specific functions. This would improve maintainability while keeping the core logic intact:

typedef struct {
  uint8_t obj_valid;
  uint8_t acc_objstatus;
  // ... other fields
} SCC11_Data;

static SCC11_Data parse_scc11(const CANPacket_t* msg) {
  SCC11_Data data = {0};
  data.obj_valid = (GET_BYTE(msg, 2) & 0x1U);
  data.acc_objstatus = ((GET_BYTE(msg, 2) >> 6) & 0x3U);
  // ... parse other fields
  return data;
}

static void escc_rx_hook(const CANPacket_t* to_push) {
  if (bus == RADAR_BUS && (is_scc_msg || is_fca_msg)) {
    switch (addr) {
      case 0x420: {
        SCC11_Data data = parse_scc11(to_push);
        escc.obj_valid = data.obj_valid;
        escc.acc_objstatus = data.acc_objstatus;
        // ... update other fields
        send_escc_msg(&escc, CAR_BUS);
        break;
      }
      // ... other cases
    }
  }
}

This approach:

  1. Separates message parsing from state updates
  2. Makes the code more testable
  3. Reduces cognitive load by grouping related fields

tests/escc/enter_canloader.py Show resolved Hide resolved
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