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 #include "Engine/CollisionProfile.h"
16 
17 ASensor::ASensor(const FObjectInitializer &ObjectInitializer)
18  : Super(ObjectInitializer)
19 {
20  auto *Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CamMesh"));
21  Mesh->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
22  Mesh->bHiddenInGame = true;
23  Mesh->CastShadow = false;
24  RootComponent = Mesh;
25 }
26 
28 {
29  Super::BeginPlay();
31  FSensorManager& SensorManager = Episode->GetSensorManager();
32  SensorManager.RegisterSensor(this);
33 }
34 
35 void ASensor::Set(const FActorDescription &Description)
36 {
37  // make a copy
38  SensorDescription = Description;
39 
40  // set the tick interval of the sensor
41  if (Description.Variations.Contains("sensor_tick"))
42  {
43  SetActorTickInterval(
45  0.0f));
46  }
47 }
48 
49 boost::optional<FActorAttribute> ASensor::GetAttribute(const FString Name)
50 {
51  if (SensorDescription.Variations.Contains(Name))
52  {
53  return SensorDescription.Variations[Name];
54  }
55  else
56  return {};
57 }
58 
59 void ASensor::Tick(const float DeltaTime)
60 {
61  TRACE_CPUPROFILER_EVENT_SCOPE(ASensor::Tick);
62  Super::Tick(DeltaTime);
64  {
66  {
68  bClientsListening = false;
69  }
70  }
71  else
72  {
74  {
76  bClientsListening = true;
77  }
78  }
80  {
81  return;
82  }
83  ReadyToTick = true;
84  PrePhysTick(DeltaTime);
85 }
86 
87 void ASensor::SetSeed(const int32 InSeed)
88 {
89  check(RandomEngine != nullptr);
90  Seed = InSeed;
91  RandomEngine->Seed(InSeed);
92 }
93 
95 {
96  Super::PostActorCreated();
97 
98 #if WITH_EDITOR
99  auto *StaticMeshComponent = Cast<UStaticMeshComponent>(RootComponent);
100  if (StaticMeshComponent && !IsRunningCommandlet() && !StaticMeshComponent->GetStaticMesh())
101  {
102  UStaticMesh *CamMesh = LoadObject<UStaticMesh>(
103  NULL,
104  TEXT("/Engine/EditorMeshes/MatineeCam_SM.MatineeCam_SM"),
105  NULL,
106  LOAD_None,
107  NULL);
108  StaticMeshComponent->SetStaticMesh(CamMesh);
109  }
110 #endif // WITH_EDITOR
111 }
112 
113 void ASensor::EndPlay(EEndPlayReason::Type EndPlayReason)
114 {
115  Super::EndPlay(EndPlayReason);
116 
117  // close all sessions associated to the sensor stream
118  auto *GameInstance = UCarlaStatics::GetGameInstance(GetEpisode().GetWorld());
119  auto &StreamingServer = GameInstance->GetServer().GetStreamingServer();
120  auto StreamId = carla::streaming::detail::token_type(Stream.GetToken()).get_stream_id();
121  StreamingServer.CloseStream(StreamId);
122 
124  if(Episode)
125  {
126  FSensorManager& SensorManager = Episode->GetSensorManager();
127  SensorManager.DeRegisterSensor(this);
128  }
129 }
130 
131 void ASensor::PostPhysTickInternal(UWorld *World, ELevelTick TickType, float DeltaSeconds)
132 {
133  TRACE_CPUPROFILER_EVENT_SCOPE(ASensor::PostPhysTickInternal);
134  if(ReadyToTick)
135  {
136  PostPhysTick(World, TickType, DeltaSeconds);
137  ReadyToTick = false;
138  }
139 }
boost::optional< FActorAttribute > GetAttribute(const FString Name)
Definition: Sensor.cpp:49
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
FSensorManager & GetSensorManager()
Definition: CarlaEpisode.h:328
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:87
bool AreClientsListening()
Definition: DataStream.h:64
bool ReadyToTick
Allows the sensor to tick with the tick rate from UE4.
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:38
void EndPlay(EEndPlayReason::Type EndPlayReason) override
Definition: Sensor.cpp:113
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:52
A description of a Carla Actor with all its variation.
virtual void BeginPlay()
Definition: Sensor.cpp:27
void RegisterSensor(ASensor *Sensor)
static UCarlaEpisode * GetCurrentEpisode(const UObject *WorldContextObject)
Definition: CarlaStatics.h:68
void Tick(const float DeltaTime) final
Definition: Sensor.cpp:59
ASensor(const FObjectInitializer &ObjectInitializer)
Definition: Sensor.cpp:17
void PostActorCreated() override
Definition: Sensor.cpp:94
virtual void PrePhysTick(float DeltaSeconds)
void PostPhysTickInternal(UWorld *World, ELevelTick TickType, float DeltaSeconds)
Definition: Sensor.cpp:131
carla::streaming::detail::token_type token_type
virtual void Set(const FActorDescription &Description)
Definition: Sensor.cpp:35
URandomEngine * RandomEngine
Random Engine used to provide noise for sensor output.