- 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
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
댓글 수: 0
채택된 답변
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.
Attaching a documentation link for your reference
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!