Get animation working for console and Jupyter
added method to figure if Jupyter or not
"""
@author Jesse Haviland
"""
-
+import time
import roboticstoolbox as rp
import numpy as np
from roboticstoolbox.backends.Connector import Connector
@@ -114,9 +114,18 @@ class PyPlot(Connector):
# disable the display of value under cursor
self.ax.format_coord = lambda x, y: ''
+ # add time display in top-right corner
+ self.timer = plt.figtext(0.85, 0.95, '')
+
+ if _isnotebook():
+ plt.ion()
+ self.fig.canvas.draw()
+ else:
plt.ion()
plt.show()
+ self.t = 0
+
# # Set the signal handler and a 0.1 second plot updater
# signal.signal(signal.SIGALRM, self._plot_handler)
# signal.setitimer(signal.ITIMER_REAL, 0.1, 0.1)
@@ -149,15 +158,29 @@ class PyPlot(Connector):
self._step_robots(dt)
# plt.ioff()
+
self._draw_ellipses()
self._draw_robots()
self._set_axes_equal()
+
+ # update time and display it on plot
+ if self.t > 0:
+ self.timer.set_text(f"t = {self.t:.2f}")
+ self.t += dt
+
# plt.ion()
+
+ if _isnotebook():
+ plt.draw()
+ self.fig.canvas.draw()
+ time.sleep(dt)
+ else:
plt.draw()
plt.pause(dt)
self._update_robots()
+
def reset(self):
"""
Reset the graphical scene
@@ -196,7 +219,7 @@ class PyPlot(Connector):
plt.close(self.fig)
#
- # Methods to interface with the robots created in other environemnts
+ # Methods to interface with the robots created in other environments
#
def add(
@@ -251,6 +274,9 @@ class PyPlot(Connector):
self.ellipses[len(self.ellipses) - 1].draw()
id = len(self.ellipses)
+ plt.draw()
+ plt.show(block=False)
+
self._set_axes_equal()
return id
@@ -370,6 +396,10 @@ class PyPlot(Connector):
self.ax.set_zlim3d([z_middle - plot_radius, z_middle + plot_radius])
def _add_teach_panel(self, robot):
+
+ if _isnotebook():
+ raise RuntimeError('cannot use teach panel under Jupyter')
+
fig = self.fig
# Add text to the plots
@@ -452,3 +482,27 @@ class PyPlot(Connector):
qlim[0, i], qlim[1, i], robot.q[i] * 180/np.pi))
self.sjoint[i].on_changed(lambda x: update(x, text, robot))
+
+
+
+def _isnotebook():
+ """
+ Determine if code is being run from a Jupyter notebook
+
+ ``_isnotebook`` is True if running Jupyter notebook, else False
+
+ :references:
+
+ - https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-
+ is-executed-in-the-ipython-notebook/39662359#39662359
+ """
+ try:
+ shell = get_ipython().__class__.__name__
+ if shell == 'ZMQInteractiveShell':
+ return True # Jupyter notebook or qtconsole
+ elif shell == 'TerminalInteractiveShell':
+ return False # Terminal running IPython
+ else:
+ return False # Other type (?)
+ except NameError:
+ return False # Probably standard Python interpreter
\ No newline at end of file
