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

#include <PIDDrone.h>

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

Public Member Functions

 APIDDrone ()
 
virtual EVehicleTypes GetVehicleType () const override
 
virtual FTransform GetInteractableWorldTransform_Implementation () const override
 
void SetDroneAction (EDroneAction mode)
 Set drone's action (roaming, manual) More...
 
EDroneAction GetDroneAction ()
 Get drone's action (roaming, manual) More...
 
FDroneParameters GetDroneParameters ()
 
void ChangeDroneParameters (const FDroneParameters &newParameters)
 
void SetDroneParameters (const FDroneParameters &InParameters)
 
void AddWayPoint (FVector waypoint)
 
void ClearWaypoints ()
 
void FlySetFlightpath ()
 Called in tick function for drone roaming through points. More...
 
void SetManualControl (bool value)
 
void MoveDroneToPosition (const FTransform Transform)
 Override all drone roaming points and continue towards this position. More...
 
TArray< FTransform > GenerateRoamingPoints (float radius, int roamingPoints)
 Generates a roadming points array for the drone in radius. More...
 
- Public Member Functions inherited from AVehicle
 AVehicle ()
 
virtual EVehicleTypes GetVehicleType () const
 
virtual FString GetTopicName ()
 
void TogglePhysics (bool isOn)
 
void TeleportVehicleTo (FVector NewLocation, FRotator NewRotation)
 
virtual void TeleportVehicleTo_Implementation (FVector NewLocation, FRotator NewRotation)
 
void ResetCamera ()
 
void ToggleLights (bool isOn)
 
void SetVisualizeVehicleOverlapArea (bool Visible)
 
FString ExportToJsonFile (const FString &FileName)
 
void SetVehicleIsInGarage (bool VehicleIsInGarage)
 
bool IsVehicleInGarage () const
 
USensorsManagerComponent * GetSensorsManager () const
 
ACollisionSensorGetCollisionSensor () const
 
ATransformSensorGetTransformSensor () const
 
AOverlapSensorGetOverlapSensor () const
 
FTransform GetMovedFromGarageTransform () const
 
void SetCurrentTransformToMovedFromGarageTransform ()
 
virtual FText GetInteractableName_Implementation () const override
 
virtual FTransform GetInteractableWorldTransform_Implementation () const override
 
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
 
virtual void PossessedBy (AController *NewController) override
 
- Public Member Functions inherited from IInteractable
FText GetInteractableName () const
 
FTransform GetInteractableWorldTransform () const
 
- 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)
 

Public Attributes

bool manualControl = false
 
- Public Attributes inherited from AVehicle
FText InteractableName
 
FVehicleGarageChangedDelegate OnVehicleGarageChanged
 

Protected Member Functions

FVector GetCurrentWaypointTarget ()
 
void SetDesiredLocationMesh (UStaticMeshComponent *meshcomponent)
 
void AssignRoamingPoints (TArray< FTransform > Points)
 
void SetDroneRotation (USkeletalMeshComponent *target, FRotator rotator)
 
virtual void BeginPlay () override
 
virtual void EndPlay (const EEndPlayReason::Type EndPlayReason) override
 

Protected Attributes

FDroneParameters DroneParameters
 
- Protected Attributes inherited from AVehicle
TObjectPtr< USensorsManagerComponent > SensorsManager
 
ACollisionSensorCollisionSensor = nullptr
 
ATransformSensorTransformSensor = nullptr
 
AOverlapSensorOverlapSensor = nullptr
 
UNiagaraComponent * NiagaraComponent = nullptr
 
FString ActorName
 
FString ActorID
 
FTransform OriginalCameraTransform
 

Private Member Functions

virtual void BeginPlay () override
 
virtual void Tick (float DeltaTime) override
 
virtual void EndPlay (const EEndPlayReason::Type EndPlayReason) override
 
void AutoPilot (float DeltaTime)
 
bool isRoaming ()
 

Private Attributes

EFlightMode flightmode = EFlightMode::Roaming
 
UWorld * World = nullptr
 
FVector StartingPosition
 
USkeletalMeshComponent * mesh = nullptr
 
UStaticMeshComponent * desiredPositionMesh = nullptr
 
bool bRosConnected = false
 
bool waypointreached = false
 
TArray< FVector > WayPoints
 
int passedWaypoints = 0
 

Additional Inherited Members

- 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 ()
 

Detailed Description

Definition at line 17 of file PIDDrone.h.

Constructor & Destructor Documentation

◆ APIDDrone()

APIDDrone::APIDDrone ( )

Definition at line 18 of file PIDDrone.cpp.

18 : Super()
19{
20 PrimaryActorTick.bCanEverTick = true;
21 InteractableName = NSLOCTEXT("Agrarsense", "DroneInteractableName", "Drone");
22}
FText InteractableName
Definition: Vehicle.h:241

References AVehicle::InteractableName.

Member Function Documentation

◆ AddWayPoint()

void APIDDrone::AddWayPoint ( FVector  waypoint)
inline

Definition at line 81 of file PIDDrone.h.

82 {
83 FTransform transform;
84 transform.SetLocation(waypoint);
85
86 DroneParameters.Points.Add(transform);
87 }
FDroneParameters DroneParameters
Definition: PIDDrone.h:161
TArray< FTransform > Points

References FDroneParameters::Points.

◆ AssignRoamingPoints()

void APIDDrone::AssignRoamingPoints ( TArray< FTransform >  Points)
inlineprotected

Definition at line 172 of file PIDDrone.h.

173 {
174 // Empty current waypoints if any
176
177 for (FTransform point : Points)
178 {
179 // Assing new waypoints to the array
180 WayPoints.Add(point.GetLocation());
181 }
182 };
TArray< FVector > WayPoints
Definition: PIDDrone.h:147
void ClearWaypoints()
Definition: PIDDrone.h:90

◆ AutoPilot()

void APIDDrone::AutoPilot ( float  DeltaTime)
private

Definition at line 198 of file PIDDrone.cpp.

199{
200 if (isRoaming())
201 {
203 }
204}
bool isRoaming()
Definition: PIDDrone.h:152
void FlySetFlightpath()
Called in tick function for drone roaming through points.
Definition: PIDDrone.cpp:51

References FlySetFlightpath(), and isRoaming().

Referenced by Tick().

◆ BeginPlay()

void APIDDrone::BeginPlay ( )
overrideprivatevirtual

Reimplemented from AVehicle.

Definition at line 24 of file PIDDrone.cpp.

25{
26 Super::BeginPlay();
27
28 World = GetWorld();
29
30 mesh = Cast<USkeletalMeshComponent>(GetComponentByClass(USkeletalMeshComponent::StaticClass()));
31 desiredPositionMesh = Cast<UStaticMeshComponent>(GetComponentByClass(UStaticMeshComponent::StaticClass()));
32
33 StartingPosition = GetActorLocation();
34}
UWorld * World
Definition: PIDDrone.h:136
FVector StartingPosition
Definition: PIDDrone.h:138
UStaticMeshComponent * desiredPositionMesh
Definition: PIDDrone.h:141
USkeletalMeshComponent * mesh
Definition: PIDDrone.h:140

References desiredPositionMesh, mesh, StartingPosition, and World.

◆ ChangeDroneParameters()

void APIDDrone::ChangeDroneParameters ( const FDroneParameters newParameters)
inline

Definition at line 69 of file PIDDrone.h.

70 {
71 UE_LOG(LogTemp, Warning, TEXT("Parameters Chnaged!"));
72 SetDroneParameters(newParameters);
73 }
void SetDroneParameters(const FDroneParameters &InParameters)
Definition: PIDDrone.h:75

Referenced by USimulatorJsonParser::ParseAndSetDroneParameters().

◆ ClearWaypoints()

void APIDDrone::ClearWaypoints ( )
inline

Definition at line 90 of file PIDDrone.h.

91 {
92 DroneParameters.Points.Empty();
93 }

◆ EndPlay()

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

Reimplemented from AVehicle.

Definition at line 36 of file PIDDrone.cpp.

37{
38 Super::EndPlay(EndPlayReason);
39}

◆ FlySetFlightpath()

void APIDDrone::FlySetFlightpath ( )

Called in tick function for drone roaming through points.

Definition at line 51 of file PIDDrone.cpp.

52{
53 // TODO: Check if in garage
54 // include drone rotation to point
55
56 // Don't try to fly if no waypoints
57 if (DroneParameters.Points.Num() == 0)
58 {
60 }
61
62 FVector waypoint = GetCurrentWaypointTarget();
63 FVector currentlocation = mesh->GetRelativeTransform().GetLocation();
64
66 {
67 // Doesn't work
68 desiredPositionMesh->SetWorldLocation(waypoint);
69 }
70#if WITH_EDITOR
71 else
72 {
73 UE_LOG(LogTemp, Warning, TEXT("Desired location mesh not found"));
74 //DrawDebugLine(World, currentlocation, waypoint, FColor::Red, false, 0.0f, 0.0f, 5.0f);
75 }
76#endif
77
78 if (FVector2f::Distance(FVector2f(waypoint.X, waypoint.Y), FVector2f(currentlocation.X, currentlocation.Y)) < 1000)
79 {
80 waypointreached = true;
81 if (passedWaypoints != DroneParameters.Points.Num() - 1)
82 {
83#if WITH_EDITOR
84 UE_LOG(LogTemp, Warning, TEXT("Changing waypoint from %i to %i"), passedWaypoints, passedWaypoints + 1);
85#endif
86
88 }
89 else
90 {
92 {
95 }
96 else
97 {
99 {
102 break;
105 passedWaypoints = 0;
106 break;
107 default:
108 break;
109 }
110 }
111 }
112 }
113}
FVector GetCurrentWaypointTarget()
Definition: PIDDrone.cpp:128
bool waypointreached
Definition: PIDDrone.h:144
TArray< FTransform > GenerateRoamingPoints(float radius, int roamingPoints)
Generates a roadming points array for the drone in radius.
Definition: PIDDrone.cpp:174
int passedWaypoints
Definition: PIDDrone.h:157
EDroneEndAction DroneEndAction
EDroneAction DroneAction

References desiredPositionMesh, FDroneParameters::DroneAction, FDroneParameters::DroneEndAction, DroneParameters, GenerateRandomNew, GenerateRoamingPoints(), GetCurrentWaypointTarget(), mesh, passedWaypoints, FDroneParameters::Points, Roaming, Stationary, Stop, and waypointreached.

Referenced by AutoPilot().

◆ GenerateRoamingPoints()

TArray< FTransform > APIDDrone::GenerateRoamingPoints ( float  radius,
int  roamingPoints 
)

Generates a roadming points array for the drone in radius.

Parameters
radiusradius of the roaming area
Returns
TArray<FVector>

Definition at line 174 of file PIDDrone.cpp.

175{
176 TArray<FTransform> generatedRoamingPoints;
177 generatedRoamingPoints.Reserve(roamingPoints);
178
179 FVector currentPosition = this->GetTransform().GetLocation();
180
181 FVector min = currentPosition - FVector(radius / 2, radius / 2, 0);
182 FVector max = currentPosition + FVector(radius / 2, radius / 2, 0);
183
184 for (int32 i = 0; i < roamingPoints; i++)
185 {
186 FTransform randomPoint;
187 randomPoint.SetLocation(FVector(FMath::RandRange(min.X, max.X), FMath::RandRange(min.Y, max.Y), 5000));
188
189 generatedRoamingPoints.Add(randomPoint);
190#if WITH_EDITOR
191 UE_LOG(LogTemp, Warning, TEXT("Waypoint %i: (%s)"), i, *randomPoint.GetLocation().ToString());
192#endif
193 }
194
195 return generatedRoamingPoints;
196}

Referenced by FlySetFlightpath().

◆ GetCurrentWaypointTarget()

FVector APIDDrone::GetCurrentWaypointTarget ( )
protected

Definition at line 128 of file PIDDrone.cpp.

129{
130 // UE_LOG(LogTemp, Warning, TEXT(" % s"), *DroneParameters.Points[passedWaypoints].GetLocation().ToString());
132 {
133 return FVector(0, 0, 0);
134 }
135
136 return DroneParameters.Points[passedWaypoints].GetLocation();
137}

References DroneParameters, passedWaypoints, and FDroneParameters::Points.

Referenced by FlySetFlightpath().

◆ GetDroneAction()

EDroneAction APIDDrone::GetDroneAction ( )
inline

Get drone's action (roaming, manual)

Returns
Drone's action

Definition at line 57 of file PIDDrone.h.

58 {
60 }

◆ GetDroneParameters()

FDroneParameters APIDDrone::GetDroneParameters ( )
inline

Definition at line 63 of file PIDDrone.h.

64 {
65 return DroneParameters;
66 }

Referenced by USimulatorJsonExporter::SetDroneParameters().

◆ GetInteractableWorldTransform_Implementation()

virtual FTransform APIDDrone::GetInteractableWorldTransform_Implementation ( ) const
inlineoverridevirtual

Reimplemented from AVehicle.

Definition at line 31 of file PIDDrone.h.

32 {
33 if (mesh)
34 {
35 return mesh->GetComponentTransform();
36 }
37 else
38 {
39 return GetActorTransform();
40 }
41 }

◆ GetVehicleType()

virtual EVehicleTypes APIDDrone::GetVehicleType ( ) const
inlineoverridevirtual

Get type of the Vehicle

Returns
Vehicle's type

Reimplemented from AVehicle.

Definition at line 29 of file PIDDrone.h.

References Drone.

◆ isRoaming()

bool APIDDrone::isRoaming ( )
inlineprivate

Definition at line 152 of file PIDDrone.h.

References Roaming.

Referenced by AutoPilot().

◆ MoveDroneToPosition()

void APIDDrone::MoveDroneToPosition ( const FTransform  Transform)

Override all drone roaming points and continue towards this position.

Parameters
Transformdrone move position

Definition at line 115 of file PIDDrone.cpp.

116{
117 if (DroneParameters.Points.Num() > 0)
118 {
119 DroneParameters.Points.Empty();
121 }
122 else
123 {
125 }
126}

References DroneParameters, FDroneParameters::Points, and Transform.

◆ SetDesiredLocationMesh()

void APIDDrone::SetDesiredLocationMesh ( UStaticMeshComponent *  meshcomponent)
inlineprotected

Definition at line 167 of file PIDDrone.h.

167 {
168 desiredPositionMesh = meshcomponent;
169 }

◆ SetDroneAction()

void APIDDrone::SetDroneAction ( EDroneAction  mode)
inline

Set drone's action (roaming, manual)

Definition at line 47 of file PIDDrone.h.

48 {
50 }

◆ SetDroneParameters()

void APIDDrone::SetDroneParameters ( const FDroneParameters InParameters)
inline

Definition at line 75 of file PIDDrone.h.

76 {
77 DroneParameters = InParameters;
78 }

◆ SetDroneRotation()

void APIDDrone::SetDroneRotation ( USkeletalMeshComponent *  target,
FRotator  rotator 
)
protected

Definition at line 139 of file PIDDrone.cpp.

140{
141 if (!target)
142 {
143 return;
144 }
145
146 FRotator rotation = FRotator(
147 FMath::Clamp(rotator.Pitch, -2, 2),
148 FMath::Clamp(rotator.Yaw, -2, 2),
149 FMath::Clamp(rotator.Roll, -2, 2));
150
151 FRotator currentRotation = target->GetRelativeRotation();
152
153 FRotator rotationDifference = currentRotation - rotator;
154
155 if (FMath::Abs(rotator.Pitch) > 0 && FMath::Abs(currentRotation.Pitch) < FMath::Abs(rotationDifference.Pitch))
156 {
157 target->AddRelativeRotation(FRotator(rotation.Pitch, 0, 0), false, nullptr, ETeleportType::TeleportPhysics);
158 }
159 if (FMath::Abs(rotator.Yaw) > 0 && FMath::Abs(currentRotation.Yaw) < FMath::Abs(rotationDifference.Yaw))
160 {
161 target->AddRelativeRotation(FRotator(0, rotation.Yaw, 0), false, nullptr, ETeleportType::TeleportPhysics);
162 }
163 if (FMath::Abs(rotator.Roll) > 0 && FMath::Abs(currentRotation.Roll) < FMath::Abs(rotationDifference.Roll))
164 {
165 target->AddRelativeRotation(FRotator(0, 0, rotation.Roll), false, nullptr, ETeleportType::TeleportPhysics);
166 }
167
168 if (rotator.Pitch == 0 || rotator.Yaw == 0 || rotator.Roll == 0)
169 {
170 target->AddRelativeRotation(FRotator(-FMath::Clamp(rotationDifference.Pitch, -2, 2), -FMath::Clamp(rotationDifference.Yaw, -2, 2), -FMath::Clamp(rotationDifference.Roll, -2, 2)), false, nullptr, ETeleportType::TeleportPhysics);
171 }
172}

◆ SetManualControl()

void APIDDrone::SetManualControl ( bool  value)
inline

Definition at line 102 of file PIDDrone.h.

102 {
103 manualControl = value;
104 }
bool manualControl
Definition: PIDDrone.h:120

◆ Tick()

void APIDDrone::Tick ( float  DeltaTime)
overrideprivatevirtual

Definition at line 41 of file PIDDrone.cpp.

42{
43 Super::Tick(DeltaTime);
44
45 if (!IsVehicleInGarage())
46 {
47 AutoPilot(DeltaTime);
48 }
49}
void AutoPilot(float DeltaTime)
Definition: PIDDrone.cpp:198
bool IsVehicleInGarage() const
Definition: Vehicle.h:131

References AutoPilot(), and AVehicle::IsVehicleInGarage().

Member Data Documentation

◆ bRosConnected

bool APIDDrone::bRosConnected = false
private

Definition at line 143 of file PIDDrone.h.

◆ desiredPositionMesh

UStaticMeshComponent* APIDDrone::desiredPositionMesh = nullptr
private

Definition at line 141 of file PIDDrone.h.

Referenced by BeginPlay(), and FlySetFlightpath().

◆ DroneParameters

FDroneParameters APIDDrone::DroneParameters
protected

Definition at line 161 of file PIDDrone.h.

Referenced by FlySetFlightpath(), GetCurrentWaypointTarget(), and MoveDroneToPosition().

◆ flightmode

EFlightMode APIDDrone::flightmode = EFlightMode::Roaming
private

Definition at line 133 of file PIDDrone.h.

◆ manualControl

bool APIDDrone::manualControl = false

Definition at line 120 of file PIDDrone.h.

◆ mesh

USkeletalMeshComponent* APIDDrone::mesh = nullptr
private

Definition at line 140 of file PIDDrone.h.

Referenced by BeginPlay(), and FlySetFlightpath().

◆ passedWaypoints

int APIDDrone::passedWaypoints = 0
private

Definition at line 157 of file PIDDrone.h.

Referenced by FlySetFlightpath(), and GetCurrentWaypointTarget().

◆ StartingPosition

FVector APIDDrone::StartingPosition
private

Definition at line 138 of file PIDDrone.h.

Referenced by BeginPlay().

◆ waypointreached

bool APIDDrone::waypointreached = false
private

Definition at line 144 of file PIDDrone.h.

Referenced by FlySetFlightpath().

◆ WayPoints

TArray<FVector> APIDDrone::WayPoints
private
Initial value:
= {
{3790, 8050, 5000},
{11779.536955,6566.541435,5000}
}

Definition at line 147 of file PIDDrone.h.

◆ World

UWorld* APIDDrone::World = nullptr
private

Definition at line 136 of file PIDDrone.h.

Referenced by BeginPlay().


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