rclUE
ROS2Publisher.h
Go to the documentation of this file.
1 
17 #pragma once
18 
19 
20 
21 #include <Components/ActorComponent.h>
22 
23 #include <CoreMinimal.h>
24 
25 
26 
27 // rclUE
28 
29 #include "Msgs/ROS2GenericMsg.h"
30 
31 #include "ROS2NodeComponent.h"
32 
33 #include "ROS2Topic.h"
34 
35 
36 
37 #include "ROS2Publisher.generated.h"
38 
39 
40 
54 class RCLUE_API UROS2Publisher : public UROS2Topic
55 
56 {
57 
58 
59 
60 public:
61 
83  static UROS2Publisher* CreateLoopPublisherWithClass(UObject* InOwner,
84 
85  const FString& InTopicName,
86 
87  const TSubclassOf<UROS2Publisher>& InPublisherClass,
88 
89  const float InPubFrequency);
90 
91 
92 
122  static UROS2Publisher* CreateLoopPublisher(UObject* InOwner,
123 
124  const FString& InTopicName,
125 
126  const TSubclassOf<UROS2Publisher>& InPublisherClass,
127 
128  const TSubclassOf<UROS2GenericMsg>& InMsgClass,
129 
130  float InPubFrequency,
131 
132  const FTopicCallback& InUpdateDelegate,
133 
134  const UROS2QoS InQoS = UROS2QoS::Default);
135 
157  static UROS2Publisher* CreatePublisherWithClass(UObject* InOwner,
158 
159  const TSubclassOf<UROS2Publisher>& InPublisherClass,
160 
161  const FString& InTopicName = TEXT(""));
162 
163 
164 
192  static UROS2Publisher* CreatePublisher(UObject* InOwner,
193 
194  const FString& InTopicName,
195 
196  const TSubclassOf<UROS2Publisher>& InPublisherClass,
197 
198  const TSubclassOf<UROS2GenericMsg>& InMsgClass,
199 
200  float InPubFrequency,
201 
202  const UROS2QoS InQoS = UROS2QoS::Default);
203 
204 
205 
228  static UROS2Publisher* CreatePublisher(UObject* InOwner,
229 
230  const FString& InTopicName,
231 
232  const TSubclassOf<UROS2Publisher>& InPublisherClass,
233 
234  const TSubclassOf<UROS2GenericMsg>& InMsgClass,
235 
236  float InPubFrequency,
237 
238  const rmw_qos_profile_t& InCustomQoS);
239 
240 
241 
255  void UpdateAndPublish();
256 
257 
258 
272  void Publish();
273 
274 
275 
288  template<typename TUEMessage, typename TUEStruct>
289 
290  void Publish(const TUEStruct& InMessageData)
291 
292  {
293 
294  // can't use macro in here.
295 
296  // if (IsValid(OwnerNode) && OwnerNode->State == UROS2State::Initialized && State == UROS2State::Initialized)
297 
298  if (State == UROS2State::Initialized)
299 
300  {
301 
302  // Update [TopicMessage] with [InMessageData]
303 
304  CastChecked<TUEMessage>(TopicMessage)->SetMsg(InMessageData);
305 
306 
307 
308  // Publish [TopicMessage]
309 
310  Publish();
311 
312  }
313 
314  else
315 
316  {
317 
318  UE_LOG_WITH_INFO_NAMED(LogTemp, Log, TEXT("Publisher or node is not yet initialized"));
319 
320  }
321 
322  }
323 
324 
325 
334  virtual void Destroy();
335 
336 
337 
351  void StopPublishTimer();
352 
353 
354 
368  void StartPublishTimer();
369 
370 
371 
373 
374 
379  float PublicationFrequencyHz = 1.f;
380 
381 
382 
384 
385 
390  FTopicCallback UpdateDelegate;
391 
392 
393 
409  virtual void UpdateMessage(UROS2GenericMsg* InMessage)
410 
411  {
412 
413  checkNoEntry();
414 
415  }
416 
417 
418 
432  void SetDefaultDelegates();
433 
434 
435 
451  void SetDelegates(const FTopicCallback& InUpdateDelegate);
452 
453 
454 
455 protected:
456 
458 
459 
464  FTimerHandle TimerHandle;
465 
466 
467 
469 
470  const void* PublishedMsg = nullptr;
471 
472 
473 
475 
476  rcl_publisher_t RclPublisher;
477 
478 
479 
481 
482 
487  URRTimerManager* TimerManager = nullptr;
488 
489 
490 
499  virtual void InitializeTopicComponent();
500 
501 };
502 
503 
504 
518 class RCLUE_API UROS2PublisherComponent : public UActorComponent
519 
520 {
521 
522 
523 
524 public:
525 
526 
531  UROS2Publisher* Publisher = nullptr;
532 
533 
534 
536 
537 
542  FString TopicName = TEXT("");
543 
544 
545 
547 
548 
553  TSubclassOf<UROS2Publisher> PublisherClass = UROS2Publisher::StaticClass();
554 
555 
556 
558 
559 
564  TSubclassOf<UROS2GenericMsg> MsgClass = UROS2GenericMsg::StaticClass();
565 
566 
567 
569 
570 
575  float PublicationFrequencyHz = 1.f;
576 
577 
578 
580 
581 
586  UROS2QoS QoS = UROS2QoS::Default;
587 
588 
589 
591 
592 
597  FTopicCallback UpdateDelegate;
598 
599 
600 
601 
606  virtual void DefaultCreatePublisher()
607 
608  {
609 
610  if (Publisher == nullptr)
611 
612  {
613 
615 
616  this, TopicName, PublisherClass, MsgClass, PublicationFrequencyHz, UpdateDelegate, QoS);
617 
618  }
619 
620  }
621 
622 
623 
624  virtual void BeginPlay() override
625 
626  {
627 
628  DefaultCreatePublisher();
629 
630  Super::BeginPlay();
631 
632  };
633 
634 
635 
647  virtual void SetParams()
648 
649  {
650 
651  if (Publisher != nullptr)
652 
653  {
654 
655  Publisher->TopicName = TopicName;
656 
657  Publisher->PublicationFrequencyHz = PublicationFrequencyHz;
658 
659  Publisher->QoS = QoS;
660 
661  }
662 
663  else
664 
665  {
666 
667  UE_LOG_WITH_INFO(LogTemp, Warning, TEXT("Publisher is not created yet."));
668 
669  }
670 
671  }
672 
673 };
674 
675 
676 
690 class RCLUE_API UROS2CustomPublisherComponent : public UActorComponent
691 
692 {
693 
694 
695 
696 public:
697 
698 
703  UROS2Publisher* Publisher = nullptr;
704 
705 
706 
708 
709 
714  FString TopicName = TEXT("");
715 
716 
717 
719 
720 
725  TSubclassOf<UROS2Publisher> PublisherClass = UROS2Publisher::StaticClass();
726 
727 
728 
730 
731 
736  float PublicationFrequencyHz = 1.f;
737 
738 
739 
740 
745  virtual void DefaultCreatePublisher()
746 
747  {
748 
749  if (Publisher == nullptr)
750 
751  {
752 
753  Publisher = UROS2Publisher::CreateLoopPublisherWithClass(this, TopicName, PublisherClass, PublicationFrequencyHz);
754 
755  }
756 
757  }
758 
759  virtual void BeginPlay() override
760 
761  {
762 
763  DefaultCreatePublisher();
764 
765  Super::BeginPlay();
766 
767  };
768 
769 };
770 
UROS2Publisher::UpdateDelegate
FTopicCallback UpdateDelegate
Delegate which is Bound with UpdateMessage by SetDefaultDelegates.
Definition: ROS2Publisher.h:390
URRTimerManager
Custom timer manager. This try to execute delegate at a given fixed rate.
Definition: rclcUtilities.h:356
UROS2PublisherComponent::UpdateDelegate
FTopicCallback UpdateDelegate
this is pass to UROS2Publisher::UpdateDelegate in #BeginPlay
Definition: ROS2Publisher.h:597
UROS2Publisher::UpdateMessage
virtual void UpdateMessage(UROS2GenericMsg *InMessage)
Update msg. Should be implemented in child class.
Definition: ROS2Publisher.h:409
UROS2Publisher
ROS2 Publisher class.
Definition: ROS2Publisher.h:54
UROS2PublisherComponent::SetParams
virtual void SetParams()
Pass this class's parameter TopicName, PublicationFrequencyHz, QoS to Publisher.
Definition: ROS2Publisher.h:647
UE_LOG_WITH_INFO
#define UE_LOG_WITH_INFO(CategoryName, Verbosity,...)
Definition: logUtilities.h:83
UROS2Topic::TopicName
FString TopicName
this information is redundant with Topic, but it's needed to initialize it
Definition: ROS2Topic.h:172
UROS2Topic::Destroy
virtual void Destroy()
Destroy topic and TopicMessage.
UROS2QoS
UROS2QoS
used to set QoS policies
Definition: rclcUtilities.h:177
UROS2CustomPublisherComponent::DefaultCreatePublisher
virtual void DefaultCreatePublisher()
Definition: ROS2Publisher.h:745
UROS2Topic::QoS
UROS2QoS QoS
Quality of service.
Definition: ROS2Topic.h:218
UROS2Publisher::Publish
void Publish(const TUEStruct &InMessageData)
Publish a message.
Definition: ROS2Publisher.h:290
UROS2Publisher::CreateLoopPublisherWithClass
static UROS2Publisher * CreateLoopPublisherWithClass(UObject *InOwner, const FString &InTopicName, const TSubclassOf< UROS2Publisher > &InPublisherClass, const float InPubFrequency)
Create a new UROS2Publisher of custom publisher class and andd to Node.
UROS2Publisher::CreateLoopPublisher
static UROS2Publisher * CreateLoopPublisher(UObject *InOwner, const FString &InTopicName, const TSubclassOf< UROS2Publisher > &InPublisherClass, const TSubclassOf< UROS2GenericMsg > &InMsgClass, float InPubFrequency, const FTopicCallback &InUpdateDelegate, const UROS2QoS InQoS=UROS2QoS::Default)
Create a Loop Publisher object.
UROS2Topic::InitializeTopicComponent
virtual void InitializeTopicComponent()
Initialize ROS2 Topic. Should be implemented in UROS2Publisher and UROS2Subscriber.
ROS2Topic.h
Class implementing ROS2 topic.
UROS2Topic
ROS2 topic class. Parent class of UROS2Publisher and UROS2Subscriber.
Definition: ROS2Topic.h:88
ROS2GenericMsg.h
This should be refactored with other generic ROS2 types (Msgs, Sensors, Actions).
UROS2Publisher::PublicationFrequencyHz
float PublicationFrequencyHz
Publish frequency. if this value > 0, this will update and publish msg periodically.
Definition: ROS2Publisher.h:379
UROS2Publisher::TimerHandle
FTimerHandle TimerHandle
Timer handler for periodic publisher.
Definition: ROS2Publisher.h:464
UE_LOG_WITH_INFO_NAMED
#define UE_LOG_WITH_INFO_NAMED(CategoryName, Verbosity,...)
Definition: logUtilities.h:139
Initialized
@ Initialized
UMETA(DisplayName = "Created"),.
Definition: rclcUtilities.h:158
UROS2GenericMsg
This should be refactored with other generic ROS2 types (Msgs, Sensors, Actions).
Definition: ROS2GenericMsg.h:46
UROS2PublisherComponent
ROS2 Publisher Component. Wrapper of UROS2Publisher for BP.
Definition: ROS2Publisher.h:518
UROS2CustomPublisherComponent
ROS2 Custom Publisher Component. Wrapper of UROS2Publisher for BP.
Definition: ROS2Publisher.h:690
ROS2NodeComponent.h
Class implementing ROS2 node.
UROS2PublisherComponent::DefaultCreatePublisher
virtual void DefaultCreatePublisher()
Definition: ROS2Publisher.h:606
UROS2Publisher::RclPublisher
rcl_publisher_t RclPublisher
ROS2 publisher.
Definition: ROS2Publisher.h:476