-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcarla_adapter.py
executable file
·207 lines (183 loc) · 8.67 KB
/
carla_adapter.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python3
#
# carla_adapter.py -- CARLA daemon for use of Veins with CARLA
# Copyright (C) 2023 Tobias Hardes <[email protected]>
#
# Documentation for these modules is at http://veins.car2x.org/
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import logging
import tempfile
import grpc
import os
import sys
from google.protobuf import empty_pb2
from google.protobuf import struct_pb2
from optparse import OptionParser
import sys
sys.path.append('./proto')
import carla_pb2_grpc
import carla_pb2
from concurrent import futures
import time
import carla
import random
from datetime import datetime
world = None
traffic_manager = None
client = None
carla_object = None
class CarlaAdapter(carla_pb2_grpc.CarlaAdapterServicer):
def __init__(self, seed, steplength):
global world
global traffic_manager
global client
print("Carla_Adapter: " + str(self.GetCurrentTime()) + ": CarlaAdapter: __init__")
print("Steplength: " + str(steplength))
print("Seed: " + str(seed))
client = carla.Client('localhost', 2000) #Default configuration for CARLA
world = client.get_world()
# Set up the simulator in synchronous mode
settings = world.get_settings()
settings.synchronous_mode = True # Enables synchronous mode
settings.fixed_delta_seconds = steplength # TODO: Make this configurable and based on the Veins configuration
world.apply_settings(settings)
# Set up the TM in synchronous mode
traffic_manager = client.get_trafficmanager()
traffic_manager.set_synchronous_mode(True)
# Set a seed so behaviour can be repeated if necessary
traffic_manager.set_random_device_seed(seed)
random.seed(seed)
print("Carla_Adapter: " + str(self.GetCurrentTime()) + ": CarlaAdapter: __init__ done")
def ExecuteOneTimeStep(self, request, context):
print("Carla_Adapter: " + str(self.GetCurrentTime()) + ": carla_adapter: ExecuteOneTimeStep")
global world
world.tick()
print("Carla_Adapter: " + str(self.GetCurrentTime()) + ": carla_adapter: ExecuteOneTimeStep done")
return struct_pb2.Value()
def GetManagedActorsIds(self, request, context):
global world
world.get_actors()
actor_list = world.get_actors()
vehicles = actor_list.filter('vehicle.*')
print("Carla_Adapter: GetManagedActorsIds: Found " + str(len(vehicles)) + " vehicles")
print(vehicles)
returnValue = carla_pb2.ActorIds()
for vehicle in vehicles:
returnValue.actorId.append(vehicle.id)
print("Carla_Adapter: returning data")
print(returnValue)
return returnValue
def GetManagedActorById(self, request, context):
global world
world.get_actors()
actor_list = world.get_actors()
actor = actor_list.find(request.num)
aLocation = actor.get_location()
aSpeed = actor.get_velocity()
aAcceleration = actor.get_acceleration()
returnValue = carla_pb2.Vehicle()
returnValue.id = request.num
returnValue.location.x = aLocation.x
returnValue.location.y = aLocation.y
returnValue.location.z = aLocation.z
returnValue.speed.x = aSpeed.x
returnValue.speed.y = aSpeed.y
returnValue.speed.z = aSpeed.z
returnValue.acceleration.x = aAcceleration.x
returnValue.acceleration.y = aAcceleration.y
returnValue.acceleration.z = aAcceleration.z
return returnValue
def InsertVehicle(self, request, context):
global world
blueprint_library = [bp for bp in world.get_blueprint_library().filter('vehicle.*')]
vehicle_bp = blueprint_library[0]
vehicle_transform = world.get_map().get_spawn_points()[2]
print("Carla_Adapter: Spawing new vehicle -> " + str(vehicle_bp) + " at " + str(vehicle_transform))
vehicle = world.spawn_actor(vehicle_bp, vehicle_transform)
print("Carla_Adapter: Setting location to " + str(request.location.x) + ", " + str(request.location.y) + ", " + str(request.location.z))
loc = vehicle.get_location()
loc.x = request.location.x
loc.y = request.location.y
loc.z = request.location.z+5
vehicle.set_location(loc)
number = carla_pb2.Number(num=vehicle.id)
print("Carla_Adapter: Spawned new vehicle")
vehicle.set_autopilot(True)
return number
def GetRandomSpawnPoint(self, request, context):
global world
print("Carla_Adapter: GetRandomSpawnPoint")
randInt = random.randint(0,len(world.get_map().get_spawn_points())-1)
randInt = 1 #TODO
print("Random int " + str(randInt))
spawnPoint = world.get_map().get_spawn_points()[randInt]
print(type(spawnPoint))
print("Carla_Adapter: SpawnPoint: " + str(spawnPoint))
retvalue = carla_pb2.Transform()
retvalue.location.x = spawnPoint.location.x
retvalue.location.y = spawnPoint.location.y
retvalue.location.z = spawnPoint.location.z
retvalue.rotation.pitch = spawnPoint.rotation.pitch
retvalue.rotation.yaw = spawnPoint.rotation.yaw
retvalue.rotation.roll = spawnPoint.rotation.roll
print("return")
return retvalue
def GetCurrentTime(self):
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
return current_time
def startAdapter(address, port, daemonize, seed, steplength):
global carla_object
print("Carla_Adapter: startAdapter")
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
carla_object = CarlaAdapter(seed, steplength)
carla_pb2_grpc.add_CarlaAdapterServicer_to_server(carla_object, server)
grpcPort = server.add_insecure_port('localhost:1337')
print("Carla_Adapter: Create server on " + address + ":" + str(grpcPort))
server.start()
try:
while True:
time.sleep(10)
print("Carla_Adapter: Adapter is still alive")
except:
sys.exit(1)
def main():
"""
Program entry point when run interactively.
"""
parser = OptionParser()
parser.add_option("-p", "--port", dest="port", type="int", default=1337, action="store", help="listen for connections on PORT [default: %default]", metavar="PORT")
parser.add_option("-b", "--bind", dest="bind", default="localhost", help="bind to ADDRESS [default: %default]", metavar="ADDRESS")
parser.add_option("-L", "--logfile", dest="logfile", default=os.path.join(tempfile.gettempdir(), "carla-adapter.log"), help="log messages to LOGFILE [default: %default]", metavar="LOGFILE")
parser.add_option("-d", "--daemon", dest="daemonize", default=False, action="store_true", help="detach and run as daemon [default: no]")
parser.add_option("-q", "--quiet", dest="count_quiet", default=0, action="count", help="decrease verbosity [default: log warnings, errors]")
parser.add_option("-v", "--verbose", dest="count_verbose", default=0, action="count", help="increase verbosity [default: don't log infos, debug]")
parser.add_option("-s", "--seed", dest="seed", default=0, type="int", action="store", help="Random seed for the simulation [default: 0]")
parser.add_option("-l", "--steplength", dest="steplength", default=1, type="float", action="store", help="Length of one simulation step in seconds [default: 1]")
(options, args) = parser.parse_args()
_LOGLEVELS = (logging.ERROR, logging.WARN, logging.INFO, logging.DEBUG)
loglevel = _LOGLEVELS[max(0, min(1 + options.count_verbose - options.count_quiet, len(_LOGLEVELS)-1))]
# configure logging
logging.basicConfig(filename=options.logfile, level=loglevel)
if not options.daemonize:
logging.warning("Superfluous command line arguments: \"%s\"" % " ".join(args))
startAdapter(options.bind, options.port, options.daemonize, options.seed, options.steplength)
# Start main() when run interactively
if __name__ == '__main__':
main()