主要内容

RunPolyspace分析来自的代码金宝app模型

You can run a Polyspace®analysis on the code generated from a Simulink®model or subsystem.

  • Polyspace Bug Finder.™检查错误或编码规则违规的代码。

  • Polyspace Code Prover™彻底检查运行时错误的代码。

This tutorial shows how to run a Polyspace analysis on the code generated from a Simulink model. For the complete workflow, seeRun Polyspace Analysis on Code Generated with Embedded Coder

先决条件

在从Simulink运行PolySpace之前,必须将您的P金宝appolySpace和Matlab链接®安装。看将PolySpace与Matlab和Simulink集成金宝app

要打开此示例中使用的模型,请在MATLAB命令窗口中运行:

OpenExample('polyspace_code_prover/OpenSimulinkModelForPolyspaceAnalysisExample')

Open Simulink Model for Polyspace Analysis

Open the model polyspace_controller_demo.

生成和分析代码

  1. 在这方面Apps选项卡,选择Polyspace Code Verifier

  2. 在这方面Polyspacetab, locate theAnalyzefield and select作为顶级模型生成的代码from the drop-down list.

  3. 单击画布中的任意位置。这分析代码字段显示模型名称。选择运行分析

    PolySpace启动代码先驱分析。

当你点击运行分析,代码抄本检查生成的代码。如果没有生成的代码,则使用嵌入式编码器,PolySpace首先生成代码®,然后开始分析。

或者,在MATLAB命令窗口中,输入:

%加载模型load_system('polyspace_controller_demo');% Generate codeslbuild('polyspace_controller_demo');% Create Polyspace options objectmlopts = pslinkoptions('polyspace_controller_demo');%指定结果文件夹mlopts.ResultDir ='\cp_result';% Set analysis to Code Prover modemlopts.VerificationMode ='CodeProver';%运行分析pslinkrun('polyspace_controller_demo',mlopts);
pslinkoptions.pslinkrun

审查分析结果

分析完成后,PolySpace用户界面将打开以显示结果。结果包括颜色编码的检查:

  • Green (proven code):检查不会因提供的数据约束而失败。例如,划分操作不会导致aDivision by Zero错误。

  • Red (verified error): The check always fails for the set of data constraints provided. For instance, a division operation always causes aDivision by Zero错误。

  • 橙色(可能的错误):检查表示未经熟合的代码,并且可以为提供的数据约束的某些值失败。例如,分割操作有时会导致aDivision by Zero错误。

  • Gray (unreachable code): The check indicates a code operation that cannot be reached for the data constraints provided.

审查每个分析结果详细结果。例如,在您的代码抄本结果中:

  1. 在这方面Results Listpane, select the red出界数组索引查看。

  2. 在这方面来源pane, place your cursor on the red check to view additional information. For instance, the tooltip on the red[操作员指出数组大小和阵列索引的可能值。这结果细节窗格中还提供了这些信息。

在手写C代码中发生了两个红色检查C Function堵塞command_stretegy.

跟踪和修复模型中的问题

Errors in code generated from the model are caused by issues in the model. Trace an error back to your model to investigate the root cause.

Error 1: Out of bounds array index

  1. 在这方面Results Listpane, select the orange出界数组索引文件中发生的错误polyspace_controller_demo.c.

  2. 在这方面来源窗格,单击链接S4:76in comments above the orange error.

    /* 过渡:':75'*/ /* Transition:':76'* /(* i)++;/ *出口:' / disualtable'* / polyspace_controller_demo_y.faulttable[* i] = 10;

这Simulink Editor highlights the origin of this error. In this case, the error occurs due to a transition in the Stateflow chartsynch_and_asynch_monitoring。Trace the error to the input variable index of the Stateflow chart.

One possible solution to avoid the orange出界数组索引违规是约束输入变量指数。用一个饱和堵塞before the Stateflow chart to limit指数between zero to100.。在修改模型上运行分析后,解决了橙色检查。

错误2:溢出

  1. 在这方面Results Listpane, select the orange溢出error shown in the code here. The error appears in the filepolyspace_controller_demo.c.

    /* 获得:'/Gain'包含:* Inport:' /电池信息'* Inport:' /旋转'*总和:'/Sum1'* / RTB_K =(int16_t)(((int16_t)((in_rootation + in_battery_info)>> 1)* 24576)>> 10);

  2. 在这方面来源pane, review the error. To trace the error back to the model, click the linkS1/Gainin comments above the orange error. The Simulink Editor highlights the origin of this error. In this case, the error occurs in the故障管理A子系统内部获得块追随A.Sum堵塞。

One possible solution to avoid the orange溢出error is to constrain the value of the signalin_battery_infothat is fed to theSum堵塞。

  1. 双击Inport块Battery infothat provides the input signalin_battery_infoto the model.

  2. 在这方面信号属性tab, change the最大信号的值为较低的值,例如500.

在修改模型上运行分析后,解决了橙色检查。

这errors in this model occur due to one of these design issues:

  • Faulty scaling, unknown calibrations, and untested data ranges coming out of a subsystem into an arithmetic block.

  • 基于状态流的建模和手写查找表函数的数组操作。

  • 饱和s leading to unexpected data flow inside the generated code.

  • Faulty Stateflow programming.

Once you identify the root cause of the error, you can modify the model appropriately to fix the issue.

Check for Coding Rule Violations

启动错误查找器分析以检查编码规则违规。

  1. 在这方面Polyspace选项卡,选择设置>项目设置和enable the MISRA C:2012 coding standard in theCoding Standards & Code Metrics节点。保存配置并关闭窗口。

  2. 在这方面Polyspace选项卡,选择臭虫查找器

  3. ClickApply或者好的和rerun the analysis.

或者,在MATLAB命令窗口中,输入:

%Enable Checking for MISRA C:2012违规MLOPTIONSTINGS ='PRJCONFIGANDMISRAC2012';%为Bug Finder分析指定单独的文件夹MLOPTS.RESULTDIR ='\ BF_RESULT';%设置对Bug Finder模式的分析MLOPTS.VerificationMode ='Bugfinder';%运行分析pslinkrun('polyspace_controller_demo',mlopts);
分析完成后,将打开多核空间UI,其中包含Misra C:2012规则违规的列表。

注释块以证明结果

You can justify your results by adding annotations to your blocks. During code analysis, Polyspace populates the result with your justification. Once you justify a result, you do not have to review it again.

  1. 在这方面Results Listpane, from the drop-down list in the upper-left corner, select文件

  2. 在文件中polyspace_controller_demo.c., in the functionpolyspace_controller_demo_step(),选择违反MISRA C:2012第10.4条第10.4条。这来源窗格显示添加操作违反规则。

  3. 在这方面来源窗格,单击链接S1 / SUM1在评论中以上添加操作。

    /* 获得:'/Gain'包含:* Inport:' /电池信息'* Inport:' /旋转'*总和:'/Sum1'* / RTB_K =(INT16_T)(((IN_ROTATION + IN_Battery_INFO)>> 1)* 24576)>> 10);

    这rule violation occurs in a Sum block.

  4. 要注释此块并证明规则违反规则:

    1. 选择块。在这方面Polyspace选项卡,选择Add Annotation

    2. 选择Misra-C-2012forAnnotation type并输入有关规则违规的信息。设定StatustoNo action planned和the严重程度to解开

    3. ClickApply或者好的。这wordsPolyspace annotation显示在块下方,指示块包含代码注释。

  5. 重新生成代码并重新运行分析。这严重程度Statuscolumns on theResults List窗格与您的注释预先填充。

相关话题