Beispiel #1
0
    def _init_controller(self, opt_dict):
        """
        Controller initialization.

        :param opt_dict: dictionary of arguments.
        :return:
        """
        # default params
        self._dt = 1.0 / 20.0
        self._default_target_speed = 20.0  # Km/h
        self._target_speed = 20.0  # Km/h
        self._sampling_radius = self._target_speed * 1 / 3.6  # 1 seconds horizon
        self._min_distance = self._sampling_radius * self.MIN_DISTANCE_PERCENTAGE
        args_lateral_dict = {
            'K_P': 1.95,
            'K_D': 0.01,
            'K_I': 1.4,
            'dt': self._dt
        }
        args_longitudinal_dict = {
            'K_P': 1.0,
            'K_D': 0,
            'K_I': 1,
            'dt': self._dt
        }

        # parameters overload
        if opt_dict:
            if 'dt' in opt_dict:
                self._dt = opt_dict['dt']
            if 'target_speed' in opt_dict:
                self._target_speed = opt_dict['target_speed']
            if 'sampling_radius' in opt_dict:
                self._sampling_radius = self._target_speed * \
                                        opt_dict['sampling_radius'] / 3.6
            if 'lateral_control_dict' in opt_dict:
                args_lateral_dict = opt_dict['lateral_control_dict']
            if 'longitudinal_control_dict' in opt_dict:
                args_longitudinal_dict = opt_dict['longitudinal_control_dict']

        self._current_waypoint = self._map.get_waypoint(
            self._vehicle.get_location())
        self._vehicle_controller = VehiclePIDController(
            self._vehicle,
            args_lateral=args_lateral_dict,
            args_longitudinal=args_longitudinal_dict)

        self._global_plan = False

        # compute initial waypoints
        self._waypoints_queue.append(
            (self._current_waypoint.next(self._sampling_radius)[0],
             RoadOption.LANEFOLLOW))

        self._target_road_option = RoadOption.LANEFOLLOW
        # fill waypoint trajectory queue
        self._compute_next_waypoints(k=200)
Beispiel #2
0
    def _init_controller(self, opt_dict):
        # default params
        self._dt = 1.0 / 20.0
        # self._switch_timestep = 0
        self._target_speed = 20.0  # Km/h
        self._sampling_radius = self._target_speed * 1 / 3.6  # 1 seconds horizon
        self._min_distance = self._sampling_radius * self.MIN_DISTANCE_PERCENTAGE
        args_lateral_dict = {
            'K_P': 0.5,  # Keyur: 0.5  Local_planner: 1.95  Traffic_manager: 10
            'K_D': 0,  # Keyur: 0.01  Local_planner: 0.2  Traffic_manager: 0
            'K_I':
            0.1,  # Keyur: 1.4  Local_planner: 0.07  Traffic_manager: 0.1
            'dt': self._dt
        }
        args_longitudinal_dict = {
            'K_P': 0.1,  # Keyur: 1.0  Local_planner: 1.0  Traffic_manager: 5.0
            'K_D': 0.15,  # Keyur: 0  Local_planner: 0  Traffic_manager: 0
            'K_I': 0.01,  # Keyur: 1  Local_planner: 0.05  Traffic_manager: 0.1
            'dt': self._dt
        }

        # parameters overload
        if opt_dict:
            if 'dt' in opt_dict:
                self._dt = opt_dict['dt']
            if 'target_speed' in opt_dict:
                self._target_speed = opt_dict['target_speed']
            if 'sampling_radius' in opt_dict:
                self._sampling_radius = self._target_speed * \
                                        opt_dict['sampling_radius'] / 3.6
            if 'lateral_control_dict' in opt_dict:
                args_lateral_dict = opt_dict['lateral_control_dict']
            if 'longitudinal_control_dict' in opt_dict:
                args_longitudinal_dict = opt_dict['longitudinal_control_dict']

        self._current_waypoint = self._map.get_waypoint(
            self._vehicle.get_location())
        self._vehicle_controller = VehiclePIDController(
            self._vehicle,
            args_lateral=args_lateral_dict,
            args_longitudinal=args_longitudinal_dict)

        # compute initial waypoints
        self._waypoints_queue.append(
            (self._current_waypoint.next(self._sampling_radius)[0],
             RoadOption.LANEFOLLOW))

        self._target_road_option = RoadOption.LANEFOLLOW
        # fill waypoint trajectory queue
        self._compute_next_waypoints(k=200)
    def Control(self):
        rate = rospy.Rate(10)
        lon_param = {
            'K_P': 0.5,
            'K_I': 0.5,
            'K_D': 0
        }  # Set PID values for longitudinal controller
        lat_param = {
            'K_P': 0.5,
            'K_I': 0.3,
            'K_D': 0
        }  # Set PID values for lateral controller
        vehicle_controller = VehiclePIDController(
            self.vehicle, lon_param,
            lat_param)  # Calling vehicle controller class from controller.py

        i = 0
        for k in range(1, len(self.current_route)
                       ):  # Iterate through all the waypoints in the route
            self.Distance(self.current_route[i][0], self.veh_pos.transform)
            self.Waypoints()
            rospy.Subscriber(
                '/machine_learning/output', Int16, self.Detection
            )  # Subscribes to topic for Stop sign detection. Need to run carla_detect_objects.py script to obtain detection
            while self.distance > 0.5:  # Control the vehicle until the distance of the next waypoint and the vehicle is less than 0.5 m
                self.Actor(
                )  # Call Actor function to update vehicle's location
                control = vehicle_controller.run_step(
                    15, self.current_route[i]
                    [0])  # Feeds the controller the waypoints one by one
                self.velocity = self.vehicle.get_velocity(
                )  # Get vehicle velocity

                if self.detection == 11:  # Stop sign detection(apply brakes). Our ML has a class ID of 11 for stop signs
                    print('Object detected, apply brakes')
                    msg = CarlaEgoVehicleControl(
                    )  # Ego vehicle's control message
                    msg.throttle = 0
                    msg.steer = control.steer
                    msg.brake = 1
                    msg.hand_brake = control.hand_brake
                    msg.reverse = control.reverse
                    msg.gear = 1
                    msg.manual_gear_shift = control.manual_gear_shift
                    self.detection = None

                elif len(self.current_route) - 5 <= k <= len(
                        self.current_route
                ):  # If the ith waypoint is between the last waypoint minus five apply brakes
                    msg = CarlaEgoVehicleControl()
                    msg.throttle = 0
                    msg.steer = control.steer
                    msg.brake = 1
                    msg.hand_brake = control.hand_brake
                    msg.reverse = control.reverse
                    msg.gear = 1
                    msg.manual_gear_shift = control.manual_gear_shift
                    print('You arrived to your destination!!')

                else:  # If neither scenario happen, keep driving
                    msg = CarlaEgoVehicleControl()
                    msg.throttle = control.throttle
                    msg.steer = control.steer
                    msg.brake = control.brake
                    msg.hand_brake = control.hand_brake
                    msg.reverse = control.reverse
                    msg.gear = 1
                    msg.manual_gear_shift = control.manual_gear_shift

                self.Publisher(msg)
                rate.sleep()
                self.Distance(
                    self.current_route[i][0], self.veh_pos.transform
                )  # Calculates the Euclidean distance between the vehicle and the next waypoint in every iteration
            i += 1