Sending UDP data from my Simulink model in External Mode while bypassing the target hardware

9 次查看(过去 30 天)
Hi. My Simulink model needs to run on my NXP MIMXRT1024 target board. I am running in External Mode, so that I can log some sensor measurements I receive using ADC block of MBDT toolbox.
Everything runs well, until I try to add a UDP communication block to transmit data from my ADC to a Unity game that listens on port 8000 of my localhost (i.e. same PC machine running the simulation).
None of the UDP methods I tried worked: I tried all Simulink library "UDP send" blocks, and writing custom MyUDP.m code inside MATLAB functions, MATLAB Systems, and Level-1 S Function using udpport and dsp.UDPSender objects, as well as trying NXP's own TCP/UDP block (which I believe will only work if we physically connect the board with Ethernet cable to my PC).
All of these methods mentioned, caused error when Simulink's Embedded Coder tries to code generate.
My question I guess is, in External Mode simulation, how can I force the UDP part of my model to not code generate itself (i.e. runs from the board), and instead make it run using the MATLAB engine from my PC without compiling (as if I was executing a "UDP send" commad from MATLAB shell, which works btw)?
PS: In trying to create a MATLAB System block, the block generated (see figure below) proposes the choices: "Code generation" and "Interpreted execution", but no matter what I choose, it throws the same error, as I believe it always tries to code generate. So I wonder if there is any actual effect of "Interpreted execution" choice.
Code inside the MATLAB System block to give you an idea:
classdef MyUDPSender < matlab.System
properties (Nontunable)
ipAddress = '127.0.0.1';
port = 8000;
end
properties (Access = private)
udpObject
end
methods (Access = protected)
function setupImpl(obj)
obj.udpObject = dsp.UDPSender('RemoteIPAddress', obj.ipAddress, 'RemoteIPPort', obj.port);
end
function stepImpl(obj, data)
data = uint8(data);
step(obj.udpObject, data);
end
function releaseImpl(obj)
release(obj.udpObject);
end
function resetImpl(obj)
obj.releaseImpl();
obj.setupImpl();
end
function numInputs = getNumInputsImpl(obj)
numInputs = 1;
end
function numOutputs = getNumOutputsImpl(obj)
numOutputs = 0;
end
end
end
Am I missing something? any other suggestions? thanks a lot for helping me.

回答(1 个)

Adeline
Adeline 2023-9-14
Hi,
I understand you are trying to run a Simulink model with a UDP communication block in external mode.
The external mode simulation allows communication between the Simulink block diagram and the standalone program that is built from the generated code. Hence, when a model runs in external mode every block which is part of that model must support code generation.
For more information about the external mode, please refer to the following documentation link:
In the current model, the code generation for the custom “MATLAB System” block fails due to its limited code generation capabilities. To avoid this error, reprogram the block code by considering its code generation limitations or choose an UDP send block with required code generation properties. The “Extended Capabilities” section of the documentation page describes the code generation properties of the block.
For example: The “UDP send” block from the Embedded Coder library supports code generation without limitations. While the “UDP send” block from the DSP System toolbox supports code generation with a few limitations. Further, the “UDP send” blocks from the Instrumentation Control Toolbox and NXP support library do not support code generation.
For detailed information on the full list UDP send blocks available in Simulink, please refer to the following documentation link:
Hope this information was helpful.
  1 个评论
Youness Lahdili
Youness Lahdili 2023-9-17
Hi,
Thank you for the very informative answer an the two weblinks that I've just examined. I understand now that external mode simulation works only with blocks that can be code generated. Thank you for your suggestion using UDP from Embedded Coder library, but in fact I already tried that quite early on as part of my early attempts, but it threw an error too.
Actually Adeline, my problem is that I don't want the UDP to code generate itself and to run inside my NXP target board, but the opposite: I want it to run just from within the Simulink environment (i.e. only on the PC) without compiling into embedded code. So I recently learned that to make some blocks run only on PC while others run on target board, my model architecture needs be re-arranged in Processor-in-Loop layout.
PiL basically means it is a simulation (placed inside a Model Reference) running inside another simluation (which is the global simulation). Only the Model Reference block gets loaded on the target board, while everything outside it will run on PC only, as if it was running without any target hardware. So I placed a regular UDP block in that "outside" region of the global simulation, and linked its input to the Model Reference output port, which essentially represents the interface giving access to the target hardware's world.
The good news is that this PiL method worked, but unforturtunately it causes a lot of computational overheads and possible latency.
I wish Simulink had a way that treats UDP block just like visualization sinks, such as scope, disp, and figure which even when running in External Mode, do not get ported to the target hardware, but remain extrinstic to it and run just on the PC/Simulink.
If you still have any ideas that will work better than PiL based on my requirement above, I will appreciate again your valuable input. But in all cases, I am very grateful to you Adeline.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by