From 99311b0375a2c3a27975f927b4f4e39973e764cd Mon Sep 17 00:00:00 2001 From: robtor Date: Thu, 19 Jun 2025 11:15:13 +0200 Subject: [PATCH] add simple linear calibrator --- Tests/modules.cpp | 15 +++++++++ floatpump/Modules/ICalibrator.h | 43 ++++++++++++++++++++++++++ floatpump/Modules/LinearCalibrator.cpp | 20 ++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 floatpump/Modules/ICalibrator.h create mode 100644 floatpump/Modules/LinearCalibrator.cpp diff --git a/Tests/modules.cpp b/Tests/modules.cpp index 42e971e..88ea8ea 100644 --- a/Tests/modules.cpp +++ b/Tests/modules.cpp @@ -1,6 +1,7 @@ #include #include "filter_fir_crude.h" +#include "ICalibrator.h" TEST_CASE("Filter Modules", "[filter]") { @@ -15,4 +16,18 @@ TEST_CASE("Filter Modules", "[filter]") REQUIRE(res == 10); } + + SECTION("Linear Calibration") + { + std::pair p1 (1, 1); + std::pair p2 (10, 10); + + auto pc = std::make_pair(p1, p2); + floatpump::Calibrator calibrator (pc); + + REQUIRE(calibrator.translate(1) == 1); + REQUIRE(calibrator.translate(5) == 5); + REQUIRE(calibrator.translate(10) == 10); + + } } \ No newline at end of file diff --git a/floatpump/Modules/ICalibrator.h b/floatpump/Modules/ICalibrator.h new file mode 100644 index 0000000..387762d --- /dev/null +++ b/floatpump/Modules/ICalibrator.h @@ -0,0 +1,43 @@ +#pragma once +#include + +namespace floatpump { + +struct ICalibrator { + virtual ~ICalibrator() = default; + virtual auto translate(double measurand) -> double = 0; +}; + +struct LinearCalibrator +{ + LinearCalibrator(std::pair, std::pair>); + auto translate(double measurand) -> double; + +private: + std::pair, std::pair> _calibration_points; + double _a; + double _b; +}; + + + +template +class Calibrator : public ICalibrator + { + public: + template + explicit Calibrator(As&&... args) + { + _internal_calibrator = std::make_unique(std::forward(args)...); + } + + double translate(double measurand) override + { + return _internal_calibrator->translate(measurand); + } + + private: + std::unique_ptr _internal_calibrator; + }; + +} \ No newline at end of file diff --git a/floatpump/Modules/LinearCalibrator.cpp b/floatpump/Modules/LinearCalibrator.cpp new file mode 100644 index 0000000..f214f65 --- /dev/null +++ b/floatpump/Modules/LinearCalibrator.cpp @@ -0,0 +1,20 @@ +// +// Created by robtor on 6/18/25. +// + +#include "ICalibrator.h" + +namespace floatpump { + LinearCalibrator::LinearCalibrator(std::pair, std::pair> points) + { + _a = 1.0 * (points.second.second - points.first.second) / (points.second.first - points.first.first); + _b = points.first.second - (_a * points.first.first); + _calibration_points = std::move(points); + } + + + auto LinearCalibrator::translate(double measurand) -> double + { + return _a * measurand + _b; + } +} // floatpump \ No newline at end of file