2 Exporting a Model and Generating C Code - MapleSim Help

2 Exporting a Model and Generating C Code

This chapter describes how to prepare a model for generating C code using subsystems.

2.1 Preparing the 2-D Rigid Slider Crank Model for Generating C Code Using Subsystems

Preparing a Model

In this example, you will perform the steps required to prepare a slider-crank mechanism model for generating C code.

1. Convert the slider-crank mechanism model to a subsystem.

2. Define subsystem inputs and outputs.

3. Define and assign subsystem parameters.

4. Generate C code.

To open the slider-crank mechanism example

 1 In MapleSim, under the Libraries tab, browse to the Examples > User's Guide Examples menu.
 2 Open the Planar Slider-Crank Mechanism example in Chapter 6. The example appears in the Model Workspace.

Converting the Model to a Subsystem

By converting your entire model or part of your model into a subsystem, you identify which parts of the model that you want to generate C code for. In this example, you will group all of the components into a subsystem.

To create a subsystem

 1 Using the selection tool ( ) located in the Model Workspace Toolbar, draw a box around all of the components in the model.

 2 From the Edit menu, select Create Subsystem.
 3 In the Create Subsystem dialog box, enter SliderCrank as the subsystem name.
 4 Click OK. A SliderCrank subsystem block appears in the Model Workspace.

Defining Subsystem Inputs and Outputs

MapleSim uses a topological representation to connect interrelated components without having to consider how signals flow between them, whereas traditional signal-flow modeling tools require explicitly defined system inputs and outputs.  Since the MapleSim Connector for B&R Automation Studio only supports data signals, properties on acausal ports, such as mechanical flanges and electrical pins, must be converted to signals using the appropriate components. The resulting signals are directed as inputs and outputs for the subsystem in MapleSim and for the generated C code.

In this example, you will convert the displacements of the slider and the joint between the crank and connecting rod to output signals. The input signal needs to be converted to a torque that is applied to the revolute joint that represents the crank shaft.

To create a subsystem output port

 1 Double-click the subsystem block to view its contents. The broken line surrounding the components indicates the subsystem boundary, which can be resized by clicking and dragging its sizing handles.
 2 Delete the probes that are attached to the model.
 3 Under the Libraries tab on the left side of the MapleSim window, expand the Multibody palette and then expand the Sensors submenu.
 4 Drag the Absolute Translation component to the Model Workspace and place it below the Prismatic Joint component.

 5 Right-click (Control-click for Macintosh®) the Absolute Translation component and select Rotate Counterclockwise.
 6 From the Signal Blocks > Routing > Demultiplexers menu, drag a Real Demultiplexer component to the Model Workspace and place it to the right of the Absolute Translation component.

 7 To connect the Absolute Translation component to the model, click the frame_b connector. The frame is highlighted in green when you hover your pointer over it.

 8 Draw a vertical line and click the connection line directly above the component. The sensor is connected to the rest of the diagram.

 9 In the same way, connect the r output port ($\mathrm{TMOutputP}$) of the Absolute Translation component to the navy blue input port of the demultiplexer. This is the displacement signal from the sensor in x, y, and z coordinates. Since the slider only moves along the x axis, the first coordinate must be an output signal.

 10 Hover your pointer over the first demultiplexer port and click your mouse button once.

 11 Drag your pointer to the subsystem boundary.

 12 Click the boundary once. A real output port is added to your subsystem.

 13 Add another Absolute Translation component above the Connecting Rod subsystem.
 14 Right-click (Control-click for Macintosh) the Absolute Translation component and select Flip Vertical.  Right-click the Absolute Translation component again and select Rotate Clockwise.
 15 Add a Real Demultiplexer component to the right of the sensor and connect the components as shown below.

Note: Since the crank is moving in the x-y plane, you only need to output the first two signals.

You will now add a real input port to your subsystem to control the torque on the crank shaft.

 16 From the 1-D Mechanical > Rotational > Torque Drivers menu, add a Torque component to the Model Workspace and place it above the Fixed Frame component.
 17 Connect the white flange of the Torque component to the white flange of the leftmost Revolute Joint.

 18 Click the input port of the Torque component and drag your pointer to the subsystem boundary.

 19 Click the boundary once. A real input port is added to your subsystem.

The complete subsystem appears below.

 20 Click Main in the Model Workspace Toolbar to browse to the top level of the model.
 21 From the Signal Blocks > Sources > Real menu, drag a Constant source into the Nonlinear Spring Damper and connect its output port to the input port of the SliderCrank subsystem as shown below.

 22 Click Attach Probe ( ) in the Model Workspace Toolbar.
 23 Click the top output port of the SliderCrank subsystem.
 24 Drag the probe to an empty location on the Model Workspace, and then click the workspace to position the probe.
 25 In the same way, add probes to the other SliderCrank output ports as shown below.

Defining and Assigning Subsystem Parameters

You can define custom parameters that can be used in expressions in your model to edit values more easily. To do so, you define a parameter with a numeric value in the parameter editor. You can then assign that parameter as a variable to the parameters of other components; those individual components will then inherit the numeric value of the parameter defined in the parameter editor. By using this approach, you only need to change the value in the parameter editor to change the parameter values for multiple components.

To edit parameters

 1 While in the detailed view of the SliderCrank subsystem, click Parameters ( ) in the Navigation Toolbar. The parameter editor appears.
 2 In the New Parameter field, define a parameter called CrankL and press Enter.
 3 Specify a default value of 1 and enter Length of the crank as the description.
 4 In the second row of the table, define a parameter called ConRodL and press Enter.
 5 Specify a default value of 2 and enter Length of the connecting rod as the description.

 6 Click Diagram ( ) to switch to the diagram view. The parameters are defined in the Parameters pane.

 7 In the Model Workspace, select the Crank subsystem.
 8 In the Parameters pane, change the length value (L) to CrankL. The Crank subsystem now inherits the numeric value of CrankL that you defined.

 9 Click SliderCrank1 in the Navigation Toolbar above the workspace to switch back to the SliderCrank view, and then select the ConnectingRod subsystem.
 10 Change its length value to ConRodL.
 11 Click Main in the Navigation Toolbar to navigate to the top level of the model.

You will include these parameter values in the model that you export. You are now ready to convert your model to C code.

Generating C Code for the Template

After preparing the model, you can use the MapleSim Connector for B&R Automation Studio file Generation template to set the C code generation options for generation.

To generate C code

 1 Click Templates ( ) in the Main Toolbar.
 2 The Create Attachment window appears.  From the list, select B&R Program Object Generation.
 3 In the Attachment name field, enter Slider Crank B&R as the worksheet name and click Create Attachment. The slider-crank model opens in the Subsystem Selection window in the B&R Connector Template.
 4 In the Subsystem Selection section, select the SliderCrank1 subsystem from the drop-down menu and click Load Selected Subsystem. All of the template fields are populated with information specific to the subsystem.
 5 Specify the location for the Target Directory and provide a name for the generated files.
 6 Click Generate B&R Program.  The files are created and saved in the target directory.

2.2 Generating C Code for the Nonlinear Spring Damper Model

Preparing a Model

In this example, you will perform the steps required to generate C code using the Nonlinear Spring Damper model.

 1 Open the Nonlinear Spring Damper example.
 2 Generate the B&R template.
 3 Define template settings.
 4 Generate C code and view files.

To open the Nonlinear Spring Damper example

 1 Under the Libraries tab, expand the B&R Connector Examples palette.
 2 Open the Nonlinear Spring Damper example.

To generate the MapleSim Connector for B&R Automation Studio template

 1 From the B&R Connector Examples palette, open the Nonlinear Spring Damper example.
 2 Click Templates ( ) in the Main Toolbar. The Create Attachment window appears.

 3 From the list, select the B&R Program Object Generation template.
 4 In the Attachment field, enter Nonlinear Spring Damper as the worksheet name.
 5 Click Create Attachment. Your MapleSim model opens in Maple, using the selected template.

To define the template settings

 1 Browse to the Nonlinear Spring Damper subsystem by selecting the subsystem name from the drop-down menu in the Navigation Toolbar above the model diagram. This menu displays all of the subsystems and components in your MapleSim model.

 2 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 to keep as configurable parameters in the generated block.
 3 In the C Code Generation Options section, set the following options:

C Code Generation Options

Setting

Solver Options

 • Fixed step solver

Euler

Optimization Options

 • Level of code optimization (0=None, 3=Full):

3

Constraint Handling Options

 • Maximum number of projection iterations
 • Error tolerance
 • Apply projection during event iterations

3

$\mathbf{0.1}{\mathbf{ⅇ}}^{\mathbf{-}\mathbf{4}}$

Apply projection during event iterations (clear check box)

Event Handling Options

 • Maximum number of event iterations
 • Width of event hysteresis band

10

$\mathbf{0.1}{\mathbf{ⅇ}}^{\mathbf{-}\mathbf{9}}$

Baserate

 • The rate at which the model runs

$\mathbf{0.1}{\mathbf{ⅇ}}^{\mathbf{-}\mathbf{1}}$

Inputs

 • Specify whether inputs are defined internally

Internal (clear check box)

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

To generate C code

 1 Click Generate B&R Program.  The files are created and saved in the target directory.  You can view the three generated files (B & R Automation Studio Program Object, ANSIC.prg, and Local Variables .var) in the View C Code section of the template.

Note: Generating a block may require a few minutes.