blob: 013822c587f5d7e522fd09ba42cab9e30f4415ee [file] [log] [blame]
Patrick Venturee6206562018-03-08 15:36:53 -08001#pragma once
2
3#include <chrono>
4
Patrick Venturea0764872020-08-08 07:48:43 -07005namespace pid_control
6{
7
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07008struct ReadReturn
9{
ykchiu7c6d35d2023-05-10 17:01:46 +080010 double value = std::numeric_limits<double>::quiet_NaN();
Patrick Venturee6206562018-03-08 15:36:53 -080011 std::chrono::high_resolution_clock::time_point updated;
Josh Lehanb3005752022-02-22 20:48:07 -080012 double unscaled = value;
Patrick Venturebd6b4762018-06-14 09:24:42 -070013
Patrick Venturee2ec0f62018-09-04 12:30:27 -070014 bool operator==(const ReadReturn& rhs) const
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070015 {
Josh Lehanb3005752022-02-22 20:48:07 -080016 return ((this->value == rhs.value) && (this->updated == rhs.updated) &&
17 (this->unscaled == rhs.unscaled));
Patrick Venturebd6b4762018-06-14 09:24:42 -070018 }
Patrick Venturee6206562018-03-08 15:36:53 -080019};
20
Josh Lehanb3005752022-02-22 20:48:07 -080021struct ValueCacheEntry
22{
23 // This is normalized to (0.0, 1.0) range, using configured min and max
24 double scaled;
25
Manojkiran Eda7ca88872024-06-17 11:55:48 +053026 // This is the raw value, as received from the input/output sensors
Josh Lehanb3005752022-02-22 20:48:07 -080027 double unscaled;
28};
29
Patrick Venturee6206562018-03-08 15:36:53 -080030/*
31 * A ReadInterface is a plug-in for the PluggableSensor and anyone implementing
32 * this basically is providing a way to read a sensor.
33 */
34class ReadInterface
35{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070036 public:
Patrick Williams8c051122023-05-10 07:50:59 -050037 ReadInterface() {}
Patrick Venturee6206562018-03-08 15:36:53 -080038
Patrick Williams8c051122023-05-10 07:50:59 -050039 virtual ~ReadInterface() {}
Patrick Venturee6206562018-03-08 15:36:53 -080040
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070041 virtual ReadReturn read(void) = 0;
James Feist36b7d8e2018-10-05 15:39:01 -070042
43 virtual bool getFailed(void) const
44 {
45 return false;
46 }
Harvey Wua4270072024-05-29 16:11:13 +080047
48 virtual std::string getFailReason(void) const
49 {
50 return "Unimplemented";
51 }
Patrick Venturee6206562018-03-08 15:36:53 -080052};
53
54/*
55 * A WriteInterface is a plug-in for the PluggableSensor and anyone implementing
56 * this basically is providing a way to write a sensor.
57 */
58class WriteInterface
59{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070060 public:
Patrick Williams8c051122023-05-10 07:50:59 -050061 WriteInterface(int64_t min, int64_t max) : _min(min), _max(max) {}
Patrick Venturee6206562018-03-08 15:36:53 -080062
Patrick Williams8c051122023-05-10 07:50:59 -050063 virtual ~WriteInterface() {}
Patrick Venturee6206562018-03-08 15:36:53 -080064
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070065 virtual void write(double value) = 0;
Patrick Venturee6206562018-03-08 15:36:53 -080066
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070067 /*
Josh Lehan2400ce42020-10-01 01:50:39 -070068 * A wrapper around write(), with additional parameters.
69 * force = true to perform redundant write, even if raw value unchanged.
70 * written = non-null to be filled in with the actual raw value written.
71 */
72 virtual void write(double value, bool force, int64_t* written)
73 {
74 (void)force;
75 (void)written;
76 return write(value);
77 }
78
79 /*
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070080 * All WriteInterfaces have min/max available in case they want to error
81 * check.
82 */
83 int64_t getMin(void)
84 {
85 return _min;
86 }
87 int64_t getMax(void)
88 {
89 return _max;
90 }
91
92 private:
93 int64_t _min;
94 int64_t _max;
Patrick Venturee6206562018-03-08 15:36:53 -080095};
Patrick Venturea0764872020-08-08 07:48:43 -070096
97} // namespace pid_control