Skip to content

Commit

Permalink
car/interfaces.py: optimize parse_gear_shifter() with predefined di…
Browse files Browse the repository at this point in the history
…ctionary (#32592)

Optimize gear shifter parsing for improved performance
  • Loading branch information
deanlee authored Jun 6, 2024
1 parent 7cfd91b commit 91fd918
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions selfdrive/car/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
TORQUE_OVERRIDE_PATH = os.path.join(BASEDIR, 'selfdrive/car/torque_data/override.toml')
TORQUE_SUBSTITUTE_PATH = os.path.join(BASEDIR, 'selfdrive/car/torque_data/substitute.toml')

GEAR_SHIFTER_MAP: dict[str, car.CarState.GearShifter] = {
'P': GearShifter.park, 'PARK': GearShifter.park,
'R': GearShifter.reverse, 'REVERSE': GearShifter.reverse,
'N': GearShifter.neutral, 'NEUTRAL': GearShifter.neutral,
'E': GearShifter.eco, 'ECO': GearShifter.eco,
'T': GearShifter.manumatic, 'MANUAL': GearShifter.manumatic,
'D': GearShifter.drive, 'DRIVE': GearShifter.drive,
'S': GearShifter.sport, 'SPORT': GearShifter.sport,
'L': GearShifter.low, 'LOW': GearShifter.low,
'B': GearShifter.brake, 'BRAKE': GearShifter.brake,
}


class LatControlInputs(NamedTuple):
lateral_acceleration: float
Expand Down Expand Up @@ -424,19 +436,7 @@ def update_blinker_from_stalk(self, blinker_time: int, left_blinker_stalk: bool,
def parse_gear_shifter(gear: str | None) -> car.CarState.GearShifter:
if gear is None:
return GearShifter.unknown

d: dict[str, car.CarState.GearShifter] = {
'P': GearShifter.park, 'PARK': GearShifter.park,
'R': GearShifter.reverse, 'REVERSE': GearShifter.reverse,
'N': GearShifter.neutral, 'NEUTRAL': GearShifter.neutral,
'E': GearShifter.eco, 'ECO': GearShifter.eco,
'T': GearShifter.manumatic, 'MANUAL': GearShifter.manumatic,
'D': GearShifter.drive, 'DRIVE': GearShifter.drive,
'S': GearShifter.sport, 'SPORT': GearShifter.sport,
'L': GearShifter.low, 'LOW': GearShifter.low,
'B': GearShifter.brake, 'BRAKE': GearShifter.brake,
}
return d.get(gear.upper(), GearShifter.unknown)
return GEAR_SHIFTER_MAP.get(gear.upper(), GearShifter.unknown)

@staticmethod
def get_can_parser(CP):
Expand Down

0 comments on commit 91fd918

Please sign in to comment.