MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   MATLAB技术文章 (https://www.labfans.com/bbs/forumdisplay.php?f=25)
-   -   Legacy Code Integration Using Simulink and Real-Time Workshop Embedded Coder (MATLAB (https://www.labfans.com/bbs/showthread.php?t=1117)

TechnicalArticles 2008-01-06 16:32

Legacy Code Integration Using Simulink and Real-Time Workshop Embedded Coder (MATLAB
 
[B]Legacy Code Integration Using Simulink and Real-Time Workshop Embedded Coder[/B]



[B]by[/B] [EMAIL="[email protected]"][U][COLOR=#0000ff]Tom Erkkinen[/COLOR][/U][/EMAIL] and [EMAIL="[email protected]"][U][COLOR=#0000ff]Eric Dillaber[/COLOR][/U][/EMAIL]
Simulink® and Real-Time Workshop® Embedded Coder let you simulate, automatically generate, and deploy software applications onto embedded systems. You can generate and build an entire application based solely on Simulink models. However, most software development organizations require incorporation of legacy code (i.e., existing code) into portions of their application. Examples include math utilities, filters, table lookups, and low-level device drivers. Legacy code integration is a complex problem where no single solution fits all cases. This article describes just one aspect of legacy code integration: data import and export for Simulink models.
You can import legacy code into Simulink for model simulation and code generation. The generated code calls the imported legacy code based on its function call signature and data attributes. If most of the application comprises legacy code, however, it may be easier to export the generated code into the legacy application. (For example, you can apply a delta change to a large code base, based on the new algorithm that you modeled in Simulink). When you export generated code into legacy code, the generated code must provide an appropriate call interface to the legacy code.
The following sections describe how to import and export legacy code data into Simulink using: [LIST][*][URL="http://www.mathworks.com/company/newsletters/digest/may04/legacycode.html#diagrams"][U][COLOR=#800080]Simulink diagrams[/COLOR][/U][/URL][*][URL="http://www.mathworks.com/company/newsletters/digest/may04/legacycode.html#objects"][U][COLOR=#800080]Simulink data objects[/COLOR][/U][/URL][*][URL="http://www.mathworks.com/company/newsletters/digest/may04/legacycode.html#storage"][U][COLOR=#800080]Custom storage classes[/COLOR][/U][/URL][/LIST]Note: The material and examples in this article are based on MathWorks Release 13 with Service Pack 1.
[B]Model Description[/B]

Figure 1 shows a model that demonstrates legacy code integration. The inputs represent raw data values from primary and backup input sensors. The model converts the raw sensor input values to counts and removes known sensor bias. A comparator validates the primary signal by comparing it to the backup signal. Then the primary sensor value and its validity flag are assigned to outputs.

[URL="http://www.mathworks.com/company/newsletters/digest/may04/images/sensorfault_wl.jpg"][IMG]http://www.mathworks.com/company/newsletters/digest/may04/images/sensorfault_w.gif[/IMG][/URL]
[I]Figure 1: Built-in-test sensor fault model (BIT_sensor_fault_legacy.mdl). Click on image to see enlarged view.[/I]
This example will show how to import and export data between a Simulink model and legacy code. We describe methods for importing the model's sensor inputs (volts_p , volts_b), importing the bias parameters (bias_p , bias_b), and exporting the algorithm outputs (value, validity).
[B]Using Simulink Diagrams[/B]

Simulink supports data import for signals, states, and parameters. You declare and assign storage for signals and states directly within the Simulink diagrams using signal and state properties. Parameters are assigned storage within Simulink using the tunable parameter settings. Data types for signals, states, and parameters are specified within the block that creates or references them. We will use int16 or Boolean types for this example.
Take the following four steps to import or export data for signals: [LIST=1][*]Select the signal, right-click, and choose [B]Signal properties[/B]. The [B]Signal Properties[/B] dialog box opens (Figure 2).[*]In the [B]Signal Name[/B] field, enter the signal name, using the same identifier used for the legacy data you want to import or export[*]To import legacy data, select [B]ImportedExtern[/B] or [B]ImportedExternPointer[/B] as its Real-Time Workshop storage class, depending on whether or not it is a pointer.[*]To export legacy data, select [B]ExportedGlobal[/B] as the Real-Time Workshop storage class.[/LIST][URL="http://www.mathworks.com/company/newsletters/digest/may04/images/sigproperties_wl.jpg"][IMG]http://www.mathworks.com/company/newsletters/digest/may04/images/sigproperties_w.gif[/IMG][/URL]
[I]Figure 2: Assigning data storage using the Signal Properties dialog box. Click on image to see enlarged view.[/I]


To assign storage to a state, right-click on the Simulink block to access the [B]State Properties[/B] dialog box (Figure 3). Assign the state name and Real-Time Workshop storage class as you did for signal storage.
[URL="http://www.mathworks.com/company/newsletters/digest/may04/images/stateproperties_wl.gif"][IMG]http://www.mathworks.com/company/newsletters/digest/may04/images/stateproperties_w.gif[/IMG][/URL]
[I]Figure 3: Assigning data storage using the State Properties dialog box. Click on image to see enlarged view.[/I]

To import or export data for parameters within Simulink, take the following five steps: [LIST=1][*]Select [B]Simulation Parameters… [/B]from the [B]Simulation [/B]menu. The [B]Simulation Parameters [/B]window opens.[*]Enable or check [B]Inline parameters [/B]from the [B]Advanced [/B]pane in the [B]Simulation Parameters [/B]window to inline all parameters and improve code efficiency.[*]Click the [B]Configure… [/B]button next to [B]Inline parameters[/B]. The [B]Model Parameter Configuration [/B]window opens (Figure 4).[*]Define global or tunable parameters for your model within the [B]Model Parameter Configuration [/B]window by selecting a parameter and choosing [B]Add to Table>>[/B].[*]A storage class can now be set for the parameter as shown in Figure 4.[/LIST]See "Inline Parameters" in the online Simulink documentation on the MathWorks Web site for detailed information on this topic.
[URL="http://www.mathworks.com/company/newsletters/digest/may04/images/tunable_wl.gif"][IMG]http://www.mathworks.com/company/newsletters/digest/may04/images/tunable_w.gif[/IMG][/URL]
[I]Figure 4: Assigning data storage for tunable parameter. Click on image to see enlarged view.[/I]


[B]Using Simulink Data Objects[/B]

You can use variables within the MATLAB workspace to assign storage instead of using the Simulink diagram graphical dialog boxes. To do this, create a Simulink data object that matches a signal, state, or parameter name in the model. Once matched, the named signal, state, or parameter will refer to the Simulink data object for its storage properties. Using Simulink data objects is helpful when you want to create, view, and manage your application data in a data dictionary. This approach also uncouples data from the diagrams and makes it easy to retarget a model for different hardware by simply changing the data objects in the workspace.
To integrate legacy data using Simulink data objects, take the following seven steps: [LIST=1][*]Label the signal, state, or parameter with the identifier used in the legacy code using the [B]Signal Properties[/B], [B]State Properties[/B], or [B]Block Parameters[/B] dialog boxes as described in "Using Simulink Diagrams."

Tip: When using Simulink data objects, set the Real-Time Workshop storage class to [B]Auto [/B](the default) within the [B]Signal Properties[/B], [B]State Properties[/B], or [B]Model Parameter Configuration[/B] dialog boxes. If you do not do this, you may get conflicts with the data storage specified by the Simulink data object.[*]Select Data Explorer from the Simulink [B]Tools[/B] menu or type slexplr from the MATLAB command prompt to invoke the Simulink Data Explorer.[*]Within the Simulink Data Explorer, right-click on empty space within the [B]Objects [/B]pane and select [B]New.[/B][*]For signals or states add a new object of class Simulink.Signal.[*]For parameters add a new object of class Simulink.Parameter.[*]Name the signal with the same identifier used in the corresponding [B]Signal Properties[/B], [B]State Properties[/B], or [B]Block Parameters [/B]dialog boxes.[*]Choose an appropriate storage class as shown in Figure 5.[/LIST]Tip: You can also create Simulink data objects using MATLAB commands and develop scripts to create a complete data dictionary (for example, kp = Simulink.Parameter)
[URL="http://www.mathworks.com/company/newsletters/digest/may04/images/data_objects_wl.gif"][IMG]http://www.mathworks.com/company/newsletters/digest/may04/images/data_objects_w.gif[/IMG][/URL]
[I]Figure 5: Importing data using Simulink data objects. Click on image to see enlarged view.[/I]


The simulation will now use the storage class settings specified within the Simulink Data Explorer, as will the generated code. You can generate code for these basic Simulink data objects using either Real-Time Workshop or Real-Time Workshop Embedded Coder.
[B]Using Real-Time Workshop Embedded Coder for Simulink Data Objects[/B]

This example uses code generated with Real-Time Workshop Embedded Coder. The code snippets below show how the sensor inputs (volts_p, volts_b) and bias parameters (bias_p, bias_b) are imported and how the algorithm outputs (value, validity) are exported. Note that we made one of the sensor inputs an imported pointer.
[B]BIT_sensor_fault_legacy.c[/B] #include "BIT_sensor_fault_legacy.h" #include "BIT_sensor_fault_legacy_private.h" /* Exported block signals */int16_T value; /* <Root>/Sum */boolean_T validity; /* <S2>/Relational Operator */[FONT=宋体, MS Song][B]BIT_sensor_fault_legacy.h[/B] [/FONT]extern int16_T value; /* '<Root>/Sum' */extern boolean_T validity; /* '<S2>/Relational Operator' */[FONT=宋体, MS Song][B]BIT_sensor_fault_legacy_private.h[/B] [/FONT]/* Imported (extern) block signals */extern int16_T volts_b; /* '<Root>/in2' */ /* Imported (extern) pointer block signals */extern int16_T *volts_p; /* '<Root>/in1' */ /* Imported (extern) block parameters */extern int16_T bias_b; /* Variable: bias_b * '<Root>/bb' */extern int16_T bias_p; /* Variable: bias_p * '<Root>/ ' */
[B]Using Custom Storage Classes[/B]

Software environments often need more flexibility and capabilities for specifying data storage classes than are provided by Simulink data objects. Custom storage classes let you extend Simulink data objects to produce nearly any data definition required, and include a set of pre-defined storage classes for signals, states, and parameters that include: [LIST][*]Bitfield[*]Const[*]ConstVolatile[*]Define[*]ExportToFile[*]ImportFromFile[*]Internal[*]Struct[*]Volatile[*]GetSet[/LIST]ImportFromFile and ExportToFile extend the ImportedExtern and ExportedGlobal classes used earlier. For example, ImportedExtern does not let you include a legacy header file containing declarations (extern) for the legacy data in the generated code. Instead, the code generator inserts extern declarations in modelname_private.h (see the code sample in [URL="http://www.mathworks.com/company/newsletters/digest/may04/legacycode.html#using_rtwec"][U][COLOR=#800080]Using Real-Time Workshop Embedded Coder for Simulink Data Objects[/COLOR][/U][/URL]). With ImportFromFile, you can create additional attributes for the imported data, including the specification of a legacy header file containing the imported data declarations. The code generator then includes the specified header file within modelname_private.h instead of creating the extern declarations. Using header files in this manner instead of creating externs is the integration approach preferred by many production software organizations.
To integrate legacy data using custom storage classes, take the following six steps: [LIST=1][*]Select [B]Data Explorer[/B] from the Simulink [B]Tools[/B] menu, or type slexplr from the MATLAB command prompt, to invoke the [B]Simulink Data Explorer[/B] dialog.[*]Within the [B]Simulink Data Explorer[/B], right-click on empty space within the [B]Objects [/B]pane and select [B]New[/B].[*]For signals or states add a new object of type Simulink.CustomSignal.[*]For parameters add a new object of type Simulink.CustomParameter.[*]Name the signal with the same identifier used in the corresponding [B]Signal Properties[/B], [B]State Properties[/B], or [B]Block Parameters [/B]dialog boxes.[*]Choose an appropriate storage class as shown in Figure 6.[/LIST][URL="http://www.mathworks.com/company/newsletters/digest/may04/images/storage_classes_wl.gif"][IMG]http://www.mathworks.com/company/newsletters/digest/may04/images/storage_classes_w.gif[/IMG][/URL]
[I]Figure 6: Integrating data using custom storage classes. Click on image to see enlarged view.[/I]

The code generated for this data will now use custom storage classes as specified within the Simulink Data Explorer. The inputs (volts_b and volts_p) were kept as the Simulink data objects ImportedExternPointer and ImportedExtern. The parameters (bias_b and bias_p) were changed to ImportFromFile and assigned to file tunables.h. The outputs (value and validity) were also changed to ExportToFile and assigned to file sensor_outputs.h.


[B]Using Real-Time Workshop Embedded Coder for Custom Storage Classes[/B]

Generating code for custom storage classes requires the Real-Time Workshop Embedded Coder. Real-Time Workshop will generate code for this model, but it ignores the custom storage classes and uses default storage. The following code sample illustrates the main difference between the previously listed code (using Simulink data objects) and the new code using custom storage classes.
[B]sensor_outputs.h[/B] extern int16_T value; /* '<Root>/Sum' */extern boolean_T validity; /* '<S2>/Relational Operator' */[FONT=宋体, MS Song][B]BIT_sensor_fault_legacy_private.h[/B] [/FONT]/* Includes for objects with custom storage classes. */
#include "tunables.h"

/* Imported (extern) block signals */ extern int16_T volts_b; /* '<Root>/in2' */ /* Imported (extern) pointer block signals */extern int16_T *volts_p; /* '<Root>/in1' */
Note that the declarations for the outputs (value and validity) now appear in a newly generated file sensor_ouputs.h instead of modelname.h. Also note that the code for the bias parameters in modelname_private.h is no longer declared directly but is replaced by the statement #include "tunables.h".
[B]Viewing Signal Storage within Simulink[/B]

Now that you have set the appropriate storage and generated code that will integrate with your legacy code, you may want to generate a report or print diagrams for a detailed design review. You can display the storage class settings on the Simulink diagrams by selecting the [B]Storage class[/B] option under the [B]Format[/B] menu within Simulink (see Figure 7).
[URL="http://www.mathworks.com/company/newsletters/digest/may04/images/view_classes_wl.gif"][IMG]http://www.mathworks.com/company/newsletters/digest/may04/images/view_classes_w.gif[/IMG][/URL]
[I]Figure 7: Viewing storage classes within Simulink. Click on image to see enlarged view.[/I]


For more information, see the Simulink product documentation on the MathWorks customer support site: [URL="http://www.mathworks.com/support"][U][COLOR=#0000ff]http://www.mathworks.com/support/[/COLOR][/U][/URL]
You can also contact the authors with additional comments or questions, [EMAIL="[email protected]"][U][COLOR=#0000ff][email protected][/COLOR][/U][/EMAIL] or [EMAIL="[email protected]"][U][COLOR=#0000ff][email protected][/COLOR][/U][/EMAIL].

[URL="http://www.mathworks.com/company/newsletters/digest/may04/legacycode.html"]更多...[/URL]


所有时间均为北京时间。现在的时间是 00:21

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.