SMACC2
Public Member Functions | Private Member Functions | Private Attributes | List of all members
smacc2::SignalDetector Class Reference

#include <smacc_signal_detector.hpp>

Collaboration diagram for smacc2::SignalDetector:
Collaboration graph

Public Member Functions

 SignalDetector (SmaccFifoScheduler *scheduler)
 
void initialize (ISmaccStateMachine *stateMachine)
 
void setProcessorHandle (SmaccFifoScheduler::processor_handle processorHandle)
 
void runThread ()
 
void join ()
 
void stop ()
 
void pollingLoop ()
 
void pollOnce ()
 
template<typename EventType >
void postEvent (EventType *ev)
 
rclcpp::Node::SharedPtr getNode ()
 
rclcpp::Logger getLogger ()
 

Private Member Functions

void findUpdatableClientsAndComponents ()
 
void findUpdatableStateElements (ISmaccState *currentState)
 

Private Attributes

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

Detailed Description

Definition at line 29 of file smacc_signal_detector.hpp.

Constructor & Destructor Documentation

◆ SignalDetector()

smacc2::SignalDetector::SignalDetector ( SmaccFifoScheduler scheduler)

SignalDetector()

Definition at line 43 of file signal_detector.cpp.

44{
45 scheduler_ = scheduler;
46 loop_rate_hz = 20.0;
47 end_ = false;
48 initialized_ = false;
49}
SmaccFifoScheduler * scheduler_
std::atomic< bool > initialized_

References end_, initialized_, loop_rate_hz, and scheduler_.

Member Function Documentation

◆ findUpdatableClientsAndComponents()

void smacc2::SignalDetector::findUpdatableClientsAndComponents ( )
private

findUpdatableClientsAndComponents()

Definition at line 73 of file signal_detector.cpp.

74{
75 this->updatableClients_.clear();
76 for (auto pair : this->smaccStateMachine_->getOrthogonals())
77 {
78 auto & orthogonal = pair.second;
79 auto & clients = orthogonal->getClients();
80
81 for (auto & client : clients)
82 {
83 // updatable client components
84 auto updatableClient = dynamic_cast<ISmaccUpdatable *>(client.get());
85
86 if (updatableClient != nullptr)
87 {
88 RCLCPP_DEBUG_STREAM(
89 getLogger(), "Adding updatable client: " << demangleType(typeid(updatableClient)));
90 this->updatableClients_.push_back(updatableClient);
91 }
92
93 // updatable client components
94 std::vector<std::shared_ptr<ISmaccComponent>> components;
95 client->getComponents(components);
96 for (auto & componententry : components)
97 {
98 auto updatableComponent = dynamic_cast<ISmaccUpdatable *>(componententry.get());
99 if (updatableComponent != nullptr)
100 {
101 RCLCPP_DEBUG_STREAM(
102 getLogger(),
103 "Adding updatable component: " << demangleType(typeid(*updatableComponent)));
104 this->updatableClients_.push_back(updatableComponent);
105 }
106 }
107 }
108 }
109}
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 116 of file signal_detector.cpp.

117{
118 this->updatableStateElements_.clear();
119 for (auto pair : this->smaccStateMachine_->getOrthogonals())
120 {
121 auto & orthogonal = pair.second;
122 auto & behaviors = orthogonal->getClientBehaviors();
123
124 for (auto & currentBehavior : behaviors)
125 {
126 ISmaccUpdatable * updatableClientBehavior =
127 dynamic_cast<ISmaccUpdatable *>(currentBehavior.get());
128
129 if (updatableClientBehavior != nullptr)
130 {
131 RCLCPP_DEBUG_STREAM(
132 getLogger(),
133 "Adding updatable behavior: " << demangleType(typeid(updatableClientBehavior)));
134 this->updatableStateElements_.push_back(updatableClientBehavior);
135 }
136 }
137 }
138
139 auto updatableState = dynamic_cast<ISmaccUpdatable *>(currentState);
140 if (updatableState != nullptr)
141 {
142 this->updatableStateElements_.push_back(updatableState);
143 }
144
145 auto statereactors = currentState->getStateReactors();
146 for (auto & sr : statereactors)
147 {
148 ISmaccUpdatable * updatableStateReactor = dynamic_cast<ISmaccUpdatable *>(sr.get());
149 if (updatableStateReactor != nullptr)
150 {
151 RCLCPP_DEBUG_STREAM(
152 getLogger(),
153 "Adding updatable stateReactorr: " << demangleType(typeid(updatableStateReactor)));
154 this->updatableStateElements_.push_back(updatableStateReactor);
155 }
156 }
157
158 auto eventgenerators = currentState->getEventGenerators();
159 for (auto & eg : eventgenerators)
160 {
161 ISmaccUpdatable * updatableEventGenerator = dynamic_cast<ISmaccUpdatable *>(eg.get());
162 if (updatableEventGenerator != nullptr)
163 {
164 RCLCPP_DEBUG_STREAM(
165 getLogger(),
166 "Adding updatable eventGenerator: " << demangleType(typeid(updatableEventGenerator)));
167 this->updatableStateElements_.push_back(updatableEventGenerator);
168 }
169 }
170}
std::vector< ISmaccUpdatable * > updatableStateElements_

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

Referenced by pollOnce().

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 58 of file smacc_signal_detector.hpp.

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

References getNode().

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

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 51 of file signal_detector.cpp.

51{ 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 58 of file signal_detector.cpp.

59{
60 smaccStateMachine_ = stateMachine;
61 lastState_ = std::numeric_limits<unsigned long>::quiet_NaN();
63 this->getNode()->declare_parameter("signal_detector_loop_freq", this->loop_rate_hz);
64
65 initialized_ = true;
66}
std::atomic< unsigned long > lastState_

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

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 197 of file signal_detector.cpp.

197{ signalDetectorThread_.join(); }

References signalDetectorThread_.

◆ pollingLoop()

void smacc2::SignalDetector::pollingLoop ( )

pollingLoop()

Definition at line 336 of file signal_detector.cpp.

337{
338 // rclcpp::Node::SharedPtr nh("~"); // use node name as root of the parameter server
339 rclcpp::Node::SharedPtr _;
340 rclcpp::Rate r0(20);
341
342 while (!initialized_)
343 {
344 r0.sleep();
345 }
346
347 auto nh = getNode();
348
349 if (!nh->get_parameter("signal_detector_loop_freq", this->loop_rate_hz))
350 {
351 RCLCPP_WARN(
352 getLogger(),
353 "Signal detector frequency (ros param signal_detector_loop_freq) was not set, using default "
354 "frequency: "
355 "%lf",
356 this->loop_rate_hz);
357 }
358 else
359 {
360 RCLCPP_WARN(
361 getLogger(), "Signal detector frequency (ros param signal_detector_loop_freq): %lf",
362 this->loop_rate_hz);
363 }
364
365 nh->set_parameter(rclcpp::Parameter("signal_detector_loop_freq", this->loop_rate_hz));
366
367 RCLCPP_INFO_STREAM(getLogger(), "[SignalDetector] loop rate hz:" << loop_rate_hz);
368
369 rclcpp::Rate r(loop_rate_hz);
370
371 while (rclcpp::ok() && !end_)
372 {
373 pollOnce();
374 rclcpp::spin_some(nh);
375 r.sleep();
376 }
377}

References end_, getLogger(), getNode(), initialized_, loop_rate_hz, and pollOnce().

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

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

◆ pollOnce()

void smacc2::SignalDetector::pollOnce ( )

poll()

Definition at line 211 of file signal_detector.cpp.

212{
213 // precondition: smaccStateMachine_ != nullptr
214
215 //TRACEPOINT( spinOnce);
216 TRACEPOINT(spinOnce);
217
218 std::lock_guard<std::recursive_mutex> lock(smaccStateMachine_->m_mutex_);
219 try
220 {
221 //smaccStateMachine_->lockStateMachine("update behaviors");
222
223 long currentStateIndex = smaccStateMachine_->getCurrentStateCounter();
224 auto currentState = smaccStateMachine_->getCurrentState();
225
226 if (currentState != nullptr)
227 {
228 RCLCPP_INFO_THROTTLE(
229 getLogger(), *(getNode()->get_clock()), 10000,
230 "[SignalDetector] heartbeat. Current State: %s",
231 demangleType(typeid(*currentState)).c_str());
232 }
233
235 RCLCPP_DEBUG_STREAM(getLogger(), "updatable clients: " << this->updatableClients_.size());
236
237 if (this->updatableClients_.size())
238 {
239 auto node = getNode();
240 for (auto * updatableClient : this->updatableClients_)
241 {
242 auto updatableElementName = demangleType(typeid(*updatableClient)).c_str();
243 try
244 {
245 RCLCPP_DEBUG_STREAM(
246 node->get_logger(),
247 "[PollOnce] update client call: " << demangleType(typeid(*updatableClient)));
248
249 TRACEPOINT(smacc2_state_update_start, updatableElementName);
250 updatableClient->executeUpdate(smaccStateMachine_->getNode());
251 TRACEPOINT(smacc2_state_update_start, updatableElementName);
252 }
253 catch (const std::exception & e)
254 {
255 RCLCPP_ERROR_STREAM(
256 node->get_logger(),
257 "Error in updatable elemnent " << updatableElementName << ": " << e.what() << '\n');
258 }
259 }
260 }
261
262 // STATE UPDATABLE ELEMENTS
263 if (
266 this->smaccStateMachine_->stateMachineCurrentAction !=
268 this->smaccStateMachine_->stateMachineCurrentAction !=
270 {
271 // we do not update updatable elements during trasitioning or configuration of states
272 RCLCPP_DEBUG_STREAM(getLogger(), "[SignalDetector] update behaviors. checking current state");
273
274 if (currentState != nullptr)
275 {
276 RCLCPP_DEBUG_STREAM(getLogger(), "[SignalDetector] current state: " << currentStateIndex);
277 RCLCPP_DEBUG_STREAM(
278 getLogger(), "[SignalDetector] last state: " << this->lastState_.load());
279
280 if (currentStateIndex != 0)
281 {
282 if (currentStateIndex != (long)this->lastState_.load())
283 {
284 RCLCPP_DEBUG_STREAM(
285 getLogger(),
286 "[PollOnce] detected new state, refreshing updatable client "
287 "behavior table");
288 // we are in a new state, refresh the updatable client behaviors table
289 this->lastState_.store(currentStateIndex);
290 this->findUpdatableStateElements(currentState);
291 }
292
293 RCLCPP_DEBUG_STREAM(
294 getLogger(),
295 "[SignalDetector] updatable state elements: " << this->updatableStateElements_.size());
296 auto node = getNode();
297 for (auto * udpatableStateElement : this->updatableStateElements_)
298 {
299 auto updatableElementName = demangleType(typeid(*udpatableStateElement)).c_str();
300 try
301 {
302 RCLCPP_DEBUG_STREAM(
303 getLogger(),
304 "[SignalDetector] update client behavior call: " << updatableElementName);
305
306 TRACEPOINT(smacc2_state_update_start, updatableElementName);
307 udpatableStateElement->executeUpdate(smaccStateMachine_->getNode());
308 TRACEPOINT(smacc2_state_update_start, updatableElementName);
309 }
310 catch (const std::exception & e)
311 {
312 RCLCPP_ERROR_STREAM(
313 node->get_logger(),
314 "Error in updatable elemnent " << updatableElementName << ": " << e.what() << '\n');
315 }
316 }
317 }
318 }
319 }
320 }
321 catch (std::exception & ex)
322 {
323 RCLCPP_ERROR(getLogger(), "Exception during Signal Detector update loop. %s", ex.what());
324 }
325
326 auto nh = this->getNode();
327 rclcpp::spin_some(nh);
328 //smaccStateMachine_->unlockStateMachine("update behaviors");
329}
StateMachineInternalAction stateMachineCurrentAction
unsigned long getCurrentStateCounter() const
void findUpdatableStateElements(ISmaccState *currentState)
void TRACEPOINT(spinOnce)
smacc2_state_update_start

References smacc2::introspection::demangleType(), findUpdatableClientsAndComponents(), findUpdatableStateElements(), smacc2::ISmaccStateMachine::getCurrentState(), smacc2::ISmaccStateMachine::getCurrentStateCounter(), getLogger(), getNode(), smacc2::ISmaccStateMachine::getNode(), lastState_, smacc2::ISmaccStateMachine::m_mutex_, smacc2_state_update_start, smaccStateMachine_, smacc2::STATE_CONFIGURING, smacc2::STATE_EXITING, smacc2::ISmaccStateMachine::stateMachineCurrentAction, TRACEPOINT(), 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 51 of file smacc_signal_detector.hpp.

52 {
53 boost::intrusive_ptr<EventType> weakPtrEvent = ev;
54 this->scheduler_->queue_event(processorHandle_, weakPtrEvent);
55 }
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 187 of file signal_detector.cpp.

188{
189 signalDetectorThread_ = boost::thread(boost::bind(&SignalDetector::pollingLoop, this));
190}

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 177 of file signal_detector.cpp.

178{
179 processorHandle_ = processorHandle;
180}

References processorHandle_.

Referenced by smacc2::run().

Here is the caller graph for this function:

◆ stop()

void smacc2::SignalDetector::stop ( )

stop()

Definition at line 204 of file signal_detector.cpp.

204{ end_ = true; }

References end_.

Member Data Documentation

◆ end_

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

Definition at line 75 of file smacc_signal_detector.hpp.

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

◆ initialized_

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

Definition at line 77 of file smacc_signal_detector.hpp.

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

◆ lastState_

std::atomic<unsigned long> smacc2::SignalDetector::lastState_
private

Definition at line 67 of file smacc_signal_detector.hpp.

Referenced by initialize(), and pollOnce().

◆ loop_rate_hz

double smacc2::SignalDetector::loop_rate_hz
private

Definition at line 73 of file smacc_signal_detector.hpp.

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

◆ processorHandle_

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

Definition at line 85 of file smacc_signal_detector.hpp.

Referenced by postEvent(), and setProcessorHandle().

◆ scheduler_

SmaccFifoScheduler* smacc2::SignalDetector::scheduler_
private

Definition at line 83 of file smacc_signal_detector.hpp.

Referenced by postEvent(), and SignalDetector().

◆ signalDetectorThread_

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

Definition at line 87 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 79 of file smacc_signal_detector.hpp.

◆ updatableClients_

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

Definition at line 63 of file smacc_signal_detector.hpp.

Referenced by findUpdatableClientsAndComponents(), and pollOnce().

◆ updatableStateElements_

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

Definition at line 65 of file smacc_signal_detector.hpp.

Referenced by findUpdatableStateElements(), and pollOnce().


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