Agrarsense
ROSHandler.cpp
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#include "ROSHandler.h"
7
8#include "Containers/StringConv.h"
9#include "Engine/World.h"
10
11void UROSHandler::Setup(bool RosIsConnected)
12{
13 // Bind to OnROSBridgeChanged event
15
16 // Set ROS connected state
17 ROSConnected = RosIsConnected;
19}
20
22{
23 if (ROSConnected)
24 {
25#if WITH_EDITOR
26 UE_LOG(LogTemp, Error, TEXT("ROSHandler.cpp: ROS is already active."));
27#endif
28 return;
29 }
30
31#if PLATFORM_LINUX
32 FString command = "gnome-terminal -- bash -c \"roslaunch rosbridge_server rosbridge_tcp.launch bson_only_mode:=True""\"";
33
34#if WITH_EDITOR
35 UE_LOG(LogTemp, Warning, TEXT("ROSHandler.cpp: Attempting to launch ROS with command: %s"), *command);
36#endif
37 // Attempt to launch ROS on Linux
38 system(TCHAR_TO_ANSI(*command));
39
40#elif PLATFORM_WINDOWS
41
42 // Attempt to launch ROS on Windows via WSL
43
44 // Create the process
45 FString Application = "wsl";
46 FString CommandParams = "-e bash -lic \"roslaunch rosbridge_server rosbridge_tcp.launch bson_only_mode:=True""\"";
47
48 // Added LaunchParams for ROS1 to ROS2 bridge. ROS2 command is in .bashrc, check build_windows.md
49 FString BridgeCommands = "-e bash -lic \"ROS2 && ros2 run ros1_bridge dynamic_bridge --bridge-all-topics""\"";
50
51 ROSProcessHandle = FPlatformProcess::CreateProc(*Application, *CommandParams, true, false, false, nullptr, 0, nullptr, nullptr);
52 ROSBridgeHandle = FPlatformProcess::CreateProc(*Application, *BridgeCommands, true, false, false, nullptr, 0, nullptr, nullptr);
53
54#if WITH_EDITOR
55 if (!ROSProcessHandle.IsValid())
56 {
57 UE_LOG(LogTemp, Error, TEXT("ROSHandler.cpp: Failed to create wsl process"));
58 }
59
60 if (!ROSBridgeHandle.IsValid())
61 {
62 UE_LOG(LogTemp, Error, TEXT("ROSHandler.cpp: Bridge not installed"));
63 }
64#endif
65
66#endif
67}
68
70{
71 if (!ROSConnected)
72 {
73 return;
74 }
75
76 FString StopCommand = "killall -9 rosmaster";
77
78#if PLATFORM_LINUX
79 // Attempt to stop ROS on Linux
80 system(TCHAR_TO_ANSI(*StopCommand));
81
82#elif PLATFORM_WINDOWS
83 // Attempt to stop ROS on Windows
84 if (ROSBridgeHandle.IsValid())
85 {
86 if (FPlatformProcess::IsProcRunning(ROSBridgeHandle))
87 {
88 FPlatformProcess::TerminateProc(ROSBridgeHandle);
89 }
90 }
91
92 if (ROSProcessHandle.IsValid())
93 {
94 if (FPlatformProcess::IsProcRunning(ROSProcessHandle))
95 {
96 FPlatformProcess::TerminateProc(ROSProcessHandle);
97 }
98 }
99 else
100 {
101 // Else ROS Bridge might have been opened by the user, so let's
102 // stop it by opening new wsl window and giving StopCommand.
103 FString Application = "wsl";
104 FProcHandle Handle = FPlatformProcess::CreateProc(*Application, *StopCommand, true, false, false, nullptr, 0, nullptr, nullptr);
105 }
106#endif
107
108 ROSConnected = false;
109}
110
112{
115}
116
118{
119 ROSState = newRosState;
120 ROSConnected = ROSState == EROSState::Connected ? true : false;
121
122#if WITH_EDITOR
123 UE_LOG(LogTemp, Warning, TEXT("ROSHandler.cpp: ROS bridge state changed. ROS is connected: %s"), (ROSConnected ? TEXT("true") : TEXT("false")));
124#endif
125}
EROSState
Definition: ROSState.h:16
void StopROSBridge()
Definition: ROSHandler.cpp:69
void RestartROSBridge()
Definition: ROSHandler.cpp:111
void Setup(bool RosIsConnected)
Definition: ROSHandler.cpp:11
EROSState ROSState
Definition: ROSHandler.h:88
FProcHandle ROSProcessHandle
Definition: ROSHandler.h:90
bool ROSConnected
Definition: ROSHandler.h:94
void LaunchROSBridge()
Definition: ROSHandler.cpp:21
FROSDelegate_ROState OnROSStateChanged
Definition: ROSHandler.h:81
void OnROSBridgeChanged(EROSState newRosState)
Definition: ROSHandler.cpp:117
FProcHandle ROSBridgeHandle
Definition: ROSHandler.h:92