-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathview_start_positions.py
More file actions
executable file
·144 lines (113 loc) · 4.56 KB
/
view_start_positions.py
File metadata and controls
executable file
·144 lines (113 loc) · 4.56 KB
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
#!/usr/bin/env python3
# Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma de
# Barcelona (UAB).
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
"""Connects with a CARLA simulator and displays the available start positions
for the current map."""
from __future__ import print_function
import argparse
import logging
import sys
import time
import matplotlib
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from carla.client import make_carla_client
from carla.planner.map import CarlaMap
from carla.settings import CarlaSettings
from carla.tcp import TCPConnectionError
def view_start_positions(args):
# We assume the CARLA server is already waiting for a client to connect at
# host:port. The same way as in the client example.
with make_carla_client(args.host, args.port) as client:
# print('CarlaClient connected')
# We load the default settings to the client.
scene = client.load_settings(CarlaSettings())
# print("Received the start positions")
try:
image = mpimg.imread('carla/planner/%s.png' % scene.map_name)
carla_map = CarlaMap(scene.map_name, 0.1653, 50)
except IOError as exception:
logging.error(exception)
logging.error('Cannot find map "%s"', scene.map_name)
sys.exit(1)
fig, ax = plt.subplots(1)
plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
fig.canvas.set_window_title('Starting Positions for Town')
figmanager = plt.get_current_fig_manager()
figmanager.window.wm_geometry("+990+0")
screen_dpi = fig.get_dpi()
fig.set_size_inches(930.0/float(screen_dpi), 606.0/float(screen_dpi))
ax.imshow(image, aspect='auto')
if args.positions == 'all':
positions_to_plot = range(len(scene.player_start_spots))
else:
positions_to_plot = map(int, args.positions.split(','))
for position in positions_to_plot:
# Check if position is valid
if position >= len(scene.player_start_spots):
raise RuntimeError('Selected position is invalid')
# Convert world to pixel coordinates
pixel = carla_map.convert_to_pixel([scene.player_start_spots[position].location.x,
scene.player_start_spots[position].location.y,
scene.player_start_spots[position].location.z])
circle = Circle((pixel[0], pixel[1]), 12, color='r', label='A point')
ax.add_patch(circle)
if not args.no_labels:
plt.text(pixel[0], pixel[1], str(position), size='x-small')
plt.xlim((0, 700))
plt.ylim((700, 0))
plt.axis('off')
plt.show()
fig.savefig('town_positions.pdf', orientation='landscape', bbox_inches='tight')
def main():
argparser = argparse.ArgumentParser(description=__doc__)
argparser.add_argument(
'-v', '--verbose',
action='store_true',
dest='debug',
help='print debug information')
argparser.add_argument(
'--host',
metavar='H',
default='localhost',
help='IP of the host server (default: localhost)')
argparser.add_argument(
'-p', '--port',
metavar='P',
default=2000,
type=int,
help='TCP port to listen to (default: 2000)')
argparser.add_argument(
'-pos', '--positions',
metavar='P',
default='all',
help='Indices of the positions that you want to plot on the map. '
'The indices must be separated by commas (default = all positions)')
argparser.add_argument(
'--no-labels',
action='store_true',
help='do not display position indices')
args = argparser.parse_args()
log_level = logging.DEBUG if args.debug else logging.INFO
logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level)
# logging.info('listening to server %s:%s', args.host, args.port)
while True:
try:
view_start_positions(args)
print('Done.')
return
except TCPConnectionError as error:
logging.error(error)
time.sleep(1)
except RuntimeError as error:
logging.error(error)
break
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('\nCancelled by user. Bye!')