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 
219  void UpdateAndPublish();
220 
221 
222 
236  void Publish();
237 
238 
239 
252  template<typename TUEMessage, typename TUEStruct>
253 
254  void Publish(const TUEStruct& InMessageData)
255 
256  {
257 
258  // can't use macro in here.
259 
260  // if (IsValid(OwnerNode) && OwnerNode->State == UROS2State::Initialized && State == UROS2State::Initialized)
261 
262  if (State == UROS2State::Initialized)
263 
264  {
265 
266  // Update [TopicMessage] with [InMessageData]
267 
268  CastChecked<TUEMessage>(TopicMessage)->SetMsg(InMessageData);
269 
270 
271 
272  // Publish [TopicMessage]
273 
274  Publish();
275 
276  }
277 
278  else
279 
280  {
281 
282  UE_LOG_WITH_INFO_NAMED(LogTemp, Log, TEXT("Publisher or node is not yet initialized"));
283 
284  }
285 
286  }
287 
288 
289 
298  virtual void Destroy();
299 
300 
301 
315  void StopPublishTimer();
316 
317 
318 
332  void StartPublishTimer();
333 
334 
335 
337 
338 
343  float PublicationFrequencyHz = 1.f;
344 
345 
346 
348 
349 
354  FTopicCallback UpdateDelegate;
355 
356 
357 
373  virtual void UpdateMessage(UROS2GenericMsg* InMessage)
374 
375  {
376 
377  checkNoEntry();
378 
379  }
380 
381 
382 
396  void SetDefaultDelegates();
397 
398 
399 
415  void SetDelegates(const FTopicCallback& InUpdateDelegate);
416 
417 
418 
419 protected:
420 
422 
423 
428  FTimerHandle TimerHandle;
429 
430 
431 
433 
434  const void* PublishedMsg = nullptr;
435 
436 
437 
439 
440  rcl_publisher_t RclPublisher;
441 
442 
443 
445 
446 
451  URRTimerManager* TimerManager = nullptr;
452 
453 
454 
463  virtual void InitializeTopicComponent();
464 
465 };
466 
467 
468 
482 class RCLUE_API UROS2PublisherComponent : public UActorComponent
483 
484 {
485 
486 
487 
488 public:
489 
490 
495  UROS2Publisher* Publisher = nullptr;
496 
497 
498 
500 
501 
506  FString TopicName = TEXT("");
507 
508 
509 
511 
512 
517  TSubclassOf<UROS2Publisher> PublisherClass = UROS2Publisher::StaticClass();
518 
519 
520 
522 
523 
528  TSubclassOf<UROS2GenericMsg> MsgClass = UROS2GenericMsg::StaticClass();
529 
530 
531 
533 
534 
539  float PublicationFrequencyHz = 1.f;
540 
541 
542 
544 
545 
550  UROS2QoS QoS = UROS2QoS::Default;
551 
552 
553 
555 
556 
561  FTopicCallback UpdateDelegate;
562 
563 
564 
565  virtual void BeginPlay() override
566 
567  {
568 
569  if (Publisher == nullptr)
570 
571  {
572 
574 
575  this, TopicName, PublisherClass, MsgClass, PublicationFrequencyHz, UpdateDelegate, QoS);
576 
577  }
578 
579  else
580 
581  {
582 
583  UE_LOG_WITH_INFO(LogTemp, Warning, TEXT("Publisher is not created in BeginPlay."));
584 
585  }
586 
587  Super::BeginPlay();
588 
589  };
590 
591 
592 
604  virtual void SetParams()
605 
606  {
607 
608  if (Publisher != nullptr)
609 
610  {
611 
612  Publisher->TopicName = TopicName;
613 
614  Publisher->PublicationFrequencyHz = PublicationFrequencyHz;
615 
616  Publisher->QoS = QoS;
617 
618  }
619 
620  else
621 
622  {
623 
624  UE_LOG_WITH_INFO(LogTemp, Warning, TEXT("Publisher is not created yet."));
625 
626  }
627 
628  }
629 
630 };
631 
632 
633 
648 
649 {
650 
651 public:
652 
653  virtual void BeginPlay() override
654 
655  {
656 
657  if (Publisher == nullptr)
658 
659  {
660 
661  Publisher = UROS2Publisher::CreateLoopPublisherWithClass(this, TopicName, PublisherClass, PublicationFrequencyHz);
662 
663  }
664 
665  else
666 
667  {
668 
669  UE_LOG_WITH_INFO(LogTemp, Warning, TEXT("Publisher class is not created in BeginPlay."));
670 
671  }
672 
673  UActorComponent::BeginPlay();
674 
675  };
676 
677 };
678 
UROS2Publisher::UpdateDelegate
FTopicCallback UpdateDelegate
Delegate which is Bound with UpdateMessage by SetDefaultDelegates.
Definition: ROS2Publisher.h:354
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:561
UROS2Publisher::UpdateMessage
virtual void UpdateMessage(UROS2GenericMsg *InMessage)
Update msg. Should be implemented in child class.
Definition: ROS2Publisher.h:373
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:604
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:170
UROS2Topic::Destroy
virtual void Destroy()
Destroy topic and TopicMessage.
UROS2QoS
UROS2QoS
used to set QoS policies
Definition: rclcUtilities.h:177
UROS2Topic::QoS
UROS2QoS QoS
Quality of service.
Definition: ROS2Topic.h:216
UROS2Publisher::Publish
void Publish(const TUEStruct &InMessageData)
Publish a message.
Definition: ROS2Publisher.h:254
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:86
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:343
UROS2Publisher::TimerHandle
FTimerHandle TimerHandle
Timer handler for periodic publisher.
Definition: ROS2Publisher.h:428
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:482
UROS2CustomPublisherComponent
ROS2 Custom Publisher Component. Wrapper of UROS2Publisher for BP.
Definition: ROS2Publisher.h:647
ROS2NodeComponent.h
Class implementing ROS2 node.
UROS2Publisher::RclPublisher
rcl_publisher_t RclPublisher
ROS2 publisher.
Definition: ROS2Publisher.h:440