2023/2 Fuzzy Logic and Control

อาจารย์ผู้สอน/อาจารย์ที่ปรึกษา ผู้ช่วยศาสตราจารย์ ดร.กิตติพงษ์ เยาวาจา (Kittipong Yaovaja)
หัวหน้ากลุ่มวิจัยวิทยาการหุ่นยนต์และระบบอัตโนมัติขั้นสูง และผู้รับผิดชอบหลักสูตรหุ่นยนต์และระบบอัตโนมัติ (นานาชาติ)​ ม.เกษตรศาสตร์ วิทยาเขตศรีราชา คณะวิศวกรรมศาสตร์ศรีราชา


Suebsakul Kamlai

matlab simscape simulation with fuzzy logic control

Test the movement of the gripper joints.

gripper simulation with matlab program

test gripper with fuzzy logic

YAOWATTANANUKUL, Piyapat

Code1

function figNumber = fuzzy_animinit(namestr)
%FUZZY_ANIMINIT Initializes a figure for Simulink animations.
%

% Copyright 2023 The MathWorks, Inc.

if (nargin == 0)
namestr = ‘Simulink Animation’;
end

figNumber = findobj(‘Type’,’figure’,’Name’,namestr)’;

if isempty(figNumber)
% Now initialize the whole figure…
position=get(0,’DefaultFigurePosition’);
position(3:4)=[400 300];
figNumber=figure( …
‘Name’,namestr, …
‘WindowStyle’,’normal’, …
‘NumberTitle’,’off’, …
‘Position’,position, …
‘MenuBar’, ‘none’);
axes( …
‘Units’,’normalized’, …
‘Position’,[0.05 0.1 0.70 0.9], …
‘XTick’,[],’YTick’,[], …
‘Visible’,’off’);

%====================================
% Information for all buttons
bottom=0.05;
left=0.80;
btnWid=0.15;
btnHt=0.10;

%====================================
% The CONSOLE frame
frmBorder=0.02;
yPos=0.05-frmBorder;
frmPos=[left-frmBorder yPos btnWid+2frmBorder 0.9+2frmBorder];
h=uicontrol( …
‘Style’,’frame’, …
‘Units’,’normalized’, …
‘Position’,frmPos, …
‘BackgroundColor’,[0.5 0.5 0.5]); %#ok

%====================================
% The CLOSE button
labelStr=’Close’;
callbackStr=’close(gcf)’;
closeHndl=uicontrol( …
‘Style’,’pushbutton’, …
‘Units’,’normalized’, …
‘Position’,[left bottom btnWid btnHt], …
‘String’,labelStr, …
‘Callback’,callbackStr); %#ok
else
% bring figure to foreground
figure(figNumber)
end

Code2

function animtank(block)
%ANIMTANK Animation of water tank system.
%

% Copyright 2021-2023 The MathWorks, Inc.

setup(block)
end
%% Local functions
function setup(block)
%%
block.NumInputPorts = 1;
block.NumOutputPorts = 0;
block.NumDialogPrms = 1;
block.NumDworks = 0;

% Register as scope block (i.e. will be skipped in codegen, input data will
% be streamed to host during rapid accelerator mode or external mode)
block.SetSimViewingDevice(true);

initializeUI()

block.RegBlockMethod(‘Update’,@updateUI);
block.RegBlockMethod(‘Outputs’,@updateNextHit);
end
%% Local functions
function initializeUI()
%%
global tankdemo %#ok

% Initialize the figure for use with this simulation
fuzzy_animinit(‘Tank Demo’);
tankdemo = findobj(0,’Name’,’Tank Demo’);

tank1Wid=1;
tank1Ht=2;
tank1Init=0;
setPt=0.5;

tankX=[0 0 1 1]-0.5;
tankY=[1 0 0 1];
% Draw the tank
line(1.1tankXtank1Wid+1,tankYtank1Ht+0.95,’LineWidth’,2,’Color’,’black’); tankX=[0 1 1 0 0]-0.5; tankY=[0 0 1 1 0]; % Draw the water waterX=tankXtank1Wid+1;
waterY=tankYtank1Init+1; tank1Hndl=patch(waterX,waterY,’blue’,’EdgeColor’,’none’); % Draw the gray wall waterY([1 2 5])=tank1Ht[1 1 1]+1;
waterY([3 4])=tank1Init[1 1]+1; tank2Hndl=patch(waterX,waterY,[.9 .9 .9],’EdgeColor’,’none’); % Draw the set point lineHndl=line([0 0.4],setPt[1 1]+1,’Color’,’red’,’LineWidth’,4);

set(tankdemo, …
‘Color’,[.9 .9 .9], …
‘UserData’,[tank1Hndl tank2Hndl lineHndl]);
ax = tankdemo.Children(3);
set(ax, …
‘XLim’,[0 2],’YLim’,[0 3.5], …
‘XColor’,’black’,’YColor’,’black’, …
‘Box’,’on’);
axis equal
xlabel(‘Water Level Control’,’Color’,’black’,’FontSize’,10);
set(get(ax,’XLabel’),’Visible’,’on’)
end

function updateUI(block)
%%
global tankdemo %#ok

if any(get(0,’Children’)==tankdemo)
if strcmp(get(tankdemo,’Name’),’Tank Demo’)
u = block.InputPort(1).Data;
% Update tank one level
tankHndlList = get(tankdemo,’UserData’);
yData = get(tankHndlList(1),’YData’);
yOffset = yData(1);
yData(3:4) = [1 1]*u(2)+yOffset;
set(tankHndlList(1),’YData’,yData);

    yData = get(tankHndlList(2),'YData');
    yData([3 4]) = [1 1]*u(2)+yOffset;
    set(tankHndlList(2),'YData',yData);

    yData = [1 1]*u(1)+1;
    set(tankHndlList(3),'YData',yData);

    drawnow
end

end

end

function updateNextHit(block)
%%
% ns stores the number of samples
t = block.CurrentTime;
ts = block.DialogPrm(1).Data;
ns = t/ts; % block.CurrentTime

% This is the time of the next sample hit.
block.NextTimeHit = (1 + floor(ns + 1e-13(1+ns)))ts;
end

Code3

%% Water Level Control in a Tank
% This model shows how to implement a fuzzy inference system (FIS) in a
% Simulink(R) model.

% Copyright 1990-2012 The MathWorks, Inc.

%% Simulink Model
% This model controls the level of water in a tank using a fuzzy inference
% system implemented using a Fuzzy Logic Controller block. Open
% the |sltank| model.
open_system(‘sltank’)

%%
% For this system, you control the water that flows into the tank using a
% valve. The outflow rate depends on the diameter of the output pipe, which
% is constant, and the pressure in the tank, which varies with water level.
% Therefore, the system has nonlinear characteristics.

%% Fuzzy Inference System
% The fuzzy system is defined in a FIS object, |tank|, in the MATLAB(R)
% workspace. For more information on how to specify a FIS in a Fuzzy Logic
% Controller block, see .
%
% The two inputs to the fuzzy system are the water level error, |level|,
% and the rate of change of the water level, |rate|. Each input has three
% membership functions.
figure
plotmf(tank,’input’,1)
figure
plotmf(tank,’input’,2)

%%
% The output of the fuzzy system is the rate at which the control valve is
% opening or closing, |valve|, which has five membership functions.
plotmf(tank,’output’,1)

%%
% Due to the diameter of the outflow pipe, the water tank in this system
% empties more slowly than it fills up. To compensate for this imbalance,
% the |close_slow| and |open_slow| valve membership functions are not
% symmetrical. A PID controller does not support such asymmetry.

%%
% The fuzzy system has five rules. The first three rules adjust the valve
% based on only the water level error.
%
% * If the water level is okay, then do not adjust the valve.
% * If the water level is low, then open the valve quickly.
% * If the water level is high, then close the valve quickly.
%
% The other two rules adjust the valve based on the rate of change of the
% water level when the water level is near the setpoint.
%
% * If the water level is okay and increasing, then close the valve slowly.
% * If the water level is okay and decreasing, then open the valve slowly.
%
tank.Rules

%%
% In this model, you can also control the water level using a PID
% controller. To switch to the PID controller, set the const block to a
% value greater than or equal to zero.

%% Simulation
% The model simulates the controller with periodic changes in the setpoint
% of the water level. Run the simulation.
sim(‘sltank’,100)
open_system(‘sltank/Comparison’)

%%
% The water level tracks the setpoint well. You can adjust the performance
% of the controller by modifying the rules of the |tank| FIS. For example,
% if you remove the last two rules, which are analogous to a derivative
% control action, the controller performs poorly, with large oscillations
% in the water level.

อาจารย์ผู้สอน/อาจารย์ที่ปรึกษา ผู้ช่วยศาสตราจารย์ ดร.กิตติพงษ์ เยาวาจา (Kittipong Yaovaja)
หัวหน้ากลุ่มวิจัยวิทยาการหุ่นยนต์และระบบอัตโนมัติขั้นสูง และผู้รับผิดชอบหลักสูตรหุ่นยนต์และระบบอัตโนมัติ (นานาชาติ)​ ม.เกษตรศาสตร์ วิทยาเขตศรีราชา คณะวิศวกรรมศาสตร์ศรีราชา

Create Account



Log In Your Account