Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
游客
帖子: n/a
|
![]()
Legacy Code Integration Using Simulink and Real-Time Workshop Embedded Coder
by Tom Erkkinen and Eric Dillaber 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: Note: The material and examples in this article are based on MathWorks Release 13 with Service Pack 1. Model Description 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. ![]() Figure 1: Built-in-test sensor fault model (BIT_sensor_fault_legacy.mdl). Click on image to see enlarged view. 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). Using Simulink Diagrams 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:
![]() Figure 2: Assigning data storage using the Signal Properties dialog box. Click on image to see enlarged view. To assign storage to a state, right-click on the Simulink block to access the State Properties dialog box (Figure 3). Assign the state name and Real-Time Workshop storage class as you did for signal storage. ![]() Figure 3: Assigning data storage using the State Properties dialog box. Click on image to see enlarged view. To import or export data for parameters within Simulink, take the following five steps:
![]() Figure 4: Assigning data storage for tunable parameter. Click on image to see enlarged view. Using Simulink Data Objects 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:
![]() Figure 5: Importing data using Simulink data objects. Click on image to see enlarged view. 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. Using Real-Time Workshop Embedded Coder for Simulink Data Objects 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. BIT_sensor_fault_legacy.c #include "BIT_sensor_fault_legacy.h" #include "BIT_sensor_fault_legacy_private.h" /* Exported block signals */int16_T value; /* Using Custom Storage Classes 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:
To integrate legacy data using custom storage classes, take the following six steps:
![]() Figure 6: Integrating data using custom storage classes. Click on image to see enlarged view. 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. Using Real-Time Workshop Embedded Coder for Custom Storage Classes 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. sensor_outputs.h extern int16_T value; /* ' #include "tunables.h" … /* Imported (extern) block signals */ extern int16_T volts_b; /* ' 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". Viewing Signal Storage within Simulink 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 Storage class option under the Format menu within Simulink (see Figure 7). ![]() Figure 7: Viewing storage classes within Simulink. Click on image to see enlarged view. For more information, see the Simulink product documentation on the MathWorks customer support site: http://www.mathworks.com/support/ You can also contact the authors with additional comments or questions, [email protected] or [email protected]. 更多... |
![]() |