18#include <util/ue-header-guard-begin.h>
19#include "Kismet/KismetMathLibrary.h"
20#include "Async/ParallelFor.h"
21#include "PhysicsEngine/PhysicsObjectExternalInterface.h"
22#include <util/ue-header-guard-end.h>
30 : Super(ObjectInitializer)
32 PrimaryActorTick.bCanEverTick =
true;
34 RandomEngine = CreateDefaultSubobject<URandomEngine>(TEXT(
"RandomEngine"));
36 TraceParams = FCollisionQueryParams(FName(TEXT(
"Laser_Trace")),
true,
this);
44 Super::Set(ActorDescription);
91 #if defined(WITH_ROS2)
93 if (ROS2->IsEnabled())
95 TRACE_CPUPROFILER_EVENT_SCOPE_STR(
"ARadar::PostPhysTick ROS2 Send");
97 AActor* ParentActor = GetAttachParentActor();
100 FTransform LocalTransformRelativeToParent = GetActorTransform().GetRelativeTransform(ParentActor->GetActorTransform());
101 ROS2->ProcessDataFromRadar(DataStream.GetSensorType(), StreamId, LocalTransformRelativeToParent,
RadarData,
this);
105 ROS2->ProcessDataFromRadar(DataStream.GetSensorType(), StreamId, DataStream.GetSensorTransform(),
RadarData,
this);
111 TRACE_CPUPROFILER_EVENT_SCOPE_STR(
"Send Stream");
112 DataStream.SerializeAndSend(*
this,
RadarData, DataStream.PopBufferFromPool());
118 const FVector RadarLocation = GetActorLocation();
126 constexpr float TO_METERS = 1e-2;
127 const FTransform& ActorTransform = GetActorTransform();
128 const FRotator& TransformRotator = ActorTransform.Rotator();
129 const FVector& RadarLocation = GetActorLocation();
130 const FVector& ForwardVector = GetActorForwardVector();
131 const FVector TransformXAxis = ActorTransform.GetUnitAxis(EAxis::X);
132 const FVector TransformYAxis = ActorTransform.GetUnitAxis(EAxis::Y);
133 const FVector TransformZAxis = ActorTransform.GetUnitAxis(EAxis::Z);
136 const float MaxRx = FMath::Tan(FMath::DegreesToRadians(
HorizontalFOV * 0.5f)) *
Range;
137 const float MaxRy = FMath::Tan(FMath::DegreesToRadians(
VerticalFOV * 0.5f)) *
Range;
142 Rays.resize(NumPoints);
143 for (
int i = 0; i <
Rays.size(); i++) {
146 Rays[i].Hitted =
false;
149 auto LockedPhysObject = FPhysicsObjectExternalInterface::LockRead(GetWorld()->GetPhysicsScene());
151 TRACE_CPUPROFILER_EVENT_SCOPE(ParallelFor);
152 ParallelFor(NumPoints, [&](int32 idx) {
153 TRACE_CPUPROFILER_EVENT_SCOPE(ParallelForTask);
154 FHitResult OutHit(ForceInit);
155 const float Radius =
Rays[idx].Radius;
156 const float Angle =
Rays[idx].Angle;
159 FMath::SinCos(&Sin, &Cos, Angle);
163 const FVector Direction = FVector(
165 MaxRx * Radius * Cos,
168 const FVector EndLocation = RadarLocation + TransformRotator.RotateVector(Direction *
Range);
170 const bool Hitted = GetWorld()->ParallelLineTraceSingleByChannel(
174 ECC_GameTraceChannel2,
176 FCollisionResponseParams::DefaultResponseParam
179 const TWeakObjectPtr<AActor> HittedActor = OutHit.GetActor();
180 if (Hitted && HittedActor.Get()) {
181 Rays[idx].Hitted =
true;
185 Rays[idx].AzimuthAndElevation = FMath::GetAzimuthAndElevation (
186 (EndLocation - RadarLocation).GetSafeNormal() *
Range,
192 Rays[idx].Distance = OutHit.Distance * TO_METERS;
196 LockedPhysObject.Release();
199 for (
auto& ray :
Rays)
205 ray.RelativeVelocity,
206 UKismetMathLibrary::Conv_DoubleToFloat(ray.AzimuthAndElevation.X),
207 UKismetMathLibrary::Conv_DoubleToFloat(ray.AzimuthAndElevation.Y),
216 constexpr float TO_METERS = 1e-2;
218 const TWeakObjectPtr<AActor> HittedActor = OutHit.GetActor();
219 const FVector TargetVelocity = HittedActor->GetVelocity();
220 const FVector TargetLocation = OutHit.ImpactPoint;
221 const FVector Direction = (TargetLocation - RadarLocation).GetSafeNormal();
223 const float V = TO_METERS * FVector::DotProduct(DeltaVelocity, Direction);
237 State.radar_previous_location = std::array<double, 3>{World.X, World.Y, World.Z};
246 if (
State.radar_previous_location)
248 const auto &World = *
State.radar_previous_location;
249 const FVector
Location(World[0], World[1], World[2]);
FVector GlobalToLocalLocation(const FVector &InLocation) const
FVector LocalToGlobalLocation(const FVector &InLocation) const
static FActorDefinition GetSensorDefinition()
void BeginPlay() override
void GetMirrorState(carla::multigpu::SensorState &State) const override
Multi-GPU: the runtime state this sensor starts its next tick from (noise generator,...
float CalculateRelativeVelocity(const FHitResult &OutHit, const FVector &RadarLocation)
std::vector< RayData > Rays
void SetPointsPerSecond(int NewPointsPerSecond)
void SetHorizontalFOV(float NewHorizontalFOV)
FVector PrevLocation
Used to compute the velocity of the radar.
void SendLineTraces(float DeltaTime)
FCollisionQueryParams TraceParams
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime) override
const FRadarData & GetRadarData() const
ARadar(const FObjectInitializer &ObjectInitializer)
void SetVerticalFOV(float NewVerticalFOV)
void CalculateCurrentVelocity(const float DeltaTime)
void SetRange(float NewRange)
void Set(const FActorDescription &Description) override
void SetMirrorState(const carla::multigpu::SensorState &State) override
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 SetRadar(const FActorDescription &Description, ARadar *Radar)
static FActorDefinition MakeRadarDefinition()
static ALargeMapManager * GetLargeMapManager(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 GetUniformFloatInRange(float Minimum, float Maximum)
void SetState(uint32 State)
Continue from State, as GetState returned it; an impossible value is ignored.
static std::shared_ptr< ROS2 > GetInstance()
void Reset()
Deletes the current detections.
void SetResolution(uint32_t resolution)
Set a new resolution for the RadarData.
void WriteDetection(RadarDetection detection)
Adds a new detection.
Serializes a stream endpoint.
const auto & get_stream_id() const
A definition of a Carla Actor with all the variation and attributes.
A description of a Carla Actor with all its variation.
One record of frame data packet 27: the runtime state a sensor of the primary starts this frame's tic...