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 {
111 clBehavior->executeOnEntry();
112 }
113 catch (const std::exception & e)
114 {
115 RCLCPP_ERROR(
116 getLogger(),
117 "[ClientBehavior %s] Exception on Entry - continuing with next client behavior. "
118 "Exception info: "
119 "%s",
120 cbName, e.what());
121 }
123 }
124 }
125 else
126 {
127 RCLCPP_INFO(
128 getLogger(), "[Orthogonal %s] OnEntry -> empty orthogonal (no client behavior) ",
130 }
131}
132
134{
135 std::lock_guard<std::mutex> lock(this->mutex_);
136
137 if (clientBehaviors_.back().size() > 0)
138 {
139 for (auto & clBehavior : clientBehaviors_.back())
140 {
141 RCLCPP_INFO(
142 getLogger(), "[Orthogonal %s] OnExit, current Behavior: %s", orthogonalName, cbName);
143 try
144 {
146 clBehavior->executeOnExit();
147 }
148 catch (const std::exception & e)
149 {
150 RCLCPP_ERROR(
151 getLogger(),
152 "[ClientBehavior %s] Exception onExit - continuing with next client behavior. Exception "
153 "info: %s",
154 cbName, e.what());
155 }
157 }
158 }
159 else
160 {
161 RCLCPP_INFO(getLogger(), "[Orthogonal %s] OnExit", orthogonalName);
162 }
163}
164
166{
167 for (auto & clBehavior : clientBehaviors_.back())
168 {
169 clBehavior->dispose();
170 this->getStateMachine()->disconnectSmaccSignalObject((void *)clBehavior.get());
171 }
172
173 clientBehaviors_.back().clear();
174 clientBehaviors_.pop_back();
175}
176
177} // namespace smacc2
ISmaccStateMachine * getStateMachine()
void setStateMachine(ISmaccStateMachine *value)
Definition: orthogonal.cpp:32
virtual void onInitialize()
Definition: orthogonal.cpp:71
void addClientBehavior(std::shared_ptr< smacc2::ISmaccClientBehavior > clientBehavior)
Definition: orthogonal.cpp:49
std::vector< std::shared_ptr< smacc2::ISmaccClient > > clients_
void initState(ISmaccState *state)
Definition: orthogonal.cpp:73
ISmaccStateMachine * stateMachine_
std::vector< std::vector< std::shared_ptr< smacc2::ISmaccClientBehavior > > > clientBehaviors_
virtual std::string getName() const
Definition: orthogonal.cpp:81
rclcpp::Node::SharedPtr getNode()
Definition: orthogonal.cpp:47
rclcpp::Node::SharedPtr getNode()
void disconnectSmaccSignalObject(void *object)
virtual std::string getClassName()
Definition: smacc_state.cpp:26
std::string demangleSymbol()
#define orthogonalName
Definition: orthogonal.cpp:27
#define statename
Definition: orthogonal.cpp:26
#define cbName
Definition: orthogonal.cpp:28
void TRACEPOINT(spinOnce)
smacc2_client_behavior_on_exit_end
smacc2_client_behavior_on_exit_start
smacc2_client_behavior_on_entry_start
smacc2_client_behavior_on_entry_end