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

#include <TransformSensor.h>

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

Public Member Functions

 ATransformSensor (const FObjectInitializer &ObjectInitializer)
 
void Init (AActor *TransformActor, bool SimulateSensor=true)
 
virtual ESensorTypes GetSensorType () 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)
 
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

virtual void BeginPlay () override
 
virtual void EndPlay (const EEndPlayReason::Type EndPlayReason) override
 
virtual void CreateROSTopic () override
 
virtual void DestroyROSTopic () override
 
- 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)
 

Private Member Functions

void TickParallel (float DeltaTime)
 
void SendTransformDataToROS (const FVector &Translation, const FQuat &Rotation)
 
void SendGnssDataToROS (const FVector &ActorPosition)
 

Private Attributes

TSharedPtr< ROSMessages::geometry_msgs::Transform > TransformMessage
 
TSharedPtr< ROSMessages::std_msgs::String > GnssMessageString
 
TWeakObjectPtr< AActor > Target
 
FVector PreviousActorPosition = FVector(0.0f, 0.0f, 0.0f)
 
FTickEntry TickEntry
 
UTopic * GnssTopic = nullptr
 
AGeoReferencingSystem * GeoReferencingSystem = nullptr
 

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)
 
- Protected Attributes inherited from ASensor
UTopic * ROSTopic = nullptr
 
bool SendDataToROS = true
 
ULogFileLogFile = nullptr
 
FString FileSavePath
 
UROSIntegrationGameInstance * ROSInstance = nullptr
 
- 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

Definition at line 24 of file TransformSensor.h.

Constructor & Destructor Documentation

◆ ATransformSensor()

ATransformSensor::ATransformSensor ( const FObjectInitializer &  ObjectInitializer)

Definition at line 14 of file TransformSensor.cpp.

14 : Super(ObjectInitializer)
15{
16 // TickManager handles this Actor ticking
17 PrimaryActorTick.bCanEverTick = false;
18}

Member Function Documentation

◆ BeginPlay()

void ATransformSensor::BeginPlay ( )
overrideprotectedvirtual

Reimplemented from ASensor.

Definition at line 33 of file TransformSensor.cpp.

34{
35 Super::BeginPlay();
36
37 SetActorEnableCollision(false);
38 PreviousActorPosition = GetActorLocation();
39
40 TickEntry = ATickManager::AddTick(this, BindTick(this, &ATransformSensor::TickParallel), ETickType::LateTickParallel);
41}
static auto BindTick(ObjectType *Object, FunctionType Function)
Definition: TickManager.h:182
static FTickEntry AddTick(UObject *Object, std::function< void(float)> Function, ETickType Type)
Definition: TickManager.cpp:55
FVector PreviousActorPosition
FTickEntry TickEntry
void TickParallel(float DeltaTime)

References ATickManager::AddTick(), BindTick(), PreviousActorPosition, TickEntry, and TickParallel().

◆ CreateROSTopic()

void ATransformSensor::CreateROSTopic ( )
overrideprotectedvirtual

Creates ROS Topic for this sensor. Can be overriden by individual sensor if needed.

Reimplemented from ASensor.

Definition at line 54 of file TransformSensor.cpp.

55{
56 Super::CreateROSTopic();
57
58 GeoReferencingSystem = AGeoReferencingSystem::GetGeoReferencingSystem(GetWorld());
59
60 // If map has been georeferenced, create gnss topic.
62 {
63 FString TopicName = FString::Printf(TEXT("/agrarsense/out/sensors/%s"), *GetSensorIdentifier());
64 int32 LastSlashIndex = TopicName.Find(TEXT("/"), ESearchCase::IgnoreCase, ESearchDir::FromEnd);
65 if (LastSlashIndex != INDEX_NONE)
66 {
67 TopicName = TopicName.Left(LastSlashIndex) + TEXT("/gnss");
68 }
69 else
70 {
71 TopicName += TEXT("/gnss");
72 }
73
74 GnssTopic = NewObject<UTopic>(UTopic::StaticClass());
75 if (GnssTopic)
76 {
77 GnssTopic->Init(ROSInstance->ROSIntegrationCore, TopicName, TEXT("std_msgs/String"));
78 GnssTopic->Advertise();
79 }
80 }
81}
UROSIntegrationGameInstance * ROSInstance
Definition: Sensor.h:352
FString GetSensorIdentifier() const
Definition: Sensor.h:74
AGeoReferencingSystem * GeoReferencingSystem

References GeoReferencingSystem, ASensor::GetSensorIdentifier(), GnssTopic, and ASensor::ROSInstance.

Referenced by Init().

◆ DestroyROSTopic()

void ATransformSensor::DestroyROSTopic ( )
overrideprotectedvirtual

Destroy created ROS Topic. Can be overriden by individual sensor if needed.

Reimplemented from ASensor.

Definition at line 83 of file TransformSensor.cpp.

84{
85 Super::DestroyROSTopic();
86
87 if (GnssTopic)
88 {
89 GnssTopic->Unadvertise();
90 GnssTopic->Unsubscribe();
91 GnssTopic->MarkAsDisconnected();
92 GnssTopic->ConditionalBeginDestroy();
93 GnssTopic = nullptr;
94 }
95}

References GnssTopic.

◆ EndPlay()

void ATransformSensor::EndPlay ( const EEndPlayReason::Type  EndPlayReason)
overrideprotectedvirtual

Reimplemented from ASensor.

Definition at line 43 of file TransformSensor.cpp.

44{
45 Super::EndPlay(EndPlayReason);
46
48
49 GnssMessageString.Reset();
50 TransformMessage.Reset();
51 Target.Reset();
52}
static void RemoveTick(FTickEntry TickEntry)
Definition: TickManager.cpp:90
TSharedPtr< ROSMessages::std_msgs::String > GnssMessageString
TSharedPtr< ROSMessages::geometry_msgs::Transform > TransformMessage
TWeakObjectPtr< AActor > Target

References GnssMessageString, ATickManager::RemoveTick(), Target, TickEntry, and TransformMessage.

◆ GetSensorType()

virtual ESensorTypes ATransformSensor::GetSensorType ( ) const
inlineoverridevirtual

Get type of the sensor

Returns
Sensor's type

Reimplemented from ASensor.

Definition at line 38 of file TransformSensor.h.

39 {
41 }

References Transform.

◆ Init()

void ATransformSensor::Init ( AActor *  TransformActor,
bool  SimulateSensor = true 
)

Definition at line 20 of file TransformSensor.cpp.

21{
22 SetSimulateSensor(SimulateSensor);
23 Target = TransformActor;
24
25 ROSMessages::geometry_msgs::Transform transformMsg;
26 TransformMessage = MakeShared<ROSMessages::geometry_msgs::Transform>(transformMsg);
27
28 GnssMessageString = MakeShared<ROSMessages::std_msgs::String>();
29
31}
void SetSimulateSensor(bool SimulateSensor)
Definition: Sensor.h:151
virtual void CreateROSTopic() override

References CreateROSTopic(), GnssMessageString, ASensor::SetSimulateSensor(), Target, and TransformMessage.

Referenced by USensorFactory::SpawnTransformSensor().

◆ SendGnssDataToROS()

void ATransformSensor::SendGnssDataToROS ( const FVector &  ActorPosition)
private

Definition at line 135 of file TransformSensor.cpp.

136{
138 {
139 // Convert Actor position to FGeographicCoordinates
140 FGeographicCoordinates GeographicCoordinates = UCoordinateConversionUtilities::UnrealToGeographicCoordinates(GeoReferencingSystem, ActorPosition);
141
142 double Latitude = GeographicCoordinates.Latitude;
143 double Longitude = GeographicCoordinates.Longitude;
144 double Altitude = GeographicCoordinates.Altitude;
145
146 // Create gnss string and send the message to the created ROS topic
147 FString GnssDataString = FString::Printf(TEXT("%f, %f, %f"), Latitude, Longitude, Altitude);
148 GnssMessageString->_Data = GnssDataString;
150 }
151}
static FGeographicCoordinates UnrealToGeographicCoordinates(AGeoReferencingSystem *GeoReferencingSystem, const FVector &Position)

References GeoReferencingSystem, GnssMessageString, GnssTopic, and UCoordinateConversionUtilities::UnrealToGeographicCoordinates().

Referenced by TickParallel().

◆ SendTransformDataToROS()

void ATransformSensor::SendTransformDataToROS ( const FVector &  Translation,
const FQuat &  Rotation 
)
private

Definition at line 124 of file TransformSensor.cpp.

125{
126 UTopic* Topic = GetROSTopic();
127 if (Topic && TransformMessage.IsValid())
128 {
129 TransformMessage->translation = Translation;
130 TransformMessage->rotation = Rotation;
131 Topic->Publish(TransformMessage);
132 }
133}
UTopic * GetROSTopic() const
Definition: Sensor.h:141

References ASensor::GetROSTopic(), and TransformMessage.

Referenced by TickParallel().

◆ TickParallel()

void ATransformSensor::TickParallel ( float  DeltaTime)
private

Definition at line 97 of file TransformSensor.cpp.

98{
99 AActor* TargetActor = Target.Get();
100 if (!CanSimulateSensor() || !TargetActor)
101 {
102 return;
103 }
104
105 FTransform CurrentTransform = TargetActor->GetTransform();
106 FVector CurrentPos = CurrentTransform.GetLocation();
107 FQuat CurrentRot = CurrentTransform.GetRotation();
108
109 if (CurrentPos != PreviousActorPosition)
110 {
111 // Send transform data to ROS topic,
112 // this is done frequently but this should be quite fast and
113 // should give precise actor's current location and rotation in the World.
114 PreviousActorPosition = CurrentPos;
115
116 if (IsROSConnected())
117 {
118 SendTransformDataToROS(CurrentPos, CurrentRot);
119 SendGnssDataToROS(CurrentPos);
120 }
121 }
122}
bool CanSimulateSensor() const
Definition: Sensor.h:161
bool IsROSConnected() const
Definition: Sensor.h:192
void SendTransformDataToROS(const FVector &Translation, const FQuat &Rotation)
void SendGnssDataToROS(const FVector &ActorPosition)

References ASensor::CanSimulateSensor(), ASensor::IsROSConnected(), PreviousActorPosition, SendGnssDataToROS(), SendTransformDataToROS(), and Target.

Referenced by BeginPlay().

Member Data Documentation

◆ GeoReferencingSystem

AGeoReferencingSystem* ATransformSensor::GeoReferencingSystem = nullptr
private

Definition at line 77 of file TransformSensor.h.

Referenced by CreateROSTopic(), and SendGnssDataToROS().

◆ GnssMessageString

TSharedPtr<ROSMessages::std_msgs::String> ATransformSensor::GnssMessageString
private

Definition at line 63 of file TransformSensor.h.

Referenced by EndPlay(), Init(), and SendGnssDataToROS().

◆ GnssTopic

UTopic* ATransformSensor::GnssTopic = nullptr
private

Definition at line 74 of file TransformSensor.h.

Referenced by CreateROSTopic(), DestroyROSTopic(), and SendGnssDataToROS().

◆ PreviousActorPosition

FVector ATransformSensor::PreviousActorPosition = FVector(0.0f, 0.0f, 0.0f)
private

Definition at line 68 of file TransformSensor.h.

Referenced by BeginPlay(), and TickParallel().

◆ Target

TWeakObjectPtr<AActor> ATransformSensor::Target
private

Definition at line 65 of file TransformSensor.h.

Referenced by EndPlay(), Init(), and TickParallel().

◆ TickEntry

FTickEntry ATransformSensor::TickEntry
private

Definition at line 71 of file TransformSensor.h.

Referenced by BeginPlay(), and EndPlay().

◆ TransformMessage

TSharedPtr<ROSMessages::geometry_msgs::Transform> ATransformSensor::TransformMessage
private

Definition at line 61 of file TransformSensor.h.

Referenced by EndPlay(), Init(), and SendTransformDataToROS().


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