MIRA
Unit.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_UNIT_H_
49 #define _MIRA_UNIT_H_
50 
51 #include <fw/MicroUnit.h>
52 
53 namespace mira {
54 
56 
64 {
66 public:
67 
74  Unit(Duration cycleTime, Flags flags = NORMAL) :
75  MicroUnit(flags),
76  mResetProcessTimer(true)
77  {
78  setCycleTime(cycleTime);
79  setCycleTimeTolerance(cycleTime);
80  }
81 
89  Unit(Duration cycleTime, Duration cycleTimeTolerance, Flags flags = NORMAL) :
90  MicroUnit(flags),
91  mResetProcessTimer(true)
92  {
93  setCycleTime(cycleTime);
94  setCycleTimeTolerance(cycleTimeTolerance);
95  }
96 
97  virtual ~Unit()
98  {}
99 
100  template<typename Reflector>
101  void reflect(Reflector& r)
102  {
104  r.property("CycleTime",
105  getter<Duration>(&Unit::getCycleTime, this),
106  setter<Duration>(&Unit::setCycleTime, this),
107  "The cycle time of our process", mCycleTime,
108  PropertyHints::limits<Duration>(Duration::milliseconds(0),
109  Duration::hours(1)) |
110  PropertyHints::step<Duration>(Duration::milliseconds(10)));
111  r.property("CycleTimeTolerance",
112  getter<Duration>(&Unit::getCycleTimeTolerance, this),
113  setter<Duration>(&Unit::setCycleTimeTolerance, this),
114  "The maximum time about that we can exceed the cycle time "
115  "before issuing a warning", mCycleTimeTolerance,
116  PropertyHints::limits<Duration>(Duration::milliseconds(1),
117  Duration::hours(1)) |
118  PropertyHints::step<Duration>(Duration::milliseconds(10)));
119  }
120 
122  virtual void checkin(const std::string& ns, const std::string& name);
123 
124 public:
125 
129  void setCycleTime(Duration period);
130 
134  Duration getCycleTime() const;
135 
140  void setCycleTimeTolerance(Duration tolerance);
141 
146  Duration getCycleTimeTolerance() const;
147 
152 
154  virtual void start();
155 
157  virtual void stop();
158 
160 
161 protected:
162 
173  virtual void process(const Timer& timer);
174 
176  virtual void needRecovery(const std::string& reason = "");
177 
179  virtual void operational();
180 
181 private:
182 
183  void processIntern(const Timer& timer);
184 
185  Duration mCycleTime;
186  Duration mCycleTimeTolerance;
187  TimerPtr mProcessTimer;
188 
189  bool mResetProcessTimer;
190 };
191 
193 
194 }
195 
197 
198 #endif
class MIRA_FRAMEWORK_EXPORT Unit
forward declaration
Definition: MicroUnit.h:60
tick_type milliseconds() const
Returns normalized number of milliseconds (0..999)
Definition: Time.h:283
DispatcherThread::TimerPtr TimerPtr
Definition: DispatcherThread.h:525
Class representing timers and tasks that can be registered and executed by the dispatcher thread...
Definition: DispatcherThread.h:147
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
virtual ~Unit()
Definition: Unit.h:97
#define MIRA_REFLECT_BASE(reflector, BaseClass)
Macro that can be used to reflect the base class easily.
Definition: ReflectorInterface.h:912
Duration getCycleTimeTolerance() const
Returns the cycle period tolerance for exceeding the period while calling the process(const Timer& ti...
Flags
Flags for creating an authority.
Definition: Authority.h:101
A more complex unit that adds a default timer to the thread dispatcher of the authority that acts as ...
Definition: Unit.h:63
Base class for all units.
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
Use this class to represent time durations.
Definition: Time.h:104
hour_type hours() const
Returns number of hours in the duration.
Definition: Time.h:268
Unit(Duration cycleTime, Flags flags=NORMAL)
Constructs a unit with given cycle time.
Definition: Unit.h:74
#define MIRA_OBJECT(classIdentifier)
Use this MACRO if you like the factory to automatically extract the class name from the given identif...
Definition: FactoryMacros.h:179
void setCycleTimeTolerance(Duration tolerance)
Set the cycle period tolerance for exceeding the period while calling the process(const Timer& timer)...
#define MIRA_NO_PUBLIC_DEFAULT_CONSTRUCTOR(CLASS)
Use this macro if your class does not have a public default constructor and should be managed by the ...
Definition: FactoryMacros.h:270
Duration getCycleTime() const
Returns the cycle period the process(const Timer& timer) method gets called.
void setCycleTime(Duration period)
Set the cycle period the process(const Timer& timer) method gets called.
Unit(Duration cycleTime, Duration cycleTimeTolerance, Flags flags=NORMAL)
Constructs a unit with given cycle time and a tolerance for the cycle time.
Definition: Unit.h:89
void reflect(Reflector &r)
Definition: Unit.h:101
Units are basic modules of a complex application.
Definition: MicroUnit.h:69