FPSCounter.cpp
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 VisionX::Tools
19  * @author Kai Welke (kai dot welke at kit dot edu)
20  * @date 2011
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 // *******************************************************
26 // include
27 // *******************************************************
28 #include "FPSCounter.h"
29 #include <sys/time.h>
30 #include <cfloat>
31 #include <unistd.h> // for usleep
32 
33 namespace visionx
34 {
35  // *******************************************************
36  // construction / destruction
37  // *******************************************************
38  FPSCounter::FPSCounter(int nDelayFrames)
39  {
40  m_nDelayFrames = nDelayFrames;
41 
42  reset();
43  }
44 
45 
46  // *******************************************************
47  // control
48  // *******************************************************
50  {
51  // init members
52  m_nIntervalTime = 0;
53 
54  m_fFPS = 0.0f;
55  m_nUpdates = 0;
56  m_fMinCycleTimeMS = FLT_MAX;
57  m_fMaxCycleTimeMS = -FLT_MAX;
58  }
59 
61  {
62  // calculate current cycle time
63  int nCycleTime = calculateTimeDiff(m_nCycleSec, m_nCycleUSec, true);
64 
65  // update members with time diff
66  updateMembers(nCycleTime);
67 
68  // increment update counter
69  m_nUpdates++;
70  }
71 
73  {
74  // TODO proper implementation
75  // calculate current cycle time
76  // int nCycleTime = calculateTimeDiff(m_nCycleSec, m_nCycleUSec, false);
77 
78  // update members with time diff
79  // updateMembers(nCycleTime);
80  }
81 
82  void FPSCounter::assureFPS(float fFrameRate)
83  {
84  // first run is not valid
85  bool bValidTime = (m_nUpdates != 0);
86  int nOverallCycleTime = (1.0f / fFrameRate) * 1000000;
87 
88  if (bValidTime)
89  {
90  // calculate target cycle time
91  int nWantedUSec = (1.0f / fFrameRate) * 1000000;
92 
93  int nTimeDiff;
94 
95  // wait until wanted cycle time is reached
96  do
97  {
98  nTimeDiff = calculateTimeDiff(m_nCycleSec, m_nCycleUSec);
99  usleep(1);
100  }
101  while (nTimeDiff < nWantedUSec);
102 
103  // update time stamp
104  nOverallCycleTime = calculateTimeDiff(m_nCycleSec, m_nCycleUSec, true);
105  }
106 
107  calculateTimeDiff(m_nCycleSec, m_nCycleUSec, true);
108  updateMembers(nOverallCycleTime);
109 
110  // increment update counter
111  m_nUpdates++;
112  }
113 
114  // *******************************************************
115  // member access
116  // *******************************************************
118  {
119  return m_nUpdates > m_nDelayFrames;
120  }
121 
123  {
124  return m_nUpdates;
125  }
126 
128  {
129  if (m_nUpdates <= m_nDelayFrames)
130  {
131  return 0.0f;
132  }
133 
134  return m_fFPS;
135  }
136 
138  {
139  if (m_nUpdates <= 10)
140  {
141  return 0.0f;
142  }
143 
144  float fMeanTimeMS = 0.0f;
145 
146  for (int i = 0 ; i < 10 ; i++)
147  {
148  fMeanTimeMS += m_fLastCycleTimesMS[i];
149  }
150 
151  return fMeanTimeMS / 10;
152  }
153 
155  {
156  if (m_nUpdates <= 1)
157  {
158  return 0.0f;
159  }
160 
161  return m_fMinCycleTimeMS;
162  }
163 
165  {
166  if (m_nUpdates <= 1)
167  {
168  return 0.0f;
169  }
170 
171  return m_fMaxCycleTimeMS;
172  }
173 
174  // *******************************************************
175  // private methods
176  // *******************************************************
177  void FPSCounter::updateMembers(int nCycleTime)
178  {
179  // first run is not valid
180  bool bValidTime = (m_nUpdates != 0);
181 
182  // update statistics
183  recalculateFPS(nCycleTime);
184 
185  if (bValidTime)
186  {
187  recalculateStats(nCycleTime);
188  }
189  }
190 
191  void FPSCounter::recalculateFPS(int nCycleUSec)
192  {
193  m_nIntervalTime += nCycleUSec;
194 
195  // only recalculate over some time
196  if ((m_nUpdates % m_nDelayFrames == 0) && (m_nUpdates != 0))
197  {
198  // calculate fps
199  float fTimeDiff = m_nIntervalTime / 1000000.0f;
200  m_fFPS = (1.0f / fTimeDiff) * m_nDelayFrames;
201  m_nIntervalTime = 0;
202  }
203  }
204 
205  void FPSCounter::recalculateStats(int nCycleUSec)
206  {
207  float fTimeMS = float(nCycleUSec) / 1000.0f;
208 
209  if (fTimeMS < m_fMinCycleTimeMS)
210  {
211  m_fMinCycleTimeMS = fTimeMS;
212  }
213 
214  if (fTimeMS > m_fMaxCycleTimeMS)
215  {
216  m_fMaxCycleTimeMS = fTimeMS;
217  }
218 
219  m_fLastCycleTimesMS[(m_nUpdates - 1) % 10] = fTimeMS;
220  }
221 
222  // calculate difference between given time stamp and current time
223  int FPSCounter::calculateTimeDiff(long& nSec, long& nUSec, bool bSetTime)
224  {
225  timeval t;
226  gettimeofday(&t, nullptr);
227  int nTimeDiff = (t.tv_sec - nSec) * 1000000 + ((int) t.tv_usec - nUSec);
228 
229  if (bSetTime)
230  {
231  nSec = t.tv_sec;
232  nUSec = t.tv_usec;
233  }
234  return nTimeDiff;
235  }
236 }
visionx::FPSCounter::getMinCycleTimeMS
float getMinCycleTimeMS()
Get minimum cycle time since start.
Definition: FPSCounter.cpp:154
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::FPSCounter::getFPS
float getFPS()
Get frames per second.
Definition: FPSCounter.cpp:127
visionx::FPSCounter::FPSCounter
FPSCounter(int nDelayFrames=10)
Constructs a new FPS counter.
Definition: FPSCounter.cpp:38
visionx::FPSCounter::getMeanCycleTimeMS
float getMeanCycleTimeMS()
Get mean cycle time over last 10 frames.
Definition: FPSCounter.cpp:137
visionx::FPSCounter::update
void update()
Updates the FPS counter.
Definition: FPSCounter.cpp:60
visionx::FPSCounter::getUpdates
int getUpdates()
Get number of updates.
Definition: FPSCounter.cpp:122
visionx::FPSCounter::reset
void reset()
Resets the FPS counter to its initial state.
Definition: FPSCounter.cpp:49
visionx::FPSCounter::getValid
bool getValid()
Get if calculated values are valid.
Definition: FPSCounter.cpp:117
visionx::FPSCounter::getMaxCycleTimeMS
float getMaxCycleTimeMS()
Get maximum cycle time since start.
Definition: FPSCounter.cpp:164
visionx::FPSCounter::assureFPS
void assureFPS(float fFrameRate)
Synchronize to FPS.
Definition: FPSCounter.cpp:82
float
#define float
Definition: 16_Level.h:22
FPSCounter.h
visionx::FPSCounter::recalculate
void recalculate()
recalculates the FPS statistics
Definition: FPSCounter.cpp:72