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;
20
21UCLASS()
22class AGRARSENSE_API APIDDrone : public AVehicle
23{
24 GENERATED_BODY()
25
26public:
27
28 APIDDrone();
29
34 virtual EVehicleTypes GetVehicleType() const override { return EVehicleTypes::Drone; }
35
36 virtual FTransform GetInteractableWorldTransform_Implementation() const override
37 {
38 if (DroneSkeletalMesh)
39 {
40 return DroneSkeletalMesh->GetComponentTransform();
41 }
42 else
43 {
44 return GetActorTransform();
45 }
46 }
47
51 UFUNCTION(BlueprintCallable, Category = "Drone")
52 void SetDroneAction(EDroneAction mode)
53 {
54 DroneParameters.DroneAction = mode;
55 }
56
61 UFUNCTION(BlueprintCallable, Category = "Drone")
63 {
64 return DroneParameters.DroneAction;
65 }
66
67 UFUNCTION(BlueprintCallable, Category = "Drone")
68 FDroneParameters GetDroneParameters() const
69 {
70 return DroneParameters;
71 }
72
73 UFUNCTION(BlueprintCallable, Category = "Drone")
74 void ChangeDroneParameters(const FDroneParameters& newParameters)
75 {
76#if WITH_EDITOR
77 UE_LOG(LogTemp, Warning, TEXT("Parameters Changed!"));
78#endif
79 SetDroneParameters(newParameters);
80 }
81
82 void SetDroneParameters(const FDroneParameters& InParameters)
83 {
84 DroneParameters = InParameters;
85 }
86
87 UFUNCTION(BlueprintCallable, Category = "Drone")
88 void AddWayPoint(FVector Waypoint)
89 {
90 FTransform transform;
91 transform.SetLocation(Waypoint);
92
93 DroneParameters.Points.Add(transform);
94 }
95
96 UFUNCTION(BlueprintCallable, Category = "Drone")
97 void ClearWaypoints()
98 {
99 DroneParameters.Points.Empty();
100 }
101
105 UFUNCTION(BlueprintCallable, Category = "Drone")
106 void SetFlightpath();
107
108 UFUNCTION(BlueprintCallable, Category = "Drone")
109 void SetManualControl(bool value)
110 {
111 manualControl = value;
112 }
113
118 void MoveDroneToPosition(const FTransform Transform);
119
125 UFUNCTION(BlueprintCallable, Category = "Drone")
126 TArray<FTransform> GenerateRoamingPoints(float radius, int32 roamingPoints);
127
128 virtual void SetCurrentTransformToMovedFromGarageTransform() override
129 {
130 if (DroneSkeletalMesh)
131 {
132 MovedFromGarageTransform = DroneSkeletalMesh->GetComponentTransform();
133 UE_LOG(LogTemp, Warning, TEXT("Moved from garage transform %s"), *MovedFromGarageTransform.ToString());
134
135 }
136 else
137 {
138 MovedFromGarageTransform = GetActorTransform();
139 UE_LOG(LogTemp, Warning, TEXT("ACTOR Moved from garage transform WHAT IS THIS: %s"), *MovedFromGarageTransform.ToString());
140
141 }
142 }
143
144 void SetShowForwardArrow(bool Show)
145 {
146 ShowForwardArrow = Show;
147 }
148
149 UFUNCTION(BlueprintCallable, Category = "Drone")
150 bool GetShowForwardArrow() const
151 {
152 return ShowForwardArrow;
153 }
154
155protected:
156
157 void ROSBridgeStateChanged(EROSState ROSState) override;
158
159 UPROPERTY()
160 UTopic* ROSTopic = nullptr;
161
162 TSharedPtr<ROSMessages::std_msgs::Float32> ROSMessage;
163
164private:
165
166 virtual void BeginPlay() override;
167
168 virtual void Tick(float DeltaTime) override;
169
170 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
171
172 void CreateTopic();
173
174 void DestroyTopic();
175
176 void AutoPilot(const float DeltaTime);
177
178 UFUNCTION(BlueprintCallable)
179 void AssignRoamingPoints(const TArray<FTransform> Points);
180
181 UFUNCTION(BlueprintCallable)
182 void SetDroneRotation(USkeletalMeshComponent* target, FRotator rotator);
183
184 void UpdateGroundHeight();
185
186 UFUNCTION(BlueprintCallable)
187 bool IsRoaming() const
188 {
189 return DroneParameters.DroneAction == EDroneAction::Roaming;
190 }
191
192 UFUNCTION(BlueprintCallable)
193 void SetDesiredLocationMesh(UStaticMeshComponent* meshcomponent)
194 {
195 PositionMesh = meshcomponent;
196 }
197
198 UFUNCTION(BlueprintCallable)
199 FVector GetCurrentWaypointTarget();
200
202
203 UPROPERTY()
205
206 UPROPERTY()
207 UWorld* World = nullptr;
208
209 FVector StartingPosition;
210
211 UPROPERTY()
212 USkeletalMeshComponent* DroneSkeletalMesh = nullptr;
213
214 UPROPERTY()
215 UStaticMeshComponent* PositionMesh = nullptr;
216
217 /* For testing purposes. Hardcoded coordinates */
218 TArray<FVector> WayPoints = {
219 {3790, 8050, 5000},
220 {11779.536955,6566.541435,5000}
221 };
222
223 bool waypointReached = false;
224
225 bool manualControl = false;
226
227 bool ShowForwardArrow = false;
228
229 int32 passedWaypoints = 0;
230
231 float DroneHeightFromGround = 0.0f;
232};
EDroneAction
Definition: DroneAction.h:15
EFlightMode
EROSState
Definition: ROSState.h:16
static EDroneAction GetDroneAction(FString actionstring)
EVehicleTypes
Definition: VehicleTypes.h:15
virtual FTransform GetInteractableWorldTransform_Implementation() const override
Definition: PIDDrone.h:36
void SetDroneParameters(const FDroneParameters &InParameters)
Definition: PIDDrone.h:82
FDroneParameters DroneParameters
Definition: PIDDrone.h:201
virtual EVehicleTypes GetVehicleType() const override
Definition: PIDDrone.h:34
void SetShowForwardArrow(bool Show)
Definition: PIDDrone.h:144
TArray< FTransform > Points