MotionSolve
MotionSolve

2022

  1. Home
  2. Tutorials

    Discover MotionSolve functionality with interactive tutorials.

  3. Co-Simulation

  4. MV-7009: Remote Co-simulation with Simulink

    In this tutorial, you will learn how to use the MotionSolve-Simulink remote co-simulation interface, driving the model from Simulink via an S-Function.

  • What's New
  • Overview
  • Tutorials
  • MotionSolve User Guide
  • MotionSolve Reference Guide
Index
MotionSolve

2022

MotionSolve
  • What's New

    View new features for MotionSolve 2022.

  • Overview

    MotionSolve® is an integrated solution to analyze, evaluate, and optimize the performance of multi-body systems.

  • Tutorials

    Discover MotionSolve functionality with interactive tutorials.

    • Access the Model Files

      Learn how to access and download required model files.

    • Introduction

    • Rigid Body Dynamics

    • Model Definition Language

    • Flexible Body Dynamics

    • Advanced Simulation

    • Optimization-DOE-Stochastics

    • Durability - Fatigue

    • Automation

    • User Subroutines

    • Co-Simulation

      • MV-7010: Co-Simulation with Activate - Quadrotor Control

        In this tutorial, you will learn how to use MotionSolve and Activate in a co-simulation to control a Quadrotor model.

      • MV-7011: Co-Simulation with Activate via Function Mockup Interface (FMI)

        In this tutorial, you will learn about the process of setting up an MBS model to co-simulate with Activate by following the FMI.

      • MV-7002: Co-simulation with Simulink

        In this tutorial, you will learn how to use the MotionSolve-Simulink co-simulation interface, driving the model from Simulink via an S-Function. MotionSolve co-simulates with Simulink using the Inter Process Communication (IPC) approach.

      • MV-7009: Remote Co-simulation with Simulink

        In this tutorial, you will learn how to use the MotionSolve-Simulink remote co-simulation interface, driving the model from Simulink via an S-Function.

      • MV-7008: Co-simulation with AcuSolve

        In this tutorial, you will learn how to setup a model in MotionView that will be coupled with AcuSolve.

      • MV-7021: MotionSolve/EDEM Co-Simulation

        In this tutorial, you will learn about the set-up and co-simulation of MotionSolve and EDEM through MotionView.

    • Vehicle Simulation using MotionView

    • Using the MotionSolve API

  • MotionSolve User Guide

    MotionSolve is a system level, multi-body solver that is based on the principles of mechanics.

  • MotionSolve Reference Guide

    This manual provides a detailed list and usage information regarding command statements, model statements, functions and the Subroutine Interface available in MotionSolve.

View All Altair Simulation Help

MotionSolve
MotionSolve

2022

  1. Home
  2. Tutorials

    Discover MotionSolve functionality with interactive tutorials.

  3. Co-Simulation

  4. MV-7009: Remote Co-simulation with Simulink

    In this tutorial, you will learn how to use the MotionSolve-Simulink remote co-simulation interface, driving the model from Simulink via an S-Function.

  • What's New
  • Overview
  • Tutorials
  • MotionSolve User Guide
  • MotionSolve Reference Guide
Index

MV-7009: Remote Co-simulation with Simulink

In this tutorial, you will learn how to use the MotionSolve-Simulink remote co-simulation interface, driving the model from Simulink via an S-Function.

MotionSolve allows you to co-simulate with Simulink when the two software are remotely located, through an implementation of the Inter Process Communication (IPC) approach via sockets.

Software and Hardware Requirements

Software requirements:
  • MotionSolve
  • MATLAB/Simulink (MATLAB Version R2015a, Simulink Version 8.5) (or newer)
Note: Older versions of MATLAB/Simulink may work, but the above minimum versions are recommended and tested.
Hardware requirements:
  • PC with 64bit CPU, running Windows 7/10 and/or
  • Linux RHEL 6.6 or CentOS 7.2 64

Simulation Environment

In this tutorial, it is assumed that MATLAB is installed on Machine "A" and MotionSolve is installed on Machine "B". The following scenarios are feasible:

Machine "A" Machine "B" IPC Co-simulation possible?
MATLAB/Simulink, Windows 64-bit MotionSolve, Windows 64-bit Yes
MATLAB/Simulink, Windows 64-bit MotionSolve, Linux 64-bit Yes
MATLAB/Simulink, Linux 64-bit MotionSolve, Windows 64-bit Yes
MATLAB/Simulink, Linux 64-bit MotionSolve, Linux 64-bit Yes
Note: When you start the co-simulation from Simulink, a few MotionSolve binaries are loaded by Simulink at runtime. For this purpose, a reduced installation of MotionSolve is needed on the machine where Simulink resides. Also, this installation of MotionSolve must be of the same platform type as Simulink. For example, to do an IPC co-simulation between Simulink on Linux and MotionSolve on Windows, you would need to have certain MotionSolve Linux binaries on the same machine where the Simulink software is installed. These libraries are listed in the appendix. The table above is updated below to reflect this.
Machine "A" Machine "B" IPC Co-simulation possible?
MATLAB/Simulink, Windows 64-bit (Requires a Windows 64-bit MotionSolve installation) MotionSolve, Windows 64-bit Yes
MATLAB/Simulink, Windows 64-bit (Requires a Windows 64-bit MotionSolve installation) MotionSolve, Linux 64-bit Yes
MATLAB/Simulink, Linux 64-bit (Requires a Linux 64-bit MotionSolve installation) MotionSolve, Windows 64-bit Yes
MATLAB/Simulink, Linux 64-bit (Requires a Linux 64-bit MotionSolve installation) MotionSolve, Linux 64-bit Yes
This tutorial describes the process to perform a co-simulation between a 64-bit installation of Simulink on Machine "A" and a 64-bit installation of MotionSolve on Machine "B" (the scenario that is in blue text in the table above). This is illustrated better in the figure below.


Figure 1. Co-Simulation Environment Used for this Tutorial

Inverted Pendulum Controller

Consider an inverted pendulum, mounted on a cart. The pendulum is constrained at its base to the cart by a revolute joint. The cart is free to translate along the X direction only. The pendulum is given an initial rotational velocity causing it to rotate about the base.

Both the pendulum and the cart are modeled as rigid bodies. The controller, modeled in Simulink, provides a control force to the cart to stabilize the inverted pendulum and prevent it from falling. This control force is applied to the cart via a translational force. The model setup is illustrated in the figure below.


Figure 2. Inverted Pendulum Model Setup
The pre-designed controller generates a control force that keeps the pendulum upright. The controller uses the pendulum's orientation and angular velocity along with the cart's displacement and velocity as inputs to calculate the direction and magnitude of the force needed to keep the pendulum upright. A block diagram of the control system is shown in the figure below.


Figure 3. Block Diagram Representation of the Controller
In the image above:
  • θ is the angular displacement of the pendulum from its model configuration
  • ω is the angular velocity of the pendulum about its center of gravity as measured in the ground frame of reference
  • x is the translational displacement of the cart measured from its model configuration in the ground frame of reference
  • x ˙ is the translational velocity of the cart measured in the ground frame of reference
  • R θ is a reference signal for the pendulum's angular displacement
  • R ω is a reference signal for the pendulum's angular velocity
  • R x is a reference signal for the cart's translational displacement
  • R x ˙ is a reference signal for the cart's translational velocity

A disturbance is added to the computed control force to assess the system response. The controller force acts on the cart body to control the displacement and velocity profiles of the cart mass.

In this exercise, you will do the following:
  • Solve the baseline model in MotionSolve only (that is, without co-simulation) by using the inverted pendulum model with a continuous controller modeled by a Force_Vector_OneBody element. You can use these results to compare to an equivalent co-simulation in the next steps.
  • Review a modified MotionSolve inverted pendulum model that mainly adds the Control_PlantInput and Control_PlantOutput entities that allow this model to act as a plant for Simulink co-simulation.
  • Review the controller in Simulink.
  • Perform a co-simulation and compare the results between the standalone MotionSolve model and the co-simulation model.

Before you begin, copy all the files in the <altair>\tutorials\mv_hv_hg\mbd_modeling\motionsolve\cosimulation folder to your working directory (referenced as <working directory> in the tutorial). Here, <altair> is the full path to the product installation.

Run the Baseline MotionSolve Model

In this step, use a single body vector force (Force_Vector_OneBody) to model the control force in MotionSolve. The force on the cart is calculated as:

F cart = F control + F dist , where

F dist is the disturbance force,

F c o n t r o l = K 1 ( e r r o r θ ) + K 2 ( e r r o r ω ) + K 3 ( e r r o r x ) + K 4 ( e r r o r x ˙ ) is the control force,

K i are gains applied to each of the error signals

error is the error (difference between reference and actual values) on the input signals.

The angular displacement and velocity of the pendulum are obtained by using the AY() and WY() expressions respectively. The translational displacement and velocity of the cart are obtained similarly, by using the DX() and VX() expressions.

  1. From the Start menu, select All Programs > Altair <version number> > MotionView <version number>.
  2. Open the model InvertedPendulum_NoCosimulation.mdl from your <Working Directory>.


    Figure 4. The MotionSolve Model of the Inverted Pendulum

    The MotionSolve model consists of the following components:

    Component Name Component Type Description
    Slider cart Rigid body Cart body
    Pendulum Rigid body Pendulum body
    Slider Trans Joint Translational joint Translational joint between the cart body and the ground
    Pendulum Rev Joint Revolute joint Revolute joint between the pendulum body and the cart body
    Control Force Vector force The control force applied to the cart body
    Output control force Output request Use this request to plot the control force
    Output slider displacement Output request Use this request to plot the cart's displacement
    Output slider velocity Output request Use this request to plot the cart's velocity
    Output pendulum displacement Output request Use this request to plot the pendulum's displacement
    Output pendulum velocity Output request Use this request to plot the pendulum's velocity
    Pendulum Rotation Angle Solver variable This variable stores the rotational displacement of the pendulum via the expression AY()
    Pendulum Angular Velocity Solver variable This variable stores the rotational velocity of the pendulum via the expression VY()
    Slider Displacement Solver variable This variable stores the translational displacement of the cart via the expression DX()
    Slider Velocity Solver variable This variable stores the translational velocity of the cart via the expression VX()
  3. In the Run panel, specify the name InvertedPendulum_NoCosimulation.xml for the MotionSolve model name and click Run.
    The results achieved will be used as the baseline to compare the results obtained from co-simulation.

Define the Plant in the Control Scheme

A MotionSolve model needs a mechanism to specify the input and output connections to the Simulink model. The MotionSolve model (XML) used above is modified to include the Control_PlantInput and Control_PlantOutput model elements and provide these connections. In this tutorial, this has already been done for you, and you can see this by opening the model InvertedPendulum_Cosimulation.mdl from your <Working_Directory>.

This model contains two additional modeling components:

Component Name Component Type Description
Plant Input Simulink Plant input This Control_PlantInput element is used to define the inputs to the MotionSolve model
Plant Output Simulink Plant output This Control_PlantOutput element is used to define the outputs from the MotionSolve model
  • The Control_PlantInput element defines the inputs to a mechanical system or plant. For this model, only one input is defined in the "Plant Input Simulink" solver array. This is set to the ID of the solver variable that holds the control force from Simulink.


    Figure 5. The Definition of the Input Channel to MotionSolve
  • The Control_PlantOutput element defines the outputs from a mechanical system or plant. For this model, four outputs are defined in the "Plant Output Simulink" solver array. These are the pendulum rotation angle, the pendulum angular velocity, slider displacement and slider velocity.


    Figure 6. The Definition of the Output Channels from MotionSolve

    The inputs specified using the Control_PlantInput and Control_PlantOutput elements can be accessed using the PINVAL() and POUVAL() functions, respectively. Since the Control_PlantInput and Control_PlantOutput list the ids of solver variables, these inputs and output variables may also be accessed using the VARVAL() function. For more details, please refer to the MotionSolve User's Guide on-line help.

    This model has the following connections:
    • Plant Input: A single control force that will be applied to the cart.
    • Plant Output: The pendulum's angular displacement and angular velocity; the cart's translational displacement and velocity.

Set Up Environment Variables

A few environment variables are needed for successfully running a co-simulation using MATLAB. These can be set using one of the following methods:
  • Control Panel (Windows)
  • In the shell/command window that calls MATLAB (with the set command on Windows, or the setenv command on Linux)
  • Within MATLAB, via the setenv() command

An example of the usage of these commands is listed below:

Environment variable Value Windows shell Linux shell MATLAB shell
PATH \mypath set PATH=\mypath setenv PATH \mypath setenv('PATH','\mypath')
  1. Set the following environment variables:
    Environment Variable Windows Path Linux Path
    NUSOL_DLL_DIR <altair>\hwsolvers\motionsolve\bin\win64 <altair>\hwsolvers\motionsolve\bin\linux64
    PATH <altair>\hwsolvers\common\bin\win64;%NUSOL_DLL_DIR%;%PATH% <altair>\hwsolvers\common\bin\linux64:$NUSOL_DLL_DIR:$PATH
    LD_LIBRARY_PATH - <altair>\hwsolvers\motionsolve\bin\linux64:$LD_LIBRARY_PATH
    MS_SERVER_IP_ADDRESS IP address of Machine “B” IP address of Machine “B”

    where <altair> is the full path to the product installation. For example, on Windows, this would typically be C:\Program Files\Altair\<version>.

  2. Note that for licensing and for other model conditions, additional environment variables may be needed. See MotionSolve Environment Variables for more information on these environment variables.

Set Up the Co-simulation

The core feature in Simulink that creates the co-simulation is an S-Function (System Function) block in Simulink. This block requires an S-Function library (a dynamically loaded library) to define its behavior. MotionSolve provides this library, but the S-Function needs to be able to find it. To help MATLAB/Simulink find the S-Function, you need to add the location of the S-Function to the list of paths that MATLAB/Simulink uses in order to search for libraries.

The S-Function libraries for co-simulation with MotionSolve is called mscosimipc – for Inter Process Communication (IPC) using TCP/IP sockets for communication.

This file is located at <altair>\hwsolvers\motionsolve\bin\<platform>.

The location of this file needs to be added to the search path of MATLAB for the S-Function to use mscosimipc.

This can be done in one of the following ways:

  1. Use the menu options.
    1. From the Matlab menu, select File > Set Path.


      Figure 7. Add Path through the MATLAB GUI
    2. From the dialog, add the directory where the mscosimipc library resides.
      <altair>\hwsolvers\motionsolve\bin\win64
    3. Select Save and Close.
      This procedure permanently adds this directory to the MATLAB/Simulink search path.
  2. Use MATLAB commands.
    1. To add the directory where mscosimipc library resides into the MATLAB search path, at the MATLAB command line, type:
      addpath('<altair>\hwsolvers\motionsolve\bin\<platform>')
      It remains valid until you exit MATLAB.
      Note: You can also create a .m script to make this process more easily repeatable.
      For example, you can set the MATLAB path and the necessary environment variables using MATLAB commands in a MATLAB (.m) script:
      addpath('<altair>\hwsolvers\motionsolve\bin\win64')
      setenv('NUSOL_DLL_DIR','<altair>\hwsolvers\motionsolve\bin\win64')
      setenv('RADFLEX_PATH',['<altair>\hwsolvers\common\bin\win64')
      setenv('PATH',['<altair >\hwsolvers\common\bin\win64;' getenv('PATH')])
      setenv('MS_SERVER_IP_ADDRRESS','192.168.1.123')
    2. For a Linux machine, additionally enter setenv('LD_LIBRARY_PATH', '<altair>\hwsolvers\motionsolve\bin\linux64:<altair>\hwsolvers\common\bin\linux64\')

View the Controller Modeled in Simulink

  1. On Machine "A", in the MATLAB window, select File > Open.
    The Open file dialog is displayed.
  2. Select the InvertedPendulum_ControlSystem.mdl file from your <Working Directory>.
  3. Click Open.
    You will see the control system that will be used in the co-simulation.


    Figure 8. The Control System in MATLAB
  4. The model contains an S-function block. Name the S-function mscosimipc.

    The S-function (system-function) is one of the blocks provided by Simulink and represents the MotionSolve model. It can be found in the Simulink User-Defined Functions block library. An S-Function allows you to model any general system of equations with inputs, outputs, states, and so on, and is somewhat analogous to a Control_StateEqn in MotionSolve. See the MATLAB/Simulink documentation for more details.

  5. Double-click the S-function with name mscosimipc. In the dialog that is displayed, under the S Function Parameters, enter the following using single quotes:
    'InvertedPendulum_Cosimulation.xml', 'InvertedPendulum_Cosimulation.mrf', ''
    The three parameters are the following:
    • MotionSolve XML model name
    • Output MRF name
    • MotionSolve user DLL name (optional); enter empty single quotes ('') if not used.

Perform the Co-simulation

  1. On Machine "B", in a shell prompt, run the command "msdaemon 22903".
    This command should be executed from the same working directory where the MotionSolve XML resides. With this command, you have setup MotionSolve in a "listening" mode. The co-simulation will start whenever the simulation is started from Simulink.
  2. On Machine "A", click Simulation > Start to start the co-simulation.
    Simulink uses ODE45 to solve the Simulink model. From this, the co-simulation should begin and Simulink will wait to connect to the MotionSolve model.

    After starting the simulation in Simulink, MotionSolve will connect with the Simulink model and the subsequent co-simulation will result in an output .mrf file for post-processing.

  3. Set the Scopes in the Simulink model to display the results.
  4. Check the .log file to make sure no errors or warnings were issued by MotionSolve.
    Note: A license for MotionSolve will only be checked out on Machine B.


    Figure 9. Running the Co-simulation from Simulink

Compare the MotionSolve-only Results to the Co-simulation Results

  1. From the Start menu, select Altair <version number> > HyperGraph.
  2. Click Build Plots .
  3. Click the file browser and load the InvertedPendulum_NoCosimulation.mrf file. This was the baseline result created by running MotionSolve by itself.
  4. In the Page Controls toolbar, create two vertical plot windows.


    Figure 10. Create Two Plot Windows
  5. Select Marker Displacement for Y-Type, REQ/70000004 Output slider displacement (on Slider cart) for Y Request, and DX for Y Component to plot the cart's translational displacement:


    Figure 11. Plot the Cart's Displacement
  6. Select the window on the left and click Apply.
  7. Select Marker Force for Y-Type, REQ/70000002 Output control force- (on Slider cart) for Y Request, and FX for Y Component to plot the X component of the control force:


    Figure 12. Plot the Control Force on the Cart
  8. Click the file browser icon and load the InvertedPendulum_Cosimulation.mrf file. This was the co-simulation results run with Simulink.
  9. Select Marker Displacement for Y-Type, REQ/70000004 Output slider displacement (on Slider cart) for Y Request, and DX for Y Component to overlay the plot in the left window.
  10. Select Marker Force for Y-Type, REQ/70000002 Output control force- (on Slider cart) for Y Request, and FX for Y Component to overlay the plot in the right window.
    Both the signals match as shown below.


    Figure 13. Comparison of the Cart Displacement and Cart Control Force Between the Two Models

    The blue curves represent results from the MotionSolve-only model and the red curves represent results from the co-simulation.

Appendix

For remote co-simulation, there are two options to installing MotionSolve on the machine where Simulink is installed:
  • Option 1: Fully install MotionSolve on Machine A, just like Machine B
  • Option 2: Install a partial set of libraries on Machine A

If you exercise Option 2 above, then the list of files that need to be copied/installed on the machine where Simulink is located include the following:

Windows Linux Path
mscosimipc.mexw64 mscosimipc.mexa64
msdaemon.exe msdaemon
nuclient.dll nuclient.so
pthreadVC2.dll  
Also, when using Option 2, you can copy the above files to any folder of your choice, for example, ../motionsolve/remote_cosimfiles/. In this case, the environment variables to set in Simulink will change based on the table below:
Environment Variable Windows Path Linux Path
NUSOL_DLL_DIR ..\motionsolve\remote_cosimfiles\ ../motionsolve/remote_cosimfiles
PATH ..\motionsolve\remote_cosimfiles\;%PATH% ../motionsolve/remote_cosimfiles:$PATH
LD_LIBRARY_PATH - ../motionsolve/remote_cosimfiles: $LD_LIBRARY_PATH
MS_SERVER_IP_ADDRESS IP address of Machine “B” IP address of Machine “B”

© 2022 Altair Engineering, Inc. All Rights Reserved.

Intellectual Property Rights Notice | Technical Support