1 import pinocchio
as pin
10 parser = argparse.ArgumentParser()
11 parser.add_argument(
"--with-cart", help=
"Add a cart at the base of the pendulum to simulate a cart pole system.",
13 parser.add_argument(
"-N", help=
"Number of pendulums compositing the dynamical system.",
15 args = parser.parse_args()
23 geom_model = pin.GeometryModel()
29 cart_length = 5 * cart_radius
31 joint_name =
"joint_cart" 33 geometry_placement = pin.SE3.Identity()
34 geometry_placement.rotation = pin.Quaternion(np.array([0.,0.,1.]),np.array([0.,1.,0.])).
toRotationMatrix()
36 joint_id = model.addJoint(parent_id, pin.JointModelPY(), pin.SE3.Identity(), joint_name)
38 body_inertia = pin.Inertia.FromCylinder(cart_mass,cart_radius,cart_length)
39 body_placement = geometry_placement
40 model.appendBodyToJoint(joint_id,body_inertia,body_placement)
42 shape_cart = fcl.Cylinder(cart_radius, cart_length)
44 geom_cart = pin.GeometryObject(
"shape_cart", joint_id, shape_cart, geometry_placement)
45 geom_cart.meshColor = np.array([1.,0.1,0.1,1.])
46 geom_model.addGeometryObject(geom_cart)
51 shape_base = fcl.Sphere(base_radius)
52 geom_base = pin.GeometryObject(
"base", 0, shape_base, pin.SE3.Identity())
53 geom_base.meshColor = np.array([1.,0.1,0.1,1.])
54 geom_model.addGeometryObject(geom_base)
56 joint_placement = pin.SE3.Identity()
61 joint_name =
"joint_" + str(k+1)
62 joint_id = model.addJoint(parent_id,pin.JointModelRX(),joint_placement,joint_name)
64 body_inertia = pin.Inertia.FromSphere(body_mass,body_radius)
65 body_placement = joint_placement.copy()
66 body_placement.translation[2] = 1.
67 model.appendBodyToJoint(joint_id,body_inertia,body_placement)
69 geom1_name =
"ball_" + str(k+1)
70 shape1 = fcl.Sphere(body_radius)
71 geom1_obj = pin.GeometryObject(geom1_name, joint_id, shape1, body_placement)
72 geom1_obj.meshColor = np.ones((4))
73 geom_model.addGeometryObject(geom1_obj)
75 geom2_name =
"bar_" + str(k+1)
76 shape2 = fcl.Cylinder(body_radius/4.,body_placement.translation[2])
77 shape2_placement = body_placement.copy()
78 shape2_placement.translation[2] /= 2.
80 geom2_obj = pin.GeometryObject(geom2_name, joint_id, shape2, shape2_placement)
81 geom2_obj.meshColor = np.array([0.,0.,0.,1.])
82 geom_model.addGeometryObject(geom2_obj)
85 joint_placement = body_placement.copy()
89 visual_model = geom_model
90 viz = Visualizer(model, geom_model, visual_model)
95 except ImportError
as err:
96 print(
"Error while initializing the viewer. It seems you should install gepetto-viewer")
101 viz.loadViewerModel(
"pinocchio")
102 except AttributeError
as err:
103 print(
"Error while loading the viewer model. It seems you should start gepetto-viewer")
108 q0 = pin.neutral(model)
117 model.lowerPositionLimit.fill(-math.pi)
118 model.upperPositionLimit.fill(+math.pi)
121 model.lowerPositionLimit[0] = model.upperPositionLimit[0] = 0.
123 data_sim = model.createData()
126 q = pin.randomConfiguration(model)
127 v = np.zeros((model.nv))
128 tau_control = np.zeros((model.nv))
133 tau_control = -damping_value * v
134 a = pin.aba(model,data_sim,q,v,tau_control)
138 q = pin.integrate(model,q,v*dt)
144 dt_sleep =
max(0,dt - (ellapsed))
AD< Scalar > max(const AD< Scalar > &x, const AD< Scalar > &y)
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.