CycleUtil.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX
19 * @author Mirko Waechter( mirko.waechter at kit dot edu)
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#pragma once
25
26#include <cstdint>
27
28#include <IceUtil/Time.h>
29
30namespace armarx
31{
32 /**
33 * @brief This util class helps with keeping a cycle time during a control cycle.
34 * The main function is waitForCycleDuration(), which should be called in every cycle.
35 * The function will wait (virtual or system time) until the cycle time is reached.
36 * If the last cycle took already too long, it will return immediately.
37 * The last cycle time is updated, when waitForCycleDuration() is called.
38 * @note If system time is used (i.e. not the timeserver) the monotonic clock is used.
39 */
41 {
42 public:
43 CycleUtil(const IceUtil::Time& cycleDuration, bool forceSystemTime = false);
44 CycleUtil(const std::int64_t& cycleDurationMs, bool forceSystemTime = false);
45 /**
46 * @brief resets startTime, lastCycleTime and cycleCount.
47 */
48 void reset();
49 /**
50 * @brief
51 * This function will wait (virtual or system time) until the cycle time is reached.
52 * If the last cycle took already too long, it will return immediately.
53 * The last cycle time is updated, when this function is called.
54 * @return returns the time the function waited
55 */
56 IceUtil::Time waitForCycleDuration();
57 IceUtil::Time update();
58
59 /**
60 * @brief Time when object was constructed or reset was last called.
61 */
62 IceUtil::Time getStartTime() const;
63
64 /**
65 * @brief Time when the last cycle finished or start time.
66 */
67 IceUtil::Time getLastCycleTime() const;
68
69 /**
70 * @brief Count of how many cycles were executed, i.e. how often waitForCycleDuration() was called.
71 */
72 std::int64_t getCycleCount() const;
73 IceUtil::Time getAverageDuration() const;
74 IceUtil::Time getMinimumDuration() const;
75 IceUtil::Time getMaximumDuration() const;
76 float getBusyWaitShare() const;
77 void setBusyWaitShare(float value);
78
79 protected:
80 static IceUtil::Time now();
81 IceUtil::Time startTime;
82 IceUtil::Time lastCycleTime;
83 IceUtil::Time cycleDuration;
84 IceUtil::Time cycleMinDuration;
85 IceUtil::Time cycleMaxDuration;
86 std::int64_t cycleCount;
89 };
90} // namespace armarx
IceUtil::Time getMaximumDuration() const
IceUtil::Time update()
Definition CycleUtil.cpp:73
CycleUtil(const IceUtil::Time &cycleDuration, bool forceSystemTime=false)
Definition CycleUtil.cpp:30
IceUtil::Time waitForCycleDuration()
This function will wait (virtual or system time) until the cycle time is reached.
Definition CycleUtil.cpp:53
IceUtil::Time cycleDuration
Definition CycleUtil.h:83
IceUtil::Time getStartTime() const
Time when object was constructed or reset was last called.
Definition CycleUtil.cpp:91
float getBusyWaitShare() const
IceUtil::Time cycleMinDuration
Definition CycleUtil.h:84
IceUtil::Time cycleMaxDuration
Definition CycleUtil.h:85
IceUtil::Time getAverageDuration() const
std::int64_t getCycleCount() const
Count of how many cycles were executed, i.e.
Definition CycleUtil.cpp:97
void setBusyWaitShare(float value)
std::int64_t cycleCount
Definition CycleUtil.h:86
IceUtil::Time getMinimumDuration() const
IceUtil::Time getLastCycleTime() const
Time when the last cycle finished or start time.
void reset()
resets startTime, lastCycleTime and cycleCount.
Definition CycleUtil.cpp:44
IceUtil::Time lastCycleTime
Definition CycleUtil.h:82
IceUtil::Time startTime
Definition CycleUtil.h:81
static IceUtil::Time now()
This file offers overloads of toIce() and fromIce() functions for STL container types.