Agrarsense
PIDDrone.cpp
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
7#include "Components/StaticMeshComponent.h"
8
11#include "Field/FieldSystemNodes.h"
12
13#include <Camera/CameraComponent.h>
14
16{
17 PrimaryActorTick.bCanEverTick = true;
18 InteractableName = NSLOCTEXT("Agrarsense", "DroneInteractableName", "Drone");
19}
20
22{
23 Super::BeginPlay();
24
25 World = GetWorld();
26
27 mesh = Cast<USkeletalMeshComponent>(GetComponentByClass(USkeletalMeshComponent::StaticClass()));
28 desiredPositionMesh = Cast<UStaticMeshComponent>(GetComponentByClass(UStaticMeshComponent::StaticClass()));
29
30 StartingPosition = GetActorLocation();
31}
32
33void APIDDrone::EndPlay(const EEndPlayReason::Type EndPlayReason)
34{
35 Super::EndPlay(EndPlayReason);
36}
37
38void APIDDrone::Tick(float DeltaTime)
39{
40 Super::Tick(DeltaTime);
41
42 if (!IsVehicleInGarage())
43 {
44 AutoPilot(DeltaTime);
45 }
46}
47
49{
50 // TODO: Check if in garage
51 // include drone rotation to point
52
53 // Don't try to fly if no waypoints
54 if (DroneParameters.Points.Num() == 0)
56
57 FVector waypoint = GetCurrentWaypointTarget();
58 FVector currentlocation = mesh->GetRelativeTransform().GetLocation();
59
60
61 if (desiredPositionMesh != nullptr)
62 {
63 // Doesn't work
64 desiredPositionMesh->SetWorldLocation(waypoint);
65 }
66 else
67 {
68 UE_LOG(LogTemp, Warning, TEXT("Desired location mesh not found"));
69 }
70
71 DrawDebugLine(World, currentlocation, waypoint, FColor::Red, false, 0.0f, 0.0f, 5.0f);
72
73 if (FVector2f::Distance(FVector2f(waypoint.X, waypoint.Y), FVector2f(currentlocation.X, currentlocation.Y)) < 1000)
74 {
75 waypointreached = true;
76 if (passedWaypoints != DroneParameters.Points.Num() - 1)
77 {
78#if WITH_EDITOR
79 UE_LOG(LogTemp, Warning, TEXT("Changing waypoint from %i to %i"), passedWaypoints, passedWaypoints + 1);
80#endif
81
83 }
84 else
85 {
87 {
90 }
91 else
92 {
94 {
97 break;
100 passedWaypoints = 0;
101 break;
102 default:
103 break;
104 }
105 }
106 }
107 }
108}
109
110void APIDDrone::MoveDroneToPosition(const FTransform Transform)
111{
112 if (DroneParameters.Points.Num() > 0)
113 {
114 DroneParameters.Points.Empty();
116 }
117 else
118 {
120 }
121}
122
124{
125 /*
126#if WITH_EDITOR
127 UE_LOG(LogTemp, Warning, TEXT(" % s"), *DroneParameters.Points[passedWaypoints].GetLocation().ToString());
128#endif
129 */
131 return FVector(0, 0, 0);
132
133 return DroneParameters.Points[passedWaypoints].GetLocation();
134}
135
136void APIDDrone::SetDroneRotation(USkeletalMeshComponent* target, FRotator rotator)
137{
138 if (!target)
139 return;
140
141 FRotator rotation = FRotator(
142 FMath::Clamp(rotator.Pitch, -2, 2),
143 FMath::Clamp(rotator.Yaw, -2, 2),
144 FMath::Clamp(rotator.Roll, -2, 2));
145
146 FRotator currentRotation = target->GetRelativeRotation();
147
148 FRotator rotationDifference = currentRotation - rotator;
149
150 if (FMath::Abs(rotator.Pitch) > 0 && FMath::Abs(currentRotation.Pitch) < FMath::Abs(rotationDifference.Pitch))
151 {
152 target->AddRelativeRotation(FRotator(rotation.Pitch, 0, 0), false, nullptr, ETeleportType::TeleportPhysics);
153 }
154 if (FMath::Abs(rotator.Yaw) > 0 && FMath::Abs(currentRotation.Yaw) < FMath::Abs(rotationDifference.Yaw))
155 {
156 target->AddRelativeRotation(FRotator(0, rotation.Yaw, 0), false, nullptr, ETeleportType::TeleportPhysics);
157 }
158 if (FMath::Abs(rotator.Roll) > 0 && FMath::Abs(currentRotation.Roll) < FMath::Abs(rotationDifference.Roll))
159 {
160 target->AddRelativeRotation(FRotator(0, 0, rotation.Roll), false, nullptr, ETeleportType::TeleportPhysics);
161 }
162
163
164 if (rotator.Pitch == 0 || rotator.Yaw == 0 || rotator.Roll == 0)
165 {
166 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);
167 }
168}
169
170TArray<FTransform> APIDDrone::GenerateRoamingPoints(float radius, int roamingPoints)
171{
172 TArray<FTransform> generatedRoamingPoints;
173 generatedRoamingPoints.Reserve(roamingPoints);
174
175 FVector currentPosition = this->GetTransform().GetLocation();
176
177 FVector min = currentPosition - FVector(radius / 2, radius / 2, 0);
178 FVector max = currentPosition + FVector(radius / 2, radius / 2, 0);
179
180 for (int32 i = 0; i < roamingPoints; i++)
181 {
182 FTransform randomPoint;
183
184 randomPoint.SetLocation(FVector(FMath::RandRange(min.X, max.X), FMath::RandRange(min.Y, max.Y), 5000));
185
186 generatedRoamingPoints.Add(randomPoint);
187#if WITH_EDITOR
188 UE_LOG(LogTemp, Warning, TEXT("Waypoint %i: (%s)"), i, *randomPoint.GetLocation().ToString());
189#endif
190 }
191
192 return generatedRoamingPoints;
193}
194
195void APIDDrone::AutoPilot(float DeltaTime)
196{
197 if (isRoaming())
198 {
200 }
201}
202
void SetDroneRotation(USkeletalMeshComponent *target, FRotator rotator)
Definition: PIDDrone.cpp:136
FVector GetCurrentWaypointTarget()
Definition: PIDDrone.cpp:123
void MoveDroneToPosition(const FTransform Transform)
Override all drone roaming points and continue towards this position.
Definition: PIDDrone.cpp:110
UWorld * World
Definition: PIDDrone.h:141
FVector StartingPosition
Definition: PIDDrone.h:143
virtual void BeginPlay() override
Definition: PIDDrone.cpp:21
UStaticMeshComponent * desiredPositionMesh
Definition: PIDDrone.h:146
bool isRoaming()
Definition: PIDDrone.h:157
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
Definition: PIDDrone.cpp:33
void AutoPilot(float DeltaTime)
Definition: PIDDrone.cpp:195
FDroneParameters DroneParameters
Definition: PIDDrone.h:166
bool waypointreached
Definition: PIDDrone.h:149
TArray< FTransform > GenerateRoamingPoints(float radius, int roamingPoints)
Generates a roadming points array for the drone in radius.
Definition: PIDDrone.cpp:170
USkeletalMeshComponent * mesh
Definition: PIDDrone.h:145
void FlySetFlightpath()
Called in tick function for drone roaming through points.
Definition: PIDDrone.cpp:48
int passedWaypoints
Definition: PIDDrone.h:162
virtual void Tick(float DeltaTime) override
Definition: PIDDrone.cpp:38
FText InteractableName
Definition: Vehicle.h:241
bool IsVehicleInGarage() const
Definition: Vehicle.h:131
EDroneEndAction DroneEndAction
EDroneAction DroneAction
TArray< FTransform > Points