Skip to content

Commit

Permalink
Use enum class
Browse files Browse the repository at this point in the history
Resolves issue #37
Documentation and examples updated.
  • Loading branch information
Dlloydev committed Dec 14, 2021
1 parent add2c3f commit 4615669
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 88 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ When the controller is set to `REVERSE` acting, the sign of the `error` and `dIn

```c++
QuickPID::QuickPID(float* Input, float* Output, float* Setpoint,
float Kp, float Ki, float Kd, uint8_t pMode = PE, uint8_t dMode = DM,
uint8_t awMode = CLAMP, uint8_t Action = DIRECT)
float Kp, float Ki, float Kd, pMode pMode = pMode::PE, dMode dMode = dMode::DM,
awMode awMode = awMode::CONDITION, Action action = Action::DIRECT);
```
- `Input`, `Output`, and `Setpoint` are pointers to the variables holding these values.
Expand All @@ -41,8 +41,8 @@ QuickPID::QuickPID(float* Input, float* Output, float* Setpoint,
- `Action` is the controller action parameter which has `DIRECT` (default) and `REVERSE` options. These options set how the controller responds to a change in input. `DIRECT` action is used if the input moves in the same direction as the controller output (i.e. heating process). `REVERSE` action is used if the input moves in the opposite direction as the controller output (i.e. cooling process).
```c++
QuickPID::QuickPID(float* Input, float* Output, float* Setpoint,
float Kp, float Ki, float Kd, uint8_t Action = DIRECT)
QuickPID::QuickPID(float* Input, float* Output, float* Setpoint, float Kp, float Ki, float Kd,
Action action);
```

This allows you to use Proportional on Error without explicitly saying so.
Expand All @@ -58,7 +58,8 @@ This function contains the PID algorithm and it should be called once every loop
#### SetTunings

```c++
void QuickPID::SetTunings(float Kp, float Ki, float Kd, uint8_t pMode = PE, uint8_t dMode = DM, uint8_t awMode = CLAMP)
void QuickPID::SetTunings(float Kp, float Ki, float Kd, pMode pMode = pMode::PE, dMode dMode = dMode::DM,
awMode awMode = awMode::CONDITION);
```
This function allows the controller's dynamic performance to be adjusted. It's called automatically from the constructor, but tunings can also be adjusted on the fly during normal operation. The parameters are as described in the constructor.
Expand Down Expand Up @@ -88,7 +89,7 @@ The PID controller is designed to vary its output within a given range. By defa
#### SetMode

```c++
void QuickPID::SetMode(uint8_t Mode)
void QuickPID::SetMode(Control Mode);
```
Allows the controller Mode to be set to `MANUAL` (0) or `AUTOMATIC` (1) or `TIMER` (2). when the transition from manual to automatic or timer occurs, the controller is automatically initialized.
Expand All @@ -109,7 +110,7 @@ Does all the things that need to happen to ensure a bump-less transfer from manu
#### SetControllerDirection

```c++
void QuickPID::SetControllerDirection(uint8_t Action)
void QuickPID::SetControllerDirection(Action Action);
```
The PID will either be connected to a `DIRECT` acting process (+Output leads to +Input) or a `REVERSE` acting process (+Output leads to -Input.) We need to know which one, because otherwise we may increase the output when we should be decreasing. This is called from the constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ float Setpoint, Input, Output;
//Specify the links and initial tuning parameters
float Kp = 2, Ki = 5, Kd = 1;

QuickPID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, QuickPID::DIRECT);
QuickPID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, myPID.Action::DIRECT);

void setup() {
Timer1.initialize(sampleTimeUs); // initialize timer1, and set the time interval
Expand All @@ -27,7 +27,7 @@ void setup() {
Setpoint = 100;

//turn the PID on
myPID.SetMode(QuickPID::AUTOMATIC);
myPID.SetMode(myPID.Control::AUTOMATIC);
}

void loop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ float Setpoint, Input, Output;
float Kp = 2, Ki = 5, Kd = 1;

Ticker timer1(runPid, sampleTimeUs, 0, MICROS_MICROS);
QuickPID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, QuickPID::DIRECT);
QuickPID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, myPID.Action::DIRECT);

void setup() {
timer1.start();
Expand All @@ -28,7 +28,7 @@ void setup() {
Setpoint = 100;

//turn the PID on
myPID.SetMode(QuickPID::AUTOMATIC);
myPID.SetMode(myPID.Control::AUTOMATIC);
}

void loop() {
Expand Down
4 changes: 2 additions & 2 deletions examples/PID_AdaptiveTunings/PID_AdaptiveTunings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ float aggKp = 4, aggKi = 0.2, aggKd = 1;
float consKp = 1, consKi = 0.05, consKd = 0.25;

//Specify the links and initial tuning parameters
QuickPID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, QuickPID::DIRECT);
QuickPID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, myPID.Action::DIRECT);

void setup()
{
Expand All @@ -31,7 +31,7 @@ void setup()
Setpoint = 100;

//turn the PID on
myPID.SetMode(QuickPID::AUTOMATIC);
myPID.SetMode(myPID.Control::AUTOMATIC);
}

void loop()
Expand Down
4 changes: 2 additions & 2 deletions examples/PID_Basic/PID_Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ float Setpoint, Input, Output;
//Specify the links and initial tuning parameters
float Kp = 2, Ki = 5, Kd = 1;

QuickPID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, QuickPID::DIRECT);
QuickPID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, myPID.Action::DIRECT);

void setup()
{
Expand All @@ -23,7 +23,7 @@ void setup()
Setpoint = 100;

//turn the PID on
myPID.SetMode(QuickPID::AUTOMATIC);
myPID.SetMode(myPID.Control::AUTOMATIC);
}

void loop()
Expand Down
53 changes: 29 additions & 24 deletions examples/PID_Controller_Options/PID_Controller_Options.ino
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**************************************************************************************
For testing and checking parameter options. From QuickPID.h:
enum Mode : uint8_t {MANUAL, AUTOMATIC, TIMER}; // controller modes
enum Action : uint8_t {DIRECT, REVERSE}; // controller actions
enum pMode : uint8_t {PE, PM, PEM}; // proportional modes
enum dMode : uint8_t {DE, DM}; // derivative modes
enum awMode : uint8_t {CONDITION, CLAMP, OFF}; // integral anti-windup modes
enum class Mode : uint8_t {MANUAL, AUTOMATIC, TIMER}; // controller modes
enum class Action : uint8_t {DIRECT, REVERSE}; // controller actions
enum class pMode : uint8_t {PE, PM, PEM}; // proportional modes
enum class dMode : uint8_t {DE, DM}; // derivative modes
enum class awMode : uint8_t {CONDITION, CLAMP, OFF}; // integral anti-windup modes
float GetKp(); // proportional gain
float GetKi(); // integral gain
Expand All @@ -17,8 +17,8 @@
uint8_t GetDirection(); // DIRECT (0), REVERSE (1)
uint8_t GetPmode(); // PE (0), PM (1), PEM (2)
uint8_t GetDmode(); // DE (0), DM (1)
uint8_t GetAwMode(); // CONDITION (0, CLAMP (1), OFF (2)
uint8_t GetAwMode(); // CONDITION (0), CLAMP (1), OFF (2)
Documentation (GitHub): https://github.com/Dlloydev/QuickPID
**************************************************************************************/

Expand All @@ -27,38 +27,43 @@
const byte inputPin = 0;
const byte outputPin = 3;


//Define variables we'll be connecting to
float Setpoint, Input, Output, Kp = 2, Ki = 5, Kd = 1;
float Setpoint = 0, Input = 0, Output = 0, Kp = 2, Ki = 5, Kd = 1;

/* pMode dMode awMode Action */
QuickPID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, myPID.PM, myPID.DM, myPID.CLAMP, myPID.DIRECT);
/* PE DE CONDITION DIRECT
PM DM CLAMP REVERSE
PEM OFF
QuickPID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, myPID.pMode::PE, myPID.dMode::DM, myPID.awMode::CONDITION, myPID.Action::DIRECT);
/* PE DE CONDITION DIRECT
PM DM CLAMP REVERSE
PEM OFF
*/

void setup()
{
Serial.begin(115200);
delay(2000);

myPID.SetMode(QuickPID::AUTOMATIC); // controller modes
myPID.SetOutputLimits(0, 255);
myPID.SetSampleTimeUs(100000);
myPID.SetTunings(Kp, Ki, Kd);
myPID.SetMode(myPID.Control::AUTOMATIC);

Setpoint = 50;
Input = analogRead(inputPin);
myPID.Compute();
analogWrite(outputPin, 10);

analogWrite(outputPin, Output);

Serial.println();
Serial.print(F(" Pterm: ")); Serial.println(myPID.GetPterm());
Serial.print(F(" Iterm: ")); Serial.println(myPID.GetIterm());
Serial.print(F(" Dterm: ")); Serial.println(myPID.GetDterm());
Serial.print(F(" Mode: ")); Serial.println(myPID.GetMode());
Serial.print(F(" Direction: ")); Serial.println(myPID.GetDirection());
Serial.print(F(" Pmode: ")); Serial.println(myPID.GetPmode());
Serial.print(F(" Dmode: ")); Serial.println(myPID.GetDmode());
Serial.print(F(" AwMode: ")); Serial.println(myPID.GetAwMode());
Serial.print(F(" Setpoint: ")); Serial.println(Setpoint);
Serial.print(F(" Input: ")); Serial.println(Input);
Serial.print(F(" Output: ")); Serial.println(Output);
Serial.print(F(" Pterm: ")); Serial.println(myPID.GetPterm());
Serial.print(F(" Iterm: ")); Serial.println(myPID.GetIterm());
Serial.print(F(" Dterm: ")); Serial.println(myPID.GetDterm());
Serial.print(F(" Control: ")); Serial.println(myPID.GetMode());
Serial.print(F(" Action: ")); Serial.println(myPID.GetDirection());
Serial.print(F(" Pmode: ")); Serial.println(myPID.GetPmode());
Serial.print(F(" Dmode: ")); Serial.println(myPID.GetDmode());
Serial.print(F(" AwMode: ")); Serial.println(myPID.GetAwMode());

analogWrite(outputPin, 0);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/PID_RelayOutput/PID_RelayOutput.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ float Setpoint, Input, Output;
//Specify the links and initial tuning parameters
float Kp = 2, Ki = 5, Kd = 1;

QuickPID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, QuickPID::DIRECT);
QuickPID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, QuickPID::Action::DIRECT);

unsigned int WindowSize = 5000;
unsigned int minWindow = 500;
Expand All @@ -44,7 +44,7 @@ void setup()
myPID.SetOutputLimits(0, WindowSize);

//turn the PID on
myPID.SetMode(QuickPID::AUTOMATIC);
myPID.SetMode(myPID.Control::AUTOMATIC);
}

void loop()
Expand Down
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "QuickPID",
"version": "3.0.0",
"version": "3.0.1",
"description": "A fast PID controller with Integral anti-windup, TIMER mode and multiple options for Proportional, Derivative and anti-windup modes of operation. Also includes analogWrite compatibility for ESP32 and ESP32-S2.",
"keywords": "PID, controller, signal",
"repository":
Expand All @@ -20,7 +20,7 @@
"license": "MIT",
"homepage": "https://github.com/Dlloydev/QuickPID",
"dependencies": {
"QuickPID": "~3.0.0"
"QuickPID": "~3.0.1"
},
"frameworks": "arduino",
"platforms": "*"
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=QuickPID
version=3.0.0
version=3.0.1
author=David Lloyd
maintainer=David Lloyd <dlloydev@testcor.ca>
sentence=A fast PID controller with Integral anti-windup, TIMER mode and multiple options for Proportional, Derivative and anti-windup modes of operation.
Expand Down
Loading

0 comments on commit 4615669

Please sign in to comment.