Agrarsense
ROSCommands.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
12#include "ROSState.h"
13
14#include "ROSCommands.generated.h"
15
17class UROSIntegrationGameInstance;
18class UTopic;
19class UWorld;
20
21typedef void (UROSCommands::* CommandHandler)(const FString&);
22
24{
25 FCommand() : Handler(nullptr), CooldownTime(0.0f), LastExecutionTime(0.0), DefaultValue(TEXT("")) {}
26
27 FCommand(CommandHandler InHandler, float InCooldownTime, const FString& InDefaultValue = "")
28 : Handler(InHandler), CooldownTime(InCooldownTime), LastExecutionTime(0.0), DefaultValue(InDefaultValue)
29 {}
30
34 FString DefaultValue;
35};
36
37/*
38* This class is responsible listening incoming string ROS messages from /Agrarsense/Commands ROS topic,
39* parsing the incoming message and turning it into Simulation action.
40* For example changing main camera (Spectator) world rendering on/off can be done with below ROS message:
41* rostopic pub /Agrarsense/Commands std_msgs/String "SetWorldRendering false" --once
42* And turning it back on can be done with below ROS message:
43* rostopic pub /Agrarsense/Commands std_msgs/String "SetWorldRendering true" --once
44*/
45UCLASS()
46class AGRARSENSE_API UROSCommands : public UObject
47{
48
49 GENERATED_BODY()
50
51public:
52
53 /*
54 * For testing purposes, should not be used otherwise.
55 * Get Game mode - GetROSCommands - TryExecuteCommand
56 */
57 UFUNCTION(BlueprintCallable)
58 void TryExecuteCommand(const FString& Command);
59
60private:
61
63
64 void Init();
65
66 void Destroy();
67
68 /*
69 * Static map of command handlers, indexed by command strings.
70 * This static implementation ensures that the cooldown times for commands are preserved when switching between maps.
71 * By not resetting cooldowns during map transitions, we prevent potential command spamming.
72 */
73 static TMap<FString, FCommand> CommandHandlers;
74
75 UWorld* GetGameWorld();
76
77 UAgrarsenseSettings* GetSettings();
78
79 /*
80 * Add Commands to CommandHandlers TMap
81 */
82 void AddCommands();
83
84 void SetupROSCommandTopic();
85
86 void DestroyROSTopic();
87
88 void ParseIncomingMessage(const FString Message);
89
90 UFUNCTION()
91 void ROSBridgeStateChanged(EROSState state);
92
99 bool TryParseBoolean(const FString& String, bool& OutBool);
100
101 UPROPERTY()
102 UTopic* CommandTopic = nullptr;
103
104 UPROPERTY()
105 UROSIntegrationGameInstance* RosInstance = nullptr;
106
107 void HandlePrintAvailableCommands(const FString& Variable);
108
109 void HandleQuit(const FString& Variable);
110
111 void HandlePauseSimulator(const FString& Variable);
112
113 void HandleUnPauseSimulator(const FString& Variable);
114
115 void HandleAdvanceOneFrame(const FString& Variable);
116
117 void HandleAdvanceFrames(const FString& Variable);
118
119 void HandleAdvanceTime(const FString& Variable);
120
121 void HandleChangeMap(const FString& Variable);
122
123 void HandleUnrealCommand(const FString& Variable);
124
125 void HandleTeleportSpectator(const FString& Variable);
126
127 void HandleTeleportActorByID(const FString& Variable);
128
129 void HandleTryDestroyObjectByID(const FString& Variable);
130
131 void HandleDestroyAllSensors(const FString& Variable);
132
133 void HandleDestroyAllWalkers(const FString& Variable);
134
135 void HandleDestroyAllVehicles(const FString& Variable);
136
137 void HandlePrintMaps(const FString& Variable);
138
139 void HandlePrintAllSensors(const FString& Variable);
140
141 void HandlePrintAllVehicles(const FString& Variable);
142
143 void HandlePrintAllWalkers(const FString& Variable);
144
145 void PrintActorInformation(const FString& Variable, UClass* ActorClass, const FString& ActorTypeName);
146
147 void HandlePrintIds(const FString& Variable);
148
149 void HandleSetGlobalTargetFrameRate(const FString& Variable);
150
151 void HandleSetGlobalTimeDilation(const FString& Variable);
152
153 void HandleSetQualityLevel(const FString& Variable);
154
155 void HandleSetWorldRendering(const FString& Variable);
156
157 void HandleSetSaveCombinedPointcloudToDisk(const FString& Variable);
158
159 void HandleSpawnObjects(const FString& Variable);
160
161 void HandleExportAll(const FString& Variable);
162
163 void HandleExportWeather(const FString& Variable);
164
165 void HandleExportWalkers(const FString& Variable);
166
167 void HandleExportVehicles(const FString& Variable);
168
169 void HandleExportSensors(const FString& Variable);
170
171 void HandleExportFoliage(const FString& Variable);
172
173 void HandleExportProps(const FString& Variable);
174
175 void HandleVisualizeOverlapSensorsBounds(const FString& Variable);
176
177 void HandleChangeOverlapSensorBounds(const FString& Variable);
178
179 void HandleChangeOverlapSensorPosition(const FString& Variable);
180
181 void HandleMoveWalkerToAndDestroy(const FString& Variable);
182
183 void HandleMoveWalkerToAndStop(const FString& Variable);
184
185 void HandleMoveWalkerTo(const FString& Variable, EWalkerEndAction EndAction);
186
187 void HandleStopWalker(const FString& Variable);
188
189 void HandleResumeWalker(const FString& Variable);
190
191 void HandleSetWalkerMovement(const FString& Variable, bool IsPaused);
192
193 void HandleMoveDroneTo(const FString& Variable, EDroneEndAction EndAction);
194
195 void HandleMoveDroneToAndStop(const FString& Variable);
196
197 void HandleSetAllSensorsEnabled(const FString& Variable);
198
199 void HandleEnableAllSensors(const FString& Variable);
200
201 void HandleDisableAllSensors(const FString& Variable);
202
203 void HandleSetSensorEnabled(const FString& Variable);
204
205 void HandleSetWPO(const FString& Variable);
206
207 void HandleSetNaniteMaxPixelsPerEdge(const FString& Variable);
208
209 void HandleDestroyTreesArea(const FString& Variable);
210
211 void HandleDestroyActorsArea(const FString& Variable);
212
213 void HandleDestroyActors(const FString& Variable, bool OnlyTrees);
214};
EDroneEndAction
void(UROSCommands::* CommandHandler)(const FString &)
Definition: ROSCommands.h:21
EROSState
Definition: ROSState.h:16
EWalkerEndAction
static TMap< FString, FCommand > CommandHandlers
Definition: ROSCommands.h:73
double LastExecutionTime
Definition: ROSCommands.h:33
FString DefaultValue
Definition: ROSCommands.h:34
FCommand(CommandHandler InHandler, float InCooldownTime, const FString &InDefaultValue="")
Definition: ROSCommands.h:27
float CooldownTime
Definition: ROSCommands.h:32
CommandHandler Handler
Definition: ROSCommands.h:31