Skip to content

Using GetMode is awkward / myPID.setMode(myPID.getMode()) fails #66

@drf5n

Description

@drf5n

If you try to test the mode, you appear to need casts:

if (myPID.Compute() || myPID.GetMode()==(uint8_t)myPID.Control::automatic);

... because GetMode() and the others do a cast before returning, making the results not matc the setting

QuickPID/src/QuickPID.cpp

Lines 252 to 265 in 6adfc2c

uint8_t QuickPID::GetMode() {
return static_cast<uint8_t>(mode);
}
uint8_t QuickPID::GetDirection() {
return static_cast<uint8_t>(action);
}
uint8_t QuickPID::GetPmode() {
return static_cast<uint8_t>(pmode);
}
uint8_t QuickPID::GetDmode() {
return static_cast<uint8_t>(dmode);
}
uint8_t QuickPID::GetAwMode() {
return static_cast<uint8_t>(iawmode);

Some test code:

/********************************************************
   PID Basic Example
   Reading analog input 0 to control analog PWM output 3
 ********************************************************/

#include <QuickPID.h>

#define PIN_INPUT 0
#define PIN_OUTPUT 3

//Define Variables we'll be connecting to
float Setpoint, Input, Output;

float Kp = 2, Ki = 5, Kd = 1;

//Specify PID links
QuickPID myPID(&Input, &Output, &Setpoint);

void setup()
{
  //initialize the variables we're linked to
  Input = analogRead(PIN_INPUT);
  Setpoint = 100;

  //apply PID gains
  myPID.SetTunings(Kp, Ki, Kd);

  //turn the PID on
  myPID.SetMode(myPID.Control::automatic);

  myPID.SetMode(myPID.GetMode());  // 



}

void loop()
{
  Input = analogRead(PIN_INPUT);
  myPID.Compute();
  analogWrite(PIN_OUTPUT, Output);
}

fails to compile with;

sketch.ino: In function 'void setup()':
sketch.ino:31:32: error: no matching function for call to 'QuickPID::SetMode(uint8_t)'
   myPID.SetMode(myPID.GetMode());
                                ^
In file included from sketch.ino:6:0:
/libraries/QuickPID/src/QuickPID.h:32:10: note: candidate: void QuickPID::SetMode(QuickPID::Control)
     void SetMode(Control Mode);
          ^~~~~~~
/libraries/QuickPID/src/QuickPID.h:32:10: note:   no known conversion for argument 1 from 'uint8_t {aka unsigned char}' to 'QuickPID::Control'

Error during build: exit status 1

I would expect these to all work easily:


myPID.setMode(myPID.getMode())

if(myPID.GetMode()==myPID.Control::automatic) Serial.print("Automatic");

void resetOutput(float value){
  auto saveMode = myPID.GetMode();
  myPID.SetMode(myPID.Control::automatic);
  Output = value;
  myPID.SetMode(saveMode);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions