Esempio n. 1
0
def plannerd_thread(gctx):
  context = zmq.Context()
  poller = zmq.Poller()

  carstate = messaging.sub_sock(context, service_list['carState'].port, poller)
  live20 = messaging.sub_sock(context, service_list['live20'].port)
  model = messaging.sub_sock(context, service_list['model'].port)

  plan = messaging.pub_sock(context, service_list['plan'].port)

  # wait for stats about the car to come in from controls
  cloudlog.info("plannerd is waiting for CarParams")
  CP = car.CarParams.from_bytes(Params().get("CarParams", block=True))
  cloudlog.info("plannerd got CarParams")

  CS = None
  PP = PathPlanner(model)
  AC = AdaptiveCruise(live20)

  # start the loop
  set_realtime_priority(2)

  # this runs whenever we get a packet that can change the plan
  while True:
    polld = poller.poll(timeout=1000)
    for sock, mode in polld:
      if mode != zmq.POLLIN or sock != carstate:
        continue

      cur_time = sec_since_boot()
      CS = messaging.recv_sock(carstate).carState

      PP.update(cur_time, CS.vEgo)

      # LoC.v_pid -> CS.vEgo
      # TODO: is this change okay?
      AC.update(cur_time, CS.vEgo, CS.steeringAngle, CS.vEgo, CP)

      # **** send the plan ****
      plan_send = messaging.new_message()
      plan_send.init('plan')

      # lateral plan
      plan_send.plan.lateralValid = not PP.dead
      if plan_send.plan.lateralValid:
        plan_send.plan.dPoly = map(float, PP.d_poly)

      # longitudal plan
      plan_send.plan.longitudinalValid = not AC.dead
      if plan_send.plan.longitudinalValid:
        plan_send.plan.vTarget = float(AC.v_target_lead)
        plan_send.plan.aTargetMin = float(AC.a_target[0])
        plan_send.plan.aTargetMax = float(AC.a_target[1])
        plan_send.plan.jerkFactor = float(AC.jerk_factor)
        plan_send.plan.hasLead = AC.has_lead

      plan.send(plan_send.to_bytes())
Esempio n. 2
0
    def __init__(self, CP):
        context = zmq.Context()
        self.CP = CP
        self.live20 = messaging.sub_sock(context, service_list['live20'].port)
        self.model = messaging.sub_sock(context, service_list['model'].port)

        self.plan = messaging.pub_sock(context, service_list['plan'].port)

        self.last_md_ts = 0
        self.last_l20_ts = 0

        if os.getenv("OPT") is not None:
            self.PP = OptPathPlanner(self.model)
        else:
            self.PP = PathPlanner()
        self.AC = AdaptiveCruise()
        self.FCW = ForwardCollisionWarning(_DT)
Esempio n. 3
0
    def __init__(self, CP):
        context = zmq.Context()
        self.CP = CP
        self.live20 = messaging.sub_sock(context, service_list['live20'].port)
        self.model = messaging.sub_sock(context, service_list['model'].port)

        self.plan = messaging.pub_sock(context, service_list['plan'].port)

        self.last_md_ts = 0
        self.last_l20_ts = 0
        self.last_model = 0.
        self.last_l20 = 0.
        self.model_dead = True
        self.radar_dead = True
        self.radar_errors = []

        self.PP = PathPlanner()
        self.AC = AdaptiveCruise()
        self.FCW = ForwardCollisionWarning(_DT)
Esempio n. 4
0
def controlsd_thread(gctx, rate=100):  #rate in Hz
    # *** log ***
    context = zmq.Context()
    live100 = messaging.pub_sock(context, service_list['live100'].port)
    carstate = messaging.pub_sock(context, service_list['carState'].port)
    carcontrol = messaging.pub_sock(context, service_list['carControl'].port)
    sendcan = messaging.pub_sock(context, service_list['sendcan'].port)

    thermal = messaging.sub_sock(context, service_list['thermal'].port)
    live20 = messaging.sub_sock(context, service_list['live20'].port)
    model = messaging.sub_sock(context, service_list['model'].port)
    health = messaging.sub_sock(context, service_list['health'].port)
    logcan = messaging.sub_sock(context, service_list['can'].port)

    # connects to can and sendcan
    CI = CarInterface()
    VP = CI.getVehicleParams()

    PP = PathPlanner(model)
    AC = AdaptiveCruise(live20)

    AM = AlertManager()

    LoC = LongControl()
    LaC = LatControl()

    # controls enabled state
    enabled = False
    last_enable_request = 0

    # learned angle offset
    angle_offset = 0

    # rear view camera state
    rear_view_toggle = False

    v_cruise_kph = 255

    # 0.0 - 1.0
    awareness_status = 0.0

    soft_disable_timer = None

    # Is cpu temp too high to enable?
    overtemp = False
    free_space = 1.0

    # start the loop
    set_realtime_priority(2)

    rk = Ratekeeper(rate, print_delay_threshold=2. / 1000)
    while 1:
        prof = Profiler()
        cur_time = sec_since_boot()

        # read CAN
        # CS = CI.update()
        CS = car.CarState.new_message()
        CS.vEgo = 13

        for a in messaging.drain_sock(logcan):
            CS.steeringAngle = a.carState.steeringAngle

        # broadcast carState
        cs_send = messaging.new_message()
        cs_send.init('carState')
        cs_send.carState = CS  # copy?
        carstate.send(cs_send.to_bytes())

        prof.checkpoint("CarInterface")

        # did it request to enable?
        enable_request, enable_condition = False, False

        if enabled:
            # gives the user 6 minutes
            # awareness_status -= 1.0/(100*60*6)
            if awareness_status <= 0.:
                # AM.add("driverDistracted", enabled)
                awareness_status = 1.0

        # reset awareness status on steering
        if CS.steeringPressed:
            awareness_status = 1.0

        # handle button presses
        for b in CS.buttonEvents:
            print b

            # reset awareness on any user action
            awareness_status = 1.0

            # button presses for rear view
            if b.type == "leftBlinker" or b.type == "rightBlinker":
                if b.pressed:
                    rear_view_toggle = True
                else:
                    rear_view_toggle = False

            if b.type == "altButton1" and b.pressed:
                rear_view_toggle = not rear_view_toggle

            if not VP.brake_only and enabled and not b.pressed:
                if b.type == "accelCruise":
                    v_cruise_kph = v_cruise_kph - (
                        v_cruise_kph % V_CRUISE_DELTA) + V_CRUISE_DELTA
                elif b.type == "decelCruise":
                    v_cruise_kph = v_cruise_kph - (
                        v_cruise_kph % V_CRUISE_DELTA) - V_CRUISE_DELTA
                v_cruise_kph = clip(v_cruise_kph, V_CRUISE_MIN, V_CRUISE_MAX)

            if not enabled and b.type in ["accelCruise", "decelCruise"
                                          ] and not b.pressed:
                enable_request = True

            # do disable on button down
            if b.type == "cancel" and b.pressed:
                AM.add("disable", enabled)

        # Hack-hack
        if not enabled:
            enable_request = True

        prof.checkpoint("Buttons")

        # *** health checking logic ***
        hh = messaging.recv_sock(health)
        if hh is not None:
            # if the board isn't allowing controls but somehow we are enabled!
            if not hh.health.controlsAllowed and enabled:
                AM.add("controlsMismatch", enabled)

        # *** thermal checking logic ***

        # thermal data, checked every second
        td = messaging.recv_sock(thermal)
        if False and td is not None:
            # Check temperature.
            overtemp = any(t > 950 for t in (td.thermal.cpu0, td.thermal.cpu1,
                                             td.thermal.cpu2, td.thermal.cpu3,
                                             td.thermal.mem, td.thermal.gpu))
            # under 15% of space free
            free_space = td.thermal.freeSpace

        prof.checkpoint("Health")

        # *** getting model logic ***
        PP.update(cur_time, CS.vEgo)

        if rk.frame % 5 == 2:
            # *** run this at 20hz again ***
            angle_offset = learn_angle_offset(enabled, CS.vEgo, angle_offset,
                                              np.asarray(PP.d_poly), LaC.y_des,
                                              CS.steeringPressed)

        # disable if the pedals are pressed while engaged, this is a user disable
        if enabled:
            if CS.gasPressed or CS.brakePressed:
                AM.add("disable", enabled)

        if enable_request:
            # check for pressed pedals
            if CS.gasPressed or CS.brakePressed:
                AM.add("pedalPressed", enabled)
                enable_request = False
            else:
                print "enabled pressed at", cur_time
                last_enable_request = cur_time

            # don't engage with less than 15% free
            if free_space < 0.15:
                AM.add("outOfSpace", enabled)
                enable_request = False

        if VP.brake_only:
            enable_condition = ((cur_time - last_enable_request) <
                                0.2) and CS.cruiseState.enabled
        else:
            enable_condition = enable_request

        if enable_request or enable_condition or enabled:
            # add all alerts from car
            for alert in CS.errors:
                AM.add(alert, enabled)

            if False and AC.dead:
                AM.add("radarCommIssue", enabled)

            if PP.dead:
                AM.add("modelCommIssue", enabled)

            if overtemp:
                AM.add("overheat", enabled)

        prof.checkpoint("Model")

        if enable_condition and not enabled and not AM.alertPresent():
            print "*** enabling controls"

            # beep for enabling
            AM.add("enable", enabled)

            # enable both lateral and longitudinal controls
            enabled = True

            # on activation, let's always set v_cruise from where we are, even if PCM ACC is active
            v_cruise_kph = int(
                round(
                    max(CS.vEgo * CV.MS_TO_KPH * VP.ui_speed_fudge,
                        V_CRUISE_ENABLE_MIN)))

            # 6 minutes driver you're on
            awareness_status = 1.0

            # reset the PID loops
            LaC.reset()
            # start long control at actual speed
            LoC.reset(v_pid=CS.vEgo)

        if VP.brake_only and CS.cruiseState.enabled:
            v_cruise_kph = CS.cruiseState.speed * CV.MS_TO_KPH

        # *** put the adaptive in adaptive cruise control ***
        AC.update(cur_time, CS.vEgo, CS.steeringAngle, LoC.v_pid,
                  awareness_status, VP)

        prof.checkpoint("AdaptiveCruise")

        # *** gas/brake PID loop ***
        final_gas, final_brake = LoC.update(enabled, CS.vEgo, v_cruise_kph,
                                            AC.v_target_lead, AC.a_target,
                                            AC.jerk_factor, VP)

        # *** steering PID loop ***
        final_steer, sat_flag = LaC.update(enabled, CS.vEgo, CS.steeringAngle,
                                           CS.steeringPressed, PP.d_poly,
                                           angle_offset, VP)

        prof.checkpoint("PID")

        # ***** handle alerts ****
        # send a "steering required alert" if saturation count has reached the limit
        if False and sat_flag:
            AM.add("steerSaturated", enabled)

        if enabled and AM.alertShouldDisable():
            print "DISABLING IMMEDIATELY ON ALERT"
            enabled = False

        if enabled and AM.alertShouldSoftDisable():
            if soft_disable_timer is None:
                soft_disable_timer = 3 * rate
            elif soft_disable_timer == 0:
                print "SOFT DISABLING ON ALERT"
                enabled = False
            else:
                soft_disable_timer -= 1
        else:
            soft_disable_timer = None

        # *** push the alerts to current ***
        alert_text_1, alert_text_2, visual_alert, audible_alert = AM.process_alerts(
            cur_time)

        # ***** control the car *****
        CC = car.CarControl.new_message()

        CC.enabled = enabled

        CC.gas = float(final_gas)
        CC.brake = float(final_brake)
        CC.steeringTorque = float(final_steer)

        CC.cruiseControl.override = True
        CC.cruiseControl.cancel = bool(
            (not VP.brake_only)
            or (not enabled and CS.cruiseState.enabled
                ))  # always cancel if we have an interceptor
        CC.cruiseControl.speedOverride = float((LoC.v_pid - .3) if (
            VP.brake_only and final_brake == 0.) else 0.0)
        CC.cruiseControl.accelOverride = float(AC.a_pcm)

        CC.hudControl.setSpeed = float(v_cruise_kph * CV.KPH_TO_MS)
        CC.hudControl.speedVisible = enabled
        CC.hudControl.lanesVisible = enabled
        CC.hudControl.leadVisible = bool(AC.has_lead)

        CC.hudControl.visualAlert = visual_alert
        CC.hudControl.audibleAlert = audible_alert

        # this alert will apply next controls cycle
        #if not CI.apply(CC):
        #  AM.add("controlsFailed", enabled)

        # broadcast carControl
        cc_send = messaging.new_message()
        cc_send.init('carControl')
        cc_send.carControl = CC  # copy?
        #carcontrol.send(cc_send.to_bytes())
        sendcan.send(cc_send.to_bytes())

        prof.checkpoint("CarControl")

        # ***** publish state to logger *****

        # publish controls state at 100Hz
        dat = messaging.new_message()
        dat.init('live100')

        # show rear view camera on phone if in reverse gear or when button is pressed
        dat.live100.rearViewCam = ('reverseGear'
                                   in CS.errors) or rear_view_toggle
        dat.live100.alertText1 = alert_text_1
        dat.live100.alertText2 = alert_text_2
        dat.live100.awarenessStatus = max(awareness_status,
                                          0.0) if enabled else 0.0

        # what packets were used to process
        dat.live100.canMonoTimes = list(CS.canMonoTimes)
        dat.live100.mdMonoTime = PP.logMonoTime
        dat.live100.l20MonoTime = AC.logMonoTime

        # if controls is enabled
        dat.live100.enabled = enabled

        # car state
        dat.live100.vEgo = CS.vEgo
        dat.live100.angleSteers = CS.steeringAngle
        dat.live100.steerOverride = CS.steeringPressed

        # longitudinal control state
        dat.live100.vPid = float(LoC.v_pid)
        dat.live100.vCruise = float(v_cruise_kph)
        dat.live100.upAccelCmd = float(LoC.Up_accel_cmd)
        dat.live100.uiAccelCmd = float(LoC.Ui_accel_cmd)

        # lateral control state
        dat.live100.yActual = float(LaC.y_actual)
        dat.live100.yDes = float(LaC.y_des)
        dat.live100.upSteer = float(LaC.Up_steer)
        dat.live100.uiSteer = float(LaC.Ui_steer)

        # processed radar state, should add a_pcm?
        dat.live100.vTargetLead = float(AC.v_target_lead)
        dat.live100.aTargetMin = float(AC.a_target[0])
        dat.live100.aTargetMax = float(AC.a_target[1])
        dat.live100.jerkFactor = float(AC.jerk_factor)

        # lag
        dat.live100.cumLagMs = -rk.remaining * 1000.

        live100.send(dat.to_bytes())

        prof.checkpoint("Live100")

        # *** run loop at fixed rate ***
        if rk.keep_time():
            prof.display()
Esempio n. 5
0
class Planner(object):
    def __init__(self, CP):
        context = zmq.Context()
        self.CP = CP
        self.live20 = messaging.sub_sock(context, service_list['live20'].port)
        self.model = messaging.sub_sock(context, service_list['model'].port)

        self.plan = messaging.pub_sock(context, service_list['plan'].port)

        self.last_md_ts = 0
        self.last_l20_ts = 0

        if os.getenv("OPT") is not None:
            self.PP = OptPathPlanner(self.model)
        else:
            self.PP = PathPlanner()
        self.AC = AdaptiveCruise()
        self.FCW = ForwardCollisionWarning(_DT)

    # this runs whenever we get a packet that can change the plan

    def update(self, CS, LoC):
        #print "===>>> File: controls/lib/planner.py; FUnction: update"
        cur_time = sec_since_boot()

        md = messaging.recv_sock(self.model)
        #print "============ Planner"
        #print md
        if md is not None:
            self.last_md_ts = md.logMonoTime


#      print "========= frameId"
#      print md.model.frameId
#    else:
#      print "======== None"
        l20 = messaging.recv_sock(self.live20)
        if l20 is not None:
            self.last_l20_ts = l20.logMonoTime

        self.PP.update(cur_time, CS.vEgo, md)

        # LoC.v_pid -> CS.vEgo
        # TODO: is this change okay?
        self.AC.update(cur_time, CS.vEgo, CS.steeringAngle, LoC.v_pid, self.CP,
                       l20)

        # **** send the plan ****
        plan_send = messaging.new_message()
        plan_send.init('plan')

        plan_send.plan.mdMonoTime = self.last_md_ts
        plan_send.plan.l20MonoTime = self.last_l20_ts

        # lateral plan
        plan_send.plan.lateralValid = not self.PP.dead
        if plan_send.plan.lateralValid:
            plan_send.plan.dPoly = map(float, self.PP.d_poly)
        #else:
        #print "====================No Vision Data========"

        # longitudal plan
        plan_send.plan.longitudinalValid = not self.AC.dead
        if plan_send.plan.longitudinalValid:
            plan_send.plan.vTarget = float(self.AC.v_target_lead)
            plan_send.plan.aTargetMin = float(self.AC.a_target[0])
            plan_send.plan.aTargetMax = float(self.AC.a_target[1])
            plan_send.plan.jerkFactor = float(self.AC.jerk_factor)
            plan_send.plan.hasLead = self.AC.has_lead

        # compute risk of collision events: fcw
        self.FCW.process(CS, self.AC)
        plan_send.plan.fcw = bool(self.FCW.active)

        self.plan.send(plan_send.to_bytes())
        return plan_send
Esempio n. 6
0
class Planner(object):
    def __init__(self, CP):
        context = zmq.Context()
        self.CP = CP
        self.live20 = messaging.sub_sock(context, service_list['live20'].port)
        self.model = messaging.sub_sock(context, service_list['model'].port)

        self.plan = messaging.pub_sock(context, service_list['plan'].port)

        self.last_md_ts = 0
        self.last_l20_ts = 0
        self.last_model = 0.
        self.last_l20 = 0.
        self.model_dead = True
        self.radar_dead = True
        self.radar_errors = []

        self.PP = PathPlanner()
        self.AC = AdaptiveCruise()
        self.FCW = ForwardCollisionWarning(_DT)

    # this runs whenever we get a packet that can change the plan
    def update(self, CS, LoC):
        cur_time = sec_since_boot()

        md = messaging.recv_sock(self.model)
        if md is not None:
            self.last_md_ts = md.logMonoTime
            self.last_model = cur_time
            self.model_dead = False
        if cur_time - self.last_model > 0.5:
            self.model_dead = True

        l20 = messaging.recv_sock(self.live20)
        if l20 is not None:
            self.last_l20_ts = l20.logMonoTime
            self.last_l20 = cur_time
            self.radar_dead = False
            self.radar_errors = list(l20.live20.radarErrors)
        if cur_time - self.last_l20 > 0.5:
            self.radar_dead = True

        self.PP.update(CS.vEgo, md)

        # LoC.v_pid -> CS.vEgo
        # TODO: is this change okay?
        self.AC.update(CS.vEgo, CS.steeringAngle, LoC.v_pid, self.CP, l20)

        # **** send the plan ****
        plan_send = messaging.new_message()
        plan_send.init('plan')

        events = []
        if self.model_dead:
            events.append(
                create_event('modelCommIssue',
                             [ET.NO_ENTRY, ET.IMMEDIATE_DISABLE]))
        if self.radar_dead or 'commIssue' in self.radar_errors:
            events.append(
                create_event('radarCommIssue',
                             [ET.NO_ENTRY, ET.IMMEDIATE_DISABLE]))
        if 'fault' in self.radar_errors:
            events.append(
                create_event('radarFault',
                             [ET.NO_ENTRY, ET.IMMEDIATE_DISABLE]))

        plan_send.plan.events = events

        plan_send.plan.mdMonoTime = self.last_md_ts
        plan_send.plan.l20MonoTime = self.last_l20_ts

        # lateral plan
        plan_send.plan.lateralValid = not self.model_dead
        plan_send.plan.dPoly = map(float, self.PP.d_poly)
        plan_send.plan.laneWidth = float(self.PP.lane_width)

        # longitudal plan
        plan_send.plan.longitudinalValid = not self.radar_dead
        plan_send.plan.vTarget = float(self.AC.v_target_lead)
        plan_send.plan.aTargetMin = float(self.AC.a_target[0])
        plan_send.plan.aTargetMax = float(self.AC.a_target[1])
        plan_send.plan.jerkFactor = float(self.AC.jerk_factor)
        plan_send.plan.hasLead = self.AC.has_lead

        # compute risk of collision events: fcw
        self.FCW.process(CS, self.AC)
        plan_send.plan.fcw = bool(self.FCW.active)

        self.plan.send(plan_send.to_bytes())
        return plan_send