blob: c13c377893d408d408969a10eac31fab153d957a [file] [log] [blame]
Patrick Venturee6206562018-03-08 15:36:53 -08001#pragma once
2
3#include <chrono>
Ed Tanousf8b6e552025-06-27 13:27:50 -07004#include <cstdint>
5#include <limits>
6#include <string>
Patrick Venturee6206562018-03-08 15:36:53 -08007
Patrick Venturea0764872020-08-08 07:48:43 -07008namespace pid_control
9{
10
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070011struct ReadReturn
12{
ykchiu7c6d35d2023-05-10 17:01:46 +080013 double value = std::numeric_limits<double>::quiet_NaN();
Patrick Venturee6206562018-03-08 15:36:53 -080014 std::chrono::high_resolution_clock::time_point updated;
Josh Lehanb3005752022-02-22 20:48:07 -080015 double unscaled = value;
Patrick Venturebd6b4762018-06-14 09:24:42 -070016
Patrick Venturee2ec0f62018-09-04 12:30:27 -070017 bool operator==(const ReadReturn& rhs) const
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070018 {
Josh Lehanb3005752022-02-22 20:48:07 -080019 return ((this->value == rhs.value) && (this->updated == rhs.updated) &&
20 (this->unscaled == rhs.unscaled));
Patrick Venturebd6b4762018-06-14 09:24:42 -070021 }
Patrick Venturee6206562018-03-08 15:36:53 -080022};
23
Josh Lehanb3005752022-02-22 20:48:07 -080024struct ValueCacheEntry
25{
26 // This is normalized to (0.0, 1.0) range, using configured min and max
27 double scaled;
28
Manojkiran Eda7ca88872024-06-17 11:55:48 +053029 // This is the raw value, as received from the input/output sensors
Josh Lehanb3005752022-02-22 20:48:07 -080030 double unscaled;
31};
32
Patrick Venturee6206562018-03-08 15:36:53 -080033/*
34 * A ReadInterface is a plug-in for the PluggableSensor and anyone implementing
35 * this basically is providing a way to read a sensor.
36 */
37class ReadInterface
38{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070039 public:
Ed Tanousd2768c52025-06-26 11:42:57 -070040 ReadInterface() = default;
Patrick Venturee6206562018-03-08 15:36:53 -080041
Ed Tanousd2768c52025-06-26 11:42:57 -070042 virtual ~ReadInterface() = default;
Patrick Venturee6206562018-03-08 15:36:53 -080043
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070044 virtual ReadReturn read(void) = 0;
James Feist36b7d8e2018-10-05 15:39:01 -070045
46 virtual bool getFailed(void) const
47 {
48 return false;
49 }
Harvey Wua4270072024-05-29 16:11:13 +080050
51 virtual std::string getFailReason(void) const
52 {
53 return "Unimplemented";
54 }
Patrick Venturee6206562018-03-08 15:36:53 -080055};
56
57/*
58 * A WriteInterface is a plug-in for the PluggableSensor and anyone implementing
59 * this basically is providing a way to write a sensor.
60 */
61class WriteInterface
62{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070063 public:
Patrick Williams8c051122023-05-10 07:50:59 -050064 WriteInterface(int64_t min, int64_t max) : _min(min), _max(max) {}
Patrick Venturee6206562018-03-08 15:36:53 -080065
Ed Tanousd2768c52025-06-26 11:42:57 -070066 virtual ~WriteInterface() = default;
Patrick Venturee6206562018-03-08 15:36:53 -080067
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070068 virtual void write(double value) = 0;
Patrick Venturee6206562018-03-08 15:36:53 -080069
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070070 /*
Josh Lehan2400ce42020-10-01 01:50:39 -070071 * A wrapper around write(), with additional parameters.
72 * force = true to perform redundant write, even if raw value unchanged.
73 * written = non-null to be filled in with the actual raw value written.
74 */
75 virtual void write(double value, bool force, int64_t* written)
76 {
77 (void)force;
78 (void)written;
79 return write(value);
80 }
81
82 /*
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070083 * All WriteInterfaces have min/max available in case they want to error
84 * check.
85 */
86 int64_t getMin(void)
87 {
88 return _min;
89 }
90 int64_t getMax(void)
91 {
92 return _max;
93 }
94
95 private:
96 int64_t _min;
97 int64_t _max;
Patrick Venturee6206562018-03-08 15:36:53 -080098};
Patrick Venturea0764872020-08-08 07:48:43 -070099
100} // namespace pid_control