Skip to content

Commit

Permalink
Visualize links using lines rather than cylinders and allow user to
Browse files Browse the repository at this point in the history
define several visualizer parameters.
  • Loading branch information
cmower committed Feb 7, 2023
1 parent fa5fa78 commit 617e216
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
8 changes: 7 additions & 1 deletion example/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
robot = optas.RobotModel(urdf_filename=urdf_filename)

q = optas.deg2rad([0, 45, 0, -90, 0, -45, 0])
optas.RobotVisualizer(robot, q=q, alpha=0.5, show_link_names=False).start()
params = {
'alpha': 0.5,
'show_link_names': True,
'show_links': True,
'link_axis_scale': 0.3,
}
optas.RobotVisualizer(robot, q=q, params=params).start()
64 changes: 45 additions & 19 deletions optas/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,31 @@
from .spatialmath import *
from urdf_parser_py.urdf import Mesh, Cylinder, Sphere

default_configurable_parameters = {
'show_global_link': True,
'alpha': 1.,
'show_link_names': False,
'show_links': False,
'link_axis_scale': 0.1,
'link_center_radius': 0.01,
}

class RobotVisualizer:

def __init__(self, robot, q=None, show_global_link=True, alpha=1., show_link_names=False):
def __init__(self, robot, q=None, params=None):


# Specify parameters
user_defined_parameters = params.copy()
parameters = default_configurable_parameters.copy()

if isinstance(user_defined_parameters, dict):

# Overwrite parameters
for key, value in user_defined_parameters.items():
parameters[key] = value

self.params = parameters

self.init_robot(robot)

Expand All @@ -29,14 +51,14 @@ def __init__(self, robot, q=None, show_global_link=True, alpha=1., show_link_nam
self.iren.SetInteractorStyle(style)

self.draw_grid_floor()
if show_global_link:
if self.params['show_global_link']:
self.draw_link(I4()) # global link

if q is None:
q = [0.]*self.robot.ndof
self.draw_robot(q, alpha)
self.draw_robot(q, self.params['alpha'])

if show_link_names:
if self.params['show_link_names']:
for name, Tf in self.link_tf.items():

tf = Tf(q)
Expand All @@ -45,6 +67,11 @@ def __init__(self, robot, q=None, show_global_link=True, alpha=1., show_link_nam
actor = self.create_text_actor(name, p, scale=[2*0.001, 2*0.001, 2*0.001])
self.ren.AddActor(actor)

if self.params['show_links']:
for name, Tf in self.link_tf.items():
self.draw_link(Tf(q))


def init_robot(self, robot):

# Setup robot attributes
Expand Down Expand Up @@ -270,11 +297,7 @@ def draw_grid_floor(self, ncells=10):

def draw_link(self, T):

scale = 0.1

axis_scale = 1 * scale
center_radius = 0.075 * scale
axis_radius = 0.03 * scale
scale = self.params['link_axis_scale']

# Extract data
p = T[:3, 3].toarray().flatten()
Expand All @@ -284,7 +307,7 @@ def draw_link(self, T):

# Draw a sphere to represent link center
center = vtkSphereSource()
center.SetRadius(center_radius)
center.SetRadius(self.params['link_center_radius'])
center.SetThetaResolution(20)
center.SetPhiResolution(20)
mapper = vtk.vtkPolyDataMapper()
Expand All @@ -297,17 +320,20 @@ def draw_link(self, T):
self.ren.AddActor(actor)

# Draw axes
xaxis = self.create_cylinder_actor(p, p+axis_scale*x, axis_radius)
xaxis.GetProperty().SetColor(1, 0, 0)
self.ren.AddActor(xaxis)
actor = self.create_line_actor(p, p+scale*x)
actor.GetProperty().SetColor(1, 0, 0)
actor.GetProperty().SetLineWidth(5.0)
self.ren.AddActor(actor)

yaxis = self.create_cylinder_actor(p, p+axis_scale*y, axis_radius)
yaxis.GetProperty().SetColor(0, 1, 0)
self.ren.AddActor(yaxis)
actor = self.create_line_actor(p, p+scale*y)
actor.GetProperty().SetColor(0, 1, 0)
actor.GetProperty().SetLineWidth(5.0)
self.ren.AddActor(actor)

zaxis = self.create_cylinder_actor(p, p+axis_scale*z, axis_radius)
zaxis.GetProperty().SetColor(0, 0, 1)
self.ren.AddActor(zaxis)
actor = self.create_line_actor(p, p+scale*z)
actor.GetProperty().SetColor(0, 0, 1)
actor.GetProperty().SetLineWidth(5.0)
self.ren.AddActor(actor)

def start(self):
# Enable user interface interactor
Expand Down

0 comments on commit 617e216

Please sign in to comment.