


function varargout = aula3(varargin)
% AULA3 M-file for aula3.fig
%      AULA3, by itself, creates a new AULA3 or raises the existing
%      singleton*.
%
%      H = AULA3 returns the handle to a new AULA3 or the handle to
%      the existing singleton*.
%
%      AULA3('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in AULA3.M with the given input arguments.
%
%      AULA3('Property','Value',...) creates a new AULA3 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before aula3_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to aula3_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help aula3

% Last Modified by GUIDE v2.5 12-Sep-2011 15:04:01

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @aula3_OpeningFcn, ...
                   'gui_OutputFcn',  @aula3_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before aula3 is made visible.
function aula3_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to aula3 (see VARARGIN)

% Choose default command line output for aula3
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes aula3 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = aula3_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pbiniciar.
function pbiniciar_Callback(hObject, eventdata, handles)
% hObject    handle to pbiniciar (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%% Global Variables
global stop;
global daDfi daDf65;
global ItmLigaBBA1 ItmDesligaBBA1 ItmCalaSirene;
%% OPC Servers
daDfi=opcda('164.41.17.129','Smar.DfiOleServer.0');
daDf65=opcda('164.41.17.129','Smar.DF65Server.1');
%% Connecting to OPC Servers
connect(daDfi);
connect(daDf65);
%% Creating Groups
grp1 = addgroup(daDfi,'Analoga');
grp2 = addgroup(daDf65,'Discreta');
%% Add Items
ItmLit31Ai = additem(grp1,'LIT-31_AI1.PV.VALUE','single');
ItmFy31Ao = additem(grp1,'FY-31_AO1.OUT.VALUE','single');
ItmLigaBBA1 = additem(grp2,'PD3.A.000.01','int8');
ItmDesligaBBA1 = additem(grp2,'PD3.A.000.02','int8');
ItmCalaSirene = additem(grp2,'PD3.A.001.06','int8');
%% Init Variables
stop = 1;
ek_1 = 0;
ss = 0;
%% Close Loop
while stop    
    kp = get(handles.txtkp,'String');
    ki = get(handles.txtki,'String');
    kd = get(handles.txtkd,'String');
    sp = get(handles.txtsp,'String');
    kp = str2num(kp);
    ki = str2num(ki);
    kd = str2num(kd);
    sp = str2num(sp);
    
    lit31ai = read(ItmLit31Ai);
    fy31ao = read(ItmFy31Ao);
    abertura = fy31ao.Value;
    valordonivel = lit31ai.Value;
    QlyLit31Ai = lit31ai.Quality;
    if strcmp(QlyLit31Ai,'Good: Non-specific')
        set(handles.activex3,'PortionPercent',valordonivel);
    end
    
    set(handles.txtnivel,'String',valordonivel);
    set(handles.txtvalvula,'String',abertura);

    QlyFy32Ao = fy31ao.Quality;
    if  strcmp(QlyFy32Ao,'Good: Non-specific')
        ek = sp - valordonivel;  
        ss  = ss + ek;
        uk  = kp*ek + ki*ss + kd*(ek - ek_1);
        write(ItmFy31Ao,uk);
        set(handles.activex4,'VariableID',0);
        set(handles.activex4,'Y', sp);
        set(handles.activex4,'VariableID',1);
        set(handles.activex4,'Y', uk);
        set(handles.activex4,'VariableID',2);
        set(handles.activex4,'Y', valordonivel); 
        ek_1 = ek;
    end
    pause(1);
end

% --- Executes on button press in pbsair.
function pbsair_Callback(hObject, eventdata, handles)
% hObject    handle to pbsair (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global stop;
global daDfi daDf65;
stop = 0;
disconnect(daDfi);
disconnect(daDf65);

delete(daDfi);
delete(daDf65);

opcreset
close


% --- Executes on button press in pbliga.
function pbliga_Callback(hObject, eventdata, handles)
global ItmLigaBBA1 ItmDesligaBBA1
write(ItmDesligaBBA1,0);
pause(0.1);
write(ItmLigaBBA1,1);
set(handles.figbomba,'BackgroundColor', [0 1 0]);

% --- Executes on button press in pbdesliga.
function pbdesliga_Callback(hObject, eventdata, handles)
global ItmLigaBBA1 ItmDesligaBBA1
write(ItmLigaBBA1,0);
pause(0.1);
write(ItmDesligaBBA1,1);
set(handles.figbomba,'BackgroundColor', [0.5 0 0]);
         
function txtsp_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function txtsp_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtkp_Callback(hObject, eventdata, handles)
% hObject    handle to txtkp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtkp as text
%        str2double(get(hObject,'String')) returns contents of txtkp as a double


% --- Executes during object creation, after setting all properties.
function txtkp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtkp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtki_Callback(hObject, eventdata, handles)
% hObject    handle to txtki (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtki as text
%        str2double(get(hObject,'String')) returns contents of txtki as a double


% --- Executes during object creation, after setting all properties.
function txtki_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtki (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtkd_Callback(hObject, eventdata, handles)
% hObject    handle to txtkd (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtkd as text
%        str2double(get(hObject,'String')) returns contents of txtkd as a double


% --- Executes during object creation, after setting all properties.
function txtkd_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtkd (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in cmdCalaSirene.
function cmdCalaSirene_Callback(hObject, eventdata, handles)
% hObject    handle to cmdCalaSirene (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ItmCalaSirene
write(ItmCalaSirene,1);
pause(0.1)
write(ItmCalaSirene,0);
