Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <chrono> |
| 4 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 5 | namespace pid_control |
| 6 | { |
| 7 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 8 | struct ReadReturn |
| 9 | { |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 10 | double value; |
| 11 | std::chrono::high_resolution_clock::time_point updated; |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame^] | 12 | double unscaled = value; |
Patrick Venture | bd6b476 | 2018-06-14 09:24:42 -0700 | [diff] [blame] | 13 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 14 | bool operator==(const ReadReturn& rhs) const |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 15 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame^] | 16 | return ((this->value == rhs.value) && (this->updated == rhs.updated) && |
| 17 | (this->unscaled == rhs.unscaled)); |
Patrick Venture | bd6b476 | 2018-06-14 09:24:42 -0700 | [diff] [blame] | 18 | } |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 19 | }; |
| 20 | |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame^] | 21 | struct ValueCacheEntry |
| 22 | { |
| 23 | // This is normalized to (0.0, 1.0) range, using configured min and max |
| 24 | double scaled; |
| 25 | |
| 26 | // This is the raw value, as recieved from the input/output sensors |
| 27 | double unscaled; |
| 28 | }; |
| 29 | |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 30 | /* |
| 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 | */ |
| 34 | class ReadInterface |
| 35 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 36 | public: |
| 37 | ReadInterface() |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 38 | {} |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 39 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 40 | virtual ~ReadInterface() |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 41 | {} |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 42 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 43 | virtual ReadReturn read(void) = 0; |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 44 | |
| 45 | virtual bool getFailed(void) const |
| 46 | { |
| 47 | return false; |
| 48 | } |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * A WriteInterface is a plug-in for the PluggableSensor and anyone implementing |
| 53 | * this basically is providing a way to write a sensor. |
| 54 | */ |
| 55 | class WriteInterface |
| 56 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 57 | public: |
| 58 | WriteInterface(int64_t min, int64_t max) : _min(min), _max(max) |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 59 | {} |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 60 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 61 | virtual ~WriteInterface() |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 62 | {} |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 63 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 64 | virtual void write(double value) = 0; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 65 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 66 | /* |
Josh Lehan | 2400ce4 | 2020-10-01 01:50:39 -0700 | [diff] [blame] | 67 | * A wrapper around write(), with additional parameters. |
| 68 | * force = true to perform redundant write, even if raw value unchanged. |
| 69 | * written = non-null to be filled in with the actual raw value written. |
| 70 | */ |
| 71 | virtual void write(double value, bool force, int64_t* written) |
| 72 | { |
| 73 | (void)force; |
| 74 | (void)written; |
| 75 | return write(value); |
| 76 | } |
| 77 | |
| 78 | /* |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 79 | * All WriteInterfaces have min/max available in case they want to error |
| 80 | * check. |
| 81 | */ |
| 82 | int64_t getMin(void) |
| 83 | { |
| 84 | return _min; |
| 85 | } |
| 86 | int64_t getMax(void) |
| 87 | { |
| 88 | return _max; |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | int64_t _min; |
| 93 | int64_t _max; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 94 | }; |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 95 | |
| 96 | } // namespace pid_control |