How to plot real time data read over serial port

5 visualizzazioni (ultimi 30 giorni)
To start, I have looked extensively for details on this subject. I have learned a bit, but I'm not sure I fully understand.
I'm reading 6 bytes of data representing acceleration values from an accelerometer. 2 bytes represent x-axis acceleration, 2 bytes for y-axis, and 2 bytes for z-axis. For synchronization purposes, 255 in binary is sent after the last z-axis byte. This means the first x-axis data byte is the one following 255.
Here is my code:
serialportlist
FPGA = serialport("COM4", 115200);
FPGA.IsContinuous = 1;
configureCallback(FPGA,"255",@readSerialData);
x_data = zeros(1, 2);
y_data = zeros(1, 2);
z_data = zeros(1, 2);
x_bin_LSB = strings(1, 1);
y_bin_LSB = strings(1, 1);
z_bin_LSB = strings(1, 1);
x_bin_MSB = strings(1, 1);
y_bin_MSB = strings(1, 1);
z_bin_MSB = strings(1, 1);
xbinstr = strings(1, 1);
ybinstr = strings(1, 1);
zbinstr = strings(1, 1);
x_data_final = zeros(1, 1);
y_data_final = zeros(1, 1);
z_data_final = zeros(1, 1);
for x = 1:1:2
x_bin_LSB = dec2bin(x_data(1), 8);
x_bin_MSB = dec2bin(x_data(2), 8);
xbinstr = strcat(x_bin_MSB, x_bin_LSB);
if(bin2dec(xbinstr) >= 512)
x_data_final = 4 * (-512 + (bin2dec(xbinstr) - 512));
end
if (bin2dec(xbinstr) < 512)
x_data_final = 4 * bin2dec(xbinstr);
end
y_bin_LSB = dec2bin(y_data(1), 8);
y_bin_MSB = dec2bin(y_data(2), 8);
ybinstr = strcat(y_bin_MSB, y_bin_LSB);
if(bin2dec(ybinstr) >= 512)
y_data_final = 4 * (-512 + (bin2dec(ybinstr) - 512));
end
if(bin2dec(ybinstr) < 512)
y_data_final = 4 * bin2dec(ybinstr);
end
z_bin_LSB = dec2bin(z_data(1), 8);
z_bin_MSB = dec2bin(z_data(2), 8);
zbinstr = strcat(z_bin_MSB, z_bin_LSB);
if(bin2dec(zbinstr) >= 512)
z_data_final = 4 * (-512 + (bin2dec(zbinstr) - 512));
end
if (bin2dec(zbinstr) < 512)
z_data_final = 4 * bin2dec(zbinstr);
end
end
%This block was used before, but won't be used in my final product. I was
%able to store accurate data and plot it, but the plots weren't real-time.
%Thats the goal now.
x = x_data_final;
y = y_data_final;
z = z_data_final;
j = 1:numel(x);
k = 1:numel(y);
L = 1:numel(z);
subplot(1, 3, 1)
plot(j(1:50:end), x(1:50:end))
axis([1 3999 -2000 2000])
title 'X-Axis Acceleration'
xlabel 'Sample Number'
ylabel 'Acceleration (mG)'
subplot(1, 3, 2)
plot(k(1:50:end), y(1:50:end))
axis([1 3999 -2000 2000])
title 'Y-Axis Acceleration'
xlabel 'Sample Number'
ylabel 'Acceleration (mG)'
subplot(1, 3, 3)
plot(L(1:50:end), z(1:50:end))
axis([1 3999 -2000 2000])
title 'Z-Axis Acceleration'
xlabel 'Sample Number'
ylabel 'Acceleration (mG)'
%End unused block
for k = 1:length(x) %Here I intend to plot the saved acceleration data for one axis (all three, in the end)
y = sin(x(k));
addpoints(h,x(k),y);
drawnow
end
delete data;
delete start;
delete(FPGA);
function readSerialData(FPGA,~) %Callback function to write new data after every "255" read
data = read(FPGA, 6, "uint8");
for x = 1:1:6
x_data(1) = data(1);
x_data(2) = data(2);
y_data(1) = data(3);
y_data(2) = data(4);
z_data(1) = data(5);
z_data(2) = data(6);
end
clear data
end
My trouble now is with understanding callback functions and using "drawnow" to draw a realtime plot.
I'm imagining this will work as follows:
Matlab reads "255" from the serial port, triggering the callback function.
In the callback function, the appropriate bytes are saved to the appropriate arrays.
Since data is sent as a signed binary number, and 10 bits of the 16 are used, the two bytes in each array are converted to a signed integer value, which is multiplied by 4 to give the acceleration in mili-G's (just the way the accelerometer is set up).
The drawnow part will draw a plot, adding the read data (the two bytes, or one number) to the plot.
255 is read again, process repeats.
Thanks for any help. This project is 25 days in the making, and I'm very excited to finish it.
Please let me know if my method needs any tweaking, or if its incorrect altogether

Risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by