Application Center - Maplesoft

App Preview:

Fuzzy controler for reversed pendulum

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

fuzzy.mws

Fuzzy Controler for Reversed Pendulum

Dr.Laczik Blint laczik@goliat.eik.bme.hu

> restart; Digits := 4; with(plots); with(plottools)

Warning, the name changecoords has been redefined

Warning, the name arrow has been redefined

Introduction
Basics of fuzzy system

Mechanical princips

Work of the controller

Animation active membership functions of the force

Displaying of the control process

Introduction

Given an initial state for an unstable nonlinear mechanical system of the reversed pendulum. Our purpose is to find a sequence of forces to adjust the system to be in a stable state by inference rules of fuzzy logic .

The differential equation system of the reversed pendulum are derived from Lagrange's formula. From a given non-equilibrical original state by N step we calculate the first linear/angular position and linear/angular velocity of cart and pole. By method of fuzzy logic we first estimate the desired force Q to approximate stabile position of pole.In the following step we observe the movement of the system from the calculated initial conditions and by the force Q.

In fuzzy logic, a fuzzy set A on universe X is defined by the ordered pair (x, mu[A](x) ), where x is the object on X, mu[A](x) is called the membership function of A.The membership function can be any value in the range of [0,1].

The expression of and rules in the fuzzy logic are the following: Assume the form x is A and y is B. This rule can be defined as a fuzzy set R on the product space X x Y with the associated membership function being

mu[R](x,y) := mu[AxB](x,y) = min(mu[A](x),mu[B](y))...

By the all-used symetrical triangular membership functions are for the given value "k" section

k := mu(y[1]) = mu(y[2])

and

[Maple OLE 2.0 Object]

In different case we must calculate the value of locus of center gravity.

In order to get the value of the objects simultane rules, we need to defuzzify theis membership function. The most frequently used method is the "center of gravity".

[Maple OLE 2.0 Object]

The flow-charte of the control system is shown in the next figure.

[Maple Bitmap]

The input is the state variables of the cart-pole system are y(t), diff(y(t),t) , phi(t) , diff(phi(t),t) , and the output is the desired force Q to be applied. We define 7-7 fuzzy sets for phi(t) , diff(phi(t),t) and Q which are negative/positive large (NL/PL), negative/positive middle (NM/PM), negative/positive small (NS,PS) and zero (Z). Rows of the table of rules "rulm" represent the angular velocity, columns the angular position of pole.

go to back

Basics of fuzzy system

For example: if the angular velocity of pole is NS (row index = 3) and the angular position is Z (column index = 4) then the force Q is PS.

> SETMEMBER := {NL = 1, NM = 2, NS = 3, Z = 4, PS = 5...

> rulm := matrix([[0, 0, PS, PL, 0, 0, 0], [0, 0, 0, ...

The membership functions of fuzzy controler are the follows:

> kappa || 1 := [-3*Pi/8, -Pi/4, -Pi/8, 0, Pi/8]; Kap...

> theta || 1 := [-3*Pi/8, -Pi/4, -Pi/8, 0, Pi/8, Pi/4...

> f || 1 := [-10, -7.5, -5, -2.5, 0, 2.5, 5]; F || 1 ...
f || 1 := [-10, -7.5, -5, -2.5, 0, 2.5, 5]; F || 1 ...
f || 1 := [-10, -7.5, -5, -2.5, 0, 2.5, 5]; F || 1 ...
f || 1 := [-10, -7.5, -5, -2.5, 0, 2.5, 5]; F || 1 ...

> A || 1 := piecewise(x < kappa || 1[1],1,x < kappa |...

> for i to 7 do C || i := piecewise(x < f || 1[i],0,x...

> B || 1 := piecewise(x < theta || 1[1],1,x < theta |...

> for i to 5 do A || (i+1) := piecewise(x < kappa || ...
for i to 5 do A || (i+1) := piecewise(x < kappa || ...
for i to 5 do A || (i+1) := piecewise(x < kappa || ...
for i to 5 do A || (i+1) := piecewise(x < kappa || ...

Displaying the membership functions:

> plot({seq(A || i,i = 1 .. 7)},x = -Pi/2 .. Pi/2,tit...
plot({seq(A || i,i = 1 .. 7)},x = -Pi/2 .. Pi/2,tit...
plot({seq(A || i,i = 1 .. 7)},x = -Pi/2 .. Pi/2,tit...

[Maple Plot]

[Maple Plot]

[Maple Plot]

go to back

Mechanical princips

Let the kinetical energy of the system be E, the potential energy U, and the dissipation (D) in the system 0. The generalized variables and their derivates are

y(t) position of the cart

y||1= diff(y(t),t) velocity of the cart

phi(t) angle between the pole and vertical position

phi || 1 = diff(phi(t),t) angular velocity of the pole.

The differential equation of system by the general Lagrange`s formula is

[Maple OLE 2.0 Object]

where q[i] is the i'th generalized variable of system, Q[i] is the i'th generalized force applied to the system. The basic equations by M = mass of cart, m = mass of pole, l = length of pole and g = constant of gravity are

> E := simplify(expand(1/2*(M*y || 1^2+m*((y || 1+l*p...

E := 1/2*M*y1^2+1/2*m*y1^2+m*l*phi1*cos(phi)*y1+1/2...

U := m*g*l*cos(phi)

Expand the derivations by Lagrange's formula

> E || 0 := -diff(E,phi); E || 1 := diff(E,phi || 1);...
E || 0 := -diff(E,phi); E || 1 := diff(E,phi || 1);...
E || 0 := -diff(E,phi); E || 1 := diff(E,phi || 1);...
E || 0 := -diff(E,phi); E || 1 := diff(E,phi || 1);...

E1 := m*l*cos(phi)*y1+m*l^2*phi1

E2 := m*l*cos(phi(t))*diff(y(t),t)+m*l^2*diff(phi(t...

E3 := m*l*cos(phi(t))*diff(y(t),`$`(t,2))+m*l^2*dif...

> E || 4 := diff(E,y || 1); E || 5 := subs({y || 1 = ...

E4 := M*y1+m*y1+m*l*phi1*cos(phi)

E5 := M*diff(y(t),t)+m*diff(y(t),t)+m*l*diff(phi(t)...

E6 := M*diff(y(t),`$`(t,2))+m*diff(y(t),`$`(t,2))+m...

E7 := -m*g*l*sin(phi)

E8 := -m*g*l*sin(phi(t))

> SYS := {simplify(E || 3+E || 8)/(m*l) = 0, collect(...

SYS := {cos(phi(t))*diff(y(t),`$`(t,2))+l*diff(phi(...

The number of cicles ("ciklusmax"), the initial conditions (xe, xv, alpha , beta ), mechanical parameters ("parameter") and the number "N" of stpes by RKF45 method:

> ciklusmax := 50; q := 0; xe := 0; xv := .25; alpha ...

The actual formula of the differential equation system of the reversed pendulum is:

> var := {y(t), phi(t)}; val := array(1 .. N,[]); for...

sys1 := {cos(phi(t))*diff(y(t),`$`(t,2))+diff(phi(t...

go to back

Work of the controller

By the working of the controller the program writes the number of observations step, the values of calculated force, the angular position, the angular velociy, the linear position and linear velocity by fuzzy logic calculation.

> for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...
for ciklus to ciklusmax do print(`*************`); ...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

1, -3.885, .83835562471358e-1, -.62087250044113, .5...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

2, 3.112, .50700310496704e-1, .47796320236006, .227...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

3, -.9253, .37046478817593e-1, -.29343842955510, -....

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

4, 1.622, .69241277201551e-1, .58685433419697e-1, -...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

5, -.4692, .61469074835230e-1, -.26848147752102, -....

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

6, 1.127, .79839511337064e-1, -.18555876997611e-1, ...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

7, .1765, .91268118975541e-1, -.17313133980247, -1....

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

8, -.1412e-1, .55933947871845e-1, -.12593301004928,...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

9, -.1470e-1, .31604932361302e-1, -.68276819911976e...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

10, -.2135, .10560466478384e-1, -.44337807379177e-1...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

11, -.9966, -.44060157915869e-1, -.47995893012444e-...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

12, 1.204, -.49685787112049e-2, .20022072017402, -3...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

13, -.4439, .17120295373763e-1, -.95869889478298e-1...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

14, 1.069, .24725988894956e-1, .58094559505939e-1, ...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

15, -.7884, .21769238668988e-1, -.18579656537427, -...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

16, .6095, .77951114235501e-2, .38938781667216e-1, ...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

17, -.8810, .60683044619146e-3, -.11703466654666, -...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

18, -.9958e-1, -.53216139034842e-1, .40483542996612...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

19, .4869, -.40674769709729e-2, .56624975907668e-1,...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

20, -.8639, -.11155819627228e-1, -.86197488415867e-...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

21, .4426, -.31632705642274e-1, .94607934224283e-1,...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

22, .6478, .23338500216207e-1, .76780444621561e-2, ...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

23, -1.147, -.45364237720745e-2, -.16251693345385, ...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

24, 1.721, .37437146642074e-1, .19154803810580, -6....

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

25, .1154, .76469641136512e-1, -.15027175496681, -6...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

26, .5025, .70081455030040e-1, -.70025366295488e-1,...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

27, -.9620, .10195342256916e-1, -.17678429451785, -...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

28, .3865, -.14873407799253e-1, .59531353984606e-1,...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

29, -.8883, -.20518987321439e-1, -.71181896917293e-...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

30, 1.380, .13383135638389e-1, .17708946071993, -6....

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

31, -1.546, -.28207175345862e-1, -.22421299676809, ...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

32, .9512, -.13955244907865e-1, .16442473295071, -7...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

33, -1.056, -.17738982200694e-1, -.11334293358079, ...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

34, 1.405, .13924706788901e-1, .18139836705189, -7....

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

35, -1.520, -.24825887422765e-1, -.22190721439609, ...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

36, .4599e-2, -.74326642604764e-1, .75121349714801e...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

37, -.6641, -.55556052623746e-1, -.91092061513980e-...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

38, .1627, -.56167971505806e-1, .80851623420125e-1,...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

39, -1.253, -.73182656198654e-1, -.67877918196513e-...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

40, .1098e-1, -.10450602737531, .10668508961406, -9...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

41, -.9293, -.82625703548815e-1, -.19031528889872e-...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

42, .8874, -.26347798257032e-1, .18007519352584, -1...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

43, .3586, .27866768818390e-1, -.19635872716040e-1,...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

44, .5772, .52378007933794e-1, -.35696356595708e-1,...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

45, -.4614, .28991256581943e-1, -.13796419916301, -...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

46, 1.718, .70480491422546e-1, .86748268512154e-1, ...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

47, -1.052, .32904524873168e-1, -.30725040599642, -...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

48, -1.057, -.39111023753256e-1, -.71267100476375e-...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

49, .1848, -.70528973212635e-1, .10240753866935, -1...

`*************`

Warning, the 'value' option is deprecated, use 'output' instead

50, -.7268, -.47221162812432e-1, -.17883825558840e-...

go to back

Animation of the motion of the reversed pendulum

The animation displayed the controled system.

> T := NULL; for s from q-1 by -1 to 1 do Q := displa...

[Maple Plot]

go to back

Displaying of the control process

Remark: between values of 0 <= nn < ne <= q - 1 can be displaying any part of control process.

> nn:=0:ne:=q-1:

> pic1 := pointplot([seq([n, v1[n]],n = nn .. ne)],co...
pic1 := pointplot([seq([n, v1[n]],n = nn .. ne)],co...
pic1 := pointplot([seq([n, v1[n]],n = nn .. ne)],co...
pic1 := pointplot([seq([n, v1[n]],n = nn .. ne)],co...

[Maple Plot]

go to back

>

>