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);
161 const FVector EndLocation = RadarLocation + TransformRotator.RotateVector({
163 MaxRx * Radius * Cos,
167 const bool Hitted = GetWorld()->ParallelLineTraceSingleByChannel(
171 ECC_GameTraceChannel2,
173 FCollisionResponseParams::DefaultResponseParam
176 const TWeakObjectPtr<AActor> HittedActor = OutHit.GetActor();
177 if (Hitted && HittedActor.Get()) {
178 Rays[idx].Hitted =
true;
182 Rays[idx].AzimuthAndElevation = FMath::GetAzimuthAndElevation (
183 (EndLocation - RadarLocation).GetSafeNormal() *
Range,
189 Rays[idx].Distance = OutHit.Distance * TO_METERS;
193 LockedPhysObject.Release();
196 for (
auto& ray :
Rays)
202 ray.RelativeVelocity,
203 UKismetMathLibrary::Conv_DoubleToFloat(ray.AzimuthAndElevation.X),
204 UKismetMathLibrary::Conv_DoubleToFloat(ray.AzimuthAndElevation.Y),
213 constexpr float TO_METERS = 1e-2;
215 const TWeakObjectPtr<AActor> HittedActor = OutHit.GetActor();
216 const FVector TargetVelocity = HittedActor->GetVelocity();
217 const FVector TargetLocation = OutHit.ImpactPoint;
218 const FVector Direction = (TargetLocation - RadarLocation).GetSafeNormal();
220 const float V = TO_METERS * FVector::DotProduct(DeltaVelocity, Direction);
234 State.radar_previous_location = std::array<double, 3>{World.X, World.Y, World.Z};
243 if (
State.radar_previous_location)
245 const auto &World = *
State.radar_previous_location;
246 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...