# Assign default states memory = Memory() memory.put(['ps3_controller/brake'], True) memory.put(['dashboard/brake'], False) memory.put(['dashboard/driver_type'], 'user') memory.put(['vehicle/brake'], True) # Initialize the car car = Vehicle(mem=memory, warm_up_seconds=cfg.WARM_UP_SECONDS, port=port) # Consume video from a cheap webcam camera = Camera(name='video', output_names=['camera/image_array'], is_localhost=is_localhost, is_verbose=args['verbose-video']) car.add(camera) # Listen for user input user_input = UserInput(name='user-input', output_names=[ 'dashboard/brake', 'dashboard/driver_type', 'dashboard/model_constant_throttle', 'remote_model/angle' ], is_localhost=is_localhost, is_verbose=args['verbose-user-input']) car.add(user_input) # Communicate with the engine engine = Engine(name='engine', input_names=[
def drive(cfg): car = Vehicle() inputs = [] # add camera if cfg.CAMERA_TYPE == "PICAM": cam = PiCamera(image_w=cfg.IMAGE_W, image_h=cfg.IMAGE_H, image_d=cfg.IMAGE_DEPTH, framerate=cfg.CAMERA_FRAMERATE, vflip=cfg.CAMERA_VFLIP, hflip=cfg.CAMERA_HFLIP) else: raise (Exception("Unkown camera type: %s" % cfg.CAMERA_TYPE)) car.add(cam, inputs=inputs, outputs=['cam/image_array'], threaded=True) # add controller if cfg.USE_JOYSTICK_AS_DEFAULT: ctrl = get_js_controller(cfg) car.add(ctrl, inputs=['cam/image_array'], outputs=['user/angle', 'user/throttle', 'user/mode', 'recording'], threaded=True) ctrl.print_controls() # add steering and throttle steering_controller = PCA9685(cfg.STEERING_CHANNEL, cfg.PCA9685_I2C_ADDR, bus_num=cfg.PCA9685_I2C_BUSNUM) steering = PWMSteering(controller=steering_controller, left_pulse=cfg.STEERING_LEFT_PWM, right_pulse=cfg.STEERING_RIGHT_PWM) throttle_controller = PCA9685(cfg.THROTTLE_CHANNEL, cfg.PCA9685_I2C_ADDR, bus_num=cfg.PCA9685_I2C_BUSNUM) throttle = PWMThrottle(controller=throttle_controller, max_pulse=cfg.THROTTLE_FORWARD_PWM, zero_pulse=cfg.THROTTLE_STOPPED_PWM, min_pulse=cfg.THROTTLE_REVERSE_PWM) car.add(steering, inputs=['user/angle']) car.add(throttle, inputs=['user/throttle']) # add tub to save data inputs = ['cam/image_array', 'user/angle', 'user/throttle', 'user/mode'] types = ['image_array', 'float', 'float', 'str'] # do we want to store new records into own dir or append to existing tub_path = TubHandler(path=cfg.DATA_PATH).create_tub_path( ) if cfg.AUTO_CREATE_NEW_TUB else cfg.DATA_PATH print('tub_path: ', cfg.DATA_PATH) tub_writer = TubWriter(base_path=tub_path, inputs=inputs, types=types) car.add(tub_writer, inputs=inputs, outputs=["tub/num_records"], run_condition='recording') if isinstance(ctrl, JoystickController): print("You can now move your joystick to drive your car.") ctrl.set_tub(tub_writer.tub) car.start(rate_hz=cfg.DRIVE_LOOP_HZ)
from car.config import load_config from car.parts.camera import Webcam from car.parts.engine import Engine from car.parts.web_controller.web import LocalWebController from car.parts.datastore import DatasetHandler # Load default settings cfg = load_config() # Initialize the car car = Vehicle() # Add a webcam cam = Webcam(ffmpeg_host=cfg.PI_HOSTNAME) car.add(cam, outputs=['cam/image_array'], threaded=True) # Add a local Tornado web server to receive commands ctr = LocalWebController() car.add(ctr, inputs=['cam/image_array'], outputs=['user/angle', 'user/throttle', 'user/mode', 'recording'], threaded=True) # Add engine engine = Engine(16, 18, 22, 19, 21, 23, ['user/angle', 'user/throttle']) car.add(engine, inputs=['user/angle', 'user/throttle'], threaded=True) # Add dataset to save data
def drive(cfg, model_path=None, use_joystick=True): """ Drive the car. You will either drive to record data for training or drive to test the autonomous mode. Either use Web controls or Joystick to control the vehicle. If driving autonomous, give the model to load. :param cfg: Configuration for user defined values. :param model_path: Path to load the model. :param use_joystick Use parameter in startup to use joystick. """ from car.keras import KerasRicoai #Initialized car V = Vehicle() # Setup camera cam = PiCamera() V.add(cam, outputs=['cam/image_array'], threaded=True) # Select if only use bluetooth PS3 controller ctr = JoystickController( max_throttle=cfg.JOYSTICK_MAX_THROTTLE, steering_scale=cfg.JOYSTICK_STEERING_SCALE, auto_record_on_throttle=cfg.AUTO_RECORD_ON_THROTTLE) V.add(ctr, inputs=['cam/image_array'], outputs=['user/angle', 'user/throttle', 'user/mode', 'recording'], threaded=True) # See if we should even run the pilot module. # This is only needed because the part run_contion only accepts boolean def pilot_condition(mode): if mode == 'user': return False else: return True pilot_condition_part = Lambda(pilot_condition) V.add(pilot_condition_part, inputs=['user/mode'], outputs=['run_pilot']) # Load the model configuration kl = KerasRicoai() if model_path: print(model_path) kl.load(model_path) V.add(kl, inputs=['cam/image_array'], outputs=['pilot/angle', 'pilot/throttle'], run_condition='run_pilot') # Choose what inputs should change the car. def drive_mode(mode, user_angle, user_throttle, pilot_angle, pilot_throttle): if mode == 'user': return user_angle, user_throttle elif mode == 'local_angle': return pilot_angle, user_throttle else: return pilot_angle, pilot_throttle drive_mode_part = Lambda(drive_mode) V.add(drive_mode_part, inputs=[ 'user/mode', 'user/angle', 'user/throttle', 'pilot/angle', 'pilot/throttle' ], outputs=['angle', 'throttle']) # Configure the throttle and angle control hardware # Calibrate min/max for steering angle # Calibrate min/max/zero for throttle steering_controller = PCA9685(1) steering = PWMSteering(controller=steering_controller, left_pulse=460, right_pulse=260, invert_steering_angle=cfg.INVERT_STEERING_ANGLE) throttle_controller = PCA9685(0) throttle = PWMThrottle(controller=throttle_controller, max_pulse=500, zero_pulse=370, min_pulse=220) V.add(steering, inputs=['angle']) V.add(throttle, inputs=['throttle']) # Add tub to save data inputs = [ 'cam/image_array', 'user/angle', 'user/throttle', 'pilot/angle', 'pilot/throttle', 'user/mode' ] types = ['image_array', 'float', 'float', 'float', 'float', 'str'] th = TubHandler(path=cfg.DATA_PATH) tub_writer = th.new_tub_writer(inputs=inputs, types=types) V.add(tub_writer, inputs=inputs, run_condition='recording') # Run the vehicle for 20 seconds V.start(rate_hz=cfg.FPS, max_loop_count=100000) print("You can now go to <your pi ip address>:8887 to drive your car.")
from car.vehicle import Vehicle from car.config import load_config from car.parts.camera import Webcam from car.parts.engine import Engine from car.parts.web_controller.web import LocalWebController from car.parts.datastore import DatasetHandler # Load default settings cfg = load_config() # Initialize the car car = Vehicle() # Add a webcam cam = Webcam(ffmpeg_host=cfg.PI_HOSTNAME) car.add(cam, outputs=['cam/image_array'], threaded=True) # Add a local Tornado web server to receive commands ctr = LocalWebController() car.add(ctr, inputs=['cam/image_array'], outputs=['user/angle', 'user/throttle', 'user/mode', 'recording'], threaded=True) # Add engine engine = Engine(16, 18, 22, 19, 21, 23, ['user/angle', 'user/throttle']) car.add(engine, inputs=['user/angle', 'user/throttle'], threaded=True) # Add dataset to save data inputs = ['cam/image_array', 'user/angle', 'user/throttle', 'user/mode'] types = ['image_array', 'float', 'float', 'str']