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