Rigid Body Dynamics Library
Python API example

Here is a simple example of the Python API that showcases a subset of the wrapped functions and how to access them from python:

1 import numpy as np
2 import rbdl
3 
4 # Create a new model
5 model = rbdl.Model()
6 
7 # Create a joint from joint type
8 joint_rot_y = rbdl.Joint.fromJointType ("JointTypeRevoluteY")
9 
10 # Create a body for given mass, center of mass, and inertia at
11 # the CoM
12 body = rbdl.Body.fromMassComInertia (
13  1.,
14  np.array([0., 0.5, 0.]),
15  np.eye(3) * 0.05)
16 xtrans= rbdl.SpatialTransform()
17 xtrans.r = np.array([0., 1., 0.])
18 
19 # You can print all types
20 print (joint_rot_y)
21 print (model)
22 print (body)
23 print (body.mInertia)
24 print (xtrans)
25 
26 # Construct the model
27 body_1 = model.AppendBody (rbdl.SpatialTransform(), joint_rot_y, body)
28 body_2 = model.AppendBody (xtrans, joint_rot_y, body)
29 body_3 = model.AppendBody (xtrans, joint_rot_y, body)
30 
31 # Create numpy arrays for the state
32 q = np.zeros (model.q_size)
33 qdot = np.zeros (model.qdot_size)
34 qddot = np.zeros (model.qdot_size)
35 tau = np.zeros (model.qdot_size)
36 
37 # Modify the state
38 q[0] = 1.3
39 q[1] = -0.5
40 q[2] = 3.2
41 
42 # Transform coordinates from local to global coordinates
43 point_local = np.array([1., 2., 3.])
44 point_base = rbdl.CalcBodyToBaseCoordinates (model, q, body_3, point_local)
45 point_local_2 = rbdl.CalcBaseToBodyCoordinates (model, q, body_3, point_base)
46 
47 # Perform forward dynamics and print the result
48 rbdl.ForwardDynamics (model, q, qdot, tau, qddot)
49 print ("qddot = " + str(qddot.transpose()))
50 
51 # Compute and print the jacobian (note: the output parameter
52 # of the Jacobian G must have proper size!)
53 G = np.zeros([3,model.qdot_size])
54 rbdl.CalcPointJacobian (model, q, body_3, point_local, G)
55 print ("G = \n" + str(G))

To build the wrapper one needs both Cython and the development libraries for NumPy installed on the system. If this is the case you can build the wrapper by enabling the CMake option RBDL_BUILD_PYTHON_WRAPPER when configuring RBDL. You can find the wrapper in the subdirectory python/ of your build directory. By running the python interpreter in this directory you can load it within the python shell using

Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rbdl

To install the wrapper you can use the python/setup.py script:

sudo ./setup.py install

This installs the RBDL python module globally and allows you to import it from any python script.