17#include "ROSIntegration/Classes/RI/Topic.h"
19#include "Camera/CameraComponent.h"
20#include "Field/FieldSystemNodes.h"
21#include "NiagaraComponent.h"
22#include "Algo/Reverse.h"
25#include "DrawDebugHelpers.h"
30 PrimaryActorTick.bCanEverTick =
true;
31 InteractableName = NSLOCTEXT(
"Agrarsense",
"DroneInteractableName",
"Drone");
37 UE_LOG(LogTemp, Warning, TEXT(
"Parameters Changed!"));
50 Params.
Size = FVector(RadiusMeters, RadiusMeters, RadiusMeters);
53 FString OverlapSensorID =
ActorID +
"/inner_overlap";
97 ROSMessage = MakeShared<ROSMessages::std_msgs::Float32>();
102 DroneSkeletalMesh = Cast<USkeletalMeshComponent>(GetComponentByClass(USkeletalMeshComponent::StaticClass()));
103 PositionMesh = Cast<UStaticMeshComponent>(GetComponentByClass(UStaticMeshComponent::StaticClass()));
106 FTransform ActorTransform = GetActorTransform();
117 FString VehicleTransformSensorID =
ActorID +
"/transform";
126 Params.
Size = FVector(25.0f, 25.0f, 25.0f);
128 FString OverlapSensorID =
ActorID +
"/overlap";
149 Super::EndPlay(EndPlayReason);
169 Super::Tick(DeltaTime);
202 for (
int i = 0; i < dronePoints.Num(); i++)
207 DrawDebugSphere(
World, dronePoints[i].GetLocation(), 5.0f, 25, FColor::Red,
false);
208 if (i < dronePoints.Num() - 1)
210 FVector nextPointLocation = dronePoints[i + 1].GetLocation();
211 DrawDebugLine(
World, dronePoints[i].GetLocation(), nextPointLocation, FColor::Blue,
false, 0.0f, 0, 5.0f);
216 for (
int i = 0; i <
dronePath.Num(); i++)
220 FVector nextPointLocation =
dronePath[i + 1].GetLocation();
221 DrawDebugLine(
World,
dronePath[i].GetLocation(), nextPointLocation, FColor::Green,
false, 0.0f, 0, 5.0f);
231 if (SimulationLevelManager)
235 if (controlledVehicle ==
this)
258 FVector currentlocation =
DroneSkeletalMesh->GetRelativeTransform().GetLocation();
268 UE_LOG(LogTemp, Warning, TEXT(
"Desired location mesh not found"));
272 distanceToNextPoint = FVector2f::Distance(FVector2f(waypoint.X, waypoint.Y), FVector2f(currentlocation.X, currentlocation.Y));
312 if (!GetWorld()->GetTimerManager().IsTimerActive(
DestroyHandle))
358 for (
const FTransform& point : Points)
371 FRotator currentRotation = target->GetRelativeRotation();
372 FRotator rotationDifference = rotator - currentRotation;
374 target->AddRelativeRotation(rotationDifference,
false,
nullptr, ETeleportType::TeleportPhysics);
377 if (FMath::Abs(rotator.Pitch) < 0.001f)
379 rotator.Pitch = 0.0f;
381 if (FMath::Abs(rotator.Yaw) < 0.001f)
385 if (FMath::Abs(rotator.Roll) < 0.001f)
391 target->SetRelativeRotation(rotator,
false,
nullptr, ETeleportType::TeleportPhysics);
402 FVector End = Start - FVector(0, 0, 10000.0f);
404 FCollisionQueryParams TraceParams = FCollisionQueryParams(FName(TEXT(
"UpdateGroundHeight")),
true,
this);
405 TraceParams.bReturnPhysicalMaterial =
false;
406 TraceParams.bTraceComplex =
false;
408 FHitResult HitResult;
410#ifdef ParallelLineTraceSingleByChannel_EXISTS
413 World->ParallelLineTraceSingleByChannel(
419 FCollisionResponseParams::DefaultResponseParam);
422 World->LineTraceSingleByChannel(
428 FCollisionResponseParams::DefaultResponseParam);
431 if (HitResult.IsValidBlockingHit())
433 float Height = (Start.Z - HitResult.ImpactPoint.Z) / 100.0f;
434 float HeightRounded = FMath::RoundToFloat(Height * 1000.0f) / 1000.0f;
451 TArray<FTransform> generatedRoamingPoints;
452 generatedRoamingPoints.Reserve(roamingPoints);
454 FVector currentPosition = GetTransform().GetLocation();
456 FVector min = currentPosition - FVector(radius / 2, radius / 2, 0);
457 FVector max = currentPosition + FVector(radius / 2, radius / 2, 0);
459 for (int32 i = 0; i < roamingPoints; i++)
461 FTransform randomPoint;
462 randomPoint.SetLocation(FVector(FMath::RandRange(min.X, max.X), FMath::RandRange(min.Y, max.Y), 5000));
464 generatedRoamingPoints.Add(randomPoint);
466 UE_LOG(LogTemp, Warning, TEXT(
"Waypoint %i: (%s)"), i, *randomPoint.GetLocation().ToString());
470 return generatedRoamingPoints;
495 if (ROSInstance && ROSInstance->IsROSConnected())
497 ROSTopic = NewObject<UTopic>(UTopic::StaticClass());
504 ROSTopic->Init(ROSInstance->ROSIntegrationCore, TopicName,
"std_msgs/Float32");
512 if (!DroneMesh || !DesiredLocation)
517 FVector ToTarget = DesiredLocation->GetComponentLocation() - DroneMesh->GetComponentLocation();
518 FRotator LookRotation = ToTarget.Rotation();
520 float CurrentYaw = DroneMesh->GetComponentRotation().Yaw;
521 float DesiredYaw = LookRotation.Yaw;
523 float DeltaYaw = FRotator::NormalizeAxis(DesiredYaw - CurrentYaw);
534 ROSTopic->ConditionalBeginDestroy();
void SetVisualizeOverlapArea(bool Visualize)
void SetOverlapBounds(const FVector &NewSize)
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
float GetYawRotationDifference(USkeletalMeshComponent *DroneSkeletalMesh, UStaticMeshComponent *DesiredLocation)
Get wanted waypoint target rotation.
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
float distanceToNextPoint
FTransform GetCurrentWaypointTarget_Transform()
FDroneParameters DroneParameters
AOverlapSensor * InnerOverlapSensor
FDroneFinished OnDroneFinished
TArray< FTransform > dronePath
UStaticMeshComponent * PositionMesh
USkeletalMeshComponent * DroneSkeletalMesh
FTimerHandle DestroyHandle
void ChangeDroneParameters(const FDroneParameters &newParameters)
TArray< FTransform > GenerateRoamingPoints(float radius, int32 roamingPoints)
Generates a roadming points array for the drone in radius.
virtual void Tick(float DeltaTime) override
void CeaseManualControlOfVehicle()
AVehicle * GetManuallyControlledVehicle()
virtual FString GetActorID_Implementation() const override
bool IsVehicleInGarage() const
AOverlapSensor * OverlapSensor
ATransformSensor * TransformSensor
UNiagaraComponent * NiagaraComponent
static UROSIntegrationGameInstance * GetROSGameInstance(const UObject *WorldContextObject)
static ASimulationLevelManager * GetSimulationLevelManager(const UObject *WorldContextObject)
static AOverlapSensor * SpawnOverlapSensor(const FTransform &transform, FOverlapSensorParameters Parameters, const FString sensorIdentifier, const FString sensorName, AActor *Parent=nullptr)
static ATransformSensor * SpawnTransformSensor(const FTransform &transform, FTransformSensorParameters Parameters, const FString sensorIdentifier, const FString sensorName, bool SimulateSensor=true, AActor *Parent=nullptr)
float InnerOverlapRadiusMeters
bool CreateInnerOverlapSensor
EDroneEndAction DroneEndAction
float OverlapRadiusMeters
TArray< FTransform > Points