diff --git a/python/initial_python_solution/.blit_test.py.swp b/python/initial_python_solution/.blit_test.py.swp deleted file mode 100644 index 5c52e18edb5224f1c6bd60b1766faf468b66fb90..0000000000000000000000000000000000000000 Binary files a/python/initial_python_solution/.blit_test.py.swp and /dev/null differ diff --git a/python/initial_python_solution/.manipulator_visual_motion_analyzer.py.swp b/python/initial_python_solution/.manipulator_visual_motion_analyzer.py.swp deleted file mode 100644 index 6dfe2a96bb05e401c7327f20ce338af45f733479..0000000000000000000000000000000000000000 Binary files a/python/initial_python_solution/.manipulator_visual_motion_analyzer.py.swp and /dev/null differ diff --git a/python/initial_python_solution/example_code/blit_in_tk.py b/python/initial_python_solution/example_code/blit_in_tk.py new file mode 100644 index 0000000000000000000000000000000000000000..db7b3ddaf94da837a0f4b43940f888ca8cd80d38 --- /dev/null +++ b/python/initial_python_solution/example_code/blit_in_tk.py @@ -0,0 +1,95 @@ +import tkinter as Tk +import numpy as np +import matplotlib.pyplot as plt +from datetime import datetime +import time + +from matplotlib.figure import Figure +from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg + +# from Backend import Backend #self written data acquisition handler +import random + +#global variables +t0 = datetime.now() #start time of program + +#Returns time difference in seconds +def time_difference(t1, t2): + delta = t2-t1 + delta = delta.total_seconds() + return delta + +# Define Class for Flow data display +class FlowFig(): + def __init__(self, master): #master:Parent frame for plot + #Initialize plot data + self.t = [] + self.Flow = [] + + #Initialize plot for FlowFig + self.fig = plt.figure(figsize=(4,4)) + self.ax = self.fig.add_subplot(111) + self.ax.set_title("Flow Control Monitor") + self.ax.set_xlabel("Time") + self.ax.set_ylabel("Flow") + self.ax.axis([0,100,0,5]) + self.line = self.ax.plot(self.t, self.Flow, '-') + + #Set up canvas + self.canvas = FigureCanvasTkAgg(self.fig, master = master) + self.canvas.draw() + self.ax.add_line(self.line[0]) + self.background = self.canvas.copy_from_bbox(self.ax.bbox) + self.ax.grid(True) + + # # Initialize handler for data aqcuisition + # self.Data= Backend() + # self.Data.initialize() + + #update figure + def update(self): + start = time.time() + # get new data values + self.t.append(time_difference(t0, datetime.now())) + Flow = random.uniform(1, 5) + self.Flow.append(Flow) + + # shorten data vector if too long + if len(self.t) > 200: + del self.t[0] + del self.Flow[0] + + #adjust xlims, add new data to plot + self.ax.set_xlim([np.min(self.t), np.max(self.t)]) + self.line[0].set_data(self.t, self.Flow) + + #blit new data into old frame + self.canvas.restore_region(self.background) + self.ax.draw_artist(self.line[0]) + self.canvas.blit(self.ax.bbox) + self.canvas.flush_events() + root.after(1,self.update) + end = time.time() + print("time on rendering", end - start) + print("fps: ", 1/ (end - start)) + +#Flow Frame of GUI +class FlowPage(Tk.Frame): + def __init__(self, parent, controller): + Tk.Frame.__init__(self,parent) + self.parent = parent + self.FlowPlot = FlowFig(self) + self.FlowPlot.canvas.get_tk_widget().grid(row=0, column=0, rowspan=9, columnspan=9) + +# Mainloop +root= Tk.Tk() +root.rowconfigure(0, weight=1) +root.columnconfigure(0, weight=1) + +Flowmonitor = FlowPage(root, root) +Flowmonitor.grid(row =0, column=0, rowspan =10, columnspan=10) +Flowmonitor.rowconfigure(0, weight=1) +Flowmonitor.columnconfigure(0, weight=1) + +root.after(25, Flowmonitor.FlowPlot.update) +root.mainloop() diff --git a/python/initial_python_solution/blit_test.py b/python/initial_python_solution/example_code/blit_test.py similarity index 100% rename from python/initial_python_solution/blit_test.py rename to python/initial_python_solution/example_code/blit_test.py diff --git a/python/initial_python_solution/dark_background_example.py b/python/initial_python_solution/example_code/dark_background_example.py similarity index 100% rename from python/initial_python_solution/dark_background_example.py rename to python/initial_python_solution/example_code/dark_background_example.py diff --git a/python/initial_python_solution/ellipse.py b/python/initial_python_solution/example_code/ellipse.py similarity index 100% rename from python/initial_python_solution/ellipse.py rename to python/initial_python_solution/example_code/ellipse.py diff --git a/python/initial_python_solution/embedding_in_tk_sgskip.py b/python/initial_python_solution/example_code/embedding_in_tk_sgskip.py similarity index 100% rename from python/initial_python_solution/embedding_in_tk_sgskip.py rename to python/initial_python_solution/example_code/embedding_in_tk_sgskip.py diff --git a/python/initial_python_solution/example_code/manager_anim.py b/python/initial_python_solution/example_code/manager_anim.py new file mode 100644 index 0000000000000000000000000000000000000000..edd9a920de1365d7f86cf052008d5264770bd887 --- /dev/null +++ b/python/initial_python_solution/example_code/manager_anim.py @@ -0,0 +1,108 @@ +import matplotlib.pyplot as plt +import numpy as np +import time + +class BlitManager: + def __init__(self, canvas, animated_artists=()): + """ + Parameters + ---------- + canvas : FigureCanvasAgg + The canvas to work with, this only works for subclasses of the Agg + canvas which have the `~FigureCanvasAgg.copy_from_bbox` and + `~FigureCanvasAgg.restore_region` methods. + + animated_artists : Iterable[Artist] + List of the artists to manage + """ + self.canvas = canvas + self._bg = None + self._artists = [] + + for a in animated_artists: + self.add_artist(a) + # grab the background on every draw + self.cid = canvas.mpl_connect("draw_event", self.on_draw) + + def on_draw(self, event): + """Callback to register with 'draw_event'.""" + cv = self.canvas + if event is not None: + if event.canvas != cv: + raise RuntimeError + self._bg = cv.copy_from_bbox(cv.figure.bbox) + self._draw_animated() + + def add_artist(self, art): + """ + Add an artist to be managed. + + Parameters + ---------- + art : Artist + + The artist to be added. Will be set to 'animated' (just + to be safe). *art* must be in the figure associated with + the canvas this class is managing. + + """ + if art.figure != self.canvas.figure: + raise RuntimeError + art.set_animated(True) + self._artists.append(art) + + def _draw_animated(self): + """Draw all of the animated artists.""" + fig = self.canvas.figure + for a in self._artists: + fig.draw_artist(a) + + def update(self): + """Update the screen with animated artists.""" + cv = self.canvas + fig = cv.figure + # paranoia in case we missed the draw event, + if self._bg is None: + self.on_draw(None) + else: + # restore the background + cv.restore_region(self._bg) + # draw all of the animated artists + self._draw_animated() + # update the GUI state + cv.blit(fig.bbox) + # let the GUI event loop process anything it has to do + cv.flush_events() + +# make a new figure +fig, ax = plt.subplots() +x = np.linspace(0, 2 * np.pi, 100) +# add a line +(ln,) = ax.plot(x, np.sin(x), animated=True) +# add a frame number +fr_number = ax.annotate( + "0", + (0, 1), + xycoords="axes fraction", + xytext=(10, -10), + textcoords="offset points", + ha="left", + va="top", + animated=True, +) +bm = BlitManager(fig.canvas, [ln, fr_number]) +# make sure our window is on the screen and drawn +plt.show(block=False) +plt.pause(.1) + +for j in range(100): + start = time.time() + # update the artists + ln.set_ydata(np.sin(x + (j / 100) * np.pi)) + fr_number.set_text(f"frame: {j}") + # tell the blitting manager to do its thing + bm.update() + + end = time.time() + print("time on rendering", end - start) + print("fps: ", 1/ (end - start)) diff --git a/python/initial_python_solution/run_classic_ik_algs.py b/python/initial_python_solution/example_code/run_classic_ik_algs.py similarity index 100% rename from python/initial_python_solution/run_classic_ik_algs.py rename to python/initial_python_solution/example_code/run_classic_ik_algs.py diff --git a/python/initial_python_solution/manipulator_visual_motion_analyzer.py b/python/initial_python_solution/manipulator_visual_motion_analyzer.py index 23a428da562b03b6b72f0ae07e0145bf91670251..690020863bf13c694cd6623dfc58c285701eb561 100644 --- a/python/initial_python_solution/manipulator_visual_motion_analyzer.py +++ b/python/initial_python_solution/manipulator_visual_motion_analyzer.py @@ -145,6 +145,7 @@ for robot_index, robot in enumerate(ik_env.robots): trajectory_plots.append(trajectory_plot) # goal point ik_env.goal_point_plot = drawPoint(ik_env.ax, ik_env.goal, 'maroon', '*') +#background_manipulator = fig_manipulator.canvas.copy_from_bbox(fig_manipulator.bbox) # let's add the manipulability ellipse @@ -293,8 +294,13 @@ notebook_right.grid(row=0, column=1, sticky='ew') # tkinterize these plots canvas_manipulator = FigureCanvasTkAgg(fig_manipulator, master=frame_manipulator) +canvas_manipulator.draw() # NEW +# TODO maybe you want another background idk +# worked elsewhere with figure.bbox background_manipulator = canvas_manipulator.copy_from_bbox(fig_manipulator.bbox) +#background_manipulator = canvas_manipulator.copy_from_bbox(canvas_manipulator.bbox) + canvas_manipulator_widget = canvas_manipulator.get_tk_widget() canvas_manipulator_widget.grid(row=0, column=0) canvas_manipulator._tkcanvas.grid(row=1, column=0) @@ -349,6 +355,7 @@ def update_points(new_val): # ee plot ########################################################################### start = ttime.time() + #fig_manipulator.canvas.restore_region(background_manipulator) canvas_manipulator.restore_region(background_manipulator) index = int(np.floor(float(new_val))) @@ -370,6 +377,11 @@ def update_points(new_val): for robot_index, robot in enumerate(ik_env.robots): ik_env.robots[robot_index].setJoints(ik_env.data[robot_index]["qs"][index]) ik_env.robots[robot_index].drawStateAnim() + + # NEW AND BROKEN + for link in robot.lines: + for line in link: + ik_env.ax.draw_artist(line) # all these are in lists as that's what the line plot wants, # despite the fact that we have single points point_in_time_eigen1_line.set_xdata([time[index]]) @@ -405,8 +417,12 @@ def update_points(new_val): ik_env.p_e_point_plots[robot_index].set_data([ik_env.data[robot_index]['p_es'][index][0]], [ik_env.data[robot_index]['p_es'][index][1]]) ik_env.p_e_point_plots[robot_index].set_3d_properties([ik_env.data[robot_index]['p_es'][index][2]]) canvas_ee.draw() - canvas_manipulator.draw() - # NEW + # NEW AND BROKEN +# fig_manipulator.canvas.blit(fig_manipulator.bbox) +# fig_manipulator.canvas.flush_events() + canvas_manipulator.blit(fig_manipulator.bbox) + canvas_manipulator.flush_events() + #canvas_manipulator.draw() # might need to manually update all artists here from ax_something #canvas_manipulator.blit(fig_manipulator.bbox) canvas_manip_graphs.draw() diff --git a/python/initial_python_solution/robot_stuff/.forw_kinm.py.swp b/python/initial_python_solution/robot_stuff/.forw_kinm.py.swp deleted file mode 100644 index c0aefbb19e9fc5f2403b0c03a9d5e4906deb177d..0000000000000000000000000000000000000000 Binary files a/python/initial_python_solution/robot_stuff/.forw_kinm.py.swp and /dev/null differ diff --git a/python/initial_python_solution/robot_stuff/__pycache__/forw_kinm.cpython-310.pyc b/python/initial_python_solution/robot_stuff/__pycache__/forw_kinm.cpython-310.pyc index fef8c56c5d57b61d99de335ea07b479488fdf269..ae795696554855aaab0586582d8ca7a29dcd7522 100644 Binary files a/python/initial_python_solution/robot_stuff/__pycache__/forw_kinm.cpython-310.pyc and b/python/initial_python_solution/robot_stuff/__pycache__/forw_kinm.cpython-310.pyc differ diff --git a/python/initial_python_solution/robot_stuff/forw_kinm.py b/python/initial_python_solution/robot_stuff/forw_kinm.py index dbf3d3e077bbd38e7084b3f0c8325d5679a2da36..0a54f807779e36f9c913ea72e29e149c5dfd516e 100644 --- a/python/initial_python_solution/robot_stuff/forw_kinm.py +++ b/python/initial_python_solution/robot_stuff/forw_kinm.py @@ -133,10 +133,14 @@ class Robot_raw: z_hat = self.joints[j].HomMat[0:3,2] p = self.joints[j].HomMat[0:3,3] - line_x, = self.ax.plot(np.array([]),np.array([]),np.array([]), 'r')#, animated=True) - line_y, = self.ax.plot(np.array([]),np.array([]),np.array([]), 'g')#, animated=True) - line_z, = self.ax.plot(np.array([]),np.array([]),np.array([]), 'b')#, animated=True) - line_p, = self.ax.plot(np.array([]),np.array([]),np.array([]), self.color_link)#, animated=True) + #line_x, = self.ax.plot(np.array([]),np.array([]),np.array([]), 'r')#, animated=True) + #line_y, = self.ax.plot(np.array([]),np.array([]),np.array([]), 'g')#, animated=True) + #line_z, = self.ax.plot(np.array([]),np.array([]),np.array([]), 'b')#, animated=True) + #line_p, = self.ax.plot(np.array([]),np.array([]),np.array([]), self.color_link)#, animated=True) + line_x, = self.ax.plot(np.array([]),np.array([]),np.array([]), 'r', animated=True) + line_y, = self.ax.plot(np.array([]),np.array([]),np.array([]), 'g', animated=True) + line_z, = self.ax.plot(np.array([]),np.array([]),np.array([]), 'b', animated=True) + line_p, = self.ax.plot(np.array([]),np.array([]),np.array([]), self.color_link, animated=True) self.lines += [[line_x, line_y, line_z, line_p]] avg_link_lenth += self.joints[j].d + self.joints[j].r