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 simple_pendulum.py.

First, we import PyMbs functions and classes from pymbs.input:

from pymbs.input import MbsSystem, diag

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', startVals=1)

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/simple_pendulum.gif

In the end, the complete source looks like this:

"""
Model for a 1D pendulum
"""

# 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 MbsSystem, diag

# 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', startVals=1)

# 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 model
world.show('SimplePendulum')