forked from commaai/opendbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit 'ea87b4fa3ba98a71c2c8e93c97788fb0fa960d55' into odyssey_…
…bosch
- Loading branch information
Showing
35 changed files
with
13,153 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import struct | ||
|
||
from Crypto.Hash import CMAC | ||
from Crypto.Cipher import AES | ||
|
||
def add_mac(key, trip_cnt, reset_cnt, msg_cnt, msg): | ||
# TODO: clean up conversion to and from hex | ||
|
||
addr, payload, bus = msg | ||
reset_flag = reset_cnt & 0b11 | ||
msg_cnt_flag = msg_cnt & 0b11 | ||
payload = payload[:4] | ||
|
||
# Step 1: Build Freshness Value (48 bits) | ||
# [Trip Counter (16 bit)][[Reset Counter (20 bit)][Message Counter (8 bit)][Reset Flag (2 bit)][Padding (2 bit)] | ||
freshness_value = struct.pack('>HI', trip_cnt, (reset_cnt << 12) | ((msg_cnt & 0xff) << 4) | (reset_flag << 2)) | ||
|
||
# Step 2: Build data to authenticate (96 bits) | ||
# [Message ID (16 bits)][Payload (32 bits)][Freshness Value (48 bits)] | ||
to_auth = struct.pack('>H', addr) + payload + freshness_value | ||
|
||
# Step 3: Calculate CMAC (28 bit) | ||
cmac = CMAC.new(key, ciphermod=AES) | ||
cmac.update(to_auth) | ||
mac = cmac.digest().hex()[:7] # truncated MAC | ||
|
||
# Step 4: Build message | ||
# [Payload (32 bit)][Message Counter Flag (2 bit)][Reset Flag (2 bit)][Authenticator (28 bit)] | ||
msg_cnt_rst_flag = struct.pack('>B', (msg_cnt_flag << 2) | reset_flag).hex()[1] | ||
msg = payload.hex() + msg_cnt_rst_flag + mac | ||
payload = bytes.fromhex(msg) | ||
|
||
return (addr, payload, bus) | ||
|
||
def build_sync_mac(key, trip_cnt, reset_cnt, id_=0xf): | ||
id_ = struct.pack('>H', id_) # 16 | ||
trip_cnt = struct.pack('>H', trip_cnt) # 16 | ||
reset_cnt = struct.pack('>I', reset_cnt << 12)[:-1] # 20 + 4 padding | ||
|
||
to_auth = id_ + trip_cnt + reset_cnt # SecOC 11.4.1.1 page 138 | ||
|
||
cmac = CMAC.new(key, ciphermod=AES) | ||
cmac.update(to_auth) | ||
|
||
msg = "0" + cmac.digest().hex()[:7] | ||
msg = bytes.fromhex(msg) | ||
return struct.unpack('>I', msg)[0] |
This file contains 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
This file contains 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
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import copy | ||
from opendbc.car.common.numpy_fast import clip | ||
from opendbc.can.packer import CANPacker | ||
from opendbc.car import apply_std_steer_angle_limits | ||
from opendbc.car.interfaces import CarControllerBase | ||
from opendbc.car.tesla.teslacan import TeslaCAN | ||
from opendbc.car.tesla.values import CarControllerParams | ||
|
||
|
||
class CarController(CarControllerBase): | ||
def __init__(self, dbc_name, CP): | ||
self.CP = CP | ||
self.frame = 0 | ||
self.apply_angle_last = 0 | ||
self.packer = CANPacker(dbc_name) | ||
self.tesla_can = TeslaCAN(self.packer) | ||
|
||
def update(self, CC, CS, now_nanos): | ||
|
||
actuators = CC.actuators | ||
can_sends = [] | ||
|
||
# Disengage and allow for user override | ||
hands_on_fault = CS.steer_warning == "EAC_ERROR_HANDS_ON" and CS.hands_on_level >= 3 | ||
lkas_enabled = CC.latActive and not hands_on_fault | ||
|
||
if self.frame % 2 == 0: | ||
if lkas_enabled: | ||
# Angular rate limit based on speed | ||
apply_angle = apply_std_steer_angle_limits(actuators.steeringAngleDeg, self.apply_angle_last, CS.out.vEgo, CarControllerParams) | ||
|
||
# To not fault the EPS | ||
apply_angle = clip(apply_angle, CS.out.steeringAngleDeg - 20, CS.out.steeringAngleDeg + 20) | ||
else: | ||
apply_angle = CS.out.steeringAngleDeg | ||
|
||
self.apply_angle_last = apply_angle | ||
can_sends.append(self.tesla_can.create_steering_control(apply_angle, lkas_enabled, (self.frame // 2) % 16)) | ||
|
||
# Longitudinal control | ||
if self.CP.openpilotLongitudinalControl and self.frame % 4 == 0: | ||
state = CS.das_control["DAS_accState"] | ||
if hands_on_fault: | ||
state = 13 # "ACC_CANCEL_GENERIC_SILENT" | ||
accel = clip(actuators.accel, CarControllerParams.ACCEL_MIN, CarControllerParams.ACCEL_MAX) | ||
cntr = (self.frame // 4) % 8 | ||
can_sends.append(self.tesla_can.create_longitudinal_command(state, accel, cntr)) | ||
|
||
# Increment counter so cancel is prioritized even without openpilot longitudinal | ||
if hands_on_fault and not self.CP.openpilotLongitudinalControl: | ||
cntr = (CS.das_control["DAS_controlCounter"] + 1) % 8 | ||
can_sends.append(self.tesla_can.create_longitudinal_command(13, 0, cntr)) | ||
|
||
# TODO: HUD control | ||
new_actuators = copy.copy(actuators) | ||
new_actuators.steeringAngleDeg = self.apply_angle_last | ||
|
||
self.frame += 1 | ||
return new_actuators, can_sends |
Oops, something went wrong.