simple_model.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2016-2023 CNRS INRIA
3 #
4 
5 from math import pi
6 from typing import ClassVar
7 
8 import pinocchio as pin
9 from pinocchio.utils import np, rotate
10 
11 DENSITY = 1
12 
13 
14 def placement(x=0, y=0, z=0, rx=0, ry=0, rz=0):
15  m = pin.SE3.Identity()
16  m.translation = np.array([x, y, z])
17  m.rotation *= rotate("x", rx)
18  m.rotation *= rotate("y", ry)
19  m.rotation *= rotate("z", rz)
20  return m
21 
22 
23 def color(body_number=1):
24  return [int(i) for i in "%03d" % int(bin(body_number % 8)[2:])] + [1]
25 
26 
28  def __init__(self, name=None, display=False):
29  self.visuals = [
30  ("universe", pin.SE3.Identity(), pin.SE3.Identity().translation)
31  ]
32  self.name = self.__class__.__name__ if name is None else name
33  self.model = pin.Model()
34  self.display = display
35  self.add_joints()
36 
37  def add_joints(self):
38  for joint in self.joints:
39  self.add_joint(**joint)
40 
41  def add_joint(
42  self,
43  joint_name,
44  joint_model=None,
45  joint_placement=None,
46  lever=None,
47  shape="box",
48  dimensions=1,
49  mass=None,
50  body_color=1,
51  parent=0,
52  ):
53  if joint_model is None:
54  joint_model = pin.JointModelFreeFlyer()
55  elif isinstance(joint_model, str):
56  joint_model = pin.__dict__["JointModel" + joint_model]()
57  if joint_placement is None:
58  joint_placement = pin.SE3.Identity()
59  elif isinstance(joint_placement, dict):
60  joint_placement = placement(**joint_placement)
61  if lever is None:
62  lever = pin.SE3.Identity()
63  elif isinstance(lever, dict):
64  lever = placement(**lever)
65  joint_name, body_name = (
66  f"world/{self.name}_{joint_name}_{i}" for i in ("joint", "body")
67  )
68  body_inertia = pin.Inertia.Random()
69  if shape == "box":
70  w, h, d = (
71  (float(i) for i in dimensions)
72  if isinstance(dimensions, tuple)
73  else [float(dimensions)] * 3
74  )
75  if mass is None:
76  mass = w * h * d * DENSITY
77  body_inertia = pin.Inertia.FromBox(mass, w, h, d)
78  if self.display:
79  self.display.viewer.gui.addBox(body_name, w, h, d, color(body_color))
80  elif shape == "cylinder":
81  r, h = dimensions
82  if mass is None:
83  mass = pi * r**2 * h * DENSITY
84  body_inertia = pin.Inertia.FromCylinder(mass, r, h)
85  if self.display:
86  self.display.viewer.gui.addCylinder(body_name, r, h, color(body_color))
87  elif shape == "sphere":
88  w, h, d = (
89  (float(i) for i in dimensions)
90  if isinstance(dimensions, tuple)
91  else [float(dimensions)] * 3
92  )
93  if mass is None:
94  mass = 4.0 / 3.0 * pi * w * h * d * DENSITY
95  body_inertia = pin.Inertia.FromEllipsoid(mass, w, h, d)
96  if self.display:
97  self.display.viewer.gui.addSphere(
98  body_name, dimensions, color(body_color)
99  )
100  body_inertia.lever = lever.translation
101  joint_id = self.model.addJoint(parent, joint_model, joint_placement, joint_name)
102  self.model.appendBodyToJoint(joint_id, body_inertia, pin.SE3.Identity())
103  self.model.addJointFrame(joint_id, -1)
104  self.model.addBodyFrame(body_name, joint_id, pin.SE3.Identity(), -1)
105  self.visuals.append((body_name, joint_placement, lever))
106  self.data = self.model.createData()
107  if self.display:
108  self.place()
109 
110  def place(self):
111  for i, (name, placement, lever) in enumerate(self.visuals):
112  if i == 0:
113  continue
114  self.display.place(name, self.data.oMi[i] * placement * lever)
115  self.display.viewer.gui.refresh()
116 
117 
119  def __init__(self, window_name="pinocchio"):
120  from gepetto import corbaserver
121 
122  self.viewer = corbaserver.Client()
123  try:
124  window_id = self.viewer.gui.getWindowID(window_name)
125  except: # noqa: E722
126  window_id = self.viewer.gui.createWindow(window_name)
127  self.viewer.gui.createSceneWithFloor("world")
128  self.viewer.gui.addSceneToWindow("world", window_id)
129  self.viewer.gui.setLightingMode("world", "OFF")
130  self.viewer.gui.setVisibility("world/floor", "OFF")
131  self.viewer.gui.refresh()
132 
133  def place(self, obj_name, m):
134  self.viewer.gui.applyConfiguration(obj_name, pin.se3ToXYZQUATtuple(m))
135 
136 
138  joints: ClassVar = [
139  {
140  "joint_name": "pelvis",
141  "dimensions": (0.1, 0.2, 0.1),
142  "mass": 0.5,
143  },
144  {
145  "joint_name": "left_leg",
146  "joint_model": "RY",
147  "joint_placement": {"y": -0.15},
148  "lever": {"z": -0.45},
149  "shape": "cylinder",
150  "dimensions": (0.1, 0.9),
151  "mass": 20,
152  "body_color": 2,
153  "parent": 1,
154  },
155  {
156  "joint_name": "right_leg",
157  "joint_model": "RY",
158  "joint_placement": {"y": 0.15},
159  "lever": {"z": -0.45},
160  "shape": "cylinder",
161  "dimensions": (0.1, 0.9),
162  "mass": 20,
163  "body_color": 3,
164  "parent": 1,
165  },
166  ]
167 
168 
169 walker = SimplestWalker()
170 model = walker.model
simple_model.RobotDisplay.place
def place(self, obj_name, m)
Definition: simple_model.py:133
simple_model.RobotDisplay.__init__
def __init__(self, window_name="pinocchio")
Definition: simple_model.py:119
simple_model.RobotDisplay
Definition: simple_model.py:118
simple_model.ModelWrapper
Definition: simple_model.py:27
simple_model.ModelWrapper.name
name
Definition: simple_model.py:32
pinocchio.utils
Definition: bindings/python/pinocchio/utils.py:1
simple_model.placement
def placement(x=0, y=0, z=0, rx=0, ry=0, rz=0)
Definition: simple_model.py:14
pinocchio::createData
ConstraintDataTpl< Scalar, Options, ConstraintCollectionTpl > createData(const ConstraintModelTpl< Scalar, Options, ConstraintCollectionTpl > &cmodel)
Definition: constraint-model-visitor.hpp:239
simple_model.ModelWrapper.data
data
Definition: simple_model.py:95
simple_model.ModelWrapper.add_joint
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:41
simple_model.ModelWrapper.place
def place(self)
Definition: simple_model.py:110
simple_model.RobotDisplay.viewer
viewer
Definition: simple_model.py:122
simple_model.ModelWrapper.display
display
Definition: simple_model.py:34
simple_model.ModelWrapper.add_joints
def add_joints(self)
Definition: simple_model.py:37
simple_model.SimplestWalker
Definition: simple_model.py:137
simple_model.ModelWrapper.__init__
def __init__(self, name=None, display=False)
Definition: simple_model.py:28
boost::fusion::append
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:32
simple_model.color
def color(body_number=1)
Definition: simple_model.py:23
simple_model.ModelWrapper.model
model
Definition: simple_model.py:33
simple_model.ModelWrapper.visuals
visuals
Definition: simple_model.py:29


pinocchio
Author(s):
autogenerated on Sun Dec 22 2024 03:41:12