Simple PendulumΒΆ

A very easy problem, and therefore used as an introduction here for PyMbs, is that of a swinging pendulum. If you want to try it first, or look at the complete source code, see SimplePendulum.py.

First, we import PyMbs functions and classes from PyMbs.Input:

from PyMbs.Input import *

Then we set up our initial reference frame, which will also serve as an item to collect all the elements we add to our model. The three item list gives the direction of the gravity vector, i.e. gravity will point in negative Z-direction in this example:

world = MbsSystem([0, 0, -1])

Now we list our parameters, in this case the mass, length and inertia of the pendulum. Each gets a symbol, and you can use floating-point numbers or expressions to set their values:

m = world.addParam('m', 1.0)
l = world.addParam('l', 1.0)
I = world.addParam('Inertia', (m*l**2)/12)

We add a body to the system, representing the pendulum. It has a mass m and an inertia I:

pend = world.addBody(mass=m, inertia=diag([0, I, 0]))

Sometimes it is convenient or even necessary to be able to refer to the current position and orientation of a point on a body. To do this, you can add additional coordinate systems to a body. Here, we would like on at the top of the pendulum, link, where the pendulum will be suspended from. You can specify the point at which the coordinate system is located and its orientation. Here, we only set the location to \(\frac12 l\), i.e. the end of the pendulum:

pend.addFrame('link', [0,0,0.5*l])

To limit the degree of freedom of our pendulum, we now add a joint to our model. The pendulum is supposed to swing around top end (link), so we connect the world with pend.link, and supply Ry to the dofList. Ry means rotation around y-axis:

world.addJoint(world, pend.link, 'Ry')

Before we take a look at the behaviour of our system, we need to define a visualization for the pendulum. A box will do nicely here:

world.addVisualisation.Box(pend, length=0.1, width=0.1, height=1.0)

Now we can generate the equations of motion for our system:

world.genEquations.Recursive()

Finally we can take a look at our system and check how it behaves:

world.show('SimplePendulum')

This is what it should look like:

../_images/SimplePendulum.gif

In the end, the complete source looks like this:

# -*- coding: utf-8 -*-
'''
This file is part of PyMbs.

PyMbs is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

PyMbs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with PyMbs.
If not, see <http://www.gnu.org/licenses/>.

Copyright 2011, 2012 Carsten Knoll, Christian Schubert,
                     Jens Frenkel, Sebastian Voigt
'''
# Warning: The source code of the examples is quoted in the documentation. If
# you change this file, you'll have to change the corresponding file in the
# documentation (see doc/examples).

# import PyMbs functionality
from PyMbs.Input import *

# create main object and inital reference frame 
world = MbsSystem([0, 0, -1])

# mass, length and inertia of the rod
m = world.addParam('m', 1.0)
l = world.addParam('l', 1.0)
I = world.addParam('Inertia', (m*l**2)/12)

# add pendulum
pend = world.addBody(mass=m, inertia=diag([0, I, 0]))
# add additional coordinate system at upper end of the rod
pend.addFrame('link', [0,0,0.5*l])

# add joint to constrain motion of the pendulum
world.addJoint(world, pend.link, 'Ry')

# use a Box to represente the pendulum
world.addVisualisation.Box(pend, length=0.1, width=0.1, height=1.0)

# generate equations of motion
world.genEquations.Recursive()

# show gui
world.show('SimplePendulum')