Best Python code snippet using autotest_python
carla_ego_vehicle.py
Source:carla_ego_vehicle.py  
...86        """87        # Get vehicle blueprint.88        blueprint = secure_random.choice(89            self.world.get_blueprint_library().filter(self.actor_filter))90        blueprint.set_attribute('role_name', "{}".format(self.role_name))91        if blueprint.has_attribute('color'):92            color = secure_random.choice(blueprint.get_attribute('color').recommended_values)93            blueprint.set_attribute('color', color)94        # Spawn the vehicle.95        if not rospy.get_param('~spawn_ego_vehicle'):96            actors = self.world.get_actors().filter(self.actor_filter)97            for actor in actors:98                if actor.attributes['role_name'] == self.role_name:99                    self.player = actor100                    break101        else:102            if self.actor_spawnpoint:103                spawn_point = carla.Transform()104                spawn_point.location.x = self.actor_spawnpoint.position.x105                spawn_point.location.y = -self.actor_spawnpoint.position.y106                spawn_point.location.z = self.actor_spawnpoint.position.z + \107                    2  # spawn 2m above ground108                quaternion = (109                    self.actor_spawnpoint.orientation.x,110                    self.actor_spawnpoint.orientation.y,111                    self.actor_spawnpoint.orientation.z,112                    self.actor_spawnpoint.orientation.w113                )114                _, _, yaw = euler_from_quaternion(quaternion)115                spawn_point.rotation.yaw = -math.degrees(yaw)116                rospy.loginfo("Spawn {} at x={} y={} z={} yaw={}".format(self.role_name,117                                                                         spawn_point.location.x,118                                                                         spawn_point.location.y,119                                                                         spawn_point.location.z,120                                                                         spawn_point.rotation.yaw))121                if self.player is not None:122                    self.player.set_transform(spawn_point)123                while self.player is None:124                    self.player = self.world.try_spawn_actor(blueprint, spawn_point)125                    self.player_created = True126            else:127                if self.player is not None:128                    spawn_point = self.player.get_transform()129                    spawn_point.location.z += 2.0130                    spawn_point.rotation.roll = 0.0131                    spawn_point.rotation.pitch = 0.0132                    self.player.set_transform(spawn_point)133                while self.player is None:134                    spawn_points = self.world.get_map().get_spawn_points()135                    spawn_point = secure_random.choice(136                        spawn_points) if spawn_points else carla.Transform()137                    self.player = self.world.try_spawn_actor(blueprint, spawn_point)138                    self.player_created = True139        if self.player_created:140            # Read sensors from file141            if not os.path.exists(self.sensor_definition_file):142                raise RuntimeError(143                    "Could not read sensor-definition from {}".format(self.sensor_definition_file))144            json_sensors = None145            with open(self.sensor_definition_file) as handle:146                json_sensors = json.loads(handle.read())147            # Set up the sensors148            self.sensor_actors = self.setup_sensors(json_sensors["sensors"])149            self.player_created = False150    def setup_sensors(self, sensors):151        """152        Create the sensors defined by the user and attach them to the ego-vehicle153        :param sensors: list of sensors154        :return:155        """156        actors = []157        bp_library = self.world.get_blueprint_library()158        for sensor_spec in sensors:159            try:160                bp = bp_library.find(str(sensor_spec['type']))161                bp.set_attribute('role_name', str(sensor_spec['id']))162                if sensor_spec['type'].startswith('sensor.camera'):163                    bp.set_attribute('image_size_x', str(sensor_spec['width']))164                    bp.set_attribute('image_size_y', str(sensor_spec['height']))165                    bp.set_attribute('fov', str(sensor_spec['fov']))166                    try:167                        bp.set_attribute('sensor_tick', str(sensor_spec['sensor_tick']))168                    except KeyError:169                        pass170                    sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],171                                                     z=sensor_spec['z'])172                    sensor_rotation = carla.Rotation(pitch=sensor_spec['pitch'],173                                                     roll=sensor_spec['roll'],174                                                     yaw=sensor_spec['yaw'])175                    if sensor_spec['type'].startswith('sensor.camera.rgb'):176                        bp.set_attribute('gamma', str(sensor_spec['gamma']))177                        bp.set_attribute('shutter_speed', str(sensor_spec['shutter_speed']))178                        bp.set_attribute('iso', str(sensor_spec['iso']))179                        bp.set_attribute('fstop', str(sensor_spec['fstop']))180                        bp.set_attribute('min_fstop', str(sensor_spec['min_fstop']))181                        bp.set_attribute('blade_count', str(sensor_spec['blade_count']))182                        bp.set_attribute('exposure_mode', str(sensor_spec['exposure_mode']))183                        bp.set_attribute('exposure_compensation', str(184                            sensor_spec['exposure_compensation']))185                        bp.set_attribute('exposure_min_bright', str(186                            sensor_spec['exposure_min_bright']))187                        bp.set_attribute('exposure_max_bright', str(188                            sensor_spec['exposure_max_bright']))189                        bp.set_attribute('exposure_speed_up', str(sensor_spec['exposure_speed_up']))190                        bp.set_attribute('exposure_speed_down', str(191                            sensor_spec['exposure_speed_down']))192                        bp.set_attribute('calibration_constant', str(193                            sensor_spec['calibration_constant']))194                        bp.set_attribute('focal_distance', str(sensor_spec['focal_distance']))195                        bp.set_attribute('blur_amount', str(sensor_spec['blur_amount']))196                        bp.set_attribute('blur_radius', str(sensor_spec['blur_radius']))197                        bp.set_attribute('motion_blur_intensity', str(198                            sensor_spec['motion_blur_intensity']))199                        bp.set_attribute('motion_blur_max_distortion', str(200                            sensor_spec['motion_blur_max_distortion']))201                        bp.set_attribute('motion_blur_min_object_screen_size', str(202                            sensor_spec['motion_blur_min_object_screen_size']))203                        bp.set_attribute('slope', str(sensor_spec['slope']))204                        bp.set_attribute('toe', str(sensor_spec['toe']))205                        bp.set_attribute('shoulder', str(sensor_spec['shoulder']))206                        bp.set_attribute('black_clip', str(sensor_spec['black_clip']))207                        bp.set_attribute('white_clip', str(sensor_spec['white_clip']))208                        bp.set_attribute('temp', str(sensor_spec['temp']))209                        bp.set_attribute('tint', str(sensor_spec['tint']))210                        bp.set_attribute('chromatic_aberration_intensity', str(211                            sensor_spec['chromatic_aberration_intensity']))212                        bp.set_attribute('chromatic_aberration_offset', str(213                            sensor_spec['chromatic_aberration_offset']))214                        bp.set_attribute('enable_postprocess_effects', str(215                            sensor_spec['enable_postprocess_effects']))216                        bp.set_attribute('lens_circle_falloff', str(217                            sensor_spec['lens_circle_falloff']))218                        bp.set_attribute('lens_circle_multiplier', str(219                            sensor_spec['lens_circle_multiplier']))220                        bp.set_attribute('lens_k', str(sensor_spec['lens_k']))221                        bp.set_attribute('lens_kcube', str(sensor_spec['lens_kcube']))222                        bp.set_attribute('lens_x_size', str(sensor_spec['lens_x_size']))223                        bp.set_attribute('lens_y_size', str(sensor_spec['lens_y_size']))224                elif sensor_spec['type'].startswith('sensor.lidar'):225                    bp.set_attribute('range', str(sensor_spec['range']))226                    bp.set_attribute('rotation_frequency', str(sensor_spec['rotation_frequency']))227                    bp.set_attribute('channels', str(sensor_spec['channels']))228                    bp.set_attribute('upper_fov', str(sensor_spec['upper_fov']))229                    bp.set_attribute('lower_fov', str(sensor_spec['lower_fov']))230                    bp.set_attribute('points_per_second', str(sensor_spec['points_per_second']))231                    try:232                        bp.set_attribute('sensor_tick', str(sensor_spec['sensor_tick']))233                    except KeyError:234                        pass235                    sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],236                                                     z=sensor_spec['z'])237                    sensor_rotation = carla.Rotation(pitch=sensor_spec['pitch'],238                                                     roll=sensor_spec['roll'],239                                                     yaw=sensor_spec['yaw'])240                elif sensor_spec['type'].startswith('sensor.other.gnss'):241                    sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],242                                                     z=sensor_spec['z'])243                    sensor_rotation = carla.Rotation()244                    bp.set_attribute('noise_alt_stddev', str(sensor_spec['noise_alt_stddev']))245                    bp.set_attribute('noise_lat_stddev', str(sensor_spec['noise_lat_stddev']))246                    bp.set_attribute('noise_lon_stddev', str(sensor_spec['noise_lon_stddev']))247                    bp.set_attribute('noise_alt_bias', str(sensor_spec['noise_alt_bias']))248                    bp.set_attribute('noise_lat_bias', str(sensor_spec['noise_lat_bias']))249                    bp.set_attribute('noise_lon_bias', str(sensor_spec['noise_lon_bias']))250                elif sensor_spec['type'].startswith('sensor.other.imu'):251                    sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],252                                                     z=sensor_spec['z'])253                    sensor_rotation = carla.Rotation(pitch=sensor_spec['pitch'],254                                                     roll=sensor_spec['roll'],255                                                     yaw=sensor_spec['yaw'])256                    bp.set_attribute('noise_accel_stddev_x',257                                     str(sensor_spec['noise_accel_stddev_x']))258                    bp.set_attribute('noise_accel_stddev_y',259                                     str(sensor_spec['noise_accel_stddev_y']))260                    bp.set_attribute('noise_accel_stddev_z',261                                     str(sensor_spec['noise_accel_stddev_z']))262                    bp.set_attribute('noise_gyro_stddev_x', str(sensor_spec['noise_gyro_stddev_x']))263                    bp.set_attribute('noise_gyro_stddev_y', str(sensor_spec['noise_gyro_stddev_y']))264                    bp.set_attribute('noise_gyro_stddev_z', str(sensor_spec['noise_gyro_stddev_z']))265                    bp.set_attribute('noise_gyro_bias_x', str(sensor_spec['noise_gyro_bias_x']))266                    bp.set_attribute('noise_gyro_bias_y', str(sensor_spec['noise_gyro_bias_y']))267                    bp.set_attribute('noise_gyro_bias_z', str(sensor_spec['noise_gyro_bias_z']))268                elif sensor_spec['type'].startswith('sensor.other.radar'):269                    sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],270                                                     z=sensor_spec['z'])271                    sensor_rotation = carla.Rotation(pitch=sensor_spec['pitch'],272                                                     roll=sensor_spec['roll'],273                                                     yaw=sensor_spec['yaw'])274                    bp.set_attribute('horizontal_fov', str(sensor_spec['horizontal_fov']))275                    bp.set_attribute('vertical_fov', str(sensor_spec['vertical_fov']))276                    bp.set_attribute('points_per_second', str(sensor_spec['points_per_second']))277                    bp.set_attribute('range', str(sensor_spec['range']))278            except KeyError as e:279                rospy.logfatal(280                    "Sensor will not be spawned, because sensor spec is invalid: '{}'".format(e))281                continue282            # create sensor283            sensor_transform = carla.Transform(sensor_location, sensor_rotation)284            sensor = self.world.spawn_actor(bp, sensor_transform,285                                            attach_to=self.player)286            actors.append(sensor)287        return actors288    @abstractmethod289    def sensors(self):290        """291        return a list of sensors attached...carla_infrastructure.py
Source:carla_infrastructure.py  
...51        bp_library = self.world.get_blueprint_library()52        for sensor_spec in sensors:53            try:54                bp = bp_library.find(str(sensor_spec['type']))55                bp.set_attribute('role_name', str(sensor_spec['id']))56                if sensor_spec['type'].startswith('sensor.camera'):57                    bp.set_attribute('image_size_x', str(sensor_spec['width']))58                    bp.set_attribute(59                        'image_size_y', str(sensor_spec['height']))60                    bp.set_attribute('fov', str(sensor_spec['fov']))61                    bp.set_attribute('gamma', str(sensor_spec['gamma']))62                    bp.set_attribute('shutter_speed', str(63                        sensor_spec['shutter_speed']))64                    bp.set_attribute('iso', str(sensor_spec['iso']))65                    bp.set_attribute('fstop', str(sensor_spec['fstop']))66                    bp.set_attribute('min_fstop', str(67                        sensor_spec['min_fstop']))68                    bp.set_attribute('blade_count', str(69                        sensor_spec['blade_count']))70                    bp.set_attribute('exposure_mode', str(71                        sensor_spec['exposure_mode']))72                    bp.set_attribute('exposure_compensation', str(73                        sensor_spec['exposure_compensation']))74                    bp.set_attribute('exposure_min_bright', str(75                        sensor_spec['exposure_min_bright']))76                    bp.set_attribute('exposure_max_bright', str(77                        sensor_spec['exposure_max_bright']))78                    bp.set_attribute('exposure_speed_up', str(79                        sensor_spec['exposure_speed_up']))80                    bp.set_attribute('exposure_speed_down', str(81                        sensor_spec['exposure_speed_down']))82                    bp.set_attribute('calibration_constant', str(83                        sensor_spec['calibration_constant']))84                    bp.set_attribute('focal_distance', str(85                        sensor_spec['focal_distance']))86                    bp.set_attribute('blur_amount', str(87                        sensor_spec['blur_amount']))88                    bp.set_attribute('blur_radius', str(89                        sensor_spec['blur_radius']))90                    bp.set_attribute('motion_blur_intensity', str(91                        sensor_spec['motion_blur_intensity']))92                    bp.set_attribute('motion_blur_max_distortion', str(93                        sensor_spec['motion_blur_max_distortion']))94                    bp.set_attribute('motion_blur_min_object_screen_size', str(95                        sensor_spec['motion_blur_min_object_screen_size']))96                    bp.set_attribute('slope', str(sensor_spec['slope']))97                    bp.set_attribute('toe', str(sensor_spec['toe']))98                    bp.set_attribute('shoulder', str(sensor_spec['shoulder']))99                    bp.set_attribute('black_clip', str(100                        sensor_spec['black_clip']))101                    bp.set_attribute('white_clip', str(102                        sensor_spec['white_clip']))103                    bp.set_attribute('temp', str(sensor_spec['temp']))104                    bp.set_attribute('tint', str(sensor_spec['tint']))105                    bp.set_attribute('chromatic_aberration_intensity', str(106                        sensor_spec['chromatic_aberration_intensity']))107                    bp.set_attribute('chromatic_aberration_offset', str(108                        sensor_spec['chromatic_aberration_offset']))109                    bp.set_attribute('enable_postprocess_effects', str(110                        sensor_spec['enable_postprocess_effects']))111                    bp.set_attribute('lens_circle_falloff', str(112                        sensor_spec['lens_circle_falloff']))113                    bp.set_attribute('lens_circle_multiplier', str(114                        sensor_spec['lens_circle_multiplier']))115                    bp.set_attribute('lens_k', str(sensor_spec['lens_k']))116                    bp.set_attribute('lens_kcube', str(117                        sensor_spec['lens_kcube']))118                    bp.set_attribute('lens_x_size', str(119                        sensor_spec['lens_x_size']))120                    bp.set_attribute('lens_y_size', str(121                        sensor_spec['lens_y_size']))122                    sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],123                                                     z=sensor_spec['z'])124                    sensor_rotation = carla.Rotation(pitch=sensor_spec['pitch'],125                                                     roll=sensor_spec['roll'],126                                                     yaw=sensor_spec['yaw'])127                elif sensor_spec['type'].startswith('sensor.lidar'):128                    bp.set_attribute('range', str(sensor_spec['range']))129                    bp.set_attribute('rotation_frequency', str(sensor_spec['rotation_frequency']))130                    bp.set_attribute('channels', str(sensor_spec['channels']))131                    bp.set_attribute('upper_fov', str(sensor_spec['upper_fov']))132                    bp.set_attribute('lower_fov', str(sensor_spec['lower_fov']))133                    bp.set_attribute('points_per_second', str(sensor_spec['points_per_second']))134                    sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],135                                                     z=sensor_spec['z'])136                    sensor_rotation = carla.Rotation(pitch=sensor_spec['pitch'],137                                                     roll=sensor_spec['roll'],138                                                     yaw=sensor_spec['yaw'])139                elif sensor_spec['type'].startswith('sensor.other.gnss'):140                    sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],141                                                     z=sensor_spec['z'])142                    sensor_rotation = carla.Rotation()143                elif sensor_spec['type'].startswith('sensor.other.imu'):144                    sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],145                                                     z=sensor_spec['z'])146                    sensor_rotation = carla.Rotation()147                elif sensor_spec['type'].startswith('sensor.other.radar'):148                    sensor_location = carla.Location(x=sensor_spec['x'], y=sensor_spec['y'],149                                                     z=sensor_spec['z'])150                    sensor_rotation = carla.Rotation(pitch=sensor_spec['pitch'],151                                                     roll=sensor_spec['roll'],152                                                     yaw=sensor_spec['yaw'])153                    bp.set_attribute('horizontal_fov', str(sensor_spec['horizontal_fov']))154                    bp.set_attribute('vertical_fov', str(sensor_spec['vertical_fov']))155                    bp.set_attribute('points_per_second', str(sensor_spec['points_per_second']))156                    bp.set_attribute('range', str(sensor_spec['range']))157            except KeyError as e:158                rospy.logfatal(159                    "Sensor will not be spawned, because sensor spec is invalid: '{}'".format(e))160                continue161            # create sensor162            sensor_transform = carla.Transform(sensor_location, sensor_rotation)163            sensor = self.world.spawn_actor(bp, sensor_transform)164            actors.append(sensor)165        return actors166    def destroy(self):167        """168        destroy the current ego vehicle and its sensors169        """170        for i, _ in enumerate(self.sensor_actors):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
