1 from pathlib
import Path
4 import pinocchio
as pin
6 np.set_printoptions(linewidth=np.inf)
55 pinocchio_model_dir = Path(__file__).parent.parent /
"models"
57 model_path = pinocchio_model_dir /
"example-robot-data/robots"
58 mesh_dir = pinocchio_model_dir
59 urdf_filename =
"solo12.urdf"
60 urdf_model_path = model_path /
"solo_description/robots" / urdf_filename
62 model, collision_model, visual_model = pin.buildModelsFromUrdf(
63 urdf_model_path, mesh_dir, pin.JointModelFreeFlyer()
65 data = model.createData()
90 v0 = np.zeros(model.nv)
91 a0 = np.zeros(model.nv)
97 g_grav = pin.rnea(model, data, q0, v0, a0)
106 feet_names = [
"FL_FOOT",
"FR_FOOT",
"HL_FOOT",
"HR_FOOT"]
107 feet_ids = [model.getFrameId(n)
for n
in feet_names]
108 bl_id = model.getFrameId(
"base_link")
109 ncontact = len(feet_names)
114 np.copy(pin.computeFrameJacobian(model, data, q0, id, pin.LOCAL))
for id
in feet_ids
117 Js__feet_bl = [np.copy(J[:3, :6])
for J
in Js__feet_q]
121 Jc__feet_bl_T = np.zeros([6, 3 * ncontact])
122 Jc__feet_bl_T[:, :] = np.vstack(Js__feet_bl).T
126 ls = np.linalg.pinv(Jc__feet_bl_T) @ g_bl
129 ls__f = np.split(ls, ncontact)
131 pin.framesForwardKinematics(model, data, q0)
135 for l__f, foot_id
in zip(ls__f, feet_ids):
136 l_sp__f = pin.Force(l__f, np.zeros(3))
137 l_sp__bl = data.oMf[bl_id].actInv(data.oMf[foot_id].
act(l_sp__f))
138 ls__bl.append(np.copy(l_sp__bl.vector))
140 print(
"\n--- CONTACT FORCES ---")
141 for l__f, foot_id, name
in zip(ls__bl, feet_ids, feet_names):
142 print(f
"Contact force at foot {name} expressed at the BL is: {l__f}")
146 "Error between contact forces and gravity at base link: "
147 f
"{np.linalg.norm(g_bl - sum(ls__bl))}"
152 Js_feet_j = [np.copy(J[:3, 6:])
for J
in Js__feet_q]
154 Jc__feet_j_T = np.zeros([12, 3 * ncontact])
155 Jc__feet_j_T[:, :] = np.vstack(Js_feet_j).T
158 tau = g_j - Jc__feet_j_T @ ls
165 pin.framesForwardKinematics(model, data, q0)
167 joint_names = [
"FL_KFE",
"FR_KFE",
"HL_KFE",
"HR_KFE"]
168 joint_ids = [model.getJointId(n)
for n
in joint_names]
170 fs_ext = [pin.Force(np.zeros(6))
for _
in range(len(model.joints))]
171 for idx, joint
in enumerate(model.joints):
172 if joint.id
in joint_ids:
173 fext__bl = pin.Force(ls__bl[joint_ids.index(joint.id)])
174 fs_ext[idx] = data.oMi[joint.id].actInv(data.oMf[bl_id].
act(fext__bl))
176 tau_rnea = pin.rnea(model, data, q0, v0, a0, fs_ext)
178 print(
"\n--- ID: JOINT TORQUES ---")
179 print(f
"Tau from RNEA: {tau_rnea}")
180 print(f
"Tau computed manually: {np.append(np.zeros(6), tau)}")
181 print(f
"Tau error: {np.linalg.norm(np.append(np.zeros(6), tau) - tau_rnea)}")
186 Js_feet3d_q = [np.copy(J[:3, :])
for J
in Js__feet_q]
187 acc = pin.forwardDynamics(
192 np.append(np.zeros(6), tau),
193 np.vstack(Js_feet3d_q),
197 print(
"\n--- FD: ACC. & CONTACT FORCES ---")
198 print(f
"Norm of the FD acceleration: {np.linalg.norm(acc)}")
199 print(f
"Contact forces manually: {ls}")
200 print(f
"Contact forces FD: {data.lambda_c}")
201 print(f
"Contact forces error: {np.linalg.norm(data.lambda_c - ls)}")