31#include "ROSIntegration/Classes/ROSIntegrationGameInstance.h"
32#include "ROSIntegration/Public/std_msgs/String.h"
33#include "ROSIntegration/Classes/RI/Topic.h"
35#include "Engine/GameViewportClient.h"
36#include "Engine/Engine.h"
37#include "Engine/World.h"
38#include "Misc/DateTime.h"
39#include "Engine/World.h"
40#include "Async/Async.h"
71 UWorld* World =
nullptr;
72 if (GEngine && GEngine->GameViewport)
74 World = GEngine->GameViewport->GetWorld();
111 CommandHandlers.Add(TEXT(
"teleportspectator"),
FCommand(&
UROSCommands::HandleTeleportSpectator, 0.05f, TEXT(
"x,y,z or x,y,z,yaw,pitch,roll")));
112 CommandHandlers.Add(TEXT(
"teleportbyid"),
FCommand(&
UROSCommands::HandleTeleportActorByID, 0.05f, TEXT(
"ID of Actor, x,y,z or x,y,z,yaw,pitch,roll")));
216 CommandTopic = NewObject<UTopic>(UTopic::StaticClass());
221 std::function<void(TSharedPtr<FROSBaseMsg>)> SubscribeCallback = [
this](TSharedPtr<FROSBaseMsg> msg) ->
void
223 auto Concrete = StaticCastSharedPtr<ROSMessages::std_msgs::String>(msg);
224 if (Concrete.IsValid())
226 FString message = *(Concrete->_Data);
227 AsyncTask(ENamedThreads::GameThread, [
this, message]()
248 if (Message.FindChar(
' ', SpaceIndex))
251 Command = Message.Left(SpaceIndex).ToLower();
254 Variable = Message.Mid(SpaceIndex + 1).ToLower();
259 Command = Message.ToLower();
263 UE_LOG(LogTemp, Warning, TEXT(
"ROSCommands.Cpp: Incoming Command: %s with variable: %s"), *Command, *Variable);
270 double CurrentTime = FPlatformTime::Seconds();
273 if (TimeSinceLastExecution >= CommandInfo->
CooldownTime)
276 (this->*(CommandInfo->
Handler))(Variable);
283 float CoolDownTimeLeft = CommandInfo->
CooldownTime - TimeSinceLastExecution;
284 FString Msg = FString::Printf(TEXT(
"Command '%s' on cooldown. Retry in %.2f sec."), *Command, CoolDownTimeLeft);
290 FString Msg = FString::Printf(TEXT(
"Couldn't find command: '%s'"), *Command);
297 FString LoweredString = String.ToLower();
299 if (LoweredString.Equals(
"true") || LoweredString.Equals(
"1"))
304 else if (LoweredString.Equals(
"false") || LoweredString.Equals(
"0"))
321 FString message =
"Available Simulator ROS commands {COMMAND} {VALUE} || ";
325 const FString& CommandString = CommandPair.Key;
326 const FCommand& CommandInfo = CommandPair.Value;
329 message += FString::Printf(TEXT(
"%s || "), *CommandString);
333 message += FString::Printf(TEXT(
"%s %s || "), *CommandString, *CommandInfo.
DefaultValue);
343 if (GEngine && World)
345 GEngine->Exec(World, TEXT(
"quit force"));
352 if (AgrarsenseSettings)
361 if (AgrarsenseSettings)
370 if (AgrarsenseSettings)
378 int32 FrameCount = 0;
379 if (!Variable.IsNumeric())
384 FrameCount = FCString::Atoi(*Variable);
388 if (AgrarsenseSettings)
398 if (!Variable.IsNumeric())
403 Time = FCString::Atof(*Variable);
407 if (AgrarsenseSettings)
417 if (AgrarsenseSettings)
426 if (GEngine && World)
428 GEngine->Exec(World, *Variable);
435 TArray<FString> Components;
436 Variable.ParseIntoArray(Components, TEXT(
","),
true);
438 if (Components.Num() >= 3)
444 float X = FCString::Atof(*Components[0]);
445 float Y = FCString::Atof(*Components[1]);
446 float Z = FCString::Atof(*Components[2]);
448 Location = FVector(X, Y, Z);
450 if (Components.Num() >= 6)
453 float Pitch = FCString::Atof(*Components[3]);
454 float Yaw = FCString::Atof(*Components[4]);
455 float Roll = FCString::Atof(*Components[5]);
458 Rotation = FQuat(FRotator(Pitch, Yaw, Roll));
479 FString TransformString;
482 TArray<FString> Tokens;
483 Variable.ParseIntoArray(Tokens, TEXT(
" "),
true);
484 if (Tokens.Num() >= 2)
487 TransformString = Variable.RightChop(ID.Len() + 1);
504 TArray<FString> Components;
505 TransformString.ParseIntoArray(Components, TEXT(
","),
true);
507 if (Components.Num() >= 3)
513 float X = FCString::Atof(*Components[0]);
514 float Y = FCString::Atof(*Components[1]);
515 float Z = FCString::Atof(*Components[2]);
517 Location = FVector(X, Y, Z);
519 if (Components.Num() >= 6)
522 float Pitch = FCString::Atof(*Components[3]);
523 float Yaw = FCString::Atof(*Components[4]);
524 float Roll = FCString::Atof(*Components[5]);
527 Rotation = FQuat(FRotator(Pitch, Yaw, Roll));
535 UE_LOG(LogTemp, Warning, TEXT(
"Teleporting Actor with ID: %s to: %s"), *ID, *TransformString);
539 AVehicle* VehiclePtr = Cast<AVehicle>(Actor);
557 if (InstancedActorPtr)
572 Message = FString::Printf(TEXT(
"Object with ID %s successfully destroyed."), *Variable);
576 Message = FString::Printf(TEXT(
"Failed to destroy object with ID %s."), *Variable);
600 TArray<AActor*> Vehicles;
601 UGameplayStatics::GetAllActorsOfClass(World, AVehicle::StaticClass(), Vehicles);
602 for (AActor* Actor : Vehicles)
615 if (AgrarsenseSettings)
617 TArray<FString> Maps = AgrarsenseSettings->
GetMapNames();
621 for (
const FString& map : Maps)
623 message += FString::Printf(TEXT(
"%s, "), *map);
647 TArray<AActor*> Actors;
648 UGameplayStatics::GetAllActorsWithInterface(
GetGameWorld(), UActorInformation::StaticClass(), Actors);
650 TArray<AActor*> TypedActors;
651 for (AActor* Actor : Actors)
653 if (Actor->IsA(ActorClass))
655 TypedActors.Add(Actor);
660 if (!TypedActors.IsEmpty())
662 Msg = FString::Printf(TEXT(
"Simulation has %d %ss \n "), TypedActors.Num(), *ActorTypeName);
663 for (AActor* Actor : TypedActors)
669 Msg += FString::Printf(TEXT(
"%s \n \n "), *ActorInfo->GetActorInformation_Implementation());
676 Msg = FString::Printf(TEXT(
"Simulation has 0 %ss."), *ActorTypeName);
689 if (Variable.IsNumeric())
692 if (AgrarsenseSettings)
694 int32 IntValue = FCString::Atoi(*Variable);
702 if (Variable.IsNumeric())
705 if (AgrarsenseSettings)
707 float FloatValue = FCString::Atof(*Variable);
716 if (AgrarsenseSettings)
732 if (AgrarsenseSettings)
791 Walker->ExportToJsonFile(
"ExportedWalker");
801 AVehicle* VehiclePtr = VehicleData.Vehicle.Get();
818 TArray<AActor*> Vehicles;
819 UGameplayStatics::GetAllActorsOfClass(World, AVehicle::StaticClass(), Vehicles);
820 for (AActor* Actor : Vehicles)
822 AVehicle* VehiclePtr = Cast<AVehicle>(Actor);
831 UGameplayStatics::GetAllActorsOfClass(World, ASensor::StaticClass(),
Sensors);
834 ASensor* SensorPtr = Cast<ASensor>(Actor);
866 TArray<AActor*> OverlapSensors;
867 UGameplayStatics::GetAllActorsOfClass(World, AOverlapSensor::StaticClass(), OverlapSensors);
869 for (AActor* SensorActor : OverlapSensors)
883 FString BoundsString;
885 TArray<FString> Tokens;
886 Variable.ParseIntoArray(Tokens, TEXT(
" "),
true);
888 if (Tokens.Num() < 2)
895 BoundsString = Tokens[1];
898 TArray<FString> BoundsTokens;
899 BoundsString.ParseIntoArray(BoundsTokens, TEXT(
","),
true);
901 if (BoundsTokens.Num() >= 3)
903 Bounds.X = FCString::Atof(*BoundsTokens[0]);
904 Bounds.Y = FCString::Atof(*BoundsTokens[1]);
905 Bounds.Z = FCString::Atof(*BoundsTokens[2]);
919 FString BoundsString;
921 TArray<FString> Tokens;
922 Variable.ParseIntoArray(Tokens, TEXT(
" "),
true);
924 if (Tokens.Num() < 2)
931 BoundsString = Tokens[1];
933 FVector RelativePosition;
934 TArray<FString> BoundsTokens;
935 BoundsString.ParseIntoArray(BoundsTokens, TEXT(
","),
true);
937 if (BoundsTokens.Num() >= 3)
939 RelativePosition.X = FCString::Atof(*BoundsTokens[0]);
940 RelativePosition.Y = FCString::Atof(*BoundsTokens[1]);
941 RelativePosition.Z = FCString::Atof(*BoundsTokens[2]);
974 FString TransformString;
977 TArray<FString> Tokens;
978 Variable.ParseIntoArray(Tokens, TEXT(
" "),
true);
979 if (Tokens.Num() >= 2)
982 TransformString = Variable.RightChop(ID.Len() + 1);
1003 TArray<FString> Components;
1004 TransformString.ParseIntoArray(Components, TEXT(
","),
true);
1006 if (Components.Num() >= 3)
1012 float X = FCString::Atof(*Components[0]);
1013 float Y = FCString::Atof(*Components[1]);
1014 float Z = FCString::Atof(*Components[2]);
1016 Location = FVector(X, Y, Z);
1018 if (Components.Num() >= 6)
1021 float Pitch = FCString::Atof(*Components[3]);
1022 float Yaw = FCString::Atof(*Components[4]);
1023 float Roll = FCString::Atof(*Components[5]);
1026 Rotation = FQuat(FRotator(Pitch, Yaw, Roll));
1040 Walker->ChangeWalkerParameters(NewParams);
1080 FString TransformString;
1083 TArray<FString> Tokens;
1084 Variable.ParseIntoArray(Tokens, TEXT(
" "),
true);
1085 if (Tokens.Num() >= 2)
1088 TransformString = Variable.RightChop(ID.Len() + 1);
1109 TArray<FString> Components;
1110 TransformString.ParseIntoArray(Components, TEXT(
","),
true);
1112 if (Components.Num() >= 3)
1118 float X = FCString::Atof(*Components[0]);
1119 float Y = FCString::Atof(*Components[1]);
1120 float Z = FCString::Atof(*Components[2]);
1122 Location = FVector(X, Y, Z);
1124 if (Components.Num() >= 6)
1127 float Pitch = FCString::Atof(*Components[3]);
1128 float Yaw = FCString::Atof(*Components[4]);
1129 float Roll = FCString::Atof(*Components[5]);
1132 Rotation = FQuat(FRotator(Pitch, Yaw, Roll));
1145 Drone->ChangeDroneParameters(NewParams);
1183 TArray<FString> Tokens;
1184 Variable.ParseIntoArray(Tokens, TEXT(
" "),
true);
1185 if (Tokens.Num() < 2)
1191 const FString& ID = Tokens[0];
1199 ASensor* Sensor = Cast<ASensor>(Actor);
1205 FString BooleanString = Tokens[1].ToLower();
1207 bool SimulateSensor;
1219 if (Variable.IsNumeric())
1222 if (AgrarsenseSettings)
1224 int32 IntValue = FCString::Atoi(*Variable);
1232 if (Variable.IsNumeric())
1235 if (AgrarsenseSettings)
1237 float Value = FCString::Atof(*Variable);
1255 TArray<FString> ParsedStrings;
1256 Variable.ParseIntoArray(ParsedStrings, TEXT(
" "),
true);
1258 if (ParsedStrings.Num() != 2)
1264 TArray<FString> PositionComponents;
1265 ParsedStrings[0].ParseIntoArray(PositionComponents, TEXT(
","),
true);
1267 if (PositionComponents.Num() != 3)
1272 FVector Position(FCString::Atof(*PositionComponents[0]), FCString::Atof(*PositionComponents[1]), FCString::Atof(*PositionComponents[2]));
1275 TArray<FString> BoundsComponents;
1276 ParsedStrings[1].ParseIntoArray(BoundsComponents, TEXT(
","),
true);
1278 if (BoundsComponents.Num() != 3)
1283 FVector Bounds(FCString::Atof(*BoundsComponents[0]), FCString::Atof(*BoundsComponents[1]), FCString::Atof(*BoundsComponents[2]));
1285 FTransform VolumeTransform;
1286 VolumeTransform.SetLocation(Position);
void DestroyOverlappingActors(bool OnlyTrees)
void ChangeOverlapBounds(FVector Bounds)
void UpdateTransformPosition()
void SetSaveCombinedCloudToDisk(bool bSaveCombinedCloud)
void SetOverlapRelativePosition(const FVector &Vector)
void SetVisualizeOverlapArea(bool Visualize)
void SetOverlapBounds(const FVector &NewSize)
void SetSimulateSensor(bool SimulateSensor)
FString ExportToJsonFile(const FString &FileName)
void TeleportSpectator(const FTransform &Transform)
FString ExportToJsonFile(const FString &FileName)
void TogglePhysics(bool isOn)
FString ExportToJSON(const FString &FileName)
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)
void SetQualityLevelFromString(const FString &QualityLevelString)
void AdvanceTime(float TimeToAdvance)
void ChangeMapByName(FString MapName)
void AdvanceFrameCount(int32 FramesToAdvance)
TArray< FString > GetMapNames() const
void SetGlobalTimeDilation(float TimeDilation)
void SetGlobalTargetFrameRate(int NewTargetFrameRate)
void PauseSimulationEndOfThisFrame()
void SetWorldPositionOffsetRenderDistance(int32 WorldPositionOffsetDistance)
void SetWorldRendering(bool enabled)
void SetNaniteMaxPixelsPerEdge(float MaxPixelsPerEdge=1.0f)
static ASpectator * GetSpectator(const UObject *WorldContextObject)
static UAgrarsenseSettings * GetAgrarsenseSettings()
static AWeather * GetWeatherActor(const UObject *WorldContextObject)
static UROSIntegrationGameInstance * GetROSGameInstance(const UObject *WorldContextObject)
static ALidarManager * GetLidarManager(const UObject *WorldContextObject)
static UROSHandler * GetROSHandle(const UObject *WorldContextObject)
static TArray< AInstancedActor * > GetAllAddedPropActors()
static void DestroyAllWalkers()
static TArray< FVehicleData > GetSpawnedVehicles()
static TArray< AInstancedActor * > GetAllAddedFoliageActors()
static TArray< AWalker * > GetAllWalkers()
void HandleTeleportActorByID(const FString &Variable)
void HandleUnrealCommand(const FString &Variable)
void HandleChangeOverlapSensorBounds(const FString &Variable)
void HandlePrintAllWalkers(const FString &Variable)
void HandleTeleportSpectator(const FString &Variable)
void HandlePrintAllVehicles(const FString &Variable)
void HandlePrintAllSensors(const FString &Variable)
UROSIntegrationGameInstance * RosInstance
void HandleDestroyActorsArea(const FString &Variable)
void HandleSetWalkerMovement(const FString &Variable, bool IsPaused)
static TMap< FString, FCommand > CommandHandlers
void HandleAdvanceOneFrame(const FString &Variable)
void HandleUnPauseSimulator(const FString &Variable)
void HandlePrintAvailableCommands(const FString &Variable)
void PrintActorInformation(const FString &Variable, UClass *ActorClass, const FString &ActorTypeName)
void HandleAdvanceTime(const FString &Variable)
void HandleExportAll(const FString &Variable)
void HandleExportSensors(const FString &Variable)
void HandleExportVehicles(const FString &Variable)
void HandleSetSaveCombinedPointcloudToDisk(const FString &Variable)
void HandleDisableAllSensors(const FString &Variable)
void HandleSetWPO(const FString &Variable)
void HandleChangeMap(const FString &Variable)
void HandleExportWalkers(const FString &Variable)
void HandleResumeWalker(const FString &Variable)
UAgrarsenseSettings * GetSettings()
void HandleMoveDroneTo(const FString &Variable, EDroneEndAction EndAction)
void HandleSetWorldRendering(const FString &Variable)
void HandleSetGlobalTimeDilation(const FString &Variable)
void HandleDestroyAllSensors(const FString &Variable)
void ParseIncomingMessage(const FString Message)
void SetupROSCommandTopic()
void HandleMoveWalkerToAndStop(const FString &Variable)
void HandleExportWeather(const FString &Variable)
bool TryParseBoolean(const FString &String, bool &OutBool)
void HandleExportFoliage(const FString &Variable)
void HandleQuit(const FString &Variable)
void HandleSetAllSensorsEnabled(const FString &Variable)
void HandleChangeOverlapSensorPosition(const FString &Variable)
void HandleSetSensorEnabled(const FString &Variable)
void HandleTryDestroyObjectByID(const FString &Variable)
void HandleVisualizeOverlapSensorsBounds(const FString &Variable)
void HandleEnableAllSensors(const FString &Variable)
void HandleMoveWalkerTo(const FString &Variable, EWalkerEndAction EndAction)
void HandleDestroyAllVehicles(const FString &Variable)
void HandleStopWalker(const FString &Variable)
void HandleDestroyActors(const FString &Variable, bool OnlyTrees)
void HandleMoveDroneToAndStop(const FString &Variable)
void HandleDestroyAllWalkers(const FString &Variable)
void HandleMoveWalkerToAndDestroy(const FString &Variable)
void HandleSetNaniteMaxPixelsPerEdge(const FString &Variable)
void HandlePrintMaps(const FString &Variable)
void HandleSpawnObjects(const FString &Variable)
void HandleDestroyTreesArea(const FString &Variable)
void HandleSetGlobalTargetFrameRate(const FString &Variable)
void HandlePrintIds(const FString &Variable)
void HandleAdvanceFrames(const FString &Variable)
void HandleSetQualityLevel(const FString &Variable)
void HandlePauseSimulator(const FString &Variable)
void TryExecuteCommand(const FString &Command)
void HandleExportProps(const FString &Variable)
void ROSBridgeStateChanged(EROSState state)
FROSDelegate_ROState OnROSStateChanged
static void DestroyAllSensors(const UObject *WorldContextObject, bool DestroyDefaultVehicleSensors=false)
static void EnableAllSensors(const UObject *WorldContextObject)
static void DisableAllSensors(const UObject *WorldContextObject)
static FString ExportInstancedActorsToJSON(FString FileName, const TArray< AInstancedActor * > &Actors)
static void ParseAndOperateJSONFile(const FString &Path)
EDroneEndAction DroneEndAction
EWalkerAction WalkerAction
TArray< FTransform > Points
EWalkerEndAction WalkerEndAction