How to use user defined class in simulink?

6 次查看(过去 30 天)
Hello everyone.
I have written a program in matlab(.m) that can read the sensor data. I hope to use these data in simulink(.slx).
I've defined a class ‘sensors’ that takes a long time to initialize. I hope that constructor function of 'sensors' will only be executed once when simulink start, and 'sensors.send()','sensors.read()' can be executed in each cycle of simulink.
I have tried 'Matlab Function' in simulink, error information is as follows:
Function 'udpport' not supported for code generation.
I don't really care if the simulink model could generates C/C++ code. What do I need to do to execute the constructor only once and still call the member function 'sensors.send()','sensors.read()' of class ‘sensors’ ?
My program is shown below.
Thank you for your help.
classdef ATI_sensor
%UNTITLED Summary of this class goes here
% Detailed explanation goes here
properties
computer_IP_address,
computer_IP_port
ATI_IP_address,
ATI_IP_port,
UDP_communicate,
cofficient = 1e7,
header = '123400020000000A'
end
methods
function obj = ATI_sensor(PC_IP_address,PC_IP_port,ATI_IP,ATI_port)
%UNTITLED Construct an instance of this class
% Detailed explanation goes here
obj.computer_IP_address = PC_IP_address;
obj.computer_IP_port = PC_IP_port;
obj.ATI_IP_address = ATI_IP;
obj.ATI_IP_port = ATI_port;
obj.UDP_communicate = udpport('byte','LocalHost',obj.computer_IP_address,'LocalPort',obj.computer_IP_port,'EnablePortSharing',true,'ByteOrder','big-endian');
obj.UDP_communicate.Timeout = 5;
obj.UDP_communicate.EnableBroadcast = true;
end
function [] = send(obj)
D = sscanf(obj.header,'%2x');
flush(obj.UDP_communicate,"output");
write(obj.UDP_communicate,D,"uint8",obj.ATI_IP_address,obj.ATI_IP_port);
end
function [data] = read(obj)
obj.send();
rawdata = read(obj.UDP_communicate,10,'int32');
data = rawdata(4:9)/obj.cofficient;
flush(obj.UDP_communicate,"output");
end
function [mean_data] = avg_read(obj,n)
data_all = [];
for i = 1:n
obj.send();
rawdata = read(obj.UDP_communicate,10,'int32');
data = rawdata(4:9);
data_all = [data_all;data];
flush(obj.UDP_communicate,"output");
end
mean_data = mean(data_all,1)/obj.cofficient;
end
function [] = stop_sensor(obj)
flush(obj.UDP_communicate,"output")
flush(obj.UDP_communicate,'input')
clear obj;
end
end
end

采纳的回答

Sarthak
Sarthak 2023-5-15
Hi lingxiao,
As per my understand, you can use a MATLAB System Block to write your own custom class for your Simulink Model and it would behave as expected.
  • Add a MATLAB System Block to your model
  • Double-click the block to open block parameters.
  • Attach your custom class or create a new system object
Attaching a documentation link for your reference
  1 个评论
lingxiao li
lingxiao li 2023-5-31
Hello Sarthak,
Thanks for your response.
I tried to use MATLAB System Block in Simulink , but the system still reported errors.The error information is as follows:
Simulink detected an error 'Function 'udpport' not supported for code generation.'. The error occurred for MATLAB System block 'untitled/MATLAB System'. See line 30, column 31 in file '.m'. The error was detected during size propagation phase. Start code generation report.
To prevent this error, use one of the following: * Modify the System object to avoid code that does not support code generation. * Implement propagation methods in the System object code. More information.
It seems 'udpport' function is unsupported in Simulink,but not user defined Class. What should I do to used 'udpport' function in simulink?
Thank you very much.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by