-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlunatic_agent.py
1531 lines (1267 loc) · 72.5 KB
/
lunatic_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
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This module implements an agent that roams around a track following random
waypoints and avoiding other vehicles. The agent also responds to traffic lights,
traffic signs, and has different possible configurations.
"""
# Decide how to handle imports
# ruff: noqa: PLC0415
from __future__ import annotations
import contextlib
import sys
from copy import deepcopy
from typing import TYPE_CHECKING, Any, ClassVar, Dict, Iterable, List, NoReturn, Optional, Sequence, Set, Union
from typing import cast as assure_type
import carla # pyright: ignore[reportMissingTypeStubs]
import omegaconf
from omegaconf import DictConfig, OmegaConf
from typing_extensions import Literal, Self, Unpack
from agents import substep_managers
from agents.dynamic_planning.dynamic_local_planner import DynamicLocalPlannerWithRss
from agents.navigation.behavior_agent import BehaviorAgent
from agents.navigation.global_route_planner import GlobalRoutePlanner
from agents.rules import rule_from_config
from agents.tools.config_creation import (
AgentConfig,
LaunchConfig,
LiveInfo,
LunaticAgentSettings,
RssRoadBoundariesModeAlias,
RuleCreatingParameters,
)
from agents.tools.logs import logger
from agents.tools.lunatic_agent_tools import (
detect_vehicles,
must_clear_hazard,
phase_callback, # type: ignore[unused-import] # noqa: F401
result_to_context,
)
from agents.tools.misc import lanes_have_same_direction
from classes.constants import AD_RSS_AVAILABLE, READTHEDOCS, AgentState, Hazard, HazardSeverity, Phase, RoadOption
from classes.exceptions import (
AgentDoneException,
ContinueLoopException,
EmergencyStopException,
LunaticAgentException,
NoFurtherRulesException,
SkipInnerLoopException,
UpdatedPathException,
UserInterruption,
)
from classes.rule import BlockingRule, Context, Rule
from classes.worldmodel import CarlaDataProvider, WorldModel
from classes.detection_matrix import AsyncDetectionMatrix, DetectionMatrix
from classes.information_manager import InformationManager
if TYPE_CHECKING:
from agents.tools.hints import ObstacleDetectionResult, TrafficLightDetectionResult
import pygame
class LunaticAgent(BehaviorAgent):
"""
BasicAgent implements an agent that navigates the scene.
This agent respects traffic lights and other vehicles, but ignores stop signs.
It has several functions available to specify the route that the agent must follow,
as well as to change its parameters in case a different driving mode is desired.
"""
BASE_SETTINGS: "type[LunaticAgentSettings]" = LunaticAgentSettings
"""
Base AgentConfig class for this agent. This is used to create the default settings for the agent
if none are provided.
"""
DEFAULT_RULES: ClassVar[Dict[Phase, List[Rule]]] = {k: [] for k in Phase.get_phases()}
"""
Default rules of this agent class when initialized.
:meta hide-value:
"""
rules: Dict[Phase, List[Rule]]
"""
The rules of the this agent.
When initialized the rules are deep copied from :py:attr:`DEFAULT_RULES`.
"""
ctx: Context
"""The context object of the current step"""
# Information from the InformationManager
walkers_nearby: List[carla.Walker]
vehicles_nearby: List[carla.Vehicle]
static_obstacles_nearby: List[carla.Actor]
"""Static obstacles detected by the :py:class:`.InformationManager`"""
obstacles_nearby: List[carla.Actor]
"""
Combination of :py:attr:`vehicles_nearby`, :py:attr:`walkers_nearby`
and :py:attr:`static_obstacles_nearby`.
"""
traffic_lights_nearby: List[carla.TrafficLight]
traffic_signs_nearby: List[carla.TrafficSign] = NotImplemented
"""
Not yet implemented
:meta private:
"""
current_states: Dict[AgentState, int]
"""The current states of the agent. The count of the steps being each state is stored as value."""
#
_world_model: WorldModel
"""Reference to the attached :py:class:`WorldModel`."""
_validate_phases = False
"""
Debugging flag to sanity check if the agent passes trough the phases in the correct order.
Attention: Currently straight forward anymore.
"""
_active_blocking_rules: Set[BlockingRule]
"""Blocking rules that are currently active and have taken over the agents loop."""
# ------------------ Initialization ------------------ #
from agents.tools.lunatic_agent_tools import create_agent_config as _create_agent_config
@classmethod
def create_world_and_agent(cls, args: LaunchConfig, *,
vehicle: Optional[carla.Vehicle] = None,
sim_world: carla.World,
settings_archetype: "Optional[type[AgentConfig]]" = None,
agent_config: Optional["LunaticAgentSettings"] = None,
overwrites: Optional[Dict[str, Any]] = None
) -> tuple[Self, WorldModel, GlobalRoutePlanner]:
"""
Setup function to create the agent from the :py:class:`LaunchConfig` settings.
Note:
- :py:meth:`.GameFramework.init_agent_and_interface` is the preferred way to create to
instantiate the agent, only use this method if you try not do create a
:py:class:`GameFramework` object.
"""
if overwrites is None:
overwrites = {}
if agent_config is None:
if hasattr(args, "agent"):
if settings_archetype is not None:
logger.warning("settings_archetype was passed but using args.agent. "
"Ignoring settings_archetype.")
agent_config = args.agent
elif settings_archetype is not None and isinstance(settings_archetype, object):
logger.warning("settings_archetype is an instance. "
"To pass an instance use agent_config instead.")
agent_config = settings_archetype # type: ignore
elif settings_archetype is not None:
logger.debug("Creating config from settings_archetype")
behavior = settings_archetype(overwrites)
agent_config = cls.BASE_SETTINGS.cast(behavior.to_dict_config())
else:
logger.debug("Using %s._base_settings %s to create config.",
cls.__name__, cls.BASE_SETTINGS)
agent_config = cls.BASE_SETTINGS.cast(cls.BASE_SETTINGS.to_dict_config())
assert agent_config is not None
else:
logger.debug("A config was passed, using it as is.")
world_model = WorldModel(agent_config, args=args, carla_world=sim_world, player=vehicle) # TEST: without args
agent_config.planner.dt = world_model.world_settings.fixed_delta_seconds or 1 / world_model._args.fps # pyright: ignore[reportPrivateUsage]
agent = cls(agent_config, world_model)
return agent, world_model, agent.get_global_planner()
def __init__(self,
settings: Union[str, LunaticAgentSettings],
world_model: Optional[WorldModel] = None,
*,
vehicle: Optional[carla.Vehicle] = None,
overwrite_options: Optional[dict[str, Any]] = None,
debug: bool = True):
"""
Initialize the LunaticAgent.
Args:
settings :
The settings of the agent to construct the :py:attr:`LunaticAgent.config`.
It can be either a string pointing to a yaml file or a LunaticAgentSettings.
world_model : The world model to use. If None, a new WorldModel will be created.
vehicle : The vehicle controlled by the agent. Can be None then the :code:`world_model.player` will be used.
overwrite_options : Additional options to overwrite the default agent configuration.
debug : Whether to enable debug mode.
"""
self._debug = debug
"""Enables additional debug output."""
if world_model is None and len(sys.argv) > 1:
logger.error("BUG: Beware when not passing a WorldModel, the WorldModel currently ignores command line overrides, "
"i.e. will only use the config file.\n> Use `%s.create_world_and_agent` and provide the LaunchConfig instead.", self.__class__.__name__)
# -------------------- Settings ------------------------
# Depending on the input type, the settings are created differently.
self.config = self._create_agent_config(settings, world_model, overwrite_options)
# -------------------------------
logger.info("\n\nAgent config is %s", OmegaConf.to_yaml(self.config))
# World Model
if world_model is None:
world_model = WorldModel(self.config, player=vehicle)
self.config.planner.dt = world_model.world_settings.fixed_delta_seconds or 1 / world_model._args.fps # pyright: ignore[reportPrivateUsage]
self._world_model: WorldModel = world_model
self._world: carla.World = world_model.world
# Register Vehicle
assert world_model.player is not None
self.set_vehicle(world_model.player)
self.current_phase: Phase = Phase.NONE
"""current phase of the agent inside the loop"""
self.ctx = None # type: ignore
self._last_traffic_light: Optional[carla.TrafficLight] = None
"""Current red traffic light"""
# TODO: No more hardcoded defaults / set them from opt_dict which must have all parameters; check which are parameters and which are set by other functions (e.g. _look_ahead_steps)
# Parameters from BehaviorAgent ------------------------------------------
# todo: check redefinitions
self._look_ahead_steps: int = 0
"""
updated in _update_information used for local_planner.get_incoming_waypoint_and_direction
"""
# Initialize the planners
self._global_planner = CarlaDataProvider.get_global_route_planner() # NOTE: THIS does not use self.config.planner.sampling_resolution
if not self._global_planner:
logger.error("Global Route Planner not set - This should not happen, if the CarlaDataProvider has been initialized.")
self._global_planner = GlobalRoutePlanner(CarlaDataProvider.get_map(), self.config.planner.sampling_resolution)
CarlaDataProvider._grp = self._global_planner # pyright: ignore[reportPrivateUsage]
# Get the static elements of the scene
self._traffic_light_map: Dict[carla.TrafficLight, carla.Transform] = CarlaDataProvider._traffic_light_map # pyright: ignore[reportPrivateUsage]
self._lights_list = CarlaDataProvider._traffic_light_map.keys() # pyright: ignore[reportPrivateUsage]
self._lights_map: Dict[int, carla.Waypoint] = {}
"""Dictionary mapping a traffic light to a wp corresponding to its trigger volume location"""
# Vehicle Lights
self._vehicle_lights = carla.VehicleLightState.NONE
# Collision Sensor # NOTE: another in the WorldModel for the HUD
self._set_collision_sensor()
# Rule Framework
# 1. add all rules from the class, if any - else this is a dict with empty lists
self.rules = deepcopy(self.__class__.DEFAULT_RULES) # Copies the ClassVar to the instance
# 2. add rules from the config
self.add_config_rules()
# Information Manager
self.information_manager = InformationManager(self)
self.current_states = self.information_manager.state_counter # share the dict
# Blocking Rules
self._active_blocking_rules = set()
# --------------------- Collision Callback ------------------------
def _set_collision_sensor(self):
# see: https://carla.readthedocs.io/en/latest/ref_sensors/#collision-detector
# and https://carla.readthedocs.io/en/latest/python_api/#carla.Sensor.listen
blueprint = CarlaDataProvider._blueprint_library.find('sensor.other.collision') # pyright: ignore[reportPrivateUsage]
self._collision_sensor: carla.Sensor = assure_type(carla.Sensor, CarlaDataProvider.get_world().spawn_actor(
blueprint, carla.Transform(), attach_to=self._vehicle))
self._collision_sensor.listen(self._collision_event)
from agents.substep_managers import collision_manager as _collision_manager
def _collision_event(self, event: carla.CollisionEvent):
"""
Callback function for the collision sensor.
By default uses :py:func:`agents.substep_managers.collision_manager`.
Executes Phases:
- :py:class:`Phase.COLLISION | Phase.BEGIN<.Phase>`
- :py:class:`Phase.COLLISION | Phase.END<.Phase>`
"""
# https://carla.readthedocs.io/en/latest/python_api/#carla.CollisionEvent
# e.g. setting ignore_vehicles to False, if it was True before.
# do an emergency stop (in certain situations)
try:
self.execute_phase(Phase.COLLISION | Phase.BEGIN, prior_results=event)
except AttributeError as e:
if "'NoneType' object has no attribute 'prior_result'" not in str(e):
raise e
# context not yet set up, very early collision
result = self._collision_manager(event)
try:
self.execute_phase(Phase.COLLISION | Phase.END, prior_results=result)
except AttributeError as e:
if "'NoneType' object has no attribute 'prior_result'" not in str(e):
raise e
# context not yet set up, very early collision
# --------------------- Adding rules ------------------------
def add_rule(self, rule: Rule, position: Union[int, None] = None):
"""
Add a rule to the agent. The rule will be inserted at the given position.
Args:
rule : The rule to add
position :
The position to insert the rule at.
If :python:`None` the rule list will be sorted by priority.
Defaults to :python:`None`.
"""
for p in rule.phases:
if p not in self.rules:
logger.warning("Phase %s from Rule %s is not a default phase. Adding a new phase.", p, rule)
self.rules[p] = []
if position is None:
self.rules[p].append(rule)
self.rules[p].sort(key=lambda r: r.priority, reverse=True)
else:
self.rules[p].insert(position, rule)
def add_rules(self, rules: "Rule | Iterable[Rule]"):
"""Add a list of rules and sort the agents rules by priority."""
if isinstance(rules, Rule):
rules = [rules]
for rule in rules:
for phase in rule.phases:
self.rules[phase].append(rule)
for phase in self.rules.keys(): # noqa: SIM118
self.rules[phase].sort(key=lambda r: r.priority, reverse=True)
def add_config_rules(self, config: Optional[Union[LunaticAgentSettings, List[RuleCreatingParameters]]] = None):
"""
Adds rules
"""
if config is None:
config = self.config
rule_list: List[RuleCreatingParameters]
# "rules" in config is also false for rules == MISSING
if isinstance(config, (AgentConfig, DictConfig)) and "rules" in config.keys(): # noqa: SIM118, RUF100
try:
rule_list = config.rules
except omegaconf.MissingMandatoryValue:
logger.debug("`rules` key was missing, skipping rule addition.")
return
else:
rule_list: List[RuleCreatingParameters] = config # type: ignore[assignment]
logger.debug("Adding rules from config:\n%s", OmegaConf.to_yaml(rule_list))
for rule in rule_list:
self.add_rules(rule_from_config(rule)) # each call could produce one or more rules
# --------------------- Properties ------------------------
@property
def live_info(self) -> LiveInfo:
return self.config.live_info
@property
def detection_matrix(self):
"""
Returns :any:`DetectionMatrix.getMatrix` if the matrix is set.
"""
if self._detection_matrix:
return self._detection_matrix.getMatrix()
return None
# ------------------ Hazard ------------------ #
@property
def detected_hazards(self) -> Set[Hazard]:
return self.ctx.detected_hazards
@property
def detected_hazards_info(self) -> Dict[Hazard, Any]:
"""
Information about the detected hazards, e.g. severity.
"""
return self.ctx.detected_hazards_info
@detected_hazards.setter
def detected_hazards(self, hazards: Set[Hazard]):
if not isinstance(hazards, set): # pyright: ignore[reportUnnecessaryIsInstance]
raise TypeError("detected_hazards must be a set of Hazards.")
self.ctx._detected_hazards = hazards # pyright: ignore[reportPrivateUsage]
# These have the same signature and can be used interchangeably
discard_hazard = Context.discard_hazard
add_hazard = Context.add_hazard
has_hazard = Context.has_hazard
#
@property
def current_traffic_light(self) -> "carla.TrafficLight | None":
"""Alias to :py:attr:`self._last_traffic_light <_last_traffic_light>`."""
return self._last_traffic_light
@current_traffic_light.setter
def current_traffic_light(self, traffic_light: carla.TrafficLight):
self._last_traffic_light = traffic_light
@property
def phase_results(self) -> Dict[Phase, Any]:
"""
Retrieves :py:attr:`agent.ctx.phase_results <classes.rule.Context.phase_results>`
Stores the results of the phases the agent has been in.
By default the keys are set to :any:`Context.PHASE_NOT_EXECUTED`.
"""
return self.ctx.phase_results
@property
def active_blocking_rules(self) -> Set[BlockingRule]:
"""
Blocking rules that are currently active and have taken over the agents loop.
"""
return self._active_blocking_rules
@property
def _map(self) -> carla.Map:
"""Get the current map of the world.""" # Needed *only* for set_destination
return CarlaDataProvider.get_map()
# ------------------
#@property
#def ctx(self) -> Union[Context, None]:
# print("Getting Context", self._ctx())
# return self._ctx() # might be None
# ------------------ Information functions ------------------ #
def update_information(self, second_pass: bool = False):
"""
Updates the information regarding the ego vehicle based on the surrounding world.
Parameters:
second_pass : *Internal usage* set to :python:`True` if this function is called a second
time in the same tick, e.g. after a route update.
See Also:
- :py:attr:`information_manager`
Executes the phases:
- :py:class:`Phase.UPDATE_INFORMATION | Phase.BEGIN<.Phase>`
- :py:class:`Phase.UPDATE_INFORMATION | Phase.END<.Phase>`
"""
self.execute_phase(Phase.UPDATE_INFORMATION | Phase.BEGIN, prior_results=None)
self._update_information(second_pass=second_pass)
self.execute_phase(Phase.UPDATE_INFORMATION | Phase.END, prior_results=None)
def _update_information(self, *, second_pass: bool = False):
"""
This method updates the information regarding the ego
vehicle based on the surrounding world.
second_pass = True will skip some calculations,
especially useful if a second call to _update_information is necessary in the same tick.
Assumes: second_pass == True => agent.done() == False
Note:
Does not execute any Phase.
"""
# --------------------------------------------------------------------------
# Information that is CONSTANT DURING THIS TICK and INDEPENDENT OF THE ROUTE
# --------------------------------------------------------------------------
if not second_pass:
if self._debug: # noqa: SIM102
if not self._lights_list and len(CarlaDataProvider._traffic_light_map.keys()): # pyright: ignore[reportPrivateUsage]
logger.error("Traffic light list is empty, but map is not.")
# ----------------------------
# First Pass for expensive and tick-constant information
# --- InformationManager ---
information: InformationManager.Information = self.information_manager.tick() # NOTE: # Warning: Currently not route-dependant, might need to be changed later
self.tick_information = information
self._current_waypoint = information.current_waypoint
self.current_states = information.current_states
# Maybe access over subattribute, or properties
# NOTE: If other scripts, e.g. the scenario_runner, are used in parallel or sync=False
# the stored actors might could have been destroyed later on.
# Global Information
self.all_vehicles = information.vehicles
self.all_walkers = information.walkers
self.all_static_obstacles = information.static_obstacles
# Combination of the three lists
self.all_obstacles = information.obstacles
# Filtered by config.obstacles.nearby_vehicles_max_distance and nearby_vehicles_max_distance
self.vehicles_nearby = information.vehicles_nearby
self.walkers_nearby = information.walkers_nearby
self.static_obstacles_nearby = information.static_obstacles_nearby
# Combination of the three lists
self.all_obstacles_nearby = information.obstacles_nearby
self.traffic_lights_nearby = information.traffic_lights_nearby
# Find vehicles and walkers nearby; could be moved to the information manager
# ----------------------------
# Data Matrix
# update not every frame to save performance
if self._detection_matrix and self._detection_matrix.sync:
self._road_matrix_counter += 1
if (self._road_matrix_counter % self.config.detection_matrix.sync_interval) == 0:
#logger.debug("Updating Road Matrix")
# TODO: Still prevent async mode from using too much resources and slowing fps down too much.
self._detection_matrix.update() # NOTE: Does nothing if in async mode. self.road_matrix is updated by another thread.
else:
pass
# used for self._local_planner.get_incoming_waypoint_and_direction
self._look_ahead_steps = int((self.live_info.current_speed_limit) / 10) # TODO: Maybe make this an interpolation and make more use of it
# ----- Follow speed limits -------
# NOTE: This is once again set in the local planner, but only on the Context config!
if self.config.speed.follow_speed_limits:
self.config.speed.target_speed = self.config.live_info.current_speed_limit
# NOTE: This is the direction used by the planner in the *last* step.
self.live_info.executed_direction = self._local_planner.target_road_option
assert self.live_info.executed_direction == self._local_planner.target_road_option, "Executed direction should not change."
# -------------------------------------------------------------------
# Information that NEEDS TO BE UPDATED AFTER a plan / ROUTE CHANGE.
# -------------------------------------------------------------------
if not self.done():
# NOTE: This should be called after
self.live_info.incoming_waypoint, self.live_info.incoming_direction = self._local_planner.get_incoming_waypoint_and_direction( # pyright: ignore[reportAttributeAccessIssue]
steps=self._look_ahead_steps)
else:
assert second_pass is False, "In the second pass the agent should have replanned and agent.done() should be False"
# Assumes second_pass is False
# Queue is empty
self.live_info.incoming_waypoint = None
self.live_info.incoming_direction = RoadOption.VOID
# Information that requires updated waypoint and route information:
self.live_info.is_taking_turn = self.is_taking_turn()
self.live_info.is_changing_lane = self.is_changing_lane()
#logger.debug(f"Incoming Direction: {str(self.live_info.incoming_direction):<20} - Second Pass: {second_pass}")
# RSS
# todo uncomment if agent is created after world model
#self.rss_set_road_boundaries_mode() # in case this was adjusted during runtime. # TODO: maybe implement this update differently. As here it is called unnecessarily often.
if self._debug:
OmegaConf.to_container(self.live_info, resolve=True, throw_on_missing=True)
def is_taking_turn(self) -> bool:
"""Checks if the agent is taking a turn in a few steps"""
return self.live_info.incoming_direction in (RoadOption.LEFT, RoadOption.RIGHT)
def is_changing_lane(self) -> bool:
"""Checks if the agent is changing lanes in a few steps"""
return self.live_info.incoming_direction in (RoadOption.CHANGELANELEFT, RoadOption.CHANGELANERIGHT)
# ------------------ Step & Loop Logic ------------------ #
def execute_phase(self, phase: Phase, *, prior_results: Any, update_controls: Optional[carla.VehicleControl] = None) -> Context:
"""
Sets the current phase of the agent and executes all rules that are associated with it.
Parameters:
phase : The phase to execute.
prior_results : The results of the previous phase, e.g. :py:attr:`detected_hazards`.
update_controls : Optionally controls that should be used from now onward.
"""
normal_next = self.current_phase.next_phase() # sanity checking if everything is correct
if self._validate_phases:
assert (normal_next in {phase, Phase.USER_CONTROLLED}
or phase & Phase.EXCEPTIONS
or phase & Phase.USER_CONTROLLED),\
f"Phase {phase} is not the next phase of {self.current_phase} or an exception phase. Expected {normal_next}"
self.current_phase = phase # set next phase
if update_controls is not None:
self.set_control(update_controls)
self.ctx.prior_result = prior_results
self.ctx.phase_results[phase] = prior_results
rules_to_check = self.rules.get(phase, ()) # use get if a custom phase is added, without a rule
try:
for rule in rules_to_check: # todo: maybe dict? grouped by phase?
assert self.current_phase in rule.phases, f"Current phase {self.current_phase} not in Rule {rule.phases}" # TODO remove:
rule(self.ctx)
# NOTE: Blocking rules can change the and above assertion will fail.
if phase != self.current_phase:
logger.warning("Phase was changed by rule %s to %s. "
"Resting self.current_phase to %s. "
"To prevent his raise an exception in the rule or adjust the phase.",
rule, self.current_phase, phase)
self.current_phase = phase
except NoFurtherRulesException:
pass
except omegaconf.ReadonlyConfigError:
print("WARNING: A action likely tried to change `ctx.config` which is non-permanent. Use `ctx.agent.config.` instead.")
raise
return self.ctx
def _plan_path_phase(self, *, second_pass: bool, debug: bool = False):
try:
self.execute_phase(Phase.PLAN_PATH | Phase.BEGIN, prior_results=None)
# User defined action
# TODO: when going around corners / junctions and the distance between waypoints is too big,
# We should replan and and make a more fine grained plan, to stay on the road.
self.execute_phase(Phase.PLAN_PATH | Phase.END, prior_results=None)
except UpdatedPathException as e:
if second_pass:
logger.warning("UpdatedPathException was raised in the second pass. This should not happen: %s. Restrict your rule on ctx.second_pass.", e)
return self.run_step(debug, second_pass=True)
return None
def _make_context(self, last_context: Union[Context, None], **kwargs: Any) -> Context:
"""Creates a new context object for the agent at the start of a step."""
if last_context is not None:
del last_context.last_context
ctx = Context(agent=self, last_context=last_context, **kwargs)
self.ctx = ctx
return ctx
def __call__(self, debug: bool = False) -> carla.VehicleControl:
"""Calculates the next vehicle control object."""
return self.run_step(debug, second_pass=False) # debug should be positional!
# Python 3.8+ add / for positional only arguments
def run_step(self, debug: bool = False, second_pass: bool = False) -> carla.VehicleControl:
"""
Calculates the next vehicle control object.
Arguments:
debug : Whether to enable debug mode.
This prints some more information and debug drawings.
second_pass : **Internal usage** set to :python:`True`
if this function is called a second time, e.g. after a route update.
Warning:
To be compatible with the :py:class:`.LunaticChallenger`, **always pass** :code:`debug` **as a positional argument**,
or use the :py:meth:`__call__` method.
"""
if not second_pass:
ctx = self._make_context(last_context=self.ctx)
else:
ctx = self.ctx
ctx.second_pass = second_pass
try:
# ----------------------------
# Phase 0 - Update Information
# ----------------------------
# > Phase.UPDATE_INFORMATION | Phase.BEGIN
self.update_information(second_pass=second_pass)
# > Phase.UPDATE_INFORMATION | Phase.END
# ----------------------------
# Phase 1 - Plan Path
# ----------------------------
# Question: What TODO if the last phase was COLLISION (async), EMERGENCY
# Some information to PLAN_PATH should reflect this
# NOTE: Currently no option to diverge from existing path here, or plan a new path
# NOTE: Currently done in the local planner and behavior functions
self._plan_path_phase(second_pass=second_pass, debug=debug)
# Check whether the agent has reached its destination.
if self.done():
# NOTE: Might be in NONE phase here.
self.execute_phase(Phase.DONE | Phase.BEGIN, prior_results=None)
if self.done():
# No Rule set a new destination
logger.info("The target has been reached, stopping the simulation")
self.execute_phase(Phase.TERMINATING | Phase.BEGIN, prior_results=None)
raise AgentDoneException
self.execute_phase(Phase.DONE | Phase.END, prior_results=None)
return self.run_step(debug, second_pass=True) # NOTE! For child classes like the leaderboard agent this calls the higher level run_step.
# ----------------------------
# Phase NONE - Before Running step
# ----------------------------
try:
planned_control = self._inner_step(debug=debug) # debug=True draws waypoints
if self.detected_hazards:
# The must_clear_hazard decorator will raise this, but in case the _inner_step is overwritten
raise EmergencyStopException(self.detected_hazards) # noqa: TRY301
# Reraise Exceptions
except UserInterruption:
raise
# Handled Exceptions
except EmergencyStopException as emergency:
# ----------------------------
# Phase Emergency
# no Rule with Phase.EMERGENCY | BEGIN cleared the provided hazards in ctx.prior_results
# ----------------------------
emergency_controls = self.emergency_manager(reasons=emergency.hazards_detected)
# TODO: somehow backup the control defined before.
self.execute_phase(Phase.EMERGENCY | Phase.END,
update_controls=emergency_controls,
prior_results=emergency.hazards_detected)
planned_control = self.get_control() # type: carla.VehicleControl # type: ignore[assignment]
except SkipInnerLoopException as skip:
self.set_control(skip.planned_control)
self.current_phase = Phase.USER_CONTROLLED
planned_control = skip.planned_control
except UpdatedPathException as update_exception:
if second_pass > 5:
logger.warning("UpdatedPathException was raised more than %s times. "
"Warning: This might be an infinite loop.", second_pass)
elif second_pass > 50:
raise RecursionError("UpdatedPathException was raised more than 50 times. "
"Assuming an infinite loop and terminating") from update_exception
else:
logger.warning("%s was raised in the inner step, this should be done in "
"Phase.PLAN_PATH instead.", update_exception)
return self.run_step(second_pass=int(second_pass) + 1) # type: ignore
except LunaticAgentException as lae:
if self.ctx.control is None:
raise ValueError(f"A VehicleControl object must be set on the agent when {type(lae).__name__} is "
"raised during `._inner_step`") from lae
planned_control = self.get_control() # type: ignore[assignment]
# assert ctx.control
# ----------------------------
# No known Phase multiple exit points
# ----------------------------
# ----------------------------
# Phase RSS - Check RSS
# ----------------------------
ctx = self.execute_phase(Phase.RSS_EVALUATION | Phase.BEGIN,
prior_results=None,
update_controls=planned_control)
if AD_RSS_AVAILABLE and self.config.rss and self.config.rss.enabled:
rss_updated_controls = self._world_model.rss_check_control(ctx.control) # type: ignore[arg-type]
else:
rss_updated_controls = None
# NOTE: rss_updated_controls could be None.
ctx = self.execute_phase(Phase.RSS_EVALUATION | Phase.END, prior_results=rss_updated_controls)
#if ctx.control is not planned_control:
# logger.debug("RSS updated control accepted.")
except ContinueLoopException as e:
logger.debug("ContinueLoopException skipping rest of loop.")
if self.ctx.control is None:
raise ValueError(f"A VehicleControl object must be set on the agent when {type(e).__name__} is raised during `._inner_step`") from e
planned_control = self.ctx.control # type: carla.VehicleControl # type: ignore[assignment]
planned_control.manual_gear_shift = False
return self.get_control() # type: ignore[return-value]
@must_clear_hazard
@result_to_context("control")
def _inner_step(self, debug: bool = False) -> carla.VehicleControl:
"""
This is is the internal function to provide the next control object for
the agent; it should run every tick.
Raises:
EmergencyStopException: If :py:attr:`detected_hazards` is not empty when the
function returns.
:meta public:
"""
self.debug = debug
# ----------------------------
# Phase 2 - Detection of Pedestrians and Traffic Lights
# ----------------------------
# Detect hazards
# phases are executed in detect_hazard
# > Phase.DETECT_TRAFFIC_LIGHTS | Phase.BEGIN # phases executed inside
pedestrians_and_tlight_hazard = self.detect_hazard()
# > Phase.DETECT_PEDESTRIANS | Phase.END
# Pedestrian avoidance behaviors
# currently doing either emergency (detect_hazard) stop or nothing
if self.detected_hazards:
# ----------------------------
# Phase Hazard Detected (traffic light or pedestrian)
# If no Rule with Phase.EMERGENCY | BEGIN clears pedestrians_or_traffic_light
# An EmergencyStopException is raised
# ----------------------------
self.react_to_hazard(pedestrians_and_tlight_hazard) # Optional[NoReturn]
# -----------------------------
# Phase Detect Static Obstacles
# -----------------------------
self.execute_phase(Phase.DETECT_STATIC_OBSTACLES | Phase.BEGIN, prior_results=None)
static_obstacle_detection_result = self.detect_obstacles_in_path(self.static_obstacles_nearby)
if static_obstacle_detection_result.obstacle_was_found:
self.current_states[AgentState.BLOCKED_BY_STATIC] += 1
# Must plan around it
self.add_hazard(Hazard.STATIC_OBSTACLE)
else:
self.current_states[AgentState.BLOCKED_BY_STATIC] = 0
# TODO: add a basic rule for circumventing static obstacles
self.execute_phase(Phase.DETECT_STATIC_OBSTACLES | Phase.END, prior_results=static_obstacle_detection_result)
# Not throwing an error here yet
# ----------------------------
# Phase 3 - Detection of Cars
# ----------------------------
self.execute_phase(Phase.DETECT_CARS | Phase.BEGIN, prior_results=None) # TODO: Maybe add some prio result
vehicle_detection_result = self.detect_obstacles_in_path(self.vehicles_nearby)
# TODO: add a way to let the execution overwrite
if vehicle_detection_result.obstacle_was_found:
# ----------------------------
# Phase 2.A - React to cars in front
# TODO: turn this into a rule.
# remove CAR_DETECTED -> pass detection_result to rules
# TODO some way to circumvent returning control here, like above.
# TODO: Needs refinement with the car_following_behavior
# ----------------------------
self.execute_phase(Phase.CAR_DETECTED | Phase.BEGIN, prior_results=vehicle_detection_result)
control = self.car_following_behavior(*vehicle_detection_result) # type: ignore[arg-type]
# NOTE: might throw EmergencyStopException
self.execute_phase(Phase.CAR_DETECTED | Phase.END, update_controls=control, prior_results=vehicle_detection_result)
return self.get_control() # type: ignore[return-value]
#TODO: maybe new phase instead of END or remove CAR_DETECTED and handle as rules (maybe better)
self.execute_phase(Phase.DETECT_CARS | Phase.END, prior_results=None) # NOTE: avoiding tailgate here
# Intersection behavior
# NOTE: is_taking_turn <- incoming_direction in (RoadOption.LEFT, RoadOption.RIGHT)
if self.live_info.incoming_waypoint.is_junction and self.is_taking_turn(): # pyright: ignore[reportOptionalMemberAccess]
# ----------------------------
# Phase Turning at Junction
# ----------------------------
self.execute_phase(Phase.TURNING_AT_JUNCTION | Phase.BEGIN, prior_results=None)
control = self._calculate_control(debug=debug)
self.execute_phase(Phase.TURNING_AT_JUNCTION | Phase.END, update_controls=control, prior_results=None)
return self.get_control() # type: ignore[return-value]
# ----------------------------
# Phase 4 - Plan Path normally
# ----------------------------
# Normal behavior
self.execute_phase(Phase.TAKE_NORMAL_STEP | Phase.BEGIN, prior_results=None)
control = self._calculate_control(debug=debug)
self.execute_phase(Phase.TAKE_NORMAL_STEP | Phase.END, prior_results=None, update_controls=control)
# Leave loop and apply controls outside
return self.get_control() # type: ignore[return-value]
def _calculate_control(self, debug: bool = False):
"""
Plan the next step of the agent. This will execute the local planner
to retrieve the next control fitting the current path and settings.
Note:
This is the innermost function of the agents run_step function.
It should be called each step to acquire a desired control object.
Use this function inside rules if a control object is desired.
**[Context.get_or_calculate_control](#Context.get_or_calculate_control)
is a safer alternative to this function**
Warning:
If you do not use this function in a [`BlockingRule`](#BlockingRule)
you should raise a `SkipInnerLoopException` or `ContinueLoopException`
else the planned path will skip a waypoint.
Warning:
This function only calculates and returns the control object directly.
**It does not set the :py:attr:`agent/ctx.control <control>` attribute which is the one
the agent uses in [`apply_control`](#apply_control) to apply the final controls.**
"""
if self.ctx.control is not None:
logger.error("Control was set before calling _calculate_control. This might lead to unexpected behavior.")
return self._local_planner.run_step(debug)
def parse_keyboard_input(self, allow_user_updates: bool = True,
*, control: Optional[carla.VehicleControl] = None) -> None:
"""
Parse the current user input and allow manual updates of the controls.
Args:
allow_user_updates: If :python:`True`, the user can update the controls manually.
Otherwise only the normal hotkeys do work.
Executes Phases:
- :py:class:`Phase.APPLY_MANUAL_CONTROLS | Phase.BEGIN <classes.constants.Phase>`
- :py:class:`Phase.APPLY_MANUAL_CONTROLS | Phase.END <classes.constants.Phase>`
"""
planned_control = control or self.get_control()
self.execute_phase(Phase.APPLY_MANUAL_CONTROLS | Phase.BEGIN, prior_results=planned_control)
# Controls can be updated inplace by the user.
if self._world_model.controller.parse_events(planned_control if allow_user_updates else carla.VehicleControl()): # pyright: ignore[reportOptionalMemberAccess]
print("Exiting by user input.")
raise UserInterruption("Exiting by user input.")
self.execute_phase(Phase.APPLY_MANUAL_CONTROLS | Phase.END, prior_results=None)
def apply_control(self, control: Optional[carla.VehicleControl] = None):
"""
Applies the control to the agent's actor.
Will execute the :py:class:`Phase.EXECUTION | Phase.BEGIN <classes.constants.Phase>`
and :py:class:`Phase.EXECUTION | Phase.END <classes.constants.Phase>` phases.
Note:
The final control object that is applied to the agent's actor
is stored in the :py:attr:`ctx.control <ctx>` attribute.
Raises ValueError:
If the control object is not set, i.e. :py:meth:`get_control` returns :python:`None`.
"""
if control is None:
control = self.get_control()
if control is None:
raise ValueError("The agent has not yet performed a step this tick "
"and has no control object was passed.")
if self.current_phase != Phase.EXECUTION | Phase.BEGIN:
self.execute_phase(Phase.EXECUTION | Phase.BEGIN, prior_results=control, update_controls=control)
else:
logger.debug("Agent is already in execution phase.")
# Set automatic control-related vehicle lights
final_control: carla.VehicleControl = self.get_control() # type: ignore[assignment]
self._update_lights(final_control)
self._vehicle.apply_control(final_control)
self.execute_phase(Phase.EXECUTION | Phase.END, prior_results=final_control)
# ------------------ Hazard Detection & Reaction ------------------ #
from agents.substep_managers import detect_traffic_light # -> TrafficLightDetectionResult
traffic_light_manager = detect_traffic_light # pyright: ignore[reportAssignmentType]
"""Alias of :py:meth:`detect_traffic_light`"""
def detect_hazard(self) -> Set[Hazard]:
"""
Checks for red traffic lights and pedestrians in the agents path.
If :py:attr:`.LunaticAgentSettings.obstacles.detect_yellow_tlights` is set to :python:`True`,
then yellow traffic lights will also be regarded as a hazard that can trigger an
:py:exc:`EmergencyStopException` in :py:meth:`react_to_hazard` that is executed after this
function.
"""
# Red lights and stops behavior
self.execute_phase(Phase.DETECT_TRAFFIC_LIGHTS | Phase.BEGIN, prior_results=None)
tlight_detection_result: TrafficLightDetectionResult = self.detect_traffic_light()
if tlight_detection_result.traffic_light_was_found:
if tlight_detection_result.traffic_light.state == carla.TrafficLightState.Red: # pyright: ignore[reportOptionalMemberAccess]
self.add_hazard(Hazard.TRAFFIC_LIGHT_RED)
else: # NOTE: self.config.obstacles.detect_yellow_tlights must be True
self.add_hazard(Hazard.TRAFFIC_LIGHT_YELLOW, HazardSeverity.WARNING)
#assert self.live_info.next_traffic_light.id == tlight_detection_result.traffic_light.id, "Next assumed traffic light should be the same as the detected one." # TEMP
# NOTE next tlight is the next bounding box and might not be the next "correct" one
# self.live_info.next_traffic_light.id != tlight_detection_result.traffic_light.id: