






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An algorithm for designing a model predictive controller for an inverted pendulum system with an adjustable cart using gekko and python. The objective is to maneuver the cart from position y=-1.0 to y=0.0 within 6.2 seconds while minimizing the use of force. The document also covers the use of deep learning methods in gekko for machine learning applications.
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!
Design A model predictive controller for an inverted pendulum system with an adjustable cart. Demonstrate that the cart can perform a sequence of moves to maneuver from position y=- 1.0 to y=0.0 within 6.2 seconds. Verify that v, theta and q are zero before and after the maneuver.
The inverted pendulum is described by the following dynamic equations: where u is the force applied to the cart, E is m2/(m1+m2), y is the position of the cart, vis the velocity of the cart, theta is the angle of the pendulum relative to the cart, m1=10, m2=1 and q is the rate of angle change. Tune the controller to minimize the use of force supplied to the cart either in the forward or reverse direction (i.e. minimize fuel consumed to perform the maneuver). Explain the tuning and the optimal solution with the appropriate plots that demonstrate that the solution is optimal.
#Weight of item m2 = 1 ################################# #Defining the time, we will go beyond the 6.2s #to check if the objective was achieved m.time = np.linspace(0,8,100) end_loc = int(100.0*6.2/8.0) #Parameters m1a = m.Param(value=10) m2a = m.Param(value=m2) final = np.zeros(len(m.time)) for i in range(len(m.time)): if m.time[i] < 6.2: final[i] = 0 else: final[i] = 1 final = m.Param(value=final) #MV ua = m.Var(value=0) #State Variables
theta_a = m.Var(value=0) qa = m.Var(value=0)
m.solve() #(disp=False) #Plotting the results import matplotlib.pyplot as plt plt.figure(figsize=(12,10)) plt.subplot(221) plt.plot(m.time,ua.value,'m',lw=2) plt.legend([r'$u$'],loc=1) plt.ylabel('Force') plt.xlabel('Time') plt.xlim(m.time[0],m.time[-1]) plt.subplot(222) plt.plot(m.time,va.value,'g',lw=2) plt.legend([r'$v$'],loc=1) plt.ylabel('Velocity') plt.xlabel('Time') plt.xlim(m.time[0],m.time[-1]) plt.subplot(223) plt.plot(m.time,ya.value,'r',lw=2) plt.legend([r'$y$'],loc=1) plt.ylabel('Position') plt.xlabel('Time') plt.xlim(m.time[0],m.time[-1])
plt.subplot(224) plt.plot(m.time,theta_a.value,'y',lw=2) plt.plot(m.time,qa.value,'c',lw=2) plt.legend([r'$\theta$',r'$q$'],loc=1) plt.ylabel('Angle') plt.xlabel('Time') plt.xlim(m.time[0],m.time[-1]) plt.rcParams['animation.html'] = 'html5' x1 = ya.value y1 = np.zeros(len(m.time)) #suppose that l = 1 x2 = 1np.sin(theta_a.value)+x x2b = 1.05np.sin(theta_a.value)+x y2 = 1np.cos(theta_a.value)-y y2b = 1.05np.cos(theta_a.value)-y fig = plt.figure(figsize=(8,6.4)) ax = fig.add_subplot(111,autoscale_on=False,
xlim=(-1.5,0.5),ylim=(-0.4,1.2)) ax.set_xlabel('position') ax.get_yaxis().set_visible(False) crane_rail, = ax.plot([-1.5,0.5],[-0.2,-0.2],'k-',lw=4) start, = ax.plot([-1,-1],[-1.5,1.5],'k:',lw=2)
mass2.set_data([x2b[i]],[y2b[i]]) line.set_data([x1[i],x2[i]],[y1[i],y2[i]]) time_text.set_text(time_template % m.time[i]) return line, mass1, mass2, time_text ani_a = animation.FuncAnimation(fig, animate,
np.arange(1,len(m.time)),
interval=40,blit=False,init_func=init)
#available from https://ffmpeg.zeranoe.com/builds/ #add ffmpeg.exe to path such as C:\ffmpeg\bin\ in #environment variables #ani_a.save('Pendulum_Control.mp4',fps=30) plt.show()