@ -2,6 +2,8 @@ import signal
import threading
import threading
import functools
import functools
from collections import namedtuple
from enum import Enum
from multiprocessing import Process , Queue , Value
from multiprocessing import Process , Queue , Value
from abc import ABC , abstractmethod
from abc import ABC , abstractmethod
@ -14,6 +16,16 @@ from openpilot.tools.sim.lib.common import SimulatorState, World
from openpilot . tools . sim . lib . simulated_car import SimulatedCar
from openpilot . tools . sim . lib . simulated_car import SimulatedCar
from openpilot . tools . sim . lib . simulated_sensors import SimulatedSensors
from openpilot . tools . sim . lib . simulated_sensors import SimulatedSensors
QueueMessage = namedtuple ( " QueueMessage " , [ " type " , " info " ] , defaults = [ None ] )
class QueueMessageType ( Enum ) :
START_STATUS = 0
CONTROL_COMMAND = 1
TERMINATION_INFO = 2
CLOSE_STATUS = 3
def control_cmd_gen ( cmd : str ) :
return QueueMessage ( QueueMessageType . CONTROL_COMMAND , cmd )
def rk_loop ( function , hz , exit_event : threading . Event ) :
def rk_loop ( function , hz , exit_event : threading . Event ) :
rk = Ratekeeper ( hz , None )
rk = Ratekeeper ( hz , None )
@ -80,11 +92,11 @@ Ignition: {self.simulator_state.ignition} Engaged: {self.simulator_state.is_enga
""" )
""" )
@abstractmethod
@abstractmethod
def spawn_world ( self ) - > World :
def spawn_world ( self , q : Queue ) - > World :
pass
pass
def _run ( self , q : Queue ) :
def _run ( self , q : Queue ) :
self . world = self . spawn_world ( )
self . world = self . spawn_world ( q )
self . simulated_car = SimulatedCar ( )
self . simulated_car = SimulatedCar ( )
self . simulated_sensors = SimulatedSensors ( self . dual_camera )
self . simulated_sensors = SimulatedSensors ( self . dual_camera )
@ -114,33 +126,34 @@ Ignition: {self.simulator_state.ignition} Engaged: {self.simulator_state.is_enga
# Read manual controls
# Read manual controls
if not q . empty ( ) :
if not q . empty ( ) :
message = q . get ( )
message = q . get ( )
m = message . split ( ' _ ' )
if message . type == QueueMessageType . CONTROL_COMMAND :
if m [ 0 ] == " steer " :
m = message . info . split ( ' _ ' )
steer_manual = float ( m [ 1 ] )
if m [ 0 ] == " steer " :
elif m [ 0 ] == " throttle " :
steer_manual = float ( m [ 1 ] )
throttle_manual = float ( m [ 1 ] )
elif m [ 0 ] == " throttle " :
elif m [ 0 ] == " brake " :
throttle_manual = float ( m [ 1 ] )
brake_manual = float ( m [ 1 ] )
elif m [ 0 ] == " brake " :
elif m [ 0 ] == " cruise " :
brake_manual = float ( m [ 1 ] )
if m [ 1 ] == " down " :
elif m [ 0 ] == " cruise " :
self . simulator_state . cruise_button = CruiseButtons . DECEL_SET
if m [ 1 ] == " down " :
elif m [ 1 ] == " up " :
self . simulator_state . cruise_button = CruiseButtons . DECEL_SET
self . simulator_state . cruise_button = CruiseButtons . RES_ACCEL
elif m [ 1 ] == " up " :
elif m [ 1 ] == " cancel " :
self . simulator_state . cruise_button = CruiseButtons . RES_ACCEL
self . simulator_state . cruise_button = CruiseButtons . CANCEL
elif m [ 1 ] == " cancel " :
elif m [ 1 ] == " main " :
self . simulator_state . cruise_button = CruiseButtons . CANCEL
self . simulator_state . cruise_button = CruiseButtons . MAIN
elif m [ 1 ] == " main " :
elif m [ 0 ] == " blinker " :
self . simulator_state . cruise_button = CruiseButtons . MAIN
if m [ 1 ] == " left " :
elif m [ 0 ] == " blinker " :
self . simulator_state . left_blinker = True
if m [ 1 ] == " left " :
elif m [ 1 ] == " right " :
self . simulator_state . left_blinker = True
self . simulator_state . right_blinker = True
elif m [ 1 ] == " right " :
elif m [ 0 ] == " ignition " :
self . simulator_state . right_blinker = True
self . simulator_state . ignition = not self . simulator_state . ignition
elif m [ 0 ] == " ignition " :
elif m [ 0 ] == " reset " :
self . simulator_state . ignition = not self . simulator_state . ignition
self . world . reset ( )
elif m [ 0 ] == " reset " :
elif m [ 0 ] == " quit " :
self . world . reset ( )
break
elif m [ 0 ] == " quit " :
break
self . simulator_state . user_brake = brake_manual
self . simulator_state . user_brake = brake_manual
self . simulator_state . user_gas = throttle_manual
self . simulator_state . user_gas = throttle_manual