0% found this document useful (0 votes)
29 views4 pages

Codigo Matlab Function ... ... ... ... ... If End If

This MATLAB code defines functions for a graphical user interface (GUI) that collects and filters sensor data from a serial port. It initializes handles and timers for reading serial data, filtering the readings with a filter defined by coefficients, and plotting the raw and filtered data over time. Buttons in the GUI allow starting/stopping the data collection and closing the serial connection.

Uploaded by

dilecon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

Codigo Matlab Function ... ... ... ... ... If End If

This MATLAB code defines functions for a graphical user interface (GUI) that collects and filters sensor data from a serial port. It initializes handles and timers for reading serial data, filtering the readings with a filter defined by coefficients, and plotting the raw and filtered data over time. Buttons in the GUI allow starting/stopping the data collection and closing the serial connection.

Uploaded by

dilecon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CODIGO MATLAB

function varargout = cidigo(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @OutputFcn, ...
'gui_OutputFcn', @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

function OpeningFcn(hObject, eventdata, handles,


varargin)

handles.output = hObject;
instrreset

COM_Port = getAvailableComPort();

handles.timer = timer('Period',0.1,
...
'StartDelay',0.5,
...
'TasksToExecute',inf,
...
'ExecutionMode','fixedSpacing',
...
'TimerFcn',
{@timerCallback,hObject});

handles.timer2 = timer('Period',0.1,
...
'StartDelay',0.5,
...
'TasksToExecute',inf,
...
'ExecutionMode','fixedSpacing',
...
'TimerFcn',
{@timerCallback2,hObject});
handles.h=[0.1521223096425, 0.1693575297271,
0.1785201606304, 0.1785201606304, 0.1693575297271,
0.1521223096425];
handles.N=length(handles.h);
handles.x1=zeros(1,handles.N);

handles.xn2=0;
handles.xn1=0;
handles.yn2=0;
handles.yn1=0;

handles.x= zeros(1,100);
handles.xf= zeros(1,100);
handles.t= 0:0.2:19.8;
handles.in = 0;
handles.y = 0;
set(handles.popupmenu1,'string', COM_Port)
ylabel(handles.axes1,'Distancia (Cm)')
xlabel(handles.axes2,'Tiempo (s)')
ylabel(handles.axes2,'Distancia (Cm)')

guidata(hObject,handles);

function timerCallback(~,~,hObject)
handles = guidata(hObject);
if isfield(handles,'s')
readasync(handles.s);
n = handles.s.BytesAvailable;
if n > 0
handles.in = fscanf(handles.s,'%f');

for k=1:handles.N-1
handles.x1(handles.N-
k+1)=handles.x1(handles.N-k);
end
handles.x1(1)=handles.in;
y_f=filter(handles.h,1,handles.x1);
handles.y = y_f(end);
set(handles.text2,'string',
handles.in)
set(handles.text4,'string',
handles.y)
disp(handles.in);
guidata(hObject,handles);
end
end

function timerCallback2(~,~,hObject)
handles = guidata(hObject);
handles.x = [handles.x(2:end), handles.in];
handles.xf = [handles.xf(2:end), handles.y];
handles.t = handles.t + 0.2;
guidata(hObject,handles);
hold(handles.axes1, 'on' )

plot(handles.axes2,handles.t,handles.xf,'g','LineWidth',
2)

plot(handles.axes1,handles.t,handles.x,'m','LineWidth',2
)
xlabel(handles.axes2,'Tiempo (s)')
ylabel(handles.axes2,'Dist (Cm)')
ylabel(handles.axes1,'Dist Filt(Cm)')
hold(handles.axes1, 'off' )
axis(handles.axes1,[handles.t(1) handles.t(end) 0
30])
axis(handles.axes2,[handles.t(1) handles.t(end) 0
30])

function varargout = OutputFcn(hObject, eventdata,


handles)

varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata,
handles)
stop(handles.timer);
stop(handles.timer2);

function pushbutton2_Callback(hObject, eventdata,


handles)
try
start(handles.timer);
start(handles.timer2);
end

function popupmenu1_Callback(hObject, eventdata,


handles)

function popupmenu1_CreateFcn(hObject, eventdata,


handles)

if ispc && isequal(get(hObject,'BackgroundColor'),


get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function pushbutton3_Callback(hObject, eventdata,


handles)

idx = get(handles.popupmenu1,'Value');
items = get(handles.popupmenu1,'String');
selectedItem = items{idx};
s =
serial(selectedItem,'BaudRate',9600,'Terminator','CR/LF'
);
warning('off','MATLAB:serial:fscanf:unsuccessfulRead');
fopen(s);
handles.s = s;
set(handles.pushbutton3,'Enable','off')
guidata(hObject,handles);

function pushbutton4_Callback(hObject, eventdata,


handles)

closereq();

You might also like