# 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)


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:

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

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

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

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