概述
1、Simulink中Matlab Function函数的输入输出除支持常见基本数据类型如uint8,single, boolean等外,还支持自定义结构体,但需在Edit Data中找到Input和Output的端口,并在Type中选定workspace中已存在的bus类型。
2、Simulink自定义Bus类型。
Simulink.Bus
对象是一种数据类型,当与 Simulink.BusElement 对象结合使用时,它指定并验证总线的属性。在仿真或更新模型时,Simulink® 检查连接到模块的总线是否与模块指定的 Simulink.Bus
对象数据类型匹配。
Bus
对象仅指定总线的架构属性。例如,总线对象可以指定元素名称、层次结构、顺序和数据类型。Bus
对象无法指定总线中信号的值。
Bus
对象类似于 C 语言中的结构体定义:它定义总线的成员,但不创建总线。Bus
对象也类似于电缆连接器。连接器定义所有的引脚及其配置,并控制它们可以连接哪些类型的电线。类似地,Bus
对象定义关联的总线必须具有的信号配置和属性。
Bus
对象包含 Simulink.BusElement 对象。每个 BusElement
对象指定总线中一个信号的属性,如信号的名称、数据类型和维度。Bus
对象中 BusElement
对象的顺序决定总线中信号的顺序。
Bus
对象可以指定未由组成信号定义但会被继承的属性。
参见链接Simulink.Bus
下面的示例中,通过Name, Description,DataType,Dimensions描述Bus中每个Element。
%% Sensor Object Bus
Sensor_Object_Bus=Simulink.Bus;
Sensor_Object_Bus.Elements(1).Name = 'ObjID';
Sensor_Object_Bus.Elements(1).Description = 'object ID';
Sensor_Object_Bus.Elements(1).DataType = 'uint8';
Sensor_Object_Bus.Elements(1).Dimensions = 1;
Sensor_Object_Bus.Elements(2).Name = 'DistX';
Sensor_Object_Bus.Elements(2).Description = 'Relative Longitude Range';
Sensor_Object_Bus.Elements(2).DataType = 'single';
Sensor_Object_Bus.Elements(2).Dimensions = 1;
Sensor_Object_Bus.Elements(3).Name = 'DistY';
Sensor_Object_Bus.Elements(3).Description = 'Relative Lateral Range';
Sensor_Object_Bus.Elements(3).DataType = 'single';
Sensor_Object_Bus.Elements(3).Dimensions = 1;
Sensor_Object_Bus.Elements(4).Name = 'VrelX';
Sensor_Object_Bus.Elements(4).Description = 'Relative Longitude Speed';
Sensor_Object_Bus.Elements(4).DataType = 'single';
Sensor_Object_Bus.Elements(4).Dimensions = 1;
Sensor_Object_Bus.Elements(5).Name = 'VrelY';
Sensor_Object_Bus.Elements(5).Description = 'Relative Lateral Speed';
Sensor_Object_Bus.Elements(5).DataType = 'single';
Sensor_Object_Bus.Elements(5).Dimensions = 1;
Sensor_Object_Bus.Elements(6).Name = 'ArelX';
Sensor_Object_Bus.Elements(6).Description = '';
Sensor_Object_Bus.Elements(6).DataType = 'single';
Sensor_Object_Bus.Elements(6).Dimensions = 1;
save('./Fusion/FusionBus.mat',...
'Sensor_Object_Bus');
示例中的Sensor_Object_Bus总共有6个Element,通过save指令将Bus以mat格式保存。
并在模型Model Properties→Callbacks→PreLoadFcn中通过load指令在每次打开模型时将自定义Bus加载至workspace。
3、为保证每个单独的Matlab Function能够实现单元独立编译,需配置Matlab Function中数据类型为结构体inport/outport的Data Type为workspace中对应的bus,避免编译报错。
通过上述操作可实现编译时simulink识别下图中matlab function的ESR_Message结构体中的CAN_RX_TRACK_ANGLE属性,避免编译报错。
function FR_ObjectList = SensorRadarTransferInitial(...
ESR_Track_01, ESR_Track_02, ESR_Track_03, ESR_Track_04, ESR_Track_05,ESR_Track_06,ESR_Track_07,ESR_Track_08, ESR_Track_09, ESR_Track_10,...
ESR_Track_11, ESR_Track_12, ESR_Track_13, ESR_Track_14, ESR_Track_15,ESR_Track_16,ESR_Track_17,ESR_Track_18, ESR_Track_19, ESR_Track_20, ...
ESR_Track_21, ESR_Track_22, ESR_Track_23, ESR_Track_24, ESR_Track_25,ESR_Track_26,ESR_Track_27,ESR_Track_28, ESR_Track_29, ESR_Track_30, ...
ESR_Track_31, ESR_Track_32, ESR_Track_33, ESR_Track_34, ESR_Track_35,ESR_Track_36,ESR_Track_37,ESR_Track_38, ESR_Track_39, ESR_Track_40, ...
ESR_Track_41, ESR_Track_42, ESR_Track_43, ESR_Track_44, ESR_Track_45,ESR_Track_46,ESR_Track_47,ESR_Track_48, ESR_Track_49, ESR_Track_50, ...
ESR_Track_51, ESR_Track_52, ESR_Track_53, ESR_Track_54, ESR_Track_55,ESR_Track_56,ESR_Track_57,ESR_Track_58, ESR_Track_59, ESR_Track_60, ...
ESR_Track_61, ESR_Track_62, ESR_Track_63, ESR_Track_64)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% RADAR
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FR_ObjectList_Struct = struct(...
'ObjID',uint8(0),...
'DistX',single(0),...
'DistY',single(0),...
'VrelX',single(0),...
'VrelY',single(0),...
'ArelX',single(0),...
'Width',single(0),...
'Length',single(0),...
'New',uint8(0),...
'LifeTime',single(0),...
'Extrapolation',uint16(0),...%%
'Classification',uint8(4),... % OBJCLASS_CAR
'RCS',single(20),...
'Beam',uint8(3),...% BEAM_BOTH
'MeasuredSource',uint8(0),...
'BrakeLight',false,...
'TurnIndictionLeft',false,...
'TurnIndictionRight',false,...
'MotionCategory',uint8(0),...
'MotionStatus',uint8(0),...
'LaneAssignment',uint8(0));
FR_ObjectList = repmat(FR_ObjectList_Struct,64,1);
%%
FR_ObjectList(1) = ESR_DataMapping(FR_ObjectList(1), ESR_Track_01, 1);
FR_ObjectList(2) = ESR_DataMapping(FR_ObjectList(2), ESR_Track_02, 2);
FR_ObjectList(3) = ESR_DataMapping(FR_ObjectList(3), ESR_Track_03, 3);
FR_ObjectList(4) = ESR_DataMapping(FR_ObjectList(4), ESR_Track_04, 4);
FR_ObjectList(5) = ESR_DataMapping(FR_ObjectList(5), ESR_Track_05, 5);
FR_ObjectList(6) = ESR_DataMapping(FR_ObjectList(6), ESR_Track_06, 6);
FR_ObjectList(7) = ESR_DataMapping(FR_ObjectList(7), ESR_Track_07, 7);
FR_ObjectList(8) = ESR_DataMapping(FR_ObjectList(8), ESR_Track_08, 8);
FR_ObjectList(9) = ESR_DataMapping(FR_ObjectList(9), ESR_Track_09, 9);
FR_ObjectList(10) = ESR_DataMapping(FR_ObjectList(10), ESR_Track_10, 10);
FR_ObjectList(11) = ESR_DataMapping(FR_ObjectList(11), ESR_Track_11, 11);
FR_ObjectList(12) = ESR_DataMapping(FR_ObjectList(12), ESR_Track_12, 12);
FR_ObjectList(13) = ESR_DataMapping(FR_ObjectList(13), ESR_Track_13, 13);
FR_ObjectList(14) = ESR_DataMapping(FR_ObjectList(14), ESR_Track_14, 14);
FR_ObjectList(15) = ESR_DataMapping(FR_ObjectList(15), ESR_Track_15, 15);
FR_ObjectList(16) = ESR_DataMapping(FR_ObjectList(16), ESR_Track_16, 16);
FR_ObjectList(17) = ESR_DataMapping(FR_ObjectList(17), ESR_Track_17, 17);
FR_ObjectList(18) = ESR_DataMapping(FR_ObjectList(18), ESR_Track_18, 18);
FR_ObjectList(19) = ESR_DataMapping(FR_ObjectList(19), ESR_Track_19, 19);
FR_ObjectList(20) = ESR_DataMapping(FR_ObjectList(20), ESR_Track_20, 20);
FR_ObjectList(21) = ESR_DataMapping(FR_ObjectList(21), ESR_Track_21, 21);
FR_ObjectList(22) = ESR_DataMapping(FR_ObjectList(22), ESR_Track_22, 22);
FR_ObjectList(23) = ESR_DataMapping(FR_ObjectList(23), ESR_Track_23, 23);
FR_ObjectList(24) = ESR_DataMapping(FR_ObjectList(24), ESR_Track_24, 24);
FR_ObjectList(25) = ESR_DataMapping(FR_ObjectList(25), ESR_Track_25, 25);
FR_ObjectList(26) = ESR_DataMapping(FR_ObjectList(26), ESR_Track_26, 26);
FR_ObjectList(27) = ESR_DataMapping(FR_ObjectList(27), ESR_Track_27, 27);
FR_ObjectList(28) = ESR_DataMapping(FR_ObjectList(28), ESR_Track_28, 28);
FR_ObjectList(29) = ESR_DataMapping(FR_ObjectList(29), ESR_Track_29, 29);
FR_ObjectList(30) = ESR_DataMapping(FR_ObjectList(30), ESR_Track_30, 30);
FR_ObjectList(31) = ESR_DataMapping(FR_ObjectList(31), ESR_Track_31, 31);
FR_ObjectList(32) = ESR_DataMapping(FR_ObjectList(32), ESR_Track_32, 32);
FR_ObjectList(33) = ESR_DataMapping(FR_ObjectList(33), ESR_Track_33, 33);
FR_ObjectList(34) = ESR_DataMapping(FR_ObjectList(34), ESR_Track_34, 34);
FR_ObjectList(35) = ESR_DataMapping(FR_ObjectList(35), ESR_Track_35, 35);
FR_ObjectList(36) = ESR_DataMapping(FR_ObjectList(36), ESR_Track_36, 36);
FR_ObjectList(37) = ESR_DataMapping(FR_ObjectList(37), ESR_Track_37, 37);
FR_ObjectList(38) = ESR_DataMapping(FR_ObjectList(38), ESR_Track_38, 38);
FR_ObjectList(39) = ESR_DataMapping(FR_ObjectList(39), ESR_Track_39, 39);
FR_ObjectList(40) = ESR_DataMapping(FR_ObjectList(40), ESR_Track_40, 40);
FR_ObjectList(41) = ESR_DataMapping(FR_ObjectList(41), ESR_Track_41, 41);
FR_ObjectList(42) = ESR_DataMapping(FR_ObjectList(42), ESR_Track_42, 42);
FR_ObjectList(43) = ESR_DataMapping(FR_ObjectList(43), ESR_Track_43, 43);
FR_ObjectList(44) = ESR_DataMapping(FR_ObjectList(44), ESR_Track_44, 44);
FR_ObjectList(45) = ESR_DataMapping(FR_ObjectList(45), ESR_Track_45, 45);
FR_ObjectList(46) = ESR_DataMapping(FR_ObjectList(46), ESR_Track_46, 46);
FR_ObjectList(47) = ESR_DataMapping(FR_ObjectList(47), ESR_Track_47, 47);
FR_ObjectList(48) = ESR_DataMapping(FR_ObjectList(48), ESR_Track_48, 48);
FR_ObjectList(49) = ESR_DataMapping(FR_ObjectList(49), ESR_Track_49, 49);
FR_ObjectList(50) = ESR_DataMapping(FR_ObjectList(50), ESR_Track_50, 50);
FR_ObjectList(51) = ESR_DataMapping(FR_ObjectList(51), ESR_Track_51, 51);
FR_ObjectList(52) = ESR_DataMapping(FR_ObjectList(52), ESR_Track_52, 52);
FR_ObjectList(53) = ESR_DataMapping(FR_ObjectList(53), ESR_Track_53, 53);
FR_ObjectList(54) = ESR_DataMapping(FR_ObjectList(54), ESR_Track_54, 54);
FR_ObjectList(55) = ESR_DataMapping(FR_ObjectList(55), ESR_Track_55, 55);
FR_ObjectList(56) = ESR_DataMapping(FR_ObjectList(56), ESR_Track_56, 56);
FR_ObjectList(57) = ESR_DataMapping(FR_ObjectList(57), ESR_Track_57, 57);
FR_ObjectList(58) = ESR_DataMapping(FR_ObjectList(58), ESR_Track_58, 58);
FR_ObjectList(59) = ESR_DataMapping(FR_ObjectList(59), ESR_Track_59, 59);
FR_ObjectList(60) = ESR_DataMapping(FR_ObjectList(60), ESR_Track_60, 60);
FR_ObjectList(61) = ESR_DataMapping(FR_ObjectList(61), ESR_Track_61, 61);
FR_ObjectList(62) = ESR_DataMapping(FR_ObjectList(62), ESR_Track_62, 62);
FR_ObjectList(63) = ESR_DataMapping(FR_ObjectList(63), ESR_Track_63, 63);
FR_ObjectList(64) = ESR_DataMapping(FR_ObjectList(64), ESR_Track_64, 64);
end
%%
function FR_Object_out = ESR_DataMapping(FR_Object_in, ESR_Message, ObjID)
FR_Object_out = FR_Object_in;
theta = single(ESR_Message.CAN_RX_TRACK_ANGLE / 180 * pi);
range = single(ESR_Message.CAN_RX_TRACK_RANGE);
velocity = single(ESR_Message.CAN_RX_TRACK_RANGE_RATE);
acceleration = single(ESR_Message.CAN_RX_TRACK_RANGE_ACCEL);
FR_Object_out.ObjID = uint8(ObjID);
FR_Object_out.DistX = single(range * cos(theta));
FR_Object_out.DistY = single(range * sin(theta));
FR_Object_out.VrelX = single(velocity * cos(theta));
FR_Object_out.VrelY = single(velocity * sin(theta));
FR_Object_out.ArelX = single(acceleration * cos(theta));
FR_Object_out.Width = single(ESR_Message.CAN_RX_TRACK_WIDTH);
end
最后
以上就是兴奋宝马为你收集整理的Matlab Function输入输出的全部内容,希望文章能够帮你解决Matlab Function输入输出所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复