-
Notifications
You must be signed in to change notification settings - Fork 8
/
controller.py
754 lines (665 loc) · 28.1 KB
/
controller.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
"""
TODO: Write Docstring
"""
from __future__ import print_function
import time
import argparse
import logging
import configparser
from pathlib import Path
import pandas as pd
import pygame
import pygame.locals as pl
import numpy as np
from carla.client import make_carla_client, VehicleControl
from carla import sensor
from carla.settings import CarlaSettings
from carla.tcp import TCPConnectionError
from carla import image_converter as ic
from timer import Timer
from disk_writer import ImageWriter, VideoWriter
from HUD import InfoBox
from enums import GameState, HighLevelCommand, TrafficLight
from non_player_objects import NonPlayerObjects
from drive_models import CNNKeras, LSTMKeras
class CarlaController:
""" TODO: Write Docstring """
def __init__(self, carla_client, args, settings):
self.client = carla_client
self._game_image = None
self._game_image_3p = None
self._measurements = None
self._sensor_data = None
self._image_history = None
self._driving_history = None
self._video_images = None
self._video_info = None
self._frame_history = None
self._pygame_display = None
self._carla_settings = None
self._settings = self._initialize_settings(settings)
self._timer = Timer()
self._output_path = args.output_path
self._game_state = GameState.NOT_RECORDING
self._new_episode_flag = False
self._exit_flag = False
self._vehicle_in_reverse = False
self._autopilot_enabled = False
self._drive_model_enabled = False
self._joystick_enabled = args.joystick
self._joystick = None
self._record_video = args.record_video
self._drive_model_path = args.drive_model_path
self._drive_model = None
self._disk_writer_thread = None
self._bottom_left_hud = InfoBox((200, 75))
self._bottom_right_hud = InfoBox((250, 75))
self._top_right_hud = InfoBox((200, 25))
self._current_traffic_light = None
self._current_speed_limit = None
self._current_hlc = None
self._start_postition = None
self._traffic_lights = NonPlayerObjects("traffic_light")
self._speed_limits = NonPlayerObjects("speed_limit_sign")
def _initialize_settings(self, f):
s = {}
s["quality_level"] = f.get("Carla", "QualityLevel", fallback="Epic")
s["weather_id"] = int(f.get("Carla", "WeatherId", fallback=1))
s["number_of_vehicles"] = int(f.get("Carla", "NumberOfVehicles", fallback=50))
s["number_of_pedastrians"] = int(
f.get("Carla", "NumberOfPedestrians", fallback=30)
)
s["autopilot_steer_noise"] = float(f.get("AutoPilot", "SteerNoise", fallback=0))
s["autopilot_throttle_noise"] = float(
f.get("AutoPilot", "ThrottleNoise", fallback=0)
)
s["window_width"] = int(f.get("Pygame", "WindowWidth", fallback=1024))
s["window_height"] = int(f.get("Pygame", "WindowHeight", fallback=768))
s["output_image_width"] = int(
f.get("Pygame", "OutputImageWidth", fallback=1024)
)
s["output_image_height"] = int(
f.get("Pygame", "OutputImageHeight", fallback=768)
)
s["randomize_weather"] = f.getboolean(
"Carla", "RandomizeWeather", fallback=False
)
s["autostart_recording"] = f.getboolean(
"Controller", "AutoStartRecording", fallback=False
)
s["frame_limit"] = int(f.get("Controller", "FrameLimit", fallback=0))
s["episode_limit"] = int(f.get("Controller", "EpisodeLimit", fallback=0))
s["drive_model_steer"] = f.getboolean(
"DriveModel", "ControlSteer", fallback=False
)
s["drive_model_throttle"] = f.getboolean(
"DriveModel", "ControlThrottle", fallback=False
)
s["drive_model_brake"] = f.getboolean(
"DriveModel", "ControlBrake", fallback=False
)
s["starting_positions"] = f.get("Carla", "StartingPositions", fallback=None)
if s["starting_positions"] is not None:
s["starting_positions"] = list(map(int, s["starting_positions"].split(",")))
return s
def _initialize_pygame(self):
self._pygame_display = pygame.display.set_mode(
(self._settings["window_width"], self._settings["window_height"]),
pygame.HWSURFACE | pygame.DOUBLEBUF,
)
if self._joystick_enabled:
pygame.joystick.init()
self._joystick = pygame.joystick.Joystick(0)
self._joystick.init()
logging.info("Use steering wheel to control vehicle")
else:
logging.info("Use keyboard to control vehicle")
self._on_new_episode()
logging.debug("pygame initialized")
def _initialize_carla(self):
# Initialize settings
settings = CarlaSettings()
settings.set(
SynchronousMode=True,
NumberOfVehicles=self._settings["number_of_vehicles"],
NumberOfPedestrians=self._settings["number_of_pedastrians"],
WeatherId=self._settings["weather_id"],
QualityLevel=self._settings["quality_level"],
SendNonPlayerAgentsInfo=True,
)
settings.randomize_seeds()
output_image_width = self._settings["output_image_width"]
output_image_height = self._settings["output_image_height"]
# Add a game camera 1 and 2
game_camera = sensor.Camera("GameCamera")
game_camera.set_image_size(
self._settings["window_width"], self._settings["window_height"]
)
game_camera.set_position(2.0, 0.0, 1.4)
game_camera.set_rotation(0.0, 0.0, 0.0)
settings.add_sensor(game_camera)
game_camera_3p = sensor.Camera("GameCamera3p")
game_camera_3p.set_image_size(
self._settings["window_width"], self._settings["window_height"]
)
game_camera_3p.set_position(-5, 0.0, 3)
game_camera_3p.set_rotation(-15, 0.0, 0)
settings.add_sensor(game_camera_3p)
# Add RGB center camera
rgb_camera_center = sensor.Camera("RGBCameraCenter")
rgb_camera_center.set_image_size(output_image_width, output_image_height)
rgb_camera_center.set_position(2.0, 0.0, 1.4)
rgb_camera_center.set_rotation(0.0, 0.0, 0.0)
settings.add_sensor(rgb_camera_center)
# Add RGB left camera
rgb_camera_left = sensor.Camera("RGBCameraLeft")
rgb_camera_left.set_image_size(output_image_width, output_image_height)
rgb_camera_left.set_position(2.0, -1, 1.4)
rgb_camera_left.set_rotation(0.0, 0.0, 0.0)
settings.add_sensor(rgb_camera_left)
# Add RGB right camera
rgb_camera_right = sensor.Camera("RGBCameraRight")
rgb_camera_right.set_image_size(output_image_width, output_image_height)
rgb_camera_right.set_position(2.0, 1, 1.4)
rgb_camera_right.set_rotation(0.0, 0.0, 0.0)
settings.add_sensor(rgb_camera_right)
# Add depth camera
depth_camera = sensor.Camera("DepthCamera", PostProcessing="Depth")
depth_camera.set_image_size(output_image_width, output_image_height)
depth_camera.set_position(2.0, 0.0, 1.4)
depth_camera.set_rotation(0.0, 0.0, 0.0)
settings.add_sensor(depth_camera)
# Add semantic segmentation camera
sem_seg_camera = sensor.Camera(
"SemSegCamera", PostProcessing="SemanticSegmentation"
)
sem_seg_camera.set_image_size(output_image_width, output_image_height)
sem_seg_camera.set_position(2.0, 0.0, 1.4)
sem_seg_camera.set_rotation(0.0, 0.0, 0.0)
settings.add_sensor(sem_seg_camera)
self._carla_settings = settings
logging.debug("Carla initialized")
def _initialize_drive_model(self):
if self._drive_model_path:
self._drive_model = CNNKeras()
logging.info("Loading drive model from: %s", self._drive_model_path)
self._drive_model.load_model(self._drive_model_path)
def _initialize_history(self):
self._driving_history = pd.DataFrame(
columns=[
"CenterRGB",
"LeftRGB",
"RightRGB",
"Depth",
"SemSeg",
"Location",
"Speed",
"Controls",
"APControls",
"HLC",
"SpeedLimit",
"TrafficLight",
"AutoPilotEnabled",
"WeatherID",
]
)
self._image_history = []
self._frame_history = []
def _on_new_episode(self):
self._timer.new_episode()
if self._settings["episode_limit"] != 0:
if self._settings["episode_limit"] < self._timer.episode_num:
self._exit_flag = True
return
scene = self.client.load_settings(self._carla_settings)
number_of_start_positions = len(scene.player_start_spots)
if self._settings["starting_positions"] is not None:
if self._start_postition is None:
current_index = 0
else:
current_index = self._settings["starting_positions"].index(
self._start_postition
)
if current_index >= len(self._settings["starting_positions"]) - 1:
current_index = 0
else:
current_index += 1
self._start_postition = self._settings["starting_positions"][current_index]
else:
self._start_postition = np.random.randint(number_of_start_positions)
self.client.start_episode(self._start_postition)
self._new_episode_flag = False
self._disk_writer_thread = None
self._initialize_history()
self._video_images = [[], []]
self._video_info = []
self._current_speed_limit = 30
self._current_traffic_light = (TrafficLight.NONE, 15)
if self._settings["randomize_weather"]:
self._carla_settings.set(WeatherId=np.random.randint(0, 15))
if self._drive_model:
self._current_hlc = HighLevelCommand.FOLLOW_ROAD
def _get_keyboard_control(self, keys):
control = VehicleControl()
if keys[pl.K_LEFT] or keys[pl.K_a]:
control.steer = -1.0
if keys[pl.K_RIGHT] or keys[pl.K_d]:
control.steer = 1.0
if keys[pl.K_UP] or keys[pl.K_w]:
control.throttle = 1.0
if keys[pl.K_DOWN] or keys[pl.K_s]:
control.brake = 1.0
if keys[pl.K_SPACE]:
control.hand_brake = True
control.reverse = self._vehicle_in_reverse
return control
def _get_joystick_control(self):
control = VehicleControl()
control.steer = self._joystick.get_axis(0)
control.throttle = max(self._joystick.get_axis(1) * -1, 0)
control.brake = max(self._joystick.get_axis(1), 0)
return control
def _get_autopilot_control(self):
speed = int(self._measurements.player_measurements.forward_speed * 3.6)
autopilot = self._measurements.player_measurements.autopilot_control
control = VehicleControl()
steer_noise = self._settings["autopilot_steer_noise"]
if steer_noise != 0:
steer_noise = np.random.uniform(-steer_noise, steer_noise)
control.steer = autopilot.steer + steer_noise
throttle_noise = (
self._settings["autopilot_throttle_noise"] if speed > 10 else 0
)
if throttle_noise != 0:
throttle_noise = np.random.uniform(-throttle_noise, throttle_noise)
control.throttle = autopilot.throttle + throttle_noise
control.brake = autopilot.brake
return control
def _get_drive_model_control(self, control):
images = self._get_camera_images()
info = {
"speed": self._measurements.player_measurements.forward_speed * 3.6,
"speed_limit": self._current_speed_limit,
"traffic_light": self._current_traffic_light[0].value,
"hlc": self._current_hlc.value,
}
steer, throttle, brake = self._drive_model.get_prediction(images, info)
if self._settings["drive_model_steer"]:
control.steer = steer
if self._settings["drive_model_throttle"]:
control.throttle = throttle
if self._settings["drive_model_brake"]:
if brake > 0.4:
control.brake = brake
return control
def _writeback_hlc_to_history(self, command):
look_back = 70
for i, row in self._driving_history.iterrows():
if int(row["HLC"]) == 0:
if i >= len(self._driving_history.index) - look_back:
self._driving_history.at[i, "HLC"] = command.value
def _handle_keydown_event(self, key):
if self._game_state is not GameState.WRITING:
if key == pl.K_p:
self._autopilot_enabled = not self._autopilot_enabled
elif key == pl.K_m:
if self._drive_model:
self._drive_model_enabled = not self._drive_model_enabled
self._current_hlc = HighLevelCommand.FOLLOW_ROAD
elif key == pl.K_q:
self._vehicle_in_reverse = not self._vehicle_in_reverse
elif key == pl.K_e:
if self._game_state == GameState.RECORDING:
self._game_state = GameState.WRITING
if self._record_video:
self._write_video_to_disk(
on_complete=self._write_history_to_disk
)
else:
self._write_history_to_disk()
else:
if self._record_video:
self._game_state = GameState.WRITING
self._write_video_to_disk()
self._new_episode_flag = True
elif key == pl.K_r:
if (
self._game_state == GameState.NOT_RECORDING
and self._output_path is not None
):
self._game_state = GameState.RECORDING
elif self._game_state == GameState.RECORDING:
self._game_state = GameState.WRITING
self._write_history_to_disk()
if self._game_state == GameState.RECORDING:
if key == pl.K_KP8:
self._writeback_hlc_to_history(HighLevelCommand.STRAIGHT_AHEAD)
elif key == pl.K_KP4:
self._writeback_hlc_to_history(HighLevelCommand.TURN_LEFT)
elif key == pl.K_KP6:
self._writeback_hlc_to_history(HighLevelCommand.TURN_RIGHT)
if self._drive_model_enabled:
if key == pl.K_KP8:
self._current_hlc = HighLevelCommand.STRAIGHT_AHEAD
elif key == pl.K_KP4:
self._current_hlc = HighLevelCommand.TURN_LEFT
elif key == pl.K_KP6:
self._current_hlc = HighLevelCommand.TURN_RIGHT
elif key == pl.K_KP5:
self._current_hlc = HighLevelCommand.FOLLOW_ROAD
def _render_HUD(self):
speed = int(self._measurements.player_measurements.forward_speed * 3.6)
autopilot_status = "Enabled" if self._autopilot_enabled else "Disabled"
drive_model_status = "Enabled" if self._drive_model_enabled else "Disabled"
reverse_status = "Enabled" if self._vehicle_in_reverse else "Disabled"
speed_value = "{} km/h".format(speed)
speed_limit = "{} km/h".format(self._current_speed_limit)
traffic_light = self._current_traffic_light[0].name
current_hlc = (
self._current_hlc.name if self._drive_model_enabled else "Disabled"
)
self._bottom_left_hud.update_content(
[
("Speed", speed_value),
("Speed Limit", speed_limit),
("Reverse", reverse_status),
("Traffic Light", traffic_light),
]
)
self._bottom_right_hud.update_content(
[
("Autopilot", autopilot_status),
("Recording State", self._game_state.name),
("Drive Model", drive_model_status),
("Drive Model HLC", current_hlc),
]
)
self._top_right_hud.update_content([("Start Position", self._start_postition)])
sw_x = 20
sw_y = self._settings["window_height"] - self._bottom_left_hud.size[1] - 20
se_x = self._settings["window_width"] - self._bottom_right_hud.size[0] - 20
se_y = self._settings["window_height"] - self._bottom_right_hud.size[1] - 20
ne_x = self._settings["window_width"] - self._top_right_hud.size[0] - 20
ne_y = 20
self._pygame_display.blit(self._bottom_left_hud.render_surface(), (sw_x, sw_y))
self._pygame_display.blit(self._bottom_right_hud.render_surface(), (se_x, se_y))
self._pygame_display.blit(self._top_right_hud.render_surface(), (ne_x, ne_y))
def _render_pygame(self):
if self._game_image is not None:
array = ic.to_rgb_array(self._game_image)
surface = pygame.surfarray.make_surface(array.swapaxes(0, 1))
if self._game_state == GameState.WRITING:
self._render_progressbar(
surface, 300, 40, self._disk_writer_thread.progress
)
self._pygame_display.blit(surface, (0, 0))
self._render_HUD()
pygame.display.flip()
def _render_progressbar(self, surface, width, height, progress):
left = (self._settings["window_width"] / 2) - (width / 2)
top = (self._settings["window_height"] / 2) - (height / 2)
pygame.draw.rect(
surface, (255, 255, 255), pygame.Rect(left, top, width * progress, height)
)
pygame.draw.rect(
surface, (128, 128, 128), pygame.Rect(left, top, width, height), 1
)
self._pygame_display.blit(surface, (0, 0))
def _get_camera_images(self):
sensor_data = self._sensor_data
image_object = {
"rgb_center": ic.to_bgra_array(sensor_data.get("RGBCameraCenter", None)),
"rgb_left": ic.to_bgra_array(sensor_data.get("RGBCameraLeft", None)),
"rgb_right": ic.to_bgra_array(sensor_data.get("RGBCameraRight", None)),
"depth": ic.depth_to_logarithmic_grayscale(
sensor_data.get("DepthCamera", None)
),
"sem_seg": ic.labels_to_cityscapes_palette(
sensor_data.get("SemSegCamera", None)
),
}
return image_object
def _prepare_video_images(self):
speed = int(self._measurements.player_measurements.forward_speed * 3.6)
speed = "{} km/h".format(speed)
speed_limit = "{} km/h".format(self._current_speed_limit)
traffic_light = self._current_traffic_light[0].name
current_hlc = (
self._current_hlc.name if self._drive_model_enabled else "Disabled"
)
info = [speed, speed_limit, traffic_light, current_hlc]
self._video_info.append(info)
self._video_images[0].append(ic.to_rgb_array(self._game_image))
self._video_images[1].append(ic.to_rgb_array(self._game_image_3p))
def _save_to_history(self, control):
measurements = self._measurements
frame = self._timer.episode_frame
self._image_history.append(self._get_camera_images())
self._frame_history.append(frame)
loc = measurements.player_measurements.transform.location
speed = measurements.player_measurements.forward_speed * 3.6
autopilot = measurements.player_measurements.autopilot_control
self._driving_history = self._driving_history.append(
pd.Series(
[
f"imgs/{frame}_rgb_center.png",
f"imgs/{frame}_rgb_left.png",
f"imgs/{frame}_rgb_right.png",
f"imgs/{frame}_depth.png",
f"imgs/{frame}_sem_seg.png",
(loc.x, loc.y),
speed,
(
control.steer,
control.throttle,
control.brake,
int(control.reverse),
),
(
autopilot.steer,
autopilot.throttle,
autopilot.brake,
int(autopilot.reverse),
),
0,
self._current_speed_limit,
self._current_traffic_light[0].value,
int(self._autopilot_enabled),
self._settings["weather_id"],
],
index=self._driving_history.columns,
),
ignore_index=True,
)
def _write_history_to_disk(self):
path = Path(f"{self._output_path}/{self._timer.episode_timestamp_str}")
self._disk_writer_thread = ImageWriter(
path,
self._image_history,
self._driving_history,
self._frame_history,
on_complete=self._images_write_complete,
)
self._disk_writer_thread.start()
def _write_video_to_disk(self, on_complete=None):
if on_complete is None:
on_complete = self._video_write_complete
path = Path(f"{self._output_path}/{self._timer.episode_timestamp_str}")
self._disk_writer_thread = VideoWriter(
path, self._video_images, self._video_info, on_complete=on_complete
)
self._disk_writer_thread.start()
def _images_write_complete(self):
self._game_state = GameState.NOT_RECORDING
self._initialize_history()
def _video_write_complete(self):
self._game_state = GameState.NOT_RECORDING
def _update_current_traffic_light(self):
old_state, old_dist = self._current_traffic_light
agent, new_dist = self._traffic_lights.get_closest_with_rotation(
self._measurements.player_measurements.transform, 12, -90, 15
)
if agent is not None:
new_state = agent.state
if new_dist <= old_dist:
self._current_traffic_light = (TrafficLight(new_state), new_dist)
else:
self._current_traffic_light = (TrafficLight(old_state), new_dist)
else:
self._current_traffic_light = (TrafficLight.NONE, 15)
def _update_current_speed_limit(self):
agent = self._speed_limits.get_closest_with_rotation(
self._measurements.player_measurements.transform, 12, -90, 20
)[0]
if agent is not None:
self._current_speed_limit = int(agent.speed_limit * 3.6)
def _on_loop(self):
if (
self._game_state is GameState.NOT_RECORDING
and self._settings["autostart_recording"]
):
if self._timer.episode_frame is 40:
self._game_state = GameState.RECORDING
if self._game_state is not GameState.WRITING:
self._timer.tick()
if self._new_episode_flag:
self._on_new_episode()
if self._exit_flag:
return False
measurements, sensor_data = self.client.read_data()
self._measurements = measurements
self._sensor_data = sensor_data
self._game_image = sensor_data.get("GameCamera", None)
self._game_image_3p = sensor_data.get("GameCamera3p", None)
if self._record_video:
self._prepare_video_images()
self._traffic_lights.update_agents(measurements.non_player_agents)
if not self._traffic_lights.valid:
self._traffic_lights.initialize_KD_tree()
else:
self._update_current_traffic_light()
if not self._speed_limits.valid:
self._speed_limits.update_agents(measurements.non_player_agents)
self._speed_limits.initialize_KD_tree()
else:
self._update_current_speed_limit()
if not self._autopilot_enabled:
if self._joystick_enabled:
control = self._get_joystick_control()
else:
control = self._get_keyboard_control(pygame.key.get_pressed())
else:
control = self._get_autopilot_control()
if self._drive_model and self._drive_model_enabled:
control = self._get_drive_model_control(control)
print(control)
self.client.send_control(control)
if self._game_state == GameState.RECORDING:
self._save_to_history(control)
if self._settings["frame_limit"] != 0:
if self._settings["frame_limit"] < self._timer.episode_frame:
if self._game_state == GameState.RECORDING:
self._game_state = GameState.WRITING
if self._record_video:
self._write_video_to_disk(
on_complete=self._write_history_to_disk
)
else:
self._write_history_to_disk()
if self._record_video:
self._game_state = GameState.WRITING
self._write_video_to_disk()
self._new_episode_flag = True
self._render_pygame()
def execute(self):
""" TODO: Write docstring """
pygame.init()
self._initialize_carla()
self._initialize_pygame()
self._initialize_drive_model()
if self._output_path is not None:
logging.info("Recorded data will be saved to: %s", self._output_path)
try:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pl.KEYDOWN:
self._handle_keydown_event(event.key)
if self._on_loop() is False:
break
finally:
pygame.quit()
def main():
""" TODO: Write docstring """
argparser = argparse.ArgumentParser(description="CARLA Manual Control Client")
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(
"-j",
"--joystick",
action="store_true",
help="control vehicle with an external steering wheel",
)
argparser.add_argument(
"-p",
"--port",
metavar="P",
default=2000,
type=int,
help="TCP port to listen to (default: 2000)",
)
argparser.add_argument(
"--record-video",
action="store_true",
dest="record_video",
default=False,
help="recorded a video from the driving",
)
argparser.add_argument(
"-o",
"--output",
metavar="PATH",
dest="output_path",
default="output",
help="recorded data will be saved to this path",
)
argparser.add_argument(
"-m",
"--model",
metavar="M",
dest="drive_model_path",
default=None,
help="path to drive model",
)
args = argparser.parse_args()
settings = configparser.ConfigParser()
settings.read("settings.ini")
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:
with make_carla_client(args.host, args.port) as client:
game = CarlaController(client, args, settings)
game.execute()
break
except TCPConnectionError as error:
logging.error(error)
time.sleep(1)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nCancelled by user. Bye!")