append-urdf-model-with-another-model.py
Go to the documentation of this file.
1 import math
2 import sys
3 from pathlib import Path
4 
5 import hppfcl as fcl
6 import numpy as np
7 import pinocchio as pin
8 from pinocchio.visualize import MeshcatVisualizer as Visualizer
9 
10 # load model from example-robot urdf
11 pinocchio_model_dir = Path(__file__).parent.parent / "models"
12 
13 model_path = pinocchio_model_dir / "example-robot-data/robots"
14 mesh_dir = pinocchio_model_dir
15 urdf_filename = "ur5_robot.urdf"
16 urdf_model_path = model_path / "ur_description/urdf" / urdf_filename
17 
18 model1, collision_model1, visual_model1 = pin.buildModelsFromUrdf(
19  urdf_model_path, package_dirs=mesh_dir
20 )
21 
22 # build model from scratch
23 model2 = pin.Model()
24 model2.name = "pendulum"
25 geom_model = pin.GeometryModel()
26 
27 parent_id = 0
28 joint_placement = pin.SE3.Identity()
29 body_mass = 1.0
30 body_radius = 1e-2
31 
32 joint_name = "joint_spherical"
33 joint_id = model2.addJoint(
34  parent_id, pin.JointModelSpherical(), joint_placement, joint_name
35 )
36 
37 body_inertia = pin.Inertia.FromSphere(body_mass, body_radius)
38 body_placement = joint_placement.copy()
39 body_placement.translation[2] = 0.1
40 model2.appendBodyToJoint(joint_id, body_inertia, body_placement)
41 
42 geom1_name = "ball"
43 shape1 = fcl.Sphere(body_radius)
44 geom1_obj = pin.GeometryObject(geom1_name, joint_id, body_placement, shape1)
45 geom1_obj.meshColor = np.ones(4)
46 geom_model.addGeometryObject(geom1_obj)
47 
48 geom2_name = "bar"
49 shape2 = fcl.Cylinder(body_radius / 4.0, body_placement.translation[2])
50 shape2_placement = body_placement.copy()
51 shape2_placement.translation[2] /= 2.0
52 
53 geom2_obj = pin.GeometryObject(geom2_name, joint_id, shape2_placement, shape2)
54 geom2_obj.meshColor = np.array([0.0, 0.0, 0.0, 1.0])
55 geom_model.addGeometryObject(geom2_obj)
56 
57 visual_model2 = geom_model
58 
59 # join the two models, append pendulum to end effector
60 frame_id_end_effector = model1.getFrameId("tool0")
61 model, visual_model = pin.appendModel(
62  model1,
63  model2,
64  visual_model1,
65  visual_model2,
66  frame_id_end_effector,
67  pin.SE3.Identity(),
68 )
69 
70 print(
71  f"Check the joints of the appended model:\n {model} \n "
72  "->Notice the spherical joint at the end."
73 )
74 
75 try:
76  viz = Visualizer(model, visual_model, visual_model)
77  viz.initViewer(open=True)
78 except ImportError as err:
79  print(
80  "Error while initializing the viewer. "
81  "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


pinocchio
Author(s):
autogenerated on Fri Nov 1 2024 02:41:38