function varargout = aula3TESTE(varargin)
% AULA3TESTE M-file for aula3TESTE.fig
%      AULA3TESTE, by itself, creates a new AULA3TESTE or raises the existing
%      singleton*.
%
%      H = AULA3TESTE returns the handle to a new AULA3TESTE or the handle to
%      the existing singleton*.
%
%      AULA3TESTE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in AULA3TESTE.M with the given input arguments.
%
%      AULA3TESTE('Property','Value',...) creates a new AULA3TESTE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before aula3TESTE_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to aula3TESTE_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 aula3TESTE

% Last Modified by GUIDE v2.5 13-Dec-2011 14:03:28

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @aula3TESTE_OpeningFcn, ...
                   'gui_OutputFcn',  @aula3TESTE_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 aula3TESTE is made visible.
function aula3TESTE_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 aula3TESTE (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = aula3TESTE_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 ItmLigaBBA2 ItmDesligaBBA2 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
ItmTit32Ai = additem(grp1,'TIT-32_AI1.PV.VALUE','single');
ItmFy32Ao = additem(grp1,'FY-32_PID1.OUT.VALUE','single');
ItmLigaBBA1 = additem(grp2,'PD3.A.000.01','int8');
ItmDesligaBBA1 = additem(grp2,'PD3.A.000.02','int8');
ItmLigaBBA2 = additem(grp2,'PD3.A.000.03','int8');
ItmDesligaBBA2 = additem(grp2,'PD3.A.000.04','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);
    
    tit32ai = read(ItmTit32Ai);
    fy32ao = read(ItmFy32Ao);
    abertura = fy32ao.Value;
    valortemperatura = tit32ai.Value;
    QlyTit31Ai = tit32ai.Quality;
    
    
    set(handles.txtnivel,'String',valortemperatura);
    set(handles.txtvalvula,'String',abertura);

    QlyFy32Ao = fy32ao.Quality;
    if  strcmp(QlyFy32Ao,'Good: Non-specific')
        ek = sp - valortemperatura;  
        ss  = ss + ek;
        uk  = kp*ek + ki*ss + kd*(ek - ek_1);
        uk  = -uk
        write(ItmFy32Ao,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', valortemperatura); 
        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]);
         
% --- Executes on button press in pbliga2.
function pbliga_Callback(hObject, eventdata, handles)
global ItmLigaBBA2 ItmDesligaBBA2
write(ItmDesligaBBA2,0);
pause(0.1);
write(ItmLigaBBA2,1);
set(handles.figbomba2,'BackgroundColor', [0 1 0]);

% --- Executes on button press in pbdesliga2.
function pbdesliga_Callback(hObject, eventdata, handles)
global ItmLigaBBA2 ItmDesligaBBA2
write(ItmLigaBBA2,0);
pause(0.1);
write(ItmDesligaBBA2,1);
set(handles.figbomba2,'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);

% --- Executes on button press in pbdesliga2.
function pbdesliga1_Callback(hObject, eventdata, handles)
% hObject    handle to pbdesliga2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pbliga2.
function pbliga1_Callback(hObject, eventdata, handles)
% hObject    handle to pbliga2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



% --- Executes on button press in pbdesliga2.
function pbdesliga2_Callback(hObject, eventdata, handles)
% hObject    handle to pbdesliga2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pbliga2.
function pbliga2_Callback(hObject, eventdata, handles)
% hObject    handle to pbliga2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
