(Untested) Float free version of PID library

This commit is contained in:
Josh Stewart 2016-04-07 13:27:51 +10:00
parent be0ccf6a5f
commit 202bfd7003
3 changed files with 62 additions and 42 deletions

View File

@ -17,8 +17,8 @@
* The parameters specified here are those for for which we can't set up
* reliable defaults, so we need to have the user set them.
***************************************************************************/
PID::PID(double* Input, double* Output, double* Setpoint,
double Kp, double Ki, double Kd, int ControllerDirection)
PID::PID(long* Input, long* Output, long* Setpoint,
byte Kp, byte Ki, byte Kd, byte ControllerDirection)
{
myOutput = Output;
@ -52,15 +52,15 @@ bool PID::Compute()
//if(timeChange>=SampleTime)
{
/*Compute all the working error variables*/
double input = *myInput;
double error = *mySetpoint - input;
ITerm+= (ki * error);
long input = *myInput;
long error = *mySetpoint - input;
ITerm+= (ki * error)/100;
if(ITerm > outMax) ITerm= outMax;
else if(ITerm < outMin) ITerm= outMin;
double dInput = (input - lastInput);
long dInput = (input - lastInput);
/*Compute PID Output*/
double output = kp * error + ITerm- kd * dInput;
long output = (kp * error)/100 + ITerm- (kd * dInput)/100;
if(output > outMax) output = outMax;
else if(output < outMin) output = outMin;
@ -80,16 +80,22 @@ bool PID::Compute()
* it's called automatically from the constructor, but tunings can also
* be adjusted on the fly during normal operation
******************************************************************************/
void PID::SetTunings(double Kp, double Ki, double Kd)
void PID::SetTunings(byte Kp, byte Ki, byte Kd)
{
if (Kp<0 || Ki<0 || Kd<0) return;
dispKp = Kp; dispKi = Ki; dispKd = Kd;
/*
double SampleTimeInSec = ((double)SampleTime)/1000;
kp = Kp;
ki = Ki * SampleTimeInSec;
kd = Kd / SampleTimeInSec;
*/
long InverseSampleTimeInSec = 100000 / SampleTime;
kp = Kp;
ki = (Ki * 100) / InverseSampleTimeInSec;
kd = (Kd * InverseSampleTimeInSec) / 100;
if(controllerDirection ==REVERSE)
{
@ -106,10 +112,10 @@ void PID::SetSampleTime(int NewSampleTime)
{
if (NewSampleTime > 0)
{
double ratio = (double)NewSampleTime
/ (double)SampleTime;
ki *= ratio;
kd /= ratio;
unsigned long ratioX1000 = (unsigned long)(NewSampleTime * 1000) / (unsigned long)SampleTime;
ki = (ki * ratioX1000) / 1000;
//kd /= ratio;
kd = (kd * 1000) / ratioX1000;
SampleTime = (unsigned long)NewSampleTime;
}
}
@ -122,7 +128,7 @@ void PID::SetSampleTime(int NewSampleTime)
* want to clamp it from 0-125. who knows. at any rate, that can all be done
* here.
**************************************************************************/
void PID::SetOutputLimits(double Min, double Max)
void PID::SetOutputLimits(long Min, long Max)
{
if(Min >= Max) return;
outMin = Min;
@ -171,7 +177,7 @@ void PID::Initialize()
* know which one, because otherwise we may increase the output when we should
* be decreasing. This is called from the constructor.
******************************************************************************/
void PID::SetControllerDirection(int Direction)
void PID::SetControllerDirection(byte Direction)
{
if(inAuto && Direction !=controllerDirection)
{
@ -187,9 +193,9 @@ void PID::SetControllerDirection(int Direction)
* functions query the internal state of the PID. they're here for display
* purposes. this are the functions the PID Front-end uses for example
******************************************************************************/
double PID::GetKp(){ return dispKp; }
double PID::GetKi(){ return dispKi;}
double PID::GetKd(){ return dispKd;}
byte PID::GetKp(){ return dispKp; }
byte PID::GetKi(){ return dispKi;}
byte PID::GetKd(){ return dispKd;}
int PID::GetMode(){ return inAuto ? AUTOMATIC : MANUAL;}
int PID::GetDirection(){ return controllerDirection;}

View File

@ -15,8 +15,8 @@ class PID
#define REVERSE 1
//commonly used functions **************************************************************************
PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
double, double, double, int); // Setpoint. Initial tuning parameters are also set here
PID(long*, long*, long*, // * constructor. links the PID to the Input, Output, and
byte, byte, byte, byte); // Setpoint. Initial tuning parameters are also set here
void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0)
@ -25,17 +25,17 @@ class PID
// calculation frequency can be set using SetMode
// SetSampleTime respectively
void SetOutputLimits(double, double); //clamps the output to a specific range. 0-255 by default, but
void SetOutputLimits(long, long); //clamps the output to a specific range. 0-255 by default, but
//it's likely the user will want to change this depending on
//the application
//available but not commonly used functions ********************************************************
void SetTunings(double, double, // * While most users will set the tunings once in the
double); // constructor, this function gives the user the option
void SetTunings(byte, byte, // * While most users will set the tunings once in the
byte); // constructor, this function gives the user the option
// of changing tunings during runtime for Adaptive control
void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT
void SetControllerDirection(byte); // * Sets the Direction, or "Action" of the controller. DIRECT
// means the output will increase when error is positive. REVERSE
// means the opposite. it's very unlikely that this will be needed
// once it is set in the constructor.
@ -45,35 +45,35 @@ class PID
//Display functions ****************************************************************
double GetKp(); // These functions query the pid for interal values.
double GetKi(); // they were created mainly for the pid front-end,
double GetKd(); // where it's important to know what is actually
byte GetKp(); // These functions query the pid for interal values.
byte GetKi(); // they were created mainly for the pid front-end,
byte GetKd(); // where it's important to know what is actually
int GetMode(); // inside the PID.
int GetDirection(); //
private:
void Initialize();
double dispKp; // * we'll hold on to the tuning parameters in user-entered
double dispKi; // format for display purposes
double dispKd; //
byte dispKp; // * we'll hold on to the tuning parameters in user-entered
byte dispKi; // format for display purposes
byte dispKd; //
double kp; // * (P)roportional Tuning Parameter
double ki; // * (I)ntegral Tuning Parameter
double kd; // * (D)erivative Tuning Parameter
byte kp; // * (P)roportional Tuning Parameter
byte ki; // * (I)ntegral Tuning Parameter
byte kd; // * (D)erivative Tuning Parameter
int controllerDirection;
double *myInput; // * Pointers to the Input, Output, and Setpoint variables
double *myOutput; // This creates a hard link between the variables and the
double *mySetpoint; // PID, freeing the user from having to constantly tell us
long *myInput; // * Pointers to the Input, Output, and Setpoint variables
long *myOutput; // This creates a hard link between the variables and the
long *mySetpoint; // PID, freeing the user from having to constantly tell us
// what these values are. with pointers we'll just know.
unsigned long lastTime;
double ITerm, lastInput;
long ITerm, lastInput;
unsigned long SampleTime;
double outMin, outMax;
long outMin, outMax;
bool inAuto;
};
#endif

View File

@ -332,9 +332,9 @@ page = 6
lnchHardLim = scalar, U08, 51, "rpm", 100, 0.0, 100, 25500, 0
lnchFuelAdd = scalar, U08, 52, "%", 1.0, 0.0, 0.0, 80, 0
unused6-53 = scalar, U08, 53, "RPM", 100.0, 0.0, 100, 25500, 0
unused6-54 = scalar, U08, 54, "RPM", 100.0, 0.0, 100, 25500, 0
unused6-55 = scalar, U08, 55, "RPM", 100.0, 0.0, 100, 25500, 0
idleKP = scalar, U08, 53, "%", 1.0, 0.0, 0.0, 200.0, 0 ; * ( 1 byte)
idleKI = scalar, U08, 54, "%", 1.0, 0.0, 0.0, 200.0, 0 ; * ( 1 byte)
idleKD = scalar, U08, 55, "%", 1.0, 0.0, 0.0, 200.0, 0 ; * ( 1 byte)
unused6-56 = scalar, U08, 56, "RPM", 100.0, 0.0, 100, 25500, 0
unused6-57 = scalar, U08, 57, "RPM", 100.0, 0.0, 100, 25500, 0
unused6-58 = scalar, U08, 58, "RPM", 100.0, 0.0, 100, 25500, 0
@ -348,7 +348,7 @@ page = 6
;Start idle and fan controls (Page 7)
;--------------------------------------------------
page = 7
iacCLValues = array, U08, 0, [10], "RPM", 1.0, 0.0, 100.0, 255, 0
iacCLValues = array, U08, 0, [10], "RPM", 10.0, 0.0, 100.0, 2550, 0
iacOLStepVal = array, U08, 10, [10], "Steps", 3, 0, 0, 765, 0
iacOLPWMVal = array, U08, 20, [10], "Duty %", 1.0, 0, 0, 100, 0
#if CELSIUS
@ -364,7 +364,7 @@ page = 7
iacCrankBins = array, U08, 48, [4], "F", 1.8, -22.23, -40, 215, 0
#endif
iacAlgorithm = bits , U08, 52, [0:2], "None", "On/Off", "PWM", "INVALID", "Stepper", "INVALID", "INVALID", "INVALID"
iacAlgorithm = bits , U08, 52, [0:2], "None", "On/Off", "PWM Open loop", "PWM Closed loop", "Stepper", "INVALID", "INVALID", "INVALID"
iacStepTime = bits , U08, 52, [3:5], "1", "2", "3", "4", "5", "6"
iacChannels = bits, U08, 52, [6:6], "1", "2"
unused7-52f = bits , U08, 52, [7:7], "One", "INVALID"
@ -506,6 +506,7 @@ page = 8
subMenu = std_warmup, "&Warmup Enrichment"
subMenu = std_separator
subMenu = idleSettings, "Idle Control"
subMenu = iacClosedLoop_curve, "Idle - Closed loop targets", 7, { iacAlgorithm == 2 || iacAlgorithm == 3 }
subMenu = iacPwm_curve, "Idle - PWM Duty Cycle", 7, { iacAlgorithm == 2 || iacAlgorithm == 3 }
subMenu = iacPwmCrank_curve, "Idle - PWM Cranking Duty Cycle", 7, { iacAlgorithm == 2 || iacAlgorithm == 3 }
subMenu = iacStep_curve, "Idle - Stepper Motor", 7, { iacAlgorithm == 4 || iacAlgorithm == 5 }
@ -674,6 +675,11 @@ page = 8
dialog = pwm_idle, "PWM Idle"
field = "Number of outputs", iacChannels, { iacAlgorithm == 2 || iacAlgorithm == 3 }
field = "Idle valve frequency", idleFreq, { iacAlgorithm == 2 || iacAlgorithm == 3 }
dialog = closedloop_idle, "Closed loop Idle"
field = "P", idleKP, { iacAlgorithm == 3 }
field = "I", idleKI, { iacAlgorithm == 3 }
field = "D", idleKD, { iacAlgorithm == 3 }
dialog = idleSettings, "Idle Settings"
topicHelp = "http://speeduino.com/wiki/index.php/Idle"
@ -682,6 +688,7 @@ page = 8
field = "Fast idle temp", iacFastTemp, { iacAlgorithm == 1 }
panel = pwm_idle
panel = stepper_idle
panel = closedloop_idle
dialog = crankPW, "Cranking Pulsewidths (ms)"
@ -892,6 +899,13 @@ page = 8
yAxis = 0, 850, 4
xBins = iacCrankBins, coolant
yBins = iacCrankSteps
curve = iacClosedLoop_curve, "IAC Closed Loop Targets"
columnLabel = "Coolant Temperature", "Motor"
xAxis = -40, 120, 6
yAxis = 0, 2000, 4
xBins = iacBins, coolant
yBins = iacCLValues
[TableEditor]