SMACC2
Loading...
Searching...
No Matches
cp_topic_publisher.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI 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 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)
44 {
45 this->topicName_ = topicname;
46 initialized_ = false;
47 }
48
49 virtual ~CpTopicPublisher() {}
50
51 void publish(const MessageType & msg) { pub_->publish(msg); }
52
53 void onInitialize() override;
54
55private:
56 typename rclcpp::Publisher<MessageType>::SharedPtr pub_;
57 //rclcpp::PublisherBase::SharedPtr pub_;
58
60 std::string topicName_;
61};
62
63template <typename T>
65{
66 if (!initialized_)
67 {
68 if (!queueSize) queueSize = 1;
69 if (!durability) durability = RMW_QOS_POLICY_DURABILITY_SYSTEM_DEFAULT;
70 if (!reliability) reliability = RMW_QOS_POLICY_RELIABILITY_SYSTEM_DEFAULT;
71 rclcpp::SensorDataQoS qos;
72 qos.keep_last(*queueSize);
73 qos.durability(*durability);
74 qos.reliability(*reliability);
75
76 RCLCPP_INFO_STREAM(
77 getLogger(), "[" << this->getName() << "] Publisher to topic: " << topicName_);
78
79 auto nh = this->getNode();
80 pub_ = nh->template create_publisher<T>(this->topicName_, qos);
81
82 this->initialized_ = true;
83 }
84}
85
86} // namespace components
87} // namespace smacc2
std::optional< rmw_qos_reliability_policy_t > reliability
rclcpp::Publisher< MessageType >::SharedPtr pub_
std::optional< rmw_qos_durability_policy_t > durability
void publish(const MessageType &msg)