MATLAB Answers

UDP Communication host computer and Arty Z7

3 views (last 30 days)
Hiago Cesar
Hiago Cesar on 19 Feb 2020
I am working on my Master Thesis project and I have faced a problem which I'm getting crazy with.
I have a model in Simulink and it works as it should. In this model, I have the controller which has 3 input values and 2 output (all of them double type).
I have a software implemented in the Arty Z7 board to calculate this controller.
I've saved the 3 inputs values in a data.mat which I got from the simulation in Simulink and sent them to the board through UDP communication via Matlab command (fwrite and fread). I compared the 2 values from this simulation and the 2 original values from the Simulink and it is very accurate, meaning the software implemented is correct.
I'd like to have this simulation in the board in the loop with the Simulink, for that I have implemented the UDP blocks receive, send and configure. And here is the problem.
The communication is not working properly... I can see in the board terminal that I'm receiving and sending some data however, I'm not getting any value in the simulink.
I'm also sometimes receiving the message 'unable to alloc pbuf in recv_handler' in the terminal, which never happened when using Matlab command.
The main part of the Matlab code is:
vectorSize = size(Vi,1);
%To prepare data for sending through the UDP interface - swap MSBs and LSBs
Vi(1: vectorSize) = swapbytes(Vi(1: vectorSize));
Vw(1: vectorSize) = swapbytes(Vw(1: vectorSize));
Vwref(1: vectorSize) = swapbytes(Vwref(1: vectorSize));
%To create UDP interface
udp_interface = udp('192.168.7.10', 7, 'LocalPort', 7);
fopen(udp_interface);
%To define a buffer for the data packet to be send through the UDP interface
dataToSend = zeros(1,3);
%To define a buffer for chip processed data
chipProcessedDataVi = single(zeros(2, vectorSize));
%To indicating start of the UDP interface communication:
disp('UDP communication has been started');
%To take time value just before starting the simulation
startTime = tic;
%To fill up the data packet to be send with values
for iteration = 1 : 1 : vectorSize
dataToSend(1) = Vi(iteration);
dataToSend(2) = Vw(iteration);
dataToSend(3) = Vwref(iteration);
fwrite(udp_interface, dataToSend, 'float');
chipProcessedDataVi(1:2,iteration) = fread(udp_interface, 2, 'float');
end
%To calculate and to print out time elapsed from starting to ending of sensor stimuli processing
elapsedTime = toc(startTime);
textMessage = sprintf('\nTime spent for processing all sensor stimuli samples: %.4f [sec]\n', elapsedTime);
disp(textMessage);
%To indicating end of the UDP interface communication:
disp('UDP communication has been stopped');
% To close UDP interface
fclose(udp_interface);
delete(udp_interface);
clear udp_interface;
%To swap MSBs and LSBs on the recived samples
chipProcessedDataVi(1: 2*vectorSize) = swapbytes(chipProcessedDataVi(1: 2*vectorSize));
The main part of the Simulink is:
How do I implement this in Simulink?
Thank you in advance!!!

Answers (0)

Community Treasure Hunt

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

Start Hunting!