PID Control Object (with questions) #2783
Replies: 26 comments
-
Posted at 2016-05-08 by ClearMemory041063 Continued:
And finally some code to test it all.
For a simple test on an Espruino board I connected pin A4 to Pin A1.
Tuning the P, I, and D values is an art. Please consult Google.
Attachments: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-08 by tve If you're coding PID up from scratch, I highly recommend reading http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/ :-) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-08 by Wilberforce Please find a attached a port of the Arduino pid library to the Espruino. Function names have been shortened, to make it more suitable for this environment. If there is interest, I'll comment the code and submit it as a module. In the short term, this can be saved as a local module for testing.
Attachments: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-08 by Wilberforce Can't seem to attach two attachmentsAttachments: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-09 by @allObjects regarding attaching/uploading of multiple files: I did not figure out how to do it at once, but one after the other work:
Also with deleting and adding again I had trouble... therefore, after every file upload or delete, save the post and edit it again with a delete or update. (I recall vaguely that deleting more than one file in one edit/save session worked.) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-09 by @gfwilliams Yeah, I'm not sure what's up with the forum there :(
:) Yeah... What I'd love is some 'magic' library that could do some tests and guess values - for example in heater-based systems, by turning the output full-on for 1 sec and then off, and then looking at the rise and fall of values on the sensor you should be able to pick out at least some of the values that are needed. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-09 by Wilberforce The arduino pid library has a self learning module. I've not implemented that yet.depending on what you are heating, it can take some time to raise the temp by a degree. And then if it overshoots it takes quite a bit of time for that degree to drop back to the first level.... In my case I'm heating 18L of water, and with a 3w element it can crank out about 1 deg/min. I think the current P value I have 14. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-09 by ClearMemory041063 In Windows, when I attach files, I first put them in one directory and then select multiple files at once then send them. Trying to do one at a time requires editing the submitted post. Both of the files Wilberforce seem to be the minimized version. How does one go about minimizing a module? Magic PID tuners exist in the world, there is some variance in PID implementations so one has to use an auto-tune with the matching PID implementation. tve thanks for the link. The method I used is based on differentiation the basic equation PID so |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-09 by ClearMemory041063 This link lays out three forms of PID control, including the differential form that calculates the change in output. Looking at it my first stab at it from memory needs to be corrected. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-09 by @allObjects About minimizing... since my module was simple and small, I selected Closure (online) Simple Minification , placed module into sandbox modules folder, made a one-liner in edit pane that requires the module, uploaded that one-liner to th3 board, 'pullled it back' in the console pane using arrow up. What you can see is the string that the Web IDE sent to the Espruino, something like this: I'm sure there are smarter and more elegant ways to do that... but it did what it had to do without figuring out a single command. It would be nice the Web IDE had a setting that would write the minimized code back.... (I would not mind an extra user prompt for the times it writes back. With leaving a timestamp with thee minified file, an auto-detect for change... (if the Web IDE can read the file without user interaction). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-10 by Wilberforce On Windows I use notepad++. It has a js plugin, that has a js-format (pretty print) and a js-minifier. I'll attach the js-formatted version tomorrow. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-10 by Wilberforce Here is the reference to the auto tune library: http://brettbeauregard.com/blog/2012/01/arduino-pid-autotune-library/ And the code: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-10 by ClearMemory041063 Thanks for the information. I'm working on an independent velocity equation based PID algorithm. I'm going to check using the Arduino link that Wilberforce provided to determine the flavor of that PID. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-10 by Wilberforce The attachment here has been updated to the human readable form: #4 http://forum.espruino.com/comments/12972573/ The module is a port of the reference that @tve mentioned above. This an adaption of the node version here: https://github.com/wilberforce/pid-controller made suitable for the Espurino. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-11 by ClearMemory041063 Thanks that helps a lot. Deriving the velocity PID equations from the position PID equations. Attachments: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-11 by ClearMemory041063 Trying out the code Wilberforce posted using the low pass filter as a test process.
Attachments: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-12 by Wilberforce Great to see you testing the code. You really should not be setting internal parts of the library - At this point - you could call Same goes for getting the output |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-12 by @allObjects @ClearMemory041063, do you have actually something concrete running where you apply these PID control object? If so, you can use the Web IDE built in testing to visualize in a graph what is going on. It's a pretty neat feature of the Web IDE. I would not call it necessarily testing, but something more like monitoring. It allows the sampling with custom sampling rate of practically any value from connected Espruino and includes also optional recording. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-12 by Wilberforce @allObjects - that's a great idea - I had actually opened the cvs in excel and plotted there... Much easier to do within the web ide in realtime! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-12 by @allObjects You still can do the cvs in excel / google spreadsheet/docs 'thing' because it has more options, but it is 'fun' to watch it real-time. The recording option gives you the best of both worlds. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-12 by ClearMemory041063 @allObjects the testing part of WebIDE is a cool thing to use. I gave it a try with @wilberforce's code and after fiddling with the testing buttons managed to get it to plot a graph of the process. Seems in my haste to try @wilberforce's module, I violated the rule of object access. Use methods to read/write object properties. Not a big thing if you are just fooling around trying to understand a piece of code, but a big deal if you build with it because a rewrite of the module can break your code. I'm wondering if there is a method to enforce that when using the keyword "export"? C++ classes have private and public keywords that can be applied to class properties. My testing so far has been using an analog output connected by a 100k resistor to a 10 uF capacitor to ground, and the resistor-capacitor junction connected to an analog input. The other option would be to dig out some chemE textbooks and write some code to simulate a process. My head still hurts thinking about simulating the control of a 20 plate fractioning column with three different control loops on a 286 computer. So slow I had to buy a math coprocessor and even then it was slow. Such things are a lot faster today. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-13 by @allObjects There are ways in Javascript to have private members - state and behavior | values and functions/methods - but I'm not seeing it done often. Some minimal discipline in following a few rules is good enough to mitigate the (worst) issues. No langue - no matter how strongly or inferred or even static ...typed and compiled compensates for ingnorance in the problem space... After acquiring a decent understanding what is going on and sorting the things out in the mind, a few formal rules consistently applied foster an equally decent implementation. These are a few rules I stick with... but as you know: Rules can only exist when Exceptions to the Rule(s) exist... ;-)...
Since JavaScript by nature has closures, various frameworks have popped-up to overcome name space issues in order to mix and match components from various - non-synchronized - providers. One very useful library is node.js' . |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-06 by maman Hello @ClearMemory041063, @wilberforce I am using pid-controller and mqtt modules for our project, now.
Is there any issue for compatibility of pid-controller and mqtt module ? Regards, |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-06 by Wilberforce It looks like you are using an esp8266 - and this has very limited memory - you could try the save on send option in the ide, and this might out put the code into flash, saving more ram space. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-06 by maman Hello @wilberforce, Yes, I am using esp8266 (wemos d1 mini).
and bellow is the error message
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by @gfwilliams You could try calling The issue is not your code itself, but that you're trying to cram MQTT, BME680 and PID into an ESP8266. You could use something like an Espruino WiFi that has more than enough space for this, however if you're after some ideas to save space you could ask on the ESP8266 section of the forum rather than hijacking this thread. I believe there is a smaller MQTT module available. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-08 by ClearMemory041063
A standard way to implement a feedback control system is the Proportional, Integral, Derivative (PID) control algorithm.
Think of things to control and how different the control action can be.
Driving a car. Use a compass module as the input and a motor to steer the wheels. As a driver you turn and hold the steering wheel until the new heading is achieved. Does a boat work the same way?
When piloting an airplane you turn the yoke to tilt the plane over about 15 degrees and then neutralize the yoke. The plane will circle until you turn the yoke to level the plane. (I’m ignoring rudder action)
When riding a bicycle or motorcycle, once in motion, the controls act opposite to that of a car. Turn right and the bike tilts to the left and you the bike turns left.
So how to implement PID control?
We need:
Using Google we find:
https://en.wikipedia.org/wiki/PID_controller
The pseudocode from the link:
Suppose the setpoint is 0.5 and the measured value is 1.0. Then errpr= 0.5 – 1.0 = -0.5. Let dt = 1 second, then integral = 0.0 – 0.5 * 1 = -0.5 and derivative= (-0.5 -0.0 )/1 = -0.5.
I think we have a problem since in an Espruino world analog inputs and outputs work between 0.0 and 1.0. I tried coding this and ran into this problem.
Another link from a University course just seems to confuse the issue.
http://www.cds.caltech.edu/~murray/courses/cds101/fa02/caltech/astrom-ch6.pdf
I recall from a course taken many years ago that the PID algorithm can be expressed in a differential form so that the change in output is expressed as a function of the PID terms.
doutput = P*sumof(error) + I error + Dderivative,
Where:
P=Kp= the proportional gain,
I=Ki= the integral gain, and
D=Kd= the derivative gain.
Maybe a math GURU can properly insert the dt values into the equation.
First let’s setup a PID object:
The Dir field is used to reverse the control action.
The Fname field is used to record a CSV file.
Now implement an object to use the PIS information and extend it.
Adding a method to setup and start the timer
Add a method to stop the timer
Beta Was this translation helpful? Give feedback.
All reactions