4 from pathlib
import Path
7 import pinocchio
as pin
11 pinocchio_model_dir = Path(__file__).parent.parent /
"models"
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
20 model, collision_model, visual_model = pin.buildModelsFromUrdf(
21 urdf_model_path, mesh_dir, pin.JointModelFreeFlyer()
33 viz.initViewer(open=
False)
34 except ImportError
as err:
36 "Error while initializing the viewer. "
37 "It seems you should install Python meshcat"
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))
51 feet_name = [
"left_sole_link",
"right_sole_link"]
52 frame_ids = [model.getFrameId(frame_name)
for frame_name
in feet_name]
54 v0 = np.zeros(model.nv)
56 data_sim = model.createData()
57 data_control = model.createData()
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
69 contact_models.append(contact_model)
70 contact_datas.append(contact_model.createData())
72 num_constraints = len(frame_ids)
73 contact_dim = 6 * num_constraints
75 pin.initConstraintDynamics(model, data_sim, contact_models)
80 S = np.zeros((model.nv - 6, model.nv))
81 S.T[6:, :] = np.eye(model.nv - 6)
83 Kv_posture = 0.05 * math.sqrt(Kp_posture)
87 tau = np.zeros(model.nv)
96 J_constraint = np.zeros((contact_dim, model.nv))
97 pin.computeJointJacobians(model, data_control, q)
99 for k
in range(num_constraints):
100 contact_model = contact_models[k]
101 J_constraint[constraint_index : constraint_index + 6, :] = pin.getFrameJacobian(
104 contact_model.joint1_id,
105 contact_model.joint1_placement,
106 contact_model.reference_frame,
108 constraint_index += 6
110 A = np.vstack((S, J_constraint))
111 b = pin.rnea(model, data_control, q, v, np.zeros(model.nv))
113 sol = np.linalg.lstsq(A.T, b, rcond=
None)[0]
114 tau = np.concatenate((np.zeros((6)), sol[: model.nv - 6]))
117 -Kp_posture * (pin.difference(model, q_ref, q))[6:]
118 - Kv_posture * (v - v_ref)[6:]
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
127 print(
"constraint:", np.linalg.norm(J_constraint @ a))
128 print(
"iter:", prox_settings.iter)
131 q = pin.integrate(model, q, v * dt)
134 elapsed_time = time.time() - tic
136 time.sleep(
max(0, dt - elapsed_time))