8000 plotting backends · wjtscn/robotics-toolbox-python@fe75d3d · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit fe75d3d

Browse files
committed
plotting backends
1 parent ce41848 commit fe75d3d

File tree

5 files changed

+467
-80
lines changed

5 files changed

+467
-80
lines changed

examples/plot_swift.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99

1010
panda = rp.models.DH.Panda()
1111

12-
panda.plot()
12+
panda.plot(backend='pyplot', q=panda.qr, vellipse=True)

roboticstoolbox/backends/PyPlot/functions.py

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,68 +16,68 @@
1616
_pil_exists = False
1717

1818

19-
def _plot(
20-
robot, block, q, dt, limits=None,
21-
vellipse=False, fellipse=False,
22-
jointaxes=True, eeframe=True, shadow=True, name=True, movie=None):
23-
24-
# Make an empty 3D figure
25-
env = rp.backends.PyPlot()
26-
27-
q = getmatrix(q, (None, robot.n))
28-
29-
# Add the robot to the figure in readonly mode
30-
if q.shape[0] == 1:
31-
env.launch(robot.name + ' Plot', limits)
32-
else:
33-
env.launch(robot.name + ' Trajectory Plot', limits)
34-
35-
env.add(
36-
robot, readonly=True,
37-
jointaxes=jointaxes, eeframe=eeframe, shadow=shadow, name=name)
38-
39-
if vellipse:
40-
vell = robot.vellipse(centre='ee')
41-
env.add(vell)
42-
43-
if fellipse:
44-
fell = robot.fellipse(centre='ee')
45-
env.add(fell)
46-
47-
if movie is not None:
48-
if not _pil_exists:
49-
raise RuntimeError(
50-
'to save movies PIL must be installed:\npip3 install PIL')
51-
images = [] # list of images saved from each plot
52-
# make the background white, looks better than grey stipple
53-
env.ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
54-
env.ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
55-
env.ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
56-
57-
for qk in q:
58-
robot.q = qk
59-
env.step()
60-
61-
if movie is not None:
62-
# render the frame and save as a PIL image in the list
63-
canvas = env.fig.canvas
64-
img = PIL.Image.frombytes(
65-
'RGB', canvas.get_width_height(),
66-
canvas.tostring_rgb())
67-
images.append(img)
68-
69-
if movie is not None:
70-
# save it as an animated GIF
71-
images[0].save(
72-
movie,
73-
save_all=True, append_images=images[1:], optimize=False,
74-
duration=dt, loop=0)
75-
76-
# Keep the plot open
77-
if block: # pragma: no cover
78-
env.hold()
79-
80-
return env
19+
# def _plot(
20+
# robot, block, q, dt, limits=None,
21+
# vellipse=False, fellipse=False,
22+
# jointaxes=True, eeframe=True, shadow=True, name=True, movie=None):
23+
24+
# # Make an empty 3D figure
25+
# env = rp.backends.PyPlot()
26+
27+
# q = getmatrix(q, (None, robot.n))
28+
29+
# # Add the robot to the figure in readonly mode
30+
# if q.shape[0] == 1:
31+
# env.launch(robot.name + ' Plot', limits)
32+
# else:
33+
# env.launch(robot.name + ' Trajectory Plot', limits)
34+
35+
# env.add(
36+
# robot, readonly=True,
37+
# jointaxes=jointaxes, eeframe=eeframe, shadow=shadow, name=name)
38+
39+
# if vellipse:
40+
# vell = robot.vellipse(centre='ee')
41+
# env.add(vell)
42+
43+
# if fellipse:
44+
# fell = robot.fellipse(centre='ee')
45+
# env.add(fell)
46+
47+
# if movie is not None:
48+
# if not _pil_exists:
49+
# raise RuntimeError(
50+
# 'to save movies PIL must be installed:\npip3 install PIL')
51+
# images = [] # list of images saved from each plot
52+
# # make the background white, looks better than grey stipple
53+
# env.ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
54+
# env.ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
55+
# env.ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
56+
57+
# for qk in q:
58+
# robot.q = qk
59+
# env.step()
60+
61+
# if movie is not None:
62+
# # render the frame and save as a PIL image in the list
63+
# canvas = env.fig.canvas
64+
# img = PIL.Image.frombytes(
65+
# 'RGB', canvas.get_width_height(),
66+
# canvas.tostring_rgb())
67+
# images.append(img)
68+
69+
# if movie is not None:
70+
# # save it as an animated GIF
71+
# images[0].save(
72+
# movie,
73+
# save_all=True, append_images=images[1:], optimize=False,
74+
# duration=dt, loop=0)
75+
76+
# # Keep the plot open
77+
# if block: # pragma: no cover
78+
# env.hold()
79+
80+
# return env
8181

8282

8383
def _plot2(

roboticstoolbox/backends/Swift/Swift.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,26 @@ def close(self):
191191
# Methods to interface with the robots created in other environemnts
192192
#
193193

194-
def add(self, ob, show_robot=True, show_collision=False):
194+
def add(
195+
self, ob, show_robot=True, show_collision=False,
196+
readonly=False):
195197
"""
196198
Add a robot to the graphical scene
197199
198200
:param ob: the object to add
199-
:type ob: ???
200-
:param show_robot: ????, defaults to True
201+
:type ob: Robot or Shape
202+
:param show_robot: Show the robot visual geometry,
203+
defaults to True
201204
:type show_robot: bool, optional
202-
:param show_collision: ???, defaults to False
205+
:param show_collision: Show the collision geometry,
206+
defaults to False
203207
:type show_collision: bool, optional
204208
:return: object id within visualizer
205209
:rtype: int
210+
:param readonly: If true, swif twill not modify any robot attributes,
211+
the robot is only nbeing displayed, not simulated,
212+
defaults to False
213+
:type readonly: bool, optional
206214
207215
``id = env.add(robot)`` adds the ``robot`` to the graphical
208216
environment.
@@ -226,6 +234,11 @@ def add(self, ob, show_robot=True, show_collision=False):
226234
robot['show_robot'] = show_robot
227235
robot['show_collision'] = show_collision
228236

237+
robot_object = {
238+
'ob': ob,
239+
'readonly': readonly
240+
}
241+
229242
if self.display:
230243
id = self._send_socket('robot', robot)
231244

@@ -236,7 +249,7 @@ def add(self, ob, show_robot=True, show_collision=False):
236249
else:
237250
id = len(self.robots)
238251

239-
self.robots.append(ob)
252+
self.robots.append(robot_object)
240253
return id
241254
elif isinstance(ob, rp.Shape):
242255
shape = ob.to_dict()
@@ -312,12 +325,13 @@ def stop_recording(self):
312325

313326
def _step_robots(self, dt):
314327

315-
for robot in self.robots:
328+
for robot_object in self.robots:
329+
robot = robot_object['ob']
316330

317-
# if rpl.readonly or robot.control_type == 'p':
318-
# pass # pragma: no cover
331+
if robot_object['readonly'] or robot.control_type == 'p':
332+
pass # pragma: no cover
319333

320-
if robot.control_type == 'v':
334+
elif robot.control_type == 'v':
321335

322336
for i in range(robot.n):
323337
robot.q[i] += robot.qd[i] * (dt)
@@ -352,10 +366,12 @@ def _step_shapes(self, dt):
352366
def _draw_all(self):
353367

354368
for i in range(len(self.robots)):
355-
self._send_socket('robot_poses', [i, self.robots[i].fk_dict()])
369+
self._send_socket(
370+
'robot_poses', [i, self.robots[i]['ob'].fk_dict()])
356371

357372
for i in range(len(self.shapes)):
358-
self._send_socket('shape_poses', [i, self.shapes[i].fk_dict()])
373+
self._send_socket(
374+
'shape_poses', [i, self.shapes[i].fk_dict()])
359375

360376
def _send_socket(self, code, data=None):
361377
msg = [code, data]

roboticstoolbox/robot/DHRobot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import spatialmath.base.symbolic as sym
1515
from scipy.optimize import minimize, Bounds
1616
from roboticstoolbox.backends.PyPlot.functions import \
17-
_plot, _teach, _fellipse, _vellipse, _plot_ellipse, \
17+
_teach, _fellipse, _vellipse, _plot_ellipse, \
1818
_plot2, _teach2
1919
from roboticstoolbox.robot.DHDynamics import DHDynamicsMixin
2020
from ansitable import ANSITable, Column

0 commit comments

Comments
 (0)
0