2 Example: Exporting a Model as a LabVIEW EMI Block - MapleSim Help

2 Example: Exporting a Model as a LabVIEW EMI Block

2.1 Preparing a Model for Export

In this example, you will perform the steps required to prepare a slider-crank mechanism model and export it as a LabVIEW EMI block.

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

2. Define subsystem inputs and outputs.

3. Define and assign subsystem parameters.

4. Export the model using the LabVIEW EMI Block Generation template.

5. Implement the EMI block in LabVIEW.

To open the slider-crank mechanism example:

 2 Select Examples > User's Guide Examples > Chapter 6, and then select Planar Slider-Crank Mechanism.

 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 export. In this example, you will group all of the components into a subsystem. To convert the model to a subsystem: 1. Draw a box around all of the components in the model by dragging your mouse over them. 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 LabVIEW 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 EMI block.

Note: Currently, code generation is limited to subsystems with defined signal input (RealInput) and signal output (RealOutput) ports.

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 define subsystem inputs and outputs:

 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 In the Library Components 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 Mac®) 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 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 and then click the boundary once. A real output port is added to your subsystem.

 12 Add another Absolute Translation component above the Connecting Rod subsystem.
 13 Right-click (Control-click for Mac) the Absolute Translation component and select Flip Vertical.  Right-click the Absolute Translation component again and select Rotate Clockwise.
 14 Add a Real Demultiplexer component to the right of the sensor and connect the components as shown below. Since the crank is moving in the x, y plane, you only need to output the first two signals. You are now ready to add a real input port to your subsystem to control the torque on the crank shaft.

 15 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.
 16 Connect the white flange of the Torque component to the white flange of the leftmost Revolute Joint.

 17 Click the input port of the Torque component, then drag your pointer to the subsystem boundary and click the boundary once. A real input port is added to your subsystem.

The complete subsystem appears below.

 18 Click Main ( ) in the Model Workspace toolbar to browse to the top level of the model.
 19 From the Signal Blocks > Sources > Real menu, drag a Constant source into the Model Workspace and connect its output port to the input port of the SliderCrank subsystem as shown below.

 20 Click Attach Probe ( ) above the Model Workspace toolbar and then click the top output port of the SliderCrank subsystem.
 21 In the Model Workspace, click the probe once to position it.
 22 In the same way, add probes to the other SliderCrank output ports as shown below.

2.2 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 Double-click the SliderCrank component on the Model Workspace to see the detailed view of the SliderCrank subsystem, and then click Parameters ( ) in the Model Workspace toolbar. The parameter editor appears.
 2 In the first Name field, type CrankL and press Enter.
 3 Specify a default value of 1 and enter Crank length 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 Connecting Rod Length as the description.

 6 Click Diagram View ( ) to switch to the diagram view, and then click Main ( ).
 7 Select the SliderCrank subsystem. The parameters are defined in the Properties tab ( ).

 8 Double-click the SliderCrank subsystem, and then select the Crank subsystem.
 9 In the Properties tab ( ), change the length value (L) to CrankL. The Crank subsystem now inherits the numeric value of CrankL that you defined.

 10 Select the ConnectingRod subsystem and change its length value to ConRodL.

 11 Click Main ( ) in the Model Workspace 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 an EMI block.

2.3 Exporting Your Model Using the LabVIEW EMI Block Generation Template

After preparing the model, you can use the LabVIEW EMI Block Generation template to set export options and convert the model to a LabVIEW EMI block.

To generate an EMI file:

 1 Select the Add Apps or Templates tab ( ).
 2 Double-click on the NI LabVIEW EMI Component Block Generation entry in the Templates palette.
 3 Enter Slider Crank EMI as the worksheet name, and click Create Attachment ( ). The slider-crank subsystem is opened in the LabVIEW EMI Block Generation Template in Maple.
 4 Use the navigation controls above the model diagram to select the SliderCrank subsystem and then click Load Selected System. All of the template fields are populated with information specific to the subsystem.
 5 Click Generate to LabVIEW to generate the block.
 6 Set the LabVIEW and Visual C++ directory paths.
 7 At the bottom of the template, click Generate and Compile EMI Component to LabVIEW to generate the Visual Studio project and dynamic-link library (.dll) file for the EMI block.
 8 In LabVIEW, open a new VI and open the block diagram window by selecting Windows > Show Block Diagram.
 9 Right-click the drawing canvas and select Control Design & Simulation > Simulations > Control and Simulation Loop. Click the canvas and draw a simulation loop box.
 10 Right-click the simulation loop box and select Control Design & Simulation > Simulations > Utilities > External Model.  Click the simulation loop box to position the model.
 11 In the Select an External Model Library window, browse to the Release subfolder located in the default directory that you specified in the LabVIEW EMI Block Generation template and open the .dll file that you generated.
 12 Click OK.
 13 Connect the output of the block to a scope and the input to a sine wave.