simulation-contact-dynamics.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 numpy as np
7 import pinocchio as pin
8 from pinocchio.visualize import MeshcatVisualizer
9 
10 # Load the URDF model.
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 = "talos_reduced.urdf"
16 urdf_model_path = model_path / "talos_data/robots" / urdf_filename
17 srdf_filename = "talos.srdf"
18 srdf_full_path = model_path / "talos_data/srdf" / srdf_filename
19 
20 model, collision_model, visual_model = pin.buildModelsFromUrdf(
21  urdf_model_path, mesh_dir, pin.JointModelFreeFlyer()
22 )
23 
24 # Start a new MeshCat server and client.
25 # Note: the server can also be started separately using the "meshcat-server" command in
26 # a terminal:
27 # this enables the server to remain active after the current script ends.
28 #
29 # Option open=True pens the visualizer.
30 # Note: the visualizer can also be opened seperately by visiting the provided URL.
31 try:
32  viz = MeshcatVisualizer(model, collision_model, visual_model)
33  viz.initViewer(open=False)
34 except ImportError as err:
35  print(
36  "Error while initializing the viewer. "
37  "It seems you should install Python meshcat"
38  )
39  print(err)
40  sys.exit(0)
41 
42 # Load the robot in the viewer.
43 viz.loadViewerModel()
44 
45 # Display a robot configuration.
46 pin.loadReferenceConfigurations(model, srdf_full_path)
47 q0 = model.referenceConfigurations["half_sitting"]
48 q_ref = pin.integrate(model, q0, 0.1 * np.random.rand(model.nv))
49 viz.display(q0)
50 
51 feet_name = ["left_sole_link", "right_sole_link"]
52 frame_ids = [model.getFrameId(frame_name) for frame_name in feet_name]
53 
54 v0 = np.zeros(model.nv)
55 v_ref = v0.copy()
56 data_sim = model.createData()
57 data_control = model.createData()
58 
59 contact_models = []
60 contact_datas = []
61 
62 
63 for frame_id in frame_ids:
64  frame = model.frames[frame_id]
65  contact_model = pin.RigidConstraintModel(
66  pin.ContactType.CONTACT_6D, model, frame.parentJoint, frame.placement
67  )
68 
69  contact_models.append(contact_model)
70  contact_datas.append(contact_model.createData())
71 
72 num_constraints = len(frame_ids)
73 contact_dim = 6 * num_constraints
74 
75 pin.initConstraintDynamics(model, data_sim, contact_models)
76 
77 t = 0
78 dt = 5e-3
79 
80 S = np.zeros((model.nv - 6, model.nv))
81 S.T[6:, :] = np.eye(model.nv - 6)
82 Kp_posture = 30.0
83 Kv_posture = 0.05 * math.sqrt(Kp_posture)
84 
85 q = q0.copy()
86 v = v0.copy()
87 tau = np.zeros(model.nv)
88 
89 T = 5
90 
91 while t <= T:
92  print("t:", t)
93  t += dt
94 
95  tic = time.time()
96  J_constraint = np.zeros((contact_dim, model.nv))
97  pin.computeJointJacobians(model, data_control, q)
98  constraint_index = 0
99  for k in range(num_constraints):
100  contact_model = contact_models[k]
101  J_constraint[constraint_index : constraint_index + 6, :] = pin.getFrameJacobian(
102  model,
103  data_control,
104  contact_model.joint1_id,
105  contact_model.joint1_placement,
106  contact_model.reference_frame,
107  )
108  constraint_index += 6
109 
110  A = np.vstack((S, J_constraint))
111  b = pin.rnea(model, data_control, q, v, np.zeros(model.nv))
112 
113  sol = np.linalg.lstsq(A.T, b, rcond=None)[0]
114  tau = np.concatenate((np.zeros((6)), sol[: model.nv - 6]))
115 
116  tau[6:] += (
117  -Kp_posture * (pin.difference(model, q_ref, q))[6:]
118  - Kv_posture * (v - v_ref)[6:]
119  )
120 
121  prox_settings = pin.ProximalSettings(1e-12, 1e-12, 10)
122  a = pin.constraintDynamics(
123  model, data_sim, q, v, tau, contact_models, contact_datas, prox_settings
124  )
125  print("a:", a.T)
126  print("v:", v.T)
127  print("constraint:", np.linalg.norm(J_constraint @ a))
128  print("iter:", prox_settings.iter)
129 
130  v += a * dt
131  q = pin.integrate(model, q, v * dt)
132 
133  viz.display(q)
134  elapsed_time = time.time() - tic
135 
136  time.sleep(max(0, dt - elapsed_time))
137  # input()
pinocchio.visualize
Definition: bindings/python/pinocchio/visualize/__init__.py:1
pinocchio.visualize.meshcat_visualizer.MeshcatVisualizer
Definition: meshcat_visualizer.py:540
CppAD::max
AD< Scalar > max(const AD< Scalar > &x, const AD< Scalar > &y)
Definition: autodiff/cppad.hpp:181


pinocchio
Author(s):
autogenerated on Sun Nov 10 2024 03:43:01