diff --git a/cmake/boardcore.cmake b/cmake/boardcore.cmake index 49c749382bc5f0c28f9c8cadaaa283afae039ec5..100029d00172ed91f09a30dda3880ab27bc53d0f 100644 --- a/cmake/boardcore.cmake +++ b/cmake/boardcore.cmake @@ -41,6 +41,7 @@ foreach(OPT_BOARD ${BOARDS}) ${SBS_BASE}/src/shared/algorithms/AirBrakes/AirBrakesInterp.cpp ${SBS_BASE}/src/shared/algorithms/NAS/NAS.cpp ${SBS_BASE}/src/shared/algorithms/NAS/StateInitializer.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 0000000000000000000000000000000000000000..72e609911d976460e79c97ea13c8427fa6102d5f --- /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 0000000000000000000000000000000000000000..d562d8015278de4532e127122f8ee86648dbe6bd --- /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