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

#include <Weather.h>

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

Public Member Functions

 AWeather (const FObjectInitializer &ObjectInitializer)
 
void UpdateWeather (const FWeatherParameters &WeatherParameters, bool updateToROS)
 
FString ExportToJSON (const FString &FileName)
 
void ResetWeather ()
 
ESeason GetCurrentSeason ()
 
ESeason GetSeasonForMonth (const int32 Month)
 
const FWeatherParametersGetCurrentWeather () const
 

Public Attributes

FLevelEventDelegate_WeatherChanged OnWeatherChanged
 

Protected Member Functions

virtual void BeginPlay () override
 
virtual void EndPlay (const EEndPlayReason::Type EndPlayReason) override
 

Private Member Functions

void ROSBridgeStateChanged (EROSState ROSState)
 
void DestroyTopic ()
 
void InitializeROSTopic ()
 
void SendWeatherData (FWeatherParameters Parameters)
 

Private Attributes

FWeatherParameters CurrentWeather
 
UTopic * WeatherTopic = nullptr
 
UROSIntegrationGameInstance * RosInstance = nullptr
 
bool ROSConnected = false
 
TArray< FWeatherParametersWeatherPresets
 
TSharedPtr< ROSMessages::std_msgs::String > Message
 
const FWeatherParameters Default = { 0.0f, 0.0f, 0.1f, 0.05f, 0.0f, 0.2f, 0.0f, 15.0f, 0.0f, 0.0f, 7, 9 }
 

Detailed Description

Definition at line 30 of file Weather.h.

Constructor & Destructor Documentation

◆ AWeather()

AWeather::AWeather ( const FObjectInitializer &  ObjectInitializer)

Definition at line 17 of file Weather.cpp.

17 : Super(ObjectInitializer)
18{
19 PrimaryActorTick.bCanEverTick = false;
20}

Member Function Documentation

◆ BeginPlay()

void AWeather::BeginPlay ( )
overrideprotectedvirtual

Definition at line 22 of file Weather.cpp.

23{
24 Super::BeginPlay();
25
27
28 UWorld* World = GetWorld();
29
31 if (RosHandler)
32 {
33 RosHandler->OnROSStateChanged.AddUniqueDynamic(this, &AWeather::ROSBridgeStateChanged);
34 }
35
36 RosInstance = Cast<UROSIntegrationGameInstance>(World->GetGameInstance());
37 if (RosInstance)
38 {
39 ROSConnected = RosInstance->IsROSConnected();
40 }
41
42 Message = MakeShared<ROSMessages::std_msgs::String>();
43
45}
void ROSBridgeStateChanged(EROSState ROSState)
Definition: Weather.cpp:165
void InitializeROSTopic()
Definition: Weather.cpp:126
bool ROSConnected
Definition: Weather.h:112
const FWeatherParameters Default
Definition: Weather.h:119
TSharedPtr< ROSMessages::std_msgs::String > Message
Definition: Weather.h:117
UROSIntegrationGameInstance * RosInstance
Definition: Weather.h:110
FWeatherParameters CurrentWeather
Definition: Weather.h:104
static UROSHandler * GetROSHandle(const UObject *WorldContextObject)
FROSDelegate_ROState OnROSStateChanged
Definition: ROSHandler.h:81

References CurrentWeather, Default, UAgrarsenseStatics::GetROSHandle(), InitializeROSTopic(), Message, UROSHandler::OnROSStateChanged, ROSBridgeStateChanged(), ROSConnected, and RosInstance.

◆ DestroyTopic()

void AWeather::DestroyTopic ( )
private

Definition at line 153 of file Weather.cpp.

154{
155 if (WeatherTopic)
156 {
157 WeatherTopic->Unadvertise();
158 WeatherTopic->Unsubscribe();
159 WeatherTopic->MarkAsDisconnected();
160 WeatherTopic->ConditionalBeginDestroy();
161 WeatherTopic = nullptr;
162 }
163}
UTopic * WeatherTopic
Definition: Weather.h:107

References WeatherTopic.

Referenced by EndPlay(), and ROSBridgeStateChanged().

◆ EndPlay()

void AWeather::EndPlay ( const EEndPlayReason::Type  EndPlayReason)
overrideprotectedvirtual

Definition at line 47 of file Weather.cpp.

48{
49 Super::EndPlay(EndPlayReason);
50
51 UROSHandler* RosHandler = UAgrarsenseStatics::GetROSHandle(GetWorld());
52 if (RosHandler)
53 {
54 RosHandler->OnROSStateChanged.AddUniqueDynamic(this, &AWeather::ROSBridgeStateChanged);
55 }
56
57 Message.Reset();
59 RosInstance = nullptr;
60}
void DestroyTopic()
Definition: Weather.cpp:153

References DestroyTopic(), UAgrarsenseStatics::GetROSHandle(), Message, UROSHandler::OnROSStateChanged, ROSBridgeStateChanged(), and RosInstance.

◆ ExportToJSON()

FString AWeather::ExportToJSON ( const FString &  FileName)

Definition at line 121 of file Weather.cpp.

122{
124}
static FString ExportWeatherToJSON(const FString &FileName, const FWeatherParameters &WeatherParameters)

References CurrentWeather, and USimulatorJsonExporter::ExportWeatherToJSON().

Referenced by UROSCommands::HandleExportWeather().

◆ GetCurrentSeason()

ESeason AWeather::GetCurrentSeason ( )

Get current season based on current FWeatherParameters Month

Definition at line 68 of file Weather.cpp.

69{
71}
ESeason GetSeasonForMonth(const int32 Month)
Definition: Weather.cpp:73

References CurrentWeather, GetSeasonForMonth(), and FWeatherParameters::Month.

◆ GetCurrentWeather()

const FWeatherParameters & AWeather::GetCurrentWeather ( ) const
inline

Get current FWeatherParameters

Definition at line 76 of file Weather.h.

77 {
78 return CurrentWeather;
79 }

Referenced by AInstancedRenderer::BeginPlay(), ALidar::BeginPlay(), and USimulatorJsonParser::ChangeWeather().

◆ GetSeasonForMonth()

ESeason AWeather::GetSeasonForMonth ( const int32  Month)

This function takes a month as input and returns the corresponding season.

Parameters
MonthThe month for which to determine the season.
Returns
The season associated with the input month.

Definition at line 73 of file Weather.cpp.

74{
75 switch (Month)
76 {
77 case 0:
78 case 1:
79 case 2:
80 case 3:
81 case 12:
82 return ESeason::Winter;
83
84 case 4:
85 case 5:
86 return ESeason::Spring;
87
88 case 6:
89 case 7:
90 case 8:
91 return ESeason::Summer;
92
93 case 9:
94 case 10:
95 case 11:
96 return ESeason::Autumn;
97
98 default:
99 return ESeason::Summer;
100 }
101}

References Autumn, Spring, Summer, and Winter.

Referenced by GetCurrentSeason().

◆ InitializeROSTopic()

void AWeather::InitializeROSTopic ( )
private

Definition at line 126 of file Weather.cpp.

127{
129 {
130 return;
131 }
132
133 // Else create Weather ROS topic
134 WeatherTopic = NewObject<UTopic>(UTopic::StaticClass());
135 const FString TopicName = "/agrarsense/out/weather/";
136 WeatherTopic->Init(RosInstance->ROSIntegrationCore, TopicName, TEXT("std_msgs/String"));
137 WeatherTopic->Advertise();
138
139#if WITH_EDITOR
140 UE_LOG(LogTemp, Warning, TEXT("Weather.cpp: Created ROS topic. Topic name: %s"), *TopicName);
141#endif
142}

References ROSConnected, RosInstance, and WeatherTopic.

Referenced by BeginPlay(), and ROSBridgeStateChanged().

◆ ResetWeather()

void AWeather::ResetWeather ( )

Reset Weather to default

Definition at line 62 of file Weather.cpp.

63{
66}
void UpdateWeather(const FWeatherParameters &WeatherParameters, bool updateToROS)
Definition: Weather.cpp:103

References CurrentWeather, Default, and UpdateWeather().

◆ ROSBridgeStateChanged()

void AWeather::ROSBridgeStateChanged ( EROSState  ROSState)
private

Definition at line 165 of file Weather.cpp.

166{
167 switch (ROSState)
168 {
170 ROSConnected = true;
172 break;
173
175 ROSConnected = false;
176 DestroyTopic();
177 break;
178 }
179}

References Connected, DestroyTopic(), Disconnected, InitializeROSTopic(), and ROSConnected.

Referenced by BeginPlay(), and EndPlay().

◆ SendWeatherData()

void AWeather::SendWeatherData ( FWeatherParameters  Parameters)
private

Definition at line 144 of file Weather.cpp.

145{
146 if (WeatherTopic && ROSConnected && Message.IsValid())
147 {
148 Message->_Data = Parameters.ToStringHumanReadable();
149 WeatherTopic->Publish(Message);
150 }
151}
FString ToStringHumanReadable() const

References Message, ROSConnected, FWeatherParameters::ToStringHumanReadable(), and WeatherTopic.

Referenced by UpdateWeather().

◆ UpdateWeather()

void AWeather::UpdateWeather ( const FWeatherParameters WeatherParameters,
bool  updateToROS 
)

Function that send current weather data to ROS and updates weather in scene

Parameters
WeatherParameters
isSubscribed= true, ONLY when subscribe message calls this function

Definition at line 103 of file Weather.cpp.

104{
105 CurrentWeather = InWeather;
106
107 if (updateToROS)
108 {
109 AsyncTask(ENamedThreads::AnyThread, [this]()
110 {
111 // Send weather parameters to ROS, we don't care if this message will be frame or two behind,
112 // as long as it doesn't block the game thread.
114 });
115 }
116
117 // Broadcast event that weather has been changed
119}
void SendWeatherData(FWeatherParameters Parameters)
Definition: Weather.cpp:144
FLevelEventDelegate_WeatherChanged OnWeatherChanged
Definition: Weather.h:85

References CurrentWeather, OnWeatherChanged, and SendWeatherData().

Referenced by USimulatorJsonParser::ChangeWeather(), and ResetWeather().

Member Data Documentation

◆ CurrentWeather

FWeatherParameters AWeather::CurrentWeather
private

Definition at line 104 of file Weather.h.

Referenced by BeginPlay(), ExportToJSON(), GetCurrentSeason(), ResetWeather(), and UpdateWeather().

◆ Default

const FWeatherParameters AWeather::Default = { 0.0f, 0.0f, 0.1f, 0.05f, 0.0f, 0.2f, 0.0f, 15.0f, 0.0f, 0.0f, 7, 9 }
private

Definition at line 119 of file Weather.h.

Referenced by BeginPlay(), and ResetWeather().

◆ Message

TSharedPtr<ROSMessages::std_msgs::String> AWeather::Message
private

Definition at line 117 of file Weather.h.

Referenced by BeginPlay(), EndPlay(), and SendWeatherData().

◆ OnWeatherChanged

FLevelEventDelegate_WeatherChanged AWeather::OnWeatherChanged

On Weather changed event

Definition at line 85 of file Weather.h.

Referenced by AInstancedRenderer::BeginPlay(), ALidar::BeginPlay(), AInstancedRenderer::EndPlay(), ALidar::EndPlay(), and UpdateWeather().

◆ ROSConnected

bool AWeather::ROSConnected = false
private

Definition at line 112 of file Weather.h.

Referenced by BeginPlay(), InitializeROSTopic(), ROSBridgeStateChanged(), and SendWeatherData().

◆ RosInstance

UROSIntegrationGameInstance* AWeather::RosInstance = nullptr
private

Definition at line 110 of file Weather.h.

Referenced by BeginPlay(), EndPlay(), and InitializeROSTopic().

◆ WeatherPresets

TArray<FWeatherParameters> AWeather::WeatherPresets
private

Definition at line 115 of file Weather.h.

◆ WeatherTopic

UTopic* AWeather::WeatherTopic = nullptr
private

Definition at line 107 of file Weather.h.

Referenced by DestroyTopic(), InitializeROSTopic(), and SendWeatherData().


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