Articles Hierarchy

Articles Home » Arduino Projects » again PID control Arduino

again PID control Arduino

again PID control Arduino?

?PID? here we talk that the Arduino acts as a closed loop controller ( reading input, calculating / forcing output based on a algorithm you can see here

now there are my existing projects UNO PID or the big brother uPCS.
but working on RPI web site, mini blog, flask, flaskr, sqlite, html5up this would be a more interesting Arduino -- RPI project as the Firmata remote I/O, as there i got angry because i not understand the libraries ( firmata on Arduino and pyfirmata on RPI ) especially because i needed to use it now from a web site / what usually sleeps until a HTML page is called over the web / not from a continuous server side python program as before /
( this shows also that i not understand FLASK? can i do a conti server job there like for continuously catch data and historic trending? like a background job ( besides waiting for HTML activity..) )
but the biggest difference is that this FLASK python webserver creates a HTML web page: and now i will need to show the FACEPLATE in HTML.. and not python TK...
so, i will have fun to make again a PID FACEPLATE but in HTML CSS jquery? animated SVG?


but first the basic spec for the arduino (UNO) code.

-a- TIMING: we need time slices to run the code for
++ one PID Proportional Integral Derivation control point ( every second )
++ 4 IND analog indicator points ( every 5 seconds )
add a
++ 1 DCD digital control point ( run PLC style by digital event and USB operation event )
inbetween the USB is scanned every 0.1 sec for server requests

-b- COMMUNICATION i insist again on a readable ASCII over USB
while the CSV data lines i used for uPCS are readable but not understandable, just separated numbers, i want a slow but full-text version now.
( so can test without server side program like just from arduino IDE terminal )
but i want it little bit tricky:
-b1- points report in real time ( like the PID every second / after action )
so no time stamp by arduino ( for historic trending it could be added by server when it catch the data line )
-b2- but if server is disabled (not reads ) we not jam the USB ( server has to activate USB reporting by a cmd: {'UNO1':{'D0':1},'CP':44} , D0 is the print control loop report enable bit,
what timeouts after 5sec.
-b3- the protocol content is variable
while the full variable list for the PID could be:
{'PID1':{'PV':55.0,'SP':33.6,'OV':99.7,'MD':3,' Pt':0.4,' It':0.1,' Dt':0.0,' At':0.3...}}
server RPI >> UNO could just send a
{'PID1':{'SP':33.6,'OV':99.7,'MD':3}} as that is what can be operated setpoint, output, mode
but also a new tuning: parameter for P (gain), I (reset), D (rate), digital input filter for PV
{'PID1':{' Pt':0.4,' It':0.1,' Dt':0.0,' At':0.3...}}
and arduino controller could just update the measured value for PV like
UNO >> RPI
{'PID1':{'PV':55.0}}
generally Arduino / The Controller / only thinks in 0.00 .. 100.00 [%]
[TAG NAME][TAG DESCRIPTION][LOW RANGE][HIGH RANGE][ENGINEERING UNITS]
for PID1, IND1, IND2, IND3, IND4, DCD1
are only known at visualization level.


one good question is if i need a command protocol pointer like
RPI>> UNO
{'PID1':{'SP':33.6},'CP':34}
and after get, understand and applicate to point PID1
UNO >> RPI
{'PID1':{},'CP':34} as a acknowledge of the command.
now that could make it possible / or would require / to check on a command send timeout on RPI side.


last time when i made that motor control point (soft MCC) in arduino the company what used it
did not understand my communication idea, so one basic feature i expect is:
the server RPI sends every line what not fits the (JSON) line communication frame or has other error ( like text to number fails.. ) to a error log file.
so i can work on arduino side with ( variable level ) diagnostic prints ( in plain text.. )
what will show off in that log file. even a protocol to enable / disable diagnostic messaging should be possible.
RPI >> UNO
{'UNO1':{'D1':1},'CP':35}


Start coding ARDUINO ( using IDE 1.8.1 nightly )

-1- just run a counting loop with a debug time reporting if we made a mistake what blocks the control / cpu, a kind of quality control measurement


in other words, a UNO can count to 1.000.000 in 2.77 secs.

-2- now we need the 0.5 sec time slices for the main control points.
again better make a 2 step thing:
- count to N until N_END
- - check millis passed since last loop point execution
need to learn how to tune N_END to have min 1Mloops but still accurate 0.5sec events.
the point is: millis() is a time consuming command so do it every loop just burns cpu time,
so i nest time loops below count loops!
every 0.5 sec slice i do the PID or one of the 4 INDs,
=>> PID 1sec, INDx 5sec or load thinking: 1 PID and 1 IND per second.

using D1 diagnostic print bit. Now switch it OFF and show PV printing ( by D0 ).
for the first steps it is good to send some simulated PV, nice sinus for trending...


-3- now we need to check on any USB input ( from RPI / terminal )
here again i first check if some counter finished,
then if there is "something" from USB in buffer
and then start a line input routine ( with timeout check ) int serial_wait(boolean debug, unsigned long newtimeout ) { ...}

working with a different D2 diagnostic print bit for serial only.
the byte check of the protocol syntax is heavy, but very detailed error msg will be send back if one byte is not like or where it should be.
until now the NETTO data, the inner { xxx } is only checked regarding the RPI command to send data / against a timeout of 5 sec // makes about 10 lines (simulated ) measurements //
{'UNO1':{'D0':1},'CP':44}

so i did this version just for testing a protocol, there is no real control function used.


code


so i just downloaded it from RPI ( by browser ), unzip to /home/pi/Arduino/ ( default sketch dir )
and connect UNO and download and test.



back to the project Maker Blog