Class the sensors listen to in order to receive their data each frame
| 16 | |
| 17 | |
| 18 | class CallBack(object): |
| 19 | |
| 20 | """ |
| 21 | Class the sensors listen to in order to receive their data each frame |
| 22 | """ |
| 23 | |
| 24 | def __init__(self, tag, sensor, data_provider): |
| 25 | """ |
| 26 | Initializes the call back |
| 27 | """ |
| 28 | self._tag = tag |
| 29 | self._data_provider = data_provider |
| 30 | |
| 31 | self._data_provider.register_sensor(tag, sensor) |
| 32 | |
| 33 | def __call__(self, data): |
| 34 | """ |
| 35 | call function |
| 36 | """ |
| 37 | if isinstance(data, carla.Image): |
| 38 | self._parse_image_cb(data, self._tag) |
| 39 | elif isinstance(data, carla.LidarMeasurement): |
| 40 | self._parse_lidar_cb(data, self._tag) |
| 41 | elif isinstance(data, carla.GnssMeasurement): |
| 42 | self._parse_gnss_cb(data, self._tag) |
| 43 | else: |
| 44 | logging.error('No callback method for this sensor.') |
| 45 | |
| 46 | # Parsing CARLA physical Sensors |
| 47 | def _parse_image_cb(self, image, tag): |
| 48 | """ |
| 49 | parses cameras |
| 50 | """ |
| 51 | array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8")) |
| 52 | array = copy.deepcopy(array) |
| 53 | array = np.reshape(array, (image.height, image.width, 4)) |
| 54 | self._data_provider.update_sensor(tag, array, image.frame) |
| 55 | |
| 56 | def _parse_lidar_cb(self, lidar_data, tag): |
| 57 | """ |
| 58 | parses lidar sensors |
| 59 | """ |
| 60 | points = np.frombuffer(lidar_data.raw_data, dtype=np.dtype('f4')) |
| 61 | points = copy.deepcopy(points) |
| 62 | points = np.reshape(points, (int(points.shape[0] / 3), 3)) |
| 63 | self._data_provider.update_sensor(tag, points, lidar_data.frame) |
| 64 | |
| 65 | def _parse_gnss_cb(self, gnss_data, tag): |
| 66 | """ |
| 67 | parses gnss sensors |
| 68 | """ |
| 69 | array = np.array([gnss_data.latitude, |
| 70 | gnss_data.longitude, |
| 71 | gnss_data.altitude], dtype=np.float64) |
| 72 | self._data_provider.update_sensor(tag, array, gnss_data.frame) |
| 73 | |
| 74 | |
| 75 | class SensorInterface(object): |