Agrarsense
PIDDrone.h
Go to the documentation of this file.
1// Copyright (c) 2023 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#pragma once
7
8#include "CoreMinimal.h"
9#include "Vehicle.h"
10#include "DroneParameters.h"
11
12#include "Components/SkeletalMeshComponent.h"
13#include "Components/StaticMeshComponent.h"
14
15#include "PIDDrone.generated.h"
16
17UCLASS()
18class AGRARSENSE_API APIDDrone : public AVehicle
19{
20 GENERATED_BODY()
21
22public:
23
24 APIDDrone();
25
30 virtual EVehicleTypes GetVehicleType() const override { return EVehicleTypes::Drone; }
31
32 virtual FTransform GetInteractableWorldTransform_Implementation() const override
33 {
34 if (mesh)
35 {
36 return mesh->GetComponentTransform();
37 }
38 else
39 {
40 return GetActorTransform();
41 }
42 }
43
47 UFUNCTION(BlueprintCallable, Category = "Drone")
48 void SetDroneAction(EDroneAction mode)
49 {
50 DroneParameters.DroneAction = mode;
51 }
52
57 UFUNCTION(BlueprintCallable, Category = "Drone")
59 {
60 return DroneParameters.DroneAction;
61 }
62
63 UFUNCTION(BlueprintCallable, Category = "Drone")
64 FDroneParameters GetDroneParameters() const
65 {
66 return DroneParameters;
67 }
68
69 UFUNCTION(BlueprintCallable, Category = "Drone")
70 void ChangeDroneParameters(const FDroneParameters& newParameters)
71 {
72#if WITH_EDITOR
73 UE_LOG(LogTemp, Warning, TEXT("Parameters Changed!"));
74#endif
75 SetDroneParameters(newParameters);
76 }
77
78 void SetDroneParameters(const FDroneParameters& InParameters)
79 {
80 DroneParameters = InParameters;
81 }
82
83 UFUNCTION(BlueprintCallable, Category = "Drone")
84 void AddWayPoint(FVector Waypoint)
85 {
86 FTransform transform;
87 transform.SetLocation(Waypoint);
88
89 DroneParameters.Points.Add(transform);
90 }
91
92 UFUNCTION(BlueprintCallable, Category = "Drone")
93 void ClearWaypoints()
94 {
95 DroneParameters.Points.Empty();
96 }
97
101 UFUNCTION(BlueprintCallable, Category = "Drone")
102 void FlySetFlightpath();
103
104 UFUNCTION(BlueprintCallable, Category = "Drone")
105 void SetManualControl(bool value)
106 {
107 manualControl = value;
108 }
109
114 void MoveDroneToPosition(const FTransform Transform);
115
121 UFUNCTION(BlueprintCallable, Category = "Drone")
122 TArray<FTransform> GenerateRoamingPoints(float radius, int32 roamingPoints);
123
124 virtual void SetCurrentTransformToMovedFromGarageTransform() override
125 {
126 if (mesh)
127 {
128 MovedFromGarageTransform = mesh->GetComponentTransform();
129 UE_LOG(LogTemp, Warning, TEXT("Moved from garage transform %s"), *MovedFromGarageTransform.ToString());
130
131 }
132 else
133 {
134 MovedFromGarageTransform = GetActorTransform();
135 UE_LOG(LogTemp, Warning, TEXT("ACTOR Moved from garage transform WHAT IS THIS: %s"), *MovedFromGarageTransform.ToString());
136
137 }
138 }
139
140 void SetShowForwardArrow(bool Show)
141 {
142 ShowForwardArrow = Show;
143 }
144
145 UFUNCTION(BlueprintCallable, Category = "Drone")
146 bool GetShowForwardArrow() const
147 {
148 return ShowForwardArrow;
149 }
150
151private:
152
153 virtual void BeginPlay() override;
154
155 virtual void Tick(float DeltaTime) override;
156
157 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
158
159 void AutoPilot(float DeltaTime);
160
161 UFUNCTION(BlueprintCallable)
162 bool IsRoaming() const
163 {
164 return DroneParameters.DroneAction == EDroneAction::Roaming;
165 }
166
167 UFUNCTION(BlueprintCallable)
168 void SetDesiredLocationMesh(UStaticMeshComponent* meshcomponent)
169 {
170 desiredPositionMesh = meshcomponent;
171 }
172
173 UFUNCTION(BlueprintCallable)
174 void AssignRoamingPoints(const TArray<FTransform> Points);
175
176 UFUNCTION(BlueprintCallable)
177 void SetDroneRotation(USkeletalMeshComponent* target, FRotator rotator);
178
179 UFUNCTION(BlueprintCallable)
180 FVector GetCurrentWaypointTarget();
181
183
184 UPROPERTY()
185 EFlightMode flightmode = EFlightMode::Roaming;
186
187 UPROPERTY()
188 UWorld* World = nullptr;
189
190 FVector StartingPosition;
191
192 USkeletalMeshComponent* mesh = nullptr;
193 UStaticMeshComponent* desiredPositionMesh = nullptr;
194
195 bool waypointReached = false;
196 bool manualControl = false;
197
198 /* For testing purposes. Hardcoded coordinates */
199 TArray<FVector> WayPoints = {
200 {3790, 8050, 5000},
201 {11779.536955,6566.541435,5000}
202 };
203
204 int32 passedWaypoints = 0;
205
206 bool ShowForwardArrow = false;
207};
EDroneAction
Definition: DroneAction.h:15
EFlightMode
static EDroneAction GetDroneAction(FString actionstring)
EVehicleTypes
Definition: VehicleTypes.h:15
virtual FTransform GetInteractableWorldTransform_Implementation() const override
Definition: PIDDrone.h:32
void SetDroneParameters(const FDroneParameters &InParameters)
Definition: PIDDrone.h:78
FDroneParameters DroneParameters
Definition: PIDDrone.h:182
virtual EVehicleTypes GetVehicleType() const override
Definition: PIDDrone.h:30
void SetShowForwardArrow(bool Show)
Definition: PIDDrone.h:140
TArray< FTransform > Points