RPI operates ARDUINO PID control
Posted by kll on July 28 2014 10:09:11
-3. DATA to GOOGLE spread sheet
-2. RPI feeds google spreadsheets with ARDUINO data
-1. Arduino - RPI - google + RPI - Arduino operation


1. this list
2. introduction / history
3. creating a faceplate for a PID control loop
4. current trend window
5. historic trend window, new browser test
6. tuning window
7. option: windows 7 PC python
8. RPI installation and about even more remote
9. the real world
10. PID tuning


back to the bigger brother, the uPCS project.


INTRODUCTION
prior info
now we have a arduino UNO / Leonardo with a PID control
PV by DALLAS one wire temperatur sensor
OUTPUT by a servo positioner

and a RPI with a python service program what talks to ARDUINO
- mode ( 0 safe, (1 interlock), 2 MAN, 3 AUTO, ( 4 CAS ), 5 RSP
- MOUT output for MAN mode
- RSP setpoint for RSP mode
and gets his reported PV, SP, OUTPUT, MODE
and sends it to a google spreadsheet ( as historic data collection )



WHAT IS A FACEPLATE
now we need a python graphical tool to operate a arduino PID controller,
to show PV, SP, OUTPUT, MODE
and send operation, RSP, MOUT, MODE to a command file
what is read and send to arduino by above service.

it was long long time ago, when the first Process Control System where build,
there was a visualization by so called FACEPLATE ( groups ),
a small graphic element with BAR graph for PV , SP, OUTPUT of a control loop and number representation.
For operation later that bar graphs changed to sliders.
Some years later there where pages with diagrams, static for piping, vessel,
dynamic colored for equipment like motors...
and small groups with colored numbers for control / indicator loops
and when you click on it in same / or new detail view / window that operation faceplate come up.
in a detailed loop window have that faceplate ( for operation )
a window with loop details like settings / tuning
optionally a current trend ( with a faster sampling rate as the historic trend ) for loop tuning.

from benvalle.com i see a good example of a
DELTA V process control system operator station ( starts with x00.000$? )

the basis for this kind of process picture is usually the Piping and Instrument Diagram ( also called PID )
just with some color dynamic and link to operation faceplate.

i want to try to rebuild that HMI starting with a FACEPLATE
with PYTHON on Raspberry PI / so you later can operate YOUR HOME AUTOMATION SYSTEM from the TV.

first i try to use ( like for PMS3 in RPI ) MatPlotLib
sudo apt-get install python-matplotlib
i loaded this slider example and RPI needs 17sec to start it! ( and the google service not running )
this bad timing i now understand, its a first run problem of python / matplotlib
and the slider operation with mouse ( via VNC! ) difficult.
( so i not use it now for the faceplate / slider , but i will come back to it for the trending later)

check on Tkinter
sudo apt-get install python-tk ( allread installed in this version)
and slider example start after 3sec.

after 2 hour play Tkinter and get angry about positioning:

but after using FRAMEs, real values and decimales...
the sliders work for fast / raw adjustment, for small steps (0.1 units) must click
the mouse on the BARGRAPH, above or below the slider.
but a add number input field would be nice!

this looks good and gives alread values back, but is not yet connected to the PID project, files...
so you also can test it first with the python WIN7 IDLE 2.7 version.
here the example code


now the FILE I/O,
we need the read the data from arduino, what is stored to a one line file in ram disk,
to show it in the faceplate.

and, after operation, we need to write the new setpoints ( for RSP, MOUT, MODE )
to a other file in ram disk, what is send to arduino by the service.
and reset to a " " for the service to remember that there is nothing more to do.

PV and SP and RSP need to be converted to PCT.

see record 526 at google, mode change to 2, output to 75,
now why is the setpoint wrong? because in MAN and AUTO the PID controller use its
AUTO setpoint ( open hardwareinput ) and not the RSP value.
( this test worked good, from powerup, but i made it without the power 5VDC
from arduino to servo. with servo i see 2 times problems / but i think
the power connector from the ext 9V is not good.

and with pressing reset on arduino leonardo, RPI sees a different port??

as the service is run by root ( at boot time ) and the operation will be started ( by you ) under user pi there might be some file access problems, but looks good.

now, we are not finished, even indication and operation look good, what is missing is a ( timed ) update of the indication ( bargraphs ) when the stream data file changes.
and my python and python Tkinter understanding is so poor, i need to dig for examples.

this Tkinter timer test, here on RPI ( via VNC ) works on the second compared with desktop.
first i try to indicate the time and for comparison the file info record time
so even the values not change i see that arduino - RPI data stream ok.
the faceplate has 5 sliders,
PV, SP, OUT are set with the value from file and then disabled == indicators only
for update must enable, set, disable.
RSP and MOUT slider are for the operation only.
but with the mode i have a problem, now i set it every sec with the value from file,
and can not operate it any more. there are workarounds, but because that mode indication is anyway a break in this faceplate philosophy, i choose to make a new separate indicator for MODE.

small add changes:
set arduino timer to 2sec / like PID cycle
when running at RPI
manualmenuoperation should be false, and [E] and [ ] is disabled,
when operate arduino by USB terminal window pls enable manualmenuoperation.

to start the faceplateoperation from desktop i made a button




UPS, the google excel is in PCT not degC!!
change in arduinostream
pls download this rev



CURRENT TREND MODULE
if i now want to add a so called "current trend"
i need to learn first
- how i know there are new data ( currently i read every sec and don't know if the data are new)
- how to make a array and fill it with PV SP OUT ( real )
- - how to make it a ring buffer
- how to open a new window and show PLOT

i start on one end: array and matplot lib (and play on desktop computer)

it was essential that the plots have same color as in faceplate
PV yellow, SP white, OUTPUT blue.
and even it could result in range problems i like them in one plot,
that's best for check on PID loop tuning.

today start same from RPI IDLE, and need 10sec ( with cpu 100% ), but it runs ( remember we installed matplotlib for a first test )
with a array length of 100.000 it needs 20sec, but still runs and no problem in taskmanager/memory
i do not understand the RPI operating system, but i expect that if (RAM - RAMDISK) is too small for OS and program variables ( like here long real arrays ) there will be a write / usage of a swap file on SD card, what means burning them.


that array / plot now is only a list, not a graph over time ( what is needed if the arduino reporting is NOT time equidistant
or i need a realtime axis ), check here and here
that date handling is tricky:
the record time i get via the csv line in ram disk file, as 6 strings, ",Y,m,d,H,M,S,,"
this can be packed in one string:
gettime= rec_year+"-"+rec_month+"-"+rec_day+" "+rec_hour+":"+rec_min+":"+rec_sec
and with strptime converted to a python date time info
datetime.strptime(gettime,'%Y-%m-%d %H:%M:%S')
and that can be converted to a matplot time float number
mgettime = mdates.date2num(..) function.
now i use that number to compare it with the prior record reading if that's a new record
and only then i do the faceplate update and the storing to array.


writing to a array with a specific length ( all filled with 0.0 ) i use a pointer.
where=where+1
if (where >= maxlength) : where = 0 # ring pointer
that is very fast ring buffer.
but when i call the trend ( and only at that moment have lots of time )
i need to make it to a clean linear list for matplotlib trend.
reshuffel a array every time you add/write a value ( 2 sec) is waste if time
only do it when you need the array for further processing,
like show trend after operator press the "CT" current trend button.
( now configured with 900 samples ( of 2 sec arduino send data ) == 30min )



HISTORIC TREND MODULE
and the "HT: historic trend button must link to the google sheet. ( i made mine public read now )
it can open a browser, but the RPI browser can not show the graph like your browser?

and RPI is anyway to slow for this.
but for operation from faceplate to arduino time is also too long ( 10sec)

now i have a backup of the SD card with the old system and a zip of the new code,
and above there was the open point with the limitations of the MIDORI browser,
it could not show the google graph. i found here that there is a better browser in development.
lets try that. ( from PC putty / Bitvise SSH client )

sudo nano /etc/apt/sources.list
deb http://raspberrypi.collabora.com wheezy web
paste as line 2 CTRL O CTRL X
sudo apt-get update
sudo apt-get dist-upgrade
[Y] ( one connection error )
sudo apt-get install epiphany-browser ( stopped, do all 2 times )
sudo reboot
- the system still works fine ( and records to google every minute )
- start [start][internet][epiphany web browser] and my webpage looks like it should.
but starting the google web2.0 datapage takes time and then comes with the same "MISSING PLUG IN" error message. But as page not fully loaded and cpu still 100% i want to sit it out.

Hmm, i do not know how to deinstall / remove a package in linux??

update: search the forum of raspberry and i found info regarding
install flash player:
sudo apt-get install gnash
sudo apt-get install browser-plugin-gnash

after that i could see the nice video at raspberry home page
and i tried above google spreadsheet again and got a timeout error

so i started to delete all my records down to a day, a hour, and now i see a graph!

one step, but still RPI to slow for that.



PID CONTROL TUNING
here i try a little bit faster menu


HOLD
that is not much better
i need to redesign that communication
arduino should learn how to read a CSV line, for set setpoints or set tuning.
i go back to the menu program ( and pls find there V2.4 )
and make a CSV line split ( to a array ( long 11 here ))
and modify the read number tool so it gives back
-1 for empty string or other problems
0 for text string
1 for integer string
2 for float string
there is a ( uncommented ) example for this menu [C] in the download

now i use this to modify PID_google04 arduino project.


as there is still space in arduino
i want try the PID tuning by variables again
even that's only temporary, ( i not again want use EEPROM... )
final setting are still to be set in arduino code.
the tuning and the initial setting for MODE MOUT / RSP.
the arduino should run as standalone PID module, RPI or USB menu terminal only optional.

menu "T" for tuning
PV fil0 addold:PVfil0,0.02
PV fil1 mul:PVfil1,0.98
Gain:PID_gain,0.20
Reset:PID_reset,0.03
Rate:PID_rate,0.00
PID_action -1/1:PID_action,-1
PID_outdir -1/1:PID_outdir,1


menu "t" for show tuning
PVfil0,0.02
PVfil1,0.98
PID_gain,0.20
PID_reset,0.03
PID_rate,0.00
PID_action,-1
PID_outdir,1

now, when the RPI is connected, these "T" should not sending anything back
and the "t" will show
PID,0.02,0.98,0.20,0.03,0.00,-1,1,, one CSV line only.
pls adjust //#define manualoperation for this

now i have to think how to deal in arduinostream with that possibilities.
first i wanted to do 2 new files, tuning from arduino and tuning to arduino
but if i use the existing 2 files i must identify the line ( JOB ) by a keyword
so ,pv,sp,output,mode for indication already have the "PCT" word,
for sending RSP, MOUT, MODE i can use also.

for tuning i can use the word "PID" and the longer variable list.
but a tuning display need not only the the new setpoints for the tuning, it needs to show also the actual settings.
menu [t] is for this, but the operation program have to tell the service program first to send this [t],
by putting a ",PID?," in the command file. service then sends the menu key "t" and asynchron ( no wait ) the ",PID," tuning answer will come and put it to the "fromarduino" file.
and in operation that "PID" line is detected and as actual tuning shown in a new window.
so that window will show AFTER arduino answered with the latest tuning and will close after [send] operation. ( that send button in the tuning window i could not make as a image button?? )
so for a new tuning step whole procedure again. press [PID] button, send "t", get "PID", show window


and when i am already about to change the record structure
i will now start all records with a DBI number ( a unique identification number for a loop on the highway )
in this case its ",1,"
,1,PCT,46.03,28.93,0.00,2, menu[t]
,1,PID,0.02,0.98,0.20,0.03,0.00,-1,1,,
,1,PCT,46.03,28.73,0.00,2,

so the PID line looks same:
after [t] from arduino, stored in "fromarduino.csv" file and read by operation to show actual tuning
and after tuning stored from operation in "toarduino.csv" file
and send as that string to arduino by service after using menu key [T]

later for the big job, my uPCS, running only in a arduino DUE, there is that "DBI" data base index prepared, with 2 PID, 2 IND, 2 DCD loop point (types).


here it works:

with current trend window


and the logfile, as it is recording each line arduino sends its growing fast and fills up RAMdisk.


pls download this rev as arduinostream4.zip

in case you stop the stream ( by the desktop tool .. )
and try to test / run it from the IDLE there will be a file error.
the 2 files in RAMdisk are owned by root, if you start the service again as pi the files can not be overwritten.
please delete them from terminal
sudo rm /run/shm/* and try start IDLE arduinostream.py again
restarting the service is no problem.




you have a arduino but no raspberry PI, no problem,
RUN IT FROM PC instead of RASPBERRY PI
the trend i made already on a PC in python
( what formerly was installed for the PMS3 tool )
so now i try to make this tool also available for PC.
And i try it with the laptop without PYTHON, so here the full installation.
( even there is a windows 7 64bit version running following works: )

from https://www.python.org/downloads/ get
python 2.7.8 for windows
doubleclick python-2.7.8.msi
it not make a desktop icon, pls find START / all programs /Python 2.7/ IDLE / for start
from https://pypi.python.org/pypi/pyserial
get pyserial-2.7.win32.exe run

find file / open / Scripts :miniterm and start, give in COMxx for your connected arduino

in open PYTHON shell type
import _tkinter and get >>> now i know its included already

from http://sourceforge.net/projects/numpy/files/
get numpy-1.8.1-win32-superpack-python2.7.exe run

from http://matplotlib.org/downloads.html
get matplotlib-1.3.1.win32-py2.7.exe run

from http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib get
python-dateutil-2.2.win32-py2.7.exe run
pyparsing-2.0.2.win32-py2.7.exe run
six-1.7.3.win32-py2.7.exe run

now get the project:
from kll.engineering-news.org
get arduinostream4.zip (RPI version )

now, even our PYTHON for WINDOWS is ready,
some things have to be changed in the code:

i created a working directory for the ( copy of ) the 2 python files
and a C:/Temp/ directory ( instead of ramdisk )

arduinostream.py
RAMDISKpath = 'C:/temp/' # for WIN PC
#RAMDISKpath = '/run/shm/'

initfilepath= RAMDISKpath # for WIN PC
#initfilepath= '/home/pi/arduinoproject/'
and copy toarduino_init.csv also to that
( with content like RSP setpoint 50% :)
,1,PCT,,50.0,20.0,5,,


# sport = "/dev/ttyACM0"
sport = "COM14" # failed PORT already open

#ser.open() #not need, and error in win 7


arduinooperation.py
RAMDISKpath = 'C:/temp/' # for WIN PC
#RAMDISKpath = '/run/shm/'

GIF_filepath = 'X:/xxx'

for test ( if service not run ) copy
fromarduino.csv to C:/temp/ ( with content like :)
,2014,08,12,21,36,44,,1,PCT,46.48,50.00,0.00,5,

the service arduinostream.py you start after testing
with RUN ( command prompt window with all full path information or just with double click on the arduinostream.py file )
C:\Users\me: C:/xxx/python.exe E:/yyy/arduinostream.py
check in C:/Temp/ for the update of the files
to stop the service type CTRL C in the command prompt window

the operation can start from python command line or python IDLE interface


in the new version these variable adjust to the operating system, still you need to preset them.



in a later version i also needed python PIL ( for *.jpg..):
from Python Imaging Library 1.1.7 for Python 2.7
get PIL-1.1.7.win32-py2.7.exe run



8. RPI installation
until now i distribute zip files, and i think to set it up in the RPI can be a pain in the back
i want to learn how to do / offer a installation routine
and i see from a PRO a example about his RPI camera tool ( Silvan Melchior ) what i use as basis for mine.
principle idea is a installation bash file and that get a zip file and does the rest of the job.
that installation routine you must get from my new /share/ directory and make it executable
UPDATE: now see here

add requirements:
installation of arduino IDE
sudo apt-get install arduino
on older systems might need install pyserial
sudo apt-get install python-serial
installation of python matplotlib
sudo apt-get install python-matplotlib
( the first start of the arduino PID operation took more 10 sec after that installation,
but the second start only 4 sec. )
these installations are now in the arduinoPID_install.sh included

edit arduinostream.py and give your google macro key in line 166 of 243
sudo nano /home/pi/arduinoproject/arduinostream.py
sudo reboot
now i test it with a new setup RPI and it works.
-1- a new NOOBS DEBIAN setup ( needed the TV for installation )
-2- a new BERRYBOOT, but there i could not download debian.
so i reinstall a berryboot backup of a debian from last year.
update it and do above installation.
- 3- in that BERRYBOOT i could download new debian after 5th attempt and set it up headless
! and google trend is running !
and just reboot and wait for the new line in the google sheet.
seems to be quite robust setup now, even JOSH ( where the idea with
historic trending in a google macro spreadsheet comes from ) now
can not recommend to use google!
and this you get by this installation v0.3



you can go on next page


more REMOTE
one of the open points for me is with the REMOTE thinking,
what i have now is a control loop / sensor / PID / valve, implemented in a RPI and arduino
powered by a wall plug adapter of the powered USB HUB, all for about 50EURO.
( i think about all the utilities what are required in a factory like instrument air ... )
for the cloud based historic data logging for controller and the remote operation / tuning, a LAN connection is required.
as RPI can use ethernet or WLAN ( what is even more perfect for REMOTE ) i check again on the configuration.
i would like to have a fixed IP either way, and i found it here.( but not work too well , so new version:
sudo nano /etc/network/interfaces
auto lo

iface lo inet loopback
iface eth0 inet dhcp
iface eth0 inet static
address 192.168.1.101
gateway 192.168.1.1

allow-hotplug wlan0
iface wlan0 inet manual
post-up iwconfig wlan0 power off
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet static
address 192.168.1.102
gateway 192.168.1.1
netmask 24

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="YOURWLANSSID"
key_mgmt=NONE
auth_alg=OPEN
wep_key0=YOURWEPKEY
}

The setup is running on WIFI and the headless way i worked anyhow now makes sense.
i have to test later what happens if i connect ethernet and WIFI adapter is still in??
if there are problems check: RPI 4um
This fix IP thing is also required for even more "REMOTE" usage via internet:
-a- a web server in RPI
-b- remote SSH ( should be possible )
-c- use RPI camera additionally
because the router can have forward ports configured only to a fix IP and must use a other port as 80,
i use DLink DSL-2750E.


-a- a web server in RPI
for the web server installation i want try apache again:
sudo apt-get install -y apache2 php5 libapache2-mod-php5
and reboot and try from PC browser: http://192.168.1.101 to see the apache well come page.
now the whole router and NO IP part is very difficult for me, i hope i don't give up.
for the port i change:
sudo nano /etc/apache2/sites-enabled/000-default 80 > 4000
sudo nano /etc/apache2/ports.conf 80 > 4000
sudo reboot
now i must call from browser: http://192.168.1.101:4000 to get connected.
and in the router i add:
Now the RPI web server should be visible from the outside also, if you have the web IP.

or use some free dynamic DNS services. ( i had that DYNDNS account but that is finished / not free anymore )
testing a FREE one i try freedns and find here how to teach my new RPI webserver to update the DNS.
as described in that manual after registration ( get your subdomain "MYRPICAM.mooo.com" ) you need to test it, best is you ask a friend to try in his browser
http://MYRPICAM.mooo.com:4000/
like i test it with http://MYRPICAM.mooo.com from my own computer ( and in router no port forwarding, but DMZ enabled )
it did work for me, but not for a friend from "outside", very confusing, i have no idea about that stuff.

possibly you can try to call a proxy first and from there call your new RPI homepage.in free-proxyserver give "MYRPICAM.mooo.com:4000/" and see your page. Is that proof that it works??
lucky if you have a tablet, switch of WLAN, go ONLINE by mobile network / SIM card and try from there.

Now, we need a update service for the free dns service, means to teach them your actual IP.

to check on your IP:
google search "MY IP" or dnsdynamic or like in the following code dyndns
to check on the IP the DNS service is pointing to, you could ping MYRPICAM.mooo.com
or like its done in following code call that page and check the HEADER.
The update at FREEDNS:
you need to login at freedns "for members" [main menu] and get under menu [Dynamic DNS] down that page, find the link for "DIRECT URL". save the LINK, in there is your HASH code. you could call that link ( by browser) from any computer in your LAN anytime and FREEDNS would know your actual IP.
all that can be done by that ( modified for test ) code in the RPI and a cron job to execute it repeatedly.



BUT: if you own a website, and the webspace provider allows a sub domain you could try it there:
with a PHP / HTML index.php you can make with header(".."); a redirect but that will show actual IP and port in the browser
or use a frame to hide that.


now you need a update routine also:

it can show your IP or, with password, set your IP what is used in the frame to link to.
Now that needs to be called by RPI CRON again.
pls download the automated webserver installation, above website and the MYDYNDNS php source
like that


-b- remote SSH have to dig deeper and need a friend for online testing


-c- use RPI camera additionally
when i updated 2014 debian raspbian i noticed the python camera tools are now preinstalled.
There are many ways to use the camera but as i want learn python i will study this
Pure Python camera interface, last year i play with that already pls see here under test PYTHON 2:


because even with more camera support in new raspbian, from desktop it is still not supported, and the preview works on TV but not headless.
just a short example here:
cd /home/pi/
mkdir /home/pi/camera/

nano /home/pi/camera/resize.py use this
nano /home/pi/camera/resize.sh use
#!/bin/bash
cd /home/pi/camera/
python /home/pi/camera/resize.py
gpicview /run/shm/last.jpg

sudo nano /home/pi/Desktop/resize.desktop use
[Desktop Entry]
Version=1.0
Name=RPI-cam_resize
Comment=python camera small picture service
Exec=lxterminal -e '/home/pi/camera/resize.sh'
Icon=/usr/share/pixmaps/pstree32.xpm
Terminal=false
Type=Application
Categories=Application;
StartupNotify=true



with the actual setting it only makes files at RAMDISK ( lost with reboot)
and will show the just last made picture, but the gpicview allows to cycle through all of them you made in that session.
there is in terminal a small error about the gpicview call ( from headless? ) a unanswered question also i see at forum
possibly the VNC picture resolution??



at the moment that are just explorations for the "MORE REMOTE" possibilities.
i not made my mind up how to use camera and webserver for the arduino PID project.



the real world or as TONY STARK say

with the software and also already with
- input one wire temperature sensor DALLAS DS18S20 ( 100THB)
- output to servo Tower Pro SG91R ( 90THB)
i follow my friends idea for his PIZZA dough temperature control
but i not fully understand what / how he want to control,
i need a fast and easy test setup now.
as the PV is the temperature sensor my idea is to use a old light bulb as a heater.
influence by distance easy adjustable ( a hardware control by dimmer / even operate that by servo would be possible too )
but i have a idea that the servo has to operate a HEAT FLOW VALVE
means a something in between the bulb light irradiance and the sensor,
but not in form of a FLAP ( open close ) what might give / need a 2 point controller,
it should have a nice analog / linear control function ( over the 0 .. 100% control output )
( and it should not cost anything!! )


So back to arduino IDE ( on PC, manual operation enabled ) rev 4.05
first i must state that there is a break in the logic of the operation concept, compared to a PCS:
there is a operator on a computer screen and in
- MAN he can change the OUTPUT
- AUTO he can change SETPOINT
- CAS / RSP he can operate only the upper / cascaded loop
- - he can change the MODE..

now here is it different because i designed a hardware input for a POTI to the SETPOINT in AUTO mode.
a local operation possibility ( without any USB connected )
that requires that at boot arduino PID goes in AUTO mode. ( code setting )
but there is no local mode switch and no OUTPUT setpoint input designed.
what should be there is a hardware input of a THH over temperature switch what sets the control to interlock mode == OUTPUT 0
( but as a arduino would never be considered as safety controller ( with tripple redundant cpu...)
actually that THH would be only indicated ( by its second contact output ) and has its own hardware to the ON/OFF switch for the HEATER.


circuit

using the 5VDC generated by arduino from 9VDC might be not enough for a servo, possible reboots of arduino ...
pls check the mA consumed by servo when
- in position < 20mA
- moving 70mA metal gear, 130mA plastic gear ( no mechanic load!! )
- check with your LOAD !
- in end position with problems in the setup myservo.attach(9,400,2100); or its a bad servo, i see near 400mA and gets hot and hums.
external 5 .. 6 VDC supply for servo are better, and you might not need more hardware, because in that case might not need the 9VDC to arduino as RPI could power it ( and poti and dallas ONLY )


now to the shadow control
first some new wires,
like the DALLAS should have a 2 or 3 wire flexible cable ( anyway better buy the food grade steel capsuled version ) but i think a PID control would be a fix setup, and i could not find a audio cable shop nearby, so i ended up again with a 4 wire telephone cable for 5m / 40THB / 1 euro.
i use black to left and right pin, red to resistor soldered near sensor head, yellow other side of resistor and middle pin.
the servo has a 25cm 3 wire flex cable with a plug, the pins from the breadboard wires fit.
the poti also needs 3 wires ( from a BOX front?? ) but as i not have a 5 .. 10 kohm poti for box installation now i use the breadboard again.
i do not need that setpoint for the test, just without it the setpoint in manual mode looks so ugly from the open Ain1.


for the heat control valve i use a plastic board, cut a triangle off, and use that as movable part.
some paperclip steel as axis and actuator from servo
a position indication 0 .. 25..50..75..100 shows that the rotation servo / axis 1to2 / and servo position not give a fully linear valve position.
the hole in the main board ( the "seat" i made a small triangle hoping for a linearization effect )
lets the heat / light to the sensor,
the "disk" is the turnable cut of part, closes the hole at position 100%
so it must be called a "increase to close" valve or N.O. Normally Open valve.
but there is no fail position with a servo ( and no spring ) so its not a F.O. Failure Open valve.
later in the tuning we will talk about that again.




ok, the lamp is too strong and shines through the plastic ( my valve seat leaks! )
but mostly its a IR effect from my USB camera i know already,
more after the temp. sensor is installed.
( following test i made with arduino leonardo, windows 7 desktop PC )
Here the first MAN and RSP test, already made the first tuning step, increase RESET , its still very slow but the trend is good. ( and not only from the data, that python mat-plot-lib tool is fantastic, for the below snaps i used the build in rectangle zoom function of that windows! )

how you interpret? In RSP the PID control could catch the SETPOINT, but the PV ( temperature ) swing at first over the SETPOINT about 1 degC, see also google historic trend




loop check, control start up and TUNING
-.1- what is PID
it is a BOX with a mathematical model with 2 inputs
- PV ( process value / our Temperature )
- SP ( setpoint from poti in AUTO or from RPI in RSP mode )
and one output
- OUT ( 0 .. 100% ) here send to a servo
and even we use computer / numeric calculation it is
ANALOG ( and could have been build with one operation amplifier and some components )
in our case it is actually only a 2 line code in arduino


d = SP - PV

OUT = GAIN * d + RESET * ( iOLD + d ) + RATE * ( d - dOLD )



there are 2 memory variables
- iOLD
- dOLD
what makes that box smart and we have already 3 tuning variables
- GAIN
- RESET
- RATE
add there is a
- PIDaction ( 1 or -1 )
and outside the BOX a
- OUTPUT INVERT ( 1 or -1 )
and also outside / prior to the BOX is a PV filter installed
- PV = PVOLD * PVfil0 + PV(input) * PVfil1
with PVfil0 + PVfil1 = 1

for the PID / BOX this PV filter is part of the process, like the output invert and the valve and the real process and the sensor and sampling...
thinking in a CLOSED LOOP


-.2- open loop / closed loop
now when control is in AUTO or RSP the loop is closed.
when you now look at the trend of PV, SP, OUTPUT there is no way to know what is responsible for what.
if you want tune the PID parameter you first have to operate
the open loop ( MAN ) and learn how the field ( or better OUTSIDE of the PID BOX ) reacts.

-a- if the OUTPUT is 75% the valve must be open 75%
and not 25%, if you have a increase close valve like i build here you need to invert the output tuning first

-b- if you now change in MAN the output you can see a PV reaction
that is the combination of your real process AND the PV filter
i have seen that this confuses most of the "TUNERS"
and i consider the filter tuning as more critical as the PID parameter.
while testing sensor ... and the PV is jumpy you tend to filter stronger.
later when you tune the PID you want to make it faster, but it does not work!!!
and as the real temperature might be higher as the filtered indicated value already product got damaged.

-c- there is NO WAY a PID can be faster as the PV reacts on a MAN OUTPUT 0 to 100% jump!
that is called process identification ( and that includes the PV filter tuning!
now later in the real world you might not be able to do that 0 ..100% output jump
( because the product got damaged or even the plant might burn down )
so you have to learn to identify the field by a smaller output step.

-d- now we adjust the output in MAN so that the
PV is near to the setpoint, that we adjusted to our requirements .
if you can not do that something is basically wrong
and also a PID AUTO will not help.
( check valve moves, heater is on, sensor works ...)

-e- and now we think / or we test / what the control does when its in AUTO
we need to tell the PID how to work "as HEATER or COOLER" and adjust PIDaction
so the PV will follow the SP after a small SP change. If the PV goes to the opposite direction change it.
now here is a funny thing: if PIDaction and OUTPUT INVERT are both wrong, PID will work! BUT the indicated valve position is wrong.
i see that hundred of times, a very confusing thing.

-f- and only after all above works you can start playing with the PID parameter GAIN RESET RATE.

above PV reaction on a output change already gives you a feeling for your process,
actually it can be analyzed mathematically according several methods to find good PID parameter,
that is called auto tuning... but its to early for that here.

i want to mention a old RULE i learned
( still on analog control boxes with potis for tuning... )

- start with a P control ( I ( reset ) and D ( rate) = 0 )
- - increase the P ( gain) until PV can catch SP.
- - increase if further until the PV ( and output ) starts swinging,
- - and now reduce P to 1/2 of its setting
- and increase I ( reset ) until the PV catch SP in a acceptable time and not with too much swinging "over" the SP.

that should be tested in several working points ( if possible)


the D ( rate ) is often considered as a tool ( help ) to react fast on influences / disturbances on the process ( PV ) from the outside.
But there are processes what require definitely a rate tuning.

Actually tuning know how is less important as process know how, i admire that process engineers ( like in chemical plants ) what sit and stare at the current trend for 3 days ( without any of the above necessary SP changes i would need to check if my tuning change worked ) and then they change the parameters.
And only in case of a change of working point ( like plant throughput increase ) they need to do it again.


as this little project was about a (one) PID control in UNO/Leonardo/ProMicro/
and operation from RPI ( or WIN 7 PC )
now i want go back to the bigger brother, the uPCS project.