8000 SB -- Adding minimum motor output setting. · EntropyZero/Arduino-PID-Library@acfba09 · GitHub
[go: up one dir, main page]

Skip to content

Commit acfba09

Browse files
committed
SB -- Adding minimum motor output setting.
1 parent c61fb89 commit acfba09

File tree

8 files changed

+57
-46
lines changed

8 files changed

+57
-46
lines changed

PID_v1/PID_v1.cpp renamed to PID_v1_Motor/PID_v1_Motor.cpp

Lines chang 8000 ed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,40 @@
1111
#include "WProgram.h"
1212
#endif
1313

14-
#include <PID_v1.h>
14+
#include <PID_v1_Motor.h>
1515

1616
/*Constructor (...)*********************************************************
17-
* The parameters specified here are those for for which we can't set up
17+
* The parameters specified here are those for for which we can't set up
1818
* reliable defaults, so we need to have the user set them.
1919
***************************************************************************/
2020
PID::PID(double* Input, double* Output, double* Setpoint,
2121
double Kp, double Ki, double Kd, int ControllerDirection)
2222
{
23-
23+
2424
myOutput = Output;
2525
myInput = Input;
2626
mySetpoint = Setpoint;
2727
inAuto = false;
28-
29-
PID::SetOutputLimits(0, 255); //default output limit corresponds to
28+
29+
PID::SetOutputLimits(0, 255); //default output limit corresponds to
3030
//the arduino pwm limits
31+
PID::SetMotorMinimum(0);
3132

3233
SampleTime = 100; //default Controller Sample Time is 0.1 seconds
3334

3435
PID::SetControllerDirection(ControllerDirection);
3536
PID::SetTunings(Kp, Ki, Kd);
3637

37-
lastTime = millis()-SampleTime;
38+
lastTime = millis()-SampleTime;
3839
}
39-
40-
40+
41+
4142
/* Compute() **********************************************************************
4243
* This, as they say, is where the magic happens. this function should be called
4344
* every time "void loop()" executes. the function will decide for itself whether a new
4445
* pid Output needs to be computed. returns true when the output is computed,
4546
* false when nothing has been done.
46-
**********************************************************************************/
47+
**********************************************************************************/
4748
bool PID::Compute()
4849
{
4950
if(!inAuto) return false;
@@ -58,14 +59,18 @@ bool PID::Compute()
5859
if(ITerm > outMax) ITerm= outMax;
5960
else if(ITerm < outMin) ITerm= outMin;
6061
double dInput = (input - lastInput);
61-
62+
6263
/*Compute PID Output*/
6364
double output = kp * error + ITerm- kd * dInput;
64-
65+
6566
if(output > outMax) output = outMax;
6667
else if(output < outMin) output = outMin;
68+
else if((*myOutput < output) && (output < motorMin)){
69+
output = motorMin;
70+
ITerm = motorMin;
71+
}
6772
*myOutput = output;
68-
73+
6974
/*Remember some variables for next time*/
7075
lastInput = input;
7176
lastTime = now;
@@ -76,31 +81,31 @@ bool PID::Compute()
7681

7782

7883
/* SetTunings(...)*************************************************************
79-
* This function allows the controller's dynamic performance to be adjusted.
84+
* This function allows the controller's dynamic performance to be adjusted.
8085
* it's called automatically from the constructor, but tunings can also
8186
* be adjusted on the fly during normal operation
82-
******************************************************************************/
87+
******************************************************************************/
8388
void PID::SetTunings(double Kp, double Ki, double Kd)
8489
{
8590
if (Kp<0 || Ki<0 || Kd<0) return;
86-
91+
8792
dispKp = Kp; dispKi = Ki; dispKd = Kd;
88-
89-
double SampleTimeInSec = ((double)SampleTime)/1000;
93+
94+
double SampleTimeInSec = ((double)SampleTime)/1000;
9095
kp = Kp;
9196
ki = Ki * SampleTimeInSec;
9297
kd = Kd / SampleTimeInSec;
93-
98+
9499
if(controllerDirection ==REVERSE)
95100
{
96101
kp = (0 - kp);
97102
ki = (0 - ki);
98103
kd = (0 - kd);
99104
}
100105
}
101-
106+
102107
/* SetSampleTime(...) *********************************************************
103-
* sets the period, in Milliseconds, at which the calculation is performed
108+
* sets the period, in Milliseconds, at which the calculation is performed
104109
******************************************************************************/
105110
void PID::SetSampleTime(int NewSampleTime)
106111
{
@@ -113,7 +118,7 @@ void PID::SetSampleTime(int NewSampleTime)
113118
SampleTime = (unsigned long)NewSampleTime;
114119
}
115120
}
116-
121+
117122
/* SetOutputLimits(...)****************************************************
118123
* This function will be used far more often than SetInputLimits. while
119124
* the input to the controller will generally be in the 0-1023 range (which is
@@ -127,22 +132,29 @@ void PID::SetOutputLimits(double Min, double Max)
127132
if(Min >= Max) return;
128133
outMin = Min;
129134
outMax = Max;
130-
135+
131136
if(inAuto)
132137
{
133138
if(*myOutput > outMax) *myOutput = outMax;
134139
else if(*myOutput < outMin) *myOutput = outMin;
135-
140+
136141
if(ITerm > outMax) ITerm= outMax;
137142
else if(ITerm < outMin) ITerm= outMin;
138143
}
139144
}
140145

146+
147+
void PID::SetMotorMinimum(double MinMotorOutput)
148+
{
149+
motorMin = MinMotorOutput;
150+
}
151+
152+
141153
/* SetMode(...)****************************************************************
142154
* Allows the controller Mode to be set to manual (0) or Automatic (non-zero)
143155
* when the transition from manual to auto occurs, the controller is
144156
* automatically initialized
145-
******************************************************************************/
157+
******************************************************************************/
146158
void PID::SetMode(int Mode)
147159
{
148160
bool newAuto = (Mode == AUTOMATIC);
@@ -152,11 +164,11 @@ void PID::SetMode(int Mode)
152164
}
153165
inAuto = newAuto;
154166
}
155-
167+
156168
/* Initialize()****************************************************************
157169
* does all the things that need to happen to ensure a bumpless transfer
158170
* from manual to automatic mode.
159-
******************************************************************************/
171+
******************************************************************************/
160172
void PID::Initialize()
161173
{
162174
ITerm = *myOutput;
@@ -166,7 +178,7 @@ void PID::Initialize()
166178
}
167179

168180
/* SetControllerDirection(...)*************************************************
169-
* The PID will either be connected to a DIRECT acting process (+Output leads
181+
* The PID will either be connected to a DIRECT acting process (+Output leads
170182
* to +Input) or a REVERSE acting process(+Output leads to -Input.) we need to
171183
* know which one, because otherwise we may increase the output when we should
172184
* be decreasing. This is called from the constructor.
@@ -178,18 +190,17 @@ void PID::SetControllerDirection(int Direction)
178190
kp = (0 - kp);
179191
ki = (0 - ki);
180192
kd = (0 - kd);
181-
}
193+
}
182194
controllerDirection = Direction;
183195
}
184196

185197
/* Status Funcions*************************************************************
186198
* Just because you set the Kp=-1 doesn't mean it actually happened. these
187-
* functions query the internal state of the PID. they're here for display
199+
* functions query the internal state of the PID. they're here for display
188200
* purposes. this are the functions the PID Front-end uses for example
189201
******************************************************************************/
190202
double PID::GetKp(){ return dispKp; }
191203
double PID::GetKi(){ return dispKi;}
192204
double PID::GetKd(){ return dispKd;}
193205
int PID::GetMode(){ return inAuto ? AUTOMATIC : MANUAL;}
194206
int PID::GetDirection(){ return controllerDirection;}
195-

PID_v1/PID_v1.h renamed to PID_v1_Motor/PID_v1_Motor.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class PID
1515
#define REVERSE 1
1616

1717
//commonly used functions **************************************************************************
18-
PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
18+
PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
1919
double, double, double, int); // Setpoint. Initial tuning parameters are also set here
20-
20+
2121
void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0)
2222

2323
bool Compute(); // * performs the PID calculation. it should be
@@ -28,53 +28,53 @@ class PID
2828
void SetOutputLimits(double, double); //clamps the output to a specific range. 0-255 by default, but
2929
//it's likely the user will want to change this depending on
3030
//the application
31-
31+
void SetMotorMinimum(double); //Set the minimum output required for motor to move with no load.
32+
3233

3334

3435
//available but not commonly used functions ********************************************************
35-
void SetTunings(double, double, // * While most users will set the tunings once in the
36+
void SetTunings(double, double, // * While most users will set the tunings once in the
3637
double); // constructor, this function gives the user the option
3738
// of changing tunings during runtime for Adaptive control
3839
void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT
3940
// means the output will increase when error is positive. REVERSE
4041
// means the opposite. it's very unlikely that this will be needed
4142
// once it is set in the constructor.
42-
void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which
43+
void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which
4344
// the PID calculation is performed. default is 100
44-
45-
46-
45+
46+
47+
4748
//Display functions ****************************************************************
4849
double GetKp(); // These functions query the pid for interal values.
4950
double GetKi(); // they were created mainly for the pid front-end,
50-
double GetKd(); // where it's important to know what is actually
51+
double GetKd(); // where it's important to know what is actually
5152
int GetMode(); // inside the PID.
5253
int GetDirection(); //
5354

5455
private:
5556
void Initialize();
56-
57-
double dispKp; // * we'll hold on to the tuning parameters in user-entered
57+
58+
double dispKp; // * we'll hold on to the tuning parameters in user-entered
5859
double dispKi; // format for display purposes
5960
double dispKd; //
60-
61+
6162
double kp; // * (P)roportional Tuning Parameter
6263
double ki; // * (I)ntegral Tuning Parameter
6364
double kd; // * (D)erivative Tuning Parameter
6465

6566
int controllerDirection;
6667

6768
double *myInput; // * Pointers to the Input, Output, and Setpoint variables
68-
double *myOutput; // This creates a hard link between the variables and the
69+
double *myOutput; // This creates a hard link between the variables and the
6970
double *mySetpoint; // PID, freeing the user from having to constantly tell us
7071
// what these values are. with pointers we'll just know.
71-
72+
7273
unsigned long lastTime;
7374
double ITerm, lastInput;
7475

7576
unsigned long SampleTime;
76-
double outMin, outMax;
77+
double outMin, outMax, motorMin;
7778
bool inAuto;
7879
};
7980
#endif
80-
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)
0