CARLA
GnssSensor.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 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"
13 
15 #include "carla/geom/Vector3D.h"
16 #include "carla/ros2/ROS2.h"
18 
19 AGnssSensor::AGnssSensor(const FObjectInitializer &ObjectInitializer)
20  : Super(ObjectInitializer)
21 {
22  PrimaryActorTick.bCanEverTick = true;
23  RandomEngine = CreateDefaultSubobject<URandomEngine>(TEXT("RandomEngine"));
24 }
25 
27 {
29 }
30 
31 void AGnssSensor::Set(const FActorDescription &ActorDescription)
32 {
33  Super::Set(ActorDescription);
34  UActorBlueprintFunctionLibrary::SetGnss(ActorDescription, this);
35 }
36 
37 void AGnssSensor::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
38 {
39  TRACE_CPUPROFILER_EVENT_SCOPE(AGnssSensor::PostPhysTick);
40 
41  FVector ActorLocation = GetActorLocation();
42  ALargeMapManager * LargeMap = UCarlaStatics::GetLargeMapManager(GetWorld());
43  if (LargeMap)
44  {
45  ActorLocation = LargeMap->LocalToGlobalLocation(ActorLocation);
46  }
47  carla::geom::Location Location = ActorLocation;
48  carla::geom::GeoLocation CurrentLocation = CurrentGeoReference.Transform(Location);
49 
50  // Compute the noise for the sensor
51  const float LatError = RandomEngine->GetNormalDistribution(0.0f, LatitudeDeviation);
52  const float LonError = RandomEngine->GetNormalDistribution(0.0f, LongitudeDeviation);
53  const float AltError = RandomEngine->GetNormalDistribution(0.0f, AltitudeDeviation);
54 
55  // Apply the noise to the sensor
56  double Latitude = CurrentLocation.latitude + LatitudeBias + LatError;
57  double Longitude = CurrentLocation.longitude + LongitudeBias + LonError;
58  double Altitude = CurrentLocation.altitude + AltitudeBias + AltError;
59 
60  auto Stream = GetDataStream(*this);
61 
62  // ROS2
63  #if defined(WITH_ROS2)
64  auto ROS2 = carla::ros2::ROS2::GetInstance();
65  if (ROS2->IsEnabled())
66  {
67  TRACE_CPUPROFILER_EVENT_SCOPE_STR("ROS2 Send");
69  AActor* ParentActor = GetAttachParentActor();
70  if (ParentActor)
71  {
72  FTransform LocalTransformRelativeToParent = GetActorTransform().GetRelativeTransform(ParentActor->GetActorTransform());
73  ROS2->ProcessDataFromGNSS(Stream.GetSensorType(), StreamId, LocalTransformRelativeToParent, carla::geom::GeoLocation{Latitude, Longitude, Altitude}, this);
74  }
75  else
76  {
77  ROS2->ProcessDataFromGNSS(Stream.GetSensorType(), StreamId, Stream.GetSensorTransform(), carla::geom::GeoLocation{Latitude, Longitude, Altitude}, this);
78  }
79  }
80  #endif
81  {
82  TRACE_CPUPROFILER_EVENT_SCOPE_STR("AGnssSensor Stream Send");
83  Stream.SerializeAndSend(*this, carla::geom::GeoLocation{Latitude, Longitude, Altitude});
84  }
85 }
86 
88 {
89  LatitudeDeviation = Value;
90 }
91 
93 {
94  LongitudeDeviation = Value;
95 }
96 
98 {
99  AltitudeDeviation = Value;
100 }
101 
103 {
104  LatitudeBias = Value;
105 }
106 
108 {
109  LongitudeBias = Value;
110 }
111 
113 {
114  AltitudeBias = Value;
115 }
116 
118 {
119  return LatitudeDeviation;
120 }
122 {
123  return LongitudeDeviation;
124 }
126 {
127  return AltitudeDeviation;
128 }
129 
131 {
132  return LatitudeBias;
133 }
135 {
136  return LongitudeBias;
137 }
139 {
140  return AltitudeBias;
141 }
142 
144 {
145  Super::BeginPlay();
146 
147  const UCarlaEpisode* episode = UCarlaStatics::GetCurrentEpisode(GetWorld());
149 }
virtual void BeginPlay() override
Definition: GnssSensor.cpp:143
auto GetToken() const
Return the token that allows subscribing to this sensor&#39;s stream.
static FActorDefinition GetSensorDefinition()
Definition: GnssSensor.cpp:26
void SetLatitudeBias(float Value)
Definition: GnssSensor.cpp:102
float GetLongitudeBias() const
Definition: GnssSensor.cpp:134
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds) override
Definition: GnssSensor.cpp:37
float GetLatitudeBias() const
Definition: GnssSensor.cpp:130
A definition of a Carla Actor with all the variation and attributes.
carla::geom::GeoLocation CurrentGeoReference
Definition: GnssSensor.h:60
void SetAltitudeDeviation(float Value)
Definition: GnssSensor.cpp:97
float GetAltitudeDeviation() const
Definition: GnssSensor.cpp:125
GeoLocation Transform(const Location &location) const
Transform the given location to a GeoLocation using this as geo-reference.
Definition: GeoLocation.cpp:66
geom::Location Location
Definition: rpc/Location.h:14
float LatitudeDeviation
Definition: GnssSensor.h:62
void SetLongitudeBias(float Value)
Definition: GnssSensor.cpp:107
void SetAltitudeBias(float Value)
Definition: GnssSensor.cpp:112
float LongitudeBias
Definition: GnssSensor.h:67
A simulation episode.
Definition: CarlaEpisode.h:38
float GetLongitudeDeviation() const
Definition: GnssSensor.cpp:121
static void SetGnss(const FActorDescription &Description, AGnssSensor *Gnss)
float AltitudeDeviation
Definition: GnssSensor.h:64
A description of a Carla Actor with all its variation.
float GetAltitudeBias() const
Definition: GnssSensor.cpp:138
const auto & get_stream_id() const
Definition: detail/Token.h:116
void SetLongitudeDeviation(float Value)
Definition: GnssSensor.cpp:92
FAsyncDataStream GetDataStream(const SensorT &Self)
Return the FDataStream associated with this sensor.
AGnssSensor(const FObjectInitializer &ObjectInitializer)
Definition: GnssSensor.cpp:19
float AltitudeBias
Definition: GnssSensor.h:68
static UCarlaEpisode * GetCurrentEpisode(const UObject *WorldContextObject)
Definition: CarlaStatics.h:68
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
Definition: CarlaStatics.h:100
void SetLatitudeDeviation(float Value)
Definition: GnssSensor.cpp:87
const carla::geom::GeoLocation & GetGeoReference() const
Return the GeoLocation point of the map loaded.
Definition: CarlaEpisode.h:134
float LongitudeDeviation
Definition: GnssSensor.h:63
void Set(const FActorDescription &ActorDescription)
Definition: GnssSensor.cpp:31
static std::shared_ptr< ROS2 > GetInstance()
Definition: ROS2.h:51
float GetNormalDistribution(float Mean, float StandardDeviation)
Definition: RandomEngine.h:110
uint64_t GetSensorType()
Definition: DataStream.h:58
float LatitudeBias
Definition: GnssSensor.h:66
carla::streaming::detail::token_type token_type
FVector LocalToGlobalLocation(const FVector &InLocation) const
float GetLatitudeDeviation() const
Definition: GnssSensor.cpp:117
URandomEngine * RandomEngine
Random Engine used to provide noise for sensor output.