append-urdf-model-with-another-model.py
Go to the documentation of this file.
1 from os.path import dirname, join, abspath
2 import math
3 import sys
4 
5 import numpy as np
6 
7 import pinocchio as pin
8 from pinocchio.visualize import MeshcatVisualizer as Visualizer
9 import hppfcl as fcl
10 
11 # load model from example-robot urdf
12 pinocchio_model_dir = join(dirname(dirname(str(abspath(__file__)))), "models/")
13 
14 model_path = join(pinocchio_model_dir, "example-robot-data/robots")
15 mesh_dir = pinocchio_model_dir
16 urdf_filename = "ur5_robot.urdf"
17 urdf_model_path = join(join(model_path, "ur_description/urdf/"), urdf_filename)
18 
19 model1, collision_model1, visual_model1 = pin.buildModelsFromUrdf(
20  urdf_model_path, package_dirs=mesh_dir
21 )
22 
23 # build model from scratch
24 model2 = pin.Model()
25 model2.name = "pendulum"
26 geom_model = pin.GeometryModel()
27 
28 parent_id = 0
29 joint_placement = pin.SE3.Identity()
30 body_mass = 1.0
31 body_radius = 1e-2
32 
33 joint_name = "joint_spherical"
34 joint_id = model2.addJoint(
35  parent_id, pin.JointModelSpherical(), joint_placement, joint_name
36 )
37 
38 body_inertia = pin.Inertia.FromSphere(body_mass, body_radius)
39 body_placement = joint_placement.copy()
40 body_placement.translation[2] = 0.1
41 model2.appendBodyToJoint(joint_id, body_inertia, body_placement)
42 
43 geom1_name = "ball"
44 shape1 = fcl.Sphere(body_radius)
45 geom1_obj = pin.GeometryObject(geom1_name, joint_id, body_placement, shape1)
46 geom1_obj.meshColor = np.ones((4))
47 geom_model.addGeometryObject(geom1_obj)
48 
49 geom2_name = "bar"
50 shape2 = fcl.Cylinder(body_radius / 4.0, body_placement.translation[2])
51 shape2_placement = body_placement.copy()
52 shape2_placement.translation[2] /= 2.0
53 
54 geom2_obj = pin.GeometryObject(geom2_name, joint_id, shape2_placement, shape2)
55 geom2_obj.meshColor = np.array([0.0, 0.0, 0.0, 1.0])
56 geom_model.addGeometryObject(geom2_obj)
57 
58 visual_model2 = geom_model
59 
60 # join the two models, append pendulum to end effector
61 frame_id_end_effector = model1.getFrameId("tool0")
62 model, visual_model = pin.appendModel(
63  model1,
64  model2,
65  visual_model1,
66  visual_model2,
67  frame_id_end_effector,
68  pin.SE3.Identity(),
69 )
70 
71 print(
72  "Check the joints of the appended model:\n %s \n ->Notice the spherical joint at the end."
73  % model
74 )
75 
76 try:
77  viz = Visualizer(model, visual_model, visual_model)
78  viz.initViewer(open=True)
79 except ImportError as err:
80  print(
81  "Error while initializing the viewer. It seems you should install Python meshcat"
82  )
83  print(err)
84  sys.exit(0)
85 
86 # Load the robot in the viewer.
87 viz.loadViewerModel()
88 
89 # Display a random robot configuration.
90 model.lowerPositionLimit.fill(-math.pi / 2)
91 model.upperPositionLimit.fill(+math.pi / 2)
92 q = pin.randomConfiguration(model)
93 viz.display(q)
pinocchio.visualize
Definition: bindings/python/pinocchio/visualize/__init__.py:1
path


pinocchio
Author(s):
autogenerated on Tue Jun 25 2024 02:42:34