CARLA
 
载入中...
搜索中...
未找到
CarlaEpisode.h
浏览该文件的文档.
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
18
19#include "GameFramework/Pawn.h"
20#include "Materials/MaterialParameterCollectionInstance.h"
21
25#include <carla/ros2/ROS2.h>
26#include <carla/rpc/Actor.h>
31
32#include "CarlaEpisode.generated.h"
33
34/// A simulation episode.
35///
36/// Each time the level is restarted a new episode is created.
37UCLASS(BlueprintType, Blueprintable)
38class CARLA_API UCarlaEpisode : public UObject
39{
40 GENERATED_BODY()
41
42 // ===========================================================================
43 // -- Constructor ------------------------------------------------------------
44 // ===========================================================================
45
46public:
47
48 UCarlaEpisode(const FObjectInitializer &ObjectInitializer);
49
50 // ===========================================================================
51 // -- Load a new episode -----------------------------------------------------
52 // ===========================================================================
53
54 /// Load a new map and start a new episode.
55 ///
56 /// If @a MapString is empty, the current map is reloaded.
57 UFUNCTION(BlueprintCallable)
58 bool LoadNewEpisode(const FString &MapString, bool ResetSettings = true);
59
60 /// Load a new map generating the mesh from OpenDRIVE data and
61 /// start a new episode.
62 ///
63 /// If @a MapString is empty, it fails.
64 bool LoadNewOpendriveEpisode(
65 const FString &OpenDriveString,
67
68 // ===========================================================================
69 // -- Episode settings -------------------------------------------------------
70 // ===========================================================================
71
72 UFUNCTION(BlueprintCallable)
73 const FEpisodeSettings &GetSettings() const
74 {
75 return EpisodeSettings;
76 }
77
78 UFUNCTION(BlueprintCallable)
79 void ApplySettings(const FEpisodeSettings &Settings);
80
81 // ===========================================================================
82 // -- Retrieve info about this episode ---------------------------------------
83 // ===========================================================================
84
85 /// Return the unique id of this episode.
86 auto GetId() const
87 {
88 return Id;
89 }
90
91 /// Return the name of the map loaded in this episode.
92 UFUNCTION(BlueprintCallable)
93 const FString &GetMapName() const
94 {
95 return MapName;
96 }
97
98 /// Game seconds since the start of this episode.
99 double GetElapsedGameTime() const
100 {
101 return ElapsedGameTime;
102 }
103
104 /// Visual game seconds
105 double GetVisualGameTime() const
106 {
107 return VisualGameTime;
108 }
109
110 void SetVisualGameTime(double Time)
111 {
112 VisualGameTime = Time;
113
114 // update time in material parameters also
115 if (MaterialParameters)
116 {
117 MaterialParameters->SetScalarParameterValue(FName("VisualTime"), VisualGameTime);
118 }
119 }
120
121 /// Return the list of actor definitions that are available to be spawned this
122 /// episode.
123 UFUNCTION(BlueprintCallable)
124 const TArray<FActorDefinition> &GetActorDefinitions() const
125 {
126 return ActorDispatcher->GetActorDefinitions();
127 }
128
129 /// Return the list of recommended spawn points for vehicles.
130 UFUNCTION(BlueprintCallable)
131 TArray<FTransform> GetRecommendedSpawnPoints() const;
132
133 /// Return the GeoLocation point of the map loaded
135 {
136 return MapGeoReference;
137 }
138
139 // ===========================================================================
140 // -- Retrieve special actors ------------------------------------------------
141 // ===========================================================================
142
143 UFUNCTION(BlueprintCallable)
144 APawn *GetSpectatorPawn() const
145 {
146 return Spectator;
147 }
148
149 UFUNCTION(BlueprintCallable)
150 AWeather *GetWeather() const
151 {
152 return Weather;
153 }
154
156 {
157 return ActorDispatcher->GetActorRegistry();
158 }
159
161 {
162 return ActorDispatcher->GetActorRegistry();
163 }
164
165 // ===========================================================================
166 // -- Actor look up methods --------------------------------------------------
167 // ===========================================================================
168
169 /// Find a Carla actor by id.
170 ///
171 /// If the actor is not found or is pending kill, the returned view is
172 /// invalid.
174 {
175 return ActorDispatcher->GetActorRegistry().FindCarlaActor(ActorId);
176 }
177
178 /// Find the actor view of @a Actor.
179 ///
180 /// If the actor is not found or is pending kill, the returned view is
181 /// invalid.
183 {
184 return ActorDispatcher->GetActorRegistry().FindCarlaActor(Actor);
185 }
186
187 /// Get the description of the Carla actor (sensor) using specific stream id.
188 ///
189 /// If the actor is not found returns an empty string
191 {
192 return ActorDispatcher->GetActorRegistry().GetDescriptionFromStream(StreamId);
193 }
194
195 // ===========================================================================
196 // -- Actor handling methods -------------------------------------------------
197 // ===========================================================================
198
199 /// Spawns an actor based on @a ActorDescription at @a Transform. To properly
200 /// despawn an actor created with this function call DestroyActor.
201 ///
202 /// @return A pair containing the result of the spawn function and a view over
203 /// the actor and its properties. If the status is different of Success the
204 /// view is invalid.
205 TPair<EActorSpawnResultStatus, FCarlaActor*> SpawnActorWithInfo(
206 const FTransform &Transform,
207 FActorDescription thisActorDescription,
208 FCarlaActor::IdType DesiredId = 0);
209
210 /// Spawns an actor based on @a ActorDescription at @a Transform.
211 ///
212 /// @return the actor to be spawned
214 const FTransform &Transform,
215 FActorDescription thisActorDescription)
216 {
217 FTransform NewTransform = Transform;
218 auto result = ActorDispatcher->ReSpawnActor(NewTransform, thisActorDescription);
219 if (Recorder->IsEnabled())
220 {
221 // do something?
222 }
223
224 return result;
225 }
226
227 /// Spawns an actor based on @a ActorDescription at @a Transform. To properly
228 /// despawn an actor created with this function call DestroyActor.
229 ///
230 /// @return nullptr on failure.
231 ///
232 /// @note Special overload for blueprints.
233 UFUNCTION(BlueprintCallable)
234 AActor *SpawnActor(
235 const FTransform &Transform,
236 FActorDescription ActorDescription)
237 {
238 return SpawnActorWithInfo(Transform, std::move(ActorDescription)).Value->GetActor();
239 }
240
241 /// Attach @a Child to @a Parent.
242 ///
243 /// @pre Actors cannot be null.
244 UFUNCTION(BlueprintCallable)
245 void AttachActors(
246 AActor *Child,
247 AActor *Parent,
248 EAttachmentType InAttachmentType = EAttachmentType::Rigid,
249 const FString& SocketName = "");
250
251 /// @copydoc FActorDispatcher::DestroyActor(AActor*)
252 UFUNCTION(BlueprintCallable)
253 bool DestroyActor(AActor *Actor)
254 {
255 FCarlaActor* CarlaActor = FindCarlaActor(Actor);
256 if (CarlaActor)
257 {
258 carla::rpc::ActorId ActorId = CarlaActor->GetActorId();
259 return DestroyActor(ActorId);
260 }
261 return false;
262 }
263
265 {
266 if (bIsPrimaryServer)
267 {
268 GetFrameData().AddEvent(
269 CarlaRecorderEventDel{ActorId});
270 }
271 if (Recorder->IsEnabled())
272 {
273 // recorder event
274 CarlaRecorderEventDel RecEvent{ActorId};
275 Recorder->AddEvent(std::move(RecEvent));
276 }
277
278 return ActorDispatcher->DestroyActor(ActorId);
279 }
280
282 {
283 ActorDispatcher->PutActorToSleep(ActorId, this);
284 }
285
287 {
288 ActorDispatcher->WakeActorUp(ActorId, this);
289 }
290
291 // ===========================================================================
292 // -- Other methods ----------------------------------------------------------
293 // ===========================================================================
294
295 /// Create a serializable object describing the actor.
296 carla::rpc::Actor SerializeActor(FCarlaActor* CarlaActor) const;
297
298 /// Create a serializable object describing the actor.
299 /// Can be used to serialized actors that are not in the registry
300 carla::rpc::Actor SerializeActor(AActor* Actor) const;
301
302 // ===========================================================================
303 // -- Private methods and members --------------------------------------------
304 // ===========================================================================
305
307 {
308 return Recorder;
309 }
310
312 {
313 Recorder = Rec;
314 }
315
317 {
318 return Recorder->GetReplayer();
319 }
320
321 std::string StartRecorder(std::string name, bool AdditionalData);
322
323 FIntVector GetCurrentMapOrigin() const { return CurrentMapOrigin; }
324
325 void SetCurrentMapOrigin(const FIntVector& NewOrigin) { CurrentMapOrigin = NewOrigin; }
326
327 FFrameData& GetFrameData() { return FrameData; }
328
329 FSensorManager& GetSensorManager() { return SensorManager; }
330
331 bool bIsPrimaryServer = true;
332
333private:
334
335 friend class ACarlaGameModeBase;
336 friend class FCarlaEngine;
337
338 void InitializeAtBeginPlay();
339
340 void EndPlay();
341
343 {
344 ActorDispatcher->Bind(ActorFactory);
345 }
346
347 std::pair<int, FCarlaActor&> TryToCreateReplayerActor(
348 FVector &Location,
349 FVector &Rotation,
350 FActorDescription &ActorDesc,
351 unsigned int desiredId);
352
353 bool SetActorSimulatePhysics(FCarlaActor &CarlaActor, bool bEnabled);
354
355 bool SetActorCollisions(FCarlaActor &CarlaActor, bool bEnabled);
356
357 bool SetActorDead(FCarlaActor &CarlaActor);
358
359 void TickTimers(float DeltaSeconds)
360 {
361 ElapsedGameTime += DeltaSeconds;
362 SetVisualGameTime(VisualGameTime + DeltaSeconds);
363 #if defined(WITH_ROS2)
364 auto ROS2 = carla::ros2::ROS2::GetInstance();
365 if (ROS2->IsEnabled())
366 ROS2->SetTimestamp(GetElapsedGameTime());
367 #endif
368
369 }
370
371 const uint64 Id = 0u;
372
373 // simulation time
374 double ElapsedGameTime = 0.0;
375
376 // visual time (used by clounds and other FX that need to be deterministic)
377 double VisualGameTime = 0.0;
378
379 UPROPERTY(VisibleAnywhere)
380 FString MapName;
381
382 UPROPERTY(VisibleAnywhere)
383 FEpisodeSettings EpisodeSettings;
384
385 UPROPERTY(VisibleAnywhere)
386 UActorDispatcher *ActorDispatcher = nullptr;
387
388 UPROPERTY(VisibleAnywhere)
389 APawn *Spectator = nullptr;
390
391 UPROPERTY(VisibleAnywhere)
392 AWeather *Weather = nullptr;
393
394 UPROPERTY(VisibleAnywhere)
395 UMaterialParameterCollectionInstance *MaterialParameters = nullptr;
396
398
399 carla::geom::GeoLocation MapGeoReference;
400
401 FIntVector CurrentMapOrigin;
402
403 FFrameData FrameData;
404
405 FSensorManager SensorManager;
406};
407
408FString CarlaGetRelevantTagAsString(const TSet<crp::CityObjectLabel> &SemanticTags);
EAttachmentType
FString CarlaGetRelevantTagAsString(const TSet< crp::CityObjectLabel > &SemanticTags)
Base class for Carla actor factories.
Base class for the CARLA Game Mode.
Recorder for the simulation
A registry of all the Carla actors.
A view over an actor and its properties.
Definition CarlaActor.h:25
uint32 IdType
Definition CarlaActor.h:28
IdType GetActorId() const
Definition CarlaActor.h:81
ACarlaRecorder * Recorder
Object in charge of binding ActorDefinitions to spawn functions, as well as keeping the registry of a...
A simulation episode.
FIntVector GetCurrentMapOrigin() const
auto GetId() const
Return the unique id of this episode.
FFrameData & GetFrameData()
double GetVisualGameTime() const
Visual game seconds
bool SetActorSimulatePhysics(FCarlaActor &CarlaActor, bool bEnabled)
CarlaReplayer * GetReplayer() const
void PutActorToSleep(carla::rpc::ActorId ActorId)
void SetCurrentMapOrigin(const FIntVector &NewOrigin)
FCarlaActor * FindCarlaActor(FCarlaActor::IdType ActorId)
Find a Carla actor by id.
FCarlaActor * FindCarlaActor(AActor *Actor) const
Find the actor view of Actor.
void RegisterActorFactory(ACarlaActorFactory &ActorFactory)
double GetElapsedGameTime() const
Game seconds since the start of this episode.
FString GetActorDescriptionFromStream(carla::streaming::detail::stream_id_type StreamId)
Get the description of the Carla actor (sensor) using specific stream id.
bool DestroyActor(carla::rpc::ActorId ActorId)
void SetRecorder(ACarlaRecorder *Rec)
AActor * ReSpawnActorWithInfo(const FTransform &Transform, FActorDescription thisActorDescription)
Spawns an actor based on ActorDescription at Transform.
std::pair< int, FCarlaActor & > TryToCreateReplayerActor(FVector &Location, FVector &Rotation, FActorDescription &ActorDesc, unsigned int desiredId)
void TickTimers(float DeltaSeconds)
bool SetActorCollisions(FCarlaActor &CarlaActor, bool bEnabled)
bool SetActorDead(FCarlaActor &CarlaActor)
void SetVisualGameTime(double Time)
const FActorRegistry & GetActorRegistry() const
ACarlaRecorder * GetRecorder() const
FSensorManager & GetSensorManager()
FActorRegistry & GetActorRegistry()
void WakeActorUp(carla::rpc::ActorId ActorId)
const carla::geom::GeoLocation & GetGeoReference() const
Return the GeoLocation point of the map loaded
static std::shared_ptr< ROS2 > GetInstance()
Definition ROS2.h:51
uint32_t ActorId
Definition ActorId.h:14
uint32_t stream_id_type
Definition Types.h:18
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133
A definition of a Carla Actor with all the variation and attributes.
A description of a Carla Actor with all its variation.
Seting for map generation from opendrive without additional geometry