Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <cstdint> |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 4 | #include <string> |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 5 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 6 | namespace pid_control |
| 7 | { |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 8 | namespace ec |
| 9 | { |
| 10 | |
| 11 | typedef struct |
| 12 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 13 | double min; |
| 14 | double max; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 15 | } limits_t; |
| 16 | |
| 17 | /* Note: If you update these structs you need to update the copy code in |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 18 | * pid/util.cpp and the initialization code in pid/buildjson.hpp files. |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 19 | */ |
| 20 | typedef struct |
| 21 | { |
Patrick Williams | 8c05112 | 2023-05-10 07:50:59 -0500 | [diff] [blame] | 22 | bool initialized; // has pid been initialized |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 23 | |
Patrick Williams | 8c05112 | 2023-05-10 07:50:59 -0500 | [diff] [blame] | 24 | double ts; // sample time in seconds |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 25 | double integral; // integral of error |
Patrick Williams | 8c05112 | 2023-05-10 07:50:59 -0500 | [diff] [blame] | 26 | double lastOutput; // value of last output |
| 27 | double lastError; // value of last error |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 28 | |
Patrick Venture | 4b0df32 | 2019-02-11 09:04:57 -0800 | [diff] [blame] | 29 | double proportionalCoeff; // coeff for P |
| 30 | double integralCoeff; // coeff for I |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 31 | double derivativeCoeff; // coeff for D |
Patrick Venture | 4b0df32 | 2019-02-11 09:04:57 -0800 | [diff] [blame] | 32 | double feedFwdOffset; // offset coeff for feed-forward term |
| 33 | double feedFwdGain; // gain for feed-forward term |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 34 | |
Patrick Williams | 8c05112 | 2023-05-10 07:50:59 -0500 | [diff] [blame] | 35 | limits_t integralLimit; // clamp of integral |
| 36 | limits_t outLim; // clamp of output |
Patrick Venture | 4b0df32 | 2019-02-11 09:04:57 -0800 | [diff] [blame] | 37 | double slewNeg; |
| 38 | double slewPos; |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 39 | double positiveHysteresis; |
| 40 | double negativeHysteresis; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 41 | } pid_info_t; |
| 42 | |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 43 | double pid(pid_info_t* pidinfoptr, double input, double setpoint, |
| 44 | const std::string* nameptr = nullptr); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 45 | |
| 46 | /* Condensed version for use by the configuration. */ |
| 47 | struct pidinfo |
| 48 | { |
Patrick Venture | 7442c37 | 2019-02-11 10:21:05 -0800 | [diff] [blame] | 49 | double ts; // sample time in seconds |
| 50 | double proportionalCoeff; // coeff for P |
| 51 | double integralCoeff; // coeff for I |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 52 | double derivativeCoeff; // coeff for D |
Patrick Venture | 7442c37 | 2019-02-11 10:21:05 -0800 | [diff] [blame] | 53 | double feedFwdOffset; // offset coeff for feed-forward term |
| 54 | double feedFwdGain; // gain for feed-forward term |
| 55 | ec::limits_t integralLimit; // clamp of integral |
| 56 | ec::limits_t outLim; // clamp of output |
| 57 | double slewNeg; |
| 58 | double slewPos; |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 59 | double positiveHysteresis; |
| 60 | double negativeHysteresis; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 63 | } // namespace ec |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 64 | } // namespace pid_control |