matlab guide read from Arduino real Time

8 Ansichten (letzte 30 Tage)
Itziar Uzqueda
Itziar Uzqueda am 2 Mai 2017
Beantwortet: Leon Bouman am 12 Apr. 2019
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 Kommentar
Kevin Gleason
Kevin Gleason am 5 Mai 2017
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Leon Bouman
Leon Bouman am 12 Apr. 2019
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

Kategorien

Mehr zu Arduino Hardware finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by