carstate.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from cereal import car
  2. from opendbc.can.parser import CANParser
  3. from openpilot.selfdrive.car.interfaces import CarStateBase
  4. from openpilot.selfdrive.car.body.values import DBC
  5. STARTUP_TICKS = 100
  6. class CarState(CarStateBase):
  7. def update(self, cp):
  8. ret = car.CarState.new_message()
  9. ret.wheelSpeeds.fl = cp.vl['MOTORS_DATA']['SPEED_L']
  10. ret.wheelSpeeds.fr = cp.vl['MOTORS_DATA']['SPEED_R']
  11. ret.vEgoRaw = ((ret.wheelSpeeds.fl + ret.wheelSpeeds.fr) / 2.) * self.CP.wheelSpeedFactor
  12. ret.vEgo, ret.aEgo = self.update_speed_kf(ret.vEgoRaw)
  13. ret.standstill = False
  14. ret.steerFaultPermanent = any([cp.vl['VAR_VALUES']['MOTOR_ERR_L'], cp.vl['VAR_VALUES']['MOTOR_ERR_R'],
  15. cp.vl['VAR_VALUES']['FAULT']])
  16. ret.charging = cp.vl["BODY_DATA"]["CHARGER_CONNECTED"] == 1
  17. ret.fuelGauge = cp.vl["BODY_DATA"]["BATT_PERCENTAGE"] / 100
  18. # irrelevant for non-car
  19. ret.gearShifter = car.CarState.GearShifter.drive
  20. ret.cruiseState.enabled = True
  21. ret.cruiseState.available = True
  22. return ret
  23. @staticmethod
  24. def get_can_parser(CP):
  25. messages = [
  26. ("MOTORS_DATA", 100),
  27. ("VAR_VALUES", 10),
  28. ("BODY_DATA", 1),
  29. ]
  30. return CANParser(DBC[CP.carFingerprint]["pt"], messages, 0)