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 "ROSIntegration/Public/std_msgs/Float32.h"
16
17#include "PIDDrone.generated.h"
18
19class UTopic;
20class AOverlapSensor;
21
22DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDroneFinished, FTransform, _LastWaypoint);
23
24UCLASS()
25class AGRARSENSE_API APIDDrone : public AVehicle
26{
27 GENERATED_BODY()
28
29public:
30
31 APIDDrone();
32
37 virtual EVehicleTypes GetVehicleType() const override { return EVehicleTypes::Drone; }
38
39 virtual FTransform GetInteractableWorldTransform_Implementation() const override
40 {
41 if (DroneSkeletalMesh)
42 {
43 return DroneSkeletalMesh->GetComponentTransform();
44 }
45 else
46 {
47 return GetActorTransform();
48 }
49 }
50
54 UFUNCTION(BlueprintCallable, Category = "Drone")
55 void SetDroneAction(EDroneAction mode)
56 {
57 DroneParameters.DroneAction = mode;
58 }
59
60 UPROPERTY(BlueprintAssignable, Category = "Drone")
61 FDroneFinished OnDroneFinished;
62
67 UFUNCTION(BlueprintCallable, Category = "Drone")
69 {
70 return DroneParameters.DroneAction;
71 }
72
73 UFUNCTION(BlueprintCallable, Category = "Drone")
74 FDroneParameters GetDroneParameters() const
75 {
76 return DroneParameters;
77 }
78
79 UFUNCTION(BlueprintCallable, Category = "Drone")
80 void ChangeDroneParameters(const FDroneParameters& newParameters);
81
82 UFUNCTION(BlueprintCallable, Category = "Drone")
83 void AddWayPoint(FVector Waypoint)
84 {
85 FTransform transform;
86 transform.SetLocation(Waypoint);
87
88 DroneParameters.Points.Add(transform);
89 }
90
91 UFUNCTION(BlueprintCallable, Category = "Drone")
92 float GetDroneDistanceToNextPoint()
93 {
94 // On manual flight, return a fixed distance to not affect autofly max speed
95 if (DroneParameters.DroneAction == EDroneAction::Manual)
96 {
97 return 5000;
98 }
99
100 return distanceToNextPoint;
101 }
102
103 UFUNCTION(BlueprintCallable, Category = "Drone")
104 float GetDroneSpeed() const
105 {
106 if (!DroneSkeletalMesh)
107 {
108 return 0;
109 }
110
111 return FMath::Max(FMath::Abs(DroneSkeletalMesh->GetComponentVelocity().X), FMath::Abs(DroneSkeletalMesh->GetComponentVelocity().Y));
112 }
113
114 UFUNCTION(BlueprintCallable, Category = "Drone")
115 void ClearWaypoints()
116 {
117 DroneParameters.Points.Empty();
118 }
119
123 UFUNCTION(BlueprintCallable, Category = "Drone")
124 void SetFlightpath();
125
126 UFUNCTION(BlueprintCallable, Category = "Drone")
127 void SetManualControl(bool value)
128 {
129 manualControl = value;
130 }
131
136 void MoveDroneToPosition(const FTransform Transform);
137
143 UFUNCTION(BlueprintCallable, Category = "Drone")
144 TArray<FTransform> GenerateRoamingPoints(float radius, int32 roamingPoints);
145
150 UFUNCTION(BlueprintCallable, Category = "Drone")
151 float GetYawRotationDifference(USkeletalMeshComponent* DroneSkeletalMesh, UStaticMeshComponent* DesiredLocation);
152
153 virtual void SetCurrentTransformToMovedFromGarageTransform() override
154 {
155 if (DroneSkeletalMesh)
156 {
157 MovedFromGarageTransform = DroneSkeletalMesh->GetComponentTransform();
158 UE_LOG(LogTemp, Warning, TEXT("Moved from garage transform %s"), *MovedFromGarageTransform.ToString());
159
160 }
161 else
162 {
163 MovedFromGarageTransform = GetActorTransform();
164 UE_LOG(LogTemp, Warning, TEXT("ACTOR Moved from garage transform WHAT IS THIS: %s"), *MovedFromGarageTransform.ToString());
165
166 }
167 }
168
169 void SetShowForwardArrow(bool Show)
170 {
171 ShowForwardArrow = Show;
172 }
173
174 void SetDrawDebugPoints(bool show)
175 {
176 drawDebugPoints = show;
177 }
178
179 UFUNCTION(BlueprintCallable, Category = "Drone")
180 bool GetShowForwardArrow() const
181 {
182 return ShowForwardArrow;
183 }
184
185 UFUNCTION(BlueprintCallable, Category = "Drone")
186 USkeletalMeshComponent* GetDroneSkeletalMesh() const
187 {
188 return DroneSkeletalMesh;
189 }
190
191protected:
192
193 void ROSBridgeStateChanged(EROSState ROSState) override;
194
195 UPROPERTY()
196 UTopic* ROSTopic = nullptr;
197
198 TSharedPtr<ROSMessages::std_msgs::Float32> ROSMessage;
199
200private:
201
202 virtual void BeginPlay() override;
203
204 virtual void Tick(float DeltaTime) override;
205
206 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
207
208 void CreateTopic();
209
210 void DestroyTopic();
211
212 void HandleDestroy();
213
214 void AutoPilot(const float DeltaTime);
215
216 void DrawDebugPoints();
217
218 UFUNCTION(BlueprintCallable)
219 void AssignRoamingPoints(const TArray<FTransform> Points);
220
221 UFUNCTION(BlueprintCallable)
222 void SetDroneRotation(USkeletalMeshComponent* target, FRotator rotator);
223
224 void UpdateGroundHeight();
225
226 UFUNCTION(BlueprintCallable)
227 bool IsRoaming() const
228 {
229 return DroneParameters.DroneAction == EDroneAction::Roaming;
230 }
231
232 UFUNCTION(BlueprintCallable)
233 void SetDesiredLocationMesh(UStaticMeshComponent* meshcomponent)
234 {
235 PositionMesh = meshcomponent;
236 }
237
238 UFUNCTION(BlueprintCallable)
239 FVector GetCurrentWaypointTarget();
240
241 UFUNCTION(BlueprintCallable)
242 FTransform GetCurrentWaypointTarget_Transform();
243
245
246 UPROPERTY()
248
249 UPROPERTY()
250 UWorld* World = nullptr;
251
252 FVector StartingPosition;
253
254 UPROPERTY()
255 USkeletalMeshComponent* DroneSkeletalMesh = nullptr;
256
257 UPROPERTY()
258 UStaticMeshComponent* PositionMesh = nullptr;
259
260 UPROPERTY()
261 AOverlapSensor* InnerOverlapSensor = nullptr;
262
263 /* For testing purposes. Hardcoded coordinates */
264 TArray<FVector> WayPoints = {
265 {3790, 8050, 5000},
266 {11779.536955,6566.541435,5000}
267 };
268
269 bool waypointReached = false;
270
271 bool manualControl = false;
272
273 bool ShowForwardArrow = false;
274
275 bool drawDebugPoints = false;
276
277 int32 passedWaypoints = 0;
278
279 float DroneHeightFromGround = 0.0f;
280
281 float distanceToNextPoint = 0.0f;
282
283 TArray<FTransform> dronePath;
284
285 FTimerHandle DestroyHandle;
286};
EDroneAction
Definition: DroneAction.h:15
EFlightMode
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDroneFinished, FTransform, _LastWaypoint)
EROSState
Definition: ROSState.h:16
static EDroneAction GetDroneAction(FString actionstring)
EVehicleTypes
Definition: VehicleTypes.h:15
TSharedPtr< ROSMessages::std_msgs::String > ROSMessage
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
virtual void BeginPlay() override
virtual FTransform GetInteractableWorldTransform_Implementation() const override
Definition: PIDDrone.h:39
void SetDrawDebugPoints(bool show)
Definition: PIDDrone.h:174
FDroneParameters DroneParameters
Definition: PIDDrone.h:244
virtual EVehicleTypes GetVehicleType() const override
Definition: PIDDrone.h:37
TArray< FTransform > dronePath
Definition: PIDDrone.h:283
FTimerHandle DestroyHandle
Definition: PIDDrone.h:285
void SetShowForwardArrow(bool Show)
Definition: PIDDrone.h:169
void ROSBridgeStateChanged(EROSState ROSState)
Definition: Sensor.cpp:162
UTopic * ROSTopic
Definition: Sensor.h:361
TArray< FTransform > Points