SMACC
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
smacc::SignalDetector Class Reference

#include <smacc_signal_detector.h>

Collaboration diagram for smacc::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 pollingLoop ()
 
template<typename EventType >
void postEvent (EventType *ev)
 
void notifyStateConfigured (ISmaccState *currentState)
 
void notifyStateExited (ISmaccState *currentState)
 

Private Member Functions

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

Private Attributes

ISmaccStateMachinesmaccStateMachine_
 
std::vector< ISmaccUpdatable * > updatableClients_
 
std::vector< std::vector< ISmaccUpdatable * > > updatableStateElements_
 
std::atomic< uint64_t > lastState_
 
double loop_rate_hz
 
std::atomic< boolend_
 
std::atomic< boolinitialized_
 
ros::NodeHandle nh_
 
ros::Publisher statusPub_
 
SmaccFifoSchedulerscheduler_
 
SmaccFifoScheduler::processor_handle processorHandle_
 
boost::thread signalDetectorThread_
 
ExecutionModel executionModel_
 

Detailed Description

Definition at line 22 of file smacc_signal_detector.h.

Constructor & Destructor Documentation

◆ SignalDetector()

smacc::SignalDetector::SignalDetector ( SmaccFifoScheduler scheduler,
ExecutionModel  executionModel = ExecutionModel::SINGLE_THREAD_SPINNER 
)

SignalDetector()

Definition at line 18 of file signal_detector.cpp.

19 : end_(false),
20 initialized_(false),
21 executionModel_(executionModel)
22{
23 scheduler_ = scheduler;
24 loop_rate_hz = 20.0;
25}
std::atomic< bool > initialized_
std::atomic< bool > end_
SmaccFifoScheduler * scheduler_

References loop_rate_hz, and scheduler_.

Member Function Documentation

◆ findUpdatableClients()

void smacc::SignalDetector::findUpdatableClients ( )
private

findUpdatableClients()

Definition at line 45 of file signal_detector.cpp.

46{
47 this->updatableClients_.clear();
48 for (auto pair : this->smaccStateMachine_->getOrthogonals())
49 {
50 auto &orthogonal = pair.second;
51 auto &clients = orthogonal->getClients();
52
53 for (auto &client : clients)
54 {
55 // updatable client components
56 auto updatableClient = dynamic_cast<ISmaccUpdatable *>(client.get());
57
58 if (updatableClient != nullptr)
59 {
60 ROS_DEBUG_STREAM("Adding updatable client: " << demangleType(typeid(updatableClient)));
61 this->updatableClients_.push_back(updatableClient);
62 }
63
64 // updatable client components
65 std::vector<std::shared_ptr<ISmaccComponent>> components;
66 client->getComponents(components);
67 for (auto &componententry : components)
68 {
69 auto updatableComponent = dynamic_cast<ISmaccUpdatable *>(componententry.get());
70 if (updatableComponent != nullptr)
71 {
72 ROS_DEBUG_STREAM("Adding updatable component: " << demangleType(typeid(*updatableComponent)));
73 this->updatableClients_.push_back(updatableComponent);
74 }
75 }
76 }
77 }
78}
const std::map< std::string, std::shared_ptr< smacc::ISmaccOrthogonal > > & getOrthogonals() const
std::vector< ISmaccUpdatable * > updatableClients_
ISmaccStateMachine * smaccStateMachine_
std::string demangleType(const std::type_info *tinfo)
Definition: introspection.h:86

References smacc::introspection::demangleType(), smacc::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 smacc::SignalDetector::findUpdatableStateElements ( ISmaccState currentState)
private

findUpdatableClientBehaviors()

Definition at line 85 of file signal_detector.cpp.

86{
87 updatableStateElements_.push_back(std::vector<ISmaccUpdatable *>());
88
89 auto& updatableElements = updatableStateElements_.back();
90
91 for (auto pair : this->smaccStateMachine_->getOrthogonals())
92 {
93 auto &orthogonal = pair.second;
94 auto &behaviors = orthogonal->getClientBehaviors().back();
95
96 for (auto &currentBehavior : behaviors)
97 {
98 ISmaccUpdatable *updatableClientBehavior = dynamic_cast<ISmaccUpdatable *>(currentBehavior.get());
99
100 if (updatableClientBehavior != nullptr)
101 {
102 ROS_DEBUG_STREAM("Adding updatable behavior: " << demangleType(typeid(updatableClientBehavior)));
103 updatableElements.push_back(updatableClientBehavior);
104 }
105 }
106 }
107
108 auto updatableState = dynamic_cast<ISmaccUpdatable *>(currentState);
109 if (updatableState != nullptr)
110 {
111 updatableElements.push_back(updatableState);
112 }
113
114 auto statereactors = currentState->getStateReactors();
115 for (auto &sr : statereactors)
116 {
117 ISmaccUpdatable *updatableStateReactor = dynamic_cast<ISmaccUpdatable *>(sr.get());
118 if (updatableStateReactor != nullptr)
119 {
120 ROS_DEBUG_STREAM("Adding updatable stateReactorr: " << demangleType(typeid(updatableStateReactor)));
121 updatableElements.push_back(updatableStateReactor);
122 }
123 }
124
125 auto eventgenerators = currentState->getEventGenerators();
126 for (auto &eg : eventgenerators)
127 {
128 ISmaccUpdatable *updatableEventGenerator = dynamic_cast<ISmaccUpdatable *>(eg.get());
129 if (updatableEventGenerator != nullptr)
130 {
131 ROS_DEBUG_STREAM("Adding updatable eventGenerator: " << demangleType(typeid(updatableEventGenerator)));
132 updatableElements.push_back(updatableEventGenerator);
133 }
134 }
135}
std::vector< std::vector< ISmaccUpdatable * > > updatableStateElements_

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

Referenced by notifyStateConfigured().

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

◆ initialize()

void smacc::SignalDetector::initialize ( ISmaccStateMachine stateMachine)

initialize()

Definition at line 32 of file signal_detector.cpp.

33{
34 smaccStateMachine_ = stateMachine;
35 lastState_ = std::numeric_limits<uint64_t>::quiet_NaN();
37 initialized_ = true;
38}
std::atomic< uint64_t > lastState_

References findUpdatableClients(), initialized_, lastState_, and smaccStateMachine_.

Referenced by smacc::ISmaccStateMachine::ISmaccStateMachine().

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

◆ join()

void smacc::SignalDetector::join ( )

join()

Definition at line 162 of file signal_detector.cpp.

163{
165}
boost::thread signalDetectorThread_

References signalDetectorThread_.

◆ notifyStateConfigured()

void smacc::SignalDetector::notifyStateConfigured ( ISmaccState currentState)

Definition at line 177 of file signal_detector.cpp.

178{
179 this->findUpdatableStateElements(currentState);
180}
void findUpdatableStateElements(ISmaccState *currentState)

References findUpdatableStateElements().

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

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

◆ notifyStateExited()

void smacc::SignalDetector::notifyStateExited ( ISmaccState currentState)

Definition at line 182 of file signal_detector.cpp.

183{
184 this->updatableStateElements_.pop_back();
185}

References updatableStateElements_.

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

Here is the caller graph for this function:

◆ pollingLoop()

void smacc::SignalDetector::pollingLoop ( )

pollingLoop()

Definition at line 267 of file signal_detector.cpp.

268{
269 // ros::NodeHandle nh("~"); // use node name as root of the parameter server
270
271 ros::NodeHandle _;
272 ros::Rate r0(20);
273 while (!initialized_)
274 {
275 r0.sleep();
276 }
277
278 ros::NodeHandle nh(smacc::utils::cleanShortTypeName(typeid(*this->smaccStateMachine_)));
279
280 if (!nh.getParam("signal_detector_loop_freq", this->loop_rate_hz))
281 {
282 ROS_WARN("Signal detector frequency (ros param ~signal_detector_loop_freq) was not set, using default frequency: "
283 "%lf",
284 this->loop_rate_hz);
285 }
286 else
287 {
288 ROS_WARN("Signal detector frequency (ros param ~signal_detector_loop_freq): %lf", this->loop_rate_hz);
289 }
290
291 nh.setParam("signal_detector_loop_freq", this->loop_rate_hz);
292
293 ROS_INFO_STREAM("[SignalDetector] loop rate hz:" << loop_rate_hz);
294
296 {
297 ROS_INFO_STREAM("[SignalDetector] running in single threaded mode");
298
299 ros::Rate r(loop_rate_hz);
300 while (ros::ok() && !end_)
301 {
302 ROS_INFO_STREAM_THROTTLE(10, "[SignalDetector] heartbeat");
303 pollOnce();
304 ros::spinOnce();
305 r.sleep();
306 }
307 }
308 else
309 {
310 ROS_INFO_STREAM("[SignalDetector] running in multi threaded mode");
311 ros::AsyncSpinner spinner(1);
312 spinner.start();
313 ros::waitForShutdown();
314 }
315
316}
std::string cleanShortTypeName(const std::type_info &tinfo)
Definition: common.cpp:14

References smacc::utils::cleanShortTypeName(), end_, executionModel_, initialized_, loop_rate_hz, pollOnce(), smacc::SINGLE_THREAD_SPINNER, and smaccStateMachine_.

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

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

◆ pollOnce()

void smacc::SignalDetector::pollOnce ( )
private

poll()

Definition at line 193 of file signal_detector.cpp.

194{
195 // precondition: smaccStateMachine_ != nullptr
196
197 try
198 {
199 smaccStateMachine_->lockStateMachine("update behaviors");
200
201 this->findUpdatableClients();
202 ROS_DEBUG_STREAM("updatable clients: " << this->updatableClients_.size());
203
204 if (this->updatableClients_.size())
205 {
206 for (auto *updatableClient : this->updatableClients_)
207 {
208 ROS_DEBUG_STREAM("[PollOnce] update client call: " << demangleType(typeid(updatableClient)));
209 updatableClient->executeUpdate();
210 }
211 }
212
213 // STATE UPDATABLE ELEMENTS
215 this->smaccStateMachine_->stateMachineCurrentAction != StateMachineInternalAction::STATE_CONFIGURING &&
216 this->smaccStateMachine_->stateMachineCurrentAction != StateMachineInternalAction::STATE_EXITING &&
217 this->smaccStateMachine_->stateMachineCurrentAction != StateMachineInternalAction::STATE_ENTERING)
218 {
219
220 // // we do not update updatable elements during trasitioning or configuration of states
221 // long currentStateIndex = smaccStateMachine_->getCurrentStateCounter();
222
223 // ROS_DEBUG_STREAM("[PollOnce] update behaviors. checking current state");
224
225 // auto currentState = smaccStateMachine_->getCurrentState();
226 // if (currentState != nullptr)
227 // {
228 // ROS_DEBUG_STREAM("[PollOnce] current state: " << currentStateIndex);
229 // ROS_DEBUG_STREAM("[PollOnce] last state: " << this->lastState_);
230
231 // if (currentStateIndex != 0)
232 // {
233 // if (currentStateIndex != this->lastState_)
234 // {
235 // ROS_DEBUG_STREAM("[PollOnce] detected new state, refreshing updatable client behavior table");
236 // // we are in a new state, refresh the updatable client behaviors table
237 // this->lastState_ = currentStateIndex;
238 // }
239
240 ROS_DEBUG_STREAM("updatable states: " << this->updatableStateElements_.size());
241
242 for (auto stateElement : this->updatableStateElements_)
243 {
244 for (auto *udpatableStateElement : stateElement)
245 {
246 ROS_DEBUG_STREAM("pollOnce update client behavior call: " << demangleType(typeid(*udpatableStateElement)));
247 udpatableStateElement->executeUpdate();
248 }
249 }
250 // }
251 //}
252 }
253 }
254 catch (std::exception &ex)
255 {
256 ROS_ERROR("Exception during Signal Detector update loop. %s", ex.what());
257 }
258
259 smaccStateMachine_->unlockStateMachine("update behaviors");
260}
void lockStateMachine(std::string msg)
StateMachineInternalAction stateMachineCurrentAction
void unlockStateMachine(std::string msg)

References smacc::introspection::demangleType(), findUpdatableClients(), smacc::ISmaccStateMachine::lockStateMachine(), smaccStateMachine_, smacc::STATE_CONFIGURING, smacc::STATE_ENTERING, smacc::STATE_EXITING, smacc::ISmaccStateMachine::stateMachineCurrentAction, smacc::TRANSITIONING, smacc::ISmaccStateMachine::unlockStateMachine(), 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 smacc::SignalDetector::postEvent ( EventType *  ev)
inline

Definition at line 42 of file smacc_signal_detector.h.

43 {
44 boost::intrusive_ptr<EventType> weakPtrEvent = ev;
45 this->scheduler_->queue_event(processorHandle_, weakPtrEvent);
46 }
SmaccFifoScheduler::processor_handle processorHandle_

References processorHandle_, and scheduler_.

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

Here is the caller graph for this function:

◆ runThread()

void smacc::SignalDetector::runThread ( )

runThread()

Definition at line 152 of file signal_detector.cpp.

153{
154 signalDetectorThread_ = boost::thread(boost::bind(&SignalDetector::pollingLoop, this));
155}

References pollingLoop(), and signalDetectorThread_.

Here is the call graph for this function:

◆ setProcessorHandle()

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

setProcessorHandle()

Definition at line 142 of file signal_detector.cpp.

143{
144 processorHandle_ = processorHandle;
145}

References processorHandle_.

Referenced by smacc::run().

Here is the caller graph for this function:

◆ stop()

void smacc::SignalDetector::stop ( )

stop()

Definition at line 172 of file signal_detector.cpp.

173{
174 end_ = true;
175}

References end_.

Member Data Documentation

◆ end_

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

Definition at line 69 of file smacc_signal_detector.h.

Referenced by pollingLoop(), and stop().

◆ executionModel_

ExecutionModel smacc::SignalDetector::executionModel_
private

Definition at line 85 of file smacc_signal_detector.h.

Referenced by pollingLoop().

◆ initialized_

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

Definition at line 71 of file smacc_signal_detector.h.

Referenced by initialize(), and pollingLoop().

◆ lastState_

std::atomic<uint64_t> smacc::SignalDetector::lastState_
private

Definition at line 61 of file smacc_signal_detector.h.

Referenced by initialize().

◆ loop_rate_hz

double smacc::SignalDetector::loop_rate_hz
private

Definition at line 67 of file smacc_signal_detector.h.

Referenced by pollingLoop(), and SignalDetector().

◆ nh_

ros::NodeHandle smacc::SignalDetector::nh_
private

Definition at line 73 of file smacc_signal_detector.h.

◆ processorHandle_

SmaccFifoScheduler::processor_handle smacc::SignalDetector::processorHandle_
private

Definition at line 81 of file smacc_signal_detector.h.

Referenced by postEvent(), and setProcessorHandle().

◆ scheduler_

SmaccFifoScheduler* smacc::SignalDetector::scheduler_
private

Definition at line 79 of file smacc_signal_detector.h.

Referenced by postEvent(), and SignalDetector().

◆ signalDetectorThread_

boost::thread smacc::SignalDetector::signalDetectorThread_
private

Definition at line 83 of file smacc_signal_detector.h.

Referenced by join(), and runThread().

◆ smaccStateMachine_

ISmaccStateMachine* smacc::SignalDetector::smaccStateMachine_
private

◆ statusPub_

ros::Publisher smacc::SignalDetector::statusPub_
private

Definition at line 75 of file smacc_signal_detector.h.

◆ updatableClients_

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

Definition at line 57 of file smacc_signal_detector.h.

Referenced by findUpdatableClients(), and pollOnce().

◆ updatableStateElements_

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

Definition at line 59 of file smacc_signal_detector.h.

Referenced by findUpdatableStateElements(), notifyStateExited(), and pollOnce().


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