SMACC2
Loading...
Searching...
No Matches
orthogonal.cpp
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
25
26#define statename stateMachine_->getCurrentState()->getClassName().c_str()
27#define orthogonalName getName().c_str()
28#define cbName clBehavior->getName().c_str()
29
30namespace smacc2
31{
33{
34 this->stateMachine_ = value;
35 this->onInitialize();
36 this->initializeClients();
37}
38
40{
41 for (auto & c : this->clients_)
42 {
43 c->initialize();
44 }
45}
46
47rclcpp::Node::SharedPtr ISmaccOrthogonal::getNode() { return this->stateMachine_->getNode(); }
48
49void ISmaccOrthogonal::addClientBehavior(std::shared_ptr<smacc2::ISmaccClientBehavior> clBehavior)
50{
51 std::lock_guard<std::mutex> lock(this->mutex_);
52
53 if (clBehavior != nullptr)
54 {
55 RCLCPP_INFO(
56 getLogger(), "[Orthogonal: %s] Adding client behavior: %s.", this->getName().c_str(),
57 clBehavior->getName().c_str());
58 clBehavior->stateMachine_ = this->getStateMachine();
59 clBehavior->currentOrthogonal = this;
60 clBehavior->currentState = clBehavior->stateMachine_->getCurrentState();
61
62 clientBehaviors_.back().push_back(clBehavior);
63 }
64 else
65 {
66 RCLCPP_INFO(
67 getLogger(), "[Orthogonal: %s] No client behaviors in this state.", this->getName().c_str());
68 }
69}
70
72
74{
75 RCLCPP_INFO(
76 getLogger(), "[Orthogonal: %s] InitState: %s.", this->getName().c_str(),
77 state->getClassName().c_str());
78 clientBehaviors_.push_back(std::vector<std::shared_ptr<smacc2::ISmaccClientBehavior>>());
79}
80
81std::string ISmaccOrthogonal::getName() const { return demangleSymbol(typeid(*this).name()); }
82
84{
85 std::lock_guard<std::mutex> lock(this->mutex_);
86
87 for (auto & clBehavior : clientBehaviors_.back())
88 {
89 RCLCPP_INFO(
90 getLogger(), "[Orthogonal: %s] runtimeConfigure(), current behavior: %s.",
91 this->getName().c_str(), clBehavior->getName().c_str());
92
93 clBehavior->runtimeConfigure();
94 }
95}
96
98{
99 std::lock_guard<std::mutex> lock(this->mutex_);
100
101 if (clientBehaviors_.back().size() > 0)
102 {
103 for (auto & clBehavior : clientBehaviors_.back())
104 {
105 RCLCPP_INFO(
106 getLogger(), "[Orthogonal: %s] OnEntry, current Behavior: %s", orthogonalName, cbName);
107
108 try
109 {
110 TRACETOOLS_TRACEPOINT(
112 clBehavior->executeOnEntry();
113 }
114 catch (const std::exception & e)
115 {
116 RCLCPP_ERROR(
117 getLogger(),
118 "[ClientBehavior: %s] Exception on Entry - continuing with next client behavior. "
119 "Exception info: "
120 "%s",
121 cbName, e.what());
122 }
124 }
125 }
126 else
127 {
128 RCLCPP_INFO(
129 getLogger(), "[Orthogonal: %s] OnEntry -> empty orthogonal (no client behavior).",
131 }
132}
133
135{
136 std::lock_guard<std::mutex> lock(this->mutex_);
137
138 if (clientBehaviors_.back().size() > 0)
139 {
140 for (auto & clBehavior : clientBehaviors_.back())
141 {
142 RCLCPP_INFO(
143 getLogger(), "[Orthogonal: %s] OnExit, current Behavior: %s", orthogonalName, cbName);
144 try
145 {
146 TRACETOOLS_TRACEPOINT(
148 clBehavior->executeOnExit();
149 }
150 catch (const std::exception & e)
151 {
152 RCLCPP_ERROR(
153 getLogger(),
154 "[ClientBehavior: %s] Exception onExit - continuing with next client behavior. Exception "
155 "info: %s",
156 cbName, e.what());
157 }
159 }
160 }
161 else
162 {
163 RCLCPP_INFO(getLogger(), "[Orthogonal %s] OnExit().", orthogonalName);
164 }
165}
166
168{
169 for (auto & clBehavior : clientBehaviors_.back())
170 {
171 clBehavior->dispose();
172 this->getStateMachine()->disconnectSmaccSignalObject((void *)clBehavior.get());
173 }
174
175 clientBehaviors_.back().clear();
176 clientBehaviors_.pop_back();
177}
178
179} // namespace smacc2
ISmaccStateMachine * getStateMachine()
void setStateMachine(ISmaccStateMachine *value)
virtual void onInitialize()
void addClientBehavior(std::shared_ptr< smacc2::ISmaccClientBehavior > clientBehavior)
std::vector< std::shared_ptr< smacc2::ISmaccClient > > clients_
void initState(ISmaccState *state)
ISmaccStateMachine * stateMachine_
std::vector< std::vector< std::shared_ptr< smacc2::ISmaccClientBehavior > > > clientBehaviors_
virtual std::string getName() const
rclcpp::Node::SharedPtr getNode()
rclcpp::Node::SharedPtr getNode()
void disconnectSmaccSignalObject(void *object)
virtual std::string getClassName()
std::string demangleSymbol()
#define orthogonalName
#define statename
#define cbName
smacc2_client_behavior_on_exit_end
smacc2_client_behavior_on_exit_start
smacc2_client_behavior_on_entry_start
smacc2_client_behavior_on_entry_end