• 基于DBC Signal Group生成Autosar SR接口(2)


    前言

    上一篇文章中,介绍了DBC中SignalGroup的提取,对于已经提取好的Group信息,就可以批量操作生成Simulink BUS及Simulink接口模型了。本文介绍这部分的脚本实现。

    m脚本生成BUS数据类型

    通过对前面导出的m文件分析,在第一个数据需要特殊处理,其他的正常按字符串写入就好了。参考代码如下:

    
    for i = 1:length(can_signal_group_Struct)
        GroupName = can_signal_group_Struct(i).GroupName;
    
    
        outputParameter_str = strcat(GroupName,' = ',' Simulink.Bus;\n');
        fprintf(fid,outputParameter_str);
        outputParameter_str = strcat(GroupName,'.Description',' = '''';\n');
        fprintf(fid,outputParameter_str);
        outputParameter_str = strcat(GroupName,'.DataScope',' = ''Auto'';\n');
        fprintf(fid,outputParameter_str);
        outputParameter_str = strcat(GroupName,'.HeaderFile',' = ''Rte_Type.h'';\n');
        fprintf(fid,outputParameter_str);
        outputParameter_str = strcat(GroupName,'.Alignment',' = -1;\n');
        fprintf(fid,outputParameter_str);
        outputParameter_str = strcat(GroupName,'.PreserveElementDimensions',' = false;\n');
        fprintf(fid,outputParameter_str);
        for j = 1 : length(can_signal_group_Struct(i).DataType)
            SignalNameOut = can_signal_group_Struct(i).SignalName{j};
            SignalDataTypeOut = can_signal_group_Struct(i).DataType{j};
            if isempty(SignalDataTypeOut)
                SignalDataTypeOut = 'uint8';
            end
            if j == 1
                outputParameter_str = 'saveVarsTmp{1} = Simulink.BusElement;\n';
                fprintf(fid,outputParameter_str);
                outputParameter_str = strcat('saveVarsTmp{1}.Name = ''',SignalNameOut,''';\n');
                fprintf(fid,outputParameter_str);
                outputParameter_str = strcat('saveVarsTmp{1}.Complexity = ''','real',''';\n');
                fprintf(fid,outputParameter_str);
                outputParameter_str = 'saveVarsTmp{1}.Dimensions = 1;\n';
                fprintf(fid,outputParameter_str);
                outputParameter_str = strcat('saveVarsTmp{1}.DataType = ''',SignalDataTypeOut,''';\n');
                fprintf(fid,outputParameter_str);
                outputParameter_str = 'saveVarsTmp{1}.Min = [];\n';
                fprintf(fid,outputParameter_str);
                outputParameter_str = 'saveVarsTmp{1}.Max = [];\n';
                fprintf(fid,outputParameter_str);
                outputParameter_str = 'saveVarsTmp{1}.DimensionsMode = ''Fixed'';\n';
                fprintf(fid,outputParameter_str);        
                outputParameter_str = 'saveVarsTmp{1}.SamplingMode = ''Sample based'';\n';
                fprintf(fid,outputParameter_str);  
                outputParameter_str = 'saveVarsTmp{1}.DocUnits = '''';\n';
                fprintf(fid,outputParameter_str);
                outputParameter_str = 'saveVarsTmp{1}.Description = '''';\n';
                fprintf(fid,outputParameter_str);
            else
                outputParameter_str = strcat('saveVarsTmp{1}(',num2str(j),', 1)',' = Simulink.BusElement;\n');
                fprintf(fid,outputParameter_str);
                outputParameter_str = strcat('saveVarsTmp{1}(',num2str(j),', 1)','.Name = ''',SignalNameOut,''';\n');
                fprintf(fid,outputParameter_str);
                outputParameter_str = strcat('saveVarsTmp{1}(',num2str(j),', 1)','.Complexity = ''','real',''';\n');
                fprintf(fid,outputParameter_str);
                outputParameter_str = strcat('saveVarsTmp{1}(',num2str(j),', 1)','.Dimensions = 1;\n');
                fprintf(fid,outputParameter_str);
                outputParameter_str = strcat('saveVarsTmp{1}(',num2str(j),', 1)','.DataType = ''',SignalDataTypeOut,''';\n');
                fprintf(fid,outputParameter_str);
                outputParameter_str = strcat('saveVarsTmp{1}(',num2str(j),', 1)','.Min = [];\n');
                fprintf(fid,outputParameter_str);
                outputParameter_str = strcat('saveVarsTmp{1}(',num2str(j),', 1)','.Max = [];\n');
                fprintf(fid,outputParameter_str);
                outputParameter_str = strcat('saveVarsTmp{1}(',num2str(j),', 1)','.DimensionsMode = ''','Fixed',''';\n');
                fprintf(fid,outputParameter_str);        
                outputParameter_str = strcat('saveVarsTmp{1}(',num2str(j),', 1)','.SamplingMode = ''','Sample based',''';\n');
                fprintf(fid,outputParameter_str);  
                outputParameter_str = strcat('saveVarsTmp{1}(',num2str(j),', 1)','.DocUnits = '''';\n');
                fprintf(fid,outputParameter_str);
                outputParameter_str = strcat('saveVarsTmp{1}(',num2str(j),', 1)','.Description = '''';\n');
                fprintf(fid,outputParameter_str);
            end
            if j == length(can_signal_group_Struct(i).DataType)
                outputParameter_str = strcat(GroupName,'.Elements = saveVarsTmp{1};\n');
                fprintf(fid,outputParameter_str);
                outputParameter_str = 'clear saveVarsTmp;\n';
                fprintf(fid,outputParameter_str);
    
            end
        end
        fprintf(fid,'\n');
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    此处我们只设定了组名称,信号名称,信号数据类型,其他的属性都是按默认值来的。

    需要注意的是HeaderFile,因为我们使用Autosar代码生成,所以定义的结构体的头文件应该设置为Rte_Type.h,否则后面在生成代码时会报错找不到数据类型定义。

    自动生成的m文件示例如下:

    BrkPedlTrvl = Simulink.Bus;
    BrkPedlTrvl.Description = '';
    BrkPedlTrvl.DataScope = 'Auto';
    BrkPedlTrvl.HeaderFile = 'Rte_Type.h';
    BrkPedlTrvl.Alignment = -1;
    BrkPedlTrvl.PreserveElementDimensions = false;
    saveVarsTmp{1} = Simulink.BusElement;
    saveVarsTmp{1}.Name = 'BrkPedlTrvlAct';
    saveVarsTmp{1}.Complexity = 'real';
    saveVarsTmp{1}.Dimensions = 1;
    saveVarsTmp{1}.DataType = 'uint16';
    saveVarsTmp{1}.Min = [];
    saveVarsTmp{1}.Max = [];
    saveVarsTmp{1}.DimensionsMode = 'Fixed';
    saveVarsTmp{1}.SamplingMode = 'Sample based';
    saveVarsTmp{1}.DocUnits = '';
    saveVarsTmp{1}.Description = '';
    saveVarsTmp{1}(2, 1) = Simulink.BusElement;
    saveVarsTmp{1}(2, 1).Name = 'BrkPedlTrvlChks';
    saveVarsTmp{1}(2, 1).Complexity = 'real';
    saveVarsTmp{1}(2, 1).Dimensions = 1;
    saveVarsTmp{1}(2, 1).DataType = 'uint8';
    saveVarsTmp{1}(2, 1).Min = [];
    saveVarsTmp{1}(2, 1).Max = [];
    saveVarsTmp{1}(2, 1).DimensionsMode = 'Fixed';
    saveVarsTmp{1}(2, 1).SamplingMode = 'Sample based';
    saveVarsTmp{1}(2, 1).DocUnits = '';
    saveVarsTmp{1}(2, 1).Description = '';
    ....
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    运行该m文件,即可得到对应的结构体定义,示例如下:

    建立Input模块及关联对应的BUS数据类型

    在之前的文章中介绍过S/R接口的生成和mapping,此处不再详细介绍。

    需要注意的是结构体类型在定义数据类型时的格式为:Bus: xxx,所以在创建Input模块时需要注意下

        GroupDataTypeStr = strcat('Bus:',32,can_signal_group_Struct(i).GroupName);
        handle = add_block('simulink/Sources/In1',RTEInput_ParameterName,'OutDataTypeStr',GroupDataTypeStr,...
            'PortDimensions',num2str(1),'BusOutputAsStruct','on');
    
    • 1
    • 2
    • 3

    此处对于BusOutputAsStruct属性,需要配置为on,否则生成代码会报错

    实现效果


    自动生成Port及mapping

    编译生成arxml


    导入Autosar工具中

    总结

    通过脚本实现Group类型的Port自动创建,可以节约一定的开发时间,对于重复性较高的工作,建议开发脚本实现半自动或自动化。

  • 相关阅读:
    【滤波跟踪】基于matlab扩展卡尔曼滤波的无人机路径跟踪【含Matlab源码 2236期】
    IDEA Intellij中SpringMVC框架搭建全过程
    数字音频工作站软件 Ableton Live 11 mac中文软件特点与功能
    浏览器的安全问题
    几乎所有的互联网公司都在进行一场脱虚向实的全新嬗变
    一些SQL的基本概念和用法
    【数据结构与算法】第一章 绪论 2-数据结构的基本概念
    [Java] 从内存的角度去理解ThreadLocal如何把不同线程间的访问隔离开来?ThreadLocal的内存泄露问题是什么?如何避免?
    搜维尔科技:【工业仿真】煤矿安全知识基础学习VR系统
    Hadoop集群搭建的详细过程
  • 原文地址:https://blog.csdn.net/weixin_49000276/article/details/133828185