2 import pinocchio
as pin
4 from os.path import dirname, join, abspath
5 np.set_printoptions(linewidth=np.inf)
50 pinocchio_model_dir = join(dirname(dirname(str(abspath(__file__)))),
"models")
52 model_path = join(pinocchio_model_dir,
"example-robot-data/robots")
53 mesh_dir = pinocchio_model_dir
54 urdf_filename =
"solo12.urdf" 55 urdf_model_path = join(join(model_path,
"solo_description/robots"),
58 model, collision_model, visual_model = pin.buildModelsFromUrdf(
59 urdf_model_path, mesh_dir, pin.JointModelFreeFlyer())
60 data = model.createData()
63 0., 0., 0.235, 0., 0., 0., 1., 0., 0.8, -1.6, 0., -0.8, 1.6, 0., 0.8, -1.6,
66 v0 = np.zeros(model.nv)
67 a0 = np.zeros(model.nv)
72 g_grav = pin.rnea(model, data, q0, v0, a0)
80 feet_names = [
'FL_FOOT',
'FR_FOOT',
'HL_FOOT',
'HR_FOOT']
81 feet_ids = [model.getFrameId(n)
for n
in feet_names]
82 bl_id = model.getFrameId(
'base_link')
83 ncontact = len(feet_names)
88 np.copy(pin.computeFrameJacobian(model, data, q0, id, pin.LOCAL))
92 Js__feet_bl = [np.copy(J[:3, :6])
for J
in Js__feet_q]
95 Jc__feet_bl_T = np.zeros([6, 3 * ncontact])
96 Jc__feet_bl_T[:, :] = np.vstack(Js__feet_bl).T
100 ls = np.linalg.pinv(Jc__feet_bl_T) @ g_bl
103 ls__f = np.split(ls, ncontact)
105 pin.framesForwardKinematics(model, data, q0)
109 for l__f, foot_id
in zip(ls__f, feet_ids):
110 l_sp__f = pin.Force(l__f, np.zeros(3))
111 l_sp__bl = data.oMf[bl_id].actInv(data.oMf[foot_id].
act(l_sp__f))
112 ls__bl.append(np.copy(l_sp__bl.vector))
114 print(
"\n--- CONTACT FORCES ---")
115 for l__f, foot_id, name
in zip(ls__bl, feet_ids, feet_names):
116 print(
"Contact force at foot {} expressed at the BL is: {}".format(
120 print(
"Error between contact forces and gravity at base link: {}".format(
121 np.linalg.norm(g_bl - sum(ls__bl))))
125 Js_feet_j = [np.copy(J[:3, 6:])
for J
in Js__feet_q]
127 Jc__feet_j_T = np.zeros([12, 3 * ncontact])
128 Jc__feet_j_T[:, :] = np.vstack(Js_feet_j).T
131 tau = g_j - Jc__feet_j_T @ ls
137 pin.framesForwardKinematics(model, data, q0)
139 joint_names = [
'FL_KFE',
'FR_KFE',
'HL_KFE',
'HR_KFE']
140 joint_ids = [model.getJointId(n)
for n
in joint_names]
142 fs_ext = [pin.Force(np.zeros(6))
for _
in range(len(model.joints))]
143 for idx, joint
in enumerate(model.joints):
144 if joint.id
in joint_ids:
145 fext__bl = pin.Force(ls__bl[joint_ids.index(joint.id)])
146 fs_ext[idx] = data.oMi[joint.id].actInv(data.oMf[bl_id].
act(fext__bl))
148 tau_rnea = pin.rnea(model, data, q0, v0, a0, fs_ext)
150 print(
"\n--- ID: JOINT TORQUES ---")
151 print(
"Tau from RNEA: {}".format(tau_rnea))
152 print(
"Tau computed manually: {}".format(np.append(np.zeros(6), tau)))
153 print(
"Tau error: {}".format(
154 np.linalg.norm(np.append(np.zeros(6), tau) - tau_rnea)))
158 Js_feet3d_q = [np.copy(J[:3, :])
for J
in Js__feet_q]
159 acc = pin.forwardDynamics(model, data, q0, v0, np.append(np.zeros(6), tau),
160 np.vstack(Js_feet3d_q), np.zeros(12))
162 print(
"\n--- FD: ACC. & CONTACT FORCES ---")
163 print(
"Norm of the FD acceleration: {}".format(np.linalg.norm(acc)))
164 print(
"Contact forces manually: {}".format(ls))
165 print(
"Contact forces FD: {}".format(data.lambda_c))
166 print(
"Contact forces error: {}".format(np.linalg.norm(data.lambda_c - ls)))
static void act(const Eigen::MatrixBase< Mat > &iV, const ForceDense< ForceDerived > &f, Eigen::MatrixBase< MatRet > const &jF)
Action of a motion set on a force object. The input motion set is represented by a 6xN matrix whose e...