CARLA
 
载入中...
搜索中...
未找到
RayCastLidar.cpp
浏览该文件的文档.
1// Copyright (c) 2017 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
7#include <PxScene.h>
8#include <cmath>
9#include "Carla.h"
12#include "carla/geom/Math.h"
13
15#include "carla/geom/Math.h"
16#include "carla/ros2/ROS2.h"
17#include "carla/geom/Location.h"
19
20#include "DrawDebugHelpers.h"
21#include "Engine/CollisionProfile.h"
22#include "Runtime/Engine/Classes/Kismet/KismetMathLibrary.h"
23
28
29
30ARayCastLidar::ARayCastLidar(const FObjectInitializer& ObjectInitializer)
31 : Super(ObjectInitializer) {
32
33 RandomEngine = CreateDefaultSubobject<URandomEngine>(TEXT("RandomEngine"));
35}
36
37void ARayCastLidar::Set(const FActorDescription &ActorDescription)
38{
39 ASensor::Set(ActorDescription);
40 FLidarDescription LidarDescription;
41 UActorBlueprintFunctionLibrary::SetLidar(ActorDescription, LidarDescription);
42 Set(LidarDescription);
43}
44
45void ARayCastLidar::Set(const FLidarDescription &LidarDescription)
46{
47 Description = LidarDescription;
48 LidarData = FLidarData(Description.Channels);
51
52 // Compute drop off model parameters
55 DropOffGenActive = Description.DropOffGenRate > std::numeric_limits<float>::epsilon();
56}
57
58void ARayCastLidar::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime)
59{
60 TRACE_CPUPROFILER_EVENT_SCOPE(ARayCastLidar::PostPhysTick);
61 SimulateLidar(DeltaTime);
62
63 auto DataStream = GetDataStream(*this);
64 auto SensorTransform = DataStream.GetSensorTransform();
65
66 {
67 TRACE_CPUPROFILER_EVENT_SCOPE_STR("Send Stream");
68 DataStream.SerializeAndSend(*this, LidarData, DataStream.PopBufferFromPool());
69 }
70 // ROS2
71 #if defined(WITH_ROS2)
73 if (ROS2->IsEnabled())
74 {
75 TRACE_CPUPROFILER_EVENT_SCOPE_STR("ROS2 Send");
77 AActor* ParentActor = GetAttachParentActor();
78 if (ParentActor)
79 {
80 FTransform LocalTransformRelativeToParent = GetActorTransform().GetRelativeTransform(ParentActor->GetActorTransform());
81 ROS2->ProcessDataFromLidar(DataStream.GetSensorType(), StreamId, LocalTransformRelativeToParent, LidarData, this);
82 }
83 else
84 {
85 ROS2->ProcessDataFromLidar(DataStream.GetSensorType(), StreamId, SensorTransform, LidarData, this);
86 }
87 }
88 #endif
89
90
91}
92
94{
95 const carla::geom::Location HitPoint = RawDetection.point;
96 const float Distance = HitPoint.Length();
97
98 const float AttenAtm = Description.AtmospAttenRate;
99 const float AbsAtm = exp(-AttenAtm * Distance);
100
101 const float IntRec = AbsAtm;
102
103 return IntRec;
104}
105
106ARayCastLidar::FDetection ARayCastLidar::ComputeDetection(const FHitResult& HitInfo, const FTransform& SensorTransf) const
107{
108 FDetection Detection;
109 const FVector HitPoint = HitInfo.ImpactPoint;
110 Detection.point = SensorTransf.Inverse().TransformPosition(HitPoint);
111
112 const float Distance = Detection.point.Length();
113
114 const float AttenAtm = Description.AtmospAttenRate;
115 const float AbsAtm = exp(-AttenAtm * Distance);
116
117 const float IntRec = AbsAtm;
118
119 Detection.intensity = IntRec;
120
121 return Detection;
122}
123
124 void ARayCastLidar::PreprocessRays(uint32_t Channels, uint32_t MaxPointsPerChannel) {
125 Super::PreprocessRays(Channels, MaxPointsPerChannel);
126
127 for (auto ch = 0u; ch < Channels; ch++) {
128 for (auto p = 0u; p < MaxPointsPerChannel; p++) {
130 }
131 }
132 }
133
135 {
136 if (Description.NoiseStdDev > std::numeric_limits<float>::epsilon()) {
137 const auto ForwardVector = Detection.point.MakeUnitVector();
138 const auto Noise = ForwardVector * RandomEngine->GetNormalDistribution(0.0f, Description.NoiseStdDev);
139 Detection.point += Noise;
140 }
141
142 const float Intensity = Detection.intensity;
143 if(Intensity > Description.DropOffIntensityLimit)
144 return true;
145 else
146 return RandomEngine->GetUniformFloat() < DropOffAlpha * Intensity + DropOffBeta;
147 }
148
149 void ARayCastLidar::ComputeAndSaveDetections(const FTransform& SensorTransform) {
150 for (auto idxChannel = 0u; idxChannel < Description.Channels; ++idxChannel)
151 PointsPerChannel[idxChannel] = RecordedHits[idxChannel].size();
152
153 LidarData.ResetMemory(PointsPerChannel);
154
155 for (auto idxChannel = 0u; idxChannel < Description.Channels; ++idxChannel) {
156 for (auto& hit : RecordedHits[idxChannel]) {
157 FDetection Detection = ComputeDetection(hit, SensorTransform);
158 if (PostprocessDetection(Detection))
159 LidarData.WritePointSync(Detection);
160 else
161 PointsPerChannel[idxChannel]--;
162 }
163 }
164
165 LidarData.WriteChannelCount(PointsPerChannel);
166 }
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld * World
carla::sensor::data::LidarData FLidarData
static FActorDefinition GetSensorDefinition()
float ComputeIntensity(const FSemanticDetection &RawDetection) const
计算激光点的接收强度
bool PostprocessDetection(FDetection &Detection) const
void PreprocessRays(uint32_t Channels, uint32_t MaxPointsPerChannel) override
Method that allow to preprocess if the rays will be traced.
float DropOffAlpha
激光点强度的衰减斜率,通过衰减限制和零强度衰减来计算 点的强度保持概率为 alpha * 强度 + beta,其中 alpha = (1 - dropoff_zero_intensity) / drop...
virtual void Set(const FActorDescription &Description) override
bool DropOffGenActive
启用/禁用激光雷达点的掉落效果
ARayCastLidar(const FObjectInitializer &ObjectInitializer)
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime)
carla::sensor::data::LidarDetection FDetection
void ComputeAndSaveDetections(const FTransform &SensorTransform) override
This method uses all the saved FHitResults, compute the RawDetections and then send it to the LidarDa...
FDetection ComputeDetection(const FHitResult &HitInfo, const FTransform &SensorTransf) const
std::vector< uint32_t > PointsPerChannel
void SimulateLidar(const float DeltaTime)
Updates LidarMeasurement with the points read in DeltaTime.
void CreateLasers()
Creates a Laser for each channel.
std::vector< std::vector< bool > > RayPreprocessCondition
std::vector< std::vector< FHitResult > > RecordedHits
FLidarDescription Description
virtual void Set(const FActorDescription &Description)
Definition Sensor.cpp:35
auto GetToken() const
Return the token that allows subscribing to this sensor's stream.
FAsyncDataStream GetDataStream(const SensorT &Self)
Return the FDataStream associated with this sensor.
URandomEngine * RandomEngine
Random Engine used to provide noise for sensor output.
void SetSeed(int32 InSeed)
Definition Sensor.cpp:87
static void SetLidar(const FActorDescription &Description, FLidarDescription &Lidar)
static FActorDefinition MakeLidarDefinition(const FString &Id)
创建一个激光雷达参与者定义。
float GetUniformFloat()
float GetNormalDistribution(float Mean, float StandardDeviation)
static std::shared_ptr< ROS2 > GetInstance()
Definition ROS2.h:51
Helper class to store and serialize the data generated by a RawLidar.
静态断言,用于确保token_data结构体的大小与Token::data的大小相同。
const auto & get_stream_id() const
获取流ID的引用。
float DropOffAtZeroIntensity
对于基于强度的下降,每个零强度点被下降的概率。
int RandomSeed
该传感器使用的噪声/丢弃的随机种子。
float DropOffGenRate
随机丢弃的点的一般比例。
uint32 Channels
激光雷达的线数
float DropOffIntensityLimit
对于基于强度的下降,高于该阈值的强度值没有任何点被下降。
float AtmospAttenRate
大气中的衰减率(单位:m^-1)