SMACC2
Loading...
Searching...
No Matches
cp_topic_publisher.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
22#include <optional>
23#include <rclcpp/rclcpp.hpp>
24#include <smacc2/common.hpp>
25#include <smacc2/component.hpp>
26
27namespace smacc2
28{
29namespace client_core_components
30{
31using namespace smacc2::default_events;
32
33template <typename MessageType>
35{
36public:
37 std::optional<int> queueSize;
38 std::optional<rmw_qos_durability_policy_t> durability;
39 std::optional<rmw_qos_reliability_policy_t> reliability;
40
41 typedef MessageType TMessageType;
42
43 CpTopicPublisher(std::string topicname) : initialized_(false), topicName_(std::move(topicname)) {}
44
45 virtual ~CpTopicPublisher() {}
46
47 void publish(const MessageType & msg) { pub_->publish(msg); }
48
49 void onInitialize() override;
50
51private:
52 typename rclcpp::Publisher<MessageType>::SharedPtr pub_;
53 //rclcpp::PublisherBase::SharedPtr pub_;
54
56 std::string topicName_;
57};
58
59template <typename T>
61{
62 if (!initialized_)
63 {
64 if (!queueSize) queueSize = 1;
65 if (!durability) durability = RMW_QOS_POLICY_DURABILITY_SYSTEM_DEFAULT;
66 if (!reliability) reliability = RMW_QOS_POLICY_RELIABILITY_SYSTEM_DEFAULT;
67 rclcpp::SensorDataQoS qos;
68 qos.keep_last(*queueSize);
69 qos.durability(*durability);
70 qos.reliability(*reliability);
71
72 RCLCPP_INFO_STREAM(
73 getLogger(), "[" << this->getName() << "] Publisher to topic: " << topicName_);
74
75 auto nh = this->getNode();
76 pub_ = nh->template create_publisher<T>(this->topicName_, qos);
77
78 this->initialized_ = true;
79 }
80}
81
82} // namespace client_core_components
83} // namespace smacc2
std::optional< rmw_qos_durability_policy_t > durability
rclcpp::Publisher< MessageType >::SharedPtr pub_
std::optional< rmw_qos_reliability_policy_t > reliability