mimic_dynamics.py
Go to the documentation of this file.
1 from pathlib import Path
2 
3 import numpy as np
4 import pinocchio
5 
6 # Get URDF model
7 pinocchio_model_dir = Path(__file__).parent.parent / "models"
8 model_path = pinocchio_model_dir / "baxter_simple.urdf"
9 model_full = pinocchio.buildModelFromUrdf(model_path)
10 
11 # To use the mimic tag from URDF set mimic flag to true
12 # (otherwise the tag will simply be ignored)
13 model_mimic_from_urdf = pinocchio.buildModelFromUrdf(model_path, mimic=True)
14 
15 print(f"{model_full.nq=}")
16 print(f"{model_mimic_from_urdf.nq=}")
17 
18 # Alternatively a "mimic" model can also be made by-hand
19 model_mimic = pinocchio.transformJointIntoMimic(model_full, 9, 10, -1.0, 0.0)
20 model_mimic = pinocchio.transformJointIntoMimic(model_mimic, 18, 19, -1.0, 0.0)
21 
22 print(f"{(model_mimic_from_urdf == model_mimic)=}") # True
23 
24 # Creating manually the G matrix (cf. Featherstone's Rigid Body Dynamics Algorithms,
25 # chapter 10 about Gears if you want to know
26 # more about how joints mimic are handled here)
27 G = np.zeros([model_mimic.nv, model_full.nv])
28 for i in range(model_full.njoints):
29  mimic_scaling = getattr(model_mimic.joints[i].extract(), "scaling", None)
30  if mimic_scaling:
31  G[model_mimic.joints[i].idx_v, model_full.joints[i].idx_v] = mimic_scaling
32  else:
33  # Not a mimic joint
34  G[model_mimic.joints[i].idx_v, model_full.joints[i].idx_v] = 1
35 print("G = ")
36 print(np.array_str(G, precision=0, suppress_small=True, max_line_width=80))
37 
38 # Random data
39 q_mimic = pinocchio.neutral(model_mimic)
40 v_mimic = np.random.random(model_mimic.nv)
41 a_mimic = np.random.random(model_mimic.nv)
42 
43 # Expand configuration using the G matrix for the full model
44 # Possible because offset is 0, otherwise
45 # you need to add it to q of joints mimic
46 q_full = G.transpose() @ q_mimic
47 
48 v_full = G.transpose() @ v_mimic
49 a_full = G.transpose() @ a_mimic
50 
51 # Supported algorithms works as-is
52 data_mimic = model_mimic.createData()
53 data_full = model_full.createData()
54 
55 tau_mimic = pinocchio.rnea(model_mimic, data_mimic, q_mimic, v_mimic, a_mimic)
56 tau_full = pinocchio.rnea(model_full, data_full, q_full, v_full, a_full)
57 print(f"{np.allclose(tau_mimic, G @ tau_full)=}")
58 
59 M_mimic = pinocchio.crba(model_mimic, data_mimic, q_mimic)
60 M_full = pinocchio.crba(model_full, data_full, q_full)
61 print(f"{np.allclose(M_mimic, G @ M_full @ G.transpose())=}")
62 
63 C_mimic = pinocchio.nle(model_mimic, data_mimic, q_mimic, v_mimic)
64 # For forward dynamics, aba does not support joints mimic.
65 # However it's still possible to compute acceleration of the
66 # mimic model, using the equation of motion tau = M*a + C
67 a_computed = np.linalg.solve(M_mimic, tau_mimic - C_mimic)
68 print(f"{np.allclose(a_mimic,a_computed)=}")
pinocchio::transformJointIntoMimic
void transformJointIntoMimic(const ModelTpl< Scalar, Options, JointCollectionTpl > &input_model, const JointIndex &index_mimicked, const JointIndex &index_mimicking, const Scalar &scaling, const Scalar &offset, ModelTpl< Scalar, Options, JointCollectionTpl > &output_model)
Transform of a joint of the model into a mimic joint. Keep the type of the joint as it was previously...
pinocchio::crba
const DataTpl< Scalar, Options, JointCollectionTpl >::MatrixXs & crba(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Convention convention=Convention::LOCAL)
Computes the upper triangular part of the joint space inertia matrix M by using the Composite Rigid B...
pinocchio::rnea
const DataTpl< Scalar, Options, JointCollectionTpl >::TangentVectorType & rnea(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q, const Eigen::MatrixBase< TangentVectorType1 > &v, const Eigen::MatrixBase< TangentVectorType2 > &a, const container::aligned_vector< ForceDerived > &fext)
The Recursive Newton-Euler algorithm. It computes the inverse dynamics, aka the joint torques accordi...
pinocchio::neutral
Eigen::Matrix< typename LieGroupCollection::Scalar, Eigen::Dynamic, 1, LieGroupCollection::Options > neutral(const LieGroupGenericTpl< LieGroupCollection > &lg)
Visit a LieGroupVariant to get the neutral element of it.
pinocchio::python::extract
void extract(const boost::python::list &list, std::vector< T, Allocator > &vec)
Definition: list.hpp:25


pinocchio
Author(s):
autogenerated on Wed Apr 16 2025 02:41:49