Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Forth sourcecode for fuzzy control of ball balancing?

132 views
Skip to first unread message

Manuel Rodriguez

unread,
Jan 2, 2019, 5:52:30 AM1/2/19
to
Fuzzy control is a grammar driven control technique which allows
to realized sophisticated control systems. In the literature the
standard example of a ball on a plate is often mentioned. The idea is
to define some linguistic variables for example acceleration, position,
distance-to-trajectory and some rules like moveleft, moveup and stop. Then
a control program is created which is realizing the fuzzy set in reality.

Without any doubt the best programming language for implementing fuzzy
control is Forth. It allows to define the fuzzy rules as words and
thanks it's interactivity capability it's great for interactive testing
the system. My question is: Are sourcecode examples available in which
Forth was used for fuzzy control?

minf...@arcor.de

unread,
Jan 2, 2019, 6:11:10 AM1/2/19
to
With due respect: this seems a Forth-centric tunnel view.

In reality you need sensors, actuators and a controller eg PLC. Their toolstack
determines what you have in hand. And it is to 99.999% not in Forth.

When you're lucky you can develop and simulate using a fuzzy control toolbox. Eg.
https://www.mathworks.com/products/fuzzy-logic.html


minf...@arcor.de

unread,
Jan 2, 2019, 6:15:48 AM1/2/19
to
p.s. I forgot:

see page 6ff in
http://www.forth.org/fd/FD-V18N6.pdf

Ahmed MELAHI

unread,
Sep 11, 2023, 12:36:18 PM9/11/23
to
Hi,
Here is a little program that permits to create fuzzy systems.
Save this program as fuzzy.fs and include it in the file where a fuzzy system will be created.

\ Here begins the code
\ Mamdani type fuzzy system with normalized output (in [-1, 1])
vocabulary fuzzy fuzzy definitions
' is alias as
defer tnorm
defer tconorm
defer rules
: and tnorm ;
: or tconorm ;
: if 1e ;
: is f@ ;
: then tnorm ;
: )r tnorm ;
: r( ;
: rt( 0e ;
: )rt or ;
fvariable output
100 value N/2 N/2 negate value -N/2
: s_z*f 0e -N/2 N/2 do i s>f 1e-1 f* output f! rules output f@ f* f+ -1 +loop ;
: s_f 0e -N/2 N/2 do i s>f 1e-1 f* output f! rules f+ -1 +loop ;
: (fuzzy_system) s_z*f s_f f/ ;

\ Here the code finishes.


Ahmed MELAHI

unread,
Sep 11, 2023, 12:40:41 PM9/11/23
to
Hi again,
Here is an example of creating a fuzzy system with 2 inputs and 1 output.

\ here the code begins


s" fuzzy.fs" included

\ example
\ fuzzy system with: 2 inputs x and y
\ 1 output z

fvariable x
fvariable y
' output alias z
fvariable zz \ to save output
\ print x, y and zz
: .xyz cr ." X = " x f@ f. cr ." Y = " y f@ f. cr ." Z = " zz f@ f. ;

\ using prod as tnorm and probabilistic or as tconorm, we can use fmin and fmax for tnorm and tconorm respectively
: tnorm1 f* ;
: tconorm1 fover fover f* f- f+ ; \ (u,v)---> u+v-u*v
' tnorm1 as tnorm
' tconorm1 as tconorm

\ here, we use gaussian membership functions for the three fuzzy variables,
\ we can use other functions: triangular, trapezoidal, ... Just define them
\ membership degree md = mf_gauss(x;m,s) = exp(-((x-m)/s)^2)
: mf_gauss ( f: x m s -- f: md ) frot frot f- fswap f/ fdup f* fnegate fexp ;

\ for input x
: nb1 -1e 0.2e mf_gauss ;
: ze1 0e 0.2e mf_gauss ;
: pb1 1e 0.2e mf_gauss ;

\ for input y
: nb2 -1e 0.2e mf_gauss ;
: ze2 0e 0.2e mf_gauss ;
: pb2 1e 0.2e mf_gauss ;

\ for output z
: nb3 -1e 0.2e mf_gauss ;
: ze3 0e 0.2e mf_gauss ;
: pb3 1e 0.2e mf_gauss ;


\ the fuzzy rules are given as below
: rules1
rt(
r( if x is nb1 and y is nb2 then z is nb3 )r or
r( if x is nb1 and y is ze2 then z is ze3 )r or
r( if x is nb1 and y is pb2 then z is ze3 )r or
r( if x is ze1 and y is ze2 then z is ze3 )r or
r( if x is ze1 and y is nb2 then z is ze3 )r or
r( if x is ze1 and y is pb2 then z is ze3 )r or
r( if x is pb1 and y is nb2 then z is ze3 )r or
r( if x is pb1 and y is ze2 then z is ze3 )r or
r( if x is pb1 and y is pb2 then z is pb3 )r
)rt
;

' rules1 as rules

\ the fuzzy sustem
: FS (fuzzy_system) zz f! ;


\ examples of results
0e x f! 0e y f!
FS .xyz \ print x y and the result which is in zz
cr

-1e x f! -1e y f!
FS .xyz
cr

1e x f! 1e y f!
FS .xyz
cr

\ here the code finishes

the timing using gforth :
\ mean time of execution
: timing_1000 utime 1000 0 do FS loop utime d>f d>f f- 1e-3 f* 1e-3 f* cr ." mean execution time: " f. ." ms. " ;

timing_1000

gives about 0.9 ms
note that the fuzzy system is FS

Bye

Marcel Hendrix

unread,
Sep 11, 2023, 2:13:45 PM9/11/23
to
On Monday, September 11, 2023 at 6:40:41 PM UTC+2, Ahmed MELAHI wrote:
[..]
> timing_1000
>
> gives about 0.9 ms
> note that the fuzzy system is FS

It takes 0.63ms in iForth, that is a bit unexpected...
I wonder what makes it so slow (or so fast for gForth)?

I can understand that you want to give the words the exact
names that you feel are right, but an overloaded IS, AND, OR,
IF, THEN, output ( not used, see zz ), and then tnorm1 and
tcnorm1 instead of and1 and or1 so that you could write
"r( if x is nb1 and y is nb2 then z is nb3 )r", was a bit *too*
cute for me :--)

-marcel

Marcel Hendrix

unread,
Sep 11, 2023, 2:20:21 PM9/11/23
to
On Monday, September 11, 2023 at 8:13:45 PM UTC+2, Marcel Hendrix wrote:
> On Monday, September 11, 2023 at 6:40:41 PM UTC+2, Ahmed MELAHI wrote:
> [..]

Sorry, I should have started by saying that this is a very interesting piece
of work.

-marcel

Ahmed MELAHI

unread,
Sep 11, 2023, 2:42:57 PM9/11/23
to
Hi,
Thanks,
I wrote this program a long ago, and when I saw this thread I wanted to share it with you all.
Here we can see that approximately one screen of code can program or create fuzzy systems.
I don't know if Mr. Bernd Paysan is interrested to add it to his one screen collection!!!
Concerning the overloading of the words " and, or, if,..." I've already eluded this by using french words like:
IF <--> SI,
THEN <--> ALORS
AND <--> ET
OR <--> OU
IS <--> EST
etc...

so that a fuzzy rule can be written like this:
r( SI x EST NB ET y EST NB ALORS z est NB)r
for example.
Concerning the speed: in gforth 0.9 ms and in iforth 0.63ms and 0.63 is less than 0.9 so iforth win.

Bye
0 new messages