필터 지우기
필터 지우기

matlab guide read from Arduino real Time

조회 수: 4 (최근 30일)
Itziar Uzqueda
Itziar Uzqueda 2017년 5월 2일
답변: Leon Bouman 2019년 4월 12일
Hi!
I'm trying to use Matlab Guide to collect data from an Arduino UNO and sent over Bluetooth. I want the sampling time to be a fixed value(for example 60ms). I can receive data but not at that rate. The first 100 samples more or less go so quickly and the others are not responding in real time because the Arduino has an ultrasonic sensor and when I move sth closer, it responds a few seconds later.
The code and the Guide interface are the following:
% --- Executes on button press in Medir.
function Medir_Callback(hObject, eventdata, handles)
% hObject handle to Medir (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.Abrir,'Enable','off');
set(handles.Medir,'Enable','off');
set(handles.guardar,'Enable','off');
set(handles.pushbutton5,'Enable','off');
set(handles.stop,'Enable','on');
global stop
stop=false;
global stop2;
stop2=false;
cla reset; %Limpiar la grafica cada vez que se hace click
handles.h=animatedline('Marker','none','LineWidth',1,'Color','b');
guidata(hObject,handles);
xlabel('Number of samples','FontSize',16,'FontWeight','bold','Color','w');
ylabel('Distance (cm)','FontSize',16,'FontWeight','bold','Color','w');
set(gca, 'XColor', [1 1 1]);
set(gca, 'YColor', [1 1 1]);
i=1;
while (stop==false) && (stop2==false)
handles.start=tic;
handles.distancia(i)=fscanf(handles.obj1,'%d');
addpoints(handles.h,i,handles.distancia(i));
drawnow;
handles.tiempo2(i)= toc(handles.start);
handles.retardo(i) = handles.interval-handles.tiempo2(i);
pause(handles.retardo(i));
i=i+1;
%handles.interval is 0.06sec in this case
end
if(stop==true)
stop=false;
set(handles.guardar,'Enable','on');
set(handles.Abrir,'Enable','on');
set(handles.Medir,'Enable','on');
guidata(hObject,handles);
cerrar_puerto(handles.obj1);
end
if(stop2==true)
stop2=false;
cerrar_puerto(handles.obj1);
guidata(hObject,handles);
delete(gcf);
end
  댓글 수: 1
Kevin Gleason
Kevin Gleason 2017년 5월 5일
The issue might be that you are reading only 1 integer at a time in fscanf. There may be a backup of values before you are reading the "close to sensor" values. You can try to clear the buffer on each read, maybe take a mean of the values recorded over the last second:
Instead of "handles.distancia(i)=fscanf(handles.obj1,'%d');" Try:
vals=double(fscanf(handles.obj1,'%c'));
handles.distancia(i)=mean(vals);
"vals" will be an array of all the values waiting in the data buffer, which will clear it for the next reading, depending on how frequently the arduino is sending data.

댓글을 달려면 로그인하십시오.

답변 (1개)

Leon Bouman
Leon Bouman 2019년 4월 12일
Hello Itziar Uzqueda, i am looking trough your posts and i am seeing that you had a similar kind of project that i am doing now. I want to readout data from a Arduino with Matlab using a HC-05 Bluetooth module. Would you be so kind and send me your Matlab scripts and Simulink models of this project so that i can use them as an example for my project. If you want, you can send them to: "leon_bouman_9@live.nl".
Thank you!!
Léon

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by