simulation-closed-kinematic-chains.py
Go to the documentation of this file.
1 import time
2 
3 import hppfcl as fcl
4 import numpy as np
5 import pinocchio as pin
6 from pinocchio.visualize import MeshcatVisualizer
7 
8 # Perform the simulation of a four-bar linkages mechanism
9 
10 height = 0.1
11 width = 0.01
12 radius = 0.05
13 
14 mass_link_A = 10.0
15 length_link_A = 1.0
16 shape_link_A = fcl.Capsule(radius, length_link_A)
17 
18 mass_link_B = 5.0
19 length_link_B = 0.6
20 shape_link_B = fcl.Capsule(radius, length_link_B)
21 
22 inertia_link_A = pin.Inertia.FromBox(mass_link_A, length_link_A, width, height)
23 placement_center_link_A = pin.SE3.Identity()
24 placement_center_link_A.translation = pin.XAxis * length_link_A / 2.0
25 placement_shape_A = placement_center_link_A.copy()
26 placement_shape_A.rotation = pin.Quaternion.FromTwoVectors(
27  pin.ZAxis, pin.XAxis
28 ).matrix()
29 
30 inertia_link_B = pin.Inertia.FromBox(mass_link_B, length_link_B, width, height)
31 placement_center_link_B = pin.SE3.Identity()
32 placement_center_link_B.translation = pin.XAxis * length_link_B / 2.0
33 placement_shape_B = placement_center_link_B.copy()
34 placement_shape_B.rotation = pin.Quaternion.FromTwoVectors(
35  pin.ZAxis, pin.XAxis
36 ).matrix()
37 
38 model = pin.Model()
39 collision_model = pin.GeometryModel()
40 
41 RED_COLOR = np.array([1.0, 0.0, 0.0, 1.0])
42 WHITE_COLOR = np.array([1.0, 1.0, 1.0, 1.0])
43 
44 base_joint_id = 0
45 geom_obj0 = pin.GeometryObject(
46  "link_A1",
47  base_joint_id,
48  pin.SE3(pin.Quaternion.FromTwoVectors(pin.ZAxis, pin.XAxis).matrix(), np.zeros(3)),
49  shape_link_A,
50 )
51 geom_obj0.meshColor = WHITE_COLOR
52 collision_model.addGeometryObject(geom_obj0)
53 
54 joint1_placement = pin.SE3.Identity()
55 joint1_placement.translation = pin.XAxis * length_link_A / 2.0
56 joint1_id = model.addJoint(
57  base_joint_id, pin.JointModelRY(), joint1_placement, "link_B1"
58 )
59 model.appendBodyToJoint(joint1_id, inertia_link_B, placement_center_link_B)
60 geom_obj1 = pin.GeometryObject("link_B1", joint1_id, placement_shape_B, shape_link_B)
61 geom_obj1.meshColor = RED_COLOR
62 collision_model.addGeometryObject(geom_obj1)
63 
64 joint2_placement = pin.SE3.Identity()
65 joint2_placement.translation = pin.XAxis * length_link_B
66 joint2_id = model.addJoint(joint1_id, pin.JointModelRY(), joint2_placement, "link_A2")
67 model.appendBodyToJoint(joint2_id, inertia_link_A, placement_center_link_A)
68 geom_obj2 = pin.GeometryObject("link_A2", joint2_id, placement_shape_A, shape_link_A)
69 geom_obj2.meshColor = WHITE_COLOR
70 collision_model.addGeometryObject(geom_obj2)
71 
72 joint3_placement = pin.SE3.Identity()
73 joint3_placement.translation = pin.XAxis * length_link_A
74 joint3_id = model.addJoint(joint2_id, pin.JointModelRY(), joint3_placement, "link_B2")
75 model.appendBodyToJoint(joint3_id, inertia_link_B, placement_center_link_B)
76 geom_obj3 = pin.GeometryObject("link_B2", joint3_id, placement_shape_B, shape_link_B)
77 geom_obj3.meshColor = RED_COLOR
78 collision_model.addGeometryObject(geom_obj3)
79 
80 visual_model = collision_model
81 viz = MeshcatVisualizer(model, collision_model, visual_model)
82 viz.initViewer(open=True)
83 viz.loadViewerModel()
84 
85 q0 = pin.neutral(model)
86 viz.display(q0)
87 
88 data = model.createData()
89 pin.forwardKinematics(model, data, q0)
90 
91 # Set the contact constraints
92 constraint1_joint1_placement = pin.SE3.Identity()
93 constraint1_joint1_placement.translation = pin.XAxis * length_link_B
94 
95 constraint1_joint2_placement = pin.SE3.Identity()
96 constraint1_joint2_placement.translation = -pin.XAxis * length_link_A / 2.0
97 
98 constraint_model = pin.RigidConstraintModel(
99  pin.ContactType.CONTACT_3D,
100  model,
101  joint3_id,
102  constraint1_joint1_placement,
103  base_joint_id,
104  constraint1_joint2_placement,
105 )
106 constraint_data = constraint_model.createData()
107 constraint_dim = constraint_model.size()
108 
109 # First, do an inverse geometry
110 rho = 1e-10
111 mu = 1e-4
112 
113 q = q0.copy()
114 
115 y = np.ones(constraint_dim)
116 data.M = np.eye(model.nv) * rho
117 kkt_constraint = pin.ContactCholeskyDecomposition(model, [constraint_model])
118 eps = 1e-10
119 N = 100
120 for k in range(N):
121  pin.computeJointJacobians(model, data, q)
122  kkt_constraint.compute(model, data, [constraint_model], [constraint_data], mu)
123  constraint_value = constraint_data.c1Mc2.translation
124 
125  J = pin.getFrameJacobian(
126  model,
127  data,
128  constraint_model.joint1_id,
129  constraint_model.joint1_placement,
130  constraint_model.reference_frame,
131  )[:3, :]
132  primal_feas = np.linalg.norm(constraint_value, np.inf)
133  dual_feas = np.linalg.norm(J.T.dot(constraint_value + y), np.inf)
134  if primal_feas < eps and dual_feas < eps:
135  print("Convergence achieved")
136  break
137  print("constraint_value:", np.linalg.norm(constraint_value))
138  rhs = np.concatenate([-constraint_value - y * mu, np.zeros(model.nv)])
139 
140  dz = kkt_constraint.solve(rhs)
141  dy = dz[:constraint_dim]
142  dq = dz[constraint_dim:]
143 
144  alpha = 1.0
145  q = pin.integrate(model, q, -alpha * dq)
146  y -= alpha * (-dy + y)
147 
148 q_sol = (q[:] + np.pi) % np.pi - np.pi
149 viz.display(q_sol)
150 
151 # Perform the simulation
152 q = q_sol.copy()
153 v = np.zeros(model.nv)
154 tau = np.zeros(model.nv)
155 dt = 5e-3
156 
157 T_sim = 100000
158 t = 0
159 mu_sim = 1e-10
160 constraint_model.corrector.Kp[:] = 10
161 constraint_model.corrector.Kd[:] = 2.0 * np.sqrt(constraint_model.corrector.Kp)
162 pin.initConstraintDynamics(model, data, [constraint_model])
163 prox_settings = pin.ProximalSettings(1e-8, mu_sim, 10)
164 
165 while t <= T_sim:
166  a = pin.constraintDynamics(
167  model, data, q, v, tau, [constraint_model], [constraint_data], prox_settings
168  )
169  v += a * dt
170  q = pin.integrate(model, q, v * dt)
171  viz.display(q)
172  time.sleep(dt)
173  t += dt
pinocchio.visualize
Definition: bindings/python/pinocchio/visualize/__init__.py:1
pinocchio.visualize.meshcat_visualizer.MeshcatVisualizer
Definition: meshcat_visualizer.py:576


pinocchio
Author(s):
autogenerated on Mon Dec 16 2024 03:41:04