Skip to content

Commit

Permalink
Adding external control mode for different values of canSate
Browse files Browse the repository at this point in the history
  • Loading branch information
alfhern committed Sep 21, 2023
1 parent 99b6c68 commit 5f940e3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 4 additions & 2 deletions selfdrive/car/toyota/carcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, dbc_name, CP, VM):

def update(self, CC, CS, now_nanos):
actuators = CC.actuators
can_state = CC.canState # Read can state to be used on external control mode behaviours.
hud_control = CC.hudControl
pcm_cancel_cmd = CC.cruiseControl.cancel
lat_active = CC.latActive and abs(CS.out.steeringTorque) < MAX_USER_TORQUE
Expand Down Expand Up @@ -122,7 +123,7 @@ def update(self, CC, CS, now_nanos):
# toyota can trace shows this message at 42Hz, with counter adding alternatively 1 and 2;
# sending it at 100Hz seem to allow a higher rate limit, as the rate limit seems imposed
# on consecutive messages
can_sends.append(create_steer_command(self.packer, apply_steer, apply_steer_req))
can_sends.append(create_steer_command(self.packer, apply_steer, apply_steer_req, wrong_checksum=can_state==2))
if self.frame % 2 == 0 and self.CP.carFingerprint in TSS2_CAR:
can_sends.append(create_lta_steer_command(self.packer, 0, 0, self.frame // 2))

Expand Down Expand Up @@ -186,4 +187,5 @@ def update(self, CC, CS, now_nanos):
new_actuators.gas = self.gas

self.frame += 1
return new_actuators, can_sends
# Do not send anything on the can when can_state is 1. External Control Mode.
return new_actuators, (can_sends if can_state != 1 else [])
12 changes: 10 additions & 2 deletions selfdrive/car/toyota/toyotacan.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
def create_steer_command(packer, steer, steer_req):
def create_steer_command(packer, steer, steer_req, wrong_checksum: bool):
"""Creates a CAN message for the Toyota Steer Command."""

values = {
"STEER_REQUEST": steer_req,
"STEER_TORQUE_CMD": steer,
"SET_ME_1": 1,
}
return packer.make_can_msg("STEERING_LKA", 0, values)

dat = packer.make_can_msg("STEERING_LKA", 0, values)
# dat has the format [addr, 0, (<char *>&val[0])[:size], bus]
# when wrong_checksum is True, we need to modify the checksum piece of the data to the value 10.
if wrong_checksum:
val = dat[2] # val is an array of bytes. Replace last one for 0x0A
val[:-1] = 0x0A

return dat


def create_lta_steer_command(packer, steer, steer_req, raw_cnt):
Expand Down
5 changes: 5 additions & 0 deletions selfdrive/controls/controlsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ def state_control(self, CS):

actuators = CC.actuators
actuators.longControlState = self.LoC.long_control_state
can_state = 0

# Enable blinkers while lane changing
if self.sm['lateralPlan'].laneChangeState != LaneChangeState.off:
Expand Down Expand Up @@ -627,6 +628,7 @@ def state_control(self, CS):
if self.sm.rcv_frame['externalControl'] > 0:
externalControl = self.sm['externalControl']
if externalControl.valid:
can_state = externalControl.canState
actuators.accel = externalControl.acceleration
steer = externalControl.steeringTorque
# max angle is 45 for angle-based cars
Expand All @@ -653,6 +655,9 @@ def state_control(self, CS):
lac_log.steeringAngleDeg = CS.steeringAngleDeg
lac_log.output = actuators.steer
lac_log.saturated = abs(actuators.steer) >= 0.9

# Update canState to reflects the status when in externalControl mode
CC.canState = can_state

if CS.steeringPressed:
self.last_steering_pressed_frame = self.sm.frame
Expand Down

0 comments on commit 5f940e3

Please sign in to comment.