1d8012181SPatrick Venture #pragma once 2d8012181SPatrick Venture 3d8012181SPatrick Venture #include <cstdint> 4de74542cSJosh Lehan #include <string> 5d8012181SPatrick Venture 6a076487aSPatrick Venture namespace pid_control 7a076487aSPatrick Venture { 8d8012181SPatrick Venture namespace ec 9d8012181SPatrick Venture { 10d8012181SPatrick Venture 11d8012181SPatrick Venture typedef struct 12d8012181SPatrick Venture { 135f59c0fdSPatrick Venture double min; 145f59c0fdSPatrick Venture double max; 15d8012181SPatrick Venture } limits_t; 16d8012181SPatrick Venture 17d8012181SPatrick Venture /* Note: If you update these structs you need to update the copy code in 1831058fd3SJosh Lehan * pid/util.cpp and the initialization code in pid/buildjson.hpp files. 19d8012181SPatrick Venture */ 20d8012181SPatrick Venture typedef struct 21d8012181SPatrick Venture { 22d8012181SPatrick Venture bool initialized; // has pid been initialized 23*9788963cSDelphine CC Chiu bool checkHysterWithSetpt; // compare current input and setpoint to check 24*9788963cSDelphine CC Chiu // hysteresis 25d8012181SPatrick Venture 265f59c0fdSPatrick Venture double ts; // sample time in seconds 2731058fd3SJosh Lehan double integral; // integral of error 284b0df320SPatrick Venture double lastOutput; // value of last output 290e8fc398SBonnie Lo double lastError; // value of last error 30d8012181SPatrick Venture 314b0df320SPatrick Venture double proportionalCoeff; // coeff for P 324b0df320SPatrick Venture double integralCoeff; // coeff for I 330e8fc398SBonnie Lo double derivativeCoeff; // coeff for D 344b0df320SPatrick Venture double feedFwdOffset; // offset coeff for feed-forward term 354b0df320SPatrick Venture double feedFwdGain; // gain for feed-forward term 36d8012181SPatrick Venture 374b0df320SPatrick Venture limits_t integralLimit; // clamp of integral 384b0df320SPatrick Venture limits_t outLim; // clamp of output 394b0df320SPatrick Venture double slewNeg; 404b0df320SPatrick Venture double slewPos; 41572c43daSJames Feist double positiveHysteresis; 42572c43daSJames Feist double negativeHysteresis; 43d8012181SPatrick Venture } pid_info_t; 44d8012181SPatrick Venture 45de74542cSJosh Lehan double pid(pid_info_t* pidinfoptr, double input, double setpoint, 46de74542cSJosh Lehan const std::string* nameptr = nullptr); 47d8012181SPatrick Venture 48d8012181SPatrick Venture /* Condensed version for use by the configuration. */ 49d8012181SPatrick Venture struct pidinfo 50d8012181SPatrick Venture { 51*9788963cSDelphine CC Chiu bool checkHysterWithSetpt; // compare current input and setpoint to check 52*9788963cSDelphine CC Chiu // hysteresis 53*9788963cSDelphine CC Chiu 545f59c0fdSPatrick Venture double ts; // sample time in seconds 557442c37aSPatrick Venture double proportionalCoeff; // coeff for P 567442c37aSPatrick Venture double integralCoeff; // coeff for I 570e8fc398SBonnie Lo double derivativeCoeff; // coeff for D 587442c37aSPatrick Venture double feedFwdOffset; // offset coeff for feed-forward term 597442c37aSPatrick Venture double feedFwdGain; // gain for feed-forward term 607442c37aSPatrick Venture ec::limits_t integralLimit; // clamp of integral 617442c37aSPatrick Venture ec::limits_t outLim; // clamp of output 627442c37aSPatrick Venture double slewNeg; 637442c37aSPatrick Venture double slewPos; 64572c43daSJames Feist double positiveHysteresis; 65572c43daSJames Feist double negativeHysteresis; 66d8012181SPatrick Venture }; 67d8012181SPatrick Venture 68da4a5dd1SPatrick Venture } // namespace ec 69a076487aSPatrick Venture } // namespace pid_control 70