simulation-pendulum.py
Go to the documentation of this file.
1 import pinocchio as pin
2 import hppfcl as fcl
3 import numpy as np
4 import math
5 import time
6 import sys
7 
8 # Parse input arguments
9 import argparse
10 
11 parser = argparse.ArgumentParser()
12 parser.add_argument(
13  "--with-cart",
14  help="Add a cart at the base of the pendulum to simulate a cart pole system.",
15  action="store_true",
16 )
17 parser.add_argument(
18  "-N", help="Number of pendulums compositing the dynamical system.", type=int
19 )
20 args = parser.parse_args()
21 
22 if args.N:
23  N = args.N
24 else:
25  N = 1 # number of pendulums
26 
27 model = pin.Model()
28 geom_model = pin.GeometryModel()
29 
30 parent_id = 0
31 
32 if args.with_cart:
33  cart_radius = 0.1
34  cart_length = 5 * cart_radius
35  cart_mass = 2.0
36  joint_name = "joint_cart"
37 
38  geometry_placement = pin.SE3.Identity()
39  geometry_placement.rotation = pin.Quaternion(
40  np.array([0.0, 0.0, 1.0]), np.array([0.0, 1.0, 0.0])
42 
43  joint_id = model.addJoint(
44  parent_id, pin.JointModelPY(), pin.SE3.Identity(), joint_name
45  )
46 
47  body_inertia = pin.Inertia.FromCylinder(cart_mass, cart_radius, cart_length)
48  body_placement = geometry_placement
49  model.appendBodyToJoint(
50  joint_id, body_inertia, body_placement
51  ) # We need to rotate the inertia as it is expressed in the LOCAL frame of the geometry
52 
53  shape_cart = fcl.Cylinder(cart_radius, cart_length)
54 
55  geom_cart = pin.GeometryObject(
56  "shape_cart", joint_id, geometry_placement, shape_cart
57  )
58  geom_cart.meshColor = np.array([1.0, 0.1, 0.1, 1.0])
59  geom_model.addGeometryObject(geom_cart)
60 
61  parent_id = joint_id
62 else:
63  base_radius = 0.2
64  shape_base = fcl.Sphere(base_radius)
65  geom_base = pin.GeometryObject("base", 0, pin.SE3.Identity(), shape_base)
66  geom_base.meshColor = np.array([1.0, 0.1, 0.1, 1.0])
67  geom_model.addGeometryObject(geom_base)
68 
69 joint_placement = pin.SE3.Identity()
70 body_mass = 1.0
71 body_radius = 0.1
72 
73 for k in range(N):
74  joint_name = "joint_" + str(k + 1)
75  joint_id = model.addJoint(
76  parent_id, pin.JointModelRX(), joint_placement, joint_name
77  )
78 
79  body_inertia = pin.Inertia.FromSphere(body_mass, body_radius)
80  body_placement = joint_placement.copy()
81  body_placement.translation[2] = 1.0
82  model.appendBodyToJoint(joint_id, body_inertia, body_placement)
83 
84  geom1_name = "ball_" + str(k + 1)
85  shape1 = fcl.Sphere(body_radius)
86  geom1_obj = pin.GeometryObject(geom1_name, joint_id, body_placement, shape1)
87  geom1_obj.meshColor = np.ones((4))
88  geom_model.addGeometryObject(geom1_obj)
89 
90  geom2_name = "bar_" + str(k + 1)
91  shape2 = fcl.Cylinder(body_radius / 4.0, body_placement.translation[2])
92  shape2_placement = body_placement.copy()
93  shape2_placement.translation[2] /= 2.0
94 
95  geom2_obj = pin.GeometryObject(geom2_name, joint_id, shape2_placement, shape2)
96  geom2_obj.meshColor = np.array([0.0, 0.0, 0.0, 1.0])
97  geom_model.addGeometryObject(geom2_obj)
98 
99  parent_id = joint_id
100  joint_placement = body_placement.copy()
101 
102 from pinocchio.visualize import MeshcatVisualizer as Visualizer
103 
104 visual_model = geom_model
105 # Initialize the viewer.
106 try:
107  viz = Visualizer(model, geom_model, visual_model)
108  viz.initViewer()
109 except ImportError as err:
110  print(
111  "Error while initializing the viewer. It seems you should install gepetto-viewer"
112  )
113  print(err)
114  sys.exit(0)
115 
116 try:
117  viz.loadViewerModel("pinocchio")
118 except AttributeError as err:
119  print(
120  "Error while loading the viewer model. It seems you should start gepetto-viewer"
121  )
122  print(err)
123  sys.exit(0)
124 
125 # Display a robot configuration.
126 q0 = pin.neutral(model)
127 viz.display(q0)
128 
129 # Play a bit with the simulation
130 dt = 0.01
131 T = 5
132 
133 N = math.floor(T / dt)
134 
135 model.lowerPositionLimit.fill(-math.pi)
136 model.upperPositionLimit.fill(+math.pi)
137 
138 if args.with_cart:
139  model.lowerPositionLimit[0] = model.upperPositionLimit[0] = 0.0
140 
141 data_sim = model.createData()
142 
143 t = 0.0
144 q = pin.randomConfiguration(model)
145 v = np.zeros((model.nv))
146 tau_control = np.zeros((model.nv))
147 damping_value = 0.1
148 for k in range(N):
149  tic = time.time()
150  tau_control = -damping_value * v # small damping
151  a = pin.aba(model, data_sim, q, v, tau_control) # Forward dynamics
152 
153  # Semi-explicit integration
154  v += a * dt
155  q = pin.integrate(model, q, v * dt) # Configuration integration
156 
157  viz.display(q)
158  toc = time.time()
159  ellapsed = toc - tic
160 
161  dt_sleep = max(0, dt - (ellapsed))
162  time.sleep(dt_sleep)
163  t += dt
pinocchio.visualize
Definition: bindings/python/pinocchio/visualize/__init__.py:1
pinocchio::toRotationMatrix
void toRotationMatrix(const Eigen::MatrixBase< Vector3 > &axis, const Scalar &cos_value, const Scalar &sin_value, const Eigen::MatrixBase< Matrix3 > &res)
Computes a rotation matrix from a vector and values of sin and cos orientations values.
Definition: rotation.hpp:26
CppAD::max
AD< Scalar > max(const AD< Scalar > &x, const AD< Scalar > &y)
Definition: autodiff/cppad.hpp:180


pinocchio
Author(s):
autogenerated on Sat Jun 1 2024 02:40:38