12#include "ROSIntegration/Classes/ROSIntegrationGameInstance.h"
13#include <geometry_msgs/Twist.h>
15#include "Async/Async.h"
18UBoomMovementSubscriber::UBoomMovementSubscriber()
20 PrimaryComponentTick.bCanEverTick =
false;
23void UBoomMovementSubscriber::BeginPlay()
32 RosHandle->
OnROSStateChanged.AddUniqueDynamic(
this, &UBoomMovementSubscriber::ROSBridgeStateChanged);
36void UBoomMovementSubscriber::OnUnregister()
38 Super::OnUnregister();
45 RosHandle->
OnROSStateChanged.RemoveDynamic(
this, &UBoomMovementSubscriber::ROSBridgeStateChanged);
49void UBoomMovementSubscriber::ROSBridgeStateChanged(
EROSState rosState)
63void UBoomMovementSubscriber::CreateROSTopic()
65 if (BoomMovementTopic)
72 if (ROSInstance && ROSInstance->IsROSConnected())
74 FString VehicleName =
"Vehicle";
77 AActor* OwnerActor = GetOwner();
82 AVehicle* VehiclePtr = Cast<AVehicle>(OwnerActor);
89 VehicleName = OwnerActor->GetName();
93 BoomMovementTopic = NewObject<UTopic>(UTopic::StaticClass());
94 BoomMovementTopic->Init(ROSInstance->ROSIntegrationCore, FString::Printf(TEXT(
"/agrarsense/in/vehicles/%s/boom_movement"), *VehicleName), TEXT(
"geometry_msgs/Twist"));
95 BoomMovementTopic->Advertise();
98 std::function<void(TSharedPtr<FROSBaseMsg>)> SubscribeCallback = [
this](TSharedPtr<FROSBaseMsg> msg) ->
void
100 auto Concrete = StaticCastSharedPtr<ROSMessages::geometry_msgs::Twist>(msg);
101 if (Concrete.IsValid())
104 Movement.
linear = FVector(Concrete->linear.x, Concrete->linear.y, Concrete->linear.z);
105 Movement.
angular = FVector(Concrete->angular.x, Concrete->angular.y, Concrete->angular.z);
107 AsyncTask(ENamedThreads::GameThread, [
this, Movement]()
109 OnBoomMessageReceived.Broadcast(Movement);
116 BoomMovementTopic->Subscribe(SubscribeCallback);
120void UBoomMovementSubscriber::DestroyROSTopic()
122 if (BoomMovementTopic)
124 BoomMovementTopic->Unadvertise();
125 BoomMovementTopic->Unsubscribe();
126 BoomMovementTopic->MarkAsDisconnected();
127 BoomMovementTopic->ConditionalBeginDestroy();
128 BoomMovementTopic =
nullptr;
virtual FString GetActorID_Implementation() const override
static UROSIntegrationGameInstance * GetROSGameInstance(const UObject *WorldContextObject)
static UROSHandler * GetROSHandle(const UObject *WorldContextObject)
FROSDelegate_ROState OnROSStateChanged