Skip to content

Commit

Permalink
Merge pull request #6 from arne182/066-clean
Browse files Browse the repository at this point in the history
066 clean
  • Loading branch information
sshane authored Dec 4, 2019
2 parents ecf1474 + a5b80cc commit 8239975
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 8 deletions.
105 changes: 98 additions & 7 deletions selfdrive/controls/controlsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ def data_sample(CI, CC, sm, can_sock, cal_status, cal_perc, overtemp, free_space
CS = CI.update(CC, can_strs)
if isinstance(CS, list): # todo: remove all this and make all interfaces return arne182 events
CS, CS_arne182 = CS[0], CS[1]
events = list(CS.events) + list(CS_arne182.events)
events = list(CS.events)
events_arne182 = list(CS_arne182.events)
else:
events = list(CS.events)
events_arne182 = []

enabled = isEnabled(state)

Expand Down Expand Up @@ -130,10 +132,10 @@ def data_sample(CI, CC, sm, can_sock, cal_status, cal_perc, overtemp, free_space
if driver_status.terminal_alert_cnt >= MAX_TERMINAL_ALERTS:
events.append(create_event("tooDistracted", [ET.NO_ENTRY]))

return CS, events, cal_status, cal_perc, overtemp, free_space, low_battery, mismatch_counter
return CS, events, cal_status, cal_perc, overtemp, free_space, low_battery, mismatch_counter, events_arne182


def state_transition(frame, CS, CP, state, events, soft_disable_timer, v_cruise_kph, AM):
def state_transition(frame, CS, CP, state, events, soft_disable_timer, v_cruise_kph, AM, events_arne182):
"""Compute conditional state transitions and execute actions on state transitions"""
enabled = isEnabled(state)

Expand Down Expand Up @@ -217,11 +219,79 @@ def state_transition(frame, CS, CP, state, events, soft_disable_timer, v_cruise_
elif not get_events(events, [ET.PRE_ENABLE]):
state = State.enabled

# DISABLED
if state == State.disabled:
if get_events(events_arne182, [ET.ENABLE]):
if get_events(events_arne182, [ET.NO_ENTRY]):
for e in get_events(events_arne182, [ET.NO_ENTRY]):
AM.add(frame, str(e) + "NoEntry", enabled)

else:
if get_events(events_arne182, [ET.PRE_ENABLE]):
state = State.preEnabled
else:
state = State.enabled
AM.add(frame, "enable", enabled)
v_cruise_kph = initialize_v_cruise(CS.vEgo, CS.buttonevents_arne182, v_cruise_kph_last)

# ENABLED
elif state == State.enabled:
if get_events(events_arne182, [ET.USER_DISABLE]):
state = State.disabled
AM.add(frame, "disable", enabled)

elif get_events(events_arne182, [ET.IMMEDIATE_DISABLE]):
state = State.disabled
for e in get_events(events_arne182, [ET.IMMEDIATE_DISABLE]):
AM.add(frame, e, enabled)

elif get_events(events_arne182, [ET.SOFT_DISABLE]):
state = State.softDisabling
soft_disable_timer = 300 # 3s
for e in get_events(events_arne182, [ET.SOFT_DISABLE]):
AM.add(frame, e, enabled)

# SOFT DISABLING
elif state == State.softDisabling:
if get_events(events_arne182, [ET.USER_DISABLE]):
state = State.disabled
AM.add(frame, "disable", enabled)

elif get_events(events_arne182, [ET.IMMEDIATE_DISABLE]):
state = State.disabled
for e in get_events(events_arne182, [ET.IMMEDIATE_DISABLE]):
AM.add(frame, e, enabled)

elif not get_events(events_arne182, [ET.SOFT_DISABLE]):
# no more soft disabling condition, so go back to ENABLED
state = State.enabled

elif get_events(events_arne182, [ET.SOFT_DISABLE]) and soft_disable_timer > 0:
for e in get_events(events_arne182, [ET.SOFT_DISABLE]):
AM.add(frame, e, enabled)

elif soft_disable_timer <= 0:
state = State.disabled

# PRE ENABLING
elif state == State.preEnabled:
if get_events(events_arne182, [ET.USER_DISABLE]):
state = State.disabled
AM.add(frame, "disable", enabled)

elif get_events(events_arne182, [ET.IMMEDIATE_DISABLE, ET.SOFT_DISABLE]):
state = State.disabled
for e in get_events(events_arne182, [ET.IMMEDIATE_DISABLE, ET.SOFT_DISABLE]):
AM.add(frame, e, enabled)

elif not get_events(events_arne182, [ET.PRE_ENABLE]):
state = State.enabled

return state, soft_disable_timer, v_cruise_kph, v_cruise_kph_last


def state_control(frame, rcv_frame, plan, path_plan, CS, CP, state, events, v_cruise_kph, v_cruise_kph_last,
AM, rk, driver_status, LaC, LoC, read_only, is_metric, cal_perc, radar_state, arne_sm):
AM, rk, driver_status, LaC, LoC, read_only, is_metric, cal_perc, radar_state, arne_sm, events_arne182):
"""Given the state, this function returns an actuators packet"""

actuators = car.CarControl.Actuators.new_message()
Expand Down Expand Up @@ -257,6 +327,16 @@ def state_control(frame, rcv_frame, plan, path_plan, CS, CP, state, events, v_cr
else:
extra_text = str(int(round(CP.minSteerSpeed * CV.MS_TO_MPH))) + " mph"
AM.add(frame, e, enabled, extra_text_2=extra_text)

# parse warnings from car specific interface
for e in get_events(events_arne182, [ET.WARNING]):
extra_text = ""
if e == "belowSteerSpeed":
if is_metric:
extra_text = str(int(round(CP.minSteerSpeed * CV.MS_TO_KPH))) + " kph"
else:
extra_text = str(int(round(CP.minSteerSpeed * CV.MS_TO_MPH))) + " mph"
AM.add(frame, e, enabled, extra_text_2=extra_text)

plan_age = DT_CTRL * (frame - rcv_frame['plan'])
dt = min(plan_age, LON_MPC_STEP + DT_CTRL) + DT_CTRL # no greater than dt mpc + dt, to prevent too high extraps
Expand Down Expand Up @@ -290,6 +370,17 @@ def state_control(frame, rcv_frame, plan, path_plan, CS, CP, state, events, v_cr
else:
extra_text_2 = str(int(round(Filter.MIN_SPEED * CV.MS_TO_MPH))) + " mph"
AM.add(frame, str(e) + "Permanent", enabled, extra_text_1=extra_text_1, extra_text_2=extra_text_2)

# Parse permanent warnings to display constantly
for e in get_events(events_arne182, [ET.PERMANENT]):
extra_text_1, extra_text_2 = "", ""
if e == "calibrationIncomplete":
extra_text_1 = str(cal_perc) + "%"
if is_metric:
extra_text_2 = str(int(round(Filter.MIN_SPEED * CV.MS_TO_KPH))) + " kph"
else:
extra_text_2 = str(int(round(Filter.MIN_SPEED * CV.MS_TO_MPH))) + " mph"
AM.add(frame, str(e) + "Permanent", enabled, extra_text_1=extra_text_1, extra_text_2=extra_text_2)

AM.process_alerts(frame)

Expand Down Expand Up @@ -527,7 +618,7 @@ def controlsd_thread(sm=None, pm=None, can_sock=None):
prof.checkpoint("Ratekeeper", ignore=True)

# Sample data and compute car events
CS, events, cal_status, cal_perc, overtemp, free_space, low_battery, mismatch_counter =\
CS, events, cal_status, cal_perc, overtemp, free_space, low_battery, mismatch_counter, events_arne182 =\
data_sample(CI, CC, sm, can_sock, cal_status, cal_perc, overtemp, free_space, low_battery,
driver_status, state, mismatch_counter, params)
prof.checkpoint("Sample")
Expand Down Expand Up @@ -561,13 +652,13 @@ def controlsd_thread(sm=None, pm=None, can_sock=None):
if not read_only:
# update control state
state, soft_disable_timer, v_cruise_kph, v_cruise_kph_last = \
state_transition(sm.frame, CS, CP, state, events, soft_disable_timer, v_cruise_kph, AM)
state_transition(sm.frame, CS, CP, state, events, soft_disable_timer, v_cruise_kph, AM, events_arne182)
prof.checkpoint("State transition")

# Compute actuators (runs PID loops and lateral MPC)
actuators, v_cruise_kph, driver_status, v_acc, a_acc, lac_log = \
state_control(sm.frame, sm.rcv_frame, sm['plan'], sm['pathPlan'], CS, CP, state, events, v_cruise_kph, v_cruise_kph_last, AM, rk,
driver_status, LaC, LoC, read_only, is_metric, cal_perc, sm['radarState'], arne_sm)
driver_status, LaC, LoC, read_only, is_metric, cal_perc, sm['radarState'], arne_sm, events_arne182)

prof.checkpoint("State Control")

Expand Down
2 changes: 1 addition & 1 deletion selfdrive/controls/lib/planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def choose_solution(self, v_cruise_setpoint, enabled, lead_1, lead_2, steeringAn
lead1_check = math.sqrt((lead_1.dRel-center_x)**2+(lead_1.yRel-center_y)**2) < abs(2.5/math.sin(steeringAngle/1800.*math.pi))+1.
lead2_check = math.sqrt((lead_2.dRel-center_x)**2+(lead_2.yRel-center_y)**2) < abs(2.5/math.sin(steeringAngle/1800.*math.pi))+1.
elif steeringAngle < -100: # only at high angles
center_y = +1-2.5/math.tan(steeringAngle/1800.*math.pi) # Car Width 2m. Right side considered in right hand turn
center_y = +1+2.5/math.tan(steeringAngle/1800.*math.pi) # Car Width 2m. Right side considered in right hand turn
lead1_check = math.sqrt((lead_1.dRel-center_x)**2+(lead_1.yRel-center_y)**2) < abs(2.5/math.sin(steeringAngle/1800.*math.pi))+1.
lead2_check = math.sqrt((lead_2.dRel-center_x)**2+(lead_2.yRel-center_y)**2) < abs(2.5/math.sin(steeringAngle/1800.*math.pi))+1.
if enabled:
Expand Down

0 comments on commit 8239975

Please sign in to comment.