SMACC2
cb_default_multirole_sensor_behavior.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
19#include <string>
20
22
23namespace cl_multirole_sensor
24{
25template <typename ClientType>
27{
28public:
29 typedef typename ClientType::TMessageType TMessageType;
30
31 ClientType * sensor_;
32
34
35 static std::string getEventLabel()
36 {
37 // show ros message type
38 return demangleSymbol(typeid(TMessageType).name());
39 }
40
41 std::function<void()> deferedEventPropagation;
42
43 template <typename TOrthogonal, typename TSourceObject>
45 {
46 deferedEventPropagation = [this]() {
47 RCLCPP_INFO(
48 getLogger(), "[CbDefaultMultiRoleSensorBehavior] onEntry. Requires client of type '%s'",
49 demangleSymbol<ClientType>().c_str());
50
51 // just propagate the client events from this client behavior source.
52 sensor_->onMessageReceived(
55 this);
56 sensor_->onFirstMessageReceived(
59 this);
60 sensor_->onMessageTimeout(
63 this);
64 };
65 }
66
67 template <typename EvType>
68 void propagateEvent(const TMessageType & /*msg*/)
69 {
70 // TODO: copy event concept fields
71 this->postEvent<EvType>();
72 }
73
74 template <typename EvType>
76 {
77 this->postEvent<EvType>();
78 }
79
80 void onEntry() override
81 {
82 RCLCPP_INFO(
83 getLogger(), "[CbDefaultMultiRoleSensorBehavior] onEntry. Requires client of type '%s'",
84 demangleSymbol<ClientType>().c_str());
85
86 if (sensor_ == nullptr)
87 {
88 this->requiresClient(sensor_);
89 }
90
91 if (sensor_ == nullptr)
92 {
93 RCLCPP_FATAL_STREAM(
94 getLogger(),
95 "[CbDefaultMultiRoleSensorBehavior]Sensor client behavior needs a client of type: "
96 << demangleSymbol<ClientType>() << " but it is not found.");
97 }
98 else
99 {
101 RCLCPP_INFO(getLogger(), "[CbDefaultMultiRoleSensorBehavior] onEntry. sensor initialize");
102 }
103 }
104
105 void onExit() {}
106
107 virtual void onMessageCallback(const TMessageType & /*msg*/)
108 {
109 // empty to fill by sensor customization based on inheritance
110 }
111};
112} // namespace cl_multirole_sensor
void requiresClient(SmaccClientType *&storage)
std::string demangleSymbol(const std::string &name)