Skip to content

Commit

Permalink
Merge branch 'master-new' into mads-new
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnyhaibin committed Dec 6, 2024
2 parents bd613bd + c67bfd6 commit 354c3bc
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 93 deletions.
1 change: 1 addition & 0 deletions .github/workflows/auto_pr_review.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- uses: actions/[email protected]
with:
dot: true
sync-labels: true
configuration-path: .github/labeler.yaml

# Check PR target branch
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ jobs:
unit-tests:
name: unit tests
runs-on: ${{ matrix.os }}
timeout-minutes: 1
strategy:
fail-fast: false
matrix:
os: [ubuntu-24.04, macos-latest]
#run: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
steps:
- uses: commaai/timeout@v1
- uses: actions/checkout@v4
- name: install uv and dependencies
run: |
Expand All @@ -35,10 +34,9 @@ jobs:
static-analysis:
name: static analysis
runs-on: ubuntu-24.04
timeout-minutes: 1
steps:
- uses: commaai/timeout@v1
- uses: actions/checkout@v4
- run: sudo apt install --no-install-recommends -y cppcheck
- name: setup python
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
Expand Down
14 changes: 0 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ repos:
- --counting=detailed
- --linelength=240
- --filter=-build,-legal,-readability,-runtime,-whitespace,+build/include_subdir,+build/forward_decl,+build/include_what_you_use,+build/deprecated,+whitespace/comma,+whitespace/line_length,+whitespace/empty_if_body,+whitespace/empty_loop_body,+whitespace/empty_conditional_body,+whitespace/forcolon,+whitespace/parens,+whitespace/semicolon,+whitespace/tab,+readability/braces
- repo: local
hooks:
- id: cppcheck
name: cppcheck
entry: cppcheck
language: system
types: [c++]
args:
- --error-exitcode=1
- --language=c++
- --inline-suppr
- --force
- --quiet
- -j4
- repo: local
hooks:
- id: generator
Expand Down
150 changes: 97 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<div align="center" style="text-align: center;">

<h1>opendbc</h1>

<p>
<b>opendbc is a Python API for your car.</b>
<br>
Read the speed, steering angle, and more. Send gas, braking, and steering commands.
Control the gas, brake, steering, and more. Read the speed, steering angle, and more.
</p>

<h3>
Expand All @@ -25,9 +24,72 @@
---

Most cars since 2016 have electronically-actuatable steering, gas, and brakes thanks to [LKAS](https://en.wikipedia.org/wiki/Lane_departure_warning_system#Lane_keeping_and_next_technologies) and [ACC](https://en.wikipedia.org/wiki/Adaptive_cruise_control).
The goal of this project is to support controlling every single one of those cars. See [docs/CARS.md](docs/CARS.md) for the current supported cars list.
The goal of this project is to support controlling the steering, gas, and brakes on every single one of those cars.

While the primary focus is on supporting ADAS interfaces for [openpilot](https://github.com/commaai/openpilot), we're also interested in reading and writing as many things as we can (EV charge status, lock/unlocking doors, etc) such that we can build the best vehicle management app ever.

---

This README and the [supported cars list](docs/CARS.md) are all the docs for the opendbc project.
Everything you need to know to use, contribute, and extend opendbc are in these docs.

## Quick start

```bash
git clone https://github.com/commaai/opendbc.git
cd opendbc

pip3 install -e .[testing,docs] # install dependencies
scons -j8 # build with 8 cores
pytest . # run the tests
pre-commit run --all-files # run the linter
./test.sh # all-in-one for setup, build, lint, and test
```

[`examples/`](examples/) contains small example programs that can read state from the car and control the steering, gas, and brakes.
[`examples/joystick.py`](examples/joystick.py) allows you to control a car with a joystick.

### Project Structure
* [`opendbc/dbc/`](opendbc/dbc/) is a repository of [DBC](https://en.wikipedia.org/wiki/CAN_bus#DBC) files
* [`opendbc/can/`](opendbc/can/) is a library for parsing and building CAN messages from DBC files
* [`opendbc/car/`](opendbc/car/) is a high-level library for interfacing with cars using Python

## How to Port a Car

This guide covers everything from adding support to a new car all the way to improving existing cars (e.g. adding longitudinal control or radar parsing). If similar cars to yours are already compatible, most of this work is likely already done for you.

At its most basic, a car port will control the steering on a car. A "complete" car port will have all of: lateral control, longitudinal control, good tuning for both lateral and longitudinal, radar parsing (if equipped), fuzzy fingerprinting, and more. The new car support docs will clearly communicate each car's support level.

### Connect to the Car

The first step is to get connected to the car with a comma 3X and a car harness.
The car harness gets you connected to two different CAN buses and splits one of those buses to send our own actuation messages.

If you're lucky, a harness compatible with your car will already be designed and sold on comma.ai/shop.
If you're not so lucky, start with a "developer harness" from comma.ai/shop and crimp on whatever connector you need.

### Structure of a port

Depending on , most of this basic structure will already be in place.

While the primary focus is on supporting ADAS interfaces for [openpilot](https://github.com/commaai/openpilot), we're also interested in reading and writing as many things as we can: EV charge status, lock/unlocking doors, etc.
The entirery of a car port lives in `opendbc/car/<brand>/`:
* `carstate.py`: parses out the relevant information from the CAN stream using the car's DBC file
* `carcontroller.py`: outputs CAN messages to control the car
* `<brand>can.py`: thin Python helpers around the DBC file to build CAN messages
* `fingerprints.py`: database of ECU firmware versions for identifying car models
* `interface.py`: high level class for interfacing with the car
* `radar_interface.py`: parses out the radar
* `values.py`: enumerates the brand's supported cars

### Reverse Engineer CAN messages

Start off by recording a route with lots of interesting events: enable LKAS and ACC, turn the steering wheel both extremes, etc. Then, load up that route in [cabana](https://github.com/commaai/openpilot/tree/master/tools/cabana).

### Tuning

#### Longitudinal

Use the [longitudinal maneuvers](https://github.com/commaai/openpilot/tree/master/tools/longitudinal_maneuvers) report to evaluate your car's longitudinal control and tune it.

## Contributing

Expand Down Expand Up @@ -59,66 +121,48 @@ Every car port is eligible for a bounty:

In addition to the standard bounties, we also offer higher value bounties for more popular cars. See those at [comma.ai/bounties](comma.ai/bounties).

### Project Structure
* [`opendbc/dbc/`](opendbc/dbc/) is a repository of [DBC](https://en.wikipedia.org/wiki/CAN_bus#DBC) files
* [`opendbc/can/`](opendbc/can/) is a library for parsing and building CAN messages from DBC files
* [`opendbc/car/`](opendbc/car/) is a high-level library for interfacing with cars using Python

### Quick start

```bash
git clone https://github.com/commaai/opendbc.git

cd opendbc

# Install the dependencies
pip3 install -e .[testing,docs]

# Build
scons -j8

# Run the tests
pytest .

# Run the linter
pre-commit run --all-files

# ./test.sh is the all-in-one that will install deps, build, lint, and test
./test.sh
```

[`examples/`](examples/) contains small example programs that can read state from the car and control the steering, gas, and brakes.
[`examples/joystick.py`](examples/joystick.py) allows you to control a car with a joystick.

## FAQ

***How do I use this?***

Depends on what you want to do. [openpilot](https://github.com/commaai/openpilot) is our development target and offers a replacement for your car's ADAS, but you can also use a [panda](https://comma.ai/shop/panda) for basic control or just reading state from your car.

***What hardware do I need?***
***How do I use this?*** A [comma 3X](https://comma.ai/shop/comma-3x) is custom-designed to be the best way to run and develop opendbc and openpilot.

* a [supported car](docs/CARS.md)
* a [car harness](https://comma.ai/shop/car-harness)
* a [comma 3X](https://comma.ai/shop/comma-3x) or [panda](https://comma.ai/shop/panda)
***Which cars are supported?*** See the [supported cars list](docs/CARS.md).

***Which cars are supported?***
***Can I add support for my car?*** Yes, most car support comes from the community. Read the guide [here](https://github.com/commaai/opendbc/blob/docs/README.md#how-to-port-a-car).

See the [supported cars list](docs/CARS.md).
***Which cars can be supported?*** Any car with LKAS and ACC. More info [here](https://github.com/commaai/openpilot/blob/master/docs/CARS.md#dont-see-your-car-here).

***Can I add support for my car?***
***How does this work?*** In short, we designed hardware to replace your car's built-in lane keep and adaptive cruise features. See [this talk](https://www.youtube.com/watch?v=FL8CxUSfipM) for an in-depth explanation.

Yes, most car support comes from the community. Join the [Discord](https://discord.comma.ai) and watch this [talk](https://www.youtube.com/watch?v=XxPS5TpTUnI&t=142s&pp=ygUPY29tbWFfY29uIGphc29u) to get started. (We also offer [paid bounties](https://comma.ai/bounties) on car ports.)
***Is there a timeline or roadmap for adding car support?*** No, most car support comes from the community, with comma doing final safety and quality validation. The more complete the community car port is and the more popular the car is, the more likely we are to pick it up as the next one to validate.

***Which cars can be supported?***
### Terms

Any car with LKAS and ACC. More info [here](https://github.com/commaai/openpilot/blob/master/docs/CARS.md#dont-see-your-car-here).
* **port**: refers to the integration and support of a specific car
* **lateral control**: aka steering control
* **longitudinal control**: aka gas/brakes control
* **fingerprinting**: automatic process for identifying the car
* **[LKAS](https://en.wikipedia.org/wiki/Lane_departure_warning_system)**: lane keeping assist
* **[ACC](https://en.wikipedia.org/wiki/Adaptive_cruise_control)**: adaptive cruise control
* **[harness](https://comma.ai/shop/car-harness)**: car-specific hardware to attach to the car and intercept the ADAS messages
* **[panda](https://github.com/commaai/panda)**: hardware used to get on a car's CAN bus
* **[ECU](https://en.wikipedia.org/wiki/Electronic_control_unit)**: computers or control modules inside the car
* **[CAN bus](https://en.wikipedia.org/wiki/CAN_bus)**: a bus that connects the ECUs in a car
* **[cabana](https://github.com/commaai/openpilot/tree/master/tools/cabana#readme)**: our tool for reverse engineering CAN messages
* **[DBC file](https://en.wikipedia.org/wiki/CAN_bus#DBC)**: contains definitions for messages on a CAN bus
* **[openpilot](https://github.com/commaai/openpilot)**: an ADAS system for cars supported by opendbc
* **[comma](https://github.com/commaai)**: the company behind opendbc
* **[comma 3X](https://comma.ai/shop/comma-3x)**: the hardware used to run openpilot

***How does this work?***
### More resources

In short, we designed hardware to sit at a convenient spot on your car's communication bus that allows for filtering out interesting messages and replacing them with our own.
See [this talk](https://www.youtube.com/watch?v=FL8CxUSfipM) for an in-depth explanation.
* [*How Do We Control The Car?*](https://www.youtube.com/watch?v=nNU6ipme878&pp=ygUoY29tbWEgY29uIDIwMjEgaG93IGRvIHdlIGNvbnRyb2wgdGhlIGNhcg%3D%3D) by [@robbederks](https://github.com/robbederks) from COMMA_CON 2021
* [*How to Port a Car*](https://www.youtube.com/watch?v=XxPS5TpTUnI&t=142s&pp=ygUPamFzb24gY29tbWEgY29u) by [@jyoung8607](https://github.com/jyoung8607) from COMMA_CON 2023
* [commaCarSegments](https://huggingface.co/datasets/commaai/commaCarSegments): a massive dataset of CAN data from 300 different car models
* [cabana](https://github.com/commaai/openpilot/tree/master/tools/cabana#readme): our tool for reverse engineering CAN messages
* [can_print_changes.py](https://github.com/commaai/openpilot/blob/master/selfdrive/debug/can_print_changes.py): diff the whole CAN bus across two drives, such as one without any LKAS and one with LKAS
* [longitudinal maneuvers](https://github.com/commaai/openpilot/tree/master/tools/longitudinal_maneuvers): a tool for evaluating and tuning longitudinal control
* [opendbc data](https://commaai.github.io/opendbc-data/): a repository of longitudinal maneuver evaluations

## Come work with us -- [comma.ai/jobs](https://comma.ai/jobs)

comma is hiring engineers to work on opendbc and [openpilot](https://github.com/commaai/openpilot). We love contributors.
comma is hiring engineers to work on opendbc and [openpilot](https://github.com/commaai/openpilot). We love hiring contributors.
1 change: 0 additions & 1 deletion opendbc/can/logger.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#ifdef SWAGLOG
// cppcheck-suppress preprocessorErrorDirective
#include SWAGLOG
#else

Expand Down
3 changes: 3 additions & 0 deletions opendbc/car/chrysler/fingerprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@
b'68320950AL',
b'68320950AM',
b'68454268AB',
b'68454268AC',
b'68475160AE',
b'68475160AF',
b'68475160AG',
Expand Down Expand Up @@ -577,6 +578,7 @@
b'68448165AK',
b'68455111AC ',
b'68455119AC ',
b'68455137AC ',
b'68455145AC ',
b'68455145AE ',
b'68455146AC ',
Expand Down Expand Up @@ -631,6 +633,7 @@
b'68384328AD',
b'68384332AD',
b'68445531AC',
b'68445532AB',
b'68445533AB',
b'68445536AB',
b'68445537AB',
Expand Down
1 change: 1 addition & 0 deletions opendbc/car/ford/fingerprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
],
(Ecu.abs, 0x760, None): [
b'NZ6C-2D053-AE\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
b'NZ6C-2D053-AF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
b'NZ6C-2D053-AG\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
b'PZ6C-2D053-ED\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
b'PZ6C-2D053-EE\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
Expand Down
2 changes: 1 addition & 1 deletion opendbc/car/mazda/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class CAR(Platforms):
MAZDA_CX9.specs
)
MAZDA_CX5_2022 = MazdaPlatformConfig(
[MazdaCarDocs("Mazda CX-5 2022-24")],
[MazdaCarDocs("Mazda CX-5 2022-25")],
MAZDA_CX5.specs,
)

Expand Down
62 changes: 55 additions & 7 deletions opendbc/car/toyota/carcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
make_tester_present_msg, rate_limit, structs, ACCELERATION_DUE_TO_GRAVITY, DT_CTRL
from opendbc.car.can_definitions import CanData
from opendbc.car.common.filter_simple import FirstOrderFilter
from opendbc.car.common.numpy_fast import clip
from opendbc.car.common.numpy_fast import clip, interp
from opendbc.car.common.pid import PIDController
from opendbc.car.secoc import add_mac, build_sync_mac
from opendbc.car.interfaces import CarControllerBase
Expand Down Expand Up @@ -37,6 +37,26 @@
MAX_LTA_DRIVER_TORQUE_ALLOWANCE = 150 # slightly above steering pressed allows some resistance when changing lanes


def get_long_tune(CP, params):
kiBP = [0.]
kdBP = [0.]
kdV = [0.]
if CP.carFingerprint in TSS2_CAR:
kiV = [0.5]
kdV = [0.25 / 4]

# Since we compensate for imprecise acceleration in carcontroller and error correct on aEgo, we can avoid using gains
if CP.flags & ToyotaFlags.RAISED_ACCEL_LIMIT:
kiV = [0.0]
else:
kiBP = [0., 5., 35.]
kiV = [3.6, 2.4, 1.5]

return PIDController(0.0, (kiBP, kiV), k_f=1.0, k_d=(kdBP, kdV),
pos_limit=params.ACCEL_MAX, neg_limit=params.ACCEL_MIN,
rate=1 / (DT_CTRL * 3))


class CarController(CarControllerBase):
def __init__(self, dbc_names, CP):
super().__init__(dbc_names, CP)
Expand All @@ -50,9 +70,16 @@ def __init__(self, dbc_names, CP):
self.steer_rate_counter = 0
self.distance_button = 0

self.long_pid = get_long_tune(self.CP, self.params)

self.error_rate = FirstOrderFilter(0.0, 0.5, DT_CTRL * 3)
self.prev_error = 0.0

# *** start PCM compensation state ***
self.pitch = FirstOrderFilter(0, 0.5, DT_CTRL)

self.accel_pid = PIDController(2.0, 0.5, 1 / (DT_CTRL * 3))
# FIXME: rate isn't set properly
self.pcm_pid = PIDController(2.0, 0.5, 1 / (DT_CTRL * 3))
self.pcm_accel_compensation = FirstOrderFilter(0, 0.5, DT_CTRL * 3)

# the PCM's reported acceleration request can sometimes mismatch aEgo, close the loop
Expand All @@ -66,6 +93,7 @@ def __init__(self, dbc_names, CP):
if not any(fw.ecu == Ecu.hybrid for fw in self.CP.carFw):
self.pcm_accel_net.update_alpha(self.CP.longitudinalActuatorDelay + 0.2)
self.net_acceleration_request.update_alpha(self.CP.longitudinalActuatorDelay + 0.2)
# *** end PCM compensation state ***

self.packer = CANPacker(dbc_names[Bus.pt])
self.accel = 0
Expand Down Expand Up @@ -222,11 +250,11 @@ def update(self, CC, CS, now_nanos):
pcm_accel_compensation = 0.0
if not stopping:
# prevent compensation windup
self.accel_pid.neg_limit = pcm_accel_cmd - self.params.ACCEL_MAX
self.accel_pid.pos_limit = pcm_accel_cmd - self.params.ACCEL_MIN
pcm_accel_compensation = self.accel_pid.update(new_pcm_accel_net - self.net_acceleration_request.x)
self.pcm_pid.neg_limit = pcm_accel_cmd - self.params.ACCEL_MAX
self.pcm_pid.pos_limit = pcm_accel_cmd - self.params.ACCEL_MIN
pcm_accel_compensation = self.pcm_pid.update(new_pcm_accel_net - self.net_acceleration_request.x)
else:
self.accel_pid.reset()
self.pcm_pid.reset()

pcm_accel_cmd = pcm_accel_cmd - self.pcm_accel_compensation.update(pcm_accel_compensation)

Expand All @@ -235,9 +263,29 @@ def update(self, CC, CS, now_nanos):
self.pcm_accel_net_offset.x = 0.0
self.net_acceleration_request.x = 0.0
self.pcm_accel_net.x = CS.pcm_accel_net
self.accel_pid.reset()
self.pcm_pid.reset()
self.permit_braking = True

if not (self.CP.flags & ToyotaFlags.RAISED_ACCEL_LIMIT):
if actuators.longControlState == LongCtrlState.pid:
# GVC does not overshoot ego acceleration when starting from stop, but still has a similar delay
if not self.CP.flags & ToyotaFlags.SECOC.value:
a_ego_blended = interp(CS.out.vEgo, [1.0, 2.0], [CS.gvc, CS.out.aEgo])
else:
a_ego_blended = CS.out.aEgo

error = pcm_accel_cmd - a_ego_blended
self.error_rate.update((error - self.prev_error) / (DT_CTRL * 3))
self.prev_error = error

pcm_accel_cmd = self.long_pid.update(error, error_rate=self.error_rate.x,
speed=CS.out.vEgo,
feedforward=pcm_accel_cmd)
else:
self.long_pid.reset()
self.error_rate.x = 0.0
self.prev_error = 0.0

# Along with rate limiting positive jerk above, this greatly improves gas response time
# Consider the net acceleration request that the PCM should be applying (pitch included)
net_acceleration_request_min = min(actuators.accel + accel_due_to_pitch, net_acceleration_request)
Expand Down
Loading

0 comments on commit 354c3bc

Please sign in to comment.