Skip to content

Commit

Permalink
Basic curvature learner based on @zorrobyte's work
Browse files Browse the repository at this point in the history
Works best when paired w/ `interface.py` changes and ZSS.  Without ZSS, `interface.py` changes will make it feel twitchy, so leaving that for the ZSS branches only.
  • Loading branch information
ErichMoraga authored Feb 22, 2020
1 parent 5f75728 commit b979bf5
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion selfdrive/controls/lib/pathplanner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import math
import numpy as np
from common.numpy_fast import clip
from common.realtime import sec_since_boot, DT_MDL
from selfdrive.swaglog import cloudlog
from selfdrive.controls.lib.lateral_mpc import libmpc_py
Expand All @@ -9,6 +11,8 @@
from common.params import Params
import cereal.messaging as messaging
from cereal import log
import os.path
import json

LaneChangeState = log.PathPlan.LaneChangeState
LaneChangeDirection = log.PathPlan.LaneChangeDirection
Expand Down Expand Up @@ -54,6 +58,12 @@ def __init__(self, CP):

self.setup_mpc()
self.solution_invalid_cnt = 0
self.frame = 0
if os.path.exists('/data/curvature.json'):
with open("/data/curvature.json", "r") as f:
self.curvature_offset_i = json.load(f)
else:
self.curvature_offset_i = 0.0
self.lane_change_enabled = Params().get('LaneChangeEnabled') == b'1'
self.lane_change_state = LaneChangeState.off
self.lane_change_direction = LaneChangeDirection.none
Expand Down Expand Up @@ -86,7 +96,7 @@ def update(self, sm, pm, CP, VM):
# Run MPC
self.angle_steers_des_prev = self.angle_steers_des_mpc
VM.update_params(sm['liveParameters'].stiffnessFactor, sm['liveParameters'].steerRatio)
curvature_factor = VM.curvature_factor(v_ego)
curvature_factor = VM.curvature_factor(v_ego) + self.curvature_offset_i

self.LP.parse_model(sm['model'])

Expand Down Expand Up @@ -151,6 +161,20 @@ def update(self, sm, pm, CP, VM):

self.LP.update_d_poly(v_ego)

if active and angle_steers - angle_offset > 0.5:
self.curvature_offset_i -= self.LP.d_poly[3] / 12000
#self.LP.d_poly[3] += self.curvature_offset_i
elif active and angle_steers - angle_offset < -0.5:
self.curvature_offset_i += self.LP.d_poly[3] / 12000

self.curvature_offset_i = clip(self.curvature_offset_i, -0.3, 0.3)
self.frame += 1
if self.frame == 12000: #every 2 mins
with open("/data/curvature.json", "w") as f:
json.dump(self.curvature_offset_i, f)
os.chmod("/data/curvature.json", 0o777)
self.frame = 0

# account for actuation delay
self.cur_state = calc_states_after_delay(self.cur_state, v_ego, angle_steers - angle_offset, curvature_factor, VM.sR, CP.steerActuatorDelay)

Expand Down

0 comments on commit b979bf5

Please sign in to comment.