Clear Filters
Clear Filters

Problems to arrange incoming Data in array

1 view (last 30 days)
Simon Schirk
Simon Schirk on 19 Apr 2024
Answered: SAI SRUJAN on 1 May 2024
I am using Simulink on a Raspberry Pi and request Data from an Arduino via Serial Communication and Receive an 18x1 uint8 array. I want to split the Array into 6 different double variables and wirte them into an n x 5 array.
Since the Pi receives a continious stream of Data I have a * at the beginnig of my array to determine that there is data in that array. It looks like that:
MessageReceived = [42;49;54;56;55;49;51;48;52;51;53;49;48;48;48;49;56;56];
The MATLAB function in my Simulink model that handles the data has following code:
function [Fahrspur, SpurFreigabe] = fcn(MessageReceived)
persistent counter;
persistent NachrichtAusgelesenTmp;
persistent FahrspurBuffer;
if isempty(counter)
counter = 0;
NachrichtAusgelesenTmp = zeros(500,5);
FahrspurBuffer = zeros(500,5);
end
% Setup
Geschwindigkeit = 0;
Fahrspur = zeros(1,5);
% read and reconditioning of the data
MessageReceivedChar = char(transpose(MessageReceived));
MessageReceivedStr = string(MessageReceivedChar);
if MessageReceivedChar(1,1) == '*'
counter = counter + 1;
xKoordinate = real(str2double(string(MessageReceivedChar(2:6))))/100;
yKoordinate = real(str2double(string(MessageReceivedChar(7:11))))/100;
Fahrtrichtung = real(str2double(string(MessageReceivedChar(12))));
MaehwerkStatus = real(str2double(string(MessageReceivedChar(13))));
Radius_VZ = real(str2double(string(MessageReceivedChar(14))));
Radius = real(str2double(string(MessageReceivedChar(15:18))))/100;
% binary into values
if Fahrtrichtung == 1
Geschwindigkeit = 0.28;
elseif Fahrtrichtung == 0
Geschwindigkeit = -0.28;
end
% some change in units
VorsteuerungKappaBahn = 1/Radius;
NachrichtAusgelesenTmp(counter,:) = [xKoordinate yKoordinate MaehwerkStatus Geschwindigkeit VorsteuerungKappaBahn];
elseif MessageReceivedStr == "AlleDatenGesendet_" % Dataset completely receives
counter = 0;
idx = find(NachrichtAusgelesenTmp(:,1));
FahrspurBuffer = NachrichtAusgelesenTmp(1:idx(end),:);
Fahrspur = FahrspurBuffer;
SpurFreigabe = true;
end
The Var Fahrspur should be nx5 and containing [xKoordinate yKoordinate MaehwerkStatus Geschwindigkeit VorsteuerungKappaBahn] in each row. The Data in the mat-Log file has all the values for one Var (e.g. xKoordinate) written in a single collum and somewhat later the same happens with the next Var. It's always the first collum and I can't figure out why the data is not sorted like I want it to be. Attached there is an example from my mat-file-logging.

Answers (1)

SAI SRUJAN
SAI SRUJAN on 1 May 2024
Hi Simon,
I understand that you are facing an issue arranging the incoming data into an array.
Please go through the following code sample below that I utilized to verify the accuracy of the provided code sample.
function [Fahrspur, SpurFreigabe,NachrichtAusgelesenTm] = fcn(MessageReceived,NachrichtAusgelesenTmp)
persistent counter;
persistent FahrspurBuffer;
if isempty(counter)
counter = 0;
NachrichtAusgelesenTmp = zeros(5,5);
FahrspurBuffer = zeros(500,5);
end
% Setup
Geschwindigkeit = 0;
Fahrspur = zeros(1,5);
% read and reconditioning of the data
MessageReceivedChar = char(transpose(MessageReceived));
MessageReceivedStr = string(MessageReceivedChar);
if MessageReceivedChar(1,1) == '*'
counter = counter + 1;
xKoordinate = real(str2double(string(MessageReceivedChar(2:6))))/100;
yKoordinate = real(str2double(string(MessageReceivedChar(7:11))))/100;
Fahrtrichtung = real(str2double(string(MessageReceivedChar(12))));
MaehwerkStatus = real(str2double(string(MessageReceivedChar(13))));
Radius_VZ = real(str2double(string(MessageReceivedChar(14))));
Radius = real(str2double(string(MessageReceivedChar(15:18))))/100;
% binary into values
if Fahrtrichtung == 1
Geschwindigkeit = 0.28;
elseif Fahrtrichtung == 0
Geschwindigkeit = -0.28;
end
% some change in units
VorsteuerungKappaBahn = 1/Radius;
NachrichtAusgelesenTmp(counter,:) = [xKoordinate yKoordinate MaehwerkStatus Geschwindigkeit VorsteuerungKappaBahn];
counter
NachrichtAusgelesenTm=NachrichtAusgelesenTmp;
SpurFreigabe = false;
elseif MessageReceivedStr == "AlleDatenGesendet_" % Dataset completely receives
counter = 0;
idx = find(NachrichtAusgelesenTmp(:,1));
FahrspurBuffer = NachrichtAusgelesenTmp(1:idx(end),:);
Fahrspur = FahrspurBuffer;
SpurFreigabe = true;
NachrichtAusgelesenTm=NachrichtAusgelesenTmp;
end
end
MessageReceived = [42;49;54;56;55;49;51;48;52;51;53;49;48;48;48;49;56;56];
ProcessedMessagesArray = zeros(5,5);
for i = [1:5]
[a, b, ProcessedMessagesArray] = fcn(MessageReceived, ProcessedMessagesArray);
end
counter = 1
counter = 2
counter = 3
counter = 4
counter = 5
ProcessedMessagesArray
ProcessedMessagesArray = 5x5
168.7100 304.3500 0 0.2800 0.5319 168.7100 304.3500 0 0.2800 0.5319 168.7100 304.3500 0 0.2800 0.5319 168.7100 304.3500 0 0.2800 0.5319 168.7100 304.3500 0 0.2800 0.5319
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
save('myArrayFile.mat', 'ProcessedMessagesArray');
It is evident that the variable "ProcessedMessagesArray" is accurately calculated, and the 'counter' is appropriately updated and saved into the MAT file, "myArrayFile.mat". To address the issue you're facing, please ensure that the 'elseif' statement correctly identifies the end of the dataset and that the data is being correctly written to the '.mat' file.
I hope this helps!

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!