Agrarsense
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
UROSHandler Class Reference

#include <ROSHandler.h>

Inheritance diagram for UROSHandler:
Inheritance graph
[legend]
Collaboration diagram for UROSHandler:
Collaboration graph
[legend]

Public Member Functions

void Setup (bool RosIsConnected)
 
void LaunchROSBridge ()
 
void StopROSBridge ()
 
void RestartROSBridge ()
 
bool IsROSConnected ()
 
EROSState GetROSState ()
 

Public Attributes

FROSDelegate_ROState OnROSStateChanged
 

Private Member Functions

void OnROSBridgeChanged (EROSState newRosState)
 

Private Attributes

EROSState ROSState = EROSState::Disconnected
 
FProcHandle ROSProcessHandle
 
FProcHandle ROSBridgeHandle
 
bool ROSConnected = false
 

Detailed Description

This class handles launching ROS bridge when the Simulator starts. On Windows this tries to start ROS Bridge through wsl, On Linux we simply start another terminal window and attempt to launch ROS bridge.

Definition at line 27 of file ROSHandler.h.

Member Function Documentation

◆ GetROSState()

EROSState UROSHandler::GetROSState ( )
inline

Get current ROS State

Returns
EROSState rosState

Definition at line 71 of file ROSHandler.h.

72 {
73 return ROSState;
74 }
EROSState ROSState
Definition: ROSHandler.h:88

◆ IsROSConnected()

bool UROSHandler::IsROSConnected ( )
inline

Is simulation connected to ROS Bridge at the moment.

Returns
bool isROSConnected

Definition at line 61 of file ROSHandler.h.

62 {
63 return ROSConnected;
64 }
bool ROSConnected
Definition: ROSHandler.h:94

◆ LaunchROSBridge()

void UROSHandler::LaunchROSBridge ( )

Attempt to launch ROS Bridge. BlueprintCallable.

Definition at line 21 of file ROSHandler.cpp.

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}
FProcHandle ROSProcessHandle
Definition: ROSHandler.h:90
FProcHandle ROSBridgeHandle
Definition: ROSHandler.h:92

References ROSBridgeHandle, ROSConnected, and ROSProcessHandle.

Referenced by RestartROSBridge(), and AAgrarsenseGameModeBase::SpawnNeededActorsInitGame().

◆ OnROSBridgeChanged()

void UROSHandler::OnROSBridgeChanged ( EROSState  newRosState)
private

Definition at line 117 of file ROSHandler.cpp.

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}

References Connected, ROSConnected, and ROSState.

Referenced by Setup().

◆ RestartROSBridge()

void UROSHandler::RestartROSBridge ( )

Shutdown ROS Bridge and then attempt to launch it again. BlueprintCallable.

Definition at line 111 of file ROSHandler.cpp.

112{
115}
void StopROSBridge()
Definition: ROSHandler.cpp:69
void LaunchROSBridge()
Definition: ROSHandler.cpp:21

References LaunchROSBridge(), and StopROSBridge().

◆ Setup()

void UROSHandler::Setup ( bool  RosIsConnected)

Setup ROSHandler. Should only be called From AgrarsenseGameModeBase.

Definition at line 11 of file ROSHandler.cpp.

12{
13 // Bind to OnROSBridgeChanged event
15
16 // Set ROS connected state
17 ROSConnected = RosIsConnected;
19}
FROSDelegate_ROState OnROSStateChanged
Definition: ROSHandler.h:81
void OnROSBridgeChanged(EROSState newRosState)
Definition: ROSHandler.cpp:117

References Connected, Disconnected, OnROSBridgeChanged(), OnROSStateChanged, ROSConnected, and ROSState.

Referenced by AAgrarsenseGameModeBase::SpawnNeededActorsInitGame().

◆ StopROSBridge()

void UROSHandler::StopROSBridge ( )

Shutdown ROS Bridge. BlueprintCallable.

Definition at line 69 of file ROSHandler.cpp.

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}

References ROSBridgeHandle, ROSConnected, and ROSProcessHandle.

Referenced by RestartROSBridge().

Member Data Documentation

◆ OnROSStateChanged

FROSDelegate_ROState UROSHandler::OnROSStateChanged

◆ ROSBridgeHandle

FProcHandle UROSHandler::ROSBridgeHandle
private

Definition at line 92 of file ROSHandler.h.

Referenced by LaunchROSBridge(), and StopROSBridge().

◆ ROSConnected

bool UROSHandler::ROSConnected = false
private

Definition at line 94 of file ROSHandler.h.

Referenced by LaunchROSBridge(), OnROSBridgeChanged(), Setup(), and StopROSBridge().

◆ ROSProcessHandle

FProcHandle UROSHandler::ROSProcessHandle
private

Definition at line 90 of file ROSHandler.h.

Referenced by LaunchROSBridge(), and StopROSBridge().

◆ ROSState

EROSState UROSHandler::ROSState = EROSState::Disconnected
private

Definition at line 88 of file ROSHandler.h.

Referenced by OnROSBridgeChanged(), and Setup().


The documentation for this class was generated from the following files: