Skip to content

Commit 0d83af5

Browse files
committed
add rtb_set/get_params for global options
use it to control unicode output in various places including ansitable
1 parent 9084b0d commit 0d83af5

File tree

7 files changed

+40
-12
lines changed

7 files changed

+40
-12
lines changed

roboticstoolbox/models/list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Type
22
from roboticstoolbox.robot.Robot import Robot
3+
from roboticstoolbox.tools import rtb_get_param
34
from roboticstoolbox.robot.ERobot import ERobot2
45
from ansitable import ANSITable, Column
56

@@ -38,6 +39,9 @@ def list(keywords=None, dof=None, mtype=None, border=None):
3839
# module = importlib.import_module(
3940
# '.' + os.path.splitext(file)[0], package='bdsim.blocks')
4041

42+
unicode = rtb_get_param("unicode")
43+
if not unicode:
44+
border = "ascii"
4145
def make_table(border=None):
4246
table = ANSITable(
4347
Column("class", headalign="^", colalign="<"),

roboticstoolbox/robot/DHRobot.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from roboticstoolbox.robot.Robot import Robot # DHLink
1212
from roboticstoolbox.robot.ETS import ETS
1313
from roboticstoolbox.robot.DHLink import DHLink # HACK
14+
from roboticstoolbox import rtb_set_param
1415
from spatialmath.base.argcheck import getvector, isscalar, verifymatrix, getmatrix
1516

1617
# from spatialmath import base
@@ -22,6 +23,7 @@
2223
from ansitable import ANSITable, Column
2324
from scipy.linalg import block_diag
2425
from roboticstoolbox.robot.DHLink import _check_rne
26+
from roboticstoolbox import rtb_get_param
2527
from frne import init, frne, delete
2628

2729
iksol = namedtuple("IKsolution", "q, success, reason")
@@ -125,6 +127,8 @@ def __str__(self):
125127
:rtype: str
126128
"""
127129

130+
unicode = rtb_get_param("unicode")
131+
border = "thin" if unicode else "ascii"
128132
s = f"DHRobot: {self.name}"
129133

130134
if self.manufacturer is not None and len(self.manufacturer) > 0:
@@ -187,7 +191,7 @@ def angle(theta, fmt=None):
187191
Column("θⱼ", headalign="^"),
188192
Column("dⱼ", headalign="^"),
189193
*qlim_columns,
190-
border="thick",
194+
border=border,
191195
)
192196
for j, L in enumerate(self):
193197
if has_qlim:
@@ -209,7 +213,7 @@ def angle(theta, fmt=None):
209213
Column("aⱼ", headalign="^"),
210214
Column("⍺ⱼ", headalign="^"),
211215
*qlim_columns,
212-
border="thick",
216+
border=border,
213217
)
214218
for j, L in enumerate(self):
215219
if has_qlim:
@@ -235,7 +239,7 @@ def angle(theta, fmt=None):
235239
table = ANSITable(
236240
Column("", colalign=">"),
237241
Column("", colalign="<"),
238-
border="thin",
242+
border=border,
239243
header=False,
240244
)
241245
if self._base is not None:
@@ -251,7 +255,7 @@ def angle(theta, fmt=None):
251255
s += "\n" + str(table)
252256

253257
# show named configurations
254-
s += self.configurations_str()
258+
s += self.configurations_str(border=border)
255259

256260
return s
257261

roboticstoolbox/robot/ERobot.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from roboticstoolbox.robot.Robot import Robot
2121
from roboticstoolbox.robot.Gripper import Gripper
2222
from roboticstoolbox.tools.data import rtb_path_to_datafile
23+
from roboticstoolbox.tools.params import rtb_get_param
2324

2425
from pathlib import PurePosixPath
2526
from ansitable import ANSITable, Column
@@ -32,7 +33,6 @@
3233

3334
import fknm
3435

35-
3636
class BaseERobot(Robot):
3737

3838
"""
@@ -276,13 +276,16 @@ def __str__(self):
276276
- The robot base frame is denoted as ``BASE`` and is equal to the
277277
robot's ``base`` attribute.
278278
"""
279+
unicode = rtb_get_param("unicode")
280+
border = "thin" if unicode else "ascii"
281+
279282
table = ANSITable(
280283
Column("id", headalign="^", colalign=">"),
281284
Column("link", headalign="^", colalign="<"),
282285
Column("joint", headalign="^", colalign=">"),
283286
Column("parent", headalign="^", colalign="<"),
284287
Column("ETS", headalign="^", colalign="<"),
285-
border="thin",
288+
border=border,
286289
)
287290
for link in self:
288291
color = "" if link.isjoint else "<<blue>>"
@@ -294,7 +297,7 @@ def __str__(self):
294297
parent_name = link.parent.name
295298
s = ets.__str__(f"q{link._jindex}")
296299
if len(s) > 0:
297-
s = " \u2295 " + s
300+
s = " \u2295 " if unicode else " * " + s # \oplus
298301

299302
if link.isjoint:
300303
# if link._joint_name is not None:
@@ -330,7 +333,7 @@ def __str__(self):
330333
s += "\n"
331334

332335
s += str(table)
333-
s += self.configurations_str()
336+
s += self.configurations_str(border=border)
334337

335338
return s
336339

roboticstoolbox/robot/ETS.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from spatialmath.base import getvector, getunit, trotx, troty, trotz, \
1313
issymbol, tr2jac, transl2, trot2, removesmall, trinv, trinv2, \
1414
verifymatrix, iseye, tr2jac2
15+
from roboticstoolbox import rtb_get_param
1516

1617
class BaseETS(UserList, ABC):
1718

@@ -654,6 +655,7 @@ def __str__(self, q=None):
654655
es = []
655656
j = 0
656657
c = 0
658+
unicode = rtb_get_param("unicode")
657659

658660
if q is None:
659661
if len(self.joints()) > 1:
@@ -695,8 +697,10 @@ def __str__(self, q=None):
695697

696698
es.append(s)
697699

698-
return " \u2295 ".join(es)
699-
700+
if unicode:
701+
return " \u2295 ".join(es)
702+
else:
703+
return " * ".join(es)
700704
# redefine * operator to concatenate the internal lists
701705
def __add__(self, rest):
702706
self.__mul__(rest)

roboticstoolbox/robot/Robot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def addconfiguration(self, name, q, unit="rad"):
402402
self._configdict[name] = v
403403
setattr(self, name, v)
404404

405-
def configurations_str(self):
405+
def configurations_str(self, border="thin"):
406406
deg = 180 / np.pi
407407

408408
# TODO: factor this out of DHRobot
@@ -425,7 +425,7 @@ def angle(theta, fmt=None):
425425
Column(f"q{j:d}", colalign="<", headalign="<")
426426
for j in range(self.n)
427427
],
428-
border="thin",
428+
border=border,
429429
)
430430

431431
for name, q in self._configdict.items():

roboticstoolbox/tools/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
rtb_path_to_datafile,
2323
)
2424

25+
from roboticstoolbox.tools.params import rtb_set_param, rtb_get_param
2526

2627
__all__ = [
2728
"null",
@@ -43,4 +44,6 @@
4344
"rtb_load_matfile",
4445
"rtb_load_jsonfile",
4546
"rtb_path_to_datafile",
47+
"rtb_set_param",
48+
"rtb_get_param",
4649
]

roboticstoolbox/tools/params.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
_params = {
2+
"unicode": True,
3+
}
4+
5+
6+
def rtb_set_param(param, value):
7+
_params[param] = value
8+
9+
def rtb_get_param(param):
10+
return _params[param]

0 commit comments

Comments
 (0)