Wednesday, January 28, 2015

Octave and Fuzzy Logic For the Paint Flow and Silkscreen Projects


I was eager to improve on the Fuzzy Logic control algorithm that I had originally developed for our paint flow control projects with K&H Finishing, Enclosures Engineering and Control Coat LLC. Fortunately Franklin Eventoff contacted Thomas Nguyen about a new project for Thomas and me, the Silkscreen Project, and I planned to develop a Fuzzy Logic algorithm for the project.

I am by no means an expert in Fuzzy Logic systems development. This is only my take on developing FL software for control. Certainly there are other ways to do FL control and others may know of better ways. Also, the FL control scheme that I developed for the Silkscreen project is considerably different from the scheme that I did for the Paint Flow Control systems, since the Silkscreen process is so very slow, requiring an hour for the conductive ink to dry on the panel before measurements can be made and corrections done to the conductivity of the ink.

Previously I would write C code to develop a FL control algorithm, producing a 3-D control surface or Look-Up_Table (LUT) using the Center of Gravity Method. See the excellent paper "Development of Fuzzy Algorithms for Servo Systems" by Y.F. Li and C. C. Lau, IEEE Control Systems Magazine, April 1989, pp 65-71.

(http://ieeexplore.ieee.org/xpl/login.jsp?tp=&arnumber=24814&url=http%3A%2F%2Fieeexplore.ieee.org%2Fxpls%2Fabs_all.jsp%3Farnumber%3D24814)

Paint Flow Control
I did not follow Li and Lau's paper religiously but used it as a guide to develop the rules and the charts of the various terms: LN, MN etc but . Below are the eleven rules that I used to produce the Fuzzy Logic Look-Up-Table (LUT), or Control Surface, shown below the rules. The first term in each rule is for the Error of the flow rate. The second term is the change of the Error from the previous measurement, or approximately the derivative of the Error. The third term is the Output value. I chose a triangular or trapezoidal shape for each of the terms. Those shapes are shown below the rules. For the terms, LN means Large Negative, MN means Medium Negative, SN means small Negative, ZE means Zero, SP means Small Positive, MP means Medium Positive and LP means Large Positive. So LP as the first term means "if the Error is Large Positive." This is explained in the Li and Lau paper referenced above.

For the Paint Flow Control system, the flow measuring device was a mass flow meter which output a current of 5 to 20 mA for zero flow to the full-scale flow. Also the controller output was a current from 5 to 20 mA which drove a current-to-air pressure transducer with then controlled the paint pump rate. In the C code, I defined the conversion factors: for the flow rate sensor and for the paint pump rate. And also I defined the conversion values for the rules such as the lbs/min that represents Zero, Small Negative etc.

Rules for Paint Flow Control Systems.

The shapes that I chose for the terms, LN, MN, ... LP, are shown in the charts below. The range is +/- 5, representing positive full scale to negative full scale of the Error and the Error Rate and the Output. So for example, -5 represents zero flow error and +5 represents full scale flow error.

Chart of Fuzzy Logic Terms for Paint Flow Control Systems.

My C code compiled the eleven rules and used the terms, charted above, to produce the LUT below. The 3-D chart of the LUT, or Control Surface, is shown to right of the LUT below. The axis along the front of the 3-D chart is the Error axis and the orthogonal axis is the Error Rate axis.


Fuzzy Logic Control Surface for Paint Flow Control Systems.

Silkscreen Project
For the Silkscreen Project, I started from scratch to write the Fuzzy Logic rules and design the terms, then computed the LUT and produce 3-D Control Surface Download.

For the analysis, I decided to use Octave to design the rules and terms. Octave is similar to Matlab but at this time has a command-line interface. The first line of the Octave description is:

"GNU Octave is a high-level interpreted language, primarily intended for numerical computations."

Octave is a free download from GNU.org. Info for downloading the current version of Octave for Windows can be found in the blog at: http://youneedtoprogram.wordpress.com/2013/10/12/installing-octave-3-6-4-on-windows-8/ 

or can be downloaded from the SourceForge website
http://sourceforge.net/projects/octave/files/Octave%20Windows%20binaries/

Download the tar.gz file "octave-3.6.4.tar.gz" or whatever is the latest version and install it. The Octave program loads in C:\Software\ and users manual is Octave.pdf which is loaded at the same time as the exe file. It is in C:\Software\Octave-3.6.4\Doc\PDF and also there is a 2005 tutorial at http://www-mdp.eng.cam.ac.uk/web/CD/engapps/octave/octavetut.pdf.

I also downloaded and installed the Octave Fuzzy Logic Toolkit and used it to perform the analyses of my rules and terms. The Toolkit can be downloaded from the SourceForge website at the Fuzzy Logic Toolkit download site. Documentation for the Octave Fuzzy Logic Toolkit is at Fuzzy Logic Toolkit Documentation. The Octave Fuzzy Logic Toolkit is very similar to the Mathworks Fuzzy Logic Toolbox and there is considerable documentation on the Mathworks website at Mathworks Fuzzy Logic Toolbox. The Fuzzy Logic Toolkit for Octave was developed by L. Markowsky  while a student at the University of Maine. Markowsky also maintains the code. See http://ieeexplore.ieee.org/xpl/articleDetails.jsp?reload=true&arnumber=6184706.

Download and save the Fuzzy Logic Toolkit tar.gz file and move or copy the tar.gz file to the Octave folder at C:\Software\Octave-3.6.4\, then start Octave and type "pkg install fuzzy-logic-toolkit-0.4.5-tar.gz" without the quotes. After installing the FLT, check for packages installed by typing "pkg list" without the quotes and the FLT should show up in the list.

The blog at: http://youneedtoprogram.wordpress.com/2013/10/12/installing-octave-3-6-4-on-windows-8/ provides much useful information on Octave such as making "m" file programs and running them in Octave.

Running the FLT, I set up the Mamdani method and other settings as shown in the Flow_calculator2.fis file listing below, modified from the mamdani_tip_calculator.fis file in the Fuzzy Logic Toolkit. The fis file is operated on with my FlowControl2.m code.. My FlowControl2.m code is modified from the mamdani_tip_demo.m program also included in the Fuzzy Logic Toolkit. Below is the listing of my Flow_calculator2.fis file. As shown, the Fuzzy Logic files were originally written by L. Markowsky.

## Copyright (C) 2011-2012 L. Markowsky <lmarkov@users.sourceforge.net>
##
## This file is part of the fuzzy-logic-toolkit.
##
## The fuzzy-logic-toolkit is free software; you can redistribute it
## and/or modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 3 of
## the License, or (at your option) any later version.
##
## The fuzzy-logic-toolkit is distributed in the hope that it will be
## useful, but WITHOUT ANY WARRANTY; without even the implied warranty
## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with the fuzzy-logic-toolkit; see the file COPYING.  If not,
## see <http://www.gnu.org/licenses/>.

## Author:        L. Markowsky
## Keywords:      fuzzy-logic-toolkit fis
## Directory:     OctaveFolder
## Filename:      Flow_calculator2.fis
## Last-Modified: 6 Mar 2013, J. Baker

[System]
Name='Flow_calculator'
Type='mamdani'
Version=2.0
NumInputs=2
NumOutputs=1
NumRules=5
AndMethod='min'
OrMethod='max'
ImpMethod='min'
AggMethod='max'
DefuzzMethod='centroid'

[Input1]
Name='Error'
Range=[-5 5]
NumMFs=6
MF1='LN':'trapmf',[-6 -5 -4 -2]
MF2='SN':'trimf',[-4 -2.0 0]
MF3='ZE':'trimf',[-2.0 0 2.0]
MF4='SP':'trimf',[0 2 4]
MF5='LP':'trapmf',[2 4 5 6]
MF6='ALL':'trapmf',[-6 -5 5 6]


 [Input2]
Name='ErrorDot'
Range=[-5 5]
NumMFs=6
MF1='LN':'trapmf',[-6 -5 -3 0]
MF2='SN':'trimf',[-3 -1.5 0]
MF3='ZE':'trimf',[-1.5 0 1.5]
MF4='SP':'trimf',[0 1.5 3]
MF5='LP':'trapmf',[0 3 5 6]
MF6='ALL':'trapmf',[-6 -5 5 6]
#MF7='NLP':'trapmf',[-6 -5 0 1]
#MF8='NLN':'trapmf',[-1 0 5 6]

[Output1]
Name='Output'
Range=[-5 5]
NumMFs=5
MF1='LN':'trapmf',[-6 -5 -4 -2]
MF2='SN':'trimf',[-4 -2 0]
MF3='ZE':'trimf',[-2 0 2]
MF4='SP':'trimf',[0 2 4]
MF5='LP':'trapmf',[2 4 5 6]


[Rules]
1 6, 1 (1) : 1    ## LN ALL: LN
#
2 6, 2 (1) : 1    ## SN ALL: SN
#
3 6, 3 (1) : 1    ## ZE ALL: ZE
#
4 6: 4 (1) : 1    ## SP ALL: SP
#
5 6, 5 (1) : 1    ## LP ALL: LP
#


As seen above, there are only five Error input terms for the Silkscreen Control System. There are six ErrorDot Input terms but the five Rules ignore the ErrorDot Input terms because I decided that the SilkScreen operation is so slow that dynamic control isn't necessary and so in the Rules list, the ErrorDot values are all "All" terms. There are five Output terms and five rules. And note that a different terminology is used for the Octave Fuzzy Logic Toolkit. The reason for so few terms and rules is that the SilkScreen system is very slow acting, requiring one hour for the ink to dry before the resistance of the screened panel can be measured. So this system is quite different from the much faster acting Paint Flow Control System where the system is dynamic and consequently requires more Derivative action to achieve good control of the paint flow.

My FlowControl2.m program listing is shown below. It is modified from Markowsky's mamdani_tip_demo.m code.

## Copyright (C) 2011-2012 L. Markowsky <lmarkov@users.sourceforge.net>
##
## This file is part of the fuzzy-logic-toolkit.
##
## The fuzzy-logic-toolkit is free software; you can redistribute it
## and/or modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 3 of
## the License, or (at your option) any later version.
##
## The fuzzy-logic-toolkit is distributed in the hope that it will be
## useful, but WITHOUT ANY WARRANTY; without even the implied warranty
## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with the fuzzy-logic-toolkit; see the file COPYING.  If not,
## see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Script File} {} mamdani_tip_demo
## Demonstrate the use of the Octave Fuzzy Logic Toolkit to read and evaluate a
## Mamdani-type FIS stored in a file.
##
## The demo:
## @itemize @minus
## @item
## reads the FIS structure from a file
## @item
## plots the input and output membership functions
## @item
## plots each of the two FIS outputs as a function of the inputs
## @item
## plots the output of the 4 individual rules for (Food-Quality, Service) = (4, 6)
## @item
## plots the aggregated fuzzy output and the crisp output for
## (Food-Quality, Service) = (4, 6)
## @item
## displays the FIS rules in symbolic format in the Octave window
## @end itemize
##
## @seealso{cubic_approx_demo, heart_disease_demo_1, heart_disease_demo_2, investment_portfolio_demo, linear_tip_demo, sugeno_tip_demo}
## @end deftypefn

## Author:        L. Markowsky
## Keywords:      fuzzy-logic-toolkit fuzzy tests demos
## Note:          This example is based on an assignment written by
##                Dr. Bruce Segee (University of Maine Dept. of ECE).
## Directory:     OctaveFolder
## Filename:      FlowControl2.m
## Last-Modified: 6 Mar 2013, J. Baker zingbats

## Read the FIS structure from a file.
#disp("before readfis")
fis=readfis ('Flow_calculator2');
#disp("after readfis")
## Plot the input and output membership functions.
plotmf (fis, 'input', 1);
plotmf (fis, 'input', 2);
plotmf (fis, 'output', 1);

## Plot the Output as functions of the inputs
gensurf (fis, [1 2], 1);
## gensurf (fis, [1 2], 2);

## Calculate the Outputs as a function of the inputs = (-1, 1).
##inputValues = [0 0; -1 1]
##inputValues = [-1 1]
[output, rule_input, rule_output, fuzzy_output] = ...
  evalfis ([-1 1], fis, 1001);
  ##evalfis (inputValues, fis, 1001);
##disp("nargin for evalfis = ")  #disp(nargin)

## Plot the first output (Output) of the individual fuzzy rules
## on one set of axes.
x_axis = linspace (fis.output(1).range(1), ...
                   fis.output(1).range(2), 1001);
colors = ['r' 'b' 'm' 'g'];
figure ('NumberTitle', 'off', 'Name', ...
        'Output of Fuzzy Rules 1-5 for Input = (1, -1)');

for i = 1 : 5
    #disp(i)
    #(y_label = [colors(i) ";Rule " num2str(i) ";"];)
    #plot (x_axis, rule_output(:,i), y_label, 'LineWidth', 2);
    plot (x_axis, rule_output(:,i), 'LineWidth', 2);
    hold on;
endfor

ylim ([-0.1, 1.1]);
xlabel ('Error', 'FontWeight', 'bold');
grid;
hold;

## Plot the first aggregated fuzzy output and the first crisp output
## (Output) on one set of axes.
figure('NumberTitle', 'off', 'Name', ...
       'Aggregation and Defuzzification for Input = (-1, 1)');
plot (x_axis, fuzzy_output(:, 1), "b;Aggregated Fuzzy Output;", ...
      'LineWidth', 2);
hold on;
crisp_output = evalmf(x_axis, output(1), 'constant');
#(y_label = ["r;Crisp Output = " num2str(output(1)) "%;"];)
plot (x_axis, crisp_output, 'LineWidth', 2);
ylim ([-0.1, 1.1]);
xlabel ('Output', 'FontWeight', 'bold');
grid;
hold;

## Show the rules in symbolic format.
puts ("\nFlow Control Rules:\n\n");
showrule (fis, 1:columns(fis.rule), 'symbolic');
 

Charts of the Error and Output terms are shown below. Along with the Error terms chart, I am showing the 3-D Control Surface that is used for the actual control of the ink pumps and you can see that the 3-D Control Surface for the Silkscreen Control System is considerably different and simpler than that for the Paint Flow Control System with no Derivative or Error Rate control. I'm not showing the Input2 or ErrorDot Terms because they are ignored in the Rules list in the "fis" file above.


Input and Output Terms Charts, SilkScreen Project.
In the Error Terms chart, the Error range is -5 to +5 for negative full-scale error to positive full-scale error. The vertical axis is the Contribution to the Output for the calculation of the 3-D Control Surface.

To run the FlowControl program, I typed "cd c:\OctaveFolder" into Octave and then "FlowControl2", again without the quotes. Octave then proceeded to graph the Terms Charts and the 3-D Control Surface. The 3-D Control Surface is shown in the chart below. Additionally, I modified the gensurf.m code of the Fuzzy Logic Toolkit to output a file of the Look-Up-Table or LUT that is represented by the 3-D chart below and I entered the LUT into my LabWindows code to do the actual simulation and control.

3-D Control Surface for the SilkScreen Project.

Happily the Fuzzy Logic LUT inserted into my LabWindows code worked very well in the simulations, as described in my previous post on the SilkScreen Project but unfortunately Thomas Nguyen and I were not able to test the operation of the control system software since the project was canceled before we could test out my code on the real Silk-Screening system.

I am extremely grateful for L. Markowsky's development of the very valuable and highly usable Fuzzy Logic Toolkit and for providing it for use in Octave through the SourceForge website. Certainly the Fuzzy Logic Toolkit is better code than the C code that I had written and used for the Paint Flow Control Project. More information is provided on the SourceForge website at http://octave.sourceforge.net/fuzzy-logic-toolkit/overview.html.


3 comments:

  1. Hello, I'm just trying and I have the following error:
    >>FlowControl2
    >>error: 'readfis' undefined near line 54 column 5
    >>error: called from
    >> FlowControl2 at line 54 column 4

    Do I need someting special to load the Fuzzy-Logic-Toolkit ?
    Regards
    Slim

    ReplyDelete
    Replies
    1. I need to execute "pkg load Fuzzy-Logic-Toolkit" before.

      Delete