Basic implementation of a synchronous client.
| 219 | |
| 220 | |
| 221 | class BasicSynchronousClient(object): |
| 222 | """ |
| 223 | Basic implementation of a synchronous client. |
| 224 | """ |
| 225 | |
| 226 | def __init__(self): |
| 227 | self.client = None |
| 228 | self.world = None |
| 229 | self.camera = None |
| 230 | self.car = None |
| 231 | |
| 232 | self.display = None |
| 233 | self.image = None |
| 234 | self.capture = True |
| 235 | |
| 236 | def camera_blueprint(self): |
| 237 | """ |
| 238 | Returns camera blueprint. |
| 239 | """ |
| 240 | |
| 241 | camera_bp = self.world.get_blueprint_library().find('sensor.camera.rgb') |
| 242 | camera_bp.set_attribute('image_size_x', str(VIEW_WIDTH)) |
| 243 | camera_bp.set_attribute('image_size_y', str(VIEW_HEIGHT)) |
| 244 | camera_bp.set_attribute('fov', str(VIEW_FOV)) |
| 245 | return camera_bp |
| 246 | |
| 247 | def set_synchronous_mode(self, synchronous_mode): |
| 248 | """ |
| 249 | Sets synchronous mode. |
| 250 | """ |
| 251 | |
| 252 | settings = self.world.get_settings() |
| 253 | settings.synchronous_mode = synchronous_mode |
| 254 | self.world.apply_settings(settings) |
| 255 | |
| 256 | def setup_car(self): |
| 257 | """ |
| 258 | Spawns actor-vehicle to be controled. |
| 259 | """ |
| 260 | |
| 261 | car_bp = self.world.get_blueprint_library().filter('vehicle.*')[0] |
| 262 | location = random.choice(self.world.get_map().get_spawn_points()) |
| 263 | self.car = self.world.spawn_actor(car_bp, location) |
| 264 | |
| 265 | def setup_camera(self): |
| 266 | """ |
| 267 | Spawns actor-camera to be used to render view. |
| 268 | Sets calibration for client-side boxes rendering. |
| 269 | """ |
| 270 | |
| 271 | camera_transform = carla.Transform(carla.Location(x=-5.5, z=2.8), carla.Rotation(pitch=-15)) |
| 272 | self.camera = self.world.spawn_actor(self.camera_blueprint(), camera_transform, attach_to=self.car) |
| 273 | weak_self = weakref.ref(self) |
| 274 | self.camera.listen(lambda image: weak_self().set_image(weak_self, image)) |
| 275 | |
| 276 | calibration = np.identity(3) |
| 277 | calibration[0, 2] = VIEW_WIDTH / 2.0 |
| 278 | calibration[1, 2] = VIEW_HEIGHT / 2.0 |