From 193252bcf9ce1cf8136d2bfe9491e8d1769600f4 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Mon, 17 Aug 2026 08:04:37 +1000 Subject: [PATCH 1/3] feat(demo): package eigdemo/tripleangledemo/twistdemo as roboticstoolbox.demo The three demo console-scripts (pyproject.toml [project.scripts]) pointed at roboticstoolbox.examples.*, a module path that hasn't existed since the demos were relocated to a top-level examples/ dir outside the packaged src/roboticstoolbox tree (wheel.packages = ["src/roboticstoolbox"]) -- every installed entry point 404'd at import time. - Move the three scripts into src/roboticstoolbox/demo/ (new package, mirroring the existing bin/ pattern used by rtbtool). - twistdemo.py: wrap its previously flat top-level code in main(), matching the other two. - tripleangledemo.py/twistdemo.py: replace `while True: env.step(0)` with `env.run(duration=...)` -- besides enabling a --test smoke-test mode (bounded duration instead of running forever), this is a genuine fix on its own: the raw loop never noticed the browser tab closing, so closing it just left the process spinning forever; run() already has disconnect-awareness built in. - eigdemo.py: fix `-h`/`--help`, which compared the whole sys.argv list against a string and so could never match; add the repo's standard hashbang line. - pyproject.toml: repoint the three [project.scripts] entries; fix the matching coverage omit path (also stale, pointing at the same never-existed src/roboticstoolbox/examples/*). --- examples/twistdemo.py | 172 ----------------- pyproject.toml | 8 +- src/roboticstoolbox/demo/__init__.py | 0 .../roboticstoolbox/demo}/eigdemo.py | 3 +- .../roboticstoolbox/demo}/tripleangledemo.py | 11 +- src/roboticstoolbox/demo/twistdemo.py | 175 ++++++++++++++++++ 6 files changed, 188 insertions(+), 181 deletions(-) delete mode 100644 examples/twistdemo.py create mode 100644 src/roboticstoolbox/demo/__init__.py rename {examples => src/roboticstoolbox/demo}/eigdemo.py (96%) rename {examples => src/roboticstoolbox/demo}/tripleangledemo.py (93%) create mode 100644 src/roboticstoolbox/demo/twistdemo.py diff --git a/examples/twistdemo.py b/examples/twistdemo.py deleted file mode 100644 index ee0c6860d..000000000 --- a/examples/twistdemo.py +++ /dev/null @@ -1,172 +0,0 @@ -#!/usr/bin/env python -""" -@author Peter Corke -@author Jesse Haviland -""" - -import sys -import swift -from math import pi -import roboticstoolbox as rtb -from spatialgeometry import Mesh, Cylinder -from spatialmath import SO3, SE3, Twist3 -import numpy as np - - -# TODO -# rotate the rings according to the rotation axis, so that the axles -# point the right way - -# Launch the simulator Swift -env = swift.Swift() -env.launch() - -path = rtb.rtb_path_to_datafile("data") - -plane = Mesh( - filename=str(path / "spitfire_assy-gear_up.stl"), - scale=(1.0 / (180 * 3),) * 3, - color=[240, 103, 103], -) -print("Supermarine Spitfire Mk VIII by Ed Morley @GRABCAD") - -screw = Cylinder(radius=0.015, length=4, color="blue", collision=False) - -BASE = SE3(0.15, 0, 0.1) * SE3.Rz(pi / 2) * SE3.Rx(np.pi / 2) - -env.add(plane) -env.add(screw) - - -twist_params = {"x": 0, "y": 0, "R": 0, "P": 0, "p": 0.1} -twist = Twist3() - - -def update_twist_param(value, key): - global twist_params - - twist_params[key] = value - - update_twist() - - -def update_twist(): - global twist - - axis = SO3.Rx(twist_params["R"]) @ SO3.Ry(twist_params["P"]) - point = [twist_params["x"], twist_params["y"], 0] - twist = Twist3.UnitRevolute(axis.A[:, 2], point, twist_params["p"]) - - update_screw_axis(axis, point) - update_plane() - - -# button to reset twist -def reset_twist(e): - global twist_params - - twist_params["x"] = 0 - twist_params["y"] = 0 - twist_params["R"] = 0 - twist_params["P"] = 0 - twist_params["p"] = 0 - update_all_sliders() - - update_twist() - update_plane() - # env.step(0) - - -def update_plane(*args): - - T = twist.SE3(twist_theta.value) * BASE - plane.T = T - - -def update_screw_axis(axis, point): - screw.T = SE3.Rt(axis, point) - - -def update_all_sliders(): - twist_x.value = twist_params["x"] - twist_y.value = twist_params["y"] - twist_rollangle.value = twist_params["R"] - twist_pitchangle.value = twist_params["P"] - twist_pitch.value = twist_params["p"] - - -twist_x = swift.Slider( - lambda x: update_twist_param(x, "x"), - min=-1, - max=1, - step=0.01, - value=0, - desc="x position", -) - -twist_y = swift.Slider( - lambda y: update_twist_param(y, "y"), - min=-1, - max=1, - step=0.01, - value=0, - desc="y position", -) - -twist_rollangle = swift.Slider( - lambda r: update_twist_param(np.deg2rad(r), "R"), - min=-180, - max=180, - step=1, - value=0, - desc="roll angle (rx)", - unit="°", -) - -twist_pitchangle = swift.Slider( - lambda p: update_twist_param(np.deg2rad(p), "P"), - min=-180, - max=180, - step=1, - value=0, - desc="pitch angle (ry)", - unit="°", -) - -twist_pitch = swift.Slider( - lambda p: update_twist_param(p, "p"), - min=0, - max=1, - step=0.02, - value=0, - desc="screw pitch", -) - -twist_theta = swift.Slider( - update_plane, min=0, max=10, step=0.02, value=0, desc="Twist rotation" -) - -button = swift.Button(reset_twist, desc="Set to Zero") -quit = swift.Button(lambda x: sys.exit(0), desc="Quit") - -label1 = swift.Label(desc="Twist parameters") -label2 = swift.Label(desc="Twist application") - -env.add(label1) -env.add(twist_x) -env.add(twist_y) -env.add(twist_rollangle) -env.add(twist_pitchangle) -env.add(twist_pitch) -env.add(button) - -env.add(label2) - -env.add(twist_theta) - -env.add(quit) - - -update_all_sliders() -while True: - env.step(0) diff --git a/pyproject.toml b/pyproject.toml index acf53e7df..358bb4daf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -123,9 +123,9 @@ docs = [ [project.scripts] -eigdemo = "roboticstoolbox.examples.eigdemo:main" -tripleangledemo = "roboticstoolbox.examples.tripleangledemo:main" -twistdemo = "roboticstoolbox.examples.twistdemo:main" +eigdemo = "roboticstoolbox.demo.eigdemo:main" +tripleangledemo = "roboticstoolbox.demo.tripleangledemo:main" +twistdemo = "roboticstoolbox.demo.twistdemo:main" rtbtool = "roboticstoolbox.bin.rtbtool:main" @@ -188,4 +188,4 @@ filterwarnings = [ ] [tool.coverage.run] -omit = ["src/roboticstoolbox/examples/*", "test_*.py"] +omit = ["src/roboticstoolbox/demo/*", "test_*.py"] diff --git a/src/roboticstoolbox/demo/__init__.py b/src/roboticstoolbox/demo/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/examples/eigdemo.py b/src/roboticstoolbox/demo/eigdemo.py similarity index 96% rename from examples/eigdemo.py rename to src/roboticstoolbox/demo/eigdemo.py index 65716ca0d..627369234 100644 --- a/examples/eigdemo.py +++ b/src/roboticstoolbox/demo/eigdemo.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python """ Eigenvalue demonstration @@ -82,7 +83,7 @@ def help(): print("eigdemo a b c d # uses matrix [a b; c d]") sys.exit(0) - if sys.argv in ("-h", "--help", "help"): + if any(arg in ("-h", "--help", "help") for arg in sys.argv[1:]): help() if len(sys.argv) == 5: diff --git a/examples/tripleangledemo.py b/src/roboticstoolbox/demo/tripleangledemo.py similarity index 93% rename from examples/tripleangledemo.py rename to src/roboticstoolbox/demo/tripleangledemo.py index 858174bc0..b8bd27783 100644 --- a/examples/tripleangledemo.py +++ b/src/roboticstoolbox/demo/tripleangledemo.py @@ -4,6 +4,7 @@ @author Jesse Haviland """ +import sys import swift from math import pi import roboticstoolbox as rtb @@ -78,7 +79,7 @@ def convert(R): plane.T = convert(R1 * R2 * R3 * SO3.Ry(pi / 2) * SO3.Rz(pi / 2)) -def demo(): +def demo(test: bool = False): # TODO # rotate the rings according to the rotation axis, so that the axles # point the right way @@ -224,12 +225,14 @@ def angle(index, ring): update_gimbals(0, 2) update_gimbals(0, 3) - while True: - env.step(0) + # duration=None runs until the browser disconnects (or forever, ^C to + # stop); a bounded duration lets --test smoke-test this demo without + # needing a browser at all (see SWIFT_HEADLESS in swift's launch()). + env.run(duration=0.5 if test else None) def main(): - demo() + demo(test="--test" in sys.argv[1:]) if __name__ == "__main__": diff --git a/src/roboticstoolbox/demo/twistdemo.py b/src/roboticstoolbox/demo/twistdemo.py new file mode 100644 index 000000000..82ab9540b --- /dev/null +++ b/src/roboticstoolbox/demo/twistdemo.py @@ -0,0 +1,175 @@ +#!/usr/bin/env python +""" +@author Peter Corke +@author Jesse Haviland +""" + +import sys +import swift +from math import pi +import roboticstoolbox as rtb +from spatialgeometry import Mesh, Cylinder +from spatialmath import SO3, SE3, Twist3 +import numpy as np + + +def demo(test: bool = False): + # TODO + # rotate the rings according to the rotation axis, so that the axles + # point the right way + + # Launch the simulator Swift + env = swift.Swift() + env.launch() + + path = rtb.rtb_path_to_datafile("data") + + plane = Mesh( + filename=str(path / "spitfire_assy-gear_up.stl"), + scale=(1.0 / (180 * 3),) * 3, + color=[240, 103, 103], + ) + print("Supermarine Spitfire Mk VIII by Ed Morley @GRABCAD") + + screw = Cylinder(radius=0.015, length=4, color="blue", collision=False) + + BASE = SE3(0.15, 0, 0.1) * SE3.Rz(pi / 2) * SE3.Rx(np.pi / 2) + + env.add(plane) + env.add(screw) + + twist_params = {"x": 0, "y": 0, "R": 0, "P": 0, "p": 0.1} + twist = Twist3() + + def update_twist_param(value, key): + nonlocal twist_params + + twist_params[key] = value + + update_twist() + + def update_twist(): + nonlocal twist + + axis = SO3.Rx(twist_params["R"]) @ SO3.Ry(twist_params["P"]) + point = [twist_params["x"], twist_params["y"], 0] + twist = Twist3.UnitRevolute(axis.A[:, 2], point, twist_params["p"]) + + update_screw_axis(axis, point) + update_plane() + + # button to reset twist + def reset_twist(e): + nonlocal twist_params + + twist_params["x"] = 0 + twist_params["y"] = 0 + twist_params["R"] = 0 + twist_params["P"] = 0 + twist_params["p"] = 0 + update_all_sliders() + + update_twist() + update_plane() + # env.step(0) + + def update_plane(*args): + + T = twist.SE3(twist_theta.value) * BASE + plane.T = T + + def update_screw_axis(axis, point): + screw.T = SE3.Rt(axis, point) + + def update_all_sliders(): + twist_x.value = twist_params["x"] + twist_y.value = twist_params["y"] + twist_rollangle.value = twist_params["R"] + twist_pitchangle.value = twist_params["P"] + twist_pitch.value = twist_params["p"] + + twist_x = swift.Slider( + lambda x: update_twist_param(x, "x"), + min=-1, + max=1, + step=0.01, + value=0, + desc="x position", + ) + + twist_y = swift.Slider( + lambda y: update_twist_param(y, "y"), + min=-1, + max=1, + step=0.01, + value=0, + desc="y position", + ) + + twist_rollangle = swift.Slider( + lambda r: update_twist_param(np.deg2rad(r), "R"), + min=-180, + max=180, + step=1, + value=0, + desc="roll angle (rx)", + unit="°", + ) + + twist_pitchangle = swift.Slider( + lambda p: update_twist_param(np.deg2rad(p), "P"), + min=-180, + max=180, + step=1, + value=0, + desc="pitch angle (ry)", + unit="°", + ) + + twist_pitch = swift.Slider( + lambda p: update_twist_param(p, "p"), + min=0, + max=1, + step=0.02, + value=0, + desc="screw pitch", + ) + + twist_theta = swift.Slider( + update_plane, min=0, max=10, step=0.02, value=0, desc="Twist rotation" + ) + + button = swift.Button(reset_twist, desc="Set to Zero") + quit = swift.Button(lambda x: sys.exit(0), desc="Quit") + + label1 = swift.Label(desc="Twist parameters") + label2 = swift.Label(desc="Twist application") + + env.add(label1) + env.add(twist_x) + env.add(twist_y) + env.add(twist_rollangle) + env.add(twist_pitchangle) + env.add(twist_pitch) + env.add(button) + + env.add(label2) + + env.add(twist_theta) + + env.add(quit) + + update_all_sliders() + + # duration=None runs until the browser disconnects (or forever, ^C to + # stop); a bounded duration lets --test smoke-test this demo without + # needing a browser at all (see SWIFT_HEADLESS in swift's launch()). + env.run(duration=0.5 if test else None) + + +def main(): + demo(test="--test" in sys.argv[1:]) + + +if __name__ == "__main__": + main() From e0de788c25b9d73b1774f1ac5fbcade82540d733 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Mon, 17 Aug 2026 08:05:02 +1000 Subject: [PATCH 2/3] test: smoke-test the roboticstoolbox.demo console scripts eigdemo runs to completion under MPLBACKEND=Agg as-is. tripleangledemo and twistdemo need --test (bounds env.run() to a short sim-time duration) plus SWIFT_HEADLESS=1 (swift jhavl/swift#122) to run without a browser; skipped when swift isn't installed, matching test_backend_capabilities.py's existing swift-optional pattern. --- tests/test_demo.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/test_demo.py diff --git a/tests/test_demo.py b/tests/test_demo.py new file mode 100644 index 000000000..a5bc7686f --- /dev/null +++ b/tests/test_demo.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +""" +Smoke tests for the console-script demos in roboticstoolbox.demo. + +eigdemo is matplotlib-only, so it runs to completion under the Agg backend +(no window ever opens, plt.show() just warns and returns). tripleangledemo +and twistdemo use Swift, which normally never returns on its own -- they +accept a ``--test`` flag that bounds Swift's env.run() to a short sim-time +duration instead of running until the browser disconnects, combined with +SWIFT_HEADLESS=1 so no browser is required at all. +""" + +import subprocess +import sys +import unittest + +_TIMEOUT = 30 +_ENV = {"MPLBACKEND": "Agg"} + + +def _run(module: str, *args: str, **extra_env: str) -> subprocess.CompletedProcess: + import os + + env = dict(os.environ, **_ENV, **extra_env) + return subprocess.run( + [sys.executable, "-m", module, *args], + stdin=subprocess.DEVNULL, + capture_output=True, + timeout=_TIMEOUT, + env=env, + ) + + +class TestEigdemo(unittest.TestCase): + def test_default_matrix(self): + result = _run("roboticstoolbox.demo.eigdemo") + self.assertEqual(result.returncode, 0, msg=result.stderr.decode()) + out = result.stdout.decode() + self.assertIn("matrix A =", out) + self.assertIn("λ1 =", out) + + def test_custom_matrix(self): + result = _run("roboticstoolbox.demo.eigdemo", "1", "0", "0", "2") + self.assertEqual(result.returncode, 0, msg=result.stderr.decode()) + self.assertIn("matrix A =", result.stdout.decode()) + + def test_help(self): + result = _run("roboticstoolbox.demo.eigdemo", "--help") + self.assertEqual(result.returncode, 0, msg=result.stderr.decode()) + self.assertIn("eigdemo", result.stdout.decode()) + + +class TestSwiftDemos(unittest.TestCase): + def setUp(self): + try: + import swift # noqa: F401 + except (ImportError, ModuleNotFoundError): + self.skipTest("swift-sim not installed") + + def test_tripleangledemo(self): + result = _run( + "roboticstoolbox.demo.tripleangledemo", "--test", SWIFT_HEADLESS="1" + ) + self.assertEqual(result.returncode, 0, msg=result.stderr.decode()) + + def test_twistdemo(self): + result = _run("roboticstoolbox.demo.twistdemo", "--test", SWIFT_HEADLESS="1") + self.assertEqual(result.returncode, 0, msg=result.stderr.decode()) + + +if __name__ == "__main__": + unittest.main() From 30df358e4b044168adbe132c640aace445ae70e0 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Mon, 17 Aug 2026 09:01:22 +1000 Subject: [PATCH 3/3] fix(demo): force UTF-8 stdout in eigdemo, fixing a Windows CI failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit eigdemo() prints λ1/λ2 (literal Greek chars). Windows defaults stdout to the legacy cp1252 console codepage whenever it isn't a real console -- piped/redirected output included -- and cp1252 has no mapping for λ, so print() raised UnicodeEncodeError. Caught by test_demo.py's new subprocess-based smoke tests on windows-latest (both 3.10 and 3.11): the exact case of capturing eigdemo's stdout through a pipe. A real Windows user piping or redirecting eigdemo's output would hit the same crash outside of CI. sys.stdout.reconfigure(encoding="utf-8") (3.7+) forces UTF-8 regardless of the console's codepage. --- src/roboticstoolbox/demo/eigdemo.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/roboticstoolbox/demo/eigdemo.py b/src/roboticstoolbox/demo/eigdemo.py index 627369234..632e12e9f 100644 --- a/src/roboticstoolbox/demo/eigdemo.py +++ b/src/roboticstoolbox/demo/eigdemo.py @@ -77,6 +77,12 @@ def animate(theta): def main(): + # eigdemo() prints λ1/λ2 -- on Windows, stdout defaults to the legacy + # cp1252 console codepage (which can't encode λ) whenever it isn't a + # real console, e.g. piped/redirected output. reconfigure() (3.7+) + # forces UTF-8 regardless. + if hasattr(sys.stdout, "reconfigure"): + sys.stdout.reconfigure(encoding="utf-8") def help(): print("eigdemo # uses default matrix [1 2; 3 4]")