CARLA
Loading...
Searching...
No Matches
SensorSpawnerActor.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma de Barcelona (UAB). This work is licensed under the terms of the MIT license. For a copy, see <https://opensource.org/licenses/MIT>.
2
3#pragma once
4
5#include <util/ue-header-guard-begin.h>
6#include "CoreMinimal.h"
7#include "GameFramework/Actor.h"
8#include <util/ue-header-guard-end.h>
9
10#include "SensorSpawnerActor.generated.h"
11
12class ASensor;
13class USceneComponent;
14class UCarlaEpisode;
15struct FActorDefinition;
17
18USTRUCT(BlueprintType)
20{
21 GENERATED_BODY()
22
23 // sensor class to spawn.
24 UPROPERTY(EditAnywhere, BlueprintReadOnly)
25 TSubclassOf<ASensor> SensorClass;
26
27 // Number of sensors to spawn of the SensorClass.
28 UPROPERTY(EditAnywhere, BlueprintReadOnly)
29 int Amount = 1;
30
31 // If true, sensor will be attached to the AttachActorClass if the Actor is found.
32 UPROPERTY(EditAnywhere, BlueprintReadOnly)
33 bool bAttachToActor = false;
34};
35
36
37UCLASS(Blueprintable)
38class CARLA_API ASensorSpawnerActor : public AActor
39{
40 GENERATED_BODY()
41
42public:
44
45 // Called OnBeginPlay().
46 UFUNCTION(BlueprintCallable)
47 void SpawnSensors();
48
49 UFUNCTION(BlueprintCallable, CallInEditor, Category="Config")
50 void StartRecordingSensorData();
51
52 UFUNCTION(BlueprintCallable, CallInEditor, Category="Config")
53 void StopRecordingSensorData();
54
55 UFUNCTION(BlueprintCallable, CallInEditor, Category="Config")
56 void ToggleRadarVisibility();
57
58protected:
59 // Called when the game starts or when spawned.
60 virtual void BeginPlay() override;
61
62 virtual void Tick(float DeltaSeconds) override;
63
64 // Root
65 UPROPERTY(BlueprintReadOnly, Category="Components")
66 TObjectPtr<USceneComponent> SceneComp;
67
68 // Array with sensors to spawn
69 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Config")
70 TArray<FSensorTuple> SensorsToSpawn;
71
72 // Class that the sensor will be attached if Actor is found. bAttachToActor bool has to be set to true.
73 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Config|Spawn")
74 TSubclassOf<AActor> AttachActorClass;
75
76 // Initial delay until the sensors start spawning.
77 UPROPERTY(EditAnywhere, BlueprintReadOnly, meta=(ClampMin=0.f, ClampMax=40.f), Category="Config|Spawn")
78 float InitialDelay = 4.f;
79
80 // Delay between spawns. Set to 0 for no delay.
81 UPROPERTY(EditAnywhere, BlueprintReadOnly, meta=(ClampMin=0.f, ClampMax=40.f), Category="Config|Spawn")
82 float DelayBetweenSpawns = 0.f;
83
84 // Max spawn location of the sensor. Spawn at random location between MaxSpawnLocation and MinSpawnLocation.
85 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Config|Spawn")
86 FVector MaxSpawnLocation {-110000.f, -100000.f, 9600.f};
87
88 // Min spawn location of the sensor. Spawn at random location between MaxSpawnLocation and MinSpawnLocation.
89 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Config|Spawn")
90 FVector MinSpawnLocation {-120000.f, -110000.f, 9300.f};
91
92 // Enables save data at begin play to /Saved/SensorSpawnerCaptures folder.
93 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Config|SaveData")
94 bool bSaveDataAtBeginPlay = false;
95
96 // Subclass of ASceneCaptureSensor that we are going to save the images to disk. Set to nullptr to capture all ASceneCaptureSensor subclasses.
97 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Config|SaveData")
98 TSubclassOf<class ASensor> SensorClassToCapture;
99
100 // Path where the sensor data will be saved. By default is the project/Saved/ folder.
101 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Config|SaveData")
102 FString SaveImagePath;
103
104 // cm/s
105 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Config|Radar")
106 float VelocityRange = 0.075;
107
108 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Config|Radar")
109 float PointSize = 15.0f;
110
111
112
113private:
114 bool bRecordingData = false;
115
116 bool bRadarVisibility = false;
117
118 UFUNCTION()
119 void OnEpisodeInitialised(UCarlaEpisode* InitialisedEpisode);
120
121 UFUNCTION()
122 void SpawnSensorsDelayed();
123
124 void GenerateSensorActorDescription(const FActorDefinition* Definition, FActorDescription& SensorDescription) const;
125
126 // Gets a transform with a random location between MaxSpawnLocation and MinSpawnLocation.
127 void GetRandomTransform(FTransform &Transform) const;
128
129 void SpawnSensorActor(const FActorDescription& SensorDescription, bool bAttachToActor);
130
131 const FActorDefinition* GetActorDefinitionByClass(const TSubclassOf<AActor> ActorClass) const;
132
133 void AddSensorToSaveDataArray(AActor* Actor);
134
135 void SaveSensorData(float DeltaSeconds);
136
137 void AttachSensorToActor(AActor* SensorActor);
138
139 void DrawRadarSensorPoints() const;
140
141 UPROPERTY()
142 TObjectPtr<UCarlaEpisode> CarlaEpisode;
143
144 FTimerHandle InitialDelaySpawnTimerHandle;
145
146 // Used for delayed spawn.
147 FTimerHandle SpawnSensorsDelayedTimerHandle;
148
149 // Used for delayed spawn. Track cameras taking pictures on tick.
150 TArray<FSensorTuple> SensorsToSpawnCopy;
151
152 // Track cameras saving pictures on tick.
153 UPROPERTY()
154 TArray<TObjectPtr<ASensor>> SpawnedSensorsArray;
155
156};
A simulation episode.
A definition of a Carla Actor with all the variation and attributes.
A description of a Carla Actor with all its variation.