Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Filter Development #68

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions include/kodlab_mjbots_sdk/filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @file filter.h
* @author Lucien C. Peach ([email protected])
* @brief Abstract base class for support of filter implementation.
* @date 9/10/2022
*
* @copyright 2022 The Trustees of the University of Pennsylvania. All rights reserved.
*
*/

#pragma once

#include "Eigen/Geometry"

namespace kodlab
{
lucienpeach marked this conversation as resolved.
Show resolved Hide resolved

class Filter {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[General] Filter should be a class template. At a minimum, it should be assumed that Filter takes in some kind of scalar type (e.g., int, float, etc.). Abstracting further, it could take in containers containing scalars (e.g., Eigen::Vector<float, ...>, std::array<double, ...>, std::vector<int>, etc.). Your current implementation only accepts Eigen::Vector3f data, which is not always the desired type.

For the scalar case, this might be

Suggested change
class Filter {
template<typename Scalar>
class Filter {

Then, the data types inside would be Scalar instead of Eigen::Vector3f.


public:

// Pure virtual function for filter update calls
lucienpeach marked this conversation as resolved.
Show resolved Hide resolved
virtual Eigen::Vector3f Update(Eigen::Vector3f raw_data) = 0;
lucienpeach marked this conversation as resolved.
Show resolved Hide resolved

// Accessor for raw_data_
Eigen::Vector3f GetRawData() {
lucienpeach marked this conversation as resolved.
Show resolved Hide resolved
return raw_data_;
}

// Accessor for filtered_data_
Eigen::Vector3f GetFilteredData() {
return filtered_data_;
}

protected:

Eigen::Vector3f raw_data_;
Eigen::Vector3f filtered_data_;

};

} // kodlab::mjbots
lucienpeach marked this conversation as resolved.
Show resolved Hide resolved

67 changes: 67 additions & 0 deletions include/kodlab_mjbots_sdk/low_pass_filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @file low_pass_filter.h
* @author Lucien C. Peach ([email protected])
* @brief A discrete-time low-pass filter class.
* @date 9/10/2022
*
* @copyright 2022 The Trustees of the University of Pennsylvania. All rights reserved.
*
*/

#pragma once

#include "kodlab_mjbots_sdk/filter.h"
#include "Eigen/Geometry"
lucienpeach marked this conversation as resolved.
Show resolved Hide resolved

namespace kodlab
{

class LowPassFilter : public Filter {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[General] Same comment as in Filter. This should probably be a class template.
Note that, for this class, both the definition and implementation would have to live in the header file if you make this a class template. See here for more information.


public:

LowPassFilter(float dt, float k_frequency_cutoff, Eigen::Vector3f raw_data);

float FilterGain(const float k_frequency_cutoff);

Eigen::Vector3f Update(Eigen::Vector3f raw_data) override;

// Accessor for dt_
float GetDt() {
return dt_;
}

// Accessor for k_frequency_cutoff_
float GetCutoffFreq() {
return k_frequency_cutoff_;
}

// Accessor for alpha_
float GetAlpha() {
return alpha_;
}


private:

float dt_;
float k_frequency_cutoff_;
float alpha_;

};

} //kodlab



/* Notes for further additions

//main.cpp

LowPassFilter<Eigen::Vector3f> lpf (dt, cutoff );
while (True){
//DO STUFF
filt_data = lpf.Update(data);

//DO STUFF
} */
lucienpeach marked this conversation as resolved.
Show resolved Hide resolved
45 changes: 45 additions & 0 deletions src/low_pass_filter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @file low_pass_filter.cpp
* @author Lucien C. Peach ([email protected])
* @brief Implementation of a discrete-time low-pass filter class.
* @date 9/10/2022
*
* @copyright 2022 The Trustees of the University of Pennsylvania. All rights reserved.
*
*/

#include "kodlab_mjbots_sdk/low_pass_filter.h"
#include <math.h>
#include <Eigen/Geometry>
lucienpeach marked this conversation as resolved.
Show resolved Hide resolved

namespace kodlab
{

LowPassFilter::LowPassFilter(float dt, float k_frequency_cutoff, Eigen::Vector3f raw_data) {

dt_ = dt;
raw_data_ = raw_data;
filtered_data_ = raw_data;
k_frequency_cutoff_ = k_frequency_cutoff;

};
Comment on lines +18 to +25
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[General] Per this ISO C++ Standard item, you should use an initializer list here instead of assignment.


float LowPassFilter::FilterGain(float k_frequency_cutoff_) {

float k_filter_time_constant = 1.0 / (2 * M_PI * k_frequency_cutoff_);

float alpha_ = dt_ / (k_filter_time_constant + dt_);

}

Eigen::Vector3f LowPassFilter::Update(Eigen::Vector3f raw_data) {

filtered_data_ = alpha_ * raw_data + (1 - alpha_) * filtered_data_;
raw_data = raw_data;

}

// Future Version: Include Reinitialize / Set Filter Data:
lucienpeach marked this conversation as resolved.
Show resolved Hide resolved

} // kodlab