CARLA
ObstacleDetectionSensor.cpp
Go to the documentation of this file.
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 
16 AObstacleDetectionSensor::AObstacleDetectionSensor(const FObjectInitializer &ObjectInitializer)
17  : Super(ObjectInitializer)
18 {
19  PrimaryActorTick.bCanEverTick = true;
20 }
21 
23 {
24  FActorDefinition SensorDefinition = FActorDefinition();
25  UActorBlueprintFunctionLibrary::MakeObstacleDetectorDefinitions(TEXT("other"), TEXT("obstacle"), SensorDefinition);
26  return SensorDefinition;
27 }
28 
30 {
31  //Multiplying numbers for 100 in order to convert from meters to centimeters
32  Super::Set(Description);
34  "distance",
35  Description.Variations,
36  Distance) * 100.0f;
38  "hit_radius",
39  Description.Variations,
40  HitRadius) * 100.0f;
42  "only_dynamics",
43  Description.Variations,
45 #if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
47  "debug_linetrace",
48  Description.Variations,
50 #endif
51 }
52 
53 void AObstacleDetectionSensor::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime)
54 {
55  TRACE_CPUPROFILER_EVENT_SCOPE(AObstacleDetectionSensor::PostPhysTick);
56  const FVector &Start = GetActorLocation();
57  const FVector &End = Start + (GetActorForwardVector() * Distance);
58  UWorld* CurrentWorld = GetWorld();
59 
60  // Struct in which the result of the scan will be saved
61  FHitResult HitOut = FHitResult();
62 
63  // Initialization of Query Parameters
64  FCollisionQueryParams TraceParams(FName(TEXT("ObstacleDetection Trace")), true, this);
65 
66 #if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
67  // If debug mode enabled, we create a tag that will make the sweep be
68  // displayed.
69  if (bDebugLineTrace)
70  {
71  const FName TraceTag("ObstacleDebugTrace");
72  CurrentWorld->DebugDrawTraceTag = TraceTag;
73  TraceParams.TraceTag = TraceTag;
74  }
75 #endif
76 
77  // Hit against complex meshes
78  TraceParams.bTraceComplex = true;
79 
80  // Ignore trigger boxes
81  TraceParams.bIgnoreTouches = true;
82 
83  // Limit the returned information
84  TraceParams.bReturnPhysicalMaterial = false;
85 
86  // Ignore ourselves
87  TraceParams.AddIgnoredActor(this);
88  if(Super::GetOwner()!=nullptr)
89  TraceParams.AddIgnoredActor(Super::GetOwner());
90 
91  bool isHitReturned;
92  // Choosing a type of sweep is a workaround until everything get properly
93  // organized under correct collision channels and object types.
94  if (bOnlyDynamics)
95  {
96  // If we go only for dynamics, we check the object type AllDynamicObjects
97  FCollisionObjectQueryParams TraceChannel = FCollisionObjectQueryParams(
98  FCollisionObjectQueryParams::AllDynamicObjects);
99  isHitReturned = CurrentWorld->SweepSingleByObjectType(
100  HitOut,
101  Start,
102  End,
103  FQuat(),
104  TraceChannel,
105  FCollisionShape::MakeSphere(HitRadius),
106  TraceParams);
107  }
108  else
109  {
110  // Else, if we go for everything, we get everything that interacts with a
111  // Pawn
112  ECollisionChannel TraceChannel = ECC_WorldStatic;
113  isHitReturned = CurrentWorld->SweepSingleByChannel(
114  HitOut,
115  Start,
116  End,
117  FQuat(),
118  TraceChannel,
119  FCollisionShape::MakeSphere(HitRadius),
120  TraceParams);
121  }
122 
123  if (isHitReturned)
124  {
125  OnObstacleDetectionEvent(this, HitOut.Actor.Get(), HitOut.Distance, HitOut);
126  }
127 }
128 
130  AActor *Actor,
131  AActor *OtherActor,
132  float HitDistance,
133  const FHitResult &Hit)
134 {
135  if ((Actor != nullptr) && (OtherActor != nullptr))
136  {
137  const auto &Episode = GetEpisode();
138  GetDataStream(*this).Send(*this,
139  Episode.SerializeActor(Actor),
140  Episode.SerializeActor(OtherActor),
141  HitDistance/100.0f);
142  }
143 }
static void MakeObstacleDetectorDefinitions(const FString &Type, const FString &Id, FActorDefinition &Definition)
void Set(const FActorDescription &Description) override
carla::rpc::Actor SerializeActor(FCarlaActor *CarlaActor) const
Create a serializable object describing the actor.
static FActorDefinition GetSensorDefinition()
static float RetrieveActorAttributeToFloat(const FString &Id, const TMap< FString, FActorAttribute > &Attributes, float Default)
A definition of a Carla Actor with all the variation and attributes.
virtual void PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSeconds) override
TMap< FString, FActorAttribute > Variations
User selected variations of the actor.
carla::SharedPtr< cc::Actor > Actor
void OnObstacleDetectionEvent(AActor *Actor, AActor *OtherActor, float Distance, const FHitResult &Hit)
A description of a Carla Actor with all its variation.
AObstacleDetectionSensor(const FObjectInitializer &ObjectInitializer)
FAsyncDataStream GetDataStream(const SensorT &Self)
Return the FDataStream associated with this sensor.
static bool RetrieveActorAttributeToBool(const FString &Id, const TMap< FString, FActorAttribute > &Attributes, bool Default)
void Send(SensorT &Sensor, ArgsT &&... Args)
Send some data down the stream.