MapleSim Connector Getting Started Guide
MapleSim Connector Getting Started Guide
Maplesoft, Maple, and MapleSim are all trademarks of Waterloo Maple Inc.
Macintosh is a trademark of Apple Inc., registered in the U.S. and other countries.
Microsoft, Windows, and Visual Studio are registered trademarks of Microsoft Corporation.
iii
Introduction
The MapleSim™ Connector provides all of the tools you need to prepare and export your dynamic systems models to
Simulink® as S-function blocks. You can create a model in MapleSim, simplify it in Maple™ by using an extensive
range of analytical tools, and then generate an S-function block that you can incorporate into your Simulink® toolchain.
You can also use these tools for exporting mathematical models that you have created from first principles in Maple
as S-functions.
Furthermore, various options allow you to use the C code generation feature in Maple to create code libraries of your
MapleSim models for implementation in other applications.
Requirements
Requires MATLAB® and Simulink®. For details on supported platforms, visit the Maplesoft System Requirements
website at http://www.maplesoft.com/products/system_requirements.aspx.
For installation instructions and system requirements, see the Install.html file on the product disc.
Note: This is the directory that contains the .dll file (Windows) or the .so file (Linux or Macintosh) that is used in
your model.
2. Add the library directory found in step 1 to the appropriate environment variable for your operating system.
• For Windows, add the library directory to your PATH environment variable.
• For Linux and Macintosh, add the library directory to your LD_LIBRARY_PATH environment variable.
iv
Introduction
Consult the help for your operating system for instructions on how to edit these environment variables.
3. Restart your computer.
1 Getting Started
1.1 Setting Up the MapleSim Connector
To generate an S-function block and have Maple communicate with MATLAB® you have to establish a connection
with MATLAB®.
>
3. A MATLAB® command window opens and the connection is established. If the window does not open, follow the
instructions in the Matlab/setup help page in the Maple help system to configure the connection.
4. Next, set up the MATLAB® mex compiler. Go to the MATLAB® command window and enter the following setup
command:
mex -setup
5. Follow the instructions to choose a local C compiler that supports ANSI (American National Standards Institute)
C code. See the MapleSimConnector,setup help page for more information.
Using this app, you can define inputs and outputs for the system, set the level of code optimization, choose the format
of the resulting S-function, and generate the source code, library code, block script, or Simulink® block. You can use
any Maple commands to perform task analysis, assign model equations to a variable, group inputs and outputs to a
single vector and define additional input and output ports for variables.
Note: Code generation now handles all systems modeled in MapleSim, including hybrid systems with defined signal
input (RealInput) and signal output (RealOutput) ports.
1
2 • Getting Started
Subsystem Preparation
Convert your model or part of your model into a subsystem. This identifies the set of modeling components that you
want to export as a block component. Since Simulink® 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. 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 MapleSim™ Connector App.
Note: For connectors you must use signal components, since acausal connectors can not be converted to a signal.
By creating a subsystem you not only improve the visual layout of a system in Model Workspace but also prepare
the model for export. The following examples in this section show you how to group all of the components into a
subsystem.
Subsystem Selection
You can select which subsystems from your model you want to export to a Simulink® block. After a subsystem is se-
lected, click Load Selected Subsystem. All defined input and output ports are loaded.
Note: If the parameters are not marked for export they will be numerically substituted.
The following selections specify the input ports, output ports, and states for generating Simulink® blocks.
Select Group all inputs into a single vector to create a single 'vector' input port for all of the input signals instead of
individual ports. The order of the inputs are the same as given in the S-function mask window.
Select Group inputs into a bus to have the S-function accept a bus signal as input.
Select Add additional inputs for required input variable derivatives to specify calculated derivative values instead
of numerical approximations.
3 • Getting Started
Select Group all outputs into a single vector to define outputs as an S-Function 'mask'.
Select Group outputs into a bus to have the S-function return a bus signal output.
Select Add an additional output port for subsystem state variables to add extra output ports for the state variables.
Select Group all parameters into a single vector to create a single parameter 'vector' for all of the parameters in the
S-function. If not selected, the S-function mask will contain one parameter input box for each of the S-function para-
meters.
Select Generate m-script for assigning parameters to generate an initialization m-file with the system parameters.
Select Group all parameters into a nested structure to specify the parameters as a nested MATLAB structure. Use
the textbox to define the name of the top-level parameter structure. Selecting this option will automatically disable the
Generate m-script for assigning parameters option.
Set the Max 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.
Set the Max 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.
Export Baumgarte parameters: Add Alpha and Beta as parameters in the generated code.
Select Optimize for use with fixed-step integrators to optimize events handling and relax the inconsistencies detection
tolerance.
Select Add Failure Handling to reinitialize the model on failure, and then select one of the following re-initialization
options:
• Re-initialize to initial point: Reinitialize the model to the initial values when a failure happens.
• Hold to previous successful step: Reinitialize the model to the values at the end of the last successful step.
Discretization
Select Export as a discrete model (no continuous states) to apply discretization to your model. When selected, you
can select a solver type from 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
In this section, you can also set the Discrete step size (in seconds) for the discretization.
Select Allow intermediate steps between fixed steps if you want to trigger events inside a fixed step (instead of at
the end of the fixed step). Your solver must be either Euler or Implicit Euler. After selecting this option, enter the fol-
lowing settings:
• Number of intermediate steps: The maximum number of intermediate steps you want to take inside the fixed step.
• Tolerance: The proportion of the current step size that the solver is allowed to overshoot so as to catch more than
one close-firing event. Generally, enter a smaller tolerance for models that are fairly linear between steps and a larger
tolerance for solutions that are far from linear.
6 • Getting Started
• Export code to work with single-precision floating point: Select this option to have internal calculations done in
single-precision rather than double-precision. Further, some of the criteria for MapleSim solvers are adjusted to
work with the lower precision.
Note: You will need to configure Simulink to properly run a single-precision S-Function.
Diagnostic Options
Select Add outputs for log(stepsize), event and constraint iterations, and constraint residual to provide run-time
diagnostics in the generated Simulink® block. This adds four outputs to the bottom of the Simulink® block: LogStep-
Size(t), EventIterations(t), ConstraintIterations(t), and ConstraintResidual(t).
Export
Provide a name and specify the location for the generated file.
To generate an S-Function block without a Simulink® connection, click Generate S-Function (no Compile).
Note: If your model contains an external library, then you must add the directory that contains the external library to
your search path. See Adding External Libraries to Your Search Path (page iv) for instructions on how to do this.
7 • Getting Started
View Code
After you generate the S-Function code and create the block a MATLAB® command window opens and the block
with any of the following specified parameters is generated in Simulink®:
• Block Generation Script
• C Code
• Parameter Script
To view an example:
1. From the Help menu, select the Examples > MapleSim Connector Examples menu, 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. In the Attached Files tab ( ), expand Documents. You can open any of these documents by right-clicking
(Control-clicking for Mac) its entry in the list and selecting View. After you add a template to a model, it will be
available from this list.
Note: Before starting this tutorial, you must set up MATLAB® and the mex compiler in order to have the app appear
in the list. For more information, see the MapleSimConnector,setup help page for more information.
Note: By default, all parameters in the model are kept as configurable parameters.
6. In the Export section, specify the directory to save the generated files to.
7. Click Generate and Compile S-Function to generate the S-function code and create the block.
A MATLAB® command window opens and the block with the specified parameters is generated in Simulink®.
8 • Getting Started
Double-click on the block to open the Parameter Mask dialog box that contains the symbolic parameters from the
original model (see the following figure).
9 • Getting Started
You can set parameters for the block from this mask. For 2016.2 and later versions of the MapleSim Connector, the
two Extended Visualization Data parameters are used to create data files that can be imported back into MapleSim.
This lets you see view the results from executing the block in MapleSim (see the following section, Enabling Extended
Visualization Data, for details).
This block can now be connected with any compatible Simulink® blocks.
• Extended Visualization Data - File Name: Enter a file name with a .bin file extension for the extended visualization
data file.The extended visualization data file is necessary in order to import the Simulink® simulation results into
MapleSim. Leave this field blank if you do not want to generate extended visualization data.
• Extended Visualization Data - Minimum Sample Step: Enter the minimum sample step, in seconds, for the
visualization data in the .bin file. The default value is 1/40 of a second. This sample step is used for both plots and
3-D animations (for 3-D models). For example, a sample step of 1/40 of a second generates plot points every 1/40
of a second and gives you a frame rate of 40 frames per second in your 3-D animation. Smaller values for the min-
imum step size give higher resolution plots and animations, but may result in larger .bin files. Set this to 0 to store
all the visualization data generated by Simulink®.
After you execute your block in Simulink®, the extended visualization data file is created in the same directory that
your Simulink® model is in. See MapleSim > Using MapleSim > Simulating a Model > Importing FMU or Sim-
ulink(R) S-function Results in the MapleSim help system for information on how to import these results into
MapleSim.
Note: For best results, set the solver type and error tolerances settings in Simulink® to match the settings you have in
the original MapleSim model.
4. Export the model using the Simulink® Component Block Generation app.
Note: The following tutorial will take you through these steps in detail. Before starting this tutorial, you must set up
MATLAB® and the mex compiler. For more information, see the MapleSimConnector,setup help page for more in-
formation.
1. Draw a box around all of the components in the model by dragging your mouse over them.
11 • Getting Started
2. From the Edit menu, select Create Subsystem. The Create Subsystem dialog box appears.
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.
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.
12 • Getting Started
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 ( ) of the Absolute Translation component to the demul-
tiplexer Real input signal (u) port. 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 needs to 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.
13 • Getting Started
12. Add another Absolute Translation component above the Connecting Rod subsystem.
13. 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.
14. 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.
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.
18. Click Main ( ) in the Model Workspace toolbar 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 ( ) in the Model Workspace toolbar and then click the top output port of the SliderCrank
subsystem.
21. Drag the probe to an empty location on the Model Workspace, and then click the workspace to position the probe.
22. In the same way, add probes to the other SliderCrank output ports as shown below.
To edit parameters
1. Double-click on the SliderCrank subsystem, and then click Parameters ( ) in the Model Workspace toolbar.
The parameter editor appears.
2. In the first Name field, enter CrankL and press Enter.
3. Enter 1 in the Default Value field of CrankL, and then enter Length of the crank in the Description field.
4. In the second row of the table, enter ConRodL in the Name field and press Enter.
5. Enter 2 in the Default Value field of ConRodL, and then enter Length of the connecting rod in the Description
field.
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
S-function block.
16 • Getting Started
Exporting Your Model Using the Simulink® Component Block Generation App
After preparing the model, you can use the Simulink® Component Block Generation app to set export options and
convert the model to an S-function block.
Note: The Export option for this parameter is selected by default. Also, by default, all input ports, output ports, and
parameters in the model are kept as configurable parameters.
5. Click Generate and Compile S-Function to generate the S-function code and create the block. A MATLAB®
command window opens and the block with the specified parameters is generated in Simulink®.
2. Click Help. This window provides a model description and information about the inputs, outputs, parameters, and
initial conditions.
3. All inputs and outputs are implemented as vector signals. To access individual signals in Simulink®, use a Mux
block for inputs and a Demux block for outputs.
Note: The extended visualization data parameters are used to generate a simulation results data file that can be imported
into MapleSim with the simulation results generated by Simulink® into MapleSim. See Enabling Extended Visualization
Data (page 9) for more information on these parameters.
2 Creating and Exporting Mathematical Models in Maple
In Maple, you can use commands from the DynamicSystems package to create a system from first principles. Maple
contains a data structure called a system object that encapsulates the properties of a dynamic system. This data structure
contains information, for example, the description of the system, and the description of the inputs. Five different types
of systems can be created.
• Differential equation or difference equation
• Transfer function as an expression
• Transfer function as a list of numerator and denominator coefficients
• State-space
• Zero, pole, gain
You can create a DynamicSystems object in a new worksheet and use commands from the MapleSimConnector
package to generate source code programmatically and save it as a MATLAB® .m file.
>
>
To create a system object from the transfer function , use the following command:
>
(2.1)
18
19 • Creating and Exporting Mathematical Models in Maple
>
(2.2)
The default values for the input names ( and output names have been used. Alternatively, during creation
of the system, different input and output names can be specified.
>
(2.3)
Finally, use the SBlock command to generate the source code and the SaveCode command to save the code as a .c file
and MATLAB® .m file.
>
>
>
This example demonstrates how to define, analyze, and export a system programmatically.
>
>
>
(2.4)
In place of the above commands, you could use the PrintSystem command to display each part of the model.
>
>
3. Generate and save the source code as a .c file and MATLAB® .m file.
>
21 • Creating and Exporting Mathematical Models in Maple
>
>
With the basic tools shown in this guide, you are now ready to use the MapleSim Connector to solve many system
design problems. For more information about the commands used in this guide, enter DynamicSystems and MapleS-
imConnector in the Maple help system.
V
Index View Code, 7
A
Apps
Simulink® Block Generation, 1, 16
C
Code Export Options, 3
D
DynamicSystems object, 18
Creating and Exporting Programmatically, 18
Transfer function, 18
E
Export S-Function, 6
Exporting, iv
Extended Visualization Data, 9
External Libraries, iv
G
Generate
External Libraries, 6
I
Inputs
Outputs
and Parameter Configuration, 2
Inputs and outputs, 11
M
MapleSim Connector Examples, 7
Mathematical model, 18
MATLAB®
Setup, 1
Models using external libraries, iv
S
S-Function
Export, 6
View Code, 7
Simulink®, 16
Subsystem
Creating, 10
Preparation, 2
Selection, 2
Subsystem parameters, 14
System object, 18
22