Skip to content

Commit

Permalink
modified: src/ExponentialFilter.cpp
Browse files Browse the repository at this point in the history
	modified:   src/ExponentialFilter.h
  • Loading branch information
MicroBeaut committed Apr 18, 2024
1 parent 4b6e45c commit 3f463cd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
6 changes: 4 additions & 2 deletions src/ExponentialFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "ExponentialFilter.h"

ExponentialFilter::ExponentialFilter(): output(_output), input(_input), id(_id) {
ExponentialFilter::ExponentialFilter(): output(_output), input(_input), id(_id), trigger(_trigger) {
cutoffTime(1);
_mode = AUTO;
}
Expand All @@ -15,11 +15,13 @@ void ExponentialFilter::init(float input) {
_output = (float)input;
}


void ExponentialFilter::eventOnTrigger(uint8_t id, ExpCallbackFunction function) {
_id = id;
_function = function;
}


void ExponentialFilter::cutoffTime(float cutoffTime) {
_timeConstant = cutoffTime / TWO_PI;
}
Expand Down Expand Up @@ -63,6 +65,7 @@ bool ExponentialFilter::schmittTrigger(ExpTrigger *trigger) {
trigger->upperTrigger = false;
bool prevTrigger = trigger->trigger;
trigger->trigger = internalTriggerCompare(trigger);
_trigger = trigger->trigger;
trigger->upperTrigger = trigger->trigger == true & prevTrigger == false;
trigger->lowerTrigger = trigger->trigger == false & prevTrigger == true;
internalOnTrigger(trigger);
Expand All @@ -80,7 +83,6 @@ void ExponentialFilter::internalOnTrigger(ExpTrigger *trigger) {
e.state = UPPER_TRIGGER;
_function(e);
}

e.state = NONE_TRIGGER;
}
}
Expand Down
53 changes: 34 additions & 19 deletions src/ExponentialFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#define TWO_PI 6.283185307179586476925286766559
#endif // TWO_PI


#define S2MS(sec) (sec * 1000)
#define S2US(sec) (sec * 1000000)
#define MS2S(ms) (ms * 0.001)
Expand All @@ -24,6 +23,7 @@ enum ExpModes {
MANUAL
};


/// @brief The State Event Arguments
enum StateEventArgs {
/// @brief Maintained the last state
Expand All @@ -46,22 +46,24 @@ enum ExpOperations {
GE
};


/// @brief Comparing the upper and lower thresholds for a trigger using exponential double operations.
enum ExpDoubleOperations {
/// @brief If the output is less than the setting, set the trigger state to false;
/// @brief if output is greater than the setting, set the trigger state to true.
/// @brief If the output is less than the upper threshold setting, set the trigger state to false;
/// @brief if output is greater than the lower threshold setting, set the trigger state to true.
LTGT,
/// @brief If the output is greater than the setting, set the trigger state to false;
/// @brief if output is less than the setting, set the trigger state to true.
/// @brief If the output is greater than the upper threshold setting, set the trigger state to false;
/// @brief if output is less than the lower threshold setting, set the trigger state to true.
GTLT,
/// @brief If the output is less than or equal to the setting, set the trigger state to false;
/// @brief if the output is greater than or equal to the setting, set the trigger state to true.
/// @brief If the output is less than or equal to the upper threshold setting, set the trigger state to false;
/// @brief if the output is greater than or equal to the lower threshold setting, set the trigger state to true.
LEGE,
/// @brief If the output is greater than or equal to the setting, set the trigger state to false;
/// @brief if output is less than the setting, set the trigger state to true.
/// @brief If the output is greater than or equal to the upper threshold setting, set the trigger state to false;
/// @brief if output is less than or equal to the lower threshold setting, set the trigger state to true.
GELE
};


/// @brief The index compares a trigger's upper and lower thresholds using exponential double operations.
const ExpOperations mapOperations[4][2] = {
{LT, GT}, // LTGT
Expand All @@ -70,6 +72,7 @@ const ExpOperations mapOperations[4][2] = {
{GE, LE} // GELE
};


/// @brief The Exponential Trigger Members
typedef struct {
/// @brief Lower Threshold Setting
Expand All @@ -86,7 +89,8 @@ typedef struct {
bool upperTrigger;
} ExpTrigger;

/// @brief Exponential Event Argument

/// @brief Exponential Event Argument
typedef struct {
/// @brief Exponential Id
uint8_t id;
Expand All @@ -107,6 +111,7 @@ class ExponentialFilter {
float _timeConstant;
float _input;
float _output;
bool _trigger;
bool _mode;
uint8_t _id;
ExpCallbackFunction _function;
Expand All @@ -119,38 +124,48 @@ class ExponentialFilter {
public:
float &input;
float &output;
bool &trigger;
uint8_t &id;

/// @brief Initial Exponential Filter
/// @brief The time constant value set by default is 1.
ExponentialFilter();

/// @brief The proposed option allows for the setting of the time constant based on the cutoff time in an exponential manner.
/// @brief cutoff time = 1/cutoffFrequency
/// @param cutoffTime
/// @param cutoffTime
void cutoffTime(float cutoffTime);

/// @brief The proposed option allows for the setting of the time constant based on the cutoff frequency in an exponential manner.
/// @param cutoffFrequency
/// @param cutoffFrequency
void cutoffFrequency(float cutoffFrequency);

/// @brief The proposed option allows for the setting of the time constant based on the time constant in an exponential manner.
/// @param timeConstant
/// @param timeConstant
void timeConstant(float timeConstant);

/// @brief The mode selection is responsible for filtering the input, and by default, it is set to AUTO mode.
/// @brief If the mode is switched to MANUAL mode, the output is equal to the input.
/// @param mode
/// @param mode
void mode(bool mode);

/// @brief The suggested alternative provides the feasibility of commencing the output with a predetermined value.
/// @param input
/// @param input
void init(float input);

/// @brief Implemented the event callback on the trigger changed with the provided ID for the option that features the array input/output.
/// @param id
/// @param function
/// @param id
/// @param function
void eventOnTrigger(uint8_t id, ExpCallbackFunction function);

/// @brief Exponential filters smooth noisy input signals in signal processing by suppressing noise and producing a stable output
/// @param input
/// @Return a stable output in a float data type
/// @param input
/// @return a stable output in a float data type
float filter(float input);

/// @brief The Schmitt Trigger compares the output to upper and lower thresholds and has multiple operation options to determine its trigger state.
/// @param *trigger
/// @return a trigger state
bool schmittTrigger(ExpTrigger *trigger);
};

Expand Down

0 comments on commit 3f463cd

Please sign in to comment.