From 8093f1f21fbd50efb30c9ab2a13effbd84030d92 Mon Sep 17 00:00:00 2001 From: Federico Lolli <federico.lolli@skywarder.eu> Date: Mon, 16 Oct 2023 17:46:57 +0200 Subject: [PATCH] [LowPass] added first draft of LowPass filter --- cmake/boardcore.cmake | 1 + src/shared/algorithms/Filters/LowPass.cpp | 40 ++++++++++++++++ src/shared/algorithms/Filters/LowPass.h | 56 +++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 src/shared/algorithms/Filters/LowPass.cpp create mode 100644 src/shared/algorithms/Filters/LowPass.h diff --git a/cmake/boardcore.cmake b/cmake/boardcore.cmake index 8d572af01..78cef4792 100644 --- a/cmake/boardcore.cmake +++ b/cmake/boardcore.cmake @@ -43,6 +43,7 @@ foreach(OPT_BOARD ${BOARDS}) ${SBS_BASE}/src/shared/algorithms/NAS/StateInitializer.cpp ${SBS_BASE}/src/shared/algorithms/SFD/SFDAscent.cpp ${SBS_BASE}/src/shared/algorithms/SFD/SFDDescent.cpp + ${SBS_BASE}/src/shared/algorithms/Filters/LowPass.cpp # Debug ${SBS_BASE}/src/shared/utils/Debug.cpp diff --git a/src/shared/algorithms/Filters/LowPass.cpp b/src/shared/algorithms/Filters/LowPass.cpp new file mode 100644 index 000000000..72e609911 --- /dev/null +++ b/src/shared/algorithms/Filters/LowPass.cpp @@ -0,0 +1,40 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Author: Federico Lolli + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "LowPass.h" + +namespace Boardcore +{ + +// TODO: WARNING! initialized at 0 +LowPass::LowPass(float gain, float cutoff_freq, float lambda) + : gain(gain), cutoff_freq(cutoff_freq), lambda(lambda), output(0) +{ +} + +float LowPass::filter(float input) +{ + output = lambda * output + (gain / cutoff_freq) * (1 - lambda) * input; + return output; +} + +} // namespace Boardcore diff --git a/src/shared/algorithms/Filters/LowPass.h b/src/shared/algorithms/Filters/LowPass.h new file mode 100644 index 000000000..d562d8015 --- /dev/null +++ b/src/shared/algorithms/Filters/LowPass.h @@ -0,0 +1,56 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Author: Federico Lolli + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +namespace Boardcore +{ + +class LowPass +{ +public: + /** + * @brief Construct a new Low Pass object + * + * @param gain The gain of the filter + * @param cutoff_freq The cutoff frequency of the filter + * @param lambda The lambda parameter of the filter + * + * @note WARNING: Initialize output at 0 at first + */ + LowPass(float gain, float cutoff_freq, float lambda); + + /** + * @brief Filter the input + * + * @param input The input to filter + */ + float filter(float input); + +private: + float cutoff_freq; + float lambda; + float gain; + float output; +}; + +} // namespace Boardcore -- GitLab