一个可重用的库子系统子系统萤火虫ed in a library that is configured for reuse. You must define a subsystem in a library and configure it for reuse to reuse the subsystem across models.
To reuse common functionality, you can include multiple instances of a subsystem:
Within a single model, which is a top model or part of model reference hierarchy
Across multiple referenced models in a model reference hierarchy
Across multiple top models that contain Model blocks
Across multiple top models that do not include Model blocks
The code generator uses checksums to determine reusability. There are cases when the code generator cannot reuse subsystem code.
For incremental code generation, if the reusable library subsystem changes, a rebuild of itself and its parents occurs. During the build, if a matching function is not found, a new instance of the reusable function is generated into the shared utilities folder. If a different matching function is found from previous builds, that function is used, and a new reusable function is not emitted.
For subsequent builds, unused files are not replaced or deleted from your folder. During development of a model, when many obsolete shared functions exist in the shared utilities folder, you can delete the folder and regenerate the code. If all instances of a reusable library subsystem are removed from a model reference hierarchy and you regenerate the code, the obsolete shared functions remain in the shared utilities folder until you delete them.
If a model changes such that the change might cause different generated code for the subsystem, a new reusable function is generated. For example, model configuration parameters that modify code comments might cause different generated code for the subsystem even if the reusable library subsystem did not change.
Embedded Coder users can generate code from a library that contains subsystems are configured for reuse. For more information, seeLibrary-Based Code Generation for Reusable Library Subsystems.
The generated code of a reusable library subsystem is independent of the generated code of the model. Code for the reusable library subsystem is generated to the shared utility folder,slprj /
, instead of the model reference hierarchy folders. The generated code for the supporting types, which are generated to thetarget
/_sharedutils.h
file, are also in the shared utilities folder.
For unique naming, reusable function names have a checksum appended to the reusable library subsystem name. For example, the code and files for a subsystem,SS1
, which links to a reusable library subsystem,RLS
, might be:
Function name:RLS_mgdjlngd
File name:RLS_mgdjlnd.c
andRLS_mgdjlnd.h
Set the Subsystem parameters as listed here:
SelectTreat as an atomic unit.
On theCode Generationtab:
SetFunction packagingtoReusable function
.
Set theFunction name options
andFile name optionsparameters to one of the following combinations:
SetFunction name optionsandFile name optionstoAuto
.
SetFunction name optionstoUse subsystem name
andFile name optionstoUse function name
.
SetFunction name optionstoUser specified
andFile name optionstoAuto
orUse function name
.
SetFunction name optionstoUser specified
andFile name optionstoUser specified
. Set the same value for theFunction nameandFile nameparameters.
In a model reference hierarchy, if an instance of the reusable library subsystem is in the top model, then on theModel Referencingpane of the Configuration Parameters dialog box, you must select thePass fixed-size scalar root input by value for code generationparameter. If you do not select the parameter, a separate shared function is generated for the reusable library subsystem instance in the top model, and a reusable function is generated for instances in the referenced models.
If a reusable library subsystem is connected to the root Outport, reuse does not happen with identical subsystems that are not connected to the root Outport. However, you can setPass reusable system outputs astoIndividual arguments
on theOptimizationspane to make sure that reuse occurs between these subsystems. This parameter requires an Embedded Coder®license.
For more information on creating a library, seeCustom Libraries. For an example of creating a reusable library subsystem, seeGenerate Reusable Code for Subsystems Shared Across Models.
For a model to use a reusable library subsystem, you must configure the model differently depending on whether the model is a reference model or top model. If the subsystem is in a referenced model hierarchy, set the configuration parameter,Shared code placementtoAuto
. Otherwise, for each model that uses the subsystem, set the model configuration parameterShared code placementtoShared location
.
If a reusable library subsystem uses a shared local data store and you configure default mapping for model data elements, leave the default storage class mapping for categoryShared local data stores设置为Default.
This example shows how to configure a reusable library subsystem and generate a reusable function for a subsystem shared across referenced models. The result is reusable code for the subsystem, which is generated to the shared utility folder (slprj /
).target
/_sharedutils
In the Simulink Editor, on theSimulationtab, selectNew > Library. SelectBlank Libraryfrom the Simulink Start Page window. Openrtwdemo_ssreuse
to copy and paste subsystemSS1
into the Library Editor. This action loads the variables forSS1
into the base workspace. Rename the subsystem block toRLS
.
Click the Subsystem block and pressCtrl+U查看的内容子系统RLS
.
To configure the subsystem, in the Library editor, right-clickRLS
. In the context menu, selectBlock Parameters (Subsystem). In the Subsystem Parameters dialog box, choose the following options:
SelectTreat as an atomic unit.
On theCode Generationtab:
SetFunction packagingtoReusable function
.
SetFunction name optionstoUser specified
and verify that theFunction nameis set tomyfun
.
SetFile name optionstoAuto
.
ClickApplyandOK.
Save the reusable library subsystem asssreuselib
, which creates a file,ssreuselib.slx
.
Create a model which includes one instance ofRLS
fromssreuselib
. Name this subsystemSS1
. Add another subsystem and name itSS2
. Name the modelex_model1
.
Create another model which includes one instance ofRLS
fromssreuselib
. Name this subsystemSS1
. Add another subsystem and name itSS3
. Name the modelex_model2
.
Create a top model with two model blocks that referenceex_model1
andex_model2
. Save the top model asex_mdlref_ssreuse
.
With modelex_mdlref_ssreuse
open in the Simulink Editor, on theC Codetab, clickSettingsto open the Configuration Parameters dialog box.
On theSolverpane, specify theTypeasFixed-step
.
On theModel Referencingpane, selectPass fixed-size scalar root inputs by value for code generation.
On theCode Generation > Reportpane, selectCreate code generation reportandOpen report automatically.
On theCode Generation > Interfacepane, set theShared code placementtoShared location
.
On theCode Generation > Identifierspane, set theMaximum identifier lengthto256
. This step is optional.
ClickApplyandOK.
In the Simulink Editor, on theModelingtab, clickModel Explorerto open the Model Explorer. In the left navigation column of the Model Explorer, expand theex_mdlref_ssreuse
node.
Select the Configurations node below theex_mdlref_ssreuse
node. In theContentspane, right-click配置and selectConvert to Configuration Reference
.
In the Convert Active Configuration to Reference dialog box, clickOK. This action converts the model configuration set to a configuration reference,Simulink.ConfigSetRef
, and creates the configuration reference object,configSetObj
, in the base workspace.
In the left navigation column, right-clickReference (Active)and selectPropagate to Referenced Models
.
In the Configuration Reference Propagation to Referenced Models dialog box, select the referenced models in the list. ClickPropagate.
Now, the top model and referenced models use the same configuration reference,Reference (Active)
, which points to a model configuration reference object,configSetObj
, in the base workspace. When you save your model, you also need to export theconfigSetObj
to a MAT-file.
To generate code, in the Simulink Editor, pressCtrl+B. After the code is generated, the code generation report opens.
To view the code generation report for a referenced model, in the left navigation pane, in theReferenced Modelssection, selectex_model1
. The code generation report displays the generated files forex_model1
.
In the left navigation pane, expand theShared files. The code generator uses the reusable library subsystem name. The code for subsystemSS1
is inmyfun.c
andmyfun.h
.
ClickBackand navigate to theex_model2
code generation report.ex_model2
uses the same source code,myfun.c
andmyfun.h
, as the code forex_model1
. Your subsystem function and file names will be different.