mode.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef MODE_H
19 #define MODE_H
20 
21 #include <ros/ros.h>
22 #include <colorUtils.h>
23 #include <boost/signals2.hpp>
24 #include <boost/thread.hpp>
25 
26 class Mode
27 {
28 public:
29  Mode(int priority = 0, double freq = 0, int pulses = 0, double timeout = 0)
30  : _priority(priority), _freq(freq), _pulses(pulses), _timeout(timeout),
31  _finished(false), _pulsed(0), _isStopRequested(false), _isPauseRequested(false),
32  _isRunning(false)
33  {
34  if(this->getFrequency() == 0.0)
35  this->setFrequency(1.0);
36  }
37  virtual ~Mode(){}
38 
39  void start()
40  {
41  if(_thread == NULL)
42  _thread.reset(new boost::thread(&Mode::run, this));
43  if(isPauseRequested())
44  {
45  boost::mutex::scoped_lock lock(_mutex_pause);
46  _isPauseRequested = false;
47  _cond_pause.notify_one();
48  }
49  _isRunning = true;
50  }
51 
52  void stop()
53  {
54  _mutex.lock();
55  _isStopRequested = true;
56  _mutex.unlock();
57  if(isPauseRequested())
58  {
59  _isPauseRequested = false;
60  boost::mutex::scoped_lock lock(_mutex_pause);
61  _cond_pause.notify_one();
62  }
63  if(_thread != NULL)
64  {
65  _thread->join();
66  _thread.reset();
67  }
68  _isStopRequested = false;
69  _isRunning = false;
70  }
71 
72  void pause()
73  {
74  _mutex.lock();
75  _isPauseRequested = true;
76  _mutex.unlock();
77  _isRunning = false;
78  }
79 
80  virtual void execute() = 0;
81 
82  virtual std::string getName() = 0;
83 
84  bool finished(){ return _finished; }
85 
86  void setPriority(int priority){ _priority = priority; }
87  int getPriority(){ return _priority; }
88 
89  void setTimeout(double timeout){ _timeout = timeout; }
90  double getTimeout(){ return _timeout; }
91 
92  void setFrequency(double freq){ _freq = freq; }
93  double getFrequency(){ return _freq; }
94 
95  void setPulses(int pulses){ _pulses = pulses; }
96  int getPulses(){ return _pulses; }
97 
98  int pulsed(){ return _pulsed; }
99 
100  void setColor(color::rgba color){ _color = color; }
102 
105 
106  bool isRunning(){ return _isRunning; }
107 
108  boost::signals2::signal<void (color::rgba color)>* signalColorReady(){ return &m_sigColorReady; }
109  boost::signals2::signal<void (std::vector<color::rgba> &colors)>* signalColorsReady(){ return &m_sigColorsReady; }
110  boost::signals2::signal<void (int)>* signalModeFinished(){ return &m_sigFinished; }
111 
112 protected:
114  double _freq;
115  int _pulses;
116  double _timeout;
117 
118  bool _finished;
119  int _pulsed;
120 
122  std::vector<color::rgba> _colors;
125 
126  static const unsigned int UPDATE_RATE_HZ = 100;
127 
128  boost::signals2::signal<void (color::rgba color)> m_sigColorReady;
129  boost::signals2::signal<void (std::vector<color::rgba> &colors)> m_sigColorsReady;
130  boost::signals2::signal<void (int)> m_sigFinished;
131 
132 private:
134  boost::mutex _mutex;
135  boost::mutex _mutex_pause;
139 
140  boost::condition_variable _cond_pause;
141 
143  {
144  bool ret;
145  _mutex.lock();
146  ret = _isStopRequested;
147  _mutex.unlock();
148  return ret;
149  }
150 
152  {
153  bool ret;
154  _mutex.lock();
155  ret = _isPauseRequested;
156  _mutex.unlock();
157  return ret;
158  }
159 
160 protected:
161  virtual void run()
162  {
163  ros::Rate r(UPDATE_RATE_HZ);
164 
165  ros::Time timeStart = ros::Time::now();
166 
167  while(!isStopRequested() && !ros::isShuttingDown())
168  {
169  while(isPauseRequested())
170  {
171  boost::mutex::scoped_lock lock(_mutex_pause);
172  _cond_pause.wait(lock);
173  }
174  this->execute();
175 
176  if((this->getPulses() != 0) &&
177  (this->getPulses() <= this->pulsed()))
178  break;
179 
180  if(this->getTimeout() != 0)
181  {
182  ros::Duration timePassed = ros::Time::now() - timeStart;
183  if(timePassed.toSec() >= this->getTimeout())
184  break;
185  }
186  r.sleep();
187  }
188  ROS_DEBUG("Mode %s finished",this->getName().c_str());
189  if(!isStopRequested())
190  m_sigFinished(this->getPriority());
191  }
192 };
193 
194 #endif
boost::shared_ptr< boost::thread > _thread
Definition: mode.h:133
boost::signals2::signal< void(int)> m_sigFinished
Definition: mode.h:130
color::rgba _color
Definition: mode.h:121
bool isRunning()
Definition: mode.h:106
bool isPauseRequested()
Definition: mode.h:151
color::rgba getColor()
Definition: mode.h:101
void setFrequency(double freq)
Definition: mode.h:92
std::vector< color::rgba > _colors
Definition: mode.h:122
Definition: mode.h:26
double getTimeout()
Definition: mode.h:90
int _pulsed
Definition: mode.h:119
Mode(int priority=0, double freq=0, int pulses=0, double timeout=0)
Definition: mode.h:29
int getPulses()
Definition: mode.h:96
int _priority
Definition: mode.h:113
static const unsigned int UPDATE_RATE_HZ
Definition: mode.h:126
void setPulses(int pulses)
Definition: mode.h:95
int pulsed()
Definition: mode.h:98
void setPriority(int priority)
Definition: mode.h:86
void setTimeout(double timeout)
Definition: mode.h:89
double getFrequency()
Definition: mode.h:93
color::rgba _init_color
Definition: mode.h:124
void stop()
Definition: mode.h:52
bool _isRunning
Definition: mode.h:138
boost::signals2::signal< void(color::rgba color)> * signalColorReady()
Definition: mode.h:108
virtual void run()
Definition: mode.h:161
color::rgba _actualColor
Definition: mode.h:123
ROSCPP_DECL bool isShuttingDown()
bool isStopRequested()
Definition: mode.h:142
virtual std::string getName()=0
boost::mutex _mutex
Definition: mode.h:134
double _freq
Definition: mode.h:114
virtual ~Mode()
Definition: mode.h:37
bool _isStopRequested
Definition: mode.h:136
bool sleep()
boost::condition_variable _cond_pause
Definition: mode.h:140
boost::mutex _mutex_pause
Definition: mode.h:135
void pause()
Definition: mode.h:72
boost::signals2::signal< void(color::rgba color)> m_sigColorReady
Definition: mode.h:128
int getPriority()
Definition: mode.h:87
boost::signals2::signal< void(std::vector< color::rgba > &colors)> m_sigColorsReady
Definition: mode.h:129
bool _finished
Definition: mode.h:118
double _timeout
Definition: mode.h:116
boost::signals2::signal< void(std::vector< color::rgba > &colors)> * signalColorsReady()
Definition: mode.h:109
static Time now()
virtual void execute()=0
color::rgba getActualColor()
Definition: mode.h:104
boost::signals2::signal< void(int)> * signalModeFinished()
Definition: mode.h:110
int _pulses
Definition: mode.h:115
void start()
Definition: mode.h:39
bool finished()
Definition: mode.h:84
void setColor(color::rgba color)
Definition: mode.h:100
void setActualColor(color::rgba color)
Definition: mode.h:103
#define ROS_DEBUG(...)
bool _isPauseRequested
Definition: mode.h:137


cob_light
Author(s): Benjamin Maidel
autogenerated on Wed Apr 7 2021 02:11:39