text: #!/usr/bin/env python3
  from cereal import car, arne182
  from selfdrive.config import Conversions as CV
  from selfdrive.car.subaru.values import CAR
  from selfdrive.car import STD_CARGO_KG, scale_rot_inertia, scale_tire_stiffness, gen_empty_fingerprint
  from selfdrive.car.interfaces import CarInterfaceBase
  
  class CarInterface(CarInterfaceBase):
  
    @staticmethod
    def compute_gb(accel, speed):
      return float(accel) / 4.0
  
    @staticmethod
    def get_params(candidate, fingerprint=gen_empty_fingerprint(), has_relay=False, car_fw=[]):  # pylint: disable=dangerous-default-value
      ret = CarInterfaceBase.get_std_params(candidate, fingerprint, has_relay)
  
      ret.carName = "subaru"
      ret.radarOffCan = True
      ret.safetyModel = car.CarParams.SafetyModel.subaru
  
      # Subaru port is a community feature, since we don't own one to test
      ret.communityFeature = True
  
      # force openpilot to fake the stock camera, since car harness is not supported yet and old style giraffe (with switches)
      # was never released
      ret.enableCamera = True
  
      ret.steerRateCost = 0.7
      ret.steerLimitTimer = 0.4
  
      if candidate == CAR.ASCENT:
        ret.mass = 2031. + STD_CARGO_KG
        ret.wheelbase = 2.89
        ret.centerToFront = ret.wheelbase * 0.5
        ret.steerRatio = 13.5
        ret.steerActuatorDelay = 0.3   # end-to-end angle controller
        ret.lateralTuning.pid.kf = 0.00003
        ret.lateralTuning.pid.kiBP, ret.lateralTuning.pid.kpBP = [[0., 20.], [0., 20.]]
        ret.lateralTuning.pid.kpV, ret.lateralTuning.pid.kiV = [[0.0025, 0.1], [0.00025, 0.01]]
  
      if candidate == CAR.IMPREZA:
        ret.mass = 1568. + STD_CARGO_KG
        ret.wheelbase = 2.67
        ret.centerToFront = ret.wheelbase * 0.5
        ret.steerRatio = 15
        ret.steerActuatorDelay = 0.4   # end-to-end angle controller
        ret.lateralTuning.pid.kiBP, ret.lateralTuning.pid.kpBP, ret.lateralTuning.pid.kfBP = [[0., 20.], [0., 20.], [0.]]
        ret.lateralTuning.pid.kpV, ret.lateralTuning.pid.kiV, ret.lateralTuning.pid.kfV = [[0.2, 0.3], [0.02, 0.03], [0.00005]]
  
      if candidate == CAR.FORESTER:
        ret.mass = 1568. + STD_CARGO_KG
        ret.wheelbase = 2.67
        ret.centerToFront = ret.wheelbase * 0.5
        ret.steerRatio = 17           # learned, 14 stock
        ret.steerActuatorDelay = 0.1
        ret.lateralTuning.pid.kf = 0.000038
        ret.lateralTuning.pid.kiBP, ret.lateralTuning.pid.kpBP = [[0., 14., 23.], [0., 14., 23.]]
        ret.lateralTuning.pid.kpV, ret.lateralTuning.pid.kiV = [[0.01, 0.065, 0.2], [0.001, 0.015, 0.025]]
  
      # TODO: get actual value, for now starting with reasonable value for
      # civic and scaling by mass and wheelbase
      ret.rotationalInertia = scale_rot_inertia(ret.mass, ret.wheelbase)
  
      # TODO: start from empirically derived lateral slip stiffness for the civic and scale by
      # mass and CG position, so all cars will have approximately similar dyn behaviors
      ret.tireStiffnessFront, ret.tireStiffnessRear = scale_tire_stiffness(ret.mass, ret.wheelbase, ret.centerToFront)
  
      return ret
  
    # returns a car.CarState
    def update(self, c, can_strings):
      ret_arne182 = arne182.CarStateArne182.new_message()
      self.cp.update_strings(can_strings)
      self.cp_cam.update_strings(can_strings)
  
  
      ret = self.CS.update(self.cp, self.cp_cam)
  
      ret.canValid = self.cp.can_valid and self.cp_cam.can_valid
      ret.steeringRateLimited = self.CC.steer_rate_limited if self.CC is not None else False
      ret.yawRate = self.VM.yaw_rate(ret.steeringAngle * CV.DEG_TO_RAD, ret.vEgo)
  
      buttonEvents = []
      be = car.CarState.ButtonEvent.new_message()
      be.type = car.CarState.ButtonEvent.Type.accelCruise
      buttonEvents.append(be)
  
      events, events_arne182 = self.create_common_events(ret, extra_gears=[car.CarState.GearShifter.unknown])
      ret.events = events.to_msg()
      ret_arne182.events = events_arne182.to_msg()
      self.CS.out = ret.as_reader()
      return self.CS.out, ret_arne182.as_reader()
  
    def apply(self, c):
      can_sends = self.CC.update(c.enabled, self.CS, self.frame, c.actuators,
                                 c.cruiseControl.cancel, c.hudControl.visualAlert,
                                 c.hudControl.leftLaneVisible, c.hudControl.rightLaneVisible)
      self.frame += 1
      return can_sends
  