def main(): global nmap, road_points, path_in_list, path_in_environment # finish = turn.execute_turns(client, nmap, road_points, path_in_list, path_in_environment) if len(sys.argv) == 2: client = airsim.CarClient() client.confirmConnection() print('Connect succcefully!') client.enableApiControl(True) car_controls = airsim.CarControls() client.simSetTimeOfDay(True, "2019-08-19 21:00:00", True, 1, 60, False) client.simSetCameraOrientation( 2, airsim.to_quaternion(0, 0, -math.pi / 4)) client.simSetCameraOrientation(1, airsim.to_quaternion(0, 0, math.pi / 4)) start_point = [13, 13] target_point = [0, 22] nmap, road_points, path_in_list, path_in_environment = turn.init_path_planning( start_point, target_point) if sys.argv[1] == 'test': print('start testing') testNetwork(client, car_controls, list(path_in_environment[-1])) elif sys.argv[1] == 'train': print('start training') trainNetwork(client, car_controls) else: print('unkown input argument, please run this file as follow!') print('python DQN_airsim.py train OR python DQN_airsim.py test') elif len(sys.argv) == 3: client = airsim.CarClient() client.confirmConnection() print('Connect succcefully!') if sys.argv[1] == 'show_map' and sys.argv[2] == 'train': print('display map') show_map.show_map(client, []) elif sys.argv[1] == 'show_map' and sys.argv[2] == 'test': print('display map') show_map.show_map(client, path_in_environment) else: print('unkown input argument, please run this file as follow!') print( 'python DQN_airsim.py show_map train OR python DQN_airsim.py show_map test' ) else: print('please include an argument train or test or show_map')
def _start(self): args = [self.env] if not (self._client_connected() or self._env_running()): for key, val in self.process_args.items(): tmp_arg = '-' + key if val is not None and val != '': tmp_arg += '=' + str(val) args.append(tmp_arg) if platform == 'win32': self.process = subprocess.Popen(args) else: self.process = subprocess.Popen(args, stdout=FNULL, stderr=FNULL) sleep(30) if not self.drone: self.client = airsim.CarClient(timeout_value=self.timeout * 3) else: self.client = airsim.MultirotorClient(timeout_value=self.timeout * 3) if not self._client_connected(): raise ChildProcessError('API not connected') self.client.confirmConnection()
def reset(self): # connect with server if self.client is None: if self.robot_dynamics: client = airsim.CarClient() client.enableApiControl(True) client.confirmConnection() else: client = airsim.VehicleClient() self.client = client if self.blocking: self.client.simPause(True) self.time = 0 self.human_states = defaultdict(list) self.robot_states = list() if self.curriculum_learning: self.goal_distance = np.random.uniform(2, 4) self.goal_position = np.array((self.goal_distance, 0, -1)) if self.robot_dynamics: self.client.reset() else: self.client.reset() self. _move(self.initial_position, 0) self._update_states() obs = self.compute_observation() return obs
def __init__(self): print("Connecting to Airsim Client:") self.client = airsim.CarClient() self.client.confirmConnection() print("Connection confirmed. Enabling car controls...") self.client.enableApiControl(True) self.car_controls = airsim.CarControls() print("Car controls enabled.") self.car_state = self.client.getCarState() self.mode = True """ self.mode = input("Would you like the rover to repeat commands?(yes/no)")) if self.mode == "yes": self.mode = False self.repeat = int(input("How many repeats?\n")) if self.repeat < 0 or self.repeat > 30: self.repeat = 2 """ self.record = (input("Do you want to record images? (yes/no)")) self.record = True if self.record == 'yes' else False self.fold = os.getcwd()+'/pictures3/' self.projectionMatrix = np.array([[1, 0.000000000, 0.000000000, -127.5000000000], [0.000000000, 1, 0.000000000, -71.5000000000], [0.000000000, 0.000000000, 1.00000000, 127.5 ], [0.000000000, 0.000000000, -1/20.0000000, 0.000000000]]) color = (0, 255, 0) self.rgb = "%d %d %d" % color
def __connect_to_airsim(self): attempt_count = 0 while True: try: print('Attempting to connect to AirSim (attempt {0})'.format( attempt_count)) self.__car_client = airsim.CarClient() self.__car_client.confirmConnection() self.__car_client.enableApiControl(True) self.__car_controls = airsim.CarControls() return except: print('Failed to connect.') attempt_count += 1 if (attempt_count % 10 == 0): print( '10 consecutive failures to connect. Attempting to start AirSim on my own.' ) if self.__local_run: os.system('START "" powershell.exe {0}'.format( os.path.join( self.__airsim_path, 'AD_Cookbook_Start_AirSim.ps1 neighborhood -windowed' ))) else: os.system( 'START "" powershell.exe D:\\AD_Cookbook_AirSim\\Scripts\\DistributedRL\\restart_airsim_if_agent.ps1' ) time.sleep(10)
def airpub(): pub = rospy.Publisher("airsim/image_raw", Image, queue_size=1) rospy.init_node('image_raw', anonymous=True) rate = rospy.Rate(10) # 10hz # connect to the AirSim simulator client = airsim.CarClient() client.confirmConnection() while not rospy.is_shutdown(): # get camera images from the car responses = client.simGetImages([ airsim.ImageRequest("1", airsim.ImageType.Scene, False, False) ]) #scene vision image in uncompressed RGB array for response in responses: img_rgb_string = response.image_data_uint8 # Populate image message msg = Image() msg.header.stamp = rospy.Time.now() msg.header.frame_id = "frameId" msg.encoding = "rgb8" msg.height = 360 # resolution should match values in settings.json msg.width = 640 msg.data = img_rgb_string msg.is_bigendian = 0 msg.step = msg.width * 3 # log time and size of published image rospy.loginfo(len(response.image_data_uint8)) # publish image message pub.publish(msg) # sleep until next cycle rate.sleep()
def __init__(self): self.client = airsim.CarClient("10.8.105.156") self.client.confirmConnection() self.client.enableApiControl(True) self.car_controls = airsim.CarControls() self.action_space = spaces.Discrete(6) self.time_step = 0 self.x_pos_goal_2 = 0.6 #0.545647 0.6 self.y_pos_goal_2 = -2.5 #-2.5 self.z_pos_goal_2 = 0.2 #0.2 self.w_rot_goal_2 = 1.0 # 0.999967 self.x_rot_goal_2 = 0.0 # self.y_rot_goal_2 = 0.0 # -0.000095 self.z_rot_goal_2 = 0.02 # 0.019440 self.x_pos_goal_1 = 0.09 #0.545647 0.6 self.y_pos_goal_1 = -4.2 #-2.5 self.z_pos_goal_1 = 0.18 #0.2 self.w_rot_goal = 0.7 # 0.999967 self.x_rot_goal_1 = -0.7 # self.y_rot_goal_1 = 0.0 # -0.000095 self.z_rot_goal_1 = -0.7 # 0.019440 self.task_one_complete = False self.height = 84 self.width = 84 # old 320 # self.observation_space = spaces.Box(low=-high, high=high, dtype=np.float32) self.observation_space = spaces.Box(low=0, high=255, shape=(2, self.height, self.width)) self.debug_mode = False self.goal_counter = 0 self.count = 0 self.state = None
def setSpeed(): pub = rospy.Publisher('sensor/speed', Float64, queue_size=1) rospy.init_node('setspeed', anonymous=True) rate = rospy.Rate(2) # 2 Hz # connect to the AirSim simulator client = airsim.CarClient() client.confirmConnection() client.enableApiControl(True) car_controls = airsim.CarControls() while not rospy.is_shutdown(): car_state = client.getCarState() print("Speed %d, Gear %d" % (car_state.speed, car_state.gear)) # Get the speed of the car car_controls.throttle = 1 car_controls.steering = 1 client.setCarControls(car_controls) # let car drive a bit time.sleep(1) car_speed = client.getCarState().speed rospy.loginfo(car_speed) pub.publish(car_speed) rate.sleep()
def main(): client = airsim.CarClient() client.confirmConnection() client.enableApiControl(True) #read image and process img = cv2.imread('Input/img_0_0_1545471200366026100.png', cv2.IMREAD_COLOR) (img_height, img_weight) = (img.shape[0] - 1, img.shape[1] - 1) pts = np.array([[img_weight / (3.9), img_height / (1.7)], [img_weight / (1.3), img_height / (1.7)], [img_weight, img_height / (1.25)], [0, img_height / (1.25)]], np.int32) # pts = np.array([[img_weight/(30), img_height/(1.5)], [img_weight/(1) , img_height/(1.5)], [img_weight, img_height/(1)], [0 , img_height/(1)]], np.int32) src_pts = pts.astype(np.float32) dst_pts = np.array( [[0, 0], [img_weight, 0], [img_weight, img_height], [0, img_height]], np.float32) ld = AdvancedLaneDetectorWithMemory(opts, ipts, src_pts, dst_pts, 20, 100, 50) # proc_img = ld.process_image(img) # plt.imshow(proc_img) # plt.show() """ filename = "C:/Users/namnh997/Documents/GitHub/Car-ML/car/Input/" responses = client.simGetImages([airsim.ImageRequest("0", airsim.ImageType.Scene, False, False)]) for response in responses: img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) img_rgba = img1d.reshape(response.height, response.width, 4) img_rgba1 = np.flipud(img_rgba) # airsim.write_png(os.path.normpath(filename + 'inputDetectLine5.png'), img_rgba1) img_rgba = cv2.cvtColor(img_rgba, cv2.COLOR_BGRA2BGR) img_rgba = ld.process_image(img_rgba) cv2.imshow("Display image", img_rgba) cv2.waitKey(0) client.enableApiControl(False) """ filename = "C:/Users/namnh997/Documents/GitHub/Car-ML/car/Input/" moveForward(client) import time while True: responses = client.simGetImages( [airsim.ImageRequest("0", airsim.ImageType.Scene, False, False)]) for response in responses: start = time.time() img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) img_rgba = img1d.reshape(response.height, response.width, 4) img_rgba1 = np.flipud(img_rgba) # airsim.write_png(os.path.normpath(filename + 'inputDetectLine5.png'), img_rgba1) img_rgba = cv2.cvtColor(img_rgba, cv2.COLOR_BGRA2BGR) img_rgba = ld.process_image(img_rgba) cv2.imshow('Display window', img_rgba) printt(time.time() - start) if cv2.waitKey(1) & 0xFF == ord('q'): break client.enableApiControl(False)
def __init__(self): # connect to the AirSim simulator self.client = airsim.CarClient() self.client.confirmConnection() self.client.enableApiControl(True) self.car_controls = airsim.CarControls()
def __airSimClientConnection(self): global client, clientCM, manualMode self.__setClient(airsim.CarClient(timeout_value=10000)) self.__setClientCM(airsim.CarClient()) self.getClient().confirmConnection() print("Do you want to activate the manual mode? [y/n]") if input() == 'n': manualMode = False self.getClient().enableApiControl(True) else: manualMode = True self.__setCarControls(airsim.CarControls()) time.sleep(2)
def __init__(self): print("Connecting to Airsim Client: ") self.client = airsim.CarClient() self.client.confirmConnection() print("Connection confirmed, Enabling Car controls ") self.client.enableApiControl(True) self.car_controls= airsim.CarControls() print("Car Controls enabled, Please enter Command parameters ") self.car_state=self.client.getCarState() self.mode = bool(input("Input car Mode: True (distance commands) " or "False")) if(self.mode): print("Going to Distance mode! ") pass else: self.repeat= int(input("How many repeats? " or "3")) self.record= bool(input("Record Images? " or "False")) self.fold= os.getcwd()+'/pictures3/' self.projectionMatrix = np.array([[1, 0.000000000, 0.000000000, -127.5000000000], [0.000000000, 1, 0.000000000, -71.5000000000], [0.000000000, 0.000000000, 1.00000000, 127.5 ], [0.000000000, 0.000000000, -1/20.0000000, 0.000000000]]) color = (0,255,0) self.rgb = "%d %d %d" % color ## Control Tello self.drone = tellopy.Tello() try: self.drone.subscribe(drone.EVENT_FLIGHT_DATA,handler) self.drone.connect() drone.wait_for_connection(60.0)
def __connect_to_airsim(self): attempt_count = 0 while True: try: print('Attempting to connect to AirSim (attempt {0})'.format( attempt_count)) self.__car_client = airsim.CarClient() # CarClient() self.__car_client.confirmConnection() self.__car_client.enableApiControl(True) self.__car_controls = airsim.CarControls() print('Connected!') return except: print('Failed to connect.') attempt_count += 1 if (attempt_count % 10 == 0): print( '10 consecutive failures to connect. Attempting to start AirSim on my own.' ) from subprocess import call call(self.__airsim_path + ' -ResX=640 -ResY=480 -windowed &', shell=True) print('Waiting a few seconds.') time.sleep(10)
def __init__(self): self.client = airsim.CarClient("10.8.105.156") self.client.confirmConnection() self.client.enableApiControl(True) self.car_controls = airsim.CarControls() self.action_space = spaces.Discrete(7) self.time_step = 0 self.x_pos_goal = 0.6 #0.545647 self.y_pos_goal = -2.5 #-1.419126 self.z_pos_goal = 0.2 #0.176768 self.counter_no_state = 0 self.w_rot_goal = 1.0 # 0.999967 self.x_rot_goal = 0.0 # self.y_rot_goal = 0.0 # -0.000095 self.z_rot_goal = 0.02 # 0.019440 self.max_step = 10 # steps to check if blocked self.last_states = queue.deque(maxlen=self.max_step) self.height = 84 self.width = 84 # old 320 # self.observation_space = spaces.Box(low=-high, high=high, dtype=np.float32) self.observation_space = spaces.Box(low=0, high=255, shape=(self.height, self.width, 2)) self.debug_mode = False self.goal_counter = 0 self.count = 0
def Plot(self, client=airsim.CarClient()): #client.simPause(True) car_state = client.getCarState() VehicleClient = airsim.VehicleClient() pos = car_state.kinematics_estimated.position #Plot red arrows persistently VehicleClient.simPlotArrows(points_start=[ Vector3r(self.startPosition[0], self.startPosition[1], self.startPosition[2]) ], points_end=[ Vector3r(self.endPosition[0], self.endPosition[1], self.endPosition[2]) ], color_rgba=[1.0, 0.0, 0.0, 1.0], arrow_size=10, thickness=5, is_persistent=True) self.startPosition = self.endPosition self.endPosition[0] -= 0.05 self.endPosition[1] += 0.05 #client.simPause(False) return 1
def main(): # connect the simulator client = airsim.CarClient() client.confirmConnection() client.enableApiControl(False) # car_controls = airsim.CarControls() pointcloud_pub = rospy.Publisher('/pointcloud', PointCloud, queue_size=10) rate = rospy.Rate(100) while not rospy.is_shutdown(): # get the lidar data lidarData = client.getLidarData() #print('lidar',lidarData) if len(lidarData.point_cloud) > 3: points = np.array(lidarData.point_cloud, dtype=np.dtype('f4')) points = np.reshape(points, (int(points.shape[0] / 3), 3)) # print('points:',points) pc = pub_pointcloud(points) pointcloud_pub.publish(pc) rate.sleep() else: print("\tNo points received from Lidar data")
def getPlayerName(self, json_data): # player_name_check = self.set_player_name() try: self.player_name = "" # sort : asc if ('Vehicles' in json_data): for key, value in sorted(json_data['Vehicles'].items()): if self.player_name == "" and airsim.CarClient( ).isApiControlEnabled(key) == False: self.player_name = key break if "" == self.player_name: print("please check the settings.json and client player_name") print("Player name : {}".format(self.player_name)) # check map num for lap count if ('Algo' in json_data): for key2, value2 in json_data['Algo'].items(): if (key2 == "Map"): self.map_num = value2 print("Map number : {}".format(self.map_num)) break except: pass
def __init__(self): self.client = airsim.CarClient() self.client.confirmConnection() self.client.enableApiControl(True) self.car_controls = airsim.CarControls() self.device = "cuda" self.reward_model = CNNNet().to(self.device) self.reward_model.load_state_dict( torch.load("model.dat", map_location=self.device)) self.reward_model.eval() # input space. high = np.array([np.inf, np.inf, 1., 1.]) low = np.array([-np.inf, -np.inf, 0., 0.]) self.observation_space = spaces.Box(low=low, high=high) # action space: [steer, accel, brake] self.action_space = spaces.Box(low=-1.0, high=1.0, shape=(3, )) self.default_action = [0.0, 1.0, 0.0] # store vehicle speeds self.max_speed = 3e5 self.prev_speed_sample = 40 self.past_vehicle_speeds = deque([self.max_speed] * self.prev_speed_sample, maxlen=self.prev_speed_sample) self.done = False self.lower_speed_limit = 5
def __init__(self): print("Establishing connection to airsim!") self.client= airsim.CarClient() self.client.confirmConnection() self.rover_state= self.client.getCarState() print("Establishing Car controls") self.client.enableApiControl(True) self.rover_controls= airsim.CarControls() print("Car Controls enabled") # Include example json of this will look? # rgb and depth will be in bytecode # IMU: imu_dict={ "speed":None, "px":None, "py": None, "pz":None, "ow":None, "ox": None, "oy": None, "oz": None } self.data_dict={ "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "rgb": None, "depth": None, "imu": imu_dict }
def reset(self): self.client.reset() self.client = airsim.CarClient() self.client.confirmConnection() self.client.enableApiControl(True) lidar_data = self.client.getLidarData(lidar_name='Lidar2') observation = parse_lidardata(lidar_data) return observation
def __init__(self, name='Car1'): self.client = airsim.CarClient(port=41452) self.name = name self.init_client() self.car_controls = airsim.CarControls() self.current_pose = None self.heading = None atexit.register(self.disconnect)
def __init__(self, ip=""): #connect to airsim self.client = airsim.CarClient(ip) self.client.confirmConnection() self.client2 = airsim.CarClient(ip) self.client2.confirmConnection() self.client_image = airsim.CarClient(ip) self.client_image.confirmConnection() self.car_controller = CarController(self.client, "Car1") self.car_controller2 = CarController(self.client2, "Car2") self.astar = Astar(json_file="./utils/airsim_nh.json") self.loop_limit = 130
def __init__(self, lidar_partition=60): self.client = airsim.CarClient() self.client.confirmConnection() self.client.enableApiControl(True) self.car_controls = airsim.CarControls() self.lidar_partition = lidar_partition # from left to right self.command_count = 0
def airpub(): ## Start ROS --------------------------------------------------------------- rospy.init_node('geo_mapping', anonymous=False) rate = rospy.Rate(10) ## Publishers -------------------------------------------------------------- # image publishers depth_pub = rospy.Publisher("airsim/depth", Image, queue_size=1) # camera paramters publisher rgb_cam_pub = rospy.Publisher("airsim/camera_info", CameraInfo, queue_size=1) depth_cam_pub = rospy.Publisher("airsim/depth/camera_info", CameraInfo, queue_size=1) # odometry publisher odom_pub = rospy.Publisher("odom", Odometry, queue_size=1) # pose publisher pose_pub = rospy.Publisher("airsim/pose", PoseStamped, queue_size=1) # curent position publisher current_pose_pub = rospy.Publisher("airsim/current_pose", Vector3, queue_size=1) ## Main -------------------------------------------------------------------- # connect to the AirSim simulator client = airsim.CarClient() client.confirmConnection() client.enableApiControl(True) client.simSetCameraOrientation(0, airsim.to_quaternion(0, 0, 0)) # client.simSetCameraOrientation(0, airsim.to_quaternion(-math.pi/2, 0, 0)) while not rospy.is_shutdown(): camera_info_msg = get_camera_params() simPose = get_sim_pose(client) odom_msg = convert_posestamped_to_odom_msg(simPose) rgb_msg, depth_msg = get_image_messages(client) current_pose = get_curr_pose(client) # header message simPose.header.stamp = rospy.Time.now() odom_msg.header.stamp = simPose.header.stamp camera_info_msg.header.stamp = simPose.header.stamp depth_msg.header = camera_info_msg.header # publish message current_pose_pub.publish(current_pose) pose_pub.publish(simPose) publish_tf_msg(simPose) odom_pub.publish(odom_msg) depth_cam_pub.publish(camera_info_msg) depth_pub.publish(depth_msg) # log PoseStamped message rospy.loginfo(simPose) # sleeps until next cycle rate.sleep()
def __init__(self, name="Car1"): self.client = airsim.CarClient() self.client.confirmConnection() self.client.enableApiControl(True, name) self.client.armDisarm(True) self.name = name self.image_pub = rospy.Publisher( "/%s/output/image_raw/compressed" % self.name, CompressedImage)
def __init__(self): self.client = airsim.CarClient() self.client.confirmConnection() self.client.enableApiControl(True) self.history = deque(maxlen=500) #for reversing during reset #GYM Properties (set in subclasses) self.observation_space = ['lidar', 'steer', 'img'] self.action_space = ['angle', 'speed']
def setup(): cli = airsim.CarClient() center = getPallet(cli) car_spot = getCar(cli) cli.enableApiControl(True) car_controls = airsim.CarControls() sleep(0.1) return cli, car_controls, getPallet(cli), car_spot
def connect_airsim(): # airsim related car_client = airsim.VehicleClient() car_client.confirmConnection() car_client.enableApiControl(True) # Get the current state of the vehicle c_client = airsim.CarClient() c_client.confirmConnection() return car_client, c_client
def CamMain(): # for car use CarClient() #=====PARAMETERS: PD = 1 # Prob of detection NoiseSD_x = 0 NoiseSD_y = 0 client = airsim.CarClient() client.confirmConnection() # client.enableApiControl(True, "Car") ObjStatList = client.simListSceneObjects( name_regex='SF.*') # Just cars, for testing in Neighborhood Scene # ObjStatList=client.simListSceneObjects(name_regex='Car.*|Tree.*') # Just Car, for testing in Neighborhood Scene # ObjStatList=client.simListSceneObjects(name_regex='Car.*') # Just cars, for testing in Neighborhood Scene # ObjStatList=client.simListSceneObjects() # All Objects in scene # print(ObjStatList) ObjListPos = [] ObjListOrn = [] startTime = time.time() # for idx in range(len(ObjSrcList)): # Get position and offset it, store orientation as well # if ("SM" in ObjSrcList[idx]): # ObjFilteredList.append(ObjSrcList[idx]) for jdx in range(len(ObjStatList)): tempPose = client.simGetObjectPose(ObjStatList[jdx]) # ObjListPos.append(tempPose.position-EgoPose.position) # print(jdx) # print(time.time()-startTime) print('Created Static Object List') #TODO: Create a ObjDynList that cycles though moving items #Now translate all to relative coords and create radar markers for ROS RdrMarkerPub = rospy.Publisher("radar_markers_rviz", Marker, queue_size=100) rate = rospy.Rate(20) # 10hz # print('Done') while not rospy.is_shutdown(): EgoPose = client.simGetObjectPose('Car') # print(EgoPose) EgoTrnfMat = R.from_quat([ EgoPose.orientation.x_val, EgoPose.orientation.y_val, EgoPose.orientation.z_val, EgoPose.orientation.w_val ]) EgoTrnfMat = np.vstack((np.hstack( (np.array(EgoTrnfMat.as_dcm()), np.array([ EgoPose.position.x_val, EgoPose.position.y_val, EgoPose.position.z_val ]).reshape((3, 1)))), np.array([0, 0, 0, 1]))) # print(EgoTrnfMat) ReturnPositionList = AddPosNoise( UpdateRelativePose(client, ObjStatList, EgoTrnfMat, EgoPose), PD, NoiseSD_x, NoiseSD_y) RadarMarkerPublisher(ReturnPositionList, RdrMarkerPub) rate.sleep()
def __init__(self): self.client = airsim.CarClient() self.client.confirmConnection() self.client.enableApiControl(True) self.car_controls = airsim.CarControls() self.initialCarControlSettings() self.blocked_detection = 0 self.collision_detection = 0 self.reverse_count = 0 self.last_collision_object = None