Processing Front-End for the PID Library

Thanks Brett,
when sending byte c from processing i was sending it as "mode" when it should have been "PUMPmode", e.g Byte c = (PUMPmode=="ON")?(byte)0:(byte)1;.
All working now thanks.

Okay I have the whole processing program running... pulls up the UI but when I click the button to go to automatic and send it to the arduino it doesn't change the value listed next to the box, but I can see the arduino blink when i click the button which means it should be communicating... I see no lines anywhere on the graph and can't figure out why it's not working!
I've got the arduino plugged in and set to port dev/tty.usbserial-A800-ev1H. When i load up processing the list in the box below the text field says

Stable Library

Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
[0] "/dev/tty.usbserial-A800ev1H"
[1] "/dev/cu.usbserial-A800ev1H"
[2] "/dev/tty.Bluetooth-PDA-Sync"
[3] "/dev/cu.Bluetooth-PDA-Sync"
[4] "/dev/tty.Bluetooth-Modem"
[5] "/dev/cu.Bluetooth-Modem"
ControlP5 0.5.0 infos, comments, questions at processing GUI, controlP5

I have input on my arduino coming in on analog in 0 and analog 2... I have my output through digital pin 3. I have the following program loaded to my arduino.

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

#include <PID_Beta6.h>

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

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,9,9,9);

void setup()
{
  //initialize the variables we're linked to
  Input = analogRead(2) - analogRead(0);
  Setpoint = 600;

  //turn the PID on
  //myPID.SetMode(AUTO);
  //Serial.begin(9600);
}

void loop()
{
  Input = analogRead(2) - analogRead(0);
  //Serial.println(Input);
  myPID.Compute();
  analogWrite(3,Output);
  
  //Serial.print("output: ");
  //Serial.println(Output);
}

Does anyone have any idea why this is happening? I don't get any compiling/loading errors anywhere so I'm not sure why this is happening!

Lance, If you posted your entire program then I think I know what the problem is. there's code you need to implement on the arduino side to listen for the signals from processing.

Take a look at the sample arduino program that's bundled with the Processing front-End. try loading that onto your arduino and see if the problem persists.

Brett

Oh man... Well at least it was an easy fix! Works perfectly now, I had always assumed that was another Processing file, never extended the window and read the whole file name lol.

Thanks for the help

Well, I have a strange problem.

I can't get any output from my pid-controller even if i change my PID constants i.e. tuning. Here is what i am doing.

double MagStartPositionY = 375;
double MagReadingsInts[3];
double YawServoOffset = 0;

//Yaw PID control
#define Input MagReadingsInts[1]
#define Output YawServoOffset
#define SetPoint MagStartPositionY

double P_Param = 0.5; //0.004
double I_Param = 0.01; //0.009
double D_Param = 0; //0.3

PID YawPID(&MagReadingsInts[1], &YawServoOffset, &MagStartPositionY, P_Param, I_Param, D_Param);

void setup()
{
//Some Code//
YawPID.SetOutputLimits(0,30); // I want output to be a number from 0-30
YawPID.SetMode(AUTO); //turn on the PID
}

void loop()
{

// Code to receive data is correct
...................

//
YawPID.compute();
Serial.print("Output : ");Serial.println(Output);
delay(2000);

}

My input is the serial Magnetometer data. I move my magnetometer to change the input and want to see how output changes. But strangely output always remain zero.

I tried putting YawPID.SetTunings(P_K,I_K,D_K); and changing the input, output and setpoint definitions to double but all in vain.

Can anybody figure out what is the problem?

Thanks!

Hi there,

I am attempting to get the frontend working for the PID library and I've hit something of a snag. I am getting the following error when I run the program in Processing:

error, disabling serialEvent() for /dev/ttyUSB0
java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:616)
      at processing.serial.Serial.serialEvent(Unknown Source)
      at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
      at gnu.io.RXTXPort.eventLoop(Native Method)
      at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Caused by: java.lang.NullPointerException
      at PID_FrontEnd_v02.serialEvent(PID_FrontEnd_v02.java:436)
      ... 8 more

The graphical display comes up but no data is passed to or received from the arduino. Nothing comes up on the graph or in the data fields to the left.

I was able to eliminate the error by removing the setValue() and setText() commands in serialEvent(). If I comment out those lines, like this:

//take the string the arduino sends us and parse it
void serialEvent(Serial myPort)
{
  
  String read = myPort.readStringUntil(10);
  if(outputFileName!="") output.print(str(millis())+ " "+read);
  String[] s = split(read, " ");
  if (s.length ==8)
  {
    Setpoint = float(s[1]);           // * pull the information
    Input = float(s[2]);              //   we need out of the
    Output = float(s[3]);             //   string and put it
    //SPLabel.setValue(s[1]);           //   where it's needed
    //InLabel.setValue(s[2]);           //
    //OutLabel.setValue(trim(s[3]));    //
    //PLabel.setValue(trim(s[4]));      //
    //ILabel.setValue(trim(s[5]));      //
    //DLabel.setValue(trim(s[6]));      //
    //AMCurrent.setValue(trim(s[7]));   //

    if(justSent)                      // * if this is the first read
    {                                 //   since we sent values to 
      //SPField.setText(trim(s[1]));    //   the arduino,  take the
      //InField.setText(trim(s[2]));    //   current values and put
      //OutField.setText(trim(s[3]));   //   them into the input fields
      //PField.setText(trim(s[4]));     //
      //IField.setText(trim(s[5]));     //
      //DField.setText(trim(s[6]));     //
      mode = trim(s[7]);              //
      //AMLabel.setValue(mode);         //
      justSent=false;                 //
    }                               //

    if(!madeContact) madeContact=true;
  }
}

Again, the graphical window comes up, but some data starts coming from the arduino (the led starts blinking), and it begins drawing the setpoint, input, and output lines. The data fields to the left do not show anything. However, I can change the setpoint, "Send to Arduino" and the green line will reflect the change.

I've read around a bit about this error, but I still don't understand what's going on.

Do you have any thoughts?

BTW I'm running on Ubuntu, if that makes any difference.

EDIT: I just tried this on a windows laptop and it worked with no errors. It must be specific to Linux or Ubuntu versions of processing.

EDIT2: This thread is the closest I've come to a similar error, but its from 2007, and they don't have a solution:

http://processing.org/discourse/yabb2/YaBB.pl?num=1183525352

Brett,

This GUI has been awesome for playing with the PID library more productively. I am now trying to record some data sets and run them through some system ID code in Matlab so I can do model-based gain selection. (And then I can use it to control the temperature of my mash for homebrewing =)

I am finding that when I give an output file name to the frontend code, it creates an empty file but never writes anything to it. I was able to fix the problem by adding output.flush() immediately after the output.println(). Is there a way to cleanly close the frontend? I didn't see an output.close() or any other wrap-up stuff, so I'm wondering if anyone has been using the write to file feature.

Thanks!

I'm wondering if anyone has been using the write to file feature.

in all honesty I threw that in there to do what you are doing, but it's been a while since I've used it. since then there's been a new version of processing, a new windows OS, and a few tweaks to the code.

the not-closing-cleanly thing is something I just plain missed.
Brett

Cool. If you're going through that code anytime soon, another recommendation I'd have is matching the specific string "PID" before trying to parse a line of serial data - I've run into trouble where other debugging strings I've printed happen to have 8 space-separated strings and confuse the frontend.

Please consider keeping PID Library an active project. I personally love it.

in all honesty I threw that in there to do what you are doing, but it's been a while since I've used it. since then there's been a new version of processing, a new windows OS, and a few tweaks to the code.

the not-closing-cleanly thing is something I just plain missed.
Brett

Nice.good working in graphics

Hey Everyone,

I'm desperately trying to get this front-end to work, but I keep getting the following error when I try to run it in the Processing program:

Note that release 1.0, libraries must be installed in a folder named 'libraries' inside the 'sketchbook' folder.

My "sketchbook" is defined as being in "My Documents/Processing" per the Preferences in Processing. I created a "libraries" folder there and placed the "controlP5" folder in it. I've also made "libraries" folders all over the place trying to get this to work. Any idea what I could be doing wrong?

Thanks,
Patrick

Hi Brett,

thanks for developing this Processing interface. I hope to trial some PID ideas soon & I'm sure I'll use your interface.

+1

It really helps!

Hi, we started looking for the PID library for our robots. Did not adopted it yet, but we worked a little bit on it. We used the v06, not the FixedPoint version, which we saw later, :(.

I don't know if this may be useful, but here is the code with the small changes. Basically, added the "const" when we think it's needed, converted the getters to inlines, and with a default param in the constructor, eliminated the second constructor and the ConstructorCommon(). The only change in the interface is that the Bias is now the last param in the constructor, due to the C++ requirement that the default arguments must be the last ones).

So we compiled the three examples, and them are a bit reduced now, and the inline members must be a little faster:

  • PIDSample: 5790 bytes -> 5708 bytes
  • PIDSample2: 5730 bytes -> 5648 bytes
  • PIDSample3: 5948 bytes -> 5826 bytes

I know it's not a lot of less bytes, but it's not only smaller, but also a little more robust (due to the const).

Here is the .h, and the cpp will be in the next post, to make this shorter:

class PID
{
  public:

  #define AUTO      1
  #define MANUAL      0
  #define LIBRARY_VERSION      0.6

  //commonly used functions **************************************************************************
    PID(double*, double*, double*,        // * constructor.  links the PID to the Input, Output, and
        const double, const double, const double,
        //This is truly NULL in C++ (see http://www2.research.att.com/~bs/bs_faq2.html#null):
        double *FFBias = 0);          //   Setpoint.  Initial tuning parameters are also set here

    void SetMode(const int Mode);               // * sets PID to either Manual (0) or Auto (non-0)

    void Compute();                       // * performs the PID calculation.  it should be
                                          //   called every time loop() cycles. ON/OFF and
                                          //   calculation frequency can be set using SetMode
                                          //   SetSampleTime respectively

    void SetInputLimits(const double, const double);  //Tells the PID what 0-100% are for the Input

    void SetOutputLimits(const double, const double); //Tells the PID what 0-100% are for the Output


  //available but not commonly used functions ********************************************************
    void SetTunings(const double, const double,       // * While most users will set the tunings once in the
                    const double);              //   constructor, this function gives the user the option
                                          //   of changing tunings during runtime for Adaptive control

    void SetSampleTime(const int);           // * sets the frequency, in Milliseconds, with which
                                          //   the PID calculation is performed.  default is 1000

    void Reset();                         // * reinitializes controller internals.  automatically
                                          //   called on a manual to auto transition

    /*****************************************************************************
     * STATUS SECTION
     * These functions allow the outside world to query the status of the PID
     *****************************************************************************/

    bool JustCalculated()
    {
        return justCalced;
    }
    int GetMode() const
    {
        if(inAuto)
            return 1;
        else
            return 0;
    }

    inline double GetINMin() const
    {
        return inMin;
    }
    inline double GetINMax() const
    {
        return inMin + inSpan;
    }
    inline double GetOUTMin() const
    {
        return outMin;
    }
    inline double GetOUTMax() const
    {
        return outMin+outSpan;
    }
    inline int GetSampleTime() const
    {
        return tSample;
    }
    inline double GetP_Param() const
    {
        return P_Param;
    }
    inline double GetI_Param() const
    {
        return I_Param;
    }
    inline double GetD_Param() const
    {
        return D_Param;
    }
  private:

   //scaled, tweaked parameters we'll actually be using
    double kc;                    // * (P)roportional Tuning Parameter
    double taur;                  // * (I)ntegral Tuning Parameter
    double taud;                  // * (D)erivative Tuning Parameter

   //nice, pretty parameters we'll give back to the user if they ask what the tunings are
    double P_Param;
    double I_Param;
    double D_Param;

    double *myInput;              // * Pointers to the Input, Output, and Setpoint variables
    double *myOutput;             //   This creates a hard link between the variables and the
    double *mySetpoint;           //   PID, freeing the user from having to constantly tell us
                                  //   what these values are.  with pointers we'll just know.

    double *myBias;               // * Pointer to the External FeedForward bias, only used
                                  //   if the advanced constructor is used
    unsigned long nextCompTime;    // * Helps us figure out when the PID Calculation needs to
                                  //   be performed next
                                  //   to determine when to compute next
    unsigned long tSample;       // * the frequency, in milliseconds, with which we want the
                                  //   the PID calculation to occur.
    bool inAuto;                  // * Flag letting us know if we are in Automatic or not

    double lastOutput;            // * remembering the last output is used to prevent
                                  //   reset windup.
    double lastInput;             // * we need to remember the last Input Value so we can compute
                                  //   the derivative required for the D term
    double accError;              // * the (I)ntegral term is based on the sum of error over
                                  //   time.  this variable keeps track of that
    double bias;                  // * the base output from which the PID operates


    double inMin, inSpan;         // * input and output limits, and spans.  used convert
    double outMin, outSpan;       //   real world numbers into percent span, with which
                                  //   the PID algorithm is more comfortable.

    bool justCalced;              // * flag gets set for one cycle after the pid calculates
};

Regards,
Julián

and here is the .cpp (we deleted he function description headers because we could not paste the complete cpp here otherwise -is there an text size limite?-):

PID::PID(double *Input, double *Output, double *Setpoint,
         const double Kc, const double TauI, const double TauD,
         double *FFBias)
{
    SetInputLimits(0, 1023);            //default the limits to the
    SetOutputLimits(0, 255);            //full ranges of the I/O

    tSample = 1000;                  //default Controller Sample Time is 1 second

    SetTunings(Kc, TauI, TauD);
    nextCompTime = millis();

    inAuto = false;
    myOutput = Output;
    myInput = Input;
    mySetpoint = Setpoint;
    myBias = FFBias;

    Reset();
}

void PID::SetInputLimits(const double INMin, const double INMax)
{
      //after verifying that mins are smaller than maxes, set the values
      if(INMin >= INMax) return;

      //rescale the working variables to reflect the changes
      lastInput = (lastInput) * (INMax - INMin) / (inSpan);
      accError *= (INMax - INMin) / (inSpan);

      //make sure the working variables are
      //within the new limits
      if (lastInput > 1) lastInput = 1;
      else if (lastInput < 0) lastInput = 0;


    inMin = INMin;
      inSpan = INMax - INMin;
}

void PID::SetOutputLimits(const double OUTMin, const double OUTMax)
{
      //after verifying that mins are smaller than maxes, set the values
      if(OUTMin >= OUTMax) return;

      //rescale the working variables to reflect the changes
      lastOutput = (lastOutput) * (OUTMax - OUTMin) / (outSpan);

      //make sure the working variables are
      //within the new limits
      if (lastOutput > 1) lastOutput = 1;
      else if (lastOutput < 0) lastOutput = 0;

      outMin = OUTMin;
      outSpan = OUTMax - OUTMin;
}

void PID::SetTunings(const double Kc, const double TauI, const double TauD)
{
      //verify that the tunings make sense
      if (Kc == 0.0 || TauI < 0.0 || TauD < 0.0) return;

      //we're going to do some funky things to the input numbers so all
      //our math works out, but we want to store the numbers intact
      //so we can return them to the user when asked.
      P_Param = Kc;
      I_Param = TauI;
      D_Param = TauD;

      //convert Reset Time into Reset Rate, and compensate for Calculation frequency
      double tSampleInSec = ((double)tSample / 1000.0);
      double tempTauR;
      if (TauI == 0.0)
            tempTauR = 0.0;
      else
            tempTauR = (1.0 / TauI) * tSampleInSec;

      if (inAuto)
      {      //if we're in auto, and we just change the tunings, the integral term
            //will become very, very, confused (trust me.) to achieve "bumpless
            // transfer" we need to rescale the accumulated error.
            if(tempTauR != 0.0) //(avoid divide by 0)
                  accError *= (kc * taur)/(Kc * tempTauR);
            else
                  accError = 0.0;
      }

      kc = Kc;
      taur = tempTauR;
      taud = TauD / tSampleInSec;
}

void PID::Reset()
{
    //Is myBias a NULL pointer?
      if(myBias != 0) //you can ask only if (myBias), but that's more error prone and less clear.
        bias = (*myBias - outMin) / outSpan;
      else
        bias = (*myOutput - outMin) / outSpan;

    lastOutput = bias;
      lastInput = (*myInput - inMin) / inSpan;

      // - clear any error in the integral
      accError = 0;

}

void PID::SetMode(const int Mode)
{
      if (Mode!=0 && !inAuto)
      {      //we were in manual, and we just got set to auto.
            //reset the controller internals
            Reset();
      }
      inAuto = (Mode!=0);
}

void PID::SetSampleTime(const int NewSampleTime)
{
      if (NewSampleTime > 0)
      {
            //convert the time-based tunings to reflect this change
            taur *= ((double)NewSampleTime)/((double) tSample);
            accError *= ((double) tSample)/((double)NewSampleTime);
            taud *= ((double)NewSampleTime)/((double) tSample);
            tSample = (unsigned long)NewSampleTime;
      }
}

void PID::Compute()
{
      justCalced=false;
      if (!inAuto) return; //if we're in manual just leave;

      unsigned long now = millis();

      //millis() wraps around to 0 at some point.  depending on the version of the
      //Arduino Program you are using, it could be in 9 hours or 50 days.
      //this is not currently addressed by this algorithm.


      //...Perform PID Computations if it's time...
      if (now>=nextCompTime)
      {
            //pull in the input and setpoint, and scale them into percent span
            double scaledInput = (*myInput - inMin) / inSpan;
            if (scaledInput>1.0) scaledInput = 1.0;
            else if (scaledInput<0.0) scaledInput = 0.0;

            double scaledSP = (*mySetpoint - inMin) / inSpan;
            if (scaledSP>1.0) scaledSP = 1;
            else if (scaledSP<0.0) scaledSP = 0;

            //compute the error
            double err = scaledSP - scaledInput;

            // check and see if the output is pegged at a limit and only
            // integrate if it is not. (this is to prevent reset-windup)
            if (!(lastOutput >= 1 && err>0) && !(lastOutput <= 0 && err<0))
                  accError = accError + err;

            // compute the current slope of the input signal
            double dMeas = (scaledInput - lastInput);  // we'll assume that dTime (the denominator) is 1 second.
                                             // if it isn't, the taud term will have been adjusted
                                             // in "SetTunings" to compensate

            //if we're using an external bias (i.e. the user used the
            //overloaded constructor,) then pull that in now
            if (myBias != 0) //Is myBias a NULL pointer?
                  bias = (*myBias - outMin) / outSpan;

            // perform the PID calculation.
            double output = bias + kc * (err + taur * accError - taud * dMeas);

            //make sure the computed output is within output constraints
            if (output < 0.0) output = 0.0;
            else if (output > 1.0) output = 1.0;

            lastOutput = output;            // remember this output for the windup
                                    // check next time
            lastInput = scaledInput;      // remember the Input for the derivative
                                    // calculation next time

            //scale the output from percent span back out to a real world number
                *myOutput = ((output * outSpan) + outMin);

            nextCompTime += tSample;                        // determine the next time the computation
            if(nextCompTime < now) nextCompTime = now + tSample;      // should be performed

            justCalced=true;  //set the flag that will tell the outside world that the output was just computed
      }
}

This should use a little less RAM too (but very very little)

Regards,
Julián

Sorry, I should post the previous two posts to the http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1226431507 , is there any way to move them?

Great work!