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


pinocchio
Author(s):
autogenerated on Tue Jun 1 2021 02:45:04