Skip to content

Commit

Permalink
Resolves #67 and adds new toggle mode
Browse files Browse the repository at this point in the history
Controller mode can now be toggled to/from manual/automatic.

  myPID.SetMode(myPID.Control::toggle);
  myPID.SetMode(myPID.Control::toggle);
  • Loading branch information
Dlloydev committed Mar 22, 2023
1 parent 3d958e4 commit bb0b35e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
10 changes: 7 additions & 3 deletions src/QuickPID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,20 @@ void QuickPID::SetOutputLimits(float Min, float Max) {
controller is automatically initialized.
******************************************************************************/
void QuickPID::SetMode(Control Mode) {
if (mode == Control::manual && Mode != Control::manual) { // just went from manual to automatic or timer
if (mode == Control::manual && Mode != Control::manual) { // just went from manual to automatic, timer or toggle
QuickPID::Initialize();
}
mode = Mode;
if (Mode == Control::toggle) {
mode = (mode == Control::manual) ? Control::automatic : Control::manual;
} else mode = Mode;
}
void QuickPID::SetMode(uint8_t Mode) {
if (mode == Control::manual && Mode != 0) { // just went from manual to automatic or timer
QuickPID::Initialize();
}
mode = (Control)Mode;
if (Mode == 3) { // toggle
mode = (mode == Control::manual) ? Control::automatic : Control::manual;
} else mode = (Control)Mode;
}

/* Initialize()****************************************************************
Expand Down
19 changes: 10 additions & 9 deletions src/QuickPID.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class QuickPID {

public:

enum class Control : uint8_t {manual, automatic, timer}; // controller mode
enum class Action : uint8_t {direct, reverse}; // controller action
enum class pMode : uint8_t {pOnError, pOnMeas, pOnErrorMeas}; // proportional mode
enum class dMode : uint8_t {dOnError, dOnMeas}; // derivative mode
enum class iAwMode : uint8_t {iAwCondition, iAwClamp, iAwOff}; // integral anti-windup mode
enum class Control : uint8_t {manual, automatic, timer, toggle}; // controller mode
enum class Action : uint8_t {direct, reverse}; // controller action
enum class pMode : uint8_t {pOnError, pOnMeas, pOnErrorMeas}; // proportional mode
enum class dMode : uint8_t {dOnError, dOnMeas}; // derivative mode
enum class iAwMode : uint8_t {iAwCondition, iAwClamp, iAwOff}; // integral anti-windup mode

// commonly used functions ************************************************************************************

Expand All @@ -28,7 +28,7 @@ class QuickPID {
// Simplified constructor which uses defaults for remaining parameters.
QuickPID(float *Input, float *Output, float *Setpoint);

// Sets PID mode to manual (0), automatic (1) or timer (2).
// Sets PID mode to manual (0), automatic (1), timer (2) or toggle manual/automatic (3).
void SetMode(Control Mode);
void SetMode(uint8_t Mode);

Expand Down Expand Up @@ -72,14 +72,17 @@ class QuickPID {
void SetAntiWindupMode(iAwMode iAwMode);
void SetAntiWindupMode(uint8_t IawMode);

// Ensure a bumpless transfer from manual to automatic mode
void Initialize();

// PID Query functions ****************************************************************************************
float GetKp(); // proportional gain
float GetKi(); // integral gain
float GetKd(); // derivative gain
float GetPterm(); // proportional component of output
float GetIterm(); // integral component of output
float GetDterm(); // derivative component of output
uint8_t GetMode(); // manual (0), automatic (1) or timer (2)
uint8_t GetMode(); // manual (0), automatic (1), timer (2) or toggle manual/automatic (3)
uint8_t GetDirection(); // direct (0), reverse (1)
uint8_t GetPmode(); // pOnError (0), pOnMeas (1), pOnErrorMeas (2)
uint8_t GetDmode(); // dOnError (0), dOnMeas (1)
Expand All @@ -89,8 +92,6 @@ class QuickPID {

private:

void Initialize();

float dispKp = 0; // for defaults and display
float dispKi = 0;
float dispKd = 0;
Expand Down

0 comments on commit bb0b35e

Please sign in to comment.