13#include "ROSIntegration/Classes/RI/Topic.h"
15#include "Camera/CameraComponent.h"
16#include "Field/FieldSystemNodes.h"
17#include "Algo/Reverse.h"
20#include "DrawDebugHelpers.h"
25 PrimaryActorTick.bCanEverTick =
true;
26 InteractableName = NSLOCTEXT(
"Agrarsense",
"DroneInteractableName",
"Drone");
36 ROSMessage = MakeShared<ROSMessages::std_msgs::Float32>();
40 DroneSkeletalMesh = Cast<USkeletalMeshComponent>(GetComponentByClass(USkeletalMeshComponent::StaticClass()));
42 PositionMesh = Cast<UStaticMeshComponent>(GetComponentByClass(UStaticMeshComponent::StaticClass()));
56 FString VehicleTransformSensorID =
ActorID +
"/transform";
63 Super::EndPlay(EndPlayReason);
71 Super::Tick(DeltaTime);
102 FVector currentlocation =
DroneSkeletalMesh->GetRelativeTransform().GetLocation();
112 UE_LOG(LogTemp, Warning, TEXT(
"Desired location mesh not found"));
117 if (FVector2f::Distance(FVector2f(waypoint.X, waypoint.Y), FVector2f(currentlocation.X, currentlocation.Y)) < 1000)
179 return FVector(0, 0, 0);
189 for (
const FTransform& point : Points)
202 FRotator currentRotation = target->GetRelativeRotation();
203 FRotator rotationDifference = rotator - currentRotation;
205 target->AddRelativeRotation(rotationDifference,
false,
nullptr, ETeleportType::TeleportPhysics);
208 if (FMath::Abs(rotator.Pitch) < 0.001f)
210 rotator.Pitch = 0.0f;
212 if (FMath::Abs(rotator.Yaw) < 0.001f)
216 if (FMath::Abs(rotator.Roll) < 0.001f)
222 target->SetRelativeRotation(rotator,
false,
nullptr, ETeleportType::TeleportPhysics);
233 FVector End = Start - FVector(0, 0, 10000.0f);
235 FCollisionQueryParams TraceParams = FCollisionQueryParams(FName(TEXT(
"UpdateGroundHeight")),
true,
this);
236 TraceParams.bReturnPhysicalMaterial =
false;
237 TraceParams.bTraceComplex =
false;
239 FHitResult HitResult;
241#ifdef ParallelLineTraceSingleByChannel_EXISTS
244 World->ParallelLineTraceSingleByChannel(
250 FCollisionResponseParams::DefaultResponseParam);
253 World->LineTraceSingleByChannel(
259 FCollisionResponseParams::DefaultResponseParam);
262 if (HitResult.IsValidBlockingHit())
264 float Height = (Start.Z - HitResult.ImpactPoint.Z) / 100.0f;
265 float HeightRounded = FMath::RoundToFloat(Height * 1000.0f) / 1000.0f;
282 TArray<FTransform> generatedRoamingPoints;
283 generatedRoamingPoints.Reserve(roamingPoints);
285 FVector currentPosition = this->GetTransform().GetLocation();
287 FVector min = currentPosition - FVector(radius / 2, radius / 2, 0);
288 FVector max = currentPosition + FVector(radius / 2, radius / 2, 0);
290 for (int32 i = 0; i < roamingPoints; i++)
292 FTransform randomPoint;
293 randomPoint.SetLocation(FVector(FMath::RandRange(min.X, max.X), FMath::RandRange(min.Y, max.Y), 5000));
295 generatedRoamingPoints.Add(randomPoint);
297 UE_LOG(LogTemp, Warning, TEXT(
"Waypoint %i: (%s)"), i, *randomPoint.GetLocation().ToString());
301 return generatedRoamingPoints;
326 if (ROSInstance && ROSInstance->IsROSConnected())
328 ROSTopic = NewObject<UTopic>(UTopic::StaticClass());
335 ROSTopic->Init(ROSInstance->ROSIntegrationCore, TopicName,
"std_msgs/Float32");
348 ROSTopic->ConditionalBeginDestroy();
TSharedPtr< ROSMessages::std_msgs::Float32 > ROSMessage
TArray< FVector > WayPoints
void AssignRoamingPoints(const TArray< FTransform > Points)
void SetDroneRotation(USkeletalMeshComponent *target, FRotator rotator)
FVector GetCurrentWaypointTarget()
float DroneHeightFromGround
void MoveDroneToPosition(const FTransform Transform)
Override all drone roaming points and continue towards this position.
void AutoPilot(const float DeltaTime)
virtual void BeginPlay() override
void UpdateGroundHeight()
void SetFlightpath()
Called in tick function for drone roaming through points.
void ROSBridgeStateChanged(EROSState ROSState) override
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
FDroneParameters DroneParameters
UStaticMeshComponent * PositionMesh
USkeletalMeshComponent * DroneSkeletalMesh
TArray< FTransform > GenerateRoamingPoints(float radius, int32 roamingPoints)
Generates a roadming points array for the drone in radius.
virtual void Tick(float DeltaTime) override
virtual FString GetActorID_Implementation() const override
bool IsVehicleInGarage() const
ATransformSensor * TransformSensor
static UROSIntegrationGameInstance * GetROSGameInstance(const UObject *WorldContextObject)
static ATransformSensor * SpawnTransformSensor(const FTransform &transform, FTransformSensorParameters Parameters, const FString sensorIdentifier, const FString sensorName, bool SimulateSensor=true, AActor *Parent=nullptr)
EDroneEndAction DroneEndAction
TArray< FTransform > Points