CTRE Phoenix C++ 5.33.1
MovingAverage.h
Go to the documentation of this file.
1/* Copyright (C) Cross The Road Electronics 2024 */
2/*
3 * Software License Agreement
4 *
5 * Copyright (C) Cross The Road Electronics. All rights
6 * reserved.
7 *
8 * Cross The Road Electronics (CTRE) licenses to you the right to
9 * use, publish, and distribute copies of CRF (Cross The Road) firmware files (*.crf) and Software
10 * API Libraries ONLY when in use with Cross The Road Electronics hardware products.
11 *
12 * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
13 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
14 * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
15 * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * CROSS THE ROAD ELECTRONICS BE LIABLE FOR ANY INCIDENTAL, SPECIAL,
17 * INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
18 * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
19 * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
20 * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
21 * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
22 * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE
23 */
24
25namespace ctre {
26namespace phoenix {
27namespace signals {
28
29/**
30 * Class to calculate the rolling average of a signal
31 */
33private:
34
35 int _in; //!< head ptr for ringbuffer
36 int _ou; //!< tail ptr for ringbuffer
37 int _cnt; //!< number of element in ring buffer
38 int _cap; //!< capacity of ring buffer
39 float _sum; //!< sum of all elements in ring buffer
40 float * _d; //!< ring buffer
41public:
42 /**
43 * Constructor for a MovingAverage Object
44 * @param capacity maximum number of items this will hold
45 */
46 MovingAverage(int capacity) {
47 _cap = capacity;
48 _d = new float[_cap];
49 Clear();
50 }
51 /**
52 * Add input & calculate average
53 * @param input Value to add
54 * @return new average
55 */
56 float Process(float input) {
57 Push(input);
58 return _sum / (float) _cnt;
59 }
60 /**
61 * Clears all data points
62 */
63 void Clear() {
64 _in = 0;
65 _ou = 0;
66 _cnt = 0;
67
68 _sum = 0;
69 }
70 /**
71 * Add new item
72 * @param d item to add
73 */
74 void Push(float d) {
75 /* process it */
76 _sum += d;
77
78 /* if full, pop one */
79 if (_cnt >= _cap)
80 Pop();
81
82 /* push new one */
83 _d[_in] = d;
84 if (++_in >= _cap)
85 _in = 0;
86 ++_cnt;
87 }
88 /**
89 * Pull out oldest item
90 */
91 void Pop() {
92 /* get the oldest */
93 float d = _d[_ou];
94
95 /* process it */
96 _sum -= d;
97
98 /* pop it */
99 if (++_ou >= _cap)
100 _ou = 0;
101 --_cnt;
102 }
103 //-------------- Properties --------------//
104 /**
105 * @return the sum of the items
106 */
107 float GetSum() {
108 return _sum;
109 }
110 /**
111 * @return the count of the items
112 */
113 int GetCount() {
114 return _cnt;
115 }
116};
117
118} // namespace Signals
119} // namespace phoenix
120} // namespace ctre
121
Class to calculate the rolling average of a signal.
Definition: MovingAverage.h:32
void Pop()
Pull out oldest item.
Definition: MovingAverage.h:91
float Process(float input)
Add input & calculate average.
Definition: MovingAverage.h:56
MovingAverage(int capacity)
Constructor for a MovingAverage Object.
Definition: MovingAverage.h:46
void Push(float d)
Add new item.
Definition: MovingAverage.h:74
int GetCount()
Definition: MovingAverage.h:113
float GetSum()
Definition: MovingAverage.h:107
void Clear()
Clears all data points.
Definition: MovingAverage.h:63
namespace ctre
Definition: paramEnum.h:5