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 | |
Jayanth Othayoth | 326cd76 | 2024-12-08 08:12:14 -0600 | [diff] [blame^] | 11 | typedef struct limits_t |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 12 | { |
Harvey Wu | 1b3b730 | 2024-10-04 16:49:46 +0800 | [diff] [blame] | 13 | double min = 0.0; |
| 14 | double max = 0.0; |
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 | */ |
Jayanth Othayoth | 326cd76 | 2024-12-08 08:12:14 -0600 | [diff] [blame^] | 20 | typedef struct pid_info_t |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 21 | { |
Harvey Wu | 1b3b730 | 2024-10-04 16:49:46 +0800 | [diff] [blame] | 22 | bool initialized = false; // has pid been initialized |
| 23 | bool checkHysterWithSetpt = false; // compare current input and setpoint to |
| 24 | // check hysteresis |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 25 | |
Harvey Wu | 1b3b730 | 2024-10-04 16:49:46 +0800 | [diff] [blame] | 26 | double ts = 0.0; // sample time in seconds |
| 27 | double integral = 0.0; // integral of error |
| 28 | double lastOutput = 0.0; // value of last output |
| 29 | double lastError = 0.0; // value of last error |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 30 | |
Harvey Wu | 1b3b730 | 2024-10-04 16:49:46 +0800 | [diff] [blame] | 31 | double proportionalCoeff = 0.0; // coeff for P |
| 32 | double integralCoeff = 0.0; // coeff for I |
| 33 | double derivativeCoeff = 0.0; // coeff for D |
| 34 | double feedFwdOffset = 0.0; // offset coeff for feed-forward term |
| 35 | double feedFwdGain = 0.0; // gain for feed-forward term |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 36 | |
Harvey Wu | 1b3b730 | 2024-10-04 16:49:46 +0800 | [diff] [blame] | 37 | limits_t integralLimit; // clamp of integral |
| 38 | limits_t outLim; // clamp of output |
| 39 | double slewNeg = 0.0; |
| 40 | double slewPos = 0.0; |
| 41 | double positiveHysteresis = 0.0; |
| 42 | double negativeHysteresis = 0.0; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 43 | } pid_info_t; |
| 44 | |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 45 | double pid(pid_info_t* pidinfoptr, double input, double setpoint, |
| 46 | const std::string* nameptr = nullptr); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 47 | |
| 48 | /* Condensed version for use by the configuration. */ |
| 49 | struct pidinfo |
| 50 | { |
Harvey Wu | 1b3b730 | 2024-10-04 16:49:46 +0800 | [diff] [blame] | 51 | bool checkHysterWithSetpt = 0.0; // compare current input and setpoint to |
| 52 | // check hysteresis |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 53 | |
Harvey Wu | 1b3b730 | 2024-10-04 16:49:46 +0800 | [diff] [blame] | 54 | double ts = 0.0; // sample time in seconds |
| 55 | double proportionalCoeff = 0.0; // coeff for P |
| 56 | double integralCoeff = 0.0; // coeff for I |
| 57 | double derivativeCoeff = 0.0; // coeff for D |
| 58 | double feedFwdOffset = 0.0; // offset coeff for feed-forward term |
| 59 | double feedFwdGain = 0.0; // gain for feed-forward term |
| 60 | ec::limits_t integralLimit; // clamp of integral |
| 61 | ec::limits_t outLim; // clamp of output |
| 62 | double slewNeg = 0.0; |
| 63 | double slewPos = 0.0; |
| 64 | double positiveHysteresis = 0.0; |
| 65 | double negativeHysteresis = 0.0; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 68 | } // namespace ec |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 69 | } // namespace pid_control |