add simple linear calibrator
This commit is contained in:
parent
de141c6ed6
commit
99311b0375
@ -1,6 +1,7 @@
|
|||||||
#include <catch2/catch_test_macros.hpp>
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
#include "filter_fir_crude.h"
|
#include "filter_fir_crude.h"
|
||||||
|
#include "ICalibrator.h"
|
||||||
|
|
||||||
TEST_CASE("Filter Modules", "[filter]")
|
TEST_CASE("Filter Modules", "[filter]")
|
||||||
{
|
{
|
||||||
@ -15,4 +16,18 @@ TEST_CASE("Filter Modules", "[filter]")
|
|||||||
|
|
||||||
REQUIRE(res == 10);
|
REQUIRE(res == 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("Linear Calibration")
|
||||||
|
{
|
||||||
|
std::pair<double, double> p1 (1, 1);
|
||||||
|
std::pair<double, double> p2 (10, 10);
|
||||||
|
|
||||||
|
auto pc = std::make_pair(p1, p2);
|
||||||
|
floatpump::Calibrator<floatpump::LinearCalibrator> calibrator (pc);
|
||||||
|
|
||||||
|
REQUIRE(calibrator.translate(1) == 1);
|
||||||
|
REQUIRE(calibrator.translate(5) == 5);
|
||||||
|
REQUIRE(calibrator.translate(10) == 10);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
43
floatpump/Modules/ICalibrator.h
Normal file
43
floatpump/Modules/ICalibrator.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <bits/unique_ptr.h>
|
||||||
|
|
||||||
|
namespace floatpump {
|
||||||
|
|
||||||
|
struct ICalibrator {
|
||||||
|
virtual ~ICalibrator() = default;
|
||||||
|
virtual auto translate(double measurand) -> double = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct LinearCalibrator
|
||||||
|
{
|
||||||
|
LinearCalibrator(std::pair<std::pair<double, double>, std::pair<double, double>>);
|
||||||
|
auto translate(double measurand) -> double;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::pair<std::pair<double, double>, std::pair<double, double>> _calibration_points;
|
||||||
|
double _a;
|
||||||
|
double _b;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class Calibrator : public ICalibrator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
template<typename ...As>
|
||||||
|
explicit Calibrator(As&&... args)
|
||||||
|
{
|
||||||
|
_internal_calibrator = std::make_unique<T>(std::forward<As>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
double translate(double measurand) override
|
||||||
|
{
|
||||||
|
return _internal_calibrator->translate(measurand);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<T> _internal_calibrator;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
20
floatpump/Modules/LinearCalibrator.cpp
Normal file
20
floatpump/Modules/LinearCalibrator.cpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
//
|
||||||
|
// Created by robtor on 6/18/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "ICalibrator.h"
|
||||||
|
|
||||||
|
namespace floatpump {
|
||||||
|
LinearCalibrator::LinearCalibrator(std::pair<std::pair<double, double>, std::pair<double, double>> 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
|
Loading…
x
Reference in New Issue
Block a user