append-urdf-model-with-another-model.py
Go to the documentation of this file.
1 import math
2 import sys
3 import time
4 from pathlib import Path
5 
6 import hppfcl as fcl
7 import numpy as np
8 import pinocchio as pin
9 from pinocchio.visualize import MeshcatVisualizer as Visualizer
10 
11 # load model from example-robot urdf
12 pinocchio_model_dir = Path(__file__).parent.parent / "models"
13 
14 model_path = pinocchio_model_dir / "example-robot-data/robots"
15 mesh_dir = pinocchio_model_dir
16 urdf_filename = "ur5_robot.urdf"
17 urdf_model_path = 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  f"Check the joints of the appended model:\n {model} \n "
73  "->Notice the spherical joint at the end."
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. "
82  "It seems you should install Python meshcat"
83  )
84  print(err)
85  sys.exit(0)
86 
87 # Load the robot in the viewer.
88 viz.loadViewerModel()
89 
90 # Display a random robot configuration.
91 model.lowerPositionLimit.fill(-math.pi / 2)
92 model.upperPositionLimit.fill(+math.pi / 2)
93 q = pin.randomConfiguration(model)
94 viz.display(q)
95 time.sleep(1.0)
pinocchio.visualize
Definition: bindings/python/pinocchio/visualize/__init__.py:1


pinocchio
Author(s):
autogenerated on Mon Dec 16 2024 03:40:58