示例#1
0
    def act(self, action: Union[dict, str] = None):
        """
        Execute an action.

        For now, no action is supported because the vehicle takes all decisions
        of acceleration and lane changes on its own, based on the IDM and MOBIL models.

        :param action: the action
        """
        if self.crashed:
            return
        action = {}
        front_vehicle, rear_vehicle = self.road.neighbour_vehicles(self)
        # Lateral: MOBIL
        self.follow_road()
        if self.enable_lane_change:
            self.change_lane_policy()
        action['steering'] = self.steering_control(self.target_lane_index)
        action['steering'] = np.clip(action['steering'],
                                     -self.MAX_STEERING_ANGLE,
                                     self.MAX_STEERING_ANGLE)

        # Longitudinal: IDM
        action['acceleration'] = self.acceleration(ego_vehicle=self,
                                                   front_vehicle=front_vehicle,
                                                   rear_vehicle=rear_vehicle)
        # action['acceleration'] = self.recover_from_stop(action['acceleration'])
        action['acceleration'] = np.clip(action['acceleration'], -self.ACC_MAX,
                                         self.ACC_MAX)
        Vehicle.act(
            self, action
        )  # Skip ControlledVehicle.act(), or the command will be overriden.
示例#2
0
    def dynamics_event(cls, vehicle: Vehicle,
                       event: pygame.event.EventType) -> None:
        """
            Map the pygame keyboard events to dynamics actuation

        :param vehicle: the vehicle receiving the event
        :param event: the pygame event
        """
        action = vehicle.action.copy()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                action['steering'] = 45 * np.pi / 180
            if event.key == pygame.K_LEFT:
                action['steering'] = -45 * np.pi / 180
            if event.key == pygame.K_DOWN:
                action['acceleration'] = -6
            if event.key == pygame.K_UP:
                action['acceleration'] = 5
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                action['steering'] = 0
            if event.key == pygame.K_LEFT:
                action['steering'] = 0
            if event.key == pygame.K_DOWN:
                action['acceleration'] = 0
            if event.key == pygame.K_UP:
                action['acceleration'] = 0
        if action != vehicle.action:
            vehicle.act(action)
示例#3
0
def test_brake():
    v = Vehicle(road=None, position=[0, 0], velocity=20, heading=0)
    for _ in range(10 * FPS):
        v.act({
            'acceleration': min(max(-1 * v.velocity, -6), 6),
            'steering': 0
        })
        v.step(dt=1 / FPS)
    assert v.velocity == pytest.approx(0, abs=0.01)
示例#4
0
def test_act():
    v = Vehicle(road=None, position=[0, 0], speed=20, heading=0)
    v.act({'acceleration': 1, 'steering': 0})
    for _ in range(1 * FPS):
        v.step(dt=1 / FPS)
    assert v.speed == pytest.approx(21)

    v.act({'acceleration': 0, 'steering': 0.5})
    for _ in range(1 * FPS):
        v.step(dt=1 / FPS)
    assert v.speed == pytest.approx(21)
    assert v.position[1] > 0
示例#5
0
    def act(self, action: Union[dict, str] = None):
        """
        Execute an action.

        For now, no action is supported because the vehicle takes all decisions
        of acceleration and lane changes on its own, based on the IDM and MOBIL models.

        :param action: the action
        """
        if self.crashed:
            return
        action = {}
        # Lateral: MOBIL
        self.follow_road()
        if self.enable_lane_change:
            self.change_lane_policy()
        action['steering'] = self.steering_control(self.target_lane_index)
        action['steering'] = np.clip(action['steering'],
                                     -self.MAX_STEERING_ANGLE,
                                     self.MAX_STEERING_ANGLE)
        action['steering'] = 0
        # if self.sadism:
        #   if np.abs(self.position[0]-self.road.vehicles[0].position[0])<5:
        #     if self.road.vehicles[0].position[1]-self.position[1]<0:
        #       action['steering']=-0.01
        #     elif self.road.vehicles[0].position[1]-self.position[1]>0:
        #       action['steering']=0.01

        # Longitudinal: IDM
        front_vehicle, rear_vehicle = self.road.neighbour_vehicles(
            self, self.lane_index)
        action['acceleration'] = self.acceleration(ego_vehicle=self,
                                                   front_vehicle=front_vehicle,
                                                   rear_vehicle=rear_vehicle)
        # When changing lane, check both current and target lanes
        if self.lane_index != self.target_lane_index:
            front_vehicle, rear_vehicle = self.road.neighbour_vehicles(
                self, self.target_lane_index)
            target_idm_acceleration = self.acceleration(
                ego_vehicle=self,
                front_vehicle=front_vehicle,
                rear_vehicle=rear_vehicle)
            action['acceleration'] = min(action['acceleration'],
                                         target_idm_acceleration)
        # action['acceleration'] = self.recover_from_stop(action['acceleration'])

        action['acceleration'] = 0
        # else:
        #     action['acceleration'] = np.clip(action['acceleration'], -self.ACC_MAX, self.ACC_MAX)
        Vehicle.act(
            self, action
        )  # Skip ControlledVehicle.act(), or the command will be overriden.
示例#6
0
    def control_event(cls, vehicle: Vehicle,
                      event: pygame.event.EventType) -> None:
        """
            Map the pygame keyboard events to control decisions

        :param vehicle: the vehicle receiving the event
        :param event: the pygame event
        """
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                vehicle.act("FASTER")
            if event.key == pygame.K_LEFT:
                vehicle.act("SLOWER")
            if event.key == pygame.K_DOWN:
                vehicle.act("LANE_RIGHT")
            if event.key == pygame.K_UP:
                vehicle.act("LANE_LEFT")