|
| 1 | +# Copyright 2024 Tier IV, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License.sr/bin/env python |
| 14 | + |
| 15 | +import signal |
| 16 | +import time |
| 17 | + |
| 18 | + |
| 19 | +from srunner.autoagents.autoware_carla_interface.modules.carla_wrapper import ( |
| 20 | + SensorReceivedNoData, |
| 21 | +) |
| 22 | +from srunner.autoagents.autoware_carla_interface.modules.carla_wrapper import ( |
| 23 | + SensorWrapper, |
| 24 | +) |
| 25 | +from srunner.autoagents.autoware_carla_interface.carla_ros import carla_ros2_interface |
| 26 | +from srunner.scenariomanager.carla_data_provider import CarlaDataProvider |
| 27 | +from srunner.scenariomanager.timer import GameTime |
| 28 | + |
| 29 | +from srunner.scenarioconfigs.environment_configuration import EnvironmentConfig |
| 30 | + |
| 31 | + |
| 32 | +class SensorLoop(object): |
| 33 | + def __init__(self): |
| 34 | + self.start_game_time = None |
| 35 | + self.start_system_time = None |
| 36 | + self.sensor = None |
| 37 | + self.ego_actor = None |
| 38 | + self.running = False |
| 39 | + self.timestamp_last_run = 0.0 |
| 40 | + self.timeout = 20.0 |
| 41 | + |
| 42 | + def _stop_loop(self): |
| 43 | + self.running = False |
| 44 | + |
| 45 | + def _tick_sensor(self, timestamp): |
| 46 | + if self.timestamp_last_run < timestamp.elapsed_seconds and self.running: |
| 47 | + self.timestamp_last_run = timestamp.elapsed_seconds |
| 48 | + try: |
| 49 | + ego_action = self.sensor() |
| 50 | + except SensorReceivedNoData as e: |
| 51 | + raise RuntimeError(e) |
| 52 | + self.ego_actor.apply_control(ego_action) |
| 53 | + |
| 54 | + |
| 55 | +class InitializeInterface(object): |
| 56 | + def __init__(self, config: EnvironmentConfig): |
| 57 | + self.interface = carla_ros2_interface() |
| 58 | + self.param_ = self.interface.get_param() |
| 59 | + self.world = None |
| 60 | + self.sensor_wrapper = None |
| 61 | + self.ego_actor = None |
| 62 | + self.prev_tick_wall_time = 0.0 |
| 63 | + |
| 64 | + self.config = config |
| 65 | + |
| 66 | + def load_world(self): |
| 67 | + self.ego_actor = CarlaDataProvider.get_actor_by_name(self.config.ego_name) |
| 68 | + self.interface.ego_actor = self.ego_actor # TODO improve design |
| 69 | + self.interface.physics_control = self.ego_actor.get_physics_control() |
| 70 | + |
| 71 | + self.sensor_wrapper = SensorWrapper(self.interface) |
| 72 | + self.sensor_wrapper.setup_sensors(self.ego_actor, False) |
| 73 | + |
| 74 | + def run_bridge(self): |
| 75 | + self.bridge_loop = SensorLoop() |
| 76 | + self.bridge_loop.sensor = self.sensor_wrapper |
| 77 | + self.bridge_loop.ego_actor = self.ego_actor |
| 78 | + self.bridge_loop.start_system_time = time.time() |
| 79 | + self.bridge_loop.start_game_time = GameTime.get_time() |
| 80 | + self.bridge_loop.running = True |
| 81 | + |
| 82 | + def tick_bridge(self): |
| 83 | + timestamp = None |
| 84 | + world = CarlaDataProvider.get_world() |
| 85 | + if world: |
| 86 | + snapshot = world.get_snapshot() |
| 87 | + if snapshot: |
| 88 | + timestamp = snapshot.timestamp |
| 89 | + if timestamp: |
| 90 | + self.prev_tick_wall_time = time.time() |
| 91 | + self.bridge_loop._tick_sensor(timestamp) |
| 92 | + |
| 93 | + def _stop_loop(self, sign, frame): |
| 94 | + self.bridge_loop._stop_loop() |
| 95 | + |
| 96 | + def _cleanup(self): |
| 97 | + self.sensor_wrapper.cleanup() |
| 98 | + CarlaDataProvider.cleanup() |
| 99 | + if self.ego_actor: |
| 100 | + self.ego_actor.destroy() |
| 101 | + self.ego_actor = None |
| 102 | + |
| 103 | + if self.interface: |
| 104 | + self.interface.shutdown() |
| 105 | + self.interface = None |
| 106 | + |
| 107 | + |
| 108 | +def main(): |
| 109 | + carla_bridge = InitializeInterface() |
| 110 | + carla_bridge.load_world() |
| 111 | + signal.signal(signal.SIGINT, carla_bridge._stop_loop) |
| 112 | + carla_bridge.run_bridge() |
| 113 | + carla_bridge._cleanup() |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments