def initialize_vehicle_type(self): if self.isDrone is False: # Connect to the AirSim simulator and begin: print('Initializing Car Client') self.client = client.CarClient() self.client.confirmConnection() self.client.enableApiControl(True) print('Initialization DONE!') else: print('Initializing Drone Client') self.client = client.MultirotorClient() self.client.confirmConnection() self.client.enableApiControl(True) print('Initialization DONE!')
def init_vehicle(self): # Set Drive Commands to zero initially self._throttle = 0 # Used as a constant gain factor for the action throttle. self._steering = 0 # Each action lasts this many seconds self._brake = 0 self.THROTTLE_INC = .10 self.THROTTLE_DEC = -.10 self.BRAKE_INC = .10 self.BRAKE_DEC = -.20 self.STEER_LEFT_INC = -.10 self.STEER_RIGHT_INC = .10 # Connect to the AirSim simulator and begin: print('Initializing Car Client') self.client = client.CarClient() self.client.confirmConnection() self.client.enableApiControl(True) print('Initialization DONE!') print("Setting Camera Views") orien = Vector3r(0, 0, 0) self.client.simSetCameraOrientation(0, orien) #radians orien = Vector3r(0, .12, -np.pi/9) self.client.simSetCameraOrientation(1, orien) orien = Vector3r(0, .12, np.pi/9) self.client.simSetCameraOrientation(2, orien) # Reset Collion Flags print("Setting Camera Views DONE!") # Set up GUI Video Feeder num_video_feeds = np.sum(np.array(self.image_mask_FC_FR_FL, dtype = np.int)) * self.IMG_STEP GUIConn, self.simEnvDataConn = multiprocessing.Pipe() self.app = AirSimGUI.CarGUI(GUIConn, vehicle_names = [self.vehicle_name], num_video_feeds = num_video_feeds, isRGB = self.isRGB, isNormal = self.isNormal)
def __init__(self, vehicle_names, image_mask_FC_FR_FL=[True, False, False], reward_function=car_racing_rewarding_function, mode="rgb_normal"): self.vehicle_names = vehicle_names # Set reward function self.reward_function = reward_function # Set Environment Return options self.mode = mode self.mode # Set Drive Commands to zero initially self._throttle = dict.fromkeys( self.vehicle_names, 0) # Used as a constant gain factor for the action throttle. self._steering = dict.fromkeys( self.vehicle_names, 0) # Each action lasts this many seconds self._brake = dict.fromkeys(self.vehicle_names, 0) self.THROTTLE_INC = .10 self.THROTTLE_DEC = -.10 self.BRAKE_INC = .10 self.BRAKE_DEC = -.20 self.STEER_LEFT_INC = -.10 self.STEER_RIGHT_INC = .10 # The number of INERTIAL state variables to keep track of self.count_inertial_state_variables = 15 # Posx, Posy, PosZ, Vx, Vy, Vz, Ax, Ay, Az, AngVelx, AngVely, AngVelz, AngAccx, AngAccy, AngAccz # Throttle up, throttle down, increase brake, decrease break, left_steer, right_steer, No action self.count_car_actions = 7 # Initialize the current inertial state to zero self.current_inertial_states = dict.fromkeys( self.vehicle_names, np.array(np.zeros(self.count_inertial_state_variables))) # Initialize the IMAGE variables -- We Take in Front Center, Right, Left self.images_rgb = dict.fromkeys(self.vehicle_names, 0) self.images_rgba = dict.fromkeys(self.vehicle_names, 0) self.image_mask_rgb = np.array( [[0 + 3 * i, 1 + 3 * i, 2 + 3 * i] for m, i in zip(image_mask_FC_FR_FL, range(3)) if m]).reshape(-1) self.image_mask_rgba = np.array( [[0 + 4 * i, 1 + 4 * i, 2 + 4 * i] for m, i in zip(image_mask_FC_FR_FL, range(3)) if m]).reshape(-1) self.image_mask_FC_FR_FL = image_mask_FC_FR_FL self.initial_position = dict.fromkeys(self.vehicle_names, 0) self.initial_velocity = dict.fromkeys(self.vehicle_names, 0) # Connect to the AirSim simulator and begin: print('Initializing Car Client') self.client = client.CarClient() self.client.confirmConnection() for vn in self.vehicle_names: self.client.enableApiControl(True, vn) orien = Vector3r(0, 0, 0) self.client.simSetCameraOrientation(0, orien.to_Quaternionr(), vehicle_name=vn) #radians orien = Vector3r(0, .12, -np.pi / 9) self.client.simSetCameraOrientation(1, orien, vehicle_name=vn) orien = Vector3r(0, .12, np.pi / 9) self.client.simSetCameraOrientation(2, orien, vehicle_name=vn) # Reset Collion Flags print('Initialization Complete!') # Timing Operations Initialize self.dt = 0 self.tic = 0 self.toc = 0
def __init__(self, vehicle_name="Car1", action_duration=.08, image_mask_FC_FR_FL=[True, True, True], sim_mode="rgb", IMG_HEIGHT=128, IMG_WIDTH=128, IMG_STEP=3): # Set Environment Return options self.action_duration = action_duration self.mode = sim_mode # Set Drive Commands to zero initially self._throttle = 0 # Used as a constant gain factor for the action throttle. self._steering = 0 # Each action lasts this many seconds self._brake = 0 # Simulator Image setup self.IMG_HEIGHT = IMG_HEIGHT self.IMG_WIDTH = IMG_WIDTH isRGB = False IMG_CHANNELS = 1 if 'rgb' in self.mode: isRGB = True IMG_CHANNELS = 3 isNormal = False if 'normal' in self.mode: isNormal = True self.IMG_CHANNELS = IMG_CHANNELS self.IMG_STEP = IMG_STEP self.IMG_VIEWS = np.sum(np.array(image_mask_FC_FR_FL, dtype=np.int)) # Initialize the container that holds the sequence of images from the simulator self.obs4 = np.zeros( (self.IMG_HEIGHT, self.IMG_WIDTH, self.IMG_CHANNELS * self.IMG_STEP * self.IMG_VIEWS)) # The number of INERTIAL state variables to keep track of self.count_inertial_state_variables = 15 # Posx, Posy, PosZ, Vx, Vy, Vz, Ax, Ay, Az, AngVelx, AngVely, AngVelz, AngAccx, AngAccy, AngAccz # Throttle up, throttle down, increase brake, decrease break, left_steer, right_steer, No action self.count_car_actions = 7 # Initialize the current inertial state to zero self.current_inertial_state = np.array( np.zeros(self.count_inertial_state_variables)) # Initialize the IMAGE variables -- We Take in Front Center, Right, Left self.images_rgb = None self.images_rgba = None self.image_mask_rgb = np.array( [[0 + 3 * i, 1 + 3 * i, 2 + 3 * i] for m, i in zip(image_mask_FC_FR_FL, range(3)) if m]).reshape(-1) self.image_mask_rgba = np.array( [[0 + 4 * i, 1 + 4 * i, 2 + 4 * i] for m, i in zip(image_mask_FC_FR_FL, range(3)) if m]).reshape(-1) self.image_mask_FC_FR_FL = image_mask_FC_FR_FL # Connect to the AirSim simulator and begin: print('Initializing Car Client') self.client = client.CarClient() self.client.confirmConnection() self.client.enableApiControl(True) print('Initialization DONE!') print("Setting Camera Views") orien = Vector3r(0, 0, 0) self.client.simSetCameraOrientation("0", orien) #radians orien = Vector3r(0, .12, -np.pi / 9) self.client.simSetCameraOrientation("1", orien) orien = Vector3r(0, .12, np.pi / 9) self.client.simSetCameraOrientation("2", orien) # Reset Collion Flags print("Setting Camera Views DONE!") # Set up GUI Video Feeder self.gui_data = {'obs': None, 'state': None, 'meta': None} self.vehicle_name = vehicle_name num_video_feeds = np.sum( np.array(self.image_mask_FC_FR_FL, dtype=np.int)) * IMG_STEP GUIConn, self.simEnvDataConn = multiprocessing.Pipe() self.app = AirSimGUI.CarGUI(GUIConn, vehicle_names=[vehicle_name], num_video_feeds=num_video_feeds, isRGB=isRGB, isNormal=isNormal) # Timing Operations Initialize self.time_to_do_action = 0 self.time_to_grab_images = 0 self.time_to_grab_states = 0 self.time_to_calc_reward = 0 self.time_to_step = 0 self.extra_metadata = None
def launch_simulator(self): self.check_accesstoken() # Abort if simulator is already running if self.simulation_process is not None: abort(400, description='Simulation already running.') # Get team config teamId = request.json['id'] self.mission = request.json['mission'] self.track = request.json['track'] self.competition_mode = request.json['competition_mode'] for obj in self.team_config['teams']: if obj['id'] == teamId: self.team = obj # Create log file. Create logs directory if it does not exist filename = 'logs/{}_{}_{}.txt'.format( str(datetime.now().strftime("%d-%m-%Y_%H%M%S")), self.team['name'].lower().replace(' ', '-'), self.mission) if not os.path.exists(os.path.dirname(filename)): try: os.makedirs(os.path.dirname(filename)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise # Write to log file self.log_file = open(filename, 'w') self.log("created logfile " + filename) self.finished_signal_received = False # Write team specific car settings to settings.json filename = os.path.realpath( os.path.dirname(__file__)) + '/../settings.json' with open(filename, 'w') as file: json.dump(self.team['car_settings'], file, sort_keys=True, indent=4, separators=(',', ': ')) try: # Launch Unreal Engine simulator proc = subprocess.Popen( ['../simulator/FSDS.exe', '/Game/' + self.track + '?listen']) time.sleep(5) # Create connection with airsim car client self.client_airsim = airsim.CarClient() self.client_airsim.confirmConnection() # Get referee state ref = self.client_airsim.getRefereeState() self.doo_count = ref.doo_counter self.lap_times = ref.laps # Start referee state listener self.referee_state_timer = Timer(1.5, self.referee_state_listener) self.referee_state_timer.start() self.simulation_process = proc self.log('Launched simulator. {} {} {}'.format( self.team['name'], self.track, self.mission)) return {} except: e = sys.exc_info()[0] print("Error while launching simulator", e) self.shutdown_process(proc) raise