1 Getting Started - MapleSim Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : MapleSim Toolboxes : MapleSim B&R Connector : Getting Started Guide : MapleSimBRConnectorGSG/Chapter01

Contents     Previous     Next     Index

1 Getting Started

1.1 B&R ANSI-C Code Generation Steps

This chapter describes how to use the MapleSim Connector for B&R Automation Studio template and in the Example: RLC Circuit Model section of this chapter, a step by step example shows you how to generate the C code.  The MapleSim Connector for B&R Automation Studio template consists of the following steps in generating C code and is described in Using the B&R Generation Template:

1. 

Subsystem selection

2. 

Inputs/Outputs and parameter management

3. 

C code generation options

4. 

Generate C code

5. 

View generated C code

MapleSim Connector for B&R Automation Studio Package

The BRConnector package is a collection of procedures for manually generating and compiling Automation Studio's ANSI-C code from MapleSim models, based on the model's algebraic equations and DynamicSystems objects.

For information about the MapleSim Connector for B&R Automation Studio package, in Maple, enter ?BRConnector at a prompt in a worksheet.

1.2 Opening the B&R Template

To open the B&R template

1. 

Click Templates ( ) in the Main Toolbar and select the B&R Program Object Creation template.

2. 

In the Attachment field, provide a worksheet name and click Create Attachment. Your MapleSim model opens in a Maple worksheet in the Subsystem Selection window.  The toolbar shows all of the subsystems.

3. 

From the drop-down list select a subsystem.  The subsystem and its contents appears in the Subsystem Selection window.

1.3 Using the B&R Generation Template

The MapleSim Connector for B&R Automation Studio provides a B&R Generation template in the form of a Maple worksheet for manipulating and exporting MapleSim subsystems. This template contain 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 B&R Connector 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 the B&R Automation Studio 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. The example in Chapter 2 shows you how to group all of the components into a subsystem.

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.

Load Selected Subsystem

After selecting a subsystem, click Load Selected Subsystem.  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  Add an additional output port for subsystem state variables to add extra output ports for the state variables.

 

After the subsystem is loaded you can group individual input and output variable elements into a vector array, and add additional input and output ports for customized parameter values.  Input ports can include variable derivatives, and output ports can include subsystem state variables.

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

Toggle Export Column

Use the Toggle Button to select or deselect all of the Output Variables or Parameters for export. In the following illustration, by default, no Output Variables are selected.

Click Toggle Export Column to select all of the Output Variables for export.

Note: Click individual Output Variables or Parameters to select specific items for export.

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): no optimization is performed; the default equations will be 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 specifies 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 specifies 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

The Baumgarte constraint stabilization method stabilizes the position constraint equations, by combining the position, velocity, and acceleration constraints into a single expression. By integrating the linear equation in terms of the acceleration, the Baumgarte parameters, alpha and beta, act to stabilize the constraints at the position level.

Baumgarte: Apply the Baumgarte constraint stabilization.

Export Baumgarte parameters: Add Alpha and Beta as parameters in the generated code.

Alpha: Set the derivative gain for Baumgarte constraint stabilization.

Beta: Set the proportional gain for Baumgarte constraint stabilization.

Baserate

The Baserate specifies 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].

Inputs

Select Internal to specify whether the inputs are internally defined.

Step 4: Generate B&R Program Object

Generating C code creates temporary files for viewing purposes in a user defined directory.

To generate C 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.

Program Name: Provide a name for the generated C code folder.  Within this folder three files are generated; B&R Automation Studio Program Object, ANSIC.prg and Local Variables .var.

2. 

To generate the C code click Generate B&R Program.  The C code is saved to the target directory.

Step 5: View C Code

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

B&R Automation Studio Program Object: Displays the code for implementation of the MapleSim Connector for B&R Automation Studio program

ANSIC.prg: Displays the MapleSim Connector for B&R Automation Studio program definition file

Local Variables .var: Displays the local variables

1.4 Viewing Examples

Within MapleSim there are many examples for you to view.

To view an example

1. 

In the Libraries tab on the left side of the MapleSim window, expand the B&R Connector Examples palette, and 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. 

In 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: RLC Circuit Model

In this example, you will generate C code for an RLC circuit model created in MapleSim.

To generate C code

1. 

Under the Libraries tab, open the B&R Connector Examples palette, and select the RLC Parallel Circuit example.

2. 

Click Templates ( ) in the Main Toolbar. The Create Attachment for RLCcircuit window appears.

3. 

From the list, select the B&R Program Object Generation template.

4. 

In the Attachment field, enter RLC Circuit as the worksheet name.

5. 

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

6. 

In the Subsystem Selection section, select the RLC subsystem from the drop-down menu in the toolbar above the model diagram. This menu displays all of the subsystems and components in your MapleSim model.

7. 

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.

8. 

In the C Code Generation Options section, set the Optimization Options 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.

9. 

In the Generate B&R Program Object section of the template, specify the target directory and name.

10. 

Click Generate B&R Program.  The files are created and saved in the target directory.

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