12 #include "Kismet/KismetMathLibrary.h" 13 #include "Runtime/Core/Public/Async/ParallelFor.h" 26 : Super(ObjectInitializer)
28 PrimaryActorTick.bCanEverTick =
true;
30 RandomEngine = CreateDefaultSubobject<URandomEngine>(TEXT(
"RandomEngine"));
32 TraceParams = FCollisionQueryParams(FName(TEXT(
"Laser_Trace")),
true,
this);
33 TraceParams.bTraceComplex =
true;
34 TraceParams.bReturnPhysicalMaterial =
false;
40 Super::Set(ActorDescription);
83 #if defined(WITH_ROS2) 85 if (ROS2->IsEnabled())
87 TRACE_CPUPROFILER_EVENT_SCOPE_STR(
"ROS2 Send");
89 AActor* ParentActor = GetAttachParentActor();
92 FTransform LocalTransformRelativeToParent = GetActorTransform().GetRelativeTransform(ParentActor->GetActorTransform());
93 ROS2->ProcessDataFromRadar(DataStream.GetSensorType(), StreamId, LocalTransformRelativeToParent,
RadarData,
this);
97 ROS2->ProcessDataFromRadar(DataStream.GetSensorType(), StreamId, DataStream.GetSensorTransform(),
RadarData,
this);
103 TRACE_CPUPROFILER_EVENT_SCOPE_STR(
"Send Stream");
104 DataStream.SerializeAndSend(*
this,
RadarData, DataStream.PopBufferFromPool());
110 const FVector RadarLocation = GetActorLocation();
118 constexpr
float TO_METERS = 1e-2;
119 const FTransform& ActorTransform = GetActorTransform();
120 const FRotator& TransformRotator = ActorTransform.Rotator();
121 const FVector& RadarLocation = GetActorLocation();
122 const FVector& ForwardVector = GetActorForwardVector();
123 const FVector TransformXAxis = ActorTransform.GetUnitAxis(EAxis::X);
124 const FVector TransformYAxis = ActorTransform.GetUnitAxis(EAxis::Y);
125 const FVector TransformZAxis = ActorTransform.GetUnitAxis(EAxis::Z);
128 const float MaxRx = FMath::Tan(FMath::DegreesToRadians(
HorizontalFOV * 0.5f)) *
Range;
129 const float MaxRy = FMath::Tan(FMath::DegreesToRadians(
VerticalFOV * 0.5f)) *
Range;
134 Rays.resize(NumPoints);
135 for (
int i = 0; i <
Rays.size(); i++) {
138 Rays[i].Hitted =
false;
141 FCriticalSection Mutex;
142 GetWorld()->GetPhysicsScene()->GetPxScene()->lockRead();
144 TRACE_CPUPROFILER_EVENT_SCOPE(ParallelFor);
145 ParallelFor(NumPoints, [&](int32 idx) {
146 TRACE_CPUPROFILER_EVENT_SCOPE(ParallelForTask);
147 FHitResult OutHit(ForceInit);
148 const float Radius =
Rays[idx].Radius;
149 const float Angle =
Rays[idx].Angle;
152 FMath::SinCos(&Sin, &Cos, Angle);
154 const FVector EndLocation = RadarLocation + TransformRotator.RotateVector({
156 MaxRx * Radius * Cos,
160 const bool Hitted = GetWorld()->ParallelLineTraceSingleByChannel(
164 ECC_GameTraceChannel2,
166 FCollisionResponseParams::DefaultResponseParam
169 const TWeakObjectPtr<AActor> HittedActor = OutHit.Actor;
170 if (Hitted && HittedActor.Get()) {
171 Rays[idx].Hitted =
true;
175 Rays[idx].AzimuthAndElevation = FMath::GetAzimuthAndElevation (
176 (EndLocation - RadarLocation).GetSafeNormal() *
Range,
182 Rays[idx].Distance = OutHit.Distance * TO_METERS;
186 GetWorld()->GetPhysicsScene()->GetPxScene()->unlockRead();
189 for (
auto& ray :
Rays) {
192 ray.RelativeVelocity,
193 ray.AzimuthAndElevation.X,
194 ray.AzimuthAndElevation.Y,
204 constexpr
float TO_METERS = 1e-2;
206 const TWeakObjectPtr<AActor> HittedActor = OutHit.Actor;
207 const FVector TargetVelocity = HittedActor->GetVelocity();
208 const FVector TargetLocation = OutHit.ImpactPoint;
209 const FVector Direction = (TargetLocation - RadarLocation).GetSafeNormal();
211 const float V = TO_METERS * FVector::DotProduct(DeltaVelocity, Direction);
void SetHorizontalFOV(float NewHorizontalFOV)
auto GetToken() const
Return the token that allows subscribing to this sensor's stream.
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime) override
std::vector< RayData > Rays
void SetRange(float NewRange)
A definition of a Carla Actor with all the variation and attributes.
void BeginPlay() override
void SetResolution(uint32_t resolution)
Set a new resolution for the RadarData.
void SendLineTraces(float DeltaTime)
float GetUniformFloatInRange(float Minimum, float Maximum)
static void SetRadar(const FActorDescription &Description, ARadar *Radar)
void WriteDetection(RadarDetection detection)
Adds a new detection.
void Set(const FActorDescription &Description) override
A description of a Carla Actor with all its variation.
ARadar(const FObjectInitializer &ObjectInitializer)
static FActorDefinition GetSensorDefinition()
const auto & get_stream_id() const
float CalculateRelativeVelocity(const FHitResult &OutHit, const FVector &RadarLocation)
void CalculateCurrentVelocity(const float DeltaTime)
FAsyncDataStream GetDataStream(const SensorT &Self)
Return the FDataStream associated with this sensor.
static FActorDefinition MakeRadarDefinition()
FVector PrevLocation
Used to compute the velocity of the radar.
static std::shared_ptr< ROS2 > GetInstance()
FCollisionQueryParams TraceParams
void Reset()
Deletes the current detections.
carla::streaming::detail::token_type token_type
void SetVerticalFOV(float NewVerticalFOV)
void SetPointsPerSecond(int NewPointsPerSecond)
URandomEngine * RandomEngine
Random Engine used to provide noise for sensor output.