CARLA
Sensor.cpp
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 #include "Carla.h"
8 #include "Carla/Sensor/Sensor.h"
10 
14 
15 ASensor::ASensor(const FObjectInitializer &ObjectInitializer)
16  : Super(ObjectInitializer)
17 {
18  auto *Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CamMesh"));
19  Mesh->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
20  Mesh->bHiddenInGame = true;
21  Mesh->CastShadow = false;
22  RootComponent = Mesh;
23 }
24 
26 {
27  Super::BeginPlay();
29  FSensorManager& SensorManager = Episode->GetSensorManager();
30  SensorManager.RegisterSensor(this);
31 }
32 
33 void ASensor::Set(const FActorDescription &Description)
34 {
35  // set the tick interval of the sensor
36  if (Description.Variations.Contains("sensor_tick"))
37  {
38  SetActorTickInterval(
40  0.0f));
41  }
42 }
43 
44 void ASensor::Tick(const float DeltaTime)
45 {
46  TRACE_CPUPROFILER_EVENT_SCOPE(ASensor::Tick);
47  Super::Tick(DeltaTime);
49  {
51  {
53  bClientsListening = false;
54  }
55  }
56  else
57  {
59  {
61  bClientsListening = true;
62  }
63  }
65  {
66  return;
67  }
68  ReadyToTick = true;
69  PrePhysTick(DeltaTime);
70 }
71 
72 void ASensor::SetSeed(const int32 InSeed)
73 {
74  check(RandomEngine != nullptr);
75  Seed = InSeed;
76  RandomEngine->Seed(InSeed);
77 }
78 
80 {
81  Super::PostActorCreated();
82 
83 #if WITH_EDITOR
84  auto *StaticMeshComponent = Cast<UStaticMeshComponent>(RootComponent);
85  if (StaticMeshComponent && !IsRunningCommandlet() && !StaticMeshComponent->GetStaticMesh())
86  {
87  UStaticMesh *CamMesh = LoadObject<UStaticMesh>(
88  NULL,
89  TEXT("/Engine/EditorMeshes/MatineeCam_SM.MatineeCam_SM"),
90  NULL,
91  LOAD_None,
92  NULL);
93  StaticMeshComponent->SetStaticMesh(CamMesh);
94  }
95 #endif // WITH_EDITOR
96 }
97 
98 void ASensor::EndPlay(EEndPlayReason::Type EndPlayReason)
99 {
100  Super::EndPlay(EndPlayReason);
101 
102  // close all sessions associated to the sensor stream
103  auto *GameInstance = UCarlaStatics::GetGameInstance(GetEpisode().GetWorld());
104  auto &StreamingServer = GameInstance->GetServer().GetStreamingServer();
105  auto StreamId = carla::streaming::detail::token_type(Stream.GetToken()).get_stream_id();
106  StreamingServer.CloseStream(StreamId);
107 
108  Stream = FDataStream();
109 
111  if(Episode)
112  {
113  FSensorManager& SensorManager = Episode->GetSensorManager();
114  SensorManager.DeRegisterSensor(this);
115  }
116 }
117 
118 void ASensor::PostPhysTickInternal(UWorld *World, ELevelTick TickType, float DeltaSeconds)
119 {
120  TRACE_CPUPROFILER_EVENT_SCOPE(ASensor::PostPhysTickInternal);
121  if(ReadyToTick)
122  {
123  PostPhysTick(World, TickType, DeltaSeconds);
124  ReadyToTick = false;
125  }
126 }
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
FSensorManager & GetSensorManager()
Definition: CarlaEpisode.h:301
void DeRegisterSensor(ASensor *Sensor)
int32 Seed
Seed of the pseudo-random engine.
static float ActorAttributeToFloat(const FActorAttribute &ActorAttribute, float Default)
void SetSeed(int32 InSeed)
Definition: Sensor.cpp:72
bool AreClientsListening()
Definition: DataStream.h:53
bool ReadyToTick
Allows the sensor to tick with the tick rate from UE4.
FDataStreamTmpl< carla::streaming::Stream > FDataStream
Definition: DataStream.h:68
TMap< FString, FActorAttribute > Variations
User selected variations of the actor.
static UCarlaGameInstance * GetGameInstance(const UObject *WorldContextObject)
Definition: CarlaStatics.h:63
A simulation episode.
Definition: CarlaEpisode.h:36
void EndPlay(EEndPlayReason::Type EndPlayReason) override
Definition: Sensor.cpp:98
void Seed(int32 InSeed)
Seed the random engine.
Definition: RandomEngine.h:44
auto GetToken() const
Return the token that allows subscribing to this stream.
Definition: DataStream.h:47
A description of a Carla Actor with all its variation.
virtual void BeginPlay()
Definition: Sensor.cpp:25
void RegisterSensor(ASensor *Sensor)
static UCarlaEpisode * GetCurrentEpisode(const UObject *WorldContextObject)
Definition: CarlaStatics.h:68
void Tick(const float DeltaTime) final
Definition: Sensor.cpp:44
ASensor(const FObjectInitializer &ObjectInitializer)
Definition: Sensor.cpp:15
void PostActorCreated() override
Definition: Sensor.cpp:79
virtual void PrePhysTick(float DeltaSeconds)
void PostPhysTickInternal(UWorld *World, ELevelTick TickType, float DeltaSeconds)
Definition: Sensor.cpp:118
carla::streaming::detail::token_type token_type
virtual void Set(const FActorDescription &Description)
Definition: Sensor.cpp:33
URandomEngine * RandomEngine
Random Engine used to provide noise for sensor output.