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