Simulink中生​成Autosar代码​中如何使得Min Max 生成fminf 或fmaxf 函数 ( How to make Min Max generate fminf or fmaxf functions when generating Autosar code in Simulink)

8 views (last 30 days)
simulink 生成Autosar 代码中
如何设置Min 或 Max 在生成代码中使用fminf ,fmaxf 函数,
不想使得代码通过if else的方式来实现 Min 或Max
Generating Autosar code using Simulink
How to set Min or Max to use the fminf and fmaxf functions in generating code,
I don't want the code to implement Min or Max through if else
目前在非Autosar代码中可以实现Min 或Max,但是在Autosar代码中不能实现Min 或Max,
Currently, Min or Max can be implemented in non Autosar code, but it cannot be implemented in Autosar code,

Accepted Answer

Xiaoning.Wang
Xiaoning.Wang on 7 Jan 2025
在设置参数中可以选择设置C99
cs.set_param('TargetLangStandard', 'C99 (ISO)'); % 语言标准
因为fminf,fmaxf是在C99标准规则中

More Answers (2)

Xiaoning.Wang
Xiaoning.Wang on 9 Feb 2025
针对C问题,Targetlink设置C99是不能满足要求
如果使用S-function,但是每个人和每个场景需要Min, Max使用的个数不一样,所以减一大家使用Stateflow做一个Min,该Min需要用户双击点击,需要自己输入 Inport 的Number
所以需要自己创建Simulink的最小Min的Lib
  2 Comments
Xiaoning.Wang
Xiaoning.Wang on 9 Feb 2025
M脚本对Sateflow的更改
function Min_X(n)
n = str2double(get_param(gcb,'n'));
%delete all inport and outport
%delete_block([gcb,'/','MinOut']); %deleted the out port
cell_inports = find_system(gcb, 'SearchDepth', 1,'LookUnderMasks','all', 'BlockType', 'Inport');
cell_lines = find_system(gcb, 'SearchDepth', 1,'FindAll','on','LookUnderMasks','all', 'type', 'line');
%cell_Outports = find_system(gcb, 'SearchDepth', 1,'LookUnderMasks','all', 'BlockType', 'Outport');
cell_DataTypeConversions = find_system(gcb, 'SearchDepth', 1,'LookUnderMasks','all', 'BlockType', 'DataTypeConversion');
if n > length(cell_inports)
for Min_idx = length(cell_inports)+1:1:n
add_block('simulink/Commonly Used Blocks/In1',[gcb,'/','Min',num2str(Min_idx)]);
end
else
%% delete inport from the last
for Min_idx = length(cell_inports):-1:n+1
delete_block([gcb,'/Min',num2str(Min_idx)]); %deleted the inport
end
end
% % if ~isempty(cell_inports)
% % for Inport_idx = 1:length(cell_inports)
% % delete_block(cell_inports{Inport_idx}); %deleted the Inport port
% % end
% % end
if ~isempty(cell_DataTypeConversions)
for DataTypeConversions_idx = 1:length(cell_DataTypeConversions)
delete_block(cell_DataTypeConversions(DataTypeConversions_idx)); %deleted the DataTypeConversion
end
end
if ~isempty(cell_lines)
for lines_idx = 1:length(cell_lines)
if ~strcmp(get(cell_lines(lines_idx),'SourcePort'),'Min_X:1')
delete_line(cell_lines(lines_idx)); %deleted the inport line
end
end
end
%if length(cell_Outports)>0
% for Outport_idx = 1:length(cell_Outports)
% delete_block(cell_Outports{Outport_idx}); %deleted the Outport port
% end
%end
%% find the Stateflow char of the gcb path
rt = sfroot;
f = @(h)(strcmp(h.Path,[gcb,'/Min_X'])); % Stateflow Name is Min_X
StateflowHandle = find(rt,'-isa','Stateflow.Chart','-and','-function',f); % find charts for whcih f returns 'ture'
%% deleted the inport of the stateflow
StateflowInputData = StateflowHandle.find('-isa','Stateflow.Data','Scope','Input');
for StateflowInputData_idx = 1:length(StateflowInputData)
delete(StateflowInputData(StateflowInputData_idx));
end
%% add the inport of the sateflow
for n =1:n
MinInput = Stateflow.Data(StateflowHandle);
MinInput.Name = ['Min',num2str(n)];
MinInput.Scope = 'Input';
MinInput.Props.Type.Method = 'Built-in';
MinInput.DataType = 'single';
end
%% find the Transition of the stateflow
StateflowTransition = StateflowHandle.find('-isa','Stateflow.Transition');
%StateflowTransition.LabelString = '{MinOut=min(Min1,Min2);}';
MinFunctionStr = '{MinOut=min(Min1';
for n=2:n
MinFunctionStr = [MinFunctionStr,',','Min',num2str(n)];
end
MinFunctionStr = [MinFunctionStr ,');}'];
StateflowTransition.LabelString = MinFunctionStr; % change the LableString
%Char_Position = get_param([gcb,'/Chart'],'Position');
%% add the inport
for Min_idx = 1:n
%add_block('simulink/Commonly Used Blocks/In1',[gcb,'/','Min',num2str(Min_idx)]);
DataTypeConversion_h = add_block('simulink/Commonly Used Blocks/Data Type Conversion',[gcb,'/','Data Type Conversion',num2str(Min_idx)]);
set_param(DataTypeConversion_h,'OutDataTypeStr','single');
% add line
add_line(gcb,['Min',num2str(Min_idx),'/1'],['Data Type Conversion',num2str(Min_idx),'/1']);
add_line(gcb,['Data Type Conversion',num2str(Min_idx),'/1'],['Min_X/',num2str(Min_idx)]);
end
%add_block('simulink/Commonly Used Blocks/Out1',[gcb,'/','MinOut']);
end
Xiaoning.Wang
Xiaoning.Wang on 9 Feb 2025
在Mask中的Initialization 的设置如下:
防止在simulink 仿真运行以及生成代码中运行改脚本脚本
cell_inports = find_system(gcb, 'SearchDepth', 1,'LookUnderMasks','all', 'BlockType', 'Inport');
%n_Number = str2double(get_param(gcb,'n'));
%disp(class(n_Number));
if n~=length(cell_inports) && n >= 2
%disp('run');
Min_X(n);
else
%disp('error');
end

Sign in to comment.


Xiaoning.Wang
Xiaoning.Wang on 21 Jun 2025
set_param('ModelName','TargetLangStandard', 'C99 (ISO)');

Categories

Find more on AUTOSAR Blockset in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!