image.lines.append(rod) image.patches.append(sphere) image.patches.append(cart) image.patches.append(joint) return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex1_InvertedPendulumTranslation.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0, 'x'), (2, 'phi')], plotinputs=[(0, 'u')]) xmin = np.min(S.sim_data[1][:, 0]) xmax = np.max(S.sim_data[1][:, 0]) A.set_limits(xlim=(xmin - 0.5, xmax + 0.5), ylim=(-0.3, 0.8)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex1_InvertedPendulumTranslation.gif')
# pendulums sphere1 = mpl.patches.Circle((x1, y1), 0.01, color='k') sphere2 = mpl.patches.Circle((0, 0), 0.01, color='k') image.lines.append(rod1) image.lines.append(rod2) image.patches.append(sphere1) image.patches.append(sphere2) return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex5_Acrobot.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0, 'phi1'), (2, 'phi2')], plotinputs=[(0, 'u')]) A.set_limits(xlim=(-1.1, 1.1), ylim=(-1.1, 1.1)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex5_Acrobot.gif')
mpl.lines.Line2D(xdata=[x_car, x_p[0]], ydata=[y_car, y_p[0]], color='black', zorder=1, linewidth=2.0)) else: image.lines.append( mpl.lines.Line2D(xdata=[x_p[i - 1], x_p[i]], ydata=[y_p[i - 1], y_p[i]], color='black', zorder=1, linewidth=2.0)) # and return the image return image # create Animation object A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(3, '$x$'), (7, '$\\dot{x}$')], plotinputs=[(0, '$u$')]) xmin = 0 xmax = 0 A.set_limits(xlim=(xmin - 1.5, xmax + 1.5), ylim=(-2.0, 2.0)) if 'plot' in sys.argv: A.show(t=S.b) if 0: A.animate() A.save('images/TriplePendulum' + str(Tb) + '.gif')
rod2 = mpl.lines.Line2D([x_car,x_pendel2],[y_car,y_pendel2],color='0.3',zorder=1,linewidth=2.0) image.patches.append(sphere1) image.patches.append(sphere2) image.patches.append(car) image.patches.append(joint) image.lines.append(rod1) image.lines.append(rod2) return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex2_InvertedDualPendulumSwingUp.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0,'x'),(2,'phi1'),(4,'phi2')], plotinputs=[(0,'u')]) xmin = np.min(S.sim_data[1][:,0]) xmax = np.max(S.sim_data[1][:,0]) A.set_limits(xlim=(xmin - 1.0, xmax + 1.0), ylim=(-0.8,0.8)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex2_InvertedDualPendulumSwingUp.gif')
zorder=1, linewidth=2.0)) else: image.lines.append( mpl.lines.Line2D(xdata=[x_p[i - 1], x_p[i]], ydata=[y_p[i - 1], y_p[i]], color='black', zorder=1, linewidth=2.0)) # and return the image return image # create Animation object A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0, r'$\varphi_1$'), (3, '$x$'), (7, '$\\dot{x}$')], plotinputs=[(0, '$u$')]) xmin = 0 xmax = 0 A.set_limits(xlim=(xmin - 1.5, xmax + 1.5), ylim=(-2.0, 2.0)) # if 'plot' in sys.argv: if 0: A.show(t=S.b) if 1: A.animate() # plt.show() # A.save('TriplePendulum1.gif') A.save('TriplePendulum1.mp4')
image.patches.append(sphere2) image.patches.append(car) image.patches.append(joint) image.lines.append(rod1) image.lines.append(rod2) return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex2_InvertedDualPendulumSwingUp.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0, 'x'), (2, 'phi1'), (4, 'phi2')], plotinputs=[(0, 'u')]) xmin = np.min(S.sim_data[1][:, 0]) xmax = np.max(S.sim_data[1][:, 0]) A.set_limits(xlim=(xmin - 1.0, xmax + 1.0), ylim=(-0.8, 0.8)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex2_InvertedDualPendulumSwingUp.gif')
x_car = x y_car = 0 car = mpl.patches.Rectangle((x_car-0.5*car_width, y_car-car_heigth), car_width, car_heigth, fill=True, facecolor='grey', linewidth=2.0) image.patches.append(car) return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex6_ConstrainedDoubleIntegrator.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0,'x'), (1,'dx')], plotinputs=[(0,'u')]) xmin = np.min(S.sim_data[1][:,0]) xmax = np.max(S.sim_data[1][:,0]) A.set_limits(xlim=(xmin - 0.1, xmax + 0.1), ylim=(-0.1,0.1)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex6_ConstrainedDoubleIntegrator.gif')
rod1 = mpl.lines.Line2D([0,x1],[0,y1],color='k',zorder=0,linewidth=2.0) rod2 = mpl.lines.Line2D([x1,x2],[y1,y2],color='k',zorder=0,linewidth=2.0) # pendulums sphere1 = mpl.patches.Circle((x1,y1),0.01,color='k') sphere2 = mpl.patches.Circle((0,0),0.01,color='k') image.lines.append(rod1) image.lines.append(rod2) image.patches.append(sphere1) image.patches.append(sphere2) return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex4_UnderactuatedManipulator.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0,'phi1'), (2,'phi2')], plotinputs=[(0,'u')]) A.set_limits(xlim= (-0.1,0.6), ylim=(-0.4,0.65)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex4_UnderactuatedManipulator.gif')
car_heigth, fill=True, facecolor='grey', linewidth=2.0) image.patches.append(car) return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex6_ConstrainedDoubleIntegrator.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0, 'x'), (1, 'dx')], plotinputs=[(0, 'u')]) xmin = np.min(S.sim_data[1][:, 0]) xmax = np.max(S.sim_data[1][:, 0]) A.set_limits(xlim=(xmin - 0.1, xmax + 0.1), ylim=(-0.1, 0.1)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex6_ConstrainedDoubleIntegrator.gif')
# add the patches and lines to the image image.patches.append(pendulum1) image.patches.append(pendulum2) image.patches.append(car) image.patches.append(joint) image.lines.append(rod1) image.lines.append(rod2) # and return the image return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex8_ConstrainedDoublePendulum.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: # create Animation object A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0,'$x$'),(1,'$\\dot{x}$')], plotinputs=[(0,'$u$')]) xmin = np.min(S.sim_data[1][:,0]) xmax = np.max(S.sim_data[1][:,0]) A.set_limits(xlim=(xmin - 1.0, xmax + 1.0), ylim=(-1.2,1.2)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex8_ConstrainedDoublePendulum.gif')
car_width = 0.05 car_heigth = 0.02 x_car = x y_car = 0 car = mpl.patches.Rectangle((x_car - 0.5 * car_width, y_car - car_heigth), car_width, car_heigth, fill=True, facecolor='grey', linewidth=2.0) image.patches.append(car) return image if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0, 'x'), (1, 'dx')], plotinputs=[(0, 'u')]) xmin = np.min(S.sim_data[1][:, 0]) xmax = np.max(S.sim_data[1][:, 0]) A.set_limits(xlim=(xmin - 0.1, xmax + 0.1), ylim=(-0.1, 0.1)) if 'plot' in sys.argv: A.show(t=S.b)
image.patches.append(car) image.patches.append(joint) image.lines.append(rod1) image.lines.append(rod2) # and return the image return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex8_ConstrainedDoublePendulum.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: # create Animation object A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0, '$x$'), (1, '$\\dot{x}$')], plotinputs=[(0, '$u$')]) xmin = np.min(S.sim_data[1][:, 0]) xmax = np.max(S.sim_data[1][:, 0]) A.set_limits(xlim=(xmin - 1.0, xmax + 1.0), ylim=(-1.2, 1.2)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex8_ConstrainedDoublePendulum.gif')
# then the pendulums for i in xrange(3): image.patches.append( mpl.patches.Circle(xy=(x_p[i], y_p[i]), radius=pendulum_sizes[i], color='black') ) if i == 0: image.lines.append( mpl.lines.Line2D(xdata=[x_car, x_p[0]], ydata=[y_car, y_p[0]], color='black', zorder=1, linewidth=2.0) ) else: image.lines.append( mpl.lines.Line2D(xdata=[x_p[i-1], x_p[i]], ydata=[y_p[i-1], y_p[i]], color='black', zorder=1, linewidth=2.0) ) # and return the image return image # create Animation object if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0,'$x$'),(1,'$\\dot{x}$')], plotinputs=[(0,'$u$')]) xmin = np.min(S.sim_data[1][:,0]) xmax = np.max(S.sim_data[1][:,0]) A.set_limits(xlim=(xmin - 1.5, xmax + 1.5), ylim=(-2.0,2.0)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex9_TriplePendulum.gif')
mpl.lines.Line2D(xdata=[x_car, x_p[0]], ydata=[y_car, y_p[0]], color='black', zorder=1, linewidth=2.0)) else: image.lines.append( mpl.lines.Line2D(xdata=[x_p[i - 1], x_p[i]], ydata=[y_p[i - 1], y_p[i]], color='black', zorder=1, linewidth=2.0)) # and return the image return image # return the drawing function return draw # create Animation object A = Animation(drawfnc=create_draw_function(N=N), simdata=S.sim) xmin = np.min(S.sim[1][:, 0]) xmax = np.max(S.sim[1][:, 0]) A.set_limits(xlim=(xmin - 1.5, xmax + 1.5), ylim=(-2.0, 2.0)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex_n_bar_pendulum.gif')
rod1 = mpl.lines.Line2D([0,x1],[0,y1],color='k',zorder=0,linewidth=2.0) rod2 = mpl.lines.Line2D([x1,x2],[y1,y2],color='0.3',zorder=0,linewidth=2.0) # pendulums sphere1 = mpl.patches.Circle((x1,y1),0.01,color='k') sphere2 = mpl.patches.Circle((0,0),0.01,color='k') image.lines.append(rod1) image.lines.append(rod2) image.patches.append(sphere1) image.patches.append(sphere2) return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex5_Acrobot.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0,'phi1'),(2,'phi2')], plotinputs=[(0,'u')]) A.set_limits(xlim=(-1.1,1.1), ylim=(-1.1,1.1)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex5_Acrobot.gif')
# joint joint = mpl.patches.Circle((x_car,0),0.005,color='k') image.lines.append(rod) image.patches.append(sphere) image.patches.append(cart) image.patches.append(joint) return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex1_InvertedPendulumTranslation.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0,'x'), (2,'phi')], plotinputs=[(0,'u')]) xmin = np.min(S.sim_data[1][:,0]) xmax = np.max(S.sim_data[1][:,0]) A.set_limits(xlim=(xmin - 0.5,xmax + 0.5), ylim=(-0.3,0.8)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex1_InvertedPendulumTranslation.gif')
# and return the image return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex0_InvertedPendulumSwingUp.pcl') # now we can create an instance of the `Animation` class # with our draw function and the simulation results # # to plot the curves of some trajectories along with the picture # we also pass the appropriate lists as arguments (see documentation) if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0,'x'), (2,'phi')], plotinputs=[(0,'u')]) # as for now we have to explicitly set the limits of the figure # (may involves some trial and error) xmin = np.min(S.sim_data[1][:,0]); xmax = np.max(S.sim_data[1][:,0]) A.set_limits(xlim=(xmin - 0.5, xmax + 0.5), ylim=(-0.6,0.6)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: # if everything is set, we can start the animation # (might take some while) A.animate() # then we can save the animation as a `mp4` video file or as an animated `gif` file
xx = S[:, 0].copy() yy = S[:, 1].copy() S[:, 0] = xx * cos(theta) - yy * sin(theta) + x S[:, 1] = yy * cos(theta) + xx * sin(theta) + y aircraft = mpl.patches.Polygon(S, closed=True, facecolor='0.75') image.patches.append(aircraft) return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex3_Aircraft.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(4, 'theta')], plotinputs=[(0, 'F1'), (1, 'F2')]) A.set_limits(xlim=(-1, 11), ylim=(-1, 7)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex3_Aircraft.gif')
[ 0.7, 0], [ 0.1, 0.1]]) xx = S[:,0].copy() yy = S[:,1].copy() S[:,0] = xx*cos(theta)-yy*sin(theta)+x S[:,1] = yy*cos(theta)+xx*sin(theta)+y aircraft = mpl.patches.Polygon(S, closed=True, facecolor='0.75') image.patches.append(aircraft) return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex3_Aircraft.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(4,'theta')], plotinputs=[(0,'F1'),(1,'F2')]) A.set_limits(xlim=(-1,11), ylim=(-1,7)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex3_Aircraft.gif')
# finally we add the patches and line to the image image.patches.append(pendulum) image.patches.append(car) image.patches.append(joint) image.lines.append(rod) # and return the image return image sol_data = tt, xx, uu # to plot the curves of some trajectories along with the picture # we also pass the appropriate lists as arguments (see documentation) A = Animation(drawfnc=draw, simdata=sol_data, plotsys=[(0,'x'), (2,'phi')], plotinputs=[(0,'u')]) # as for now we have to explicitly set the limits of the figure # (may involve some trial and error) xmin = np.min(xx[:,0]); xmax = np.max(xx[:,0]) A.set_limits(xlim=(xmin - 0.5, xmax + 0.5), ylim=(-0.6,0.6)) if 'animate' in sys.argv: # if everything is set, we can start the animation # (might take some while) A.animate() # then we can save the animation as a `mp4` video file or as an animated `gif` file A.save('InvertedPendulum.gif')
linewidth=2.0) image.patches.append(pendulum) image.patches.append(car) image.patches.append(joint) image.lines.append(rod) return image if not 'no-pickle' in sys.argv: # here we save the simulation results so we don't have to run # the iteration again in case the following fails S.save(fname='ex7_ConstrainedInvertedPendulum.pcl') if 'plot' in sys.argv or 'animate' in sys.argv: A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0, 'x'), (1, 'dx')], plotinputs=[(0, 'u1')]) xmin = np.min(S.sim_data[1][:, 0]) xmax = np.max(S.sim_data[1][:, 0]) A.set_limits(xlim=(xmin - 0.5, xmax + 0.5), ylim=(-0.6, 0.6)) if 'plot' in sys.argv: A.show(t=S.b) if 'animate' in sys.argv: A.animate() A.save('ex7_ConstrainedInvertedPendulum.gif')
# now we can create an instance of the `Animation` class # with our draw function and the simulation results # save the simulation data (solution of IVP) to csv tt, xx, uu = S.sim_data export_array = np.hstack((tt.reshape(-1, 1), xx, uu)) np.savetxt("pendulum.csv", export_array, delimiter=",") # first column: time # next n columns: state (here n = 4) # last m columns: input (here m = 1) # to plot the curves of some trajectories along with the picture # we also pass the appropriate lists as arguments (see documentation) A = Animation(drawfnc=draw, simdata=S.sim_data, plotsys=[(0, 'x'), (2, 'phi')], plotinputs=[(0, 'u')]) # as for now we have to explicitly set the limits of the figure # (may involves some trial and error) xmin = np.min(S.sim_data[1][:, 0]) xmax = np.max(S.sim_data[1][:, 0]) A.set_limits(xlim=(xmin - 0.5, xmax + 0.5), ylim=(-0.6, 0.6)) A.animate() # then we can save the animation as a `mp4` video file or as an animated `gif` file A.save('pendulum.mp4')