SMACC
Loading...
Searching...
No Matches
smacc_signal_detector.h
Go to the documentation of this file.
1/*****************************************************************************************************************
2 * ReelRobotix Inc. - Software License Agreement Copyright (c) 2018
3 * Authors: Pablo Inigo Blasco, Brett Aldrich
4 *
5 ******************************************************************************************************************/
6#pragma once
7
8#include <boost/thread.hpp>
9#include <smacc/common.h>
10#include <atomic>
11
12namespace smacc
13{
15{
18};
19
20// Mostly define the state machine ros thread and receive events state machine components (clients, state elements)
21// This class also contains the event queue of the state machine
23{
24public:
26
27 void initialize(ISmaccStateMachine* stateMachine);
28
29 void setProcessorHandle(SmaccFifoScheduler::processor_handle processorHandle);
30
31 // Runs the polling loop into a thread...
32 void runThread();
33
34 // Waits for the polling thread to end...
35 void join();
36
37 void stop();
38
39 void pollingLoop();
40
41 template <typename EventType>
42 void postEvent(EventType* ev)
43 {
44 boost::intrusive_ptr<EventType> weakPtrEvent = ev;
45 this->scheduler_->queue_event(processorHandle_, weakPtrEvent);
46 }
47
48 void notifyStateConfigured(ISmaccState* currentState);
49
50 void notifyStateExited(ISmaccState* currentState);
51
52private:
53 void pollOnce();
54
56
57 std::vector<ISmaccUpdatable*> updatableClients_;
58
59 std::vector<std::vector<ISmaccUpdatable*>> updatableStateElements_;
60
61 std::atomic<uint64_t> lastState_;
62
64 void findUpdatableStateElements(ISmaccState* currentState);
65
66 // Loop frequency of the signal detector (to check answers from actionservers)
68
69 std::atomic<bool> end_;
70
71 std::atomic<bool> initialized_;
72
73 ros::NodeHandle nh_;
74
75 ros::Publisher statusPub_;
76
77 // ---- boost statechart related ----
78
80
81 SmaccFifoScheduler::processor_handle processorHandle_;
82
83 boost::thread signalDetectorThread_;
84
86};
87
88// Main entry point for any SMACC state machine
89// It instantiates and starts the specified state machine type
90// it uses two threads: a new thread and the current one.
91// The created thread is for the state machine process
92// it locks the current thread to handle events of the state machine
93template <typename StateMachineType>
95{
96 // create the asynchronous state machine scheduler
97 SmaccFifoScheduler scheduler1(true);
98
99 // create the signalDetector component
100 SignalDetector signalDetector(&scheduler1, executionModel);
101
102 // create the asynchronous state machine processor
103 SmaccFifoScheduler::processor_handle sm = scheduler1.create_processor<StateMachineType>(&signalDetector);
104
105 // initialize the asynchronous state machine processor
106 signalDetector.setProcessorHandle(sm);
107
108 scheduler1.initiate_processor(sm);
109
110 // create a thread for the asynchronous state machine processor execution
111 boost::thread otherThread(boost::bind(&sc::fifo_scheduler<>::operator(), &scheduler1, 0));
112
113 // use the main thread for the signal detector component (waiting actionclient requests)
114 signalDetector.pollingLoop();
115 scheduler1.terminate();
116 otherThread.join();
117}
118
119} // namespace smacc
std::atomic< bool > initialized_
std::vector< std::vector< ISmaccUpdatable * > > updatableStateElements_
std::vector< ISmaccUpdatable * > updatableClients_
std::atomic< uint64_t > lastState_
void notifyStateConfigured(ISmaccState *currentState)
boost::thread signalDetectorThread_
void findUpdatableStateElements(ISmaccState *currentState)
ISmaccStateMachine * smaccStateMachine_
void postEvent(EventType *ev)
void initialize(ISmaccStateMachine *stateMachine)
SmaccFifoScheduler::processor_handle processorHandle_
std::atomic< bool > end_
void setProcessorHandle(SmaccFifoScheduler::processor_handle processorHandle)
void notifyStateExited(ISmaccState *currentState)
SmaccFifoScheduler * scheduler_
void run(ExecutionModel executionModel=ExecutionModel::SINGLE_THREAD_SPINNER)
boost::statechart::fifo_scheduler< SmaccFifoWorker, SmaccAllocator > SmaccFifoScheduler