MCPcopy Create free account
hub / github.com/br3ttb/Arduino-PID-Library / Compute

Method Compute

PID_v1.cpp:58–95  ·  view source on GitHub ↗

Compute() ********************************************************************** * This, as they say, is where the magic happens. this function should be called * every time "void loop()" executes. the function will decide for itself whether a new * pid Output needs to be computed. returns true when the output is computed, * false when nothing has been done. *********************

Source from the content-addressed store, hash-verified

56 * false when nothing has been done.
57 **********************************************************************************/
58bool PID::Compute()
59{
60 if(!inAuto) return false;
61 unsigned long now = millis();
62 unsigned long timeChange = (now - lastTime);
63 if(timeChange>=SampleTime)
64 {
65 /*Compute all the working error variables*/
66 double input = *myInput;
67 double error = *mySetpoint - input;
68 double dInput = (input - lastInput);
69 outputSum+= (ki * error);
70
71 /*Add Proportional on Measurement, if P_ON_M is specified*/
72 if(!pOnE) outputSum-= kp * dInput;
73
74 if(outputSum > outMax) outputSum= outMax;
75 else if(outputSum < outMin) outputSum= outMin;
76
77 /*Add Proportional on Error, if P_ON_E is specified*/
78 double output;
79 if(pOnE) output = kp * error;
80 else output = 0;
81
82 /*Compute Rest of PID Output*/
83 output += outputSum - kd * dInput;
84
85 if(output > outMax) output = outMax;
86 else if(output < outMin) output = outMin;
87 *myOutput = output;
88
89 /*Remember some variables for next time*/
90 lastInput = input;
91 lastTime = now;
92 return true;
93 }
94 else return false;
95}
96
97/* SetTunings(...)*************************************************************
98 * This function allows the controller's dynamic performance to be adjusted.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected