SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
angle_correction.cpp
Go to the documentation of this file.
1#include "angle_correction.h"
2
3namespace sensesp {
4
5constexpr double kPi = 3.14159265358979323846;
6
7AngleCorrection::AngleCorrection(float offset, float min_angle,
8 const String& config_path)
9 : FloatTransform(config_path), offset_{offset}, min_angle_{min_angle} {
10 load();
11}
12
13void AngleCorrection::set(const float& input) {
14 // first the correction
15 float x = input + offset_;
16
17 // then wrap around the values
18 x = fmod(x - min_angle_, 2 * kPi);
19 if (x < 0) {
20 x += 2 * kPi;
21 }
22 this->emit(x + min_angle_);
23}
24
25bool AngleCorrection::to_json(JsonObject& root) {
26 root["offset"] = offset_;
27 root["min_angle"] = min_angle_;
28 return true;
29}
30
31bool AngleCorrection::from_json(const JsonObject& config) {
32 String const expected[] = {"offset", "min_angle"};
33 for (auto str : expected) {
34 if (!config[str].is<JsonVariant>()) {
35 return false;
36 }
37 }
38 offset_ = config["offset"];
39 min_angle_ = config["min_angle"];
40 return true;
41}
42
43const String ConfigSchema(const AngleCorrection& obj) {
44 return R"###({"type":"object","properties":{"offset":{"title":"Constant offset","description":"Value to be added, in degrees","type":"number","displayMultiplier":0.017453292519943295,"displayOffset":0},"min_angle":{"title":"Minimum angle value","description":"Typically 0 or -180.","type":"number","displayMultiplier":0.017453292519943295,"displayOffset":0}}})###";
45}
46
47} // namespace sensesp
Add a value to an angle input (in radians). The output value is wrapped to a range between [0,...
AngleCorrection(float offset, float min_angle=0, const String &config_path="")
virtual void set(const float &input) override
virtual bool from_json(const JsonObject &config) override
virtual bool to_json(JsonObject &root) override
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
void emit(const float &new_value)
const String ConfigSchema(const SmartSwitchController &obj)
constexpr double kPi