|
|
|
Andrew Kaluzniacki
|
Inverted Pendulum |
March 2004
|
|
|
How to develop a physically correct simulation of an inverted pendulumStart with a picture of the system we want to model.
Finding the Equations of MotionWe want to develop a set of equations that describe the dynamics of this pendulum. These equations are known as the equations of motion. To get these equations I prefer to use the Lagrangian[1] method because it is scalable to much more complex simulations. The Lagrangian is L = T- U where T is the kinetic energy of the system and U is the potential energy. T is the sum of kinetic energies of m1 and m2 T = 1/2 m1 v12 + 1/2 m2 v22 where v1 and v2 are the velocities of m1 and m2 respectively. U = -m1 g l cos(Φ) Giving us the Lagrangian L = 1/2 m1 v12 + 1/2 m2 v22 + m1 g l cos(Φ) We have to get L in terms of l, x and Φ instead of v1 and v2. I will use Mathematica to work through the equations, as it is a very nice tool for such work. The details are here, and give the resulting Lagrangian as L = l cos(Φ(t)) ( g m1 + m2 x'(t) Φ'(t) ) + 1/2 (m1x'(t)2 + m2 ( x'(t)2 + l2 Φ'(t)2 ) ) The Lagrangian method is nice because given this equation we can now find the equations of motion by solving the following equation.[1],[2]
Where qk are the generalized coordinates of the system and Qk the generalized force, in our case x, Φ, Fx, and FΦ. From this we find two coupled differential equations of motion. x''(t) = ( Fx(t) + l m2 (
sin(Φ(t)) Φ'(t)2 -
cos(Φ(t)) Φ''(t) )) / ( m1 + m2 ) Integrating the Equations of MotionNow that we have equations describing the dynamic motion of our pendulum we need to solve them. As our equations are differential equations, solving involves integration of the equations. In general anything but the simplest differential equations can not be integrated in closed form. That is to say there is no simple solution involving known functions like sine or cosine. Since our intention is to come up with a computer simulation we will use a numerical method of integration. Runge-Kutta[3] is the numerical method that I like to use. Runge-Kutta works with first order differential equations, and we will use the 4th order Runge-Kutta which is computed as follows. Given dy/dx = f(x,y), we want to find y(x). Suppose y = y0 at x = x0, we choose a small interval h and calculate
Then at x = x0 + h , y = y0 + 1/6(k1 + 2 k2 + 2 k3 + k4). Since we have second order equations, we first need to put them in first
order form. x'(t) = v(t) Φ'(t) = ω(t) v'(t) = (2(l Fx(t)-cos(Φ(t))FΦ(t)+l
sin(Φ(t))(gml
cos(Φ(t))
+lm2ω(t)2))) / ω'(t) = (
2lm2cos(ø(t))Fx(t) +2(m1+m2)(-FΦ(t)
+ glm1sin(Φ(t)))
+l2m22sin(2Φ(t))ω(t)2 )/
( l2m2(-2m1-m2+m2cos(2Φ(t)))) Now that we have our equations in the right form we can write the software. Model View ControllerWe will write a Java applet using the model view controller pattern, or MVC[4]. The MVC breaks up the problem into the three named classes. ModelThe model is responsible for simulating the behavior of the system. This is where we use the derived equations of motion and where our Runge-Kutta method is implemented. The model needs to keep track of the state of the system, to iterate the system through small time steps, and allow us to get the results. We will start with a Java class IPModel. IPModel exposes these public methods:
A more complete implementation would provide get() and set() methods for all the state variables but we do not need them for this demo and they are left out for clarity. You can compile and run IPModel.java by itself. The main() method will run the model through 10 seconds of time and output the model state to the console. ViewThe view needs to display the block, the pendulum mass, and the pendulum rod. We will subclass javax.swing.JPanel as our IPView class. IPView exposes these public methods:
The protected method paintComponent() handles the actual drawing. This method is not called directly, but indirectly via the this.repaint() in modelStateChanged(). A note about the Netbeans IDE
(Integrated Development Environment). ControllerOur controller is fairly simple. At a regular interval of 1/10 second the controller updates the model and the view. Therefore we do not have a separate controller class but integrate the functionality of the controller into the IPApplet class. The method init() creates an instance of IPModel and associates it with our IPView and finally starts a timer which calls actionPerformed() periodically. In turn actionPerfomed() updates the model and notifies the view that the model state has changed. Here is the result. IPApplet
Where to go from hereThis demo is complete in the sense that we have an accurate representation of the physics of the system, but it is limited in that the parameters, m1, m2 , and l are fixed. Separating the controller from IPApplet and extending it to manipulate these model parameters would be interesting. IPView should be modified in this case to better represent these values. In this case consideration needs to be taken so that the length l is correctly proportional to the distance x moved by m1. Furthermore, the model allows for Fx, and FΦ to be non zero. One could implement a controller that provides feedback to Fx such that the pendulum is balanced in the vertical position against random fluctuations applied to FΦ. CommentsI am interested in any difficulties encountered in using this information. [1] L. D. Landau and E. M. Lifshitz. Course of Theoretical Physics: Mechanics [2] H. Goldstein. Classical Mechanics [3] Jon Mathew and R. L. Walker. Mathematical Methods of Physics, Second Edition. [4] java.sun.com has a good MVC explanationhere. |
|
|
|
Copyright 2003-2004 Andrew Kaluzniacki. All rights reserved.
|