SMACC2
Loading...
Searching...
No Matches
smacc2::SignalDetector Class Reference

#include <smacc_signal_detector.hpp>

Collaboration diagram for smacc2::SignalDetector:
Collaboration graph

Public Member Functions

 SignalDetector (SmaccFifoScheduler *scheduler, ExecutionModel executionModel=ExecutionModel::SINGLE_THREAD_SPINNER)
 
void initialize (ISmaccStateMachine *stateMachine)
 
void setProcessorHandle (SmaccFifoScheduler::processor_handle processorHandle)
 
void runThread ()
 
void join ()
 
void stop ()
 
void terminateScheduler ()
 
void pollingLoop ()
 
template<typename EventType >
void postEvent (EventType *ev)
 
rclcpp::Node::SharedPtr getNode ()
 
rclcpp::Logger getLogger ()
 
void notifyStateConfigured (ISmaccState *currentState)
 
void notifyStateExited (ISmaccState *currentState)
 
void notifyRosInitialized ()
 

Private Member Functions

void pollOnce ()
 
void findUpdatableClientsAndComponents ()
 
void findUpdatableStateElements (ISmaccState *currentState)
 

Private Attributes

ISmaccStateMachinesmaccStateMachine_
 
std::vector< ISmaccUpdatable * > updatableClients_
 
std::vector< std::vector< ISmaccUpdatable * > > updatableStateElements_
 
std::atomic< int64_t > lastState_
 
double loop_rate_hz
 
std::atomic< boolend_
 
std::atomic< boolinitialized_
 
std::atomic< boolrosInitialized_
 
rclcpp::Publisher< smacc2_msgs::msg::SmaccStatus >::SharedPtr statusPub_
 
SmaccFifoSchedulerscheduler_
 
SmaccFifoScheduler::processor_handle processorHandle_
 
boost::thread signalDetectorThread_
 
ExecutionModel executionModel_
 

Detailed Description

Definition at line 37 of file smacc_signal_detector.hpp.

Constructor & Destructor Documentation

◆ SignalDetector()

smacc2::SignalDetector::SignalDetector ( SmaccFifoScheduler * scheduler,
ExecutionModel executionModel = ExecutionModel::SINGLE_THREAD_SPINNER )

SignalDetector()

Definition at line 61 of file signal_detector.cpp.

62{
63 scheduler_ = scheduler;
64 loop_rate_hz = 20.0;
65 end_ = false;
66 initialized_ = false;
67 rosInitialized_ = false;
68 executionModel_ = executionModel;
69}
SmaccFifoScheduler * scheduler_
std::atomic< bool > rosInitialized_
std::atomic< bool > initialized_

References end_, executionModel_, initialized_, loop_rate_hz, rosInitialized_, and scheduler_.

Member Function Documentation

◆ findUpdatableClientsAndComponents()

void smacc2::SignalDetector::findUpdatableClientsAndComponents ( )
private

findUpdatableClientsAndComponents()

Definition at line 101 of file signal_detector.cpp.

102{
103 this->updatableClients_.clear();
104 for (auto pair : this->smaccStateMachine_->getOrthogonals())
105 {
106 auto & orthogonal = pair.second;
107 auto & clients = orthogonal->getClients();
108
109 for (auto & client : clients)
110 {
111 // updatable client components
112 auto updatableClient = dynamic_cast<ISmaccUpdatable *>(client.get());
113
114 if (updatableClient != nullptr)
115 {
116 RCLCPP_DEBUG_STREAM(
117 getLogger(), "Adding updatable client: " << demangleType(typeid(updatableClient)));
118 this->updatableClients_.push_back(updatableClient);
119 }
120
121 // updatable client components
122 std::vector<std::shared_ptr<ISmaccComponent>> components;
123 client->getComponents(components);
124 for (auto & componententry : components)
125 {
126 auto updatableComponent = dynamic_cast<ISmaccUpdatable *>(componententry.get());
127 if (updatableComponent != nullptr)
128 {
129 RCLCPP_DEBUG_STREAM(
130 getLogger(),
131 "Adding updatable component: " << demangleType(typeid(*updatableComponent)));
132 this->updatableClients_.push_back(updatableComponent);
133 }
134 }
135 }
136 }
137}
const std::map< std::string, std::shared_ptr< smacc2::ISmaccOrthogonal > > & getOrthogonals() const
std::vector< ISmaccUpdatable * > updatableClients_
ISmaccStateMachine * smaccStateMachine_
std::string demangleType(const std::type_info *tinfo)

References smacc2::introspection::demangleType(), getLogger(), smacc2::ISmaccStateMachine::getOrthogonals(), smaccStateMachine_, and updatableClients_.

Referenced by initialize(), and pollOnce().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ findUpdatableStateElements()

void smacc2::SignalDetector::findUpdatableStateElements ( ISmaccState * currentState)
private

findUpdatableClientBehaviors()

Definition at line 144 of file signal_detector.cpp.

145{
146 updatableStateElements_.push_back(std::vector<ISmaccUpdatable *>());
147
148 auto & updatableElements = updatableStateElements_.back();
149
150 for (auto pair : this->smaccStateMachine_->getOrthogonals())
151 {
152 auto & orthogonal = pair.second;
153 auto & behaviors = orthogonal->getClientBehaviors().back();
154
155 for (auto & currentBehavior : behaviors)
156 {
157 ISmaccUpdatable * updatableClientBehavior =
158 dynamic_cast<ISmaccUpdatable *>(currentBehavior.get());
159
160 if (updatableClientBehavior != nullptr)
161 {
162 RCLCPP_DEBUG_STREAM(
163 getLogger(),
164 "Adding updatable behavior: " << demangleType(typeid(updatableClientBehavior)));
165 updatableElements.push_back(updatableClientBehavior);
166 }
167 }
168 }
169
170 auto updatableState = dynamic_cast<ISmaccUpdatable *>(currentState);
171 if (updatableState != nullptr)
172 {
173 updatableElements.push_back(updatableState);
174 }
175
176 auto statereactors = currentState->getStateReactors();
177 for (auto & sr : statereactors)
178 {
179 ISmaccUpdatable * updatableStateReactor = dynamic_cast<ISmaccUpdatable *>(sr.get());
180 if (updatableStateReactor != nullptr)
181 {
182 RCLCPP_DEBUG_STREAM(
183 getLogger(),
184 "Adding updatable stateReactorr: " << demangleType(typeid(updatableStateReactor)));
185 updatableElements.push_back(updatableStateReactor);
186 }
187 }
188
189 auto eventgenerators = currentState->getEventGenerators();
190 for (auto & eg : eventgenerators)
191 {
192 ISmaccUpdatable * updatableEventGenerator = dynamic_cast<ISmaccUpdatable *>(eg.get());
193 if (updatableEventGenerator != nullptr)
194 {
195 RCLCPP_DEBUG_STREAM(
196 getLogger(),
197 "Adding updatable eventGenerator: " << demangleType(typeid(updatableEventGenerator)));
198 updatableElements.push_back(updatableEventGenerator);
199 }
200 }
201}
std::vector< std::vector< ISmaccUpdatable * > > updatableStateElements_

References smacc2::introspection::demangleType(), smacc2::ISmaccState::getEventGenerators(), getLogger(), smacc2::ISmaccStateMachine::getOrthogonals(), smacc2::ISmaccState::getStateReactors(), smaccStateMachine_, and updatableStateElements_.

Referenced by notifyStateConfigured().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getLogger()

rclcpp::Logger smacc2::SignalDetector::getLogger ( )
inline

Definition at line 68 of file smacc_signal_detector.hpp.

68{ return getNode()->get_logger(); }
rclcpp::Node::SharedPtr getNode()

References getNode().

Referenced by findUpdatableClientsAndComponents(), findUpdatableStateElements(), notifyRosInitialized(), pollingLoop(), pollOnce(), and terminateScheduler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getNode()

rclcpp::Node::SharedPtr smacc2::SignalDetector::getNode ( )

Definition at line 71 of file signal_detector.cpp.

71{ return this->smaccStateMachine_->getNode(); }
rclcpp::Node::SharedPtr getNode()

References smacc2::ISmaccStateMachine::getNode(), and smaccStateMachine_.

Referenced by getLogger(), initialize(), pollingLoop(), and pollOnce().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initialize()

void smacc2::SignalDetector::initialize ( ISmaccStateMachine * stateMachine)

initialize()

Definition at line 78 of file signal_detector.cpp.

79{
80 smaccStateMachine_ = stateMachine;
81 lastState_ = std::numeric_limits<int64_t>::quiet_NaN();
83 this->getNode()->declare_parameter("signal_detector_loop_freq", this->loop_rate_hz);
84
85 auto context = getNode()->get_node_base_interface()->get_context();
86 context->on_shutdown(
87 [this]()
88 {
89 this->stop();
90 this->terminateScheduler();
91 });
92
93 initialized_ = true;
94}
std::atomic< int64_t > lastState_

References findUpdatableClientsAndComponents(), getNode(), initialized_, lastState_, loop_rate_hz, smaccStateMachine_, stop(), and terminateScheduler().

Referenced by smacc2::ISmaccStateMachine::ISmaccStateMachine().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ join()

void smacc2::SignalDetector::join ( )

join()

Definition at line 244 of file signal_detector.cpp.

244{ signalDetectorThread_.join(); }

References signalDetectorThread_.

◆ notifyRosInitialized()

void smacc2::SignalDetector::notifyRosInitialized ( )

Definition at line 213 of file signal_detector.cpp.

214{
215 RCLCPP_INFO(getLogger(), "[SignalDetector] ROS initialization complete, enabling polling loop");
216 rosInitialized_ = true;
217}

References getLogger(), and rosInitialized_.

Referenced by smacc2::ISmaccStateMachine::initializeROS().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ notifyStateConfigured()

void smacc2::SignalDetector::notifyStateConfigured ( ISmaccState * currentState)

Definition at line 203 of file signal_detector.cpp.

204{
205 this->findUpdatableStateElements(currentState);
206}
void findUpdatableStateElements(ISmaccState *currentState)

References findUpdatableStateElements().

Referenced by smacc2::ISmaccStateMachine::notifyOnRuntimeConfigurationFinished().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ notifyStateExited()

void smacc2::SignalDetector::notifyStateExited ( ISmaccState * currentState)

Definition at line 208 of file signal_detector.cpp.

209{
210 this->updatableStateElements_.pop_back();
211}

References updatableStateElements_.

Referenced by smacc2::ISmaccStateMachine::notifyOnStateExited().

Here is the caller graph for this function:

◆ pollingLoop()

void smacc2::SignalDetector::pollingLoop ( )

pollingLoop()

Definition at line 353 of file signal_detector.cpp.

354{
355 // rclcpp::Node::SharedPtr nh("~"); // use node name as root of the parameter server
356 rclcpp::Node::SharedPtr _;
357 rclcpp::Rate r0(20);
358
359 // Wait for both SignalDetector::initialize() (called from ISmaccStateMachine constructor)
360 // and ROS initialization to complete (initializeROS called from initiate_impl).
361 // This ensures orthogonals, clients, and ROS objects are fully initialized
362 // before we start polling and accessing them.
363 while ((!initialized_ || !rosInitialized_) && rclcpp::ok() && !end_)
364 {
365 r0.sleep();
366 }
367
368 if (!rclcpp::ok() || end_)
369 {
370 RCLCPP_INFO(getLogger(), "[SignalDetector] Shutdown requested before initialization completed");
371 return;
372 }
373
374 auto nh = getNode();
375
376 if (!nh->get_parameter("signal_detector_loop_freq", this->loop_rate_hz))
377 {
378 RCLCPP_WARN(
379 getLogger(),
380 "Signal Detector frequency (ros param signal_detector_loop_freq) was not set, using default "
381 "frequency: "
382 "%lf",
383 this->loop_rate_hz);
384 }
385 else
386 {
387 RCLCPP_WARN(
388 getLogger(), "Signal Detector frequency (ros param signal_detector_loop_freq): %lf",
389 this->loop_rate_hz);
390 }
391
392 nh->set_parameter(rclcpp::Parameter("signal_detector_loop_freq", this->loop_rate_hz));
393
394 RCLCPP_INFO_STREAM(getLogger(), "[SignalDetector] Loop rate hz:" << loop_rate_hz);
395
397 {
398 RCLCPP_INFO_STREAM(getLogger(), "[SignalDetector] Running in single-threaded mode.");
399
400 rclcpp::Rate r(loop_rate_hz);
401 while (rclcpp::ok() && !end_)
402 {
403 RCLCPP_INFO_STREAM_THROTTLE(
404 getLogger(), *getNode()->get_clock(), 10000, "[SignalDetector] Heartbeat");
405 pollOnce();
406 rclcpp::spin_some(nh);
407 r.sleep();
408 }
409 }
410 else
411 {
412 RCLCPP_INFO_STREAM(getLogger(), "[SignalDetector] Running in multi-threaded mode.");
413
414 rclcpp::executors::MultiThreadedExecutor executor;
415 executor.add_node(nh);
416 executor.spin();
417 }
418}

References end_, executionModel_, getLogger(), getNode(), initialized_, loop_rate_hz, pollOnce(), rosInitialized_, and smacc2::SINGLE_THREAD_SPINNER.

Referenced by smacc2::run(), smacc2::run_async(), and runThread().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pollOnce()

void smacc2::SignalDetector::pollOnce ( )
private

poll()

Definition at line 272 of file signal_detector.cpp.

273{
274 // precondition: smaccStateMachine_ != nullptr
275
276 TRACETOOLS_TRACEPOINT(spinOnce);
277
278 std::lock_guard<std::recursive_mutex> lock(smaccStateMachine_->m_mutex_);
279 try
280 {
282 RCLCPP_DEBUG_STREAM(getLogger(), "Updatable clients: " << this->updatableClients_.size());
283
284 if (this->updatableClients_.size())
285 {
286 auto node = getNode();
287 for (auto * updatableClient : this->updatableClients_)
288 {
289 auto updatableElementNameStr = demangleType(typeid(*updatableClient));
290 auto updatableElementName = updatableElementNameStr.c_str();
291 try
292 {
293 RCLCPP_DEBUG_STREAM(
294 node->get_logger(),
295 "[PollOnce] update client call: " << demangleType(typeid(*updatableClient)));
296
297 TRACETOOLS_TRACEPOINT(smacc2_state_update_start, updatableElementName);
298 updatableClient->executeUpdate(smaccStateMachine_->getNode());
299 TRACETOOLS_TRACEPOINT(smacc2_state_update_start, updatableElementName);
300 }
301 catch (const std::exception & e)
302 {
303 RCLCPP_ERROR_STREAM(
304 node->get_logger(),
305 "Error in updatable elemnent " << updatableElementName << ": " << e.what() << '\n');
306 }
307 }
308 }
309
310 // STATE UPDATABLE ELEMENTS
311 if (
314 this->smaccStateMachine_->stateMachineCurrentAction !=
316 this->smaccStateMachine_->stateMachineCurrentAction !=
318 this->smaccStateMachine_->stateMachineCurrentAction !=
320 {
321 RCLCPP_DEBUG_STREAM(
322 getLogger(), "Updatable states: " << this->updatableStateElements_.size());
323
324 for (auto stateElement : this->updatableStateElements_)
325 {
326 for (auto * udpatableStateElement : stateElement)
327 {
328 std::string updatableElementName = demangleType(typeid(*udpatableStateElement));
329 auto updatableElementNameCstr = updatableElementName.c_str();
330
331 RCLCPP_DEBUG_STREAM(
332 getLogger(), "pollOnce update client behavior call: "
333 << demangleType(typeid(*udpatableStateElement)));
334 TRACETOOLS_TRACEPOINT(smacc2_state_update_start, updatableElementNameCstr);
335
336 udpatableStateElement->executeUpdate(smaccStateMachine_->getNode());
337 TRACETOOLS_TRACEPOINT(smacc2_state_update_start, updatableElementNameCstr);
338 }
339 }
340 }
341 }
342 catch (std::exception & ex)
343 {
344 RCLCPP_ERROR(getLogger(), "Exception during Signal Detector update loop. %s.", ex.what());
345 }
346}
StateMachineInternalAction stateMachineCurrentAction
smacc2_state_update_start

References smacc2::introspection::demangleType(), findUpdatableClientsAndComponents(), getLogger(), smacc2::ISmaccStateMachine::getNode(), getNode(), smacc2::ISmaccStateMachine::m_mutex_, smacc2_state_update_start, smaccStateMachine_, smacc2::STATE_CONFIGURING, smacc2::STATE_ENTERING, smacc2::STATE_EXITING, smacc2::ISmaccStateMachine::stateMachineCurrentAction, smacc2::TRANSITIONING, updatableClients_, and updatableStateElements_.

Referenced by pollingLoop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ postEvent()

template<typename EventType >
void smacc2::SignalDetector::postEvent ( EventType * ev)
inline

Definition at line 61 of file smacc_signal_detector.hpp.

62 {
63 boost::intrusive_ptr<EventType> weakPtrEvent = ev;
64 this->scheduler_->queue_event(processorHandle_, weakPtrEvent);
65 }
SmaccFifoScheduler::processor_handle processorHandle_

References processorHandle_, and scheduler_.

Referenced by smacc2::ISmaccStateMachine::postEvent().

Here is the caller graph for this function:

◆ runThread()

void smacc2::SignalDetector::runThread ( )

runThread()

Definition at line 234 of file signal_detector.cpp.

235{
236 signalDetectorThread_ = boost::thread(boost::bind(&SignalDetector::pollingLoop, this));
237}

References pollingLoop(), and signalDetectorThread_.

Here is the call graph for this function:

◆ setProcessorHandle()

void smacc2::SignalDetector::setProcessorHandle ( SmaccFifoScheduler::processor_handle processorHandle)

setProcessorHandle()

Definition at line 224 of file signal_detector.cpp.

225{
226 processorHandle_ = processorHandle;
227}

References processorHandle_.

Referenced by smacc2::run(), and smacc2::run_async().

Here is the caller graph for this function:

◆ stop()

void smacc2::SignalDetector::stop ( )

stop()

Definition at line 251 of file signal_detector.cpp.

251{ end_ = true; }

References end_.

Referenced by initialize(), and smacc2::onSignalShutdown().

Here is the caller graph for this function:

◆ terminateScheduler()

void smacc2::SignalDetector::terminateScheduler ( )

terminateScheduler()

Definition at line 258 of file signal_detector.cpp.

259{
260 if (scheduler_)
261 {
262 RCLCPP_INFO(getLogger(), "[SignalDetector] Terminating scheduler...");
263 scheduler_->terminate();
264 }
265}

References getLogger(), and scheduler_.

Referenced by initialize(), and smacc2::run().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ end_

std::atomic<bool> smacc2::SignalDetector::end_
private

Definition at line 95 of file smacc_signal_detector.hpp.

Referenced by pollingLoop(), SignalDetector(), and stop().

◆ executionModel_

ExecutionModel smacc2::SignalDetector::executionModel_
private

Definition at line 112 of file smacc_signal_detector.hpp.

Referenced by pollingLoop(), and SignalDetector().

◆ initialized_

std::atomic<bool> smacc2::SignalDetector::initialized_
private

Definition at line 97 of file smacc_signal_detector.hpp.

Referenced by initialize(), pollingLoop(), and SignalDetector().

◆ lastState_

std::atomic<int64_t> smacc2::SignalDetector::lastState_
private

Definition at line 86 of file smacc_signal_detector.hpp.

Referenced by initialize().

◆ loop_rate_hz

double smacc2::SignalDetector::loop_rate_hz
private

Definition at line 93 of file smacc_signal_detector.hpp.

Referenced by initialize(), pollingLoop(), and SignalDetector().

◆ processorHandle_

SmaccFifoScheduler::processor_handle smacc2::SignalDetector::processorHandle_
private

Definition at line 108 of file smacc_signal_detector.hpp.

Referenced by postEvent(), and setProcessorHandle().

◆ rosInitialized_

std::atomic<bool> smacc2::SignalDetector::rosInitialized_
private

Definition at line 100 of file smacc_signal_detector.hpp.

Referenced by notifyRosInitialized(), pollingLoop(), and SignalDetector().

◆ scheduler_

SmaccFifoScheduler* smacc2::SignalDetector::scheduler_
private

Definition at line 106 of file smacc_signal_detector.hpp.

Referenced by postEvent(), SignalDetector(), and terminateScheduler().

◆ signalDetectorThread_

boost::thread smacc2::SignalDetector::signalDetectorThread_
private

Definition at line 110 of file smacc_signal_detector.hpp.

Referenced by join(), and runThread().

◆ smaccStateMachine_

ISmaccStateMachine* smacc2::SignalDetector::smaccStateMachine_
private

◆ statusPub_

rclcpp::Publisher<smacc2_msgs::msg::SmaccStatus>::SharedPtr smacc2::SignalDetector::statusPub_
private

Definition at line 102 of file smacc_signal_detector.hpp.

◆ updatableClients_

std::vector<ISmaccUpdatable *> smacc2::SignalDetector::updatableClients_
private

Definition at line 83 of file smacc_signal_detector.hpp.

Referenced by findUpdatableClientsAndComponents(), and pollOnce().

◆ updatableStateElements_

std::vector<std::vector<ISmaccUpdatable *> > smacc2::SignalDetector::updatableStateElements_
private

The documentation for this class was generated from the following files: