CARLA
CarlaEpisode.h
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
15 #include "Carla/Weather/Weather.h"
16 
17 #include "GameFramework/Pawn.h"
18 
20 #include <carla/geom/BoundingBox.h>
21 #include <carla/geom/GeoLocation.h>
22 #include <carla/rpc/Actor.h>
25 #include <carla/streaming/Server.h>
27 
28 #include "CarlaEpisode.generated.h"
29 
30 /// A simulation episode.
31 ///
32 /// Each time the level is restarted a new episode is created.
33 UCLASS(BlueprintType, Blueprintable)
34 class CARLA_API UCarlaEpisode : public UObject
35 {
36  GENERATED_BODY()
37 
38  // ===========================================================================
39  // -- Constructor ------------------------------------------------------------
40  // ===========================================================================
41 
42 public:
43 
44  UCarlaEpisode(const FObjectInitializer &ObjectInitializer);
45 
46  // ===========================================================================
47  // -- Load a new episode -----------------------------------------------------
48  // ===========================================================================
49 
50  /// Load a new map and start a new episode.
51  ///
52  /// If @a MapString is empty, the current map is reloaded.
53  UFUNCTION(BlueprintCallable)
54  bool LoadNewEpisode(const FString &MapString, bool ResetSettings = true);
55 
56  /// Load a new map generating the mesh from OpenDRIVE data and
57  /// start a new episode.
58  ///
59  /// If @a MapString is empty, it fails.
60  bool LoadNewOpendriveEpisode(
61  const FString &OpenDriveString,
63 
64  // ===========================================================================
65  // -- Episode settings -------------------------------------------------------
66  // ===========================================================================
67 
68  UFUNCTION(BlueprintCallable)
69  const FEpisodeSettings &GetSettings() const
70  {
71  return EpisodeSettings;
72  }
73 
74  UFUNCTION(BlueprintCallable)
75  void ApplySettings(const FEpisodeSettings &Settings);
76 
77  // ===========================================================================
78  // -- Retrieve info about this episode ---------------------------------------
79  // ===========================================================================
80 
81  /// Return the unique id of this episode.
82  auto GetId() const
83  {
84  return Id;
85  }
86 
87  /// Return the name of the map loaded in this episode.
88  UFUNCTION(BlueprintCallable)
89  const FString &GetMapName() const
90  {
91  return MapName;
92  }
93 
94  /// Game seconds since the start of this episode.
95  double GetElapsedGameTime() const
96  {
97  return ElapsedGameTime;
98  }
99 
100  /// Return the list of actor definitions that are available to be spawned this
101  /// episode.
102  UFUNCTION(BlueprintCallable)
103  const TArray<FActorDefinition> &GetActorDefinitions() const
104  {
105  return ActorDispatcher->GetActorDefinitions();
106  }
107 
108  /// Return the list of recommended spawn points for vehicles.
109  UFUNCTION(BlueprintCallable)
110  TArray<FTransform> GetRecommendedSpawnPoints() const;
111 
112  /// Return the GeoLocation point of the map loaded
114  {
115  return MapGeoReference;
116  }
117 
118  // ===========================================================================
119  // -- Retrieve special actors ------------------------------------------------
120  // ===========================================================================
121 
122  UFUNCTION(BlueprintCallable)
123  APawn *GetSpectatorPawn() const
124  {
125  return Spectator;
126  }
127 
128  UFUNCTION(BlueprintCallable)
129  AWeather *GetWeather() const
130  {
131  return Weather;
132  }
133 
135  {
136  return ActorDispatcher->GetActorRegistry();
137  }
138 
140  {
141  return ActorDispatcher->GetActorRegistry();
142  }
143 
144  // ===========================================================================
145  // -- Actor look up methods --------------------------------------------------
146  // ===========================================================================
147 
148  /// Find a Carla actor by id.
149  ///
150  /// If the actor is not found or is pending kill, the returned view is
151  /// invalid.
153  {
154  return ActorDispatcher->GetActorRegistry().FindCarlaActor(ActorId);
155  }
156 
157  /// Find the actor view of @a Actor.
158  ///
159  /// If the actor is not found or is pending kill, the returned view is
160  /// invalid.
162  {
163  return ActorDispatcher->GetActorRegistry().FindCarlaActor(Actor);
164  }
165 
166  // ===========================================================================
167  // -- Actor handling methods -------------------------------------------------
168  // ===========================================================================
169 
170  /// Spawns an actor based on @a ActorDescription at @a Transform. To properly
171  /// despawn an actor created with this function call DestroyActor.
172  ///
173  /// @return A pair containing the result of the spawn function and a view over
174  /// the actor and its properties. If the status is different of Success the
175  /// view is invalid.
176  TPair<EActorSpawnResultStatus, FCarlaActor*> SpawnActorWithInfo(
177  const FTransform &Transform,
178  FActorDescription thisActorDescription,
179  FCarlaActor::IdType DesiredId = 0);
180 
181  /// Spawns an actor based on @a ActorDescription at @a Transform.
182  ///
183  /// @return the actor to be spawned
185  const FTransform &Transform,
186  FActorDescription thisActorDescription)
187  {
188  FTransform NewTransform = Transform;
189  auto result = ActorDispatcher->ReSpawnActor(NewTransform, thisActorDescription);
190  if (Recorder->IsEnabled())
191  {
192  // do something?
193  }
194 
195  return result;
196  }
197 
198  /// Spawns an actor based on @a ActorDescription at @a Transform. To properly
199  /// despawn an actor created with this function call DestroyActor.
200  ///
201  /// @return nullptr on failure.
202  ///
203  /// @note Special overload for blueprints.
204  UFUNCTION(BlueprintCallable)
205  AActor *SpawnActor(
206  const FTransform &Transform,
207  FActorDescription ActorDescription)
208  {
209  return SpawnActorWithInfo(Transform, std::move(ActorDescription)).Value->GetActor();
210  }
211 
212  /// Attach @a Child to @a Parent.
213  ///
214  /// @pre Actors cannot be null.
215  UFUNCTION(BlueprintCallable)
216  void AttachActors(
217  AActor *Child,
218  AActor *Parent,
219  EAttachmentType InAttachmentType = EAttachmentType::Rigid);
220 
221  /// @copydoc FActorDispatcher::DestroyActor(AActor*)
222  UFUNCTION(BlueprintCallable)
223  bool DestroyActor(AActor *Actor)
224  {
225  FCarlaActor* CarlaActor = FindCarlaActor(Actor);
226  if (CarlaActor)
227  {
228  carla::rpc::ActorId ActorId = CarlaActor->GetActorId();
229  return DestroyActor(ActorId);
230  }
231  return false;
232  }
233 
235  {
236  if (Recorder->IsEnabled())
237  {
238  // recorder event
239  CarlaRecorderEventDel RecEvent{ActorId};
240  Recorder->AddEvent(std::move(RecEvent));
241  }
242 
243  return ActorDispatcher->DestroyActor(ActorId);
244  }
245 
247  {
248  ActorDispatcher->PutActorToSleep(ActorId, this);
249  }
250 
252  {
253  ActorDispatcher->WakeActorUp(ActorId, this);
254  }
255 
256  // ===========================================================================
257  // -- Other methods ----------------------------------------------------------
258  // ===========================================================================
259 
260  /// Create a serializable object describing the actor.
261  carla::rpc::Actor SerializeActor(FCarlaActor* CarlaActor) const;
262 
263  /// Create a serializable object describing the actor.
264  /// Can be used to serialized actors that are not in the registry
265  carla::rpc::Actor SerializeActor(AActor* Actor) const;
266 
267  // ===========================================================================
268  // -- Private methods and members --------------------------------------------
269  // ===========================================================================
270 
272  {
273  return Recorder;
274  }
275 
277  {
278  Recorder = Rec;
279  }
280 
282  {
283  return Recorder->GetReplayer();
284  }
285 
286  std::string StartRecorder(std::string name, bool AdditionalData);
287 
288  FIntVector GetCurrentMapOrigin() const { return CurrentMapOrigin; }
289 
290  void SetCurrentMapOrigin(const FIntVector& NewOrigin) { CurrentMapOrigin = NewOrigin; }
291 
292 private:
293 
294  friend class ACarlaGameModeBase;
295  friend class FCarlaEngine;
296 
297  void InitializeAtBeginPlay();
298 
299  void EndPlay();
300 
302  {
303  ActorDispatcher->Bind(ActorFactory);
304  }
305 
306  std::pair<int, FCarlaActor&> TryToCreateReplayerActor(
307  FVector &Location,
308  FVector &Rotation,
309  FActorDescription &ActorDesc,
310  unsigned int desiredId);
311 
312  bool SetActorSimulatePhysics(FCarlaActor &CarlaActor, bool bEnabled);
313 
314  void TickTimers(float DeltaSeconds)
315  {
316  ElapsedGameTime += DeltaSeconds;
317  }
318 
319  const uint64 Id = 0u;
320 
321  double ElapsedGameTime = 0.0;
322 
323  UPROPERTY(VisibleAnywhere)
324  FString MapName;
325 
326  UPROPERTY(VisibleAnywhere)
327  FEpisodeSettings EpisodeSettings;
328 
329  UPROPERTY(VisibleAnywhere)
330  UActorDispatcher *ActorDispatcher = nullptr;
331 
332  UPROPERTY(VisibleAnywhere)
333  APawn *Spectator = nullptr;
334 
335  UPROPERTY(VisibleAnywhere)
336  AWeather *Weather = nullptr;
337 
339 
340  carla::geom::GeoLocation MapGeoReference;
341 
342  FIntVector CurrentMapOrigin;
343 };
Base class for Carla actor factories.
FCarlaActor * FindCarlaActor(FCarlaActor::IdType ActorId)
Find a Carla actor by id.
Definition: CarlaEpisode.h:152
CarlaReplayer * GetReplayer() const
Definition: CarlaEpisode.h:281
Seting for map generation from opendrive without additional geometry.
FActorRegistry & GetActorRegistry()
Definition: CarlaEpisode.h:139
FCarlaActor * FindCarlaActor(AActor *Actor) const
Find the actor view of Actor.
Definition: CarlaEpisode.h:161
A registry of all the Carla actors.
Definition: ActorRegistry.h:20
bool DestroyActor(carla::rpc::ActorId ActorId)
Definition: CarlaEpisode.h:234
double GetElapsedGameTime() const
Game seconds since the start of this episode.
Definition: CarlaEpisode.h:95
EAttachmentType
Definition: ActorAttacher.h:20
Object in charge of binding ActorDefinitions to spawn functions, as well as keeping the registry of a...
void WakeActorUp(carla::rpc::ActorId ActorId)
Definition: CarlaEpisode.h:251
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
uint32 IdType
Definition: CarlaActor.h:27
void PutActorToSleep(carla::rpc::ActorId ActorId)
Definition: CarlaEpisode.h:246
A definition of a Carla Actor with all the variation and attributes.
geom::Location Location
Definition: rpc/Location.h:14
const FActorRegistry & GetActorRegistry() const
Definition: CarlaEpisode.h:134
carla::SharedPtr< cc::Actor > Actor
A simulation episode.
Definition: CarlaEpisode.h:34
Recorder for the simulation.
Definition: CarlaRecorder.h:67
ACarlaRecorder * Recorder
Definition: CarlaEngine.h:89
auto GetId() const
Return the unique id of this episode.
Definition: CarlaEpisode.h:82
A description of a Carla Actor with all its variation.
uint32_t ActorId
Definition: ActorId.h:14
FIntVector GetCurrentMapOrigin() const
Definition: CarlaEpisode.h:288
void RegisterActorFactory(ACarlaActorFactory &ActorFactory)
Definition: CarlaEpisode.h:301
ACarlaRecorder * GetRecorder() const
Definition: CarlaEpisode.h:271
void SetCurrentMapOrigin(const FIntVector &NewOrigin)
Definition: CarlaEpisode.h:290
void SetRecorder(ACarlaRecorder *Rec)
Definition: CarlaEpisode.h:276
geom::Rotation Rotation
Definition: rpc/Transform.h:14
const carla::geom::GeoLocation & GetGeoReference() const
Return the GeoLocation point of the map loaded.
Definition: CarlaEpisode.h:113
Base class for the CARLA Game Mode.
IdType GetActorId() const
Definition: CarlaActor.h:80
AActor * ReSpawnActorWithInfo(const FTransform &Transform, FActorDescription thisActorDescription)
Spawns an actor based on ActorDescription at Transform.
Definition: CarlaEpisode.h:184
geom::Transform Transform
Definition: rpc/Transform.h:16
void TickTimers(float DeltaSeconds)
Definition: CarlaEpisode.h:314
A view over an actor and its properties.
Definition: CarlaActor.h:23