def play(self, logger_folder=None, no_iter = -1): """ If logger_folder exists and the result file is saved, then the specific iteration can be chosen to play the animation. \\ Parameter ---------- logger_folder : string The name of the logger folder no_iter : int The number of iteration to play the animation """ fig, ax = super().create_plot(figsize=(8, 2), xlim=(-5,75), ylim=(-15,5)) trajectory = np.asarray(logger.read_from_json(logger_folder, no_iter)["trajectory"]) car = patches.FancyBboxPatch((0, 0), 3, 2, "round,pad=0.02") car.set_color('C0') ax.add_patch(car) plt.plot(trajectory[:,0], trajectory[:,1]) self._is_interrupted=False for i in range(self.T): angle = trajectory[i,2,0] t_start = ax.transData x = trajectory[i,0,0] + 1*np.sin(angle) y = trajectory[i,1,0] - 1*np.cos(angle) rotate_center = t_start.transform([x, y]) car.set_x(x) car.set_y(y) t = mpl.transforms.Affine2D().rotate_around(rotate_center[0], rotate_center[1], angle) t_end = t_start + t car.set_transform(t_end) fig.canvas.draw() plt.pause(0.01) if self._is_interrupted: return self._is_interrupted = True
def play(self, logger_folder=None, no_iter=-1): """ If logger_folder exists and the result file is saved, then the specific iteration can be chosen to play the animation. \\ Parameter ---------- logger_folder : string The name of the logger folder no_iter : int The number of iteration to play the animation """ fig, ax = super().create_plot(xlim=(-4, 4), ylim=(-4, 4)) trajectory = np.asarray( logger.read_from_json(logger_folder, no_iter)["trajectory"]) pole1 = patches.FancyBboxPatch((0, 0), 0.04, self.l1, "round,pad=0.02") pole1.set_color('C0') pole2 = patches.FancyBboxPatch((0, 0), 0.04, self.l2, "round,pad=0.02") pole2.set_color('C1') ax.add_patch(pole1) ax.add_patch(pole2) self._is_interrupted = False for i in range(self.T): self.play_trajectory_current = trajectory[i, :, 0] # draw pole1 t_start = ax.transData x1 = -0.02 * np.cos(self.play_trajectory_current[0]) y1 = 0.02 * np.sin(self.play_trajectory_current[0]) rotate_center = t_start.transform([x1, y1]) pole1.set_x(x1) pole1.set_y(y1) t = mpl.transforms.Affine2D().rotate_around( rotate_center[0], rotate_center[1], -self.play_trajectory_current[0]) t_end = t_start + t pole1.set_transform(t_end) # draw pole2 x2 = self.l1 * np.sin(self.play_trajectory_current[ 0]) - 0.02 * np.cos(self.play_trajectory_current[0] + self.play_trajectory_current[2]) y2 = self.l1 * np.cos(self.play_trajectory_current[ 0]) + 0.02 * np.sin(self.play_trajectory_current[0] + self.play_trajectory_current[2]) rotate_center = t_start.transform([x2, y2]) pole2.set_x(x2) pole2.set_y(y2) t = mpl.transforms.Affine2D().rotate_around( rotate_center[0], rotate_center[1], -self.play_trajectory_current[0] - self.play_trajectory_current[2]) t_end = t_start + t pole2.set_transform(t_end) fig.canvas.draw() plt.pause(0.001) if self._is_interrupted: return self._is_interrupted = True
def play(self, logger_folder=None, no_iter=-1): """ If ``logger_folder`` exists and the result json file is saved, then the specific iteration can be chosen to play the animation. If ``logger_folder`` is set to be none, then the trajectroy in the last iteration will be played. :param logger_folder: Name of the logger folder where the result json is saved, defaults to True :type logger_folder: str, optional :param no_iter: Number of iteration to play the animation. If it is set as -1, then the trajectroy in the last iteration in the given result file will be played. defaults to -1. :type no_iter: int, optional """ fig, ax = super().create_plot(figsize=(5, 5), xlim=(-5, 10), ylim=(-7.5, 7.5)) trajectory = np.asarray( logger.read_from_json(logger_folder, no_iter)["trajectory"]) car = patches.FancyBboxPatch((0, 0), 3, 2, "round,pad=0.2") car.set_color('C0') ax.add_patch(car) plt.plot(trajectory[:, 0], trajectory[:, 1], 'C1') plt.plot([-1, -1], [10, -10], 'C2') plt.plot([-1, 5], [2, 2], 'C2') plt.plot([-1, 5], [-2, -2], 'C2') self._is_interrupted = False for i in range(self.T): angle = trajectory[i, 2, 0] t_start = ax.transData x = trajectory[i, 0, 0] + 1 * np.sin(angle) y = trajectory[i, 1, 0] - 1 * np.cos(angle) rotate_center = t_start.transform([x, y]) car.set_x(x) car.set_y(y) t = mpl.transforms.Affine2D().rotate_around( rotate_center[0], rotate_center[1], angle) t_end = t_start + t car.set_transform(t_end) fig.canvas.draw() plt.pause(0.001) if self._is_interrupted: return self._is_interrupted = True