Agrarsense
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
AOverlapSensor Class Reference

#include <OverlapSensor.h>

Inheritance diagram for AOverlapSensor:
Inheritance graph
[legend]
Collaboration diagram for AOverlapSensor:
Collaboration graph
[legend]

Public Member Functions

 AOverlapSensor (const FObjectInitializer &ObjectInitializer)
 
void Init (FOverlapSensorParameters InParameters)
 
virtual ESensorTypes GetSensorType () const override
 
void SetOverlapBounds (const FVector &NewSize)
 
void SetVisualizeOverlapArea (bool Visualize)
 
void SetOverlapRelativePosition (const FVector &Vector)
 
void SetOverlapResponseToAllChannels (const ECollisionResponse Response)
 
bool IsVisualizingOverlapArea () const
 
virtual FString GetParametersAsString () const override
 
- Public Member Functions inherited from ASensor
 ASensor (const FObjectInitializer &ObjectInitializer)
 
FString ExportToJsonFile (const FString &FileName)
 
virtual ESensorTypes GetSensorType () const
 
FString GetSensorIdentifier () const
 
void SetSensorIdentifier (const FString newIdentifier)
 
FString GetSensorName () const
 
virtual FString GetParametersAsString () const
 
void SetSensorName (const FString newName)
 
virtual FString GetTopicName ()
 
UTopic * GetROSTopic () const
 
void SetSimulateSensor (bool SimulateSensor)
 
bool CanSimulateSensor () const
 
ASensorModelGetSensorModel () const
 
void SetSensorModel (ASensorModel *NewSensorModel)
 
FORCEINLINE bool IsROSConnected () const
 
UROSIntegrationGameInstance * GetROSGameInstance () const
 
virtual FString GetActorID_Implementation () const override
 
virtual FString GetActorName_Implementation () const override
 
virtual FString GetActorInformation_Implementation () const override
 
virtual void SetActorName_Implementation (const FString &NewActorName) override
 
virtual void SetActorIDAndName_Implementation (const FString &NewActorName, const FString &NewID) override
 
- Public Member Functions inherited from IActorInformation
FString GetActorID () const
 
FString GetActorName () const
 
FString GetActorInformation () const
 
void SetActorName (const FString &NewActorName)
 
void SetActorIDAndName (const FString &NewActorName, const FString &NewID)
 

Protected Member Functions

void OnOverlapBegin (UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
 
void OnOverlapEnd (UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex)
 
- Protected Member Functions inherited from ASensor
virtual void BeginPlay () override
 
virtual void EndPlay (const EEndPlayReason::Type EndPlayReason) override
 
virtual void CreateROSTopic ()
 
virtual void DestroyROSTopic ()
 
virtual void CreateDataSavePath ()
 
bool IsLogFileCreated ()
 
virtual void CreateLogFile ()
 
void WriteToLogFile (const FString &Message)
 

Protected Attributes

UBoundsVisualizerComponent * BoundsComponent = nullptr
 
- Protected Attributes inherited from ASensor
UTopic * ROSTopic = nullptr
 
bool SendDataToROS = true
 
ULogFileLogFile = nullptr
 
FString FileSavePath
 
UROSIntegrationGameInstance * ROSInstance = nullptr
 

Private Member Functions

virtual void BeginPlay () override
 
virtual void EndPlay (const EEndPlayReason::Type EndPlayReason) override
 
void BuildAndSendMessage (const FString &Prefix, AActor *Actor)
 
FString TryGetID (AActor *Actor)
 

Private Attributes

TSharedPtr< ROSMessages::std_msgs::String > ROSMessage
 
FOverlapSensorParameters Parameters
 
AVehicleVehicle = nullptr
 
FString VehicleName
 
FString VehicleID
 
bool IsVisible = false
 

Additional Inherited Members

- Static Public Member Functions inherited from ASensor
static void HideComponentForAllCameras (UPrimitiveComponent *PrimitiveComponent)
 
static TMap< FString, FColor > GetSemanticColors ()
 
static TArray< TWeakObjectPtr< UPrimitiveComponent > > GetComponentsToHide ()
 
- Static Public Member Functions inherited from IActorInformation
static void SetAndValidateActorIDAndName (FString &ActorName, FString &ActorID, TWeakObjectPtr< AActor > Actor)
 
static bool DestroyActorByID (const FString &ID)
 
static AActor * GetActorByID (const FString &ID)
 
template<typename T >
static TArray< T * > GetActorsWithInterface ()
 
static void PrintAllIds ()
 
- Public Attributes inherited from ASensor
FSensorDestroy OnSensorDestroy
 
FString AttachedToComponent
 
FName AttachedToBone
 
- Static Protected Member Functions inherited from ASensor
template<typename InStructType >
static FString StructToString (const InStructType &InStruct)
 
- Static Protected Attributes inherited from ASensor
static FPrimitiveAdded OnPrimitiveAdded
 
static const FName NiagaraPointsInt = "User.PointCount"
 
static const FName NiagaraHitPoints = "User.HitPoints"
 
static const FName NiagaraHitColors = "User.HitColors"
 
static const FName NiagaraPointsFloat = "User.Test"
 

Detailed Description

Overlap sensor is a sensor that detects overlap with certain objects in the simulation. This can be useful for development purposes, such as determining when a Walker/Vehicle enters the "safe zone" of another Vehicle. Note that this sensor does not simulate any real-world sensor.

Definition at line 28 of file OverlapSensor.h.

Constructor & Destructor Documentation

◆ AOverlapSensor()

AOverlapSensor::AOverlapSensor ( const FObjectInitializer &  ObjectInitializer)

Definition at line 19 of file OverlapSensor.cpp.

19 : Super(ObjectInitializer)
20{
21 PrimaryActorTick.bCanEverTick = false;
22
23 BoundsComponent = CreateDefaultSubobject<UBoundsVisualizerComponent>(TEXT("BoundsComponent"));
24 BoundsComponent->SetupAttachment(RootComponent);
25 BoundsComponent->SetGenerateOverlapEvents(true);
26 BoundsComponent->SetVisibility(false);
27 BoundsComponent->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
28}
UBoundsVisualizerComponent * BoundsComponent
Definition: OverlapSensor.h:98

References BoundsComponent.

Member Function Documentation

◆ BeginPlay()

void AOverlapSensor::BeginPlay ( )
overrideprivatevirtual

Reimplemented from ASensor.

Definition at line 95 of file OverlapSensor.cpp.

96{
97 Super::BeginPlay();
98
99 BoundsComponent->OnComponentBeginOverlap.AddDynamic(this, &AOverlapSensor::OnOverlapBegin);
100 BoundsComponent->OnComponentEndOverlap.AddDynamic(this, &AOverlapSensor::OnOverlapEnd);
101}
void OnOverlapEnd(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex)
void OnOverlapBegin(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)

References BoundsComponent, OnOverlapBegin(), and OnOverlapEnd().

◆ BuildAndSendMessage()

void AOverlapSensor::BuildAndSendMessage ( const FString &  Prefix,
AActor *  Actor 
)
private

Definition at line 191 of file OverlapSensor.cpp.

192{
193 if (!Actor)
194 {
195 return;
196 }
197
198 FString ID = TryGetID(Actor);
199 FString Message;
200
201 // Calculate distance from sensor parent (vehicle) to incoming Actor
202 AActor* From = this;
203 if (AActor* VehicleActorPtr = GetParentActor())
204 {
205 From = VehicleActorPtr;
206 }
207 float DistanceInMeters = FVector::Dist(From->GetActorLocation(), Actor->GetActorLocation()) / 100.0f;
208
209 // Add distance to the end of the message
210 if (ID.IsEmpty())
211 {
212 Message = FString::Printf(TEXT("%s: %s, Distance: %.2f m"), *Prefix, *Actor->GetName(), DistanceInMeters);
213 }
214 else
215 {
216 Message = FString::Printf(TEXT("%s: %s: ID: %s, Distance: %.2f m"), *Prefix, *Actor->GetName(), *ID, DistanceInMeters);
217 }
218
219 UTopic* Topic = GetROSTopic();
220 if (ROSMessage.IsValid() && Topic && IsROSConnected())
221 {
222 ROSMessage->_Data = Message;
223 Topic->Publish(ROSMessage);
224 }
225
226 WriteToLogFile(Message);
227
228 if (Vehicle)
229 {
230 FString Msg = FString::Printf(TEXT("Actor '%s' with ID: '%s' %s"), *VehicleName, *VehicleID, *Message);
232 }
233}
TSharedPtr< ROSMessages::std_msgs::String > ROSMessage
FString VehicleName
AVehicle * Vehicle
FString TryGetID(AActor *Actor)
UTopic * GetROSTopic() const
Definition: Sensor.h:141
void WriteToLogFile(const FString &Message)
Definition: Sensor.cpp:269
FORCEINLINE bool IsROSConnected() const
Definition: Sensor.h:192
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)

References ASensor::GetROSTopic(), ASensor::IsROSConnected(), SimulatorLog::Log(), ROSMessage, TryGetID(), Vehicle, VehicleID, VehicleName, and ASensor::WriteToLogFile().

Referenced by OnOverlapBegin(), and OnOverlapEnd().

◆ EndPlay()

void AOverlapSensor::EndPlay ( const EEndPlayReason::Type  EndPlayReason)
overrideprivatevirtual

Reimplemented from ASensor.

Definition at line 103 of file OverlapSensor.cpp.

104{
105 Super::EndPlay(EndPlayReason);
106
107 BoundsComponent->OnComponentBeginOverlap.RemoveDynamic(this, &AOverlapSensor::OnOverlapBegin);
108 BoundsComponent->OnComponentEndOverlap.RemoveDynamic(this, &AOverlapSensor::OnOverlapEnd);
109}

References BoundsComponent, OnOverlapBegin(), and OnOverlapEnd().

◆ GetParametersAsString()

virtual FString AOverlapSensor::GetParametersAsString ( ) const
inlineoverridevirtual

Get this sensor parameters as string

Reimplemented from ASensor.

Definition at line 82 of file OverlapSensor.h.

83 {
85 }
FOverlapSensorParameters Parameters
static FString StructToString(const InStructType &InStruct)
Definition: Sensor.h:305

◆ GetSensorType()

virtual ESensorTypes AOverlapSensor::GetSensorType ( ) const
inlineoverridevirtual

Get type of the sensor

Returns
Sensor's type

Reimplemented from ASensor.

Definition at line 43 of file OverlapSensor.h.

44 {
46 }

References Overlap.

◆ Init()

void AOverlapSensor::Init ( FOverlapSensorParameters  InParameters)

Definition at line 30 of file OverlapSensor.cpp.

31{
32 Parameters = InParameters;
33
34 Vehicle = Cast<AVehicle>(Parameters.OwningActor);
35
36 if (Vehicle)
37 {
38 VehicleName = IActorInformation::Execute_GetActorName(Vehicle);
39 VehicleID = IActorInformation::Execute_GetActorID(Vehicle);
40 }
41
42 UPrimitiveComponent* TriggerComponent = Cast<UPrimitiveComponent>(BoundsComponent);
43
44 // if you change DefaultEngine.ini collision channels then this index must be changed as well!
45 const int32 OverlapSensorIndex = 2;
46 const ECollisionChannel OverlapSensorChannel = UCollisionProfile::Get()->ConvertToCollisionChannel(false, OverlapSensorIndex);
47 BoundsComponent->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
48 BoundsComponent->SetGenerateOverlapEvents(true);
49 BoundsComponent->SetCollisionObjectType(OverlapSensorChannel);
50 BoundsComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
51 BoundsComponent->SetCollisionResponseToChannel(OverlapSensorChannel, ECollisionResponse::ECR_Overlap);
52 BoundsComponent->SetRelativeLocation(Parameters.RelativePosition);
53
54 if (RootComponent)
55 {
56 BoundsComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepWorldTransform);
57 }
58
59 TriggerComponent = BoundsComponent;
60
61 if (TriggerComponent)
62 {
63 SetOverlapBounds(InParameters.Size);
64 //SetRootComponent(TriggerComponent);
66
67 ROSMessages::std_msgs::String StringMsg;
68 ROSMessage = MakeShared<ROSMessages::std_msgs::String>(StringMsg);
69
71 {
73 }
74 }
75
77 {
78 // Adjust the scale of the Sphere/Box component bounds to match the scale of the VisualizerComponent mesh
79 FVector ScaledVector = FVector(Parameters.Size.X / SIZE_DIVISOR, Parameters.Size.Y / SIZE_DIVISOR, Parameters.Size.Z / SIZE_DIVISOR);
80 BoundsComponent->SetRelativeScale3D(ScaledVector);
81 BoundsComponent->SetRelativeLocation(Parameters.RelativePosition);
82
83 if (RootComponent)
84 {
85 BoundsComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepWorldTransform);
86 }
87 }
88
90 {
91 AttachToActor(Parameters.OwningActor, FAttachmentTransformRules::KeepWorldTransform);
92 }
93}
static constexpr float SIZE_DIVISOR
void SetOverlapBounds(const FVector &NewSize)
void SetSimulateSensor(bool SimulateSensor)
Definition: Sensor.h:151
virtual void CreateROSTopic()
Definition: Sensor.cpp:173

References BoundsComponent, FOverlapSensorParameters::CreateROSTopic, ASensor::CreateROSTopic(), FOverlapSensorParameters::OwningActor, Parameters, FOverlapSensorParameters::RelativePosition, ROSMessage, SetOverlapBounds(), ASensor::SetSimulateSensor(), FOverlapSensorParameters::Size, SIZE_DIVISOR, Vehicle, VehicleID, and VehicleName.

Referenced by USensorFactory::SpawnOverlapSensor().

◆ IsVisualizingOverlapArea()

bool AOverlapSensor::IsVisualizingOverlapArea ( ) const
inline

Definition at line 77 of file OverlapSensor.h.

78 {
79 return IsVisible;
80 }

◆ OnOverlapBegin()

void AOverlapSensor::OnOverlapBegin ( UPrimitiveComponent *  OverlappedComponent,
AActor *  OtherActor,
UPrimitiveComponent *  OtherComp,
int32  OtherBodyIndex,
bool  bFromSweep,
const FHitResult &  SweepResult 
)
protected

Definition at line 165 of file OverlapSensor.cpp.

167{
168 // Don't allow Overlapping with other Overlapping sensors..
169 AOverlapSensor* OtherOverlapSensor = Cast<AOverlapSensor>(OtherActor);
170 if (OtherOverlapSensor)
171 {
172 return;
173 }
174
175 BuildAndSendMessage("Overlap Begin with", OtherActor);
176}
void BuildAndSendMessage(const FString &Prefix, AActor *Actor)

References BuildAndSendMessage().

Referenced by BeginPlay(), and EndPlay().

◆ OnOverlapEnd()

void AOverlapSensor::OnOverlapEnd ( UPrimitiveComponent *  OverlappedComponent,
AActor *  OtherActor,
UPrimitiveComponent *  OtherComp,
int32  OtherBodyIndex 
)
protected

Definition at line 178 of file OverlapSensor.cpp.

180{
181 // Don't allow Overlapping with other Overlapping sensors..
182 AOverlapSensor* OtherOverlapSensor = Cast< AOverlapSensor>(OtherActor);
183 if (OtherOverlapSensor)
184 {
185 return;
186 }
187
188 BuildAndSendMessage("Overlap End with", OtherActor);
189}

References BuildAndSendMessage().

Referenced by BeginPlay(), and EndPlay().

◆ SetOverlapBounds()

void AOverlapSensor::SetOverlapBounds ( const FVector &  NewSize)

Set this Overlap sensor Overlap bounds.

Definition at line 111 of file OverlapSensor.cpp.

112{
113 Parameters.Size = NewSize;
114
115 if (BoundsComponent)
116 {
117 // Adjust the scale of the Sphere/Box component bounds to match the scale of the VisualizerComponent mesh
118 FVector ScaledVector = FVector(Parameters.Size.X / SIZE_DIVISOR, Parameters.Size.Y / SIZE_DIVISOR, Parameters.Size.Z / SIZE_DIVISOR);
119 BoundsComponent->SetRelativeScale3D(ScaledVector);
120 }
121
122 FString ActorID = IActorInformation::Execute_GetActorID(this);
123
124 FString Msg = FString::Printf(TEXT("OverlapSensor with ID: %s changed overlap bounds to: %s"), *ActorID, *NewSize.ToString());
126}

References BoundsComponent, SimulatorLog::Log(), Parameters, FOverlapSensorParameters::Size, and SIZE_DIVISOR.

Referenced by UROSCommands::HandleChangeOverlapSensorBounds(), and Init().

◆ SetOverlapRelativePosition()

void AOverlapSensor::SetOverlapRelativePosition ( const FVector &  Vector)

Set this Overlap sensor relative position to parent.

Definition at line 138 of file OverlapSensor.cpp.

139{
141
142 if (BoundsComponent)
143 {
144 BoundsComponent->SetRelativeLocation(Parameters.RelativePosition);
145 }
146
147 FString Msg = FString::Printf(TEXT("OverlapSensor.cpp: Changed overlap relative position to: %s"), *Vector.ToString());
149}

References BoundsComponent, SimulatorLog::Log(), Parameters, and FOverlapSensorParameters::RelativePosition.

Referenced by UROSCommands::HandleChangeOverlapSensorPosition().

◆ SetOverlapResponseToAllChannels()

void AOverlapSensor::SetOverlapResponseToAllChannels ( const ECollisionResponse  Response)

Set this sensor overlap response.

Parameters
ResponseThe desired collision response. ECR_Overlap: Allows all overlaps to be recognized. ECR_Ignore: Allows only Walker overlaps to be recognized. Other than ECR_Overlap or ECR_Ignore is ignored.

Definition at line 151 of file OverlapSensor.cpp.

152{
153 if (Response != ECollisionResponse::ECR_Overlap && Response != ECollisionResponse::ECR_Ignore)
154 {
155 // If Response is neither ECR_Overlap nor ECR_Ignore, return
156 return;
157 }
158
159 if (BoundsComponent)
160 {
161 BoundsComponent->SetCollisionResponseToAllChannels(Response);
162 }
163}

References BoundsComponent.

◆ SetVisualizeOverlapArea()

void AOverlapSensor::SetVisualizeOverlapArea ( bool  Visualize)

Set this Overlap sensor bounds visulization on or off.

Definition at line 128 of file OverlapSensor.cpp.

129{
130 IsVisible = Visualize;
131
132 if (BoundsComponent)
133 {
134 BoundsComponent->SetVisibility(IsVisible);
135 }
136}

References BoundsComponent, and IsVisible.

Referenced by UROSCommands::HandleVisualizeOverlapSensorsBounds(), and AVehicle::SetVisualizeVehicleOverlapArea().

◆ TryGetID()

FString AOverlapSensor::TryGetID ( AActor *  Actor)
private

Definition at line 235 of file OverlapSensor.cpp.

236{
237 FString ID;
238
239 if (Actor && Actor->GetClass()->ImplementsInterface(UActorInformation::StaticClass()))
240 {
241
242 if (Actor->Implements<UActorInformation>())
243 {
244 ID = IActorInformation::Execute_GetActorID(Actor);
245 }
246 }
247
248 return ID;
249}

Referenced by BuildAndSendMessage().

Member Data Documentation

◆ BoundsComponent

UBoundsVisualizerComponent* AOverlapSensor::BoundsComponent = nullptr
protected

◆ IsVisible

bool AOverlapSensor::IsVisible = false
private

Definition at line 121 of file OverlapSensor.h.

Referenced by SetVisualizeOverlapArea().

◆ Parameters

FOverlapSensorParameters AOverlapSensor::Parameters
private

Definition at line 112 of file OverlapSensor.h.

Referenced by Init(), SetOverlapBounds(), and SetOverlapRelativePosition().

◆ ROSMessage

TSharedPtr<ROSMessages::std_msgs::String> AOverlapSensor::ROSMessage
private

Definition at line 110 of file OverlapSensor.h.

Referenced by BuildAndSendMessage(), and Init().

◆ Vehicle

AVehicle* AOverlapSensor::Vehicle = nullptr
private

◆ VehicleID

FString AOverlapSensor::VehicleID
private

Definition at line 119 of file OverlapSensor.h.

Referenced by BuildAndSendMessage(), and Init().

◆ VehicleName

FString AOverlapSensor::VehicleName
private

Definition at line 117 of file OverlapSensor.h.

Referenced by BuildAndSendMessage(), and Init().


The documentation for this class was generated from the following files: