Exemplo n.º 1
0
    def on_loop(self):
        self.step += 1
        keys = pygame.key.get_pressed()
        gas = 0
        steer = 0
        restart = False
        pressed_keys = []
        if keys[K_LEFT]:
            steer = -1.
            pressed_keys.append('left')
        if keys[K_RIGHT]:
            pressed_keys.append('right')
            steer = 1.
        if keys[K_UP]:
            pressed_keys.append('up')
            gas = 1.
        if keys[K_DOWN]:
            pressed_keys.append('down')
            gas = -1.
        if keys[K_r]:
            pressed_keys.append('r')
            if time.time() - self.prev_restart_time > 2.:
                self.prev_restart_time = time.time()
                restart = True
        if time.time() - self.prev_restart_time < 2.:
            gas = 0.
            steer = 0.

        control = Control()
        control.throttle = gas
        control.steer = steer
        self.carla.sendCommand(control)
        measurements = self.carla.getMeasurements()
        pack = measurements['PlayerMeasurements']
        self.img_vec = measurements['BGRA']
        self.depth_vec = measurements['Depth']
        self.labels_vec = measurements['Labels']

        if time.time() - self.prev_time > 1.:
            print(
                'Step', self.step, 'FPS',
                float(self.step - self.prev_step) /
                (time.time() - self.prev_time))


            print('speed', pack.forward_speed, 'collision', pack.collision_other, \
            'collision_car', pack.collision_vehicles, 'colision_ped', pack.collision_pedestrians, 'pressed:', pressed_keys)
            self.prev_step = self.step
            self.prev_time = time.time()

        if restart:
            print('\n *** RESTART *** \n')

            player_pos = np.random.randint(self.num_pos)

            print('  Player pos %d \n' % (player_pos))
            self.carla.newEpisode(player_pos)
Exemplo n.º 2
0
    def on_loop(self):
        self.step += 1
        keys=pygame.key.get_pressed()
        gas = 0
        steer = 0
        restart = False
        pressed_keys = []
        if keys[K_LEFT]:
            steer = -1.
            pressed_keys.append('left')
        if keys[K_RIGHT]:
            pressed_keys.append('right')
            steer = 1.
        if keys[K_UP]:
            pressed_keys.append('up')
            gas = 1.
        if keys[K_DOWN]:
            pressed_keys.append('down')
            gas = -1.
        if keys[K_r]:
            pressed_keys.append('r')
            if time.time() - self.prev_restart_time > 2.:
                self.prev_restart_time = time.time()
                restart = True
        if time.time() - self.prev_restart_time < 2.:
            gas = 0.
            steer = 0.

        control = Control()
        control.throttle = gas
        control.steer = steer
        self.carla.sendCommand(control)
        measurements = self.carla.getMeasurements()
        pack = measurements['PlayerMeasurements']
        self.img_vec = measurements['BGRA']
        self.depth_vec = measurements['Depth']
        self.labels_vec = measurements['Labels']


        if time.time() - self.prev_time > 1.:
            print('Step', self.step, 'FPS', float(self.step - self.prev_step) / (time.time() - self.prev_time))

            
            print('speed', pack.forward_speed, 'collision', pack.collision_other, \
            'collision_car', pack.collision_vehicles, 'colision_ped', pack.collision_pedestrians, 'pressed:', pressed_keys)            
            self.prev_step = self.step
            self.prev_time = time.time()
            
        if restart:
            print('\n *** RESTART *** \n')

            player_pos = np.random.randint(self.num_pos)

            print('  Player pos %d \n' % (player_pos))
            self.carla.newEpisode(player_pos)
    def compute_action(self, sensor, speed, direction=None):

        capture_time = time.time()

        if capture_time - self._start_time > 400:

            self._target = random.randint(0, len(self.positions))
            self._start_time = time.time()

        if direction == None:
            direction = self.compute_direction((0, 0, 0), (0, 0, 0))

        sensor = sensor[self._image_cut[0]:self._image_cut[1], :, :3]
        sensor = sensor[:, :, ::-1]

        sensor = scipy.misc.imresize(sensor, [
            self._config.network_input_size[0],
            self._config.network_input_size[1]
        ])

        image_input = sensor.astype(np.float32)

        #print future_image

        #print "2"

        image_input = np.multiply(image_input, 1.0 / 255.0)

        steer, acc, brake = self._control_function(image_input, speed,
                                                   direction, self._config,
                                                   self._sess,
                                                   self._train_manager)

        control = Control()
        control.steer = steer

        if control.steer > 0.3:
            control.steer = 1.0
        elif control.steer < -0.3:
            control.steer = -1.0
        else:
            control.steer = 0.0

        control.throttle = 1.0
        control.brake = 0.0
        # print brake

        control.hand_brake = 0
        control.reverse = 0

        return control
    def run_step(self, data, target):

        rewards = data[0]
        sensor = data[2][0]
        speed = rewards.speed
        direction,made_turn,completed = self.planner.get_next_command((rewards.player_x,rewards.player_y,22),(rewards.ori_x,rewards.ori_y,rewards.ori_z),\
          (target[0],target[1],22),(1.0,0.02,-0.001))
        #pos = (rewards.player_x,rewards.player_y,22)
        #ori =(rewards.ori_x,rewards.ori_y,rewards.ori_z)
        #pos,point = self.planner.get_defined_point(pos,ori,(target[0],target[1],22),(1.0,0.02,-0.001),self._select_goal)
        #direction = convert_to_car_coord(point[0],point[1],pos[0],pos[1],ori[0],ori[1])

        capture_time = time.time()

        sensor = sensor[self._image_cut[0]:self._image_cut[1], :, :]

        sensor = scipy.misc.imresize(sensor, [
            self._config.network_input_size[0],
            self._config.network_input_size[1]
        ])

        image_input = sensor.astype(np.float32)

        #print future_image
        #print direction
        #print "2"
        image_input = np.multiply(image_input, 1.0 / 255.0)

        steer, acc, brake = self._control_function(image_input, speed,
                                                   direction, self._config,
                                                   self._sess,
                                                   self._train_manager)

        if brake < 0.1:
            brake = 0.0
        if acc > 2 * brake:
            brake = 0.0

        control = Control()
        control.steer = steer
        control.throttle = acc
        control.brake = brake

        control.hand_brake = 0
        control.reverse = 0

        #### DELETE THIS VERSION NOT COMMITABLE
        made_turn = 0
        completed = 0

        return control, made_turn, completed
Exemplo n.º 5
0
    def compute_action(self, sensor, speed):
        self._old_speed = speed
        global start_time
        """ Get Steering """
        if not self._autopilot:

            if self.joystick.get_button(buttons.steer_left):  #left
                self.steering_direction = -1
            elif self.joystick.get_button(buttons.steer_right):  #right
                self.steering_direction = 1
            else:
                self.steering_direction = 0  #when left or right button is not pressed, bring the steering to centre

            #acc_axis = self.joystick.get_axis(2)
            #brake_axis = self.joystick.get_axis(3)

            if (self.joystick.get_button(buttons.more_speed)):  #increase speed
                end_time = datetime.datetime.now()
                time_diff = (end_time -
                             start_time).microseconds / 1000  #in milliseconds
                if time_diff > 300:  #to ensure same click isnt counted multiple times
                    self._new_speed = self._old_speed + 0.7  #max speed = 7 kmph, changes in 10 steps
                    self._new_speed = min(
                        7, self._new_speed)  #restrict between 0-7
                    start_time = datetime.datetime.now()
            if (self.joystick.get_button(buttons.less_speed)):  #decrease speed
                end_time = datetime.datetime.now()
                time_diff = (end_time - start_time).microseconds / 1000
                if time_diff > 300:
                    self._new_speed = self._old_speed - 0.7
                    self._new_speed = max(0, self._new_speed)
                    start_time = datetime.datetime.now()

            if (self.joystick.get_button(buttons.rear_on)):
                self._rear = True
            if (self.joystick.get_button(buttons.rear_off)):
                self._rear = False

            control = Control()
            control.steer = self.steering_direction
            #control.throttle = -(acc_axis -1)/2.0
            if (self._new_speed - speed) > 0.05:
                control.throttle = (
                    (self._new_speed - speed) / 2.5
                ) + 0.4  # accl till carla speed nearly equal to actual speed, constant added to overcome friction
            else:
                control.throttle = 0  #if required speed is less than carla speed, do nothing. car will automatically slow down due to friction
            #control.brake = -(brake_axis -1)/2.0
            #if control.brake < 0.001:
            #	control.brake = 0.0
            control.brake = 0
            control.hand_brake = 0
            control.reverse = self._rear

        else:

            control = self._latest_data.ai_control
            if control.steer > 0.3:
                control.steer = 1.0
            elif control.steer < -0.3:
                control.steer = -1.0
            else:
                control.steer = 0.0

        return control
Exemplo n.º 6
0
def use_example(ini_file,
                port=2000,
                host='127.0.0.1',
                print_measurements=True,
                images_to_disk=False):

    frequency = 10
    rate = rospy.Rate(frequency)

    # We assume the CARLA server is already waiting for a client to connect at
    # host:port. To create a connection we can use the CARLA
    # constructor, it creates a CARLA client object and starts the
    # connection. It will throw an exception if something goes wrong.

    carla = CARLA(host, port)
    """ As a first step, Carla must have a configuration file loaded. This will load a map in the server
		with the properties specified by the ini file. It returns all the posible starting positions on the map
		in a vector.
	"""
    positions = carla.loadConfigurationFile(ini_file)
    """ 
		Ask Server for a new episode starting on position of index zero in the positions vector
	"""
    carla.newEpisode(0)

    capture = time.time()
    # General iteratior
    i = 1
    # Iterator that will go over the positions on the map after each episode
    iterator_start_positions = 1

    while not rospy.is_shutdown():
        try:
            """
				User get the measurements dictionary from the server. 
				Measurements contains:
				* WallTime: Current time on Wall from server machine.
				* GameTime: Current time on Game. Restarts at every episode
				* PlayerMeasurements : All information and events that happens to player
				* Agents : All non-player agents present on the map information such as cars positions, traffic light states
				* BRGA : BGRA optical images
				* Depth : Depth Images
				* Labels : Images containing the semantic segmentation. NOTE: the semantic segmentation must be
					previously activated on the server. See documentation for that. 

			"""

            measurements = carla.getMeasurements()

            ego_x = measurements['PlayerMeasurements'].transform.location.x
            ego_y = measurements['PlayerMeasurements'].transform.location.y
            ego_z = measurements['PlayerMeasurements'].transform.location.z
            ego_ox = measurements['PlayerMeasurements'].transform.orientation.x
            ego_oy = measurements['PlayerMeasurements'].transform.orientation.y
            ego_oz = measurements['PlayerMeasurements'].transform.orientation.z

            # Print all the measurements... Will write images to disk
            if print_measurements:
                print_pack(measurements, i, images_to_disk)
            """
				Sends a control command to the server
				This control structue contains the following fields:
				* throttle : goes from 0 to -1
				* steer : goes from -1 to 1
				* brake : goes from 0 to 1
				* hand_brake : Activate or desactivate the Hand Brake.
				* reverse: Activate or desactive the reverse gear.

			"""

            control = Control()
            control.throttle = 0.9
            control.steer = (random.random() * 2) - 1

            carla.sendCommand(control)

            rate.sleep()

            i += 1

            if i % RESET_FREQUENCY == 0:

                print('Fps for this episode : ',
                      (1.0 / ((time.time() - capture) / 100.0)))
                """ 
					Starts another new episode, the episode will have the same configuration as the previous
					one. In order to change configuration, the loadConfigurationFile could be called at any
					time.
				"""
                if iterator_start_positions < len(positions):
                    carla.newEpisode(iterator_start_positions)
                    iterator_start_positions += 1
                else:
                    carla.newEpisode(0)
                    iterator_start_positions = 1

                print("Now Starting on Position: ",
                      iterator_start_positions - 1)
                capture = time.time()

        except Exception as e:

            logging.exception('Exception raised to the top')
            time.sleep(1)
    def on_loop(self):
        self.step += 1
        keys = pygame.key.get_pressed()

        numAxes = self.js.get_numaxes()
        jsInputs = [ float(self.js.get_axis(i)) for i in range(numAxes)]

        print('Js inputs [%s]' % ', '.join(map(str, jsInputs)))
        restart = False

        control = Control()

        pressed_keys = []
        if keys[K_LEFT] or keys[K_a]:
            control.steer = -1.0
            pressed_keys.append('left')
        if keys[K_RIGHT] or keys[K_d]:
            control.steer = 1.0
            pressed_keys.append('right')
        if keys[K_UP] or keys[K_w]:
            control.throttle = 1.0
            pressed_keys.append('reverse' if self.reverse_gear else 'forward')
        if keys[K_DOWN] or keys[K_s]:
            control.brake = 1.0
            pressed_keys.append('brake')
        if keys[K_SPACE]:
            control.hand_brake = True
            pressed_keys.append('hand-brake')
        if keys[K_q]:
            self.reverse_gear = not self.reverse_gear
            pressed_keys.append('toggle reverse')
        if keys[K_r]:
            pressed_keys.append('reset')
            if time.time() - self.prev_restart_time > 2.:
                self.prev_restart_time = time.time()
                restart = True

        if time.time() - self.prev_restart_time < 2.:
            control.throttle = 0.0
            control.steer = 0.0

        control.steer = jsInputs[0]

        brakeCmd = (((jsInputs[1] - (-1)) * (1.0 - 0)) / (1.0 - (-1.0))) + 0
        throttleCmd = (((jsInputs[2] - (-1)) * (1.0 - 0)) / (1.0 - (-1.0))) + 0

        control.brake = brakeCmd
        control.throttle = throttleCmd

        control.reverse = self.reverse_gear
        self.carla.sendCommand(control)

        measurements = self.carla.getMeasurements()
        pack = measurements['PlayerMeasurements']
        self.img_vec = measurements['BGRA']
        self.depth_vec = measurements['Depth']
        self.labels_vec = measurements['Labels']

        if time.time() - self.prev_time > 1.:
            message = 'Step {step} ({fps:.1f} FPS): '
            message += '{speed:.2f} km/h, '
            message += '{other_lane:.0f}% other lane, {offroad:.0f}% off-road'
            message += ': pressed [%s]' % ', '.join(pressed_keys)

            message = message.format(
                step=self.step,
                fps=float(self.step - self.prev_step) / (time.time() - self.prev_time),
                speed=pack.forward_speed,
                other_lane=100 * pack.intersection_otherlane,
                offroad=100 * pack.intersection_offroad)

            empty_space = max(0, get_terminal_width() - len(message))
            sys.stdout.write('\r' + message + empty_space * ' ')
            sys.stdout.flush()

            self.prev_step = self.step
            self.prev_time = time.time()

        if restart:
            print('\n *** RESTART *** \n')
            player_pos = np.random.randint(self.num_pos)
            print('  Player pos %d \n' % (player_pos))
            self.carla.newEpisode(player_pos)
Exemplo n.º 8
0
def use_example(ini_file,port = 2000, host ='127.0.0.1',print_measurements =False,images_to_disk=False):

    # We assume the CARLA server is already waiting for a client to connect at
    # host:port. To create a connection we can use the CARLA
    # constructor, it creates a CARLA client object and starts the
    # connection. It will throw an exception if something goes wrong. 

	carla =CARLA(host,port)
	
	""" As a first step, Carla must have a configuration file loaded. This will load a map in the server
		with the properties specified by the ini file. It returns all the posible starting positions on the map
		in a vector.
	"""
	positions = carla.loadConfigurationFile(ini_file)

	""" 
		Ask Server for a new episode starting on position of index zero in the positions vector
	"""
	carla.newEpisode(0)
	
	capture = time.time()
	# General iteratior
	i = 1
	# Iterator that will go over the positions on the map after each episode
	iterator_start_positions = 1

	while True:
		try:
			"""
				User get the measurements dictionary from the server. 
				Measurements contains:
				* WallTime: Current time on Wall from server machine.
				* GameTime: Current time on Game. Restarts at every episode
				* PlayerMeasurements : All information and events that happens to player
				* Agents : All non-player agents present on the map information such as cars positions, traffic light states
				* BRGA : BGRA optical images
				* Depth : Depth Images
				* Labels : Images containing the semantic segmentation. NOTE: the semantic segmentation must be
					previously activated on the server. See documentation for that. 

			"""


			measurements = carla.getMeasurements()

			# Print all the measurements... Will write images to disk
			if print_measurements:
				print_pack(measurements,i,images_to_disk)

			"""
				Sends a control command to the server
				This control structue contains the following fields:
				* throttle : goes from 0 to -1
				* steer : goes from -1 to 1
				* brake : goes from 0 to 1
				* hand_brake : Activate or desactivate the Hand Brake.
				* reverse: Activate or desactive the reverse gear.

			"""

			control = Control()
			control.throttle = 0.9
			control.steer = (random.random() * 2) - 1

			carla.sendCommand(control)
	
					
			
			i+=1


			if i % RESET_FREQUENCY ==0:
					
				print ('Fps for this episode : ',(1.0/((time.time() -capture)/100.0)))

				
				""" 
					Starts another new episode, the episode will have the same configuration as the previous
					one. In order to change configuration, the loadConfigurationFile could be called at any
					time.
				"""
				if iterator_start_positions < len(positions):
					carla.newEpisode(iterator_start_positions)
					iterator_start_positions+=1
				else :
					carla.newEpisode(0)
					iterator_start_positions = 1

				print("Now Starting on Position: ",iterator_start_positions-1)
				capture = time.time()


		except Exception as e:

			logging.exception('Exception raised to the top')
			time.sleep(1)
Exemplo n.º 9
0
    def main_loop(self):
        # Carla Setup
        carla = CARLA(self.host, self.port)
        positions = carla.loadConfigurationFile(self.ini_file)
        carla.newEpisode(0)
        capture = time.time()
        i = 1
        # Iterator that will go over the positions on the map after each episode
        iterator_start_positions = 1

        rate = rospy.Rate(ROS_FREQUENCY)
        while not rospy.is_shutdown():
            try:
                """
					User get the measurements dictionary from the server. 
					Measurements contains:
					* WallTime: Current time on Wall from server machine.
					* GameTime: Current time on Game. Restarts at every episode
					* PlayerMeasurements : All information and events that happens to player
					* Agents : All non-player agents present on the map information such as cars positions, traffic light states
					* BRGA : BGRA optical images
					* Depth : Depth Images
					* Labels : Images containing the semantic segmentation. NOTE: the semantic segmentation must be
						previously activated on the server. See documentation for that. 

				"""
                measurements = carla.getMeasurements()
                self.measurements_process(measurements)
                self.measurements_publish()
                """
					Sends a control command to the server
					This control structue contains the following fields:
					* throttle : goes from 0 to 1
					* steer : goes from -1 to 1
					* brake : goes from 0 to 1
					* hand_brake : Activate or desactivate the Hand Brake.
					* reverse: Activate or desactive the reverse gear.

				"""

                control = Control()
                control.throttle = 0.9
                control.steer = 0
                carla.sendCommand(control)
                rate.sleep()

                i += 1

                if i % RESET_FREQUENCY == 0:

                    print('Fps for this episode : ',
                          (1.0 / ((time.time() - capture) / 100.0)))
                    """ 
						Starts another new episode, the episode will have the same configuration as the previous
						one. In order to change configuration, the loadConfigurationFile could be called at any
						time.
					"""
                    if iterator_start_positions < len(positions):
                        carla.newEpisode(iterator_start_positions)
                        iterator_start_positions += 1
                    else:
                        carla.newEpisode(0)
                        iterator_start_positions = 1

                    print("Now Starting on Position: ",
                          iterator_start_positions - 1)
                    capture = time.time()

            except KeyboardInterrupt:
                pass