import math
from cereal import log
from common . numpy_fast import interp
from selfdrive . controls . lib . latcontrol import LatControl , MIN_STEER_SPEED
from selfdrive . controls . lib . pid import PIDController
from selfdrive . controls . lib . vehicle_model import ACCELERATION_DUE_TO_GRAVITY
# At higher speeds (25+mph) we can assume:
# Lateral acceleration achieved by a specific car correlates to
# torque applied to the steering rack. It does not correlate to
# wheel slip, or to speed.
# This controller applies torque to achieve desired lateral
# accelerations. To compensate for the low speed effects we
# use a LOW_SPEED_FACTOR in the error. Additionally, there is
# friction in the steering wheel that needs to be overcome to
# move it at all, this is compensated for too.
class LatControlTorque ( LatControl ) :
def __init__ ( self , CP , CI ) :
super ( ) . __init__ ( CP , CI )
self . torque_params = CP . lateralTuning . torque
self . pid = PIDController ( self . torque_params . kp , self . torque_params . ki ,
k_f = self . torque_params . kf , pos_limit = self . steer_max , neg_limit = - self . steer_max )
self . torque_from_lateral_accel = CI . torque_from_lateral_accel ( )
self . use_steering_angle = self . torque_params . useSteeringAngle
self . steering_angle_deadzone_deg = self . torque_params . steeringAngleDeadzoneDeg
def update_live_torque_params ( self , latAccelFactor , latAccelOffset , friction ) :
self . torque_params . latAccelFactor = latAccelFactor
self . torque_params . latAccelOffset = latAccelOffset
self . torque_params . friction = friction
def update ( self , active , CS , VM , params , last_actuators , steer_limited , desired_curvature , desired_curvature_rate , llk ) :
pid_log = log . ControlsState . LateralTorqueState . new_message ( )
if CS . vEgo < MIN_STEER_SPEED or not active :
output_torque = 0.0
pid_log . active = False
else :
if self . use_steering_angle :
actual_curvature = - VM . calc_curvature ( math . radians ( CS . steeringAngleDeg - params . angleOffsetDeg ) , CS . vEgo , params . roll )
curvature_deadzone = abs ( VM . calc_curvature ( math . radians ( self . steering_angle_deadzone_deg ) , CS . vEgo , 0.0 ) )
else :
actual_curvature_vm = - VM . calc_curvature ( math . radians ( CS . steeringAngleDeg - params . angleOffsetDeg ) , CS . vEgo , params . roll )
actual_curvature_llk = llk . angularVelocityCalibrated . value [ 2 ] / CS . vEgo
actual_curvature = interp ( CS . vEgo , [ 2.0 , 5.0 ] , [ actual_curvature_vm , actual_curvature_llk ] )
curvature_deadzone = 0.0
desired_lateral_accel = desired_curvature * CS . vEgo * * 2
# desired rate is the desired rate of change in the setpoint, not the absolute desired curvature
Live torque (#25456)
* wip torqued
* add basic logic
* setup in manager
* check sanity and publish msg
* add first order filter to outputs
* wire up controlsd, and update gains
* rename intercept to offset
* add cloudlog, live values are not updated
* fix bugs, do not reset points for now
* fix crashes
* rename to main
* fix bugs, works offline
* fix float in cereal bug
* add latacc filter
* randomly choose points, approx for iid
* add variable decay
* local param to capnp instead of dict
* verify works in replay
* use torqued output in controlsd
* use in controlsd; use points from past routes
* controlsd bugfix
* filter before updating gains, needs to be replaced
* save all points to ensure smooth transition across routes, revert friction factor to 1.5
* add filters to prevent noisy low-speed data points; improve fit sanity
* add engaged buffer
* revert lat_acc thresh
* use paramsd realtime process config
* make latacc-to-torque generic, and overrideable
* move freq to 4Hz, avoid storing in np.array, don't publish points in the message
* float instead of np
* remove constant while storing pts
* rename slope, offset to lat_accet_factor, offset
* resolve issues
* use camelcase in all capnp params
* use camelcase everywhere
* reduce latacc threshold or sanity, add car_sane todo, save points properly
* add and check tag
* write param to disk at end of route
* remove args
* rebase op, cereal
* save on exit
* restore default handler
* cpu usage check
* add to process replay
* handle reset better, reduce unnecessary computation
* always publish raw values - useful for debug
* regen routes
* update refs
* checks on cache restore
* check tuning vals too
* clean that up
* reduce cpu usage
* reduce cpu usage by 75%
* cleanup
* optimize further
* handle reset condition better, don't put points in init, use only in corolla
* bump cereal after rebasing
* update refs
* Update common/params.cc
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
* remove unnecessary checks
* Update RELEASES.md
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
old-commit-hash: 4fa62f146426f76c9c1c2867d9729b33ec612b59
3 years ago
# desired_lateral_jerk = desired_curvature_rate * CS.vEgo ** 2
actual_lateral_accel = actual_curvature * CS . vEgo * * 2
lateral_accel_deadzone = curvature_deadzone * CS . vEgo * * 2
low_speed_factor = interp ( CS . vEgo , [ 0 , 10 , 20 ] , [ 500 , 500 , 200 ] )
setpoint = desired_lateral_accel + low_speed_factor * desired_curvature
measurement = actual_lateral_accel + low_speed_factor * actual_curvature
error = setpoint - measurement
gravity_adjusted_lateral_accel = desired_lateral_accel - params . roll * ACCELERATION_DUE_TO_GRAVITY
pid_log . error = self . torque_from_lateral_accel ( error , self . torque_params , error , lateral_accel_deadzone , friction_compensation = False )
ff = self . torque_from_lateral_accel ( gravity_adjusted_lateral_accel , self . torque_params , error , lateral_accel_deadzone , friction_compensation = True )
freeze_integrator = steer_limited or CS . steeringPressed or CS . vEgo < 5
output_torque = self . pid . update ( pid_log . error ,
feedforward = ff ,
speed = CS . vEgo ,
freeze_integrator = freeze_integrator )
pid_log . active = True
pid_log . p = self . pid . p
pid_log . i = self . pid . i
pid_log . d = self . pid . d
pid_log . f = self . pid . f
pid_log . output = - output_torque
pid_log . actualLateralAccel = actual_lateral_accel
pid_log . desiredLateralAccel = desired_lateral_accel
pid_log . saturated = self . _check_saturation ( self . steer_max - abs ( output_torque ) < 1e-3 , CS , steer_limited )
# TODO left is positive in this convention
return - output_torque , 0.0 , pid_log