CARLA
 
载入中...
搜索中...
未找到
Sensor.cpp
浏览该文件的文档.
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"
10
14
15#include "Engine/CollisionProfile.h"
16
17ASensor::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
35void 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
49boost::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
59void 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
87void 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
113void 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();
121 StreamingServer.CloseStream(StreamId);
122
124 if(Episode)
125 {
126 FSensorManager& SensorManager = Episode->GetSensorManager();
127 SensorManager.DeRegisterSensor(this);
128 }
129}
130
131void 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}
void EndPlay(EEndPlayReason::Type EndPlayReason) override
Definition Sensor.cpp:113
void Tick(const float DeltaTime) final
Definition Sensor.cpp:59
boost::optional< FActorAttribute > GetAttribute(const FString Name)
Definition Sensor.cpp:49
bool ReadyToTick
Allows the sensor to tick with the tick rate from UE4.
int32 Seed
Seed of the pseudo-random engine.
virtual void Set(const FActorDescription &Description)
Definition Sensor.cpp:35
void PostActorCreated() override
Definition Sensor.cpp:94
URandomEngine * RandomEngine
Random Engine used to provide noise for sensor output.
void PostPhysTickInternal(UWorld *World, ELevelTick TickType, float DeltaSeconds)
Definition Sensor.cpp:131
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
virtual void BeginPlay()
Definition Sensor.cpp:27
ASensor(const FObjectInitializer &ObjectInitializer)
Definition Sensor.cpp:17
void SetSeed(int32 InSeed)
Definition Sensor.cpp:87
virtual void PrePhysTick(float DeltaSeconds)
auto GetToken() const
Return the token that allows subscribing to this stream.
Definition DataStream.h:52
bool AreClientsListening()
Definition DataStream.h:64
void DeRegisterSensor(ASensor *Sensor)
void RegisterSensor(ASensor *Sensor)
static float ActorAttributeToFloat(const FActorAttribute &ActorAttribute, float Default)
A simulation episode.
FSensorManager & GetSensorManager()
static UCarlaGameInstance * GetGameInstance(const UObject *WorldContextObject)
static UCarlaEpisode * GetCurrentEpisode(const UObject *WorldContextObject)
void Seed(int32 InSeed)
Seed the random engine.
Serializes a stream endpoint.
A description of a Carla Actor with all its variation.
TMap< FString, FActorAttribute > Variations
User selected variations of the actor.