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

% Copyright 2002-2003 The MathWorks, Inc.

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

% Last Modified by GUIDE v2.5 22-Mar-2007 23:35:37

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

    % comunicação serie
        handles.com = 3;
        handles.H = 0;
        handles.scuid = 1;
        
    % posição
        handles.j1.pos = 0;
       
    % velocidade
        handles.j1.vel = 50;
        
    % controlo
        handles.j1.tipo = 0;
        handles.j1.ki = 0;
        handles.j1.kp = 0;
        handles.j1.kd = 0;
        
        
% Choose default command line output for ControlTest
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = ControlTest_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 slider movement.
function slider_ki_Callback(hObject, eventdata, handles)
% hObject    handle to slider_ki (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
val = round(get(hObject,'Value'));
error=1;
while(error),
    [rx, error] = applycontrol(handles.H, handles.scuid, 0, [val 0 0]);
end
set(handles.edit_ki, 'string', num2str(val));
handles.j1.ki = val;
guidata(hObject, handles);


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

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_ki_Callback(hObject, eventdata, handles)
% hObject    handle to edit_ki (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 edit_ki as text
%        str2double(get(hObject,'String')) returns contents of edit_ki as a double
val = round(str2double(get(handles.edit_ki,'String')));
% Determine whether val is a number between 0 and 1
if isnumeric(val) & length(val)==1 & ...
    val >= get(handles.slider_ki,'Min') & ...
    val <= get(handles.slider_ki,'Max')
    error=1;
    while(error)
        [rx, error] = applycontrol(handles.H, handles.scuid, 0, [val 0 0]);
    end
    set(handles.slider_ki,'Value',val);   
    handles.j1.ki=val;
else
% Increment the error count, and display it
    set(handles.edit_ki,'String',...
    ['valor inválido ']);
end
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function edit_ki_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_ki (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider_kp_Callback(hObject, eventdata, handles)
% hObject    handle to slider_kp (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
val = round(get(hObject,'Value'));
error=1;
while(error),
    [rx, error] = applycontrol(handles.H, handles.scuid, 1, [val 0 0]);
end
set(handles.edit_kp, 'string', num2str(val));
handles.j1.kp = val;
guidata(hObject, handles);


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

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_kp_Callback(hObject, eventdata, handles)
% hObject    handle to edit_kp (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 edit_kp as text
%        str2double(get(hObject,'String')) returns contents of edit_kp as a double
val = round(str2double(get(handles.edit_kp,'String')));
% Determine whether val is a number between 0 and 1
if isnumeric(val) & length(val)==1 & ...
    val >= get(handles.slider_kp,'Min') & ...
    val <= get(handles.slider_kp,'Max')
    error=1;
    while(error)
        [rx, error] = applycontrol(handles.H, handles.scuid, 1, [val 0 0]);
    end
    set(handles.slider_kp,'Value',val);   
    handles.j1.kp=val;
else
% Increment the error count, and display it
    set(handles.edit_kp,'String',...
    ['valor inválido ']);
end
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function edit_kp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_kp (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider_kd_Callback(hObject, eventdata, handles)
% hObject    handle to slider_kd (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
val = round(get(hObject,'Value'));
error=1;
while(error),
    [rx, error] = applycontrol(handles.H, handles.scuid, 2, [val 0 0]);
end
set(handles.edit_kd, 'string', num2str(val));
handles.j1.kd = val;
guidata(hObject, handles);


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

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_kd_Callback(hObject, eventdata, handles)
% hObject    handle to edit_kd (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 edit_kd as text
%        str2double(get(hObject,'String')) returns contents of edit_kd as a double
val = round(str2double(get(handles.edit_kd,'String')));
% Determine whether val is a number between 0 and 1
if isnumeric(val) & length(val)==1 & ...
    val >= get(handles.slider_kd,'Min') & ...
    val <= get(handles.slider_kd,'Max')
    error=1;
    while(error)
        [rx, error] = applycontrol(handles.H, handles.scuid, 2, [val 0 0]);
    end
    set(handles.slider_kd,'Value',val);   
    handles.j1.kd=val;
else
% Increment the error count, and display it
    set(handles.edit_kd,'String',...
    ['valor inválido ']);
end
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function edit_kd_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_kd (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_scuid_Callback(hObject, eventdata, handles)
% hObject    handle to edit_scuid (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 edit_scuid as text
%        str2double(get(hObject,'String')) returns contents of edit_scuid as a double


% --- Executes during object creation, after setting all properties.
function edit_scuid_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_scuid (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider_posicao_Callback(hObject, eventdata, handles)
% hObject    handle to slider_posicao (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
val = round(get(hObject,'Value'));
error=1;
while(error),
    [rx, error] = applyjoint(handles.H, handles.scuid, 0, [val 0 0]);
end
set(handles.edit_posicao, 'string', num2str(val));
handles.j1.pos = val;
guidata(hObject, handles);


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

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_posicao_Callback(hObject, eventdata, handles)
% hObject    handle to edit_posicao (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 edit_posicao as text
%        str2double(get(hObject,'String')) returns contents of edit_posicao as a double
val = round(str2double(get(handles.edit_posicao,'String')));
% Determine whether val is a number between 0 and 1
if isnumeric(val) & length(val)==1 & ...
    val >= get(handles.slider_posicao,'Min') & ...
    val <= get(handles.slider_posicao,'Max')
    error=1;
    while(error)
        [rx, error] = applyjoint(handles.H, handles.scuid, 0, [val 0 0]);
    end
    set(handles.slider_posicao,'Value',val);   
    handles.j1.pos=val;
else
% Increment the error count, and display it
    set(handles.edit_posicao,'String',...
    ['valor inválido ']);
end
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function edit_posicao_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_posicao (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider_velocidade_Callback(hObject, eventdata, handles)
% hObject    handle to slider_velocidade (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
val = round(get(hObject,'Value'));
error=1;
while(error),
    [rx, error] = applyjoint(handles.H, handles.scuid, 1, [val 0 0]);
end
set(handles.edit_velocidade, 'string', num2str(val));
handles.j1.vel = val;
guidata(hObject, handles);


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

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_velocidade_Callback(hObject, eventdata, handles)
% hObject    handle to edit_velocidade (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 edit_velocidade as text
%        str2double(get(hObject,'String')) returns contents of edit_velocidade as a double
val = round(str2double(get(handles.edit_velocidade,'String')));
% Determine whether val is a number between 0 and 1
if isnumeric(val) & length(val)==1 & ...
    val >= get(handles.slider_velocidade,'Min') & ...
    val <= get(handles.slider_velocidade,'Max')
    error=1;
    while(error)
        [rx, error] = applyjoint(handles.H, handles.scuid, 1, [val 0 0]);
    end
    set(handles.slider_velocidade,'Value',val);   
    handles.j1.vel=val;
else
% Increment the error count, and display it
    set(handles.edit_velocidade,'String',...
    ['valor inválido ']);
end
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function edit_velocidade_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_velocidade (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




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



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




% --------------------------------------------------------------------
function control_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to control (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
switch get(hObject,'Tag')
    case 'no_control'
        val = 0;
    case 'pid'
        val = 1;
    case 'proporcional'
        val = 2;     
    case 'jacobiano'
        val = 3;        
end
error=1;
while(error)
    [rx, error] = applycontrol(handles.H, handles.scuid, 3, [val 0 0]);
end
handles.j1.tipo = val;
guidata(hObject, handles);



% --------------------------------------------------------------------
function conect_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to conect (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
switch get(hObject,'Tag')
    case 'conect_on'
        val=initcom(handles.com, 115200);
        if(val<=0)
            set(hObject, 'Tag', 'conect_off');
        else
            handles.H = val;
        end
        
    case 'conect_off'
        killcom(handles.H);       
end
guidata(hObject, handles);


% --------------------------------------------------------------------
function pwm_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to pwm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
switch get(hObject,'Tag')
    case 'pwm_on'
        val=1;        
    case 'pwm_off'
        val=0;       
end
error=1;
while(error)
    [rx, error] = applyjoint(handles.H, handles.scuid, 3, [val 0 0]);
end
guidata(hObject, handles);



