Skip to content

Commit bb0b35e

Browse files
committed
Resolves #67 and adds new toggle mode
Controller mode can now be toggled to/from manual/automatic. myPID.SetMode(myPID.Control::toggle); myPID.SetMode(myPID.Control::toggle);
1 parent 3d958e4 commit bb0b35e

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/QuickPID.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,20 @@ void QuickPID::SetOutputLimits(float Min, float Max) {
177177
controller is automatically initialized.
178178
******************************************************************************/
179179
void QuickPID::SetMode(Control Mode) {
180-
if (mode == Control::manual && Mode != Control::manual) { // just went from manual to automatic or timer
180+
if (mode == Control::manual && Mode != Control::manual) { // just went from manual to automatic, timer or toggle
181181
QuickPID::Initialize();
182182
}
183-
mode = Mode;
183+
if (Mode == Control::toggle) {
184+
mode = (mode == Control::manual) ? Control::automatic : Control::manual;
185+
} else mode = Mode;
184186
}
185187
void QuickPID::SetMode(uint8_t Mode) {
186188
if (mode == Control::manual && Mode != 0) { // just went from manual to automatic or timer
187189
QuickPID::Initialize();
188190
}
189-
mode = (Control)Mode;
191+
if (Mode == 3) { // toggle
192+
mode = (mode == Control::manual) ? Control::automatic : Control::manual;
193+
} else mode = (Control)Mode;
190194
}
191195

192196
/* Initialize()****************************************************************

src/QuickPID.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ class QuickPID {
66

77
public:
88

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

1515
// commonly used functions ************************************************************************************
1616

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

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

@@ -72,14 +72,17 @@ class QuickPID {
7272
void SetAntiWindupMode(iAwMode iAwMode);
7373
void SetAntiWindupMode(uint8_t IawMode);
7474

75+
// Ensure a bumpless transfer from manual to automatic mode
76+
void Initialize();
77+
7578
// PID Query functions ****************************************************************************************
7679
float GetKp(); // proportional gain
7780
float GetKi(); // integral gain
7881
float GetKd(); // derivative gain
7982
float GetPterm(); // proportional component of output
8083
float GetIterm(); // integral component of output
8184
float GetDterm(); // derivative component of output
82-
uint8_t GetMode(); // manual (0), automatic (1) or timer (2)
85+
uint8_t GetMode(); // manual (0), automatic (1), timer (2) or toggle manual/automatic (3)
8386
uint8_t GetDirection(); // direct (0), reverse (1)
8487
uint8_t GetPmode(); // pOnError (0), pOnMeas (1), pOnErrorMeas (2)
8588
uint8_t GetDmode(); // dOnError (0), dOnMeas (1)
@@ -89,8 +92,6 @@ class QuickPID {
8992

9093
private:
9194

92-
void Initialize();
93-
9495
float dispKp = 0; // for defaults and display
9596
float dispKi = 0;
9697
float dispKd = 0;

0 commit comments

Comments
 (0)