Tutorial: Co-Simulation with MotionSolve
Learn how to build a Multi-Body System Model (MBS) of an inverted pendulum, combine it with a controls system model, and co-simulate the models.
Files for This Tutorial
Inverted_Pendulum_Tutorial.scm; Inverted_Pendulum_Tutorial.mdl
Overview of Co-Simulation and Prerequisites
Co-simulation enables MotionSolve Multi-Body Dynamics (MBS) models and Twin Activate models to communicate with each other during simulation.
An ideal use case for co-simulation is the development of a control system for an MBS model. This tutorial shows you how to develop such a control system for an inverted pendulum.
An inverted pendulum is a pendulum that has its center of mass above its pivot point, such as a pendulum moving horizontally with its pivot point mounted on a moving railroad cart. Whereas a normal pendulum is stable when hanging downward, an inverted pendulum is inherently unstable, and must be actively balanced in order to remain upright either by applying a torque at the pivot point or by moving the pivot point horizontally as part of a feedback system.
This tutorial shows you how to design a control force acting horizontally on a cart to keep a pendulum upright, much like a child balancing a stick on his hand. The inverted pendulum is a classic problem in dynamics and control theory and is used as a benchmark for testing control strategies. Some real-world examples of inverted pendulum problems include attitude control of a rocket at launch, and the balance and movement of a Segway motorized vehicle.
Prerequisites
The installation of HyperWorks MotionSolve software is required for co-simulation. MotionSolve offers powerful modeling, analysis, visualization and optimization capabilities for multi-disciplinary simulations that include kinematics and dynamics, statics and quasi-statics, linear and vibration studies, stress and durability, loads extraction, co-simulation, effort estimation, and packaging synthesis.
Defining the Software Path Settings
Defining the paths to MotionSolve and other software that is required for local and remote co-simulation methods.
-
From the File menu, select the Preferences button.
The Preferences dialog appears.
- On the left column of the dialog, select the category, Twin Activate Paths.
-
For each preference, enter an installation path, and click
Apply.
Preference Path MotionView <HyperWorks_install_path>/hwdesktop/hw/bin/platform MotionSolve <HyperWorks_install_path>/hwsolvers/motionsolve/bin/platform MotionSolve License (Radflex) <HyperWorks_install_path>/hwsolvers/common/bin/platform Python (For HyperWorks 2021): <HyperWorks_install_path>/common/python/python3.5/platform
(For HyperWorks 2020): <HyperWorks install_dir>/hwdesktop/common/python/python3.5/platform
Building an Inverted Pendulum
Build a Multi-Body System (MBD) model of an inverted pendulum on a cart.
Entities in the MBS Model
- Pendulum
- Cart
- Rod to connect the pendulum to the cart
- Rail on which the cart is constrained to move horizontally
- Pivot which is located on the cart, between the pendulum and cart
Building the Geometry
With MotionView software, create the geometry for the inverted pendulum.
- Launch MotionView and create a new model.
-
Add three points of geometry to the model with the following names and
locations:
Point X Y Z PivotCenter 300 0 0 PendulumCenter 350 0 300 Z-Direction 400 0 600 - Save your model as Inverted_Pendulum.mdl.
Adding Bodies
Add the pendulum and cart to your model.
- Insert a body entity and label it Cart.
- For the Cart, set the CM Coordinates to the point PivotCenter.
- Insert a second body entity and label it Pendulum.
- For the Pendulum, set the CM Coordinates to the point PendulumCenter, and the Z-axis to the direction of point ZDirection.
-
Enter the following inertial properties for the bodies:
Body ID Mass (kg) Ixx(kg-mm^2) Iyy(kg-mm^2) Izz(kg-mm^2) Cart b_0 0.45 100 100 100 Pendulum b_1 0.2 32 32 32
Adding Joints
Add a revolute and translational joint to the model.
-
Insert a revolute joint, label it PendulumPivot, and
set the Connectivity parameters as follows:
-
Set the Initial Conditions as follows:
-
Insert a translational joint, label it CartTranslation,
and set the Connectivity parameters as follows:
-
Set the Initial Conditions as follows:
Adding Graphics
Graphics are essential for understanding the model behavior in an animation.
- Insert a cylinder graphic and label it Rail.
- For the Connectivity parameters, set the parent body to Ground Body; origin to Global Origin; and direction to vector Global X.
-
Set the Properties as follows:
- Insert a sphere graphic, and label it Pendulum.
- For Connectivity parameters, set the parent body to Pendulum, and set the origin to PendulumCenter.
- For Properties, set the radius to 20.
- Insert a cylinder graphic, and label it Rod.
- For Connectivity parameters, set the parent body to Pendulum; set the origin to PendulumCenter; set the direction to the point PivotCenter.
-
Set the Properties as follows:
- Insert a box graphic, and label it Cart.
-
For Connectivity parameters, set the Type to Center; set
the Parent Body to Cart; set the Origin Point to
PivotCenter; set the Z axis direction to Vector
Global Z; set the ZX plane direction to Vector
Global X.
-
Set the Properties as follows:
Adding Forces
Apply translational forces to the Cart.
-
On the Connectivity tab, add the force, frc_0, label it
Control Force, and set the Connectivity properties as
follows:
-
On the Connectivity tab, add a second force, frc_1,
label it PivotTorque, and set the Connectivity properties
as follows:
-
Select
Inverted_Pendulum_Tutorial.mdl.
, and save your model with the name:
Modifying the MBS Model for Co-Simulation
Modify the basic MBS model for co-simulation by adding MotionView entities including solver arrays and solver variables.
Adding Solver Variables
-
Create two solver variables: ControlForce (varname: sv_0)
and PivotTorque (varname: sv_1) as follows:
-
Connect the two solver variables to the forces you created in the previous
section, Adding Forces.
As you see in the following figure, on the Trans Properties tab for ControlForce, for Fx, select Expression, and for f, enter: `VARVAL({sv_0.id})`. This expression specifies the ControlForce to be the first solver variable, sv_0. This variable sets the control signal sent from Twin Activate to be the control force acting on the translational joint of the Cart.
-
Verify that the varnames match (sv_0): From the Expression
Builder, select the solver variable by name and enter its ID as you see in the
following figure:
-
On the Rot Properties tab for PivotTorque, for Ty, select
Expression, and then for f,
enter:`VARVAL({sv_1.id})`. This expression sets the
PivotTorque as the second solver variable, sv_1, which
specifies the control signal sent from Twin Activate as
the torque acting on the rotation of the pendulum.
- Insert three additional solver variables: AngularDeviation, AngularRate and CartPosition.
-
For AngularDeviation, enter sv_2. For Type, select
Expression, and enter:
`RTOD(AY({b_1.cm.idstring}))`
, whereb1
is the ID of the Pendulum body. Note that theAY
function returns theA
ngle about theY
axis of the marker specified as the argument. The units of the angle are in radians, therefore, apply theRTOD
function to convertR
adiansTO
D
egrees. -
For Angular Rate, enter sv_3. For Type, select
Expression, and
enter:
`RTOD(WY({b_1.cm.idstring}))`
, whereb2
is the ID of the Pendulum body. -
For Car Position, enter, sv_4. For Type, select
Expression, and enter:
`DX({b_0.cm.idstring})-300`
, whereb1
is the ID of the cart body. -
Insert two solver arrays. Label them ControlInput and
PlantOutput.
-
The ControlInput solver array includes both the PivotTorque and ControlForce
solver variables. Set the properties as follows:
- The PlantOutput solver array includes the following solver variables: AngularDeviation, AngularRate and CartPosition. Set the Plant Output properties as follows:
-
To help you select the proper solver variable names, click
and a panel appears:
- Save your model as Inverted_Pendulum_Tutorial.mdl.
Validating the Inverted Pendulum Model
Simulate the MBS model in MotionSolve and validate that it performs as required.
- From the Run panel, select: Save and run current model.
- Select the folder icon, and enter a path and name for saving the MotionSolve model (XML file). The name is your choice, but a good practice is to use the same root name as your MotionView model. The solver run requires the XML file. When entering the path, note that the default directory of the tutorial may not be writable, and you may need to specify a different directory or change the write permissions on the directory.
-
From the Run panel, save your model as an XML file. This file is used for the
solver run as well as the Twin Activate model. Alternatively, you can run the
MotionView .mdl file, and name it
Inverted_Pendulum_Tutorial.xml.
-
Animate the results from the simulation and confirm that the model works as
intended before continuing to the next steps. The controller is not yet
connected to the model, therefore the input force and torque are zero.
Building the Controls System Model
Build a controls system model with Twin Activate.
The controls system model requires these blocks:
Block | Description |
---|---|
MotionSolve | Enables the embedding of a MotionSolve model into an Twin Activate model. |
Mux | Reads multiple signals into one signal. |
Demux | Reads a signal which contains multiple components and splits the signal into individual components. |
OMLCustomBlock | |
Scope | Plots data as defined. |
Constant | Generates a command signal. |
PID | Serves as the controller block, the brain of the model. |
Sum | Calculates the error from reference signals and feedback. |
Creating the Diagram of the Twin Activate Model
- On the ribbon, select .
- From the Palette Browser, select MotionSolve block into the current diagram. , then drag and drop one
-
In the diagram, on the MotionSolve block, double-click.
From the dialog that appears, define the following block properties:
For this property Do this XML or MDL input file name Copy and paste the path to your MotionView .mdl model or MotionSolve .xml file. Output MRF file name Enter a path to an output multi-body results file. Direct feedthrough Clear the check box for this option. Direct feedthrough defines if the outputs of a block are directly affected by the inputs. An example is a gain block where y = K*u, which causes algebraic loops. If this result is not desired, clear the check box to break the loops.
Note: The following parameters are populated automatically after you load the input model, *.mdl or *.xml:This parameter Is defined with this value Input Row Size The value 2 indicates that two signals from the Twin Activate model are supplied to the MotionSolve model. These signals correspond to the solver array PlantInput, with variables PivotTorque and ControlForce. Output Row Size The value 3 indicates that the MotionSolve model sends out three signals from one port, which is why the Demux block is added to separate the signals. The signals correspond to the solver array ControlOutput with variables AngularRate, AngularDeviation and CartPosition. - In the Palette Browser from Sum block into the diagram. , drag and drop one
- In the Palette Browser, from Demux and one Mux block into the diagram. , drag and drop one
- On the DeMux block, double-click, and in the block dialog, for Number of outputs, enter 3.
-
Do one of the following:
- Create a super block that includes a function to limit an angle between -180º and 180º. An example is shown below.
- Alternately, add an OmlCustomBlock. In the block dialog box, select the
SimFunction tab, and enter the following function
in Function Code:
function OmlBlockFunction(block,flag)
angle=vssGetInPortData(block,1);
if flag==vssBlockOutputUpdateFlag
x= mod(angle(1),360);
if(x>180)
x=-360+x;
end
vssSetOutPortData(block,1,x,vssGetOutputDataType(block,1));
end
end
-
In the Palette Browser, from Scope blocks into the
diagram.
, drag and drop three The Scope blocks correspond to the responses of AngularDeviation, AngularRate and CartPosition. Define the scope block parameters for axis and curve properties for these responses.
- In the Palette Browser, from PID blocks into the diagram. , drag and drop two
- Select the first PID block. From the Property Editor, select , and enter the name PID1_AngularDeviation.
- Select the second PID block. From the Property Editor, select , and enter the name: PID2_CartPosition
-
On the block, PID1_AngularDeviation, double-click, and
enter these values for the parameters:
Note:
- Proportional Gain multiplies the error signal by a constant gain Kp.
- Integral Gain multiplies the sum history of error with integral gain Ki.
- Derivative Gain multiplies the rate of change of error with gain Kd.
-
On the block, PID2_CartPosition, double-click. In the
dialog, define the parameters:
- In the Palette Browser, from Constant block into the diagram. , drag and drop one
- On the Constant block, double-click. In the dialog, for Constant, enter 0.
- Select the Constant block, and copy and paste it into your diagram so that you have a duplicate.
-
All of the required blocks are now present. Assemble and link the blocks in the
diagram as follows:
- Experiment with the diagram and try to modify the PID gain parameters to see their effect on the simulation.
Simulating the Diagram
-
On the ribbon, select Setup.
- On the dialog that appears, select the tab, Simulation Time.
-
For Final Time, enter 7, indicating that the simulation
will run for 7 seconds.
Your model is complete and you are ready to simulate it!
-
On the ribbon, select Run.
The Scope blocks in the model generate the following plots for the cart: Angular Deviation, Angular Rate and Cart Position.
- Experiment with the diagram by modifying the PID gain parameters and evaluating their effect on the simulation.