simple_model.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2016-2023 CNRS INRIA
3 #
4 
5 import pinocchio as pin
6 from math import pi
7 from pinocchio.utils import np, rotate
8 
9 DENSITY = 1
10 
11 
12 def placement(x=0, y=0, z=0, rx=0, ry=0, rz=0):
13  m = pin.SE3.Identity()
14  m.translation = np.array([x, y, z])
15  m.rotation *= rotate('x', rx)
16  m.rotation *= rotate('y', ry)
17  m.rotation *= rotate('z', rz)
18  return m
19 
20 
21 def color(body_number=1):
22  return [int(i) for i in '%03d' % int(bin(body_number % 8)[2:])] + [1]
23 
24 
25 class ModelWrapper(object):
26  def __init__(self, name=None, display=False):
27  self.visuals = [('universe', pin.SE3.Identity(), pin.SE3.Identity().translation)]
28  self.name = self.__class__.__name__ if name is None else name
29  self.model = pin.Model()
30  self.display = display
31  self.add_joints()
32 
33  def add_joints(self):
34  for joint in self.joints:
35  self.add_joint(**joint)
36 
37  def add_joint(self, joint_name, joint_model=None, joint_placement=None, lever=None, shape="box",
38  dimensions=1, mass=None, body_color=1, parent=0):
39  if joint_model is None:
40  joint_model = pin.JointModelFreeFlyer()
41  elif isinstance(joint_model, str):
42  joint_model = pin.__dict__['JointModel' + joint_model]()
43  if joint_placement is None:
44  joint_placement = pin.SE3.Identity()
45  elif isinstance(joint_placement, dict):
46  joint_placement = placement(**joint_placement)
47  if lever is None:
48  lever = pin.SE3.Identity()
49  elif isinstance(lever, dict):
50  lever = placement(**lever)
51  joint_name, body_name = ('world/%s_%s_%s' % (self.name, joint_name, i) for i in ('joint', 'body'))
52  body_inertia = pin.Inertia.Random()
53  if shape == "box":
54  w, h, d = (float(i) for i in dimensions) if isinstance(dimensions, tuple) else [float(dimensions)] * 3
55  if mass is None:
56  mass = w * h * d * DENSITY
57  body_inertia = pin.Inertia.FromBox(mass, w, h, d)
58  if self.display:
59  self.display.viewer.gui.addBox(body_name, w, h, d, color(body_color))
60  elif shape == "cylinder":
61  r, h = dimensions
62  if mass is None:
63  mass = pi * r ** 2 * h * DENSITY
64  body_inertia = pin.Inertia.FromCylinder(mass, r, h)
65  if self.display:
66  self.display.viewer.gui.addCylinder(body_name, r, h, color(body_color))
67  elif shape == "sphere":
68  w, h, d = (float(i) for i in dimensions) if isinstance(dimensions, tuple) else [float(dimensions)] * 3
69  if mass is None:
70  mass = 4. / 3. * pi * w * h * d * DENSITY
71  body_inertia = pin.Inertia.FromEllipsoid(mass, w, h, d)
72  if self.display:
73  self.display.viewer.gui.addSphere(body_name, dimensions, color(body_color))
74  body_inertia.lever = lever.translation
75  joint_id = self.model.addJoint(parent, joint_model, joint_placement, joint_name)
76  self.model.appendBodyToJoint(joint_id, body_inertia, pin.SE3.Identity())
77  self.model.addJointFrame(joint_id, -1)
78  self.model.addBodyFrame(body_name, joint_id, pin.SE3.Identity(),-1)
79  self.visuals.append((body_name, joint_placement, lever))
80  self.data = self.model.createData()
81  if self.display:
82  self.place()
83 
84  def place(self):
85  for i, (name, placement, lever) in enumerate(self.visuals):
86  if i == 0:
87  continue
88  self.display.place(name, self.data.oMi[i] * placement * lever)
89  self.display.viewer.gui.refresh()
90 
91 
92 class RobotDisplay(object):
93  def __init__(self, window_name="pinocchio"):
94  from gepetto import corbaserver
95 
96  self.viewer = corbaserver.Client()
97  try:
98  window_id = self.viewer.gui.getWindowID(window_name)
99  except:
100  window_id = self.viewer.gui.createWindow(window_name)
101  self.viewer.gui.createSceneWithFloor("world")
102  self.viewer.gui.addSceneToWindow("world", window_id)
103  self.viewer.gui.setLightingMode('world', 'OFF')
104  self.viewer.gui.setVisibility('world/floor', "OFF")
105  self.viewer.gui.refresh()
106 
107  def place(self, obj_name, m):
108  self.viewer.gui.applyConfiguration(obj_name,pin.se3ToXYZQUATtuple(m))
109 
110 
112  joints = [
113  {'joint_name': "pelvis",
114  'dimensions': (.1, .2, .1),
115  'mass': .5,
116  },
117  {'joint_name': "left_leg",
118  'joint_model': "RY",
119  'joint_placement': {'y': -.15},
120  'lever': {'z': -.45},
121  'shape': "cylinder",
122  'dimensions': (.1, .9),
123  'mass': 20,
124  'body_color': 2,
125  'parent': 1,
126  },
127  {'joint_name': "right_leg",
128  'joint_model': "RY",
129  'joint_placement': {'y': .15},
130  'lever': {'z': -.45},
131  'shape': "cylinder",
132  'dimensions': (.1, .9),
133  'mass': 20,
134  'body_color': 3,
135  'parent': 1,
136  },
137  ]
138 
139 walker = SimplestWalker()
140 model = walker.model
result_of::push_front< V const, T >::type append(T const &t, V const &v)
Append the element T at the front of boost fusion vector V.
Definition: fusion.hpp:24
def color(body_number=1)
Definition: simple_model.py:21
JointDataTpl< Scalar, Options, JointCollectionTpl > createData(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through CreateData visitor to create a JointDataTpl.
def placement(x=0, y=0, z=0, rx=0, ry=0, rz=0)
Definition: simple_model.py:12
def add_joint(self, joint_name, joint_model=None, joint_placement=None, lever=None, shape="box", dimensions=1, mass=None, body_color=1, parent=0)
Definition: simple_model.py:38
def __init__(self, name=None, display=False)
Definition: simple_model.py:26
def place(self, obj_name, m)
def __init__(self, window_name="pinocchio")
Definition: simple_model.py:93


pinocchio
Author(s):
autogenerated on Fri Jun 23 2023 02:38:32