Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
IPS.hpp
Go to the documentation of this file.
1
11#ifndef IPS_HPP
12#define IPS_HPP
13
15#include <algorithm>
16#include <chrono>
17#include <deque>
18
20
21
29class IPS
30{
31 private:
32 // Declare private methods and member variables.
33 double m_dCurrentIPS;
34 double m_dHighestIPS;
35 double m_dLowestIPS;
36 double m_d1PercentLow;
37 std::deque<double> m_dqIPSHistory;
38 std::chrono::high_resolution_clock::time_point m_tLastUpdateTime;
39
40 // Define class constants.
41 const long unsigned int m_nMaxMetricsHistorySize = 100;
42
43
52 {
53 // Check highest IPS.
54 if (m_dCurrentIPS > m_dHighestIPS)
55 {
56 // Update new highest.
57 m_dHighestIPS = m_dCurrentIPS;
58 }
59 // Check lowest IPS.
60 if (m_dCurrentIPS < m_dLowestIPS)
61 {
62 // Update new lowest.
63 m_dLowestIPS = m_dCurrentIPS;
64 }
65
66 // Add current IPS to history.
67 m_dqIPSHistory.emplace_back(m_dCurrentIPS);
68 // Sort the IPS history vector, this allows us to find the 1% low more easily.
69 // Throw out oldest element in deque if size is over given number.
70 if (m_dqIPSHistory.size() > m_nMaxMetricsHistorySize)
71 {
72 m_dqIPSHistory.pop_front();
73 }
74 // Get the index correlating to the IPS number that was lower than 99% of the rest of the IPS history.
75 m_d1PercentLow = *std::min_element(m_dqIPSHistory.begin(), m_dqIPSHistory.end());
76 }
77
78 public:
79 // Declare public methods and member variables.
80
88 {
89 // Initialize member variables and objects.
90 m_dCurrentIPS = 0.0;
91 m_dHighestIPS = 0.0;
92 m_dLowestIPS = 9999999;
93 m_d1PercentLow = 0.0;
94 }
95
96
104 {
105 // Nothing to destroy.
106 }
107
108
117 IPS& operator=(const IPS& OtherIPS)
118 {
119 // Copy values from other IPS object.
120 m_dCurrentIPS = OtherIPS.m_dCurrentIPS;
121 m_dHighestIPS = OtherIPS.m_dHighestIPS;
122 m_dLowestIPS = OtherIPS.m_dLowestIPS;
123 m_d1PercentLow = OtherIPS.m_d1PercentLow;
124 m_dqIPSHistory = OtherIPS.m_dqIPSHistory;
125
126 // Return this object.
127 return *this;
128 }
129
130
138 void Tick()
139 {
140 // Get the current and elapsed time.
141 std::chrono::time_point tCurrentTime = std::chrono::high_resolution_clock::now();
142 long int nElapsedTimeSinceLastTick = std::chrono::duration_cast<std::chrono::microseconds>(tCurrentTime - m_tLastUpdateTime).count();
143
144 // Calculate current IPS. 1 second == 1000000 microseconds.
145 m_dCurrentIPS = 1e6 / static_cast<double>(nElapsedTimeSinceLastTick);
146
147 // Calculate IPS overall stats.
148 this->UpdateMetrics();
149
150 // Set current time to old.
151 m_tLastUpdateTime = tCurrentTime;
152 }
153
154
165 double GetExactIPS() const
166 {
167 // Return current iterations per second.
168 return m_dCurrentIPS;
169 }
170
171
180 double GetAverageIPS() const
181 {
182 // Check if there is any history.
183 if (m_dqIPSHistory.empty())
184 {
185 // Return zero IPS average.
186 return 0.0;
187 }
188
189 // Loop through IPS history and calculate average.
190 double dSum = 0.0;
191 for (const double dVal : m_dqIPSHistory)
192 {
193 // Add FPS to total sum.
194 dSum += dVal;
195 }
196
197 // Return calculated average.
198 return dSum / static_cast<double>(m_dqIPSHistory.size());
199 }
200
201
209 double GetHighestIPS() const
210 {
211 // Return the highest iterations per second.
212 return m_dHighestIPS;
213 }
214
215
223 double GetLowestIPS() const
224 {
225 // Return the lowest iterations per second.
226 return m_dLowestIPS;
227 }
228
229
237 double Get1PercentLow() const
238 {
239 // Return the 1% low for IPS.
240 return m_d1PercentLow;
241 }
242
243
250 void Reset()
251 {
252 // Reset member variable.
253 m_dCurrentIPS = 0.0;
254 m_dHighestIPS = 0.0;
255 m_dLowestIPS = 9999999;
256 m_d1PercentLow = 0.0;
257 // Reset deque.
258 m_dqIPSHistory.clear();
259 }
260};
261#endif
This util class provides an easy way to keep track of iterations per second for any body of code.
Definition IPS.hpp:30
IPS & operator=(const IPS &OtherIPS)
Operator equals for IPS class.
Definition IPS.hpp:117
double GetExactIPS() const
Accessor for the Current I P S private member. This method will return the immediate IPS since the la...
Definition IPS.hpp:165
~IPS()
Construct a new IPS object.
Definition IPS.hpp:103
void Reset()
Resets all metrics and frame time history.
Definition IPS.hpp:250
IPS()
Construct a new IPS object.
Definition IPS.hpp:87
double GetHighestIPS() const
Accessor for the Highest I P S private member.
Definition IPS.hpp:209
double Get1PercentLow() const
Accessor for the 1PercentLow I P S private member.
Definition IPS.hpp:237
void UpdateMetrics()
This method is used to calculate the IPS stats. Highest, lowest, and 1 percent low IPS.
Definition IPS.hpp:51
double GetAverageIPS() const
Calculates the average iterations per second.
Definition IPS.hpp:180
double GetLowestIPS() const
Accessor for the Lowest I P S private member.
Definition IPS.hpp:223
void Tick()
This method is used to update the iterations per second counter and recalculate all of the IPS metric...
Definition IPS.hpp:138