Has anybody heard of a PID library for Processing?
Im thinking: The frontend has lots of power, so why not do the PID math here and only run Firmata on the Arduino.
I assume it is not possible to use Arduino libs in Processing.
/Preben
Has anybody heard of a PID library for Processing?
I've never written a processing library before, but my guess is porting the arduino version should be pretty easy. the only arduino function it calls is millis().
I'll see if I can bang one out this weekend
Brett
hi,
thanks brett great program has helped me a lot, i am also using your frontend for my home brewery, and im trying to add a stir button to control a stir motor. I tried the code you gave EVAN but i dont know how to take the byte sent and apply it to my stirmotor Pin's output. Could you please point me in the right direction, your help would be much appreciated.
Thanks,
mooooks.
mooooks, assuming you copied evan's front-end code, the byte being sent should be a 0 or 1. the code I posted in reply#11 takes this byte and puts it into "pump_OnOff".
after that it should be a matter of doing a:
digitalWrite(outputPin, pump_OnOff);
I don't have my arduino on me, so I can't test this. if the above doesn't work you may need to tell the compiler "look man, I know the value in pump_OnOff is only ever going to be 0 or 1":
digitalWrite(outputPin, (boolean)pump_OnOff);
Hope this helps,
Brett
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