Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <cstdint> |
| 4 | |
| 5 | namespace ec |
| 6 | { |
| 7 | |
| 8 | typedef struct |
| 9 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 10 | float min; |
| 11 | float max; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 12 | } limits_t; |
| 13 | |
| 14 | /* Note: If you update these structs you need to update the copy code in |
| 15 | * pid/util.cpp. |
| 16 | */ |
| 17 | typedef struct |
| 18 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 19 | bool initialized; // has pid been initialized |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 20 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 21 | float ts; // sample time in seconds |
| 22 | float integral; // intergal of error |
| 23 | float last_output; // value of last output |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 24 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 25 | float p_c; // coeff for P |
| 26 | float i_c; // coeff for I |
| 27 | float ff_off; // offset coeff for feed-forward term |
| 28 | float ff_gain; // gain for feed-forward term |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 29 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 30 | limits_t i_lim; // clamp of integral |
| 31 | limits_t out_lim; // clamp of output |
| 32 | float slew_neg; |
| 33 | float slew_pos; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 34 | } pid_info_t; |
| 35 | |
| 36 | float pid(pid_info_t* pidinfoptr, float input, float setpoint); |
| 37 | |
| 38 | /* Condensed version for use by the configuration. */ |
| 39 | struct pidinfo |
| 40 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 41 | float ts; // sample time in seconds |
| 42 | float p_c; // coeff for P |
| 43 | float i_c; // coeff for I |
| 44 | float ff_off; // offset coeff for feed-forward term |
| 45 | float ff_gain; // gain for feed-forward term |
| 46 | ec::limits_t i_lim; // clamp of integral |
| 47 | ec::limits_t out_lim; // clamp of output |
| 48 | float slew_neg; |
| 49 | float slew_pos; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 52 | } // namespace ec |