CARLA
 
载入中...
搜索中...
未找到
ObstacleDetectionSensor.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 "Carla.h"
9
15
17#include "carla/ros2/ROS2.h"
19
20AObstacleDetectionSensor::AObstacleDetectionSensor(const FObjectInitializer &ObjectInitializer)
21 : Super(ObjectInitializer)
22{
23 PrimaryActorTick.bCanEverTick = true;
24}
25
27{
28 FActorDefinition SensorDefinition = FActorDefinition();
29 UActorBlueprintFunctionLibrary::MakeObstacleDetectorDefinitions(TEXT("other"), TEXT("obstacle"), SensorDefinition);
30 return SensorDefinition;
31}
32
34{
35 //Multiplying numbers for 100 in order to convert from meters to centimeters
36 Super::Set(Description);
38 "distance",
39 Description.Variations,
40 Distance) * 100.0f;
42 "hit_radius",
43 Description.Variations,
44 HitRadius) * 100.0f;
46 "only_dynamics",
47 Description.Variations,
49#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
51 "debug_linetrace",
52 Description.Variations,
54#endif
55}
56
57void AObstacleDetectionSensor::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime)
58{
59 TRACE_CPUPROFILER_EVENT_SCOPE(AObstacleDetectionSensor::PostPhysTick);
60 const FVector &Start = GetActorLocation();
61 const FVector &End = Start + (GetActorForwardVector() * Distance);
62 UWorld* CurrentWorld = GetWorld();
63
64 // Struct in which the result of the scan will be saved
65 FHitResult HitOut = FHitResult();
66
67 // Initialization of Query Parameters
68 FCollisionQueryParams TraceParams(FName(TEXT("ObstacleDetection Trace")), true, this);
69
70#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
71 // If debug mode enabled, we create a tag that will make the sweep be
72 // displayed.
74 {
75 const FName TraceTag("ObstacleDebugTrace");
76 CurrentWorld->DebugDrawTraceTag = TraceTag;
77 TraceParams.TraceTag = TraceTag;
78 }
79#endif
80
81 // Hit against complex meshes
82 TraceParams.bTraceComplex = true;
83
84 // Ignore trigger boxes
85 TraceParams.bIgnoreTouches = true;
86
87 // Limit the returned information
88 TraceParams.bReturnPhysicalMaterial = false;
89
90 // Ignore ourselves
91 TraceParams.AddIgnoredActor(this);
92 if(Super::GetOwner()!=nullptr)
93 TraceParams.AddIgnoredActor(Super::GetOwner());
94
95 bool isHitReturned;
96 // Choosing a type of sweep is a workaround until everything get properly
97 // organized under correct collision channels and object types.
98 if (bOnlyDynamics)
99 {
100 // If we go only for dynamics, we check the object type AllDynamicObjects
101 FCollisionObjectQueryParams TraceChannel = FCollisionObjectQueryParams(
102 FCollisionObjectQueryParams::AllDynamicObjects);
103 isHitReturned = CurrentWorld->SweepSingleByObjectType(
104 HitOut,
105 Start,
106 End,
107 FQuat(),
108 TraceChannel,
109 FCollisionShape::MakeSphere(HitRadius),
110 TraceParams);
111 }
112 else
113 {
114 // Else, if we go for everything, we get everything that interacts with a
115 // Pawn
116 ECollisionChannel TraceChannel = ECC_WorldStatic;
117 isHitReturned = CurrentWorld->SweepSingleByChannel(
118 HitOut,
119 Start,
120 End,
121 FQuat(),
122 TraceChannel,
123 FCollisionShape::MakeSphere(HitRadius),
124 TraceParams);
125 }
126
127 if (isHitReturned)
128 {
129 OnObstacleDetectionEvent(this, HitOut.Actor.Get(), HitOut.Distance, HitOut);
130 }
131}
132
134 AActor *Actor,
135 AActor *OtherActor,
136 float HitDistance,
137 const FHitResult &Hit)
138{
139 if ((Actor != nullptr) && (OtherActor != nullptr) && IsStreamReady())
140 {
141 const auto &Episode = GetEpisode();
142
143 auto DataStream = GetDataStream(*this);
144
145 // ROS2
146 #if defined(WITH_ROS2)
147 auto ROS2 = carla::ros2::ROS2::GetInstance();
148 if (ROS2->IsEnabled())
149 {
150 TRACE_CPUPROFILER_EVENT_SCOPE_STR("ROS2 Send");
152 AActor* ParentActor = GetAttachParentActor();
153 if (ParentActor)
154 {
155 FTransform LocalTransformRelativeToParent = GetActorTransform().GetRelativeTransform(ParentActor->GetActorTransform());
156 ROS2->ProcessDataFromObstacleDetection(DataStream.GetSensorType(), StreamId, LocalTransformRelativeToParent, Actor, OtherActor, HitDistance/100.0f, this);
157 }
158 else
159 {
160 ROS2->ProcessDataFromObstacleDetection(DataStream.GetSensorType(), StreamId, DataStream.GetSensorTransform(), Actor, OtherActor, HitDistance/100.0f, this);
161 }
162 }
163 #endif
164
165 DataStream.SerializeAndSend(*this,
166 Episode.SerializeActor(Actor),
167 Episode.SerializeActor(OtherActor),
168 HitDistance/100.0f);
169 }
170}
void Set(const FActorDescription &Description) override
void OnObstacleDetectionEvent(AActor *Actor, AActor *OtherActor, float Distance, const FHitResult &Hit)
AObstacleDetectionSensor(const FObjectInitializer &ObjectInitializer)
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds) override
static FActorDefinition GetSensorDefinition()
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.
static float RetrieveActorAttributeToFloat(const FString &Id, const TMap< FString, FActorAttribute > &Attributes, float Default)
static bool RetrieveActorAttributeToBool(const FString &Id, const TMap< FString, FActorAttribute > &Attributes, bool Default)
static void MakeObstacleDetectorDefinitions(const FString &Type, const FString &Id, FActorDefinition &Definition)
carla::rpc::Actor SerializeActor(FCarlaActor *CarlaActor) const
Create a serializable object describing the actor.
static std::shared_ptr< ROS2 > GetInstance()
Definition ROS2.h:51
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.
TMap< FString, FActorAttribute > Variations
User selected variations of the actor.