12#include "ROSIntegration/Classes/ROSIntegrationGameInstance.h"
13#include <geometry_msgs/Twist.h>
14#include <std_msgs/String.h>
16#include "Async/Async.h"
19UMovementSubscriber::UMovementSubscriber()
21 PrimaryComponentTick.bCanEverTick =
false;
24void UMovementSubscriber::BeginPlay()
33 RosHandle->
OnROSStateChanged.AddUniqueDynamic(
this, &UMovementSubscriber::ROSBridgeStateChanged);
37void UMovementSubscriber::OnUnregister()
39 Super::OnUnregister();
46 RosHandle->
OnROSStateChanged.RemoveDynamic(
this, &UMovementSubscriber::ROSBridgeStateChanged);
50void UMovementSubscriber::ROSBridgeStateChanged(
EROSState rosState)
64void UMovementSubscriber::CreateROSTopic()
73 if (ROSInstance && ROSInstance->IsROSConnected())
75 FString VehicleName =
"Vehicle";
78 AActor* OwnerActor = GetOwner();
83 AVehicle* VehiclePtr = Cast<AVehicle>(OwnerActor);
90 VehicleName = OwnerActor->GetName();
94 MovementTopic = NewObject<UTopic>(UTopic::StaticClass());
95 MovementTopic->Init(ROSInstance->ROSIntegrationCore, FString::Printf(TEXT(
"/agrarsense/in/vehicles/%s/movement"), *VehicleName), TEXT(
"std_msgs/String"));
96 MovementTopic->Advertise();
98 std::function<void(TSharedPtr<FROSBaseMsg>)> SubscribeCallback = [
this](TSharedPtr<FROSBaseMsg> msg) ->
void
101 auto Concrete = StaticCastSharedPtr<ROSMessages::std_msgs::String>(msg);
102 if (Concrete.IsValid())
105 TArray<FString> TempResult;
106 Concrete->_Data.ParseIntoArray(TempResult, TEXT(
","));
108 if (TempResult.Num() < 3)
112 Movement.
Throttle = TempResult[0].IsNumeric() ? FMath::Clamp(FCString::Atof(*TempResult[0]), -1 , 1) : 0.0f;
113 Movement.
Break = TempResult[1].IsNumeric() ? FMath::Clamp(FCString::Atof(*TempResult[1]), 0, 1) : 0.0f;
114 Movement.
Steering = TempResult[2].IsNumeric() ? FMath::Clamp(FCString::Atof(*TempResult[2]), -90, 90) : 0.0f;
116 AsyncTask(ENamedThreads::GameThread, [
this, Movement]()
118 OnMessageReceived.Broadcast(Movement);
125 MovementTopic->Subscribe(SubscribeCallback);
129void UMovementSubscriber::DestroyROSTopic()
133 MovementTopic->Unadvertise();
134 MovementTopic->Unsubscribe();
135 MovementTopic->MarkAsDisconnected();
136 MovementTopic->ConditionalBeginDestroy();
137 MovementTopic =
nullptr;
virtual FString GetActorID_Implementation() const override
static UROSIntegrationGameInstance * GetROSGameInstance(const UObject *WorldContextObject)
static UROSHandler * GetROSHandle(const UObject *WorldContextObject)
FROSDelegate_ROState OnROSStateChanged