CARLA
Loading...
Searching...
No Matches
AutowareGnssSensor.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
9#include "Carla.h"
15
17#include "carla/geom/Vector3D.h"
18#include "carla/ros2/ROS2.h"
20
21AAutowareGnssSensor::AAutowareGnssSensor(const FObjectInitializer &ObjectInitializer)
22 : Super(ObjectInitializer)
23{
24 PrimaryActorTick.bCanEverTick = true;
25 RandomEngine = CreateDefaultSubobject<URandomEngine>(TEXT("RandomEngine"));
26}
27
32
34{
35 Super::BeginPlay();
36
37 // Load MGRS data from the active level's world settings.
39
40 const UCarlaEpisode* episode = UCarlaStatics::GetCurrentEpisode(GetWorld());
42}
43
44void AAutowareGnssSensor::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
45{
46 TRACE_CPUPROFILER_EVENT_SCOPE(AAutowareGnssSensor::PostPhysTick);
47
48 FVector ActorLocation = GetActorLocation();
50 if (LargeMap)
51 {
52 ActorLocation = LargeMap->LocalToGlobalLocation(ActorLocation);
53 }
54 carla::geom::Location Location = ActorLocation;
56
57 LatitudeValue = CurrentLocation.latitude;
58 LongitudeValue = CurrentLocation.longitude;
59 AltitudeValue = CurrentLocation.altitude;
60
62 {
63 // Compute the noise for the sensor
64 const float LatError = RandomEngine->GetNormalDistribution(0.0f, LatitudeDeviation);
65 const float LonError = RandomEngine->GetNormalDistribution(0.0f, LongitudeDeviation);
66 const float AltError = RandomEngine->GetNormalDistribution(0.0f, AltitudeDeviation);
67
68 // Apply the noise to the sensor
69 LatitudeValue += LatitudeBias + LatError;
70 LongitudeValue += LongitudeBias + LonError;
71 AltitudeValue += AltitudeBias + AltError;
72 }
73
74 auto DataStream = GetDataStream(*this);
75
76 // ROS2
77 #if defined(WITH_ROS2)
79 if (ROS2->IsEnabled())
80 {
81 TRACE_CPUPROFILER_EVENT_SCOPE_STR("ROS2 Send");
83 AActor* ParentActor = GetAttachParentActor();
84 const FTransform SensorWorldTransform = GetActorTransform();
85 double MgrsOffsetPosition[3] = {0.0, 0.0, 0.0};
86 if (MgrsDataAsset)
87 {
88 MgrsOffsetPosition[0] = static_cast<double>(MgrsDataAsset->MgrsOffsetPosition.X);
89 MgrsOffsetPosition[1] = static_cast<double>(MgrsDataAsset->MgrsOffsetPosition.Y);
90 MgrsOffsetPosition[2] = static_cast<double>(MgrsDataAsset->MgrsOffsetPosition.Z);
91 }
92
93 if (ParentActor)
94 {
95 FTransform LocalTransformRelativeToParent = GetActorTransform().GetRelativeTransform(ParentActor->GetActorTransform());
96 ROS2->ProcessDataFromAutowareGNSS(DataStream.GetSensorType(), StreamId, LocalTransformRelativeToParent, carla::geom::GeoLocation{LatitudeValue, LongitudeValue, AltitudeValue}, SensorWorldTransform, MgrsOffsetPosition, this);
97 }
98 else
99 {
100 ROS2->ProcessDataFromAutowareGNSS(DataStream.GetSensorType(), StreamId, DataStream.GetSensorTransform(), carla::geom::GeoLocation{LatitudeValue, LongitudeValue, AltitudeValue}, SensorWorldTransform, MgrsOffsetPosition, this);
101 }
102 }
103 #endif
104
105 {
106 TRACE_CPUPROFILER_EVENT_SCOPE_STR("AAutowareGnssSensor Stream Send");
107 DataStream.SerializeAndSend(*this, carla::geom::GeoLocation{LatitudeValue, LongitudeValue, AltitudeValue});
108 }
109}
110
112{
113 if (MgrsDataAsset)
114 {
115 return; // Already loaded
116 }
117
118 if (const auto* WS = Cast<AAutowareWorldSettings>(GetWorld()->GetWorldSettings()))
119 {
120 if (WS->MgrsDataAssetSoftPtr.IsNull())
121 {
122 UE_LOG(LogCarla, Warning, TEXT("MGRS Data Asset SoftPtr not set in WorldSettings."));
123 return;
124 }
125
126 UMgrsDataAsset* Data = WS->MgrsDataAssetSoftPtr.Get();
127 if (!IsValid(Data))
128 {
129 Data = WS->MgrsDataAssetSoftPtr.LoadSynchronous();
130 }
131
132 MgrsDataAsset = Data;
133 }
134}
135
136void AAutowareGnssSensor::Set(const FActorDescription &ActorDescription)
137{
138 Super::Set(ActorDescription);
140}
141
143{
144 LatitudeDeviation = Value;
145}
146
148{
149 LongitudeDeviation = Value;
150}
151
153{
154 AltitudeDeviation = Value;
155}
156
158{
159 LatitudeBias = Value;
160}
161
163{
164 LongitudeBias = Value;
165}
166
168{
169 AltitudeBias = Value;
170}
171
184
186{
187 return LatitudeBias;
188}
190{
191 return LongitudeBias;
192}
194{
195 return AltitudeBias;
196}
197
199{
200 return LatitudeValue;
201}
202
204{
205 return LongitudeValue;
206}
207
209{
210 return AltitudeValue;
211}
212
217
219{
220 bApplyNoiseError = bEnabled;
221}
222
224{
225 if (RandomEngine != nullptr)
226 {
227 State.random_engine = RandomEngine->GetState();
228 }
229}
230
232{
233 if (State.random_engine && (RandomEngine != nullptr))
234 {
235 RandomEngine->SetState(*State.random_engine);
236 }
237}
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
TObjectPtr< UMgrsDataAsset > MgrsDataAsset
void GetMirrorState(carla::multigpu::SensorState &State) const override
Multi-GPU: the runtime state this sensor starts its next tick from (noise generator,...
void SetAltitudeDeviation(float Value)
carla::geom::GeoProjection CurrentGeoProjection
double GetLatitudeValue() const
static FActorDefinition GetSensorDefinition()
float GetLongitudeDeviation() const
virtual void BeginPlay() override
double GetAltitudeValue() const
void SetLatitudeDeviation(float Value)
double GetLongitudeValue() const
void SetLongitudeBias(float Value)
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds) override
void SetMirrorState(const carla::multigpu::SensorState &State) override
void SetLatitudeBias(float Value)
void SetNoiseErrorActive(const bool bEnabled)
float GetAltitudeDeviation() const
void Set(const FActorDescription &ActorDescription)
float GetLatitudeDeviation() const
AAutowareGnssSensor(const FObjectInitializer &ObjectInitializer)
void SetLongitudeDeviation(float Value)
void SetAltitudeBias(float Value)
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 SetAutowareGnss(const FActorDescription &Description, AAutowareGnssSensor *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)
Data Asset containing MGRS-related configuration for Autoware maps.
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