9 import pinocchio
as pin
12 parser = argparse.ArgumentParser()
15 help=
"Add a cart at the base of the pendulum to simulate a cart pole system.",
19 "-N", help=
"Number of pendulums compositing the dynamical system.", type=int
21 args = parser.parse_args()
29 geom_model = pin.GeometryModel()
35 cart_length = 5 * cart_radius
37 joint_name =
"joint_cart"
39 geometry_placement = pin.SE3.Identity()
40 geometry_placement.rotation = pin.Quaternion(
41 np.array([0.0, 0.0, 1.0]), np.array([0.0, 1.0, 0.0])
44 joint_id = model.addJoint(
45 parent_id, pin.JointModelPY(), pin.SE3.Identity(), joint_name
48 body_inertia = pin.Inertia.FromCylinder(cart_mass, cart_radius, cart_length)
49 body_placement = geometry_placement
52 model.appendBodyToJoint(joint_id, body_inertia, body_placement)
54 shape_cart = fcl.Cylinder(cart_radius, cart_length)
56 geom_cart = pin.GeometryObject(
57 "shape_cart", joint_id, geometry_placement, shape_cart
59 geom_cart.meshColor = np.array([1.0, 0.1, 0.1, 1.0])
60 geom_model.addGeometryObject(geom_cart)
65 shape_base = fcl.Sphere(base_radius)
66 geom_base = pin.GeometryObject(
"base", 0, pin.SE3.Identity(), shape_base)
67 geom_base.meshColor = np.array([1.0, 0.1, 0.1, 1.0])
68 geom_model.addGeometryObject(geom_base)
70 joint_placement = pin.SE3.Identity()
75 joint_name =
"joint_" + str(k + 1)
76 joint_id = model.addJoint(
77 parent_id, pin.JointModelRX(), joint_placement, joint_name
80 body_inertia = pin.Inertia.FromSphere(body_mass, body_radius)
81 body_placement = joint_placement.copy()
82 body_placement.translation[2] = 1.0
83 model.appendBodyToJoint(joint_id, body_inertia, body_placement)
85 geom1_name =
"ball_" + str(k + 1)
86 shape1 = fcl.Sphere(body_radius)
87 geom1_obj = pin.GeometryObject(geom1_name, joint_id, body_placement, shape1)
88 geom1_obj.meshColor = np.ones(4)
89 geom_model.addGeometryObject(geom1_obj)
91 geom2_name =
"bar_" + str(k + 1)
92 shape2 = fcl.Cylinder(body_radius / 4.0, body_placement.translation[2])
93 shape2_placement = body_placement.copy()
94 shape2_placement.translation[2] /= 2.0
96 geom2_obj = pin.GeometryObject(geom2_name, joint_id, shape2_placement, shape2)
97 geom2_obj.meshColor = np.array([0.0, 0.0, 0.0, 1.0])
98 geom_model.addGeometryObject(geom2_obj)
101 joint_placement = body_placement.copy()
104 visual_model = geom_model
107 viz = Visualizer(model, geom_model, visual_model)
109 except ImportError
as err:
111 "Error while initializing the viewer. "
112 "It seems you should install gepetto-viewer"
118 viz.loadViewerModel(
"pinocchio")
119 except AttributeError
as err:
121 "Error while loading the viewer model. "
122 "It seems you should start gepetto-viewer"
128 q0 = pin.neutral(model)
135 N = math.floor(T / dt)
137 model.lowerPositionLimit.fill(-math.pi)
138 model.upperPositionLimit.fill(+math.pi)
141 model.lowerPositionLimit[0] = model.upperPositionLimit[0] = 0.0
143 data_sim = model.createData()
146 q = pin.randomConfiguration(model)
147 v = np.zeros(model.nv)
148 tau_control = np.zeros(model.nv)
152 tau_control = -damping_value * v
153 a = pin.aba(model, data_sim, q, v, tau_control)
157 q = pin.integrate(model, q, v * dt)
163 dt_sleep =
max(0, dt - (ellapsed))