1 Getting Started - MapleSim Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : MapleSim Toolboxes : MapleSim Connector for VI-CarRealTime : Getting Started Guide : MapleSimCRTConnectorGSG/Chapter01

Contents     Previous     Next     Index

1 Getting Started

1.1 VI-CarRealTime ANSI-C Code Generation Steps

This chapter describes how to use the MapleSim Connector for VI-CarRealTime and, in the Example: Full Powertrain Model section of this chapter, provides a step by step example on how to generate the C code. The MapleSim Connector for VI-CarRealTime template consists of the following steps for generating C code and is described in Using the Template:

1. 

Subsystem selection

2. 

Inputs/Outputs and parameter management

3. 

C code generation options

4. 

Generate plugin solver code

5. 

View generated C code

CRTConnector package

The CRTConnector package is a collection of procedures for manually generating and compiling VI-grade's ANSI-C code from MapleSim models, based on the model's algebraic equations and Dynamic System objects.

For information about the CRTConnector package, enter ?CRTConnector at a prompt in a Maple worksheet.

1.2 Opening the VI-CarRealTime Plugin Solver Generation Template

To open the VI-CarRealTime Plugin Solver Generation template

1. 

With your model open in MapleSim, click Templates ( ) in the main toolbar and select the VI-CarRealTime Plugin Solver Generation template.

2. 

In the Attachment field, provide a worksheet name.

3. 

Click Create Attachment.

The VI-CarRealTime Plugin Solver Generation template opens in a Maple worksheet. Your MapleSim model is displayed in the Subsystem Selection window. The Main drop-down list in the toolbar shows all of the subsystems in your model.

1.3 Using the Template

The MapleSim Connector for VI-CarRealTime provides a VI-CarRealTime Plugin Solver Generation template in the form of a Maple worksheet for manipulating and exporting MapleSim subsystems. This template contains pre-built embedded components that allow you to generate C code from a MapleSim subsystem.

With this template, you can define inputs and outputs for the system, set the level of code optimization, generate the source code, and choose the format of the resulting C code and library code. You can use any Maple commands to perform task analysis, assign model equations to a variable, group inputs and outputs, and define additional input and output ports for variables.

Note: C code generation now handles all systems modeled in MapleSim, including hybrid systems with defined signal input (RealInput) and signal output (RealOutput) ports.

Example models are available in the VI-CarRealTime Examples palette in MapleSim.

Step 1: Subsystem Selection

This part of the template identifies the subsystem modeling components that you want to generate C code for. Since VI-CarRealTime only supports data signals, properties on acausal connectors such as mechanical flanges and electrical pins, must be converted to signals using the appropriate ports.

To connect a subsystem to modeling components outside of its boundary, you add subsystem ports to your model. A subsystem port is an extension of a component port in your subsystem. The resulting signals can then be directed as inputs and outputs for the C code files. By creating a subsystem you not only improve the visual layout of a system in model workspace but you also prepare the model for export.

Note: For connectors you must use signal components since acausal connectors cannot be converted to a signal.

You can select which subsystems from your model you want to create C code for.

To select a subsystem

1. 

From Main, select a subsystem in your model.

 

2. 

Select your version of VI-CarRealTime from the VI-CarRealTime Version list.

3. 

Click Load Selected Subsystem.

The subsystem appears in the Subsystem Selection window. All defined input and output ports are loaded.

Step 2: Inputs/Outputs and Parameter Management

The Port and Parameter Management interface lets you customize, define, and assign parameter values to specific ports. Subsystem components to which you assign the parameter inherit a parameter value defined at the subsystem level.

Select either one of or both of the Generate external file for assigning parameters and Generate external file for assigning initial conditions options to generate external files for assigning parameters and initial conditions. When selected, a .params file (for assigning external parameters) and an .ics file (for assigning initial conditions) are generated along with your C code. These files can be edited before running your model on VI-CarRealTime to see how different parameters and initial conditions affect your model without having to regenerate the C code for your model.

 

 

After the subsystem is loaded you can group individual input and output variable elements into a vector array. Input ports can include variable derivatives.

Note: If the parameters are not marked for export they will be numerically substituted.

 

 

Step 3: C Code Generation Options

The C code Generation Options settings specify the advanced options for the code generation process.

Solver Options

Select the fixed step solver by specifying the numerical solution method for the model equations during the code generation process.

Select one of the following options:

Euler: forward Euler method

RK2: second-order Runge-Kutta method

RK3: third-order Runge-Kutta method

RK4: fourth-order Runge-Kutta method

Implicit Euler: implicit Euler method

 

 

Optimization Options

Set the level of code optimization to specify whether equations are left in their implicit form or converted to an ordinary differential equation (ODE) system during the code generation process. This option specifies the degree of simplification applied to the model equations during the code generation process and eliminates redundant variables and equations in the system.

Select one of the following options:

None (0): performs no optimization; the default equations are used in the generated code.

Partial (1, 2): removes redundant equations from the system.

Full (3): performs index reduction to reduce the system to an ODE system or a differential algebraic equation (DAE) system of index 1, and removes redundant equations.

Constraint Handling Options

The Constraint Handling Options specify whether the constraints are satisfied in a DAE system by using constraint projection in the generated C code. Use this option to improve the accuracy of a DAE system that has constraints. If the constraint is not satisfied, the system result may deviate from the actual solution and could lead to an increase in error at an exponential rate.

Set the Maximum number of projection iterations to specify the maximum number of times that a projection is permitted to iterate to obtain a more accurate solution.

Set the Error tolerance to specify the desirable error tolerance to achieve after the projection.

Select Apply projection during event iterations to interpolate iterations to obtain a more accurate solution.

Constraint projection is performed using the constraint projection routine in the External Model Interface as described on The MathWorks™ web site to control the drift in the result of the DAE system.

Event Handling Options

The Event Handling Options specify whether the events are satisfied in a DAE system by using event projection in the generated C code. Use this option to improve the accuracy of a DAE system with events. If the constraint is not satisfied, the system result may deviate from the actual solution and could lead to an increase in error at an exponential rate.

Set the Maximum number of event iterations to specify the maximum number of times that a projection is permitted to iterate to obtain a more accurate solution.

Set the Width of event hysteresis band to specify the desirable error tolerance to achieve after the projection.

Event projection is performed using the event projection routine in the External Model Interface as described on The MathWorks™ web site to control the drift in the result of the DAE system.

 

Baumgarte Constraint Stabilization

Select Apply Baumgarte constraint stabilization in order to apply Baumgarte constraint stabilization to your model. When selected, you can enter values for the derivative gain (Alpha) and the proportional gain (Beta) that are appropriate for your model.

Select Export Baumgarte parameters to add Alpha and Beta as parameters in the generated plugin solver code for your model. This allows you to change the values of Alpha and Beta when using your plugin solver.

 

Baserate

The baserate specifies the rate at which the model runs (in seconds). Enter the value for the baserate in The rate at which the model runs. Use this option to improve the accuracy of a DAE system with events. If the constraint is not satisfied, the system result may deviate from the actual solution and could lead to an increase in error at an exponential rate. Default is [0.001].

If your baserate is smaller than the step size used in your VI-CarRealTime simulation, you must specify a value in Number of internal steps so that:

(model baserate) ⋅ (number of internal steps) = VI-CarRealTime step size

Step 4: Generate Plugin Solver Code

Generating the plugin solver code creates temporary files for viewing purposes in a user defined directory.

 

To generate plugin solver code

1. 

Provide the following information for the location and name of the generated code:

• 

Target directory: Browse to or create the location for the generated C code files.

• 

VI-Grade CarRealTime installation directory: Browse to the installation directory for VI-CarRealTime.

• 

Visual C++ directory: Browse to the location of the Visual C++ directory on your computer.

• 

Model Name: Provide a name for the generated C code folder. This folder is a subdirectory of Target directory. Within this folder three files are generated: the VI-CarRealTime interface C code, cMsimModel.h, and a batch file to compile the source code.

2. 

Select either 32-bit or 64-bit for Target binary, depending on the version of VI-CarRealTime you have installed.

3. 

To generate the plugin solver code, click Generate Plugin Solver Code. The C code for the plugin solver is saved in the C code folder.

4. 

To generate and compile the plugin solver code, click Generate and Compile Plugin Solver Code. In addition to the C source code files, object files and a library file (.dll) are created and saved in the C code folder.

Step 5: View Generated C Code

Once the C code is generated, specific portions of the C code can be viewed:

VI-CarRealTime Interface C Code: Displays the code for implementation of the MapleSim Connector for VI-CarRealTime.

MapleSim model: cMsimModel.c: Displays the code for implementation of the MapleSim model.

1.4 Viewing Examples

Within MapleSim there are some examples for you to view.

To view an example

1. 

Under the Libraries tab on the left side of the MapleSim window, expand the VI-CarRealTime Examples palette, and then click the entry for the model that you want to view.

Note: Some models include additional documents, such as templates that display model equations or define custom components.

2. 

Under the Project tab, expand the Attachments palette and then expand Documents. You can open any of these documents by right-clicking its entry in the list and clicking View. After you add a template to a model, it will be available from this list.

1.5 Example: Full Powertrain Model

In this example, you will generate C code for a simple powertrain model created in MapleSim.

To generate C code

1. 

From the VI-CarRealTime Examples palette, click the Full Powertrain example.

2. 

Click Templates ( ) in the main toolbar. The Create Attachment for FullPowertrain window appears.

3. 

From the template list, select the VI-CarRealTime Plugin Solver Generation template.

4. 

In the Attachment field, enter Full Powertrain as the worksheet name.

5. 

Click Create Attachment. Your MapleSim model opens in Maple, using the selected template.

6. 

Select the FullPowertrain1 subsystem from the Main drop-down list in the toolbar above the model diagram.

7. 

Select your version of VI-CarRealTime from the VI-CarRealTime Version list.

8. 

Click Load Selected Subsystem. All of the template fields are populated with information specific to the subsystem displayed in the model diagram. You can now specify which subsystem parameters will be kept as configurable parameters in the generated block.

9. 

In the C Code Generation Options > Optimization Options section, set Level of code optimization to Full (3). This option specifies the degree of simplification applied to the model equations during the code generation process, and eliminates redundant variables and equations in the system.

10. 

In the Generate Plugin Solver Code section of the template, specify the Target directory, the VI-Grade CarRealTime installation directory, the Visual C++ directory, and the Model Name.

11. 

Select either 32-Bit or 64-bit from the Target binary list.

12. 

Click Generate Plugin Solver Code. The files are created and saved in the C code folder.

Note: Generating a block may require a few minutes.

 

Contents     Previous     Next     Index


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam