SMACC2
smacc_signal_detector.hpp
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#pragma once
21
22#include <atomic>
23#include <boost/thread.hpp>
24#include <smacc2/common.hpp>
25#include <smacc2_msgs/msg/smacc_status.hpp>
26
27namespace smacc2
28{
30{
31public:
33
34 void initialize(ISmaccStateMachine * stateMachine);
35
36 void setProcessorHandle(SmaccFifoScheduler::processor_handle processorHandle);
37
38 // Runs the polling loop into a thread...
39 void runThread();
40
41 // Waits for the polling thread to end...
42 void join();
43
44 void stop();
45
46 void pollingLoop();
47
48 void pollOnce();
49
50 template <typename EventType>
51 void postEvent(EventType * ev)
52 {
53 boost::intrusive_ptr<EventType> weakPtrEvent = ev;
54 this->scheduler_->queue_event(processorHandle_, weakPtrEvent);
55 }
56
57 rclcpp::Node::SharedPtr getNode();
58 inline rclcpp::Logger getLogger() { return getNode()->get_logger(); }
59
60private:
62
63 std::vector<ISmaccUpdatable *> updatableClients_;
64
65 std::vector<ISmaccUpdatable *> updatableStateElements_;
66
67 std::atomic<unsigned long> lastState_;
68
70 void findUpdatableStateElements(ISmaccState * currentState);
71
72 // Loop frequency of the signal detector (to check answers from actionservers)
74
75 std::atomic<bool> end_;
76
77 std::atomic<bool> initialized_;
78
79 rclcpp::Publisher<smacc2_msgs::msg::SmaccStatus>::SharedPtr statusPub_;
80
81 // ---- boost statechart related ----
82
84
85 SmaccFifoScheduler::processor_handle processorHandle_;
86
87 boost::thread signalDetectorThread_;
88};
89
90// Main entry point for any SMACC state machine
91// It instantiates and starts the specified state machine type
92// it uses two threads: a new thread and the current one.
93// The created thread is for the state machine process
94// it locks the current thread to handle events of the state machine
95template <typename StateMachineType>
96void run()
97{
98 // create the asynchronous state machine scheduler
99 SmaccFifoScheduler scheduler1(true);
100
101 // create the signalDetector component
102 SignalDetector signalDetector(&scheduler1);
103
104 // create the asynchronous state machine processor
105 SmaccFifoScheduler::processor_handle sm =
106 scheduler1.create_processor<StateMachineType>(&signalDetector);
107
108 // initialize the asynchronous state machine processor
109 signalDetector.setProcessorHandle(sm);
110
111 scheduler1.initiate_processor(sm);
112
113 //create a thread for the asynchronous state machine processor execution
114 boost::thread otherThread(boost::bind(&sc::fifo_scheduler<>::operator(), &scheduler1, 0));
115
116 // use the main thread for the signal detector component (waiting actionclient requests)
117 signalDetector.pollingLoop();
118}
119
120} // namespace smacc2
SmaccFifoScheduler * scheduler_
void postEvent(EventType *ev)
void initialize(ISmaccStateMachine *stateMachine)
SignalDetector(SmaccFifoScheduler *scheduler)
std::atomic< unsigned long > lastState_
std::vector< ISmaccUpdatable * > updatableStateElements_
rclcpp::Node::SharedPtr getNode()
SmaccFifoScheduler::processor_handle processorHandle_
std::vector< ISmaccUpdatable * > updatableClients_
void setProcessorHandle(SmaccFifoScheduler::processor_handle processorHandle)
rclcpp::Publisher< smacc2_msgs::msg::SmaccStatus >::SharedPtr statusPub_
void findUpdatableStateElements(ISmaccState *currentState)
ISmaccStateMachine * smaccStateMachine_
std::atomic< bool > initialized_
boost::statechart::fifo_scheduler< SmaccFifoWorker, SmaccAllocator > SmaccFifoScheduler