Plotting of a real-time signal acquired via USB @10 Hz; is Matlab fast enough?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Alessandro Russo
el 18 de Mayo de 2016
Comentada: Ramon Martinez Cancino
el 28 de Nov. de 2023
Hi everyone! I recently opened a topic about this, i resume briefly the situation:
i have a bluetooth development board that receives values from a sensor, every 100 ms (that is to say 10 Hz, temporized by a timer counter of the source of the signal); as soon as it receives a value, it sends it to the PC via usb. In Matlab, i developed a GUI with a plot in it that sets the parameters for the function "serial", that is for example to stay opened for 10 second and receive a buffer of 8 bits (which is the dimension of the single transmitted datum). After this, in a while loop i plot the value and just update the time and samples arrays.
The problem is that this small cycle (code below) seems to be too slow, so i sometimes lose values! I used "tic" and "tac" to see how long does it take to complete a cycle, and sometimes it takes 500 ms, so i lose 4 values! Also removing the plot an just using "fread" as instruction in the cycle, the situation doesn't improve, it seems that the "slowing" instruction is indeed fread (by removing it, the cycle takes around 10-20 ms to complete). How can i solve this and get correctly all the values? Please note that while running this program anything else on the PC is opened, so it must be at full performance.
As said i have two versione of the code, one with the while and one with a timer counter that fires every 100 ms, but the problem is the same. Here is the while version, just the GUI function that starts the acquiring and plotting!
% --- Executes on button press in startvis.
function startvis_Callback(hObject, eventdata, handles)
% hObject handle to startvis (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
hgui=getappdata(0,'hgui');
setappdata(hgui,'startv',1);
axes(handles.figvis)
priorPorts = instrfind;
delete(priorPorts);
handles.s=serial ('COM3');
set(handles.s,'BaudRate', 38400, 'DataBits', 8, 'Parity', 'none','StopBits', 1, 'FlowControl', 'none');
set(handles.s, 'InputBufferSize', 4);
set(handles.s, 'Timeout',100);
set(handles.s, 'Terminator', 'CR/LF');
fopen(handles.s);
array=[];
T=0;
i=0;
guidata(hObject, handles)
while (getappdata(hgui,'startv')==1)
tic
i=i+1;
a=fread(handles.s);
array=[array a(1)*3/255];
if i>1
T=[T T(end)+0.1];
end
if i>100
i=0;
array=a(1)*3/255;
end
plot(array),grid on,xlabel('Sample'),ylabel('Voltage [V]')
axis([0,100,0,3.5])
pause(0.00000000001);
toc
end
fclose(h);
1 comentario
Respuestas (0)
Ver también
Categorías
Más información sobre Graphics Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!