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