pinocchio  2.3.1-dirty
Display a model using RobotWrapper

RobotWrapper is a wrapper class written in Python holding Pinocchio models and data. It also includes wrapper functions for a few common methods. RobotWrapper can interoperate with different visualizers. Below, you find an example on how to load a model with RobotWrapper and how to use it with a visualizer of your choice.

1 #
2 # In this short script, we show how to use RobotWrapper
3 # integrating different kinds of viewers
4 #
5 
6 import pinocchio as pin
7 pin.switchToNumpyMatrix()
8 from pinocchio.robot_wrapper import RobotWrapper
9 from pinocchio.visualize import (GepettoVisualizer, MeshcatVisualizer)
10 from sys import argv
11 import os
12 from os.path import dirname, join, abspath
13 
14 # If you want to visualize the robot in this example,
15 # you can choose which visualizer to employ
16 # by specifying an option from the command line:
17 # GepettoVisualizer: -g
18 # MeshcatVisualizer: -m
19 VISUALIZER = None
20 if len(argv)>1:
21  opt = argv[1]
22  if opt == '-g':
23  VISUALIZER = GepettoVisualizer
24  elif opt == '-m':
25  VISUALIZER = MeshcatVisualizer
26  else:
27  raise ValueError("Unrecognized option: " + opt)
28 
29 # Load the URDF model with RobotWrapper
30 # Conversion with str seems to be necessary when executing this file with ipython
31 pinocchio_model_dir = join(dirname(dirname(str(abspath(__file__)))),"models")
32 
33 model_path = join(pinocchio_model_dir,"others/robots")
34 mesh_dir = model_path
35 urdf_filename = "talos_reduced.urdf"
36 urdf_model_path = join(join(model_path,"talos_data/urdf"),urdf_filename)
37 
38 robot = RobotWrapper.BuildFromURDF(urdf_model_path, mesh_dir, pin.JointModelFreeFlyer())
39 
40 # alias
41 model = robot.model
42 data = robot.data
43 
44 # do whatever, e.g. compute the center of mass position expressed in the world frame
45 q0 = robot.q0
46 com = robot.com(q0)
47 
48 # This last command is similar to:
49 com2 = pin.centerOfMass(model,data,q0)
50 
51 # Show model with a visualizer of your choice
52 if VISUALIZER:
53  robot.setVisualizer(VISUALIZER())
54  robot.initViewer()
55  robot.loadViewerModel("pinocchio")
56  robot.display(q0)