stop1_executor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
19 
20 namespace prbt_hardware_support
21 {
22 
24  const TServiceCallFunc& unhold_func,
25  const TServiceCallFunc& recover_func,
26  const TServiceCallFunc& halt_func)
27 {
28  state_machine_ = std::unique_ptr<RunPermittedStateMachine>(
29  new RunPermittedStateMachine(recover_func, halt_func, hold_func, unhold_func) );
30 
31  state_machine_->start();
32 
33  worker_thread_ = std::thread(&Stop1Executor::workerThreadFun, this);
34 }
35 
37 {
38  if (worker_thread_.joinable())
39  {
40  {
41  std::lock_guard<std::mutex> lock(sm_mutex_);
42  terminate_ = true;
43  }
44  worker_cv_.notify_one();
45  worker_thread_.join();
46  }
47 
49 }
50 
51 void Stop1Executor::updateRunPermitted(const bool run_permitted)
52 {
53  ROS_DEBUG_STREAM("updateRunPermitted(" << std::boolalpha << run_permitted << std::noboolalpha << ")");
54  {
55  std::lock_guard<std::mutex> lock(sm_mutex_);
56  state_machine_->process_event(typename RunPermittedStateMachine::run_permitted_updated(run_permitted));
57  }
58  worker_cv_.notify_one();
59 }
60 
61 bool Stop1Executor::updateRunPermittedCallback(std_srvs::SetBool::Request &req,
62  std_srvs::SetBool::Response &res)
63 {
64  updateRunPermitted(req.data);
65  res.success = true;
66  return true;
67 }
68 
70 {
71  std::unique_lock<std::mutex> sm_lock(sm_mutex_);
72  while (!terminate_)
73  {
74  worker_cv_.wait(sm_lock, [this]() { return (!this->state_machine_->task_queue_.empty() || this->terminate_); });
75  if (terminate_)
76  {
77  break;
78  }
79 
80  AsyncRunPermittedTask task = state_machine_->task_queue_.front();
81  state_machine_->task_queue_.pop();
82 
83  sm_lock.unlock(); // | This part is executed async from
84  task.execute(); // | the state machine since new run_permitted updates need to be handled
85  // | during service calls.
86  sm_lock.lock(); // |
87 
88  task.signalCompletion(); //Could add Task to Queue and does process_event on the state machine. Needs lock.
89  }
90 }
91 
92 
93 } // namespace prbt_hardware_support
bool updateRunPermittedCallback(std_srvs::SetBool::Request &req, std_srvs::SetBool::Response &res)
Stop1Executor(const TServiceCallFunc &hold_func, const TServiceCallFunc &unhold_func, const TServiceCallFunc &recover_func, const TServiceCallFunc &halt_func)
Create required service clients and state machine; start worker-thread and state machine.
std::atomic_bool terminate_
Flag indicating if the worker-thread should terminate.
std::condition_variable worker_cv_
Condition variable for notifying a waiting worker-thread.
void updateRunPermitted(const bool run_permitted)
This is called everytime an updated run_permitted value is obtained.
std::thread worker_thread_
Worker-thread.
void stopStateMachine()
Stop the state machine.
#define ROS_DEBUG_STREAM(args)
void workerThreadFun()
This is executed in the worker-thread and allows asynchronous handling of run_permitted updates...
std::function< bool()> TServiceCallFunc
msm::back::state_machine< RunPermittedStateMachine_ > RunPermittedStateMachine
The top-level (back-end) state machine.
An AsyncRunPermittedTask is represented by a task execution and a completion signalling.
void signalCompletion()
Signal completion of the task execution.
std::unique_ptr< RunPermittedStateMachine > state_machine_
State machine.
virtual ~Stop1Executor()
Stop state machine and terminate worker-thread.


prbt_hardware_support
Author(s):
autogenerated on Tue Feb 2 2021 03:50:17