CARLA
Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/Sensor.h
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 #pragma once
8 
12 
13 #include "Carla/Game/CarlaEngine.h"
14 
15 #include "GameFramework/Actor.h"
16 
17 #include "Sensor.generated.h"
18 
19 struct FActorDescription;
20 
21 /// Base class for sensors.
22 UCLASS(Abstract, hidecategories = (Collision, Attachment, Actor))
23 class CARLA_API ASensor : public AActor
24 {
25  GENERATED_BODY()
26 
27 public:
28 
29  ASensor(const FObjectInitializer &ObjectInitializer);
30 
31  void SetEpisode(const UCarlaEpisode &InEpisode)
32  {
33  Episode = &InEpisode;
34  }
35 
36  virtual void Set(const FActorDescription &Description);
37 
38  boost::optional<FActorAttribute> GetAttribute(const FString Name);
39 
40  virtual void BeginPlay();
41 
42  /// Replace the FDataStream associated with this sensor.
43  ///
44  /// @warning Do not change the stream after BeginPlay. It is not thread-safe.
45  void SetDataStream(FDataStream InStream)
46  {
47  Stream = std::move(InStream);
48  }
49 
51  {
52  return std::move(Stream);
53  }
54 
55  /// Return the token that allows subscribing to this sensor's stream.
56  auto GetToken() const
57  {
58  return Stream.GetToken();
59  }
60 
62  {
63  return Stream.IsStreamReady();
64  }
65 
66  void Tick(const float DeltaTime) final;
67 
68  virtual void PrePhysTick(float DeltaSeconds) {}
69  virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds) {}
70  // Small interface to notify sensors when clients are listening
71  virtual void OnFirstClientConnected() {};
72  // Small interface to notify sensors when no clients are listening
73  virtual void OnLastClientDisconnected() {};
74 
75 
76  void PostPhysTickInternal(UWorld *World, ELevelTick TickType, float DeltaSeconds);
77 
78  UFUNCTION(BlueprintCallable)
79  URandomEngine *GetRandomEngine()
80  {
81  return RandomEngine;
82  }
83 
84  UFUNCTION(BlueprintCallable)
85  int32 GetSeed() const
86  {
87  return Seed;
88  }
89 
90  UFUNCTION(BlueprintCallable)
91  void SetSeed(int32 InSeed);
92 
93  const UCarlaEpisode &GetEpisode() const
94  {
95  check(Episode != nullptr);
96  return *Episode;
97  }
98 
99 protected:
100 
101  void PostActorCreated() override;
102 
103  void EndPlay(EEndPlayReason::Type EndPlayReason) override;
104 
105  /// Return the FDataStream associated with this sensor.
106  ///
107  /// You need to provide a reference to self, this is necessary for template
108  /// deduction.
109  template <typename SensorT>
110  FAsyncDataStream GetDataStream(const SensorT &Self)
111  {
112  return Stream.MakeAsyncDataStream(Self, GetEpisode().GetElapsedGameTime());
113  }
114 
115  /// Seed of the pseudo-random engine.
116  UPROPERTY(Category = "Random Engine", EditAnywhere)
117  int32 Seed = 123456789;
118 
119  /// Random Engine used to provide noise for sensor output.
120  UPROPERTY()
121  URandomEngine *RandomEngine = nullptr;
122 
123  UPROPERTY()
124  bool bIsActive = false;
125 
126 private:
127 
129 
130  FDelegateHandle OnPostTickDelegate;
131 
132  FActorDescription SensorDescription;
133 
134  const UCarlaEpisode *Episode = nullptr;
135 
136  /// Allows the sensor to tick with the tick rate from UE4.
137  bool ReadyToTick = false;
138 
139  bool bClientsListening = false;
140 
141 };
auto GetToken() const
Return the token that allows subscribing to this sensor&#39;s stream.
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
carla::SharedPtr< cc::Actor > Actor
A simulation episode.
Definition: CarlaEpisode.h:38
detail::Stream< detail::MultiStreamState > Stream
A stream represents an unidirectional channel for sending data from server to client.
Definition: Stream.h:19
A description of a Carla Actor with all its variation.
FAsyncDataStream GetDataStream(const SensorT &Self)
Return the FDataStream associated with this sensor.
A streaming channel for sending sensor data to clients, supports sending data asynchronously.
void SetDataStream(FDataStream InStream)
Replace the FDataStream associated with this sensor.
virtual void PrePhysTick(float DeltaSeconds)
void SetEpisode(const UCarlaEpisode &InEpisode)