build-reduced-model.py
Go to the documentation of this file.
1 import pinocchio as pin
2 import numpy as np
3 
4 from os.path import *
5 
6 # Goal: Build a reduced model from an existing URDF model by fixing the desired joints at a specified position.
7 
8 # Load UR robot arm
9 # This path refers to Pinocchio source code but you can define your own directory here.
10 pinocchio_model_dir = join(dirname(dirname(str(abspath(__file__)))), "models")
11 model_path = pinocchio_model_dir + "/example-robot-data/robots"
12 mesh_dir = pinocchio_model_dir
13 # You should change here to set up your own URDF file
14 urdf_filename = model_path + "/ur_description/urdf/ur5_robot.urdf"
15 model, collision_model, visual_model = pin.buildModelsFromUrdf(urdf_filename, mesh_dir)
16 
17 # Check dimensions of the original model
18 print("standard model: dim=" + str(len(model.joints)))
19 for jn in model.joints:
20  print(jn)
21 print("-" * 30)
22 
23 # Create a list of joints to lock
24 jointsToLock = ["wrist_1_joint", "wrist_2_joint", "wrist_3_joint"]
25 
26 # Get the ID of all existing joints
27 jointsToLockIDs = []
28 for jn in jointsToLock:
29  if model.existJointName(jn):
30  jointsToLockIDs.append(model.getJointId(jn))
31  else:
32  print("Warning: joint " + str(jn) + " does not belong to the model!")
33 
34 # Set initial position of both fixed and revoulte joints
35 initialJointConfig = np.array(
36  [
37  0,
38  0,
39  0, # shoulder and elbow
40  1,
41  1,
42  1,
43  ]
44 ) # gripper)
45 
46 # Option 1: Only build the reduced model in case no display needed:
47 model_reduced = pin.buildReducedModel(model, jointsToLockIDs, initialJointConfig)
48 
49 # Option 2: Build the reduced model including the geometric model for proper displaying of the robot
50 model_reduced, visual_model_reduced = pin.buildReducedModel(
51  model, visual_model, jointsToLockIDs, initialJointConfig
52 )
53 
54 # Option 3: Build the reduced model including multiple geometric models (for example: visuals, collision)
55 geom_models = [visual_model, collision_model]
56 model_reduced, geometric_models_reduced = pin.buildReducedModel(
57  model,
58  list_of_geom_models=geom_models,
59  list_of_joints_to_lock=jointsToLockIDs,
60  reference_configuration=initialJointConfig,
61 )
62 # geometric_models_reduced is a list, ordered as the passed variable "geom_models" so:
63 visual_model_reduced, collision_model_reduced = (
64  geometric_models_reduced[0],
65  geometric_models_reduced[1],
66 )
67 
68 # Check dimensions of the reduced model
69 # options 1-3 only take joint ids
70 print("joints to lock (only ids):", jointsToLockIDs)
71 print("reduced model: dim=" + str(len(model_reduced.joints)))
72 print("-" * 30)
73 
74 # Option 4: Build a reduced model of a robot using RobotWrapper
75 # reference_configuration is optional: if not provided, neutral configuration used
76 # you can even mix joint names and joint ids
77 mixed_jointsToLockIDs = [jointsToLockIDs[0], "wrist_2_joint", "wrist_3_joint"]
78 robot = pin.RobotWrapper.BuildFromURDF(urdf_filename, mesh_dir)
79 reduced_robot = robot.buildReducedRobot(
80  list_of_joints_to_lock=mixed_jointsToLockIDs,
81  reference_configuration=initialJointConfig,
82 )
83 
84 # Check dimensions of the reduced model and joint info
85 print("mixed joints to lock (names and ids):", mixed_jointsToLockIDs)
86 print("RobotWrapper reduced model: dim=" + str(len(reduced_robot.model.joints)))
87 for jn in robot.model.joints:
88  print(jn)
path


pinocchio
Author(s):
autogenerated on Sat Jun 1 2024 02:40:31