Importing a robot arm multibody model from a URDF file

Importing a robot arm multibody model from a URDF file#

The purpose of this notebook is to

  • demonstrate the creation of a Karana.Dynamics.Multibody instance for a robot arm from a URDF model file

  • show the structure of the loaded model

  • show a 3D visualization of the robot arm

  • serve as a hello world script for verifying a proper kdFlex installation

Resources

Acknowledgements The Barrett WAM urdf is sourced from the public repository barrett-ros2-pkg. Thank you to the authors and contributors.

Import kdFlex Python modules objects for use later

from pathlib import Path
import atexit

import Karana.KUtils.BasicPrefab as kub
from Karana.KUtils.Sim import Sim

Build the filepath to the URDF file we will import

resources = Path().absolute().parent / "resources"
urdf_file = resources / "wam7dof" / "wam7dof_hand.urdf"

Import the URDF file to create a BasicPrefabDS. The “DS” is short for “Data Structure”. This is kdFlex’s native, file-format agnostic way of specifying a multibody and associated KModels. We’ll use this next to create our simulation.

basic_prefab_ds = kub.BasicPrefabDS.fromFile(urdf_file)

Instantiate and configure the Karana.Dynamics.Multibody based on the BasicPrefabDS description.

sim, prefab = Sim.fromBasicPrefabDS(basic_prefab_ds)

Finalize the model to get it ready for use.

sim.mb.ensureHealthy()
sim.mb.resetData()

Set up the 3D graphics.

_, graphics = sim.setupGraphics(port=0)
graphics.defaultCamera().pointCameraAt(
    offset=[0.2, 0.5, 1.7],
    target=[0, 0, 1],
    up=[1, 0, 0],
)
del graphics

Display the bodies in the multibody model.

sim.mb.displayModel()

Display the topological structure of the multibody system

sim.mb.dumpTree()
[WebUI] Web server is running on port 33661
You may be able to connect in your browser at:
	http://newton:33661

               ===== Bodies for 'wam7dof' Multibody =====
Legend:
  No.              topological body number
  Hinge            hinge type followed by one prescribed optional marker per subhinge
  P                prescribed subhinge; blank means unprescribed
  DOFs             hinge-local / DOFs accumulated before this body
========================================================
No.     Body          Parent            Hinge       DOFs
------  ------------  ----------------  ----------  ----
1.      FirstLink     wam7dof_MBVROOT_  REVOLUTE     1/0
2.      SecondLink    FirstLink         REVOLUTE     1/1
3.      ThirdLink     SecondLink        REVOLUTE     1/2
4.      FourthLink    ThirdLink         REVOLUTE     1/3
5.      FifthLink     FourthLink        REVOLUTE     1/4
6.      SixthLink     FifthLink         REVOLUTE     1/5
7.      SeventhLink   SixthLink         REVOLUTE     1/6
8.      bhand_base    SeventhLink       LOCKED       0/7
9.      bhand_link11  bhand_base        REVOLUTE     1/7
10.     bhand_link12  bhand_link11      REVOLUTE     1/8
11.     bhand_link13  bhand_link12      REVOLUTE     1/9
12.     bhand_link21  bhand_base        REVOLUTE    1/10
13.     bhand_link22  bhand_link21      REVOLUTE    1/11
14.     bhand_link23  bhand_link22      REVOLUTE    1/12
15.     bhand_link32  bhand_base        REVOLUTE    1/13
16.     bhand_link33  bhand_link32      REVOLUTE    1/14
--------------------------------------------------------
Totals                                                15
LEGEND: [<hinge type[/dofs]> <prescribed subhinges>] <body name>[/num embedded bodies > 0] <flex dofs > 0>
|-wam7dof_MBVROOT_
   |-[REVOLUTE] FirstLink
      |-[REVOLUTE] SecondLink
         |-[REVOLUTE] ThirdLink
            |-[REVOLUTE] FourthLink
               |-[REVOLUTE] FifthLink
                  |-[REVOLUTE] SixthLink
                     |-[REVOLUTE] SeventhLink
                        |-[LOCKED] bhand_base
                           |-[REVOLUTE] bhand_link11
                              |-[REVOLUTE] bhand_link12
                                 |-[REVOLUTE] bhand_link13
                           |-[REVOLUTE] bhand_link21
                              |-[REVOLUTE] bhand_link22
                                 |-[REVOLUTE] bhand_link23
                           |-[REVOLUTE] bhand_link32
                              |-[REVOLUTE] bhand_link33

Animate the graphics by articulating the individual joints.

sim.mb.articulateBodies()

Cleanup

def cleanup():
    """Cleanup the simulation."""
    global basic_prefab_ds, prefab, sim
    del basic_prefab_ds, prefab, sim


atexit.register(cleanup)
<function __main__.cleanup()>