F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
RateLimiter.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title RateLimiter.hpp
3 // \author vwong
4 // \brief hpp file for a rate limiter utility class
5 //
6 // \copyright
7 // Copyright (C) 2009-2020 California Institute of Technology.
8 //
9 // ALL RIGHTS RESERVED. United States Government Sponsorship
10 // acknowledged.
11 // ======================================================================
12 
13 #ifndef RateLimiter_HPP
14 #define RateLimiter_HPP
15 
16 #include <Fw/FPrimeBasicTypes.hpp>
17 #include <Fw/Time/Time.hpp>
18 
19 namespace Utils {
20 
21 class RateLimiter {
22  public:
23  // Construct with defined cycles
24  RateLimiter(U32 counterCycle, U32 timeCycle);
25 
26  // Construct with cycles set to 0
27  RateLimiter();
28 
29  public:
30  // Adjust cycles at run-time
31  void setCounterCycle(U32 counterCycle);
32  void setTimeCycle(U32 timeCycle);
33 
34  // Main point of entry
35  //
36  // It will only factor in counter or time, whichever one has a cycle defined
37  //
38  // If both are defined, then satisfying _either_ one will work
39  // e.g. I want to trigger only once every X times or once every Y
40  // seconds, whichever comes first
41  //
42  // The argument-less version is a shorthand for counter-only RateLimiters
43  // If a time cycle is defined but the argument-less version is called,
44  // RateLimiter assumes the client forgot to supply a time, and asserts
45  //
46  bool trigger(Fw::Time time);
47  bool trigger();
48 
49  // Manual state adjustments, if necessary
50  void reset();
51  void resetCounter();
52  void resetTime();
53  void setCounter(U32);
54  void setTime(Fw::Time time);
55 
56  private:
57  // Helper functions to update each independently
58  bool shouldCounterTrigger();
59  bool shouldTimeTrigger(Fw::Time time);
60  void updateCounter(bool triggered);
61  void updateTime(bool triggered, Fw::Time time);
62 
63  private:
64  // parameters
65  U32 m_counterCycle;
66  U32 m_timeCycle;
67 
68  // state
69  U32 m_counter;
70  Fw::Time m_time;
71  bool m_timeAtNegativeInfinity;
72 };
73 
74 } // end namespace Utils
75 
76 #endif
void setCounter(U32)
Definition: RateLimiter.cpp:46
void setTime(Fw::Time time)
Definition: RateLimiter.cpp:50
void setCounterCycle(U32 counterCycle)
Definition: RateLimiter.cpp:24
void setTimeCycle(U32 timeCycle)
Definition: RateLimiter.cpp:28