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 int 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:159
void InitializeROSTopic()
Definition: Weather.cpp:120
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 147 of file Weather.cpp.

148{
149 if (WeatherTopic)
150 {
151 WeatherTopic->Unadvertise();
152 WeatherTopic->Unsubscribe();
153 WeatherTopic->MarkAsDisconnected();
154 WeatherTopic->ConditionalBeginDestroy();
155 WeatherTopic = nullptr;
156 }
157}
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:147

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

◆ ExportToJSON()

FString AWeather::ExportToJSON ( const FString &  FileName)

Definition at line 115 of file Weather.cpp.

116{
118}
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 int 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 int  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 3:
78 case 4:
79 case 5:
80 return ESeason::Spring;
81
82 case 6:
83 case 7:
84 case 8:
85 return ESeason::Summer;
86
87 case 9:
88 case 10:
89 case 11:
90 return ESeason::Autumn;
91
92 default:
93 return ESeason::Winter;
94 }
95}

References Autumn, Spring, Summer, and Winter.

Referenced by GetCurrentSeason().

◆ InitializeROSTopic()

void AWeather::InitializeROSTopic ( )
private

Definition at line 120 of file Weather.cpp.

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

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:97

References CurrentWeather, Default, and UpdateWeather().

◆ ROSBridgeStateChanged()

void AWeather::ROSBridgeStateChanged ( EROSState  ROSState)
private

Definition at line 159 of file Weather.cpp.

160{
161 switch (ROSState)
162 {
164 ROSConnected = true;
166 break;
167
169 ROSConnected = false;
170 DestroyTopic();
171 break;
172 }
173}

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

Referenced by BeginPlay(), and EndPlay().

◆ SendWeatherData()

void AWeather::SendWeatherData ( FWeatherParameters  Parameters)
private

Definition at line 138 of file Weather.cpp.

139{
140 if (WeatherTopic && ROSConnected && Message.IsValid())
141 {
142 Message->_Data = Parameters.ToStringHumanReadable();
143 WeatherTopic->Publish(Message);
144 }
145}
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 97 of file Weather.cpp.

98{
99 CurrentWeather = InWeather;
100
101 if (updateToROS)
102 {
103 AsyncTask(ENamedThreads::AnyThread, [this]()
104 {
105 // Send weather parameters to ROS, we don't care if this message will be frame or two behind,
106 // as long as it doesn't block the game thread.
108 });
109 }
110
111 // Broadcast event that weather has been changed
113}
void SendWeatherData(FWeatherParameters Parameters)
Definition: Weather.cpp:138
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: