-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathautonomous_agent.py
132 lines (98 loc) · 4.02 KB
/
autonomous_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
"""
This module provides the base class for all autonomous agents
"""
from __future__ import print_function
from enum import Enum
import carla
from srunner.scenariomanager.timer import GameTime
from leaderboard.utils.route_manipulation import downsample_route
from leaderboard.envs.sensor_interface import SensorInterface
class Track(Enum):
"""
This enum represents the different tracks of the CARLA AD leaderboard.
"""
SENSORS = 'SENSORS'
MAP = 'MAP'
SENSORS_QUALIFIER = 'SENSORS_QUALIFIER'
MAP_QUALIFIER = 'MAP_QUALIFIER'
class AutonomousAgent(object):
"""
Autonomous agent base class. All user agents have to be derived from this class
"""
def __init__(self, carla_host, carla_port, debug=False):
self.track = Track.SENSORS
# current global plans to reach a destination
self._global_plan = None
self._global_plan_world_coord = None
# this data structure will contain all sensor data
self.sensor_interface = SensorInterface()
self.wallclock_t0 = None
def setup(self, path_to_conf_file):
"""
Initialize everything needed by your agent and set the track attribute to the right type:
Track.SENSORS : CAMERAS, LIDAR, RADAR, GPS and IMU sensors are allowed
Track.MAP : OpenDRIVE map is also allowed
"""
pass
def sensors(self): # pylint: disable=no-self-use
"""
Define the sensor suite required by the agent
:return: a list containing the required sensors in the following format:
[
{'type': 'sensor.camera.rgb', 'x': 0.7, 'y': -0.4, 'z': 1.60, 'roll': 0.0, 'pitch': 0.0, 'yaw': 0.0,
'width': 300, 'height': 200, 'fov': 100, 'id': 'Left'},
{'type': 'sensor.camera.rgb', 'x': 0.7, 'y': 0.4, 'z': 1.60, 'roll': 0.0, 'pitch': 0.0, 'yaw': 0.0,
'width': 300, 'height': 200, 'fov': 100, 'id': 'Right'},
{'type': 'sensor.lidar.ray_cast', 'x': 0.7, 'y': 0.0, 'z': 1.60, 'yaw': 0.0, 'pitch': 0.0, 'roll': 0.0,
'id': 'LIDAR'}
]
"""
sensors = []
return sensors
def run_step(self, input_data, timestamp):
"""
Execute one step of navigation.
:return: control
"""
control = carla.VehicleControl()
control.steer = 0.0
control.throttle = 0.0
control.brake = 0.0
control.hand_brake = False
return control
def destroy(self):
"""
Destroy (clean-up) the agent
:return:
"""
pass
def __call__(self):
"""
Execute the agent call, e.g. agent()
Returns the next vehicle controls
"""
input_data = self.sensor_interface.get_data(GameTime.get_frame())
timestamp = GameTime.get_time()
if not self.wallclock_t0:
self.wallclock_t0 = GameTime.get_wallclocktime()
wallclock = GameTime.get_wallclocktime()
wallclock_diff = (wallclock - self.wallclock_t0).total_seconds()
sim_ratio = 0 if wallclock_diff == 0 else timestamp/wallclock_diff
print('=== [Agent] -- Wallclock = {} -- System time = {} -- Game time = {} -- Ratio = {}x'.format(
str(wallclock)[:-3], format(wallclock_diff, '.3f'), format(timestamp, '.3f'), format(sim_ratio, '.3f')))
control = self.run_step(input_data, timestamp)
control.manual_gear_shift = False
return control
@staticmethod
def get_ros_version():
return -1
def set_global_plan(self, global_plan_gps, global_plan_world_coord):
"""
Set the plan (route) for the agent
"""
ds_ids = downsample_route(global_plan_world_coord, 200)
self._global_plan_world_coord = [(global_plan_world_coord[x][0], global_plan_world_coord[x][1]) for x in ds_ids]
self._global_plan = [global_plan_gps[x] for x in ds_ids]