simulation-inverted-pendulum.py
Go to the documentation of this file.
1 import math
2 import sys
3 import time
4 
5 import hppfcl as fcl
6 import numpy as np
7 import pinocchio as pin
8 from pinocchio.visualize import GepettoVisualizer
9 
10 N = 10 # number of pendulums
11 model = pin.Model()
12 geom_model = pin.GeometryModel()
13 
14 parent_id = 0
15 joint_placement = pin.SE3.Identity()
16 body_mass = 1.0
17 body_radius = 0.1
18 
19 shape0 = fcl.Sphere(body_radius)
20 geom0_obj = pin.GeometryObject("base", 0, pin.SE3.Identity(), shape0)
21 geom0_obj.meshColor = np.array([1.0, 0.1, 0.1, 1.0])
22 geom_model.addGeometryObject(geom0_obj)
23 
24 for k in range(N):
25  joint_name = "joint_" + str(k + 1)
26  joint_id = model.addJoint(
27  parent_id, pin.JointModelRY(), joint_placement, joint_name
28  )
29 
30  body_inertia = pin.Inertia.FromSphere(body_mass, body_radius)
31  body_placement = joint_placement.copy()
32  body_placement.translation[2] = 1.0
33  model.appendBodyToJoint(joint_id, body_inertia, body_placement)
34 
35  geom1_name = "ball_" + str(k + 1)
36  shape1 = fcl.Sphere(body_radius)
37  geom1_obj = pin.GeometryObject(geom1_name, joint_id, body_placement, shape1)
38  geom1_obj.meshColor = np.ones(4)
39  geom_model.addGeometryObject(geom1_obj)
40 
41  geom2_name = "bar_" + str(k + 1)
42  shape2 = fcl.Cylinder(body_radius / 4.0, body_placement.translation[2])
43  shape2_placement = body_placement.copy()
44  shape2_placement.translation[2] /= 2.0
45 
46  geom2_obj = pin.GeometryObject(geom2_name, joint_id, shape2_placement, shape2)
47  geom2_obj.meshColor = np.array([0.0, 0.0, 0.0, 1.0])
48  geom_model.addGeometryObject(geom2_obj)
49 
50  parent_id = joint_id
51  joint_placement = body_placement.copy()
52 
53 
54 visual_model = geom_model
55 viz = GepettoVisualizer(model, geom_model, visual_model)
56 
57 # Initialize the viewer.
58 try:
59  viz.initViewer()
60 except ImportError as err:
61  print(
62  "Error while initializing the viewer. "
63  "It seems you should install gepetto-viewer"
64  )
65  print(err)
66  sys.exit(0)
67 
68 try:
69  viz.loadViewerModel("pinocchio")
70 except AttributeError as err:
71  print(
72  "Error while loading the viewer model. "
73  "It seems you should start gepetto-viewer"
74  )
75  print(err)
76  sys.exit(0)
77 
78 # Display a robot configuration.
79 q0 = pin.neutral(model)
80 viz.display(q0)
81 
82 # Play a bit with the simulation
83 dt = 0.01
84 T = 5
85 
86 N = math.floor(T / dt)
87 
88 model.lowerPositionLimit.fill(-math.pi)
89 model.upperPositionLimit.fill(+math.pi)
90 q = pin.randomConfiguration(model)
91 v = np.zeros(model.nv)
92 
93 t = 0.0
94 data_sim = model.createData()
95 for k in range(N):
96  tau_control = np.zeros(model.nv)
97  a = pin.aba(model, data_sim, q, v, tau_control) # Forward dynamics
98 
99  v += a * dt
100  # q += v*dt
101  q = pin.integrate(model, q, v * dt)
102 
103  viz.display(q)
104  time.sleep(dt)
105  t += dt
pinocchio.visualize
Definition: bindings/python/pinocchio/visualize/__init__.py:1
pinocchio.visualize.gepetto_visualizer.GepettoVisualizer
Definition: gepetto_visualizer.py:18


pinocchio
Author(s):
autogenerated on Thu Dec 19 2024 03:41:32