Skip to content

Commit

Permalink
User can specify the type that holds the sum of the Moving Average to…
Browse files Browse the repository at this point in the history
… prevent wrapping and overflows.
  • Loading branch information
gabryelreyes committed Sep 30, 2024
1 parent b9a2235 commit f165dd3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/ConvoyFollower/src/DrivingState.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class DrivingState : public IState
int32_t m_cumulativeQueueDistance;

/** Average distance to the predecessor in mm. */
MovAvg<int32_t, IVS_MOVAVG_NUMBER_OF_MEASUREMENTS> m_avgIvs;
MovAvg<int32_t, int32_t, IVS_MOVAVG_NUMBER_OF_MEASUREMENTS> m_avgIvs;

/**
* Get latest waypoint from the queue, validate it and set it to as the current target.
Expand Down
2 changes: 1 addition & 1 deletion lib/HALTarget/src/Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Battery : public IBattery
static const uint32_t REFERENCE_VOLTAGE = 3300U; /**< Reference voltage of the ADCs in millivolts*/
static const uint32_t CONVERSION_FACTOR = 10000U; /**< Conversion factor from measured to real battery voltage. */

MovAvg<uint32_t, 2> m_voltMovAvg; /**< The moving average of the measured voltage over 2 calling cycles. */
MovAvg<uint32_t, uint32_t, 2U> m_voltMovAvg; /**< The moving average of the measured voltage over 2 calling cycles. */
};

/******************************************************************************
Expand Down
5 changes: 3 additions & 2 deletions lib/Utilities/src/MovAvg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@
* It is designed for fix point integers.
*
* @tparam T The data type of the moving average result and input values.
* @tparam U The data type of the moving average sum. Must be able to store the maximum value of T * length.
* @tparam length The number of values, which are considered in the moving average calculation.
*/
template<typename T, uint8_t length>
template<typename T, typename U, uint8_t length>
class MovAvg
{
public:
Expand Down Expand Up @@ -149,7 +150,7 @@ class MovAvg
T m_values[length]; /**< List of values, used for moving average calculation. */
uint8_t m_wrIdx; /**< Write index to list of values */
uint8_t m_written; /**< The number of written values to the list of values, till length is reached. */
T m_sum; /**< Sum of all values */
U m_sum; /**< Sum of all values */

/**
* Copy construction of an instance.
Expand Down

0 comments on commit f165dd3

Please sign in to comment.