rviz_visualizer.py
Go to the documentation of this file.
1 import warnings
2 
3 import numpy as np
4 
5 from .. import pinocchio_pywrap_default as pin
6 from ..utils import npToTuple
7 from . import BaseVisualizer
8 
9 try:
10  import hppfcl
11 
12  WITH_HPP_FCL_BINDINGS = True
13 except ImportError:
14  WITH_HPP_FCL_BINDINGS = False
15 
16 
17 def create_capsule_markers(marker_ref, oMg, d, fl):
18  """Make capsule using two sphere and one cylinder"""
19  from copy import deepcopy
20 
21  from geometry_msgs.msg import Point
22  from visualization_msgs.msg import Marker
23 
24  displacment = pin.SE3.Identity()
25 
26  displacment.translation[2] = fl / 2.0
27  oMsphere_1 = oMg * displacment
28  displacment.translation[2] = -fl / 2.0
29  oMsphere_2 = oMg * displacment
30 
31  marker_cylinder = marker_ref
32  marker_cylinder.type = Marker.CYLINDER
33  marker_cylinder.scale = Point(d, d, fl)
34  marker_cylinder.pose = SE3ToROSPose(oMg)
35 
36  marker_sphere_1 = deepcopy(marker_ref)
37  marker_sphere_1.id += 10000 # How to ensure this id is not taken ?
38  marker_sphere_1.type = Marker.SPHERE
39  marker_sphere_1.scale = Point(d, d, d)
40  marker_sphere_1.pose = SE3ToROSPose(oMsphere_1)
41 
42  marker_sphere_2 = deepcopy(marker_ref)
43  marker_sphere_2.id += 20000 # How to ensure this id is not taken ?
44  marker_sphere_2.type = Marker.SPHERE
45  marker_sphere_2.scale = Point(d, d, d)
46  marker_sphere_2.pose = SE3ToROSPose(oMsphere_2)
47 
48  return [marker_cylinder, marker_sphere_1, marker_sphere_2]
49 
50 
51 def SE3ToROSPose(oMg):
52  """Converts SE3 matrix to ROS geometry_msgs/Pose format"""
53  from geometry_msgs.msg import Point, Pose, Quaternion
54 
55  xyz_quat = pin.SE3ToXYZQUATtuple(oMg)
56  return Pose(position=Point(*xyz_quat[:3]), orientation=Quaternion(*xyz_quat[3:]))
57 
58 
59 class RVizVisualizer(BaseVisualizer):
60  """A Pinocchio display using RViz"""
61 
62  class Viewer:
63  app = None
64  viz = None
65  viz_manager = None
66 
68  self,
69  viewer=None,
70  windowName="python-pinocchio",
71  loadModel=False,
72  initRosNode=True,
73  ):
74  """
75  Init RVizViewer by starting a ros node (or not) and creating an RViz window.
76  """
77  from python_qt_binding.QtWidgets import QApplication
78  from rosgraph import is_master_online
79  from rospy import WARN, init_node
80  from rviz import bindings as rviz
81 
82  if not is_master_online(): # Checks the master uri
83  # ROS Master is offline
84  warnings.warn(
85  "Error while importing the viz client.\n"
86  "Check whether ROS master (roscore) is properly started",
87  category=UserWarning,
88  stacklevel=2,
89  )
90  return None
91 
92  if initRosNode:
93  init_node("pinocchio_viewer", anonymous=True, log_level=WARN)
94 
95  if viewer is None:
97  self.viewer.app = QApplication([])
98  self.viewer.viz = rviz.VisualizationFrame()
99  self.viewer.viz.setSplashPath("")
100  self.viewer.viz.initialize()
101  self.viewer.viz.setWindowTitle(windowName + "[*]")
102  self.viewer.viz_manager = self.viewer.viz.getManager()
103  self.viewer.viz.show()
104  else:
105  self.viewer = viewer
106 
107  if loadModel:
108  self.loadViewerModel()
109 
110  return self.viewer
111 
112  def loadViewerModel(self, rootNodeName="pinocchio"):
113  """Create the displays in RViz and create publishers for the MarkerArray"""
114  from rospy import Publisher
115  from visualization_msgs.msg import MarkerArray
116 
117  # Visuals
118  self.visuals_publisher = Publisher(
119  rootNodeName + "_visuals", MarkerArray, queue_size=1, latch=True
120  )
121  self.visual_Display = self.viewer.viz_manager.createDisplay(
122  "rviz/MarkerArray", rootNodeName + "_visuals", True
123  )
124  self.visual_Display.subProp("Marker Topic").setValue(rootNodeName + "_visuals")
125  self.visual_ids = []
126 
127  # Collisions
128  self.collisions_publisher = Publisher(
129  rootNodeName + "_collisions", MarkerArray, queue_size=1, latch=True
130  )
131  self.collision_Display = self.viewer.viz_manager.createDisplay(
132  "rviz/MarkerArray", rootNodeName + "_collisions", True
133  )
134  self.collision_Display.subProp("Marker Topic").setValue(
135  rootNodeName + "_collisions"
136  )
137  self.collision_ids = []
138 
139  # Group
140  root_group = self.viewer.viz_manager.getRootDisplayGroup()
141  self.group_Display = self.viewer.viz_manager.createDisplay(
142  "rviz/Group", rootNodeName, True
143  )
144  self.group_Display.addChild(
145  root_group.takeChild(self.visual_Display)
146  ) # Remove display from root group and add it to robot group
147  self.group_Display.addChild(
148  root_group.takeChild(self.collision_Display)
149  ) # Remove display from root group and add it to robot group
150 
151  self.seq = 0
152  self.display()
153 
154  def clean(self):
155  """Delete all the objects from the whole scene"""
156  if hasattr(self, "collisions_publisher"):
157  self._clean(self.collisions_publisher)
158  self.collision_ids = []
159 
160  if hasattr(self, "visuals_publisher"):
161  self._clean(self.visuals_publisher)
162  self.visual_ids = []
163 
164  def display(self, q=None):
165  """Display the robot at configuration q in the viz by placing all the bodies."""
166  # Update the robot kinematics and geometry.
167  if q is not None:
168  pin.forwardKinematics(self.model, self.data, q)
169 
170  if self.collision_model is not None and hasattr(self, "collisions_publisher"):
171  pin.updateGeometryPlacements(
172  self.model, self.data, self.collision_model, self.collision_data
173  )
174  self.collision_ids = self._plot(
176  self.collision_model,
177  self.collision_data,
178  self.collision_ids,
179  )
180 
181  if self.visual_model is not None and hasattr(self, "visuals_publisher"):
182  pin.updateGeometryPlacements(
183  self.model, self.data, self.visual_model, self.visual_data
184  )
185  self.visual_ids = self._plot(
186  self.visuals_publisher,
187  self.visual_model,
188  self.visual_data,
189  self.visual_ids,
190  )
191 
192  def _plot(self, publisher, model, data, previous_ids=()):
193  """
194  Create markers for each object of the model and publish it as MarkerArray
195  (also delete unused previously created markers)
196  """
197  from geometry_msgs.msg import Point
198  from rospy import get_rostime
199  from std_msgs.msg import ColorRGBA, Header
200  from visualization_msgs.msg import Marker, MarkerArray
201 
202  self.seq += 1
203  header = Header(
204  frame_id="map", seq=self.seq, stamp=get_rostime()
205  ) # Common header for every marker
206 
207  marker_array = MarkerArray()
208  for obj in model.geometryObjects:
209  obj_id = model.getGeometryId(obj.name)
210 
211  # Prepare marker
212  marker = Marker()
213  marker.id = obj_id
214  marker.header = header
215  marker.action = Marker.ADD # same as Marker.MODIFY
216  marker.pose = SE3ToROSPose(data.oMg[obj_id])
217  marker.color = ColorRGBA(*obj.meshColor)
218 
219  if obj.meshTexturePath != "":
220  warnings.warn(
221  "Textures are not supported in RVizVisualizer (for "
222  + obj.name
223  + ")",
224  category=UserWarning,
225  stacklevel=2,
226  )
227 
228  # Create geometry
229  geom = obj.geometry
230  if WITH_HPP_FCL_BINDINGS and isinstance(geom, hppfcl.ShapeBase):
231  # append a primitive geometry
232  if isinstance(geom, hppfcl.Cylinder):
233  d, fl = 2 * geom.radius, 2 * geom.halfLength
234  marker.type = Marker.CYLINDER
235  marker.scale = Point(d, d, fl)
236  marker_array.markers.append(marker)
237  elif isinstance(geom, hppfcl.Box):
238  size = npToTuple(2.0 * geom.halfSide)
239  marker.type = Marker.CUBE
240  marker.scale = Point(*size)
241  marker_array.markers.append(marker)
242  elif isinstance(geom, hppfcl.Sphere):
243  d = 2 * geom.radius
244  marker.type = Marker.SPHERE
245  marker.scale = Point(d, d, d)
246  marker_array.markers.append(marker)
247  elif isinstance(geom, hppfcl.Capsule):
248  d, fl = 2 * geom.radius, 2 * geom.halfLength
249  marker_array.markers.extend(
250  create_capsule_markers(marker, data.oMg[obj_id], d, fl)
251  )
252  else:
253  msg = f"Unsupported geometry type for {obj.name} ({type(geom)})"
254  warnings.warn(msg, category=UserWarning, stacklevel=2)
255  continue
256  else:
257  # append a mesh
258  marker.type = Marker.MESH_RESOURCE # Custom mesh
259  marker.scale = Point(*npToTuple(obj.meshScale))
260  marker.mesh_resource = "file://" + obj.meshPath
261  marker_array.markers.append(marker)
262 
263  # Remove unused markers
264  new_ids = [marker.id for marker in marker_array.markers]
265  for old_id in previous_ids:
266  if old_id not in new_ids:
267  marker_remove = Marker()
268  marker_remove.header = header
269  marker_remove.id = old_id
270  marker_remove.action = Marker.DELETE
271  marker_array.markers.append(marker_remove)
272 
273  # Publish markers
274  publisher.publish(marker_array)
275 
276  # Return list of markers id
277  return new_ids
278 
279  def _clean(self, publisher):
280  """Delete all the markers from a topic (use one marker with action DELETEALL)"""
281  from rospy import get_rostime
282  from std_msgs.msg import Header
283  from visualization_msgs.msg import Marker, MarkerArray
284 
285  # Increment seq number
286  self.seq += 1
287 
288  # Prepare a clean_all marker
289  marker = Marker()
290  marker.header = Header(frame_id="map", seq=self.seq, stamp=get_rostime())
291  marker.action = Marker.DELETEALL
292 
293  # Add the marker to a MarkerArray
294  marker_array = MarkerArray(markers=[marker])
295 
296  # Publish marker
297  publisher.publish(marker_array)
298 
299  def displayCollisions(self, visibility):
300  """Set whether to display collision objects or not"""
301  self.collision_Display.setEnabled(visibility)
302 
303  def displayVisuals(self, visibility):
304  """Set whether to display visual objects or not"""
305  self.visual_Display.setEnabled(visibility)
306 
307  def sleep(self, dt):
308  from python_qt_binding.QtTest import QTest
309 
310  QTest.qWait(1e3 * dt)
311 
313  raise NotImplementedError()
314 
315  def setCameraTarget(self, target):
316  raise NotImplementedError()
317 
318  def setCameraPosition(self, position: np.ndarray):
319  raise NotImplementedError()
320 
321  def setCameraZoom(self, zoom: float):
322  raise NotImplementedError()
323 
324  def setCameraPose(self, pose: np.ndarray):
325  raise NotImplementedError()
326 
327  def captureImage(self, w=None, h=None):
328  raise NotImplementedError()
329 
331  raise NotImplementedError()
332 
334  raise NotImplementedError()
335 
336  def drawFrameVelocities(self, *args, **kwargs):
337  raise NotImplementedError()
338 
339 
340 __all__ = ["RVizVisualizer"]
pinocchio.visualize.rviz_visualizer.RVizVisualizer.visual_ids
visual_ids
Definition: rviz_visualizer.py:125
pinocchio.visualize.rviz_visualizer.RVizVisualizer._plot
def _plot(self, publisher, model, data, previous_ids=())
Definition: rviz_visualizer.py:192
pinocchio.visualize.rviz_visualizer.RVizVisualizer.setCameraZoom
def setCameraZoom(self, float zoom)
Definition: rviz_visualizer.py:321
pinocchio.visualize.rviz_visualizer.RVizVisualizer.group_Display
group_Display
Definition: rviz_visualizer.py:141
pinocchio.visualize.rviz_visualizer.RVizVisualizer.setCameraTarget
def setCameraTarget(self, target)
Definition: rviz_visualizer.py:315
pinocchio.visualize.rviz_visualizer.RVizVisualizer.seq
seq
Definition: rviz_visualizer.py:151
pinocchio.visualize.rviz_visualizer.RVizVisualizer.Viewer
Definition: rviz_visualizer.py:62
pinocchio.visualize.rviz_visualizer.RVizVisualizer.enableCameraControl
def enableCameraControl(self)
Definition: rviz_visualizer.py:333
pinocchio.visualize.rviz_visualizer.RVizVisualizer.clean
def clean(self)
Definition: rviz_visualizer.py:154
pinocchio.visualize.rviz_visualizer.RVizVisualizer.setCameraPose
def setCameraPose(self, np.ndarray pose)
Definition: rviz_visualizer.py:324
pinocchio.visualize.rviz_visualizer.RVizVisualizer
Definition: rviz_visualizer.py:59
pinocchio::python::context::Quaternion
Eigen::Quaternion< Scalar, Options > Quaternion
Definition: bindings/python/context/generic.hpp:49
pinocchio.visualize.rviz_visualizer.RVizVisualizer.disableCameraControl
def disableCameraControl(self)
Definition: rviz_visualizer.py:330
pinocchio.visualize.rviz_visualizer.RVizVisualizer.displayVisuals
def displayVisuals(self, visibility)
Definition: rviz_visualizer.py:303
pinocchio.visualize.rviz_visualizer.RVizVisualizer.setCameraPosition
def setCameraPosition(self, np.ndarray position)
Definition: rviz_visualizer.py:318
pinocchio.visualize.rviz_visualizer.RVizVisualizer.setBackgroundColor
def setBackgroundColor(self)
Definition: rviz_visualizer.py:312
pinocchio.visualize.rviz_visualizer.RVizVisualizer.sleep
def sleep(self, dt)
Definition: rviz_visualizer.py:307
pinocchio.visualize.rviz_visualizer.RVizVisualizer.captureImage
def captureImage(self, w=None, h=None)
Definition: rviz_visualizer.py:327
pinocchio.visualize.rviz_visualizer.RVizVisualizer.visuals_publisher
visuals_publisher
Definition: rviz_visualizer.py:118
pinocchio.visualize.rviz_visualizer.RVizVisualizer.displayCollisions
def displayCollisions(self, visibility)
Definition: rviz_visualizer.py:299
pinocchio.visualize.rviz_visualizer.RVizVisualizer.drawFrameVelocities
def drawFrameVelocities(self, *args, **kwargs)
Definition: rviz_visualizer.py:336
pinocchio.utils.npToTuple
def npToTuple(M)
Definition: bindings/python/pinocchio/utils.py:22
pinocchio.visualize.rviz_visualizer.RVizVisualizer._clean
def _clean(self, publisher)
Definition: rviz_visualizer.py:279
pinocchio.visualize.rviz_visualizer.RVizVisualizer.initViewer
def initViewer(self, viewer=None, windowName="python-pinocchio", loadModel=False, initRosNode=True)
Definition: rviz_visualizer.py:67
pinocchio.visualize.rviz_visualizer.RVizVisualizer.collision_ids
collision_ids
Definition: rviz_visualizer.py:137
pinocchio.visualize.rviz_visualizer.RVizVisualizer.loadViewerModel
def loadViewerModel(self, rootNodeName="pinocchio")
Definition: rviz_visualizer.py:112
pinocchio.visualize.rviz_visualizer.create_capsule_markers
def create_capsule_markers(marker_ref, oMg, d, fl)
Definition: rviz_visualizer.py:17
pinocchio.visualize.rviz_visualizer.SE3ToROSPose
def SE3ToROSPose(oMg)
Definition: rviz_visualizer.py:51
pinocchio.visualize.rviz_visualizer.RVizVisualizer.visual_Display
visual_Display
Definition: rviz_visualizer.py:121
pinocchio.visualize.rviz_visualizer.RVizVisualizer.viewer
viewer
Definition: rviz_visualizer.py:90
pinocchio.visualize.rviz_visualizer.RVizVisualizer.collision_Display
collision_Display
Definition: rviz_visualizer.py:131
pinocchio.visualize.rviz_visualizer.RVizVisualizer.display
def display(self, q=None)
Definition: rviz_visualizer.py:164
pinocchio.visualize.rviz_visualizer.RVizVisualizer.collisions_publisher
collisions_publisher
Definition: rviz_visualizer.py:128


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