16#include <util/ue-header-guard-begin.h>
17#include "GameFramework/Controller.h"
24#include <util/ue-header-guard-end.h>
37 UE_LOG(LogCarla, Warning, TEXT(
"Invalid definition '%s' ignored"), *Definition.
Id);
51 for (
auto It =
Classes.CreateIterator(); It; ++It)
53 const bool bStillDefined =
Definitions.ContainsByPredicate(
66 const bool bAlreadyBound =
Definitions.ContainsByPredicate(
67 [&Definition](
const FActorDefinition &Existing) {
return Existing.
Id == Definition.Id; });
73 return ActorFactory.
SpawnActor(Transform, Description);
83 return ActorFactory.
SpawnActor(Transform, Description);
89 const FTransform &Transform,
95 UE_LOG(LogCarla, Error, TEXT(
"Invalid ActorDescription '%s' (UId=%d)"), *Description.
Id, Description.
UId);
96 return MakeTuple(EActorSpawnResultStatus::InvalidDescription,
nullptr);
99 UE_LOG(LogCarla, Log, TEXT(
"Spawning actor '%s'"), *Description.
Id);
101 auto Class =
Classes.Find(Description.
Id);
102 if (Class ==
nullptr)
104 UE_LOG(LogCarla, Error, TEXT(
"Invalid ActorDescription '%s' (UId=%d)"), *Description.
Id, Description.
UId);
105 return MakeTuple(EActorSpawnResultStatus::InvalidDescription,
nullptr);
108 Description.
Class = *Class;
111 if ((Result.
Status == EActorSpawnResultStatus::Success) && (Result.
Actor ==
nullptr))
113 UE_LOG(LogCarla, Warning, TEXT(
"ActorSpawnResult: Trying to spawn '%s'"), *Description.
Id);
114 UE_LOG(LogCarla, Warning, TEXT(
"ActorSpawnResult: Reported success but did not return an actor"));
115 Result.
Status = EActorSpawnResultStatus::UnknownError;
131 UE_LOG(LogCarla, Warning, TEXT(
"Failed to spawn actor '%s'"), *Description.
Id);
132 check(Result.
Status != EActorSpawnResultStatus::Success);
135 return MakeTuple(Result.
Status, View);
139 const FTransform &Transform,
144 UE_LOG(LogCarla, Error, TEXT(
"Invalid ActorDescription '%s' (UId=%d)"), *Description.
Id, Description.
UId);
148 UE_LOG(LogCarla, Log, TEXT(
"Spawning actor '%s'"), *Description.
Id);
150 auto Class =
Classes.Find(Description.
Id);
151 if (Class ==
nullptr)
153 UE_LOG(LogCarla, Error, TEXT(
"Invalid ActorDescription '%s' (UId=%d)"), *Description.
Id, Description.
UId);
157 Description.
Class = *Class;
160 if ((Result.
Status == EActorSpawnResultStatus::Success) && (Result.
Actor ==
nullptr))
162 UE_LOG(LogCarla, Warning, TEXT(
"ActorSpawnResult: Trying to spawn '%s'"), *Description.
Id);
163 UE_LOG(LogCarla, Warning, TEXT(
"ActorSpawnResult: Reported success but did not return an actor"));
164 Result.
Status = EActorSpawnResultStatus::UnknownError;
168 if (Result.
Status == EActorSpawnResultStatus::Success)
184 UE_LOG(LogCarla, Warning, TEXT(
"Trying to destroy actor that is not in the registry"));
194 APawn* Pawn = Cast<APawn>(Actor);
195 AController* Controller = (Pawn !=
nullptr ? Pawn->GetController() :
nullptr);
196 if (Controller !=
nullptr)
198 UE_LOG(LogCarla, Log, TEXT(
"Destroying actor's controller: '%s'"), *Id);
199 bool Success = Controller->Destroy();
202 UE_LOG(LogCarla, Error, TEXT(
"Failed to destroy actor's controller: '%s'"), *Id);
207 UE_LOG(LogCarla, Log, TEXT(
"UActorDispatcher::Destroying actor: '%s' %x"), *Id, Actor);
208 UE_LOG(LogCarla, Log, TEXT(
" %s"), Actor?*Actor->GetName():*FString(
"None"));
209 if (!Actor || !Actor->Destroy())
211 UE_LOG(LogCarla, Error, TEXT(
"Failed to destroy actor: '%s'"), *Id);
234 if (ROS2->IsEnabled())
238 std::string RosTopicName;
241 if (Attr.Key ==
"ros_name")
243 RosName = std::string(TCHAR_TO_UTF8(*Attr.Value.Value));
245 if (Attr.Key ==
"ros_topic_name")
247 RosTopicName = std::string(TCHAR_TO_UTF8(*Attr.Value.Value));
250 const std::string
id = std::string(TCHAR_TO_UTF8(*Description.
Id));
251 std::string ResolvedRosName;
253 if(RosName.find(
"vehicle") != std::string::npos)
255 ResolvedRosName =
"vehicle" + std::to_string(View->
GetActorId());
259 size_t pos = RosName.find_last_of(
'.');
260 if (pos != std::string::npos) {
261 ResolvedRosName = RosName.substr(pos + 1) +
"__";
265 ResolvedRosName = RosName;
267 if (!ResolvedRosName.empty())
269 ROS2->RegisterSensor(
static_cast<void*
>(&Actor), ResolvedRosName, ResolvedRosName,
true);
273 ROS2->AddActorRosTopicName(
static_cast<void*
>(&Actor), RosTopicName ==
id ?
"" : RosTopicName);
278 bool bIsHero =
false;
279 bool bEnableAckermannControl =
false;
280 bool bEnableAutowareControl =
false;
283 if (Attr.Key ==
"role_name" && (Attr.Value.Value ==
"hero" || Attr.Value.Value ==
"ego"))
287 else if (Attr.Key ==
"ros2_ackermann_control")
289 bEnableAckermannControl = Attr.Value.Value.ToBool();
291 else if (Attr.Key ==
"ros2_autoware_control")
293 bEnableAutowareControl = Attr.Value.Value.ToBool();
298 if (bEnableAutowareControl)
304 ROS2->RegisterVehicle(
static_cast<void*
>(&Actor), ResolvedRosName, ResolvedRosName, [ResolvedRosName](
void *Actor,
carla::ros2::ROS2CallbackData Data) ->
void
308 std::visit(Handler, Data);
309 }, bEnableAckermannControl, bEnableAutowareControl);
310 #if defined(WITH_ROS2_DEMO)
315 std::visit(Handler, Data);
341 if (ROS2->IsEnabled())
343 void *ActorKey =
reinterpret_cast<void *
>(Actor);
348 ROS2->UnregisterVehicle(ActorKey);
349 ROS2->RemoveActorRosTopicName(ActorKey);
350 #if defined(WITH_ROS2_DEMO)
351 ROS2->RemoveBasicSubscriberCallback(ActorKey);
356 if (CarlaActor && CarlaActor->
IsActive())
Base class for Carla actor factories.
virtual TArray< FActorDefinition > GetDefinitions()
Retrieve the list of actor definitions that this class is able to spawn.
virtual FActorSpawnResult SpawnActor(const FTransform &SpawnAtTransform, const FActorDescription &ActorDescription)
Spawn an actor based on ActorDescription and Transform.
static bool DefinitionReferencesPath(const FActorDefinition &Definition, const FString &MountPoint)
True when the definition's class or any attribute/variation value points at content under MountPoint.
static void TagActor(const AActor &Actor, bool bTagForSemanticSegmentation)
Set the tag of an actor.
static bool FlattenSteeringCurve(AActor *Actor)
Flatten the vehicle's steering curve to 1.0 at all speeds so the Autoware steering command maps 1:1 t...
FCarlaActor::IdType IdType
FCarlaActor * Register(AActor &Actor, FActorDescription Description, IdType DesiredId=0)
Register the Actor in the database.
void Deregister(IdType Id)
FCarlaActor * FindCarlaActor(IdType Id)
void PutActorToSleep(IdType Id, UCarlaEpisode *CarlaEpisode)
void WakeActorUp(IdType Id, UCarlaEpisode *CarlaEpisode)
A view over an actor and its properties.
const FActorInfo * GetActorInfo() const
IdType GetActorId() const
void AddEvent(const CarlaRecorderEventAdd &Event)
static bool CheckActorDefinition(const FActorDefinition &ActorDefinitions)
Return whether the actor definition is valid. Prints all the errors found.
void BindNewDefinitions(ACarlaActorFactory &ActorFactory)
Bind the definitions of ActorFactory that are not bound yet.
void OnActorDestroyed(AActor *Actor)
TFunction< FActorSpawnResult(const FTransform &, const FActorDescription &)> SpawnFunctionType
void Bind(FActorDefinition Definition, SpawnFunctionType SpawnFunction)
Bind a definition to a spawn function.
void PutActorToSleep(FCarlaActor::IdType Id, UCarlaEpisode *CarlaEpisode)
TMap< FString, TSubclassOf< AActor > > Classes
void WakeActorUp(FCarlaActor::IdType Id, UCarlaEpisode *CarlaEpisode)
FCarlaActor * RegisterActor(AActor &Actor, FActorDescription ActorDescription, FActorRegistry::IdType DesiredId=0)
Register an actor that was not created using "SpawnActor" function but that should be kept in the reg...
bool DestroyActor(FCarlaActor::IdType ActorId)
Destroys an actor, properly removing it from the registry.
AActor * ReSpawnActor(const FTransform &Transform, FActorDescription ActorDescription)
ReSpawns an actor based on ActorDescription at Transform.
TPair< EActorSpawnResultStatus, FCarlaActor * > SpawnActor(const FTransform &Transform, FActorDescription ActorDescription, FCarlaActor::IdType DesiredId=0)
Spawns an actor based on ActorDescription at Transform.
TArray< FActorDefinition > Definitions
UPROPERTY so the garbage collector clears the class references when a content pack's classes are dest...
TArray< SpawnFunctionType > SpawnFunctions
void ReleaseContentPack(const FString &MountPoint)
Forget the classes of every definition that refers to content under MountPoint (a content pack being ...
FFrameData & GetFrameData()
static std::shared_ptr< ROS2 > GetInstance()
std::variant< MessageControl > ROS2MessageCallbackData
std::variant< VehicleControl, AckermannControl, VehicleAccelerationControl > ROS2CallbackData
A definition of a Carla Actor with all the variation and attributes.
FString Id
Display ID that identifies the actor.
uint32 UId
Uniquely identifies the definition (no need to fill it).
TSubclassOf< AActor > Class
Class of the actor to be spawned (Optional).
A description of a Carla Actor with all its variation.
TMap< FString, FActorAttribute > Variations
User selected variations of the actor.
TSubclassOf< AActor > Class
Class of the actor to be spawned.
uint32 UId
UId of the definition in which this description was based.
FString Id
Display ID that identifies the actor.
FActorDescription Description
Result of an actor spawn function.
EActorSpawnResultStatus Status