Skip to content

Commit 7994078

Browse files
committed
improve logic to determine which backend to use
1 parent 589ef01 commit 7994078

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

roboticstoolbox/robot/Robot.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,40 +1182,65 @@ def control_type(self, cn):
11821182
# --------------------------------------------------------------------- #
11831183

11841184
# TODO probably should be a static method
1185-
def _get_graphical_backend(self, backend):
1185+
def _get_graphical_backend(self, backend=None):
1186+
1187+
default = None
11861188

11871189
# figure out the right default
11881190
if backend is None:
11891191
if isinstance(self, rtb.DHRobot):
1190-
backend = 'pyplot'
1192+
default = 'pyplot'
11911193
elif isinstance(self, rtb.ERobot2):
1192-
backend = 'pyplot2'
1194+
default = 'pyplot2'
11931195
else:
1194-
backend = 'swift'
1196+
default = 'swift'
1197+
1198+
if backend is not None:
1199+
backend = backend.lower()
11951200

1196-
#
11971201
# find the right backend, modules are imported here on an as needs
11981202
# basis
1199-
if backend.lower() == 'swift': # pragma nocover
1203+
if backend == 'swift' or default == 'swift': # pragma nocover
1204+
# swift was requested, is it installed?
12001205
if isinstance(self, rtb.DHRobot):
12011206
raise NotImplementedError(
12021207
'Plotting in Swift is not implemented for DHRobots yet')
1208+
try:
1209+
# yes, use it
1210+
from roboticstoolbox.backends.Swift import Swift
1211+
env = Swift()
1212+
return env
1213+
except ModuleNotFoundError:
1214+
if backend == 'swift':
1215+
print('Swift is not installed, install it using pip or conda')
1216+
backend = 'pyplot'
12031217

1204-
from roboticstoolbox.backends.Swift import Swift
1205-
env = Swift()
1218+
elif backend == 'vpython' or default == 'vpython': # pragma nocover
1219+
# vpython was requested, is it installed?
1220+
if not isinstance(self, rtb.DHRobot):
1221+
raise NotImplementedError(
1222+
'Plotting in VPython is only implemented for DHRobots')
1223+
try:
1224+
# yes, use it
1225+
from roboticstoolbox.backends.VPython import VPython
1226+
env = VPython()
1227+
return env
1228+
except ModuleNotFoundError:
1229+
if backend == 'vpython':
1230+
print('VPython is not installed, install it using pip or conda')
1231+
backend = 'pyplot'
12061232

1207-
elif backend.lower() == 'pyplot':
1233+
if backend is None:
1234+
backend = default
1235+
1236+
if backend == 'pyplot':
12081237
from roboticstoolbox.backends.PyPlot import PyPlot
12091238
env = PyPlot()
12101239

1211-
elif backend.lower() == 'pyplot2':
1240+
elif backend == 'pyplot2':
12121241
from roboticstoolbox.backends.PyPlot import PyPlot2
12131242
env = PyPlot2()
12141243

1215-
elif backend.lower() == 'vpython':
1216-
from roboticstoolbox.backends.VPython import VPython
1217-
env = VPython()
1218-
12191244
else:
12201245
raise ValueError('unknown backend', backend)
12211246

0 commit comments

Comments
 (0)