Skip to content

Commit

Permalink
Saving P,I,F parameters from pid controller (#116)
Browse files Browse the repository at this point in the history
Added separate save/load functions, try catch - and saving the right constants to the params file.
  • Loading branch information
tb205gti authored and BogGyver committed Nov 5, 2019
1 parent 44e6cde commit 9c4b0fa
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion selfdrive/car/tesla/PCC_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,31 @@ def __init__(self,carcontroller):
self.params = Params()
self.average_speed_over_x_suggestions = 3 #10x a second
self.maxsuggestedspeed_avg = MovingAverage(self.average_speed_over_x_suggestions)

def load_pid(self):
try:
v_pid_json = open(V_PID_FILE)
data = json.load(v_pid_json)
if (self.LoC):
self.LoC.pid.p = data['p']
self.LoC.pid.i = data['i']
self.LoC.pid.f = data['f']
else:
print("self.LoC not initialized!")
except IOError:
print("file not present, creating at next reset")

#Helper function for saving the PCC pid constants across drives
def save_pid(self, pid):
data = {}
data['p'] = pid.p
data['i'] = pid.i
data['f'] = pid.f
try:
with open(V_PID_FILE , 'w') as outfile :
json.dump(data, outfile)
except IOError:
print("PDD pid parameters could not be saved to file")

def max_v_by_speed_limit(self,pedal_set_speed_ms ,speed_limit_ms, CS):
# if more than 10 kph / 2.78 ms, consider we have speed limit
Expand All @@ -195,6 +220,9 @@ def max_v_by_speed_limit(self,pedal_set_speed_ms ,speed_limit_ms, CS):
return pedal_set_speed_ms

def reset(self, v_pid):
#save the pid parameters to params file
self.save_pid(self.LoC.pid)

if self.LoC and RESET_PID_ON_DISENGAGE:
self.LoC.reset(v_pid)

Expand Down Expand Up @@ -378,7 +406,12 @@ def update_pdl(self, enabled, CS, frame, actuators, pcm_speed, speed_limit_ms, s
# how much accel and break we have to do
####################################################################
if PCCModes.is_selected(FollowMode(), CS.cstm_btns):
self.v_pid = self.calc_follow_speed_ms(CS,alca_enabled)
# Get v_id from the stored file, only the first activation where v_pid eq 0
if (not RESET_PID_ON_DISENGAGE and self.v_pid == 0.):
self.load_pid()
else:
self.v_pid = self.calc_follow_speed_ms(CS,alca_enabled)

if mapd is not None:
v_curve = max_v_in_mapped_curve_ms(mapd.liveMapData, self.pedal_speed_kph)
if v_curve:
Expand Down

1 comment on commit 9c4b0fa

@DavidWAbrahams
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crashes due to missing constant V_PID_FILE

Please sign in to comment.