SMACC2
Loading...
Searching...
No Matches
cl_generic_sensor.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#pragma once
16
18#include <optional>
19#include <rclcpp/rclcpp.hpp>
22
24{
25using namespace smacc2;
26
27// Pure component-based multirole sensor client
28// This client follows the ClKeyboard pattern where all functionality
29// is implemented through composable components
30template <typename MessageType>
32{
33public:
34 typedef MessageType TMessageType;
35
36 // Optional topic name - if not set, must be configured during component initialization
37 std::optional<std::string> topicName_;
38
39 // Optional timeout duration - if not set, timeout functionality is disabled
40 std::optional<rclcpp::Duration> timeout_;
41
43
44 // Constructor with topic name
45 ClGenericSensor(std::string topicName) : topicName_(topicName) {}
46
47 // Constructor with topic name and timeout
48 ClGenericSensor(std::string topicName, rclcpp::Duration timeout)
49 : topicName_(topicName), timeout_(timeout)
50 {
51 }
52
53 virtual ~ClGenericSensor() {}
54
55 // Component-based architecture initialization
56 // This method creates and configures the components that provide
57 // the client's functionality
58 template <typename TOrthogonal, typename TClient>
60 {
61 RCLCPP_INFO(getLogger(), "[ClGenericSensor] Initializing component-based sensor client");
62
63 // Create the topic subscriber component
64 // This component handles ROS topic subscription and posts SMACC events
65 if (!topicName_)
66 {
67 RCLCPP_ERROR(
68 getLogger(),
69 "[ClGenericSensor] Topic name not set. Please configure topicName_ before "
70 "initialization.");
71 return;
72 }
73
74 this->createComponent<
76 *topicName_);
77
78 RCLCPP_INFO(
79 getLogger(), "[ClGenericSensor] Created CpTopicSubscriber for topic: %s",
80 topicName_->c_str());
81
82 // Create the message timeout component (optional - only if timeout is configured)
83 // This component monitors message reception and posts timeout events
84 if (timeout_)
85 {
86 auto timeoutComponent = this->createComponent<
89
90 // Configure the timeout duration
91 timeoutComponent->timeout_ = timeout_;
92
93 RCLCPP_INFO(
94 getLogger(), "[ClGenericSensor] Created CpMessageTimeout with duration: %f seconds",
95 timeout_->seconds());
96 }
97 else
98 {
99 RCLCPP_INFO(
100 getLogger(), "[ClGenericSensor] Timeout not configured - watchdog functionality disabled");
101 }
102 }
103
104 // Convenience method to set topic name after construction
105 void setTopicName(const std::string & topicName) { topicName_ = topicName; }
106
107 // Convenience method to set timeout after construction
108 void setTimeout(const rclcpp::Duration & timeout) { timeout_ = timeout; }
109};
110
111} // namespace cl_generic_sensor
void setTimeout(const rclcpp::Duration &timeout)
void setTopicName(const std::string &topicName)
std::optional< std::string > topicName_
std::optional< rclcpp::Duration > timeout_
ClGenericSensor(std::string topicName, rclcpp::Duration timeout)
rclcpp::Logger getLogger()
SmaccComponentType * createComponent(TArgs... targs)