Agrarsense
InfoTopic.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 "InfoTopic.h"
7
10
11#include "ROSIntegration/Classes/ROSIntegrationGameInstance.h"
12#include "ROSIntegration/Classes/RI/Topic.h"
13
14#include "Engine/GameViewportClient.h"
15#include "Engine/Engine.h"
16#include "Engine/World.h"
17
18TSharedPtr<ROSMessages::std_msgs::String> UInfoTopic::ROSMessage;
20TWeakObjectPtr<UTopic> UInfoTopic::InfoTopic;
22bool UInfoTopic::ROSConnected = false;
23
25{
26 if (Instance == nullptr)
27 {
28 Instance = NewObject<UInfoTopic>();
29 Instance->Setup();
30 }
31}
32
34{
35 if (Instance)
36 {
38 Instance = nullptr;
39 }
40}
41
43{
44 Instance = this;
45
46 if (GEngine && GEngine->GameViewport)
47 {
48 UWorld* World = GEngine->GameViewport->GetWorld();
50 if (RosHandle)
51 {
52 RosHandle->OnROSStateChanged.AddUniqueDynamic(this, &UInfoTopic::ROSBridgeStateChanged);
53 }
54 }
55
57}
58
60{
61 switch (state)
62 {
64 ROSConnected = true;
66 break;
67
69 ROSConnected = false;
71 break;
72 }
73}
74
76{
77 if (InfoTopic.IsValid())
78 {
79 InfoTopic->Unadvertise();
80 InfoTopic->Unsubscribe();
81 InfoTopic->MarkAsDisconnected();
82 InfoTopic->ConditionalBeginDestroy();
83 InfoTopic.Reset();
84
85#if WITH_EDITOR
86 UE_LOG(LogTemp, Warning, TEXT("InfoTopic.cpp: Destroyed ROS Info Topic"));
87#endif
88 }
89}
90
92{
93 if (InfoTopic.IsValid() && ROSMessage.IsValid())
94 {
95 return;
96 }
97
98 if (GEngine && GEngine->GameViewport)
99 {
100 UWorld* World = GEngine->GameViewport->GetWorld();
101 UROSIntegrationGameInstance* RosInstance = UAgrarsenseStatics::GetROSGameInstance(World);
102
103 if (RosInstance)
104 {
105 ROSMessage.Reset();
106 ROSMessage = MakeShared<ROSMessages::std_msgs::String>();
107
108 ROSConnected = RosInstance->IsROSConnected();
109
110 if (!InfoTopic.IsValid() && ROSConnected)
111 {
112 InfoTopic = NewObject<UTopic>(UTopic::StaticClass());
113 InfoTopic->Init(RosInstance->ROSIntegrationCore, TEXT("/agrarsense/out/info"), TEXT("std_msgs/String"));
114 InfoTopic->Advertise();
115
116#if WITH_EDITOR
117 UE_LOG(LogTemp, Warning, TEXT("InfoTopic.cpp: Created ROS Info Topic"));
118#endif
119 }
120 }
121 }
122}
123
125{
126 AllowInfoTopic = Allow;
127}
128
129void UInfoTopic::SendMessage(const FString& Message)
130{
131 // UInfoTopic::Init must be called before SendMessage is called.
132 // At the moment Init called from AgrarsenseGameModeBase.cpp BeginPlay
133
134 if (!InfoTopic.IsValid() || !ROSMessage.IsValid())
135 {
136 if (Instance)
137 {
139 }
140 }
141
143 && InfoTopic.IsValid() && ROSMessage.IsValid() && !Message.IsEmpty())
144 {
145 ROSMessage->_Data = Message;
146 InfoTopic->Publish(ROSMessage);
147 }
148}
EROSState
Definition: ROSState.h:16
static UROSIntegrationGameInstance * GetROSGameInstance(const UObject *WorldContextObject)
static UROSHandler * GetROSHandle(const UObject *WorldContextObject)
static TWeakObjectPtr< UTopic > InfoTopic
Definition: InfoTopic.h:58
static void Init()
Definition: InfoTopic.cpp:24
void SetupROSInfoTopic()
Definition: InfoTopic.cpp:91
static bool AllowInfoTopic
Definition: InfoTopic.h:64
static TSharedPtr< ROSMessages::std_msgs::String > ROSMessage
Definition: InfoTopic.h:60
static void SendMessage(const FString &Message)
Definition: InfoTopic.cpp:129
void DestroyROSTopic()
Definition: InfoTopic.cpp:75
void ROSBridgeStateChanged(EROSState state)
Definition: InfoTopic.cpp:59
void Setup()
Definition: InfoTopic.cpp:42
static bool ROSConnected
Definition: InfoTopic.h:62
static UInfoTopic * Instance
Definition: InfoTopic.h:56
static void Destroy()
Definition: InfoTopic.cpp:33
static void SetAllowSendInfo(bool Allow)
Definition: InfoTopic.cpp:124
FROSDelegate_ROState OnROSStateChanged
Definition: ROSHandler.h:81