Continuously update offset between TSS2 angle sensors (#21943)

* Continuously update offset between TSS2 angle sensors

* add comment

* less lines

* only when initialized

* init to None

* update ref
old-commit-hash: c15a616ac4
This commit is contained in:
Willem Melching 2021-09-01 15:00:52 -07:00 committed by GitHub
parent 503fe7421a
commit 2e285bea20
3 changed files with 21 additions and 14 deletions

View File

@ -1,13 +1,18 @@
class FirstOrderFilter:
# first order filter
def __init__(self, x0, rc, dt):
def __init__(self, x0, rc, dt, initialized=True):
self.x = x0
self.dt = dt
self.update_alpha(rc)
self.initialized = initialized
def update_alpha(self, rc):
self.alpha = self.dt / (rc + self.dt)
def update(self, x):
self.x = (1. - self.alpha) * self.x + self.alpha * x
if self.initialized:
self.x = (1. - self.alpha) * self.x + self.alpha * x
else:
self.initialized = True
self.x = x
return self.x

View File

@ -1,5 +1,7 @@
from cereal import car
from common.numpy_fast import mean
from common.filter_simple import FirstOrderFilter
from common.realtime import DT_CTRL
from opendbc.can.can_define import CANDefine
from selfdrive.car.interfaces import CarStateBase
from opendbc.can.parser import CANParser
@ -18,7 +20,7 @@ class CarState(CarStateBase):
# Need to apply an offset as soon as the steering angle measurements are both received
self.needs_angle_offset = True
self.accurate_steer_angle_seen = False
self.angle_offset = 0.
self.angle_offset = FirstOrderFilter(None, 60.0, DT_CTRL, initialized=False)
self.low_speed_lockout = False
self.acc_type = 1
@ -47,21 +49,21 @@ class CarState(CarStateBase):
ret.standstill = ret.vEgoRaw < 0.001
ret.steeringAngleDeg = cp.vl["STEER_ANGLE_SENSOR"]["STEER_ANGLE"] + cp.vl["STEER_ANGLE_SENSOR"]["STEER_FRACTION"]
torque_sensor_angle_deg = cp.vl["STEER_TORQUE_SENSOR"]["STEER_ANGLE"]
# Some newer models have a more accurate angle measurement in the TORQUE_SENSOR message. Use if non-zero
if abs(cp.vl["STEER_TORQUE_SENSOR"]["STEER_ANGLE"]) > 1e-3:
if abs(torque_sensor_angle_deg) > 1e-3:
self.accurate_steer_angle_seen = True
if self.accurate_steer_angle_seen:
ret.steeringAngleDeg = cp.vl["STEER_TORQUE_SENSOR"]["STEER_ANGLE"] - self.angle_offset
ret.steeringAngleOffsetDeg = self.angle_offset
# Offset seems to be invalid for large steering angles
if abs(ret.steeringAngleDeg) < 90:
self.angle_offset.update(torque_sensor_angle_deg - ret.steeringAngleDeg)
if self.needs_angle_offset:
angle_wheel = cp.vl["STEER_ANGLE_SENSOR"]["STEER_ANGLE"] + cp.vl["STEER_ANGLE_SENSOR"]["STEER_FRACTION"]
if abs(angle_wheel) > 1e-3:
self.needs_angle_offset = False
self.angle_offset = ret.steeringAngleDeg - angle_wheel
else:
ret.steeringAngleDeg = cp.vl["STEER_ANGLE_SENSOR"]["STEER_ANGLE"] + cp.vl["STEER_ANGLE_SENSOR"]["STEER_FRACTION"]
if self.angle_offset.initialized:
ret.steeringAngleOffsetDeg = self.angle_offset.x
ret.steeringAngleDeg = torque_sensor_angle_deg - self.angle_offset.x
ret.steeringRateDeg = cp.vl["STEER_ANGLE_SENSOR"]["STEER_RATE"]

View File

@ -1 +1 @@
f3e220d82db802c653777634228ce57576ac7aad
3e8037d39f40c59a2b9e86539e0952645b2cb3ea