CARLA
Loading...
Searching...
No Matches
GnssSensor.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 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
8#include "Carla.h"
13
15#include "carla/geom/Vector3D.h"
16#include "carla/ros2/ROS2.h"
18
19AGnssSensor::AGnssSensor(const FObjectInitializer &ObjectInitializer)
20 : Super(ObjectInitializer)
21{
22 PrimaryActorTick.bCanEverTick = true;
23 RandomEngine = CreateDefaultSubobject<URandomEngine>(TEXT("RandomEngine"));
24}
25
30
31void AGnssSensor::Set(const FActorDescription &ActorDescription)
32{
33 Super::Set(ActorDescription);
34 UActorBlueprintFunctionLibrary::SetGnss(ActorDescription, this);
35}
36
37void AGnssSensor::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
38{
39 TRACE_CPUPROFILER_EVENT_SCOPE(AGnssSensor::PostPhysTick);
40
41 FVector ActorLocation = GetActorLocation();
43 if (LargeMap)
44 {
45 ActorLocation = LargeMap->LocalToGlobalLocation(ActorLocation);
46 }
47 carla::geom::Location Location = ActorLocation;
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 LatitudeValue = CurrentLocation.latitude + LatitudeBias + LatError;
57 LongitudeValue = CurrentLocation.longitude + LongitudeBias + LonError;
58 AltitudeValue = CurrentLocation.altitude + AltitudeBias + AltError;
59
60 auto DataStream = GetDataStream(*this);
61
62 // ROS2
63 #if defined(WITH_ROS2)
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(DataStream.GetSensorType(), StreamId, LocalTransformRelativeToParent, carla::geom::GeoLocation{LatitudeValue, LongitudeValue, AltitudeValue}, this);
74 }
75 else
76 {
77 ROS2->ProcessDataFromGNSS(DataStream.GetSensorType(), StreamId, DataStream.GetSensorTransform(), carla::geom::GeoLocation{LatitudeValue, LongitudeValue, AltitudeValue}, this);
78 }
79 }
80 #endif
81 {
82 TRACE_CPUPROFILER_EVENT_SCOPE_STR("AGnssSensor Stream Send");
83 DataStream.SerializeAndSend(*this, carla::geom::GeoLocation{LatitudeValue, LongitudeValue, AltitudeValue});
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 return LatitudeValue;
146}
147
149{
150 return LongitudeValue;
151}
152
154{
155 return AltitudeValue;
156}
157
159{
160 Super::BeginPlay();
161
162 const UCarlaEpisode* episode = UCarlaStatics::GetCurrentEpisode(GetWorld());
164}
165
167{
168 if (RandomEngine != nullptr)
169 {
170 State.random_engine = RandomEngine->GetState();
171 }
172}
173
175{
176 if (State.random_engine && (RandomEngine != nullptr))
177 {
178 RandomEngine->SetState(*State.random_engine);
179 }
180}
float GetLongitudeDeviation() const
float GetAltitudeBias() const
float LongitudeBias
Definition GnssSensor.h:75
void SetLatitudeDeviation(float Value)
float GetAltitudeDeviation() const
void SetLongitudeBias(float Value)
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds) override
double LatitudeValue
Definition GnssSensor.h:78
float LatitudeBias
Definition GnssSensor.h:74
float LatitudeDeviation
Definition GnssSensor.h:70
double AltitudeValue
Definition GnssSensor.h:80
void GetMirrorState(carla::multigpu::SensorState &State) const override
Multi-GPU: the runtime state this sensor starts its next tick from (noise generator,...
void SetLongitudeDeviation(float Value)
void SetAltitudeDeviation(float Value)
double GetAltitudeValue() const
float AltitudeBias
Definition GnssSensor.h:76
float GetLatitudeDeviation() const
float GetLongitudeBias() const
void Set(const FActorDescription &ActorDescription)
carla::geom::GeoProjection CurrentGeoProjection
Definition GnssSensor.h:68
float GetLatitudeBias() const
void SetAltitudeBias(float Value)
float AltitudeDeviation
Definition GnssSensor.h:72
void SetLatitudeBias(float Value)
double GetLongitudeValue() const
double LongitudeValue
Definition GnssSensor.h:79
virtual void BeginPlay() override
float LongitudeDeviation
Definition GnssSensor.h:71
static FActorDefinition GetSensorDefinition()
void SetMirrorState(const carla::multigpu::SensorState &State) override
AGnssSensor(const FObjectInitializer &ObjectInitializer)
double GetLatitudeValue() const
FVector LocalToGlobalLocation(const FVector &InLocation) const
auto GetToken() const
Return the token that allows subscribing to this sensor's stream.
FAsyncDataStream GetDataStream(SensorType &&Self)
Return the FDataStream associated with this sensor.
URandomEngine * RandomEngine
Random Engine used to provide noise for sensor output.
static void SetGnss(const FActorDescription &Description, AGnssSensor *Gnss)
A simulation episode.
const carla::geom::GeoProjection & GetGeoProjection() const
Return the geo projection of the map loaded.
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
static UCarlaEpisode * GetCurrentEpisode(const UObject *WorldContextObject)
uint32 GetState() const
The engine's state, to continue its exact sequence elsewhere (a Multi-GPU secondary's mirror of a sen...
float GetNormalDistribution(float Mean, float StandardDeviation)
void SetState(uint32 State)
Continue from State, as GetState returned it; an impossible value is ignored.
static std::shared_ptr< ROS2 > GetInstance()
Definition ROS2.h:61
Serializes a stream endpoint.
A definition of a Carla Actor with all the variation and attributes.
A description of a Carla Actor with all its variation.
GeoLocation TransformToGeoLocation(const Location &location) const
Transform the given geo location to a location.
One record of frame data packet 27: the runtime state a sensor of the primary starts this frame's tic...
Definition sensorSync.h:87