* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Controller Analysis with Inverted Pendulum
Cellular repeater wikipedia , lookup
Analog television wikipedia , lookup
Operational amplifier wikipedia , lookup
Schmitt trigger wikipedia , lookup
Phase-locked loop wikipedia , lookup
Wien bridge oscillator wikipedia , lookup
Oscilloscope history wikipedia , lookup
LN-3 inertial navigation system wikipedia , lookup
Switched-mode power supply wikipedia , lookup
Radio transmitter design wikipedia , lookup
Two-port network wikipedia , lookup
Resistive opto-isolator wikipedia , lookup
Power electronics wikipedia , lookup
Analog-to-digital converter wikipedia , lookup
Valve RF amplifier wikipedia , lookup
Mathematics of radio engineering wikipedia , lookup
Voltage regulator wikipedia , lookup
Rectiverter wikipedia , lookup
DEGREE PROJECT, IN BACHELOR'S THESIS IN MECHATRONICS , FIRST LEVEL STOCKHOLM, SWEDEN 2015 Controller Analysis with Inverted Pendulum REGULATORANALYS MED INVERTERAD PENDEL FILIP STENBECK ARON NYGREN KTH ROYAL INSTITUTE OF TECHNOLOGY SCHOOL OF INDUSTRIAL ENGINEERING AND MANAGEMENT Controller Analysis with Inverted Pendulum FILIP STENBECK ARON NYGREN Bachelor’s Thesis in Mechatronics Supervisor: Examiner: Approved: Martin Edin Grimheden Martin Edin Grimheden 2015-05-20 TRITA MMK 2015:21 MDAB074 2015-06 Abstract The aim of this thesis is to examine if feedback of the angle from an inverted pendulum is sufficient to control its angle at an unstable equilibrium with statics and force impulses, and through different approaches and choice of controller find the most suitable one for these types of applications. The controllers that was tested was, the PID regulator and the state space regulator. The results would show that the mathematical approach to find a controller is difficult and time consuming, and it is often better to use a trial and error approach to find a regulator if repeated test on the system is possible. The core of the thesis lies in the mathematical approximations of the mechanical and electrical system, the analysis of the controller and the choice and usage of components. Analysis of the combined electrical and mechanical systems were made in Simulink and Matlab and was then generated to mechanical code to an micro controller controlling the voltage to a dc-motor. The system is non linear but can be linearised around the equilibrium point that we want to maintain, which is a good approximation for small angles. This thesis describes the electrical and mechanical components used to build a rotary inverted pendulum and how to produce an effective controller in detail. iii Sammanfattning Regulatoranalys med Inverterad Pendel Målet med examensarbetet är att utvärdera om återkoppling av vinkeln från en inverterad pendel är tillräcklig för att kontrollera denna kring en instabil jämviktspunkt med störningar samt pålagda kraftimpulser, och genom val av olika tillvägagångssätt och regulatorer finna den mest lämpliga för dessa typer av tillämpningar. De regulatorer som användes i projektet var PID-regulatorn samt state space regulatorn. Resultaten kom att visa att ett matematisk tillvägagångsätt att skapa en regulator är svårt och tidskrävande, och det är ofta mer effektivt att testa sig fram till en regulator om systemet tillåter. Kärnan i arbetet ligger i de matematiska approximationerna av de mekaniska och elektriska system, analysen av kontrollern och valet samt tillämpningen av komponenter. Analysen av det kombinerade elektriska och mekaniska systemen gjordes i Simulink och Matlab och var därefter genererad till mekanisk kod till en mikro-kontroller för att regulera spänningen till en likströmsmotor. Den inverterade pendeln är ett olinjärt system men kan med god approximation och litet fel linjariseras runt dess instabila jämnviktspunkt. Detta examensarbete kommer i huvudsak handla om hur man konstruerar en regulator genom simulering samt analys av systemet. Alla komponenter såväl elektriska som mekaniska kommer att beskrivas i detalj. v Preface During our project we have received lots of assistance, all from building the prototype to modelling the system. We like to thank Staffan Qvarnström, Tomas Östberg and Jonas Stenbeck for inputs and usage of tools to construct the prototype, Sebastian Quiroga for valuable information of the software and control theory and Martin Edin Grimheden along with our fellow students taking the course for guidance writing the thesis. Filip Stenbeck, Aron Nygren Stockholm, May, 2015 vii Contents Abstract iii Sammanfattning v Preface vii Contents ix Nomenclature xi 1 Introduction 1.1 Background 1.2 Purpose . . 1.3 Scope . . . 1.4 Method . . . . . . 1 1 2 2 3 2 Theory 2.1 Control Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Electronics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 5 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Demonstrator 3.1 Problem Formulation . . 3.2 Analysis of the System . 3.3 Pole Placement . . . . . 3.4 Friction Approximation 3.5 Rotary Encoder . . . . . 3.6 Sensor Values . . . . . . 3.7 Hardware . . . . . . . . 3.8 Software . . . . . . . . . 3.8.1 Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 11 11 12 14 15 15 16 17 17 4 Results 4.1 Comparing Mathematical Controllers . . . . . . . . . . . . . . . . . . 4.2 Comparing Controllers On Demonstrator . . . . . . . . . . . . . . . 19 19 20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Discussion and conclusions 5.1 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 23 24 6 Recommendations and future work 6.1 Recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2 Future work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 27 27 Appendices A Additional information A.1 Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B Proofs B.1 Derivation of the systems transfer function . B.1.1 Mechanical system . . . . . . . . . . . B.1.2 Electrical system . . . . . . . . . . . . B.1.3 Combined systems in Laplace domain B.2 Pendulum Friction Approximation . . . . . . C References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 5 5 5 7 9 10 11 x Nomenclature Symbols Symbols Description I1 l1 m θ1 lcrank θ2 θ¨2 Icrank−arm ẍ Mmotor Kt Ke Ia La Ra Vin VC VR I R C ωc UA URa ULa Inertia of the pendulum (kg m2 ) Length of the pendulum (m) Mass of the pendulum (kg) Angle of the pendulum (rad) Length of the crank-arm (m) Angle of the crank-arm (rad) Angle acceleration of the crank-arm (rad/s2 ) Inertia of the crank-arm (kg m2 ) Acceleration in x direction (m/s2 ) Torque from the motor (N m) Torque constant (N m/A) Back emf constant(V /rpm) Electrical current (A) Inner inductance (H) Inner resistance (Ω) Voltage from sensor (V ) Voltage across capacitor (V ) Voltage across resistor (V ) Current (A) Resistance (Ω) Capacitance (F ) cut-off frequency (Hz) Voltage from H-bridge (V ) Voltage across motor resistance (V ) Voltage across motor inductor (V ) xi Symbols Description Ub Te Tω̇ Tω Ja B Back electromotive force voltage (V ) Electromagnetic torque (N m) Acceleration torque(N m) Velocity torque(N m) Inertia of the rotor (kg m2 ) Motor viscous friction (N ms) Abbreviations Abbreviation Description LQ PWM Matlab CAD PZ EMF Linear Quadratic Optimal Control Pulse Width Modulation Matrix Laboratory Computer Aided Design Pole Zero Electromotive force xii Chapter 1 Introduction We have all tried to balance something like a pole or a pen on our finger tip or seen a clown balancing plates on a pole. From this we know that we have to adjust and change the direction of our finger to maintain balance of the pen. This is done by motion tracking of our eyes and through computation in our brain evaluate which output signal should be sent to our muscles. An electronic apparatus balancing an object needs to have these hardware and software as well. For this application we can correlate the eyes of the human as a sensor, the brain as software and the muscles as the motor. To balance an object does not often seem like a difficult achievement, but in theory it is a complex task. In our daily lives inverted pendulums often appears and it is essential to understand the mechanics and to control it in an upright position. The computation that combines the input signal with the output signal is a delicate operation. Depending on how the output signal is computed, different properties of the system is obtained. To control the output signal to the muscle some sort of a regulator is needed, this calculates what acceleration of the pendulum has to be applied to keep it from falling. A regulator can be more or less advanced and take different aspects in consideration. For example not just the angle of the pendulum but also its velocity and the position of the finger. All people are not equally skilled at balancing objects. This is often not due to differences within hardware, as eyesight or muscle strength but more within the computation of the signal to the muscle. This project goes in depth of how to produce a regulator and discusses which methods are preferable for different applications. This chapter describes in more detail the background, purpose, limitations and methods used in the project. 1.1 Background The inverted pendulum in general is a well known and basic mechatronic problem that focuses much on modelling and control of non linear differential equations. 1 CHAPTER 1. INTRODUCTION This problem occurs in other areas such as the Segway and the SpaceX program and is often used as an example of showing the usage of control theory. The rotary inverted pendulum was first invented by Katsuhisa Furuta, and has since then been called the Furuta pendulum [14]. The Furuta pendulum is a simple design and compared with a one dimension motion control of a pendulum reduces the number of moving parts and friction within the system, this is naturally better for the project since it makes the mathematical model easier and with less approximations. A regulator for a system can be produced in a number of ways, all with different advantages. The most common is the PID regulator that has been used since the beginning of the 20th century [2]. A newer regulator is the state space regulator that can take other things than the output signal in consideration to calculate the input signal. These two controllers generally have diverse approaches to generate the controllers. The PID controller is often used as a trial and error method to create a working controller [2]. The state space is usually used when more information than the output signal is needed to control a system, with this method it is necessary to have a good mathematical model of the system which is then used to extract a state space controller. 1.2 Purpose What is the best approach to produce a controller with only respect to an output signal? Since only one output signal is measured, this is equivalent to "How to build the best PID-regulator?". To create a regulator is not a fundamental process, therefore knowledge of which controller and how to generate that controller is necessary to be an efficient mechatronics technician. In this project two of the most essential and wide spread regulators are compared and analysed. The conclusions within this thesis are meaningful for many companies involved with control design. The purpose of this project is to answer the research question stated above and to gain knowledge in automatic control and in the use of mathematical models. 1.3 Scope To evaluate the properties of the mechanical system, one must first try to explain the system in mathematical terms that as well as possible reflects the real prototype. This was the most challenging phase during the project which involves lots of electronic and mechanical theory. Nature is often non linear and many used equations in this report involves approximations based on assumptions. The mechanical model is difficult to evaluate without linearisation around its equilibrium point, and by doing this, approximate a complex non linear system to a slightly less complex linear system. Approximations within the motor model are that the inner friction and inner inductance are neglected. It is difficult to clarify and examine all approx2 1.4. METHOD imations made and to determine how well the approximation represents reality. The state space model has no more information than what the PID controller has, which in this case is only the angle of the pendulum and its derivatives. This makes the state space model control the system on the same premises and therefore a very specific way of comparing the two controller design methods. 1.4 Method The research question is about how to produce efficient regulators in both functionality and development. to answer this question some sort of evaluation of regulators must be made. To compare the regulators, certain properties of the system under impulses is analysed, such as rise time, overshoot and settling time. Investigating these criteria is a way to evaluate the speed of the controlled system. After the system was derived as seen in appendix B Matlab was used to make a controller using the LQ-method in state space form and a regular PID. The state space model was derived through equations in the derivation of the transfer function whilst the PID was constructed using the transfer function of the system and the built in PID tuning within Matlab. The software Matlab and Simulink was used to evaluate the theoretical system and then the two regulators was tested on the prototype for a final validation. 3 Chapter 2 Theory In order to develop and evaluate regulators controlling a pendulum some theory in electronics and control theory must be implemented. For development of the regulators one must first understand how they work and how to generate them through the mathematical model of the system. Many electrical components are used in the project and this chapter reviews what had to be accounted for, such as how the PWM frequency affects the signal and how to reduce statics within the sensor measurements. 2.1 Control Theory A mechanical system can be described with differential equations [2]. From these equations the transfer function can be derived using Laplace transform, and thus convert it to the complex plane. The transfer function is a function that describes the system, in this case the correlation between input voltage of the motor to angle of the pendulum. Pole Zero plot A pole zero (PZ) plot is a graphical representation of the transfer function. From the PZ plot certain properties can be derived, such as stability and speed of the system. The poles of a transfer function corresponds to the roots of the denominator and the zeroes are the roots of the numerator. The pole closest to the imaginary axis is the dominating pole. Imaginary poles indicates oscillations in the system [2]. PID Regulator A regulator is a device that from a given input calculates a control signal,u, and transmits it to a system. [2] When the system receives this control signal it will respond and give an output y. The output can be used to again calculate the control signal, this is called feedback. When feedback is obtained one can implement a PID 5 CHAPTER 2. THEORY controller. The PID uses the error e e(t) = r − y (2.1) which is the difference between the output and a desired value/reference value r, in the following way [2] u(t) = KP e(t) + KI Z t 0 e(τ ) dτ + KD d e(t) dt (2.2) where KP , KI and KD are design parameters. The design parameters can be determined by following this heuristic technique. [2] KP increases the response time of the system but worsens the stability. KI eliminates consistent errors, but also worsens the stability. KD improves the stability. State Space Regulator In a state space model the internal states x of the system are used to predict a future output, y(t). Therefore y is no longer only depending on the input but also on the internal states of the system. A state space model can be written as ẋ = A x + B u (2.3) where A is a matrix that that describes how the system is depending on the internal states and B is how the system depends on the input y = Cx + Du (2.4) where C and D are matrices that describes how the output is depending on the internal states respective input. The feedback is introduced as u = −Lx + r∗ (2.5) this inserted in 2.3 gives the state space feedback as ẋ = (A − BL) x + B r∗ where r∗ is connected to the reference signal. 6 (2.6) 2.2. ELECTRONICS Linear Quadratic Regulator A Linear quadratic regulator minimizes the deviations, i.e in this case the deviations from the desired angle. This is done by minimizing the cost function, which is a sum of the deviation of the measured angle from its desired value [3]. The cost function for a continuous-time linear system is J= Z ∞ 0 (xT (Q + AT P + P A − P B B T P ) x + (u + B T P x)2 )dt (2.7) where Q is a diagonal matrix. The diagonal elements of Q indicates how fast you want the corresponding x component to stabilize [2]. The u that minimizes the cost is u = −L x = −B T P x (2.8) where P can be calculated from the Reccati equation Q + AT P + P A − P B B T P = 0 (2.9) When P is solved and inserted in 2.8 the following system with feedback is 2.2 ẋ = ( A − B L ) x + B u (2.10) y=Cx (2.11) Electronics Low Pass Filter The system is made stable around an unstable equilibrium point by controlling the output from a dc-motor with regard to the angle of a rotary encoder. This encoder gives an analogue output signal depending on the angle of the pendulum. This signal contains lots of statics, and without filtering the output signal, a stable system cannot be generated. Statics occurs often in higher frequencies and with a first order low pass filter the influence of these statics can be decreased significantly. A resistor-capacitor circuit in series was chosen as the low pass filter, this is a brief derivation of how a RC-filter lowers the affect from statics. Kirchhoff’s voltage law on figure 2.1 and transformation into the Laplace domain gives the following Vin − VC − VR = 0 (2.12) where VC and VR are [15] VR = RI → I = VR R Z i VC = dt C (2.13) (2.14) the above equation 2.14 in the Laplace domain results in VC = I → I = VC Cs Cs 7 (2.15) CHAPTER 2. THEORY Figure 2.1: RC-Circuit. the equations 2.14 and 2.13 can be combined and inserted in 2.12 to derive the transfer functions VC 1 = HC (s) = (2.16) Vin 1 + RCs and VR RCs (2.17) = HR (s) = Vin 1 + RCs With substitution of the imaginary Laplace variable s to iw the transfer functions can be analysed for different frequencies w. For small w ( HC (iω) → 1 ω→0 HR (iω) → 0 And for high frequencies HC (iω) → 0 HR (iω) → 1 ( ω→∞ So if ω → 0 all of the voltage drop is over the capacitor and nothing over the resistor. The other way if ω → ∞ then all the voltage drop is over the resistor. Thus for a low pass filter the output signal should be measured over the capacitor and for a high pass filter the voltage over the resistor should be measured. The cut-off frequency is defined as when HC (s) = HR (s) = √12 Solving this will generate the cutoff frequency ωc as ωc = 1 . RC (2.18) Pulse Width Modulation Pulse width modulation is a way to create what can be interpreted as an analogue signal but is actually a digital signal. This is done by setting the voltage to high and low at high frequencies. For example, if the digital signal is high 50 percent of the time and low 50 percent then the average voltage is one half of the high voltage. When the signal is high 50 percent of the time it is called 50 percent duty 8 2.2. ELECTRONICS cycle. In order to use this method properly the frequency of the PWM should be so high that the driven device cannot differentiate the variations between high and low. The micro-controller used in this project [6] has 3 timers. These timers counts from 0 to a set value over and over again.[7] The timers also have two outputs compare registers each. The output compare registers check for a value set by the user while the timers are counting. When the value is found it toggles the output, thus creating the PWM width of the signal. For example if the compare register value is set to 180 the duty cycle of the output is 180 256 = 0.70, i.e 70 percent. The timers have two main modes, these are called fast PWM and phase-correct PWM. In the fast PWM the counters count from 0 to 255 and the phase-correct PWM also counts from 0 to 255 but then back to 0 again. The main differences between these two modes are that in phase-correct mode the output is turned off both when the timer hits the compare register value on the way from 0 to 255 and from 255 to 0, the fast PWM mode only turns off the output when the timer hits the compare register value when counting from 0 to 255. This will give the phase-correct PWM mode about half the frequency of the fast PWM mode. PWM Frequency The output of the PID- controller updates every 3 milliseconds. Therefore a high PWM frequency is required, because you want as many periods as possible between every update to get a continuous torque to the motor. If a high frequency is not used there is a chance that the PWM signal to the motor is not able to complete a full period and thus the duty cycle is changed from its given value from the controller. Figure 2.2: PWM signal 500 Hz with 3 ms uppdate. As seen in figure 2.2 if a 50 percent duty cycle on a 500 Hz frequency with 3 ms update the signal is cut in the middle of a period and thus creating a new duty cycle of cirka 67 percent. 9 Chapter 3 Demonstrator This chapter is about the components of the demonstrator and how it is used to analyse the system and answer the research question. 3.1 Problem Formulation What is the best approach to produce a controller with only respect to an output signal? Answering this question with a good basis can be done through a number of ways, the path chosen in this project was to develop a prototype with one input signal and establish two separate regulators to draw conclusions from. When comparing the controllers the outcome depends on how well the mathematical model reflects the prototype and it is then crucial to make it as accurate as possible. 3.2 Analysis of the System The transfer function of the system is derived through equation B.23. With the approximation to ignore the influence of the inner friction of the motor and inner inductance the following transfer function is obtained. −0.1646s s3 − 1.044s2 − 41.84s + 0.001771 (3.1) This transfer function should approximate the mechanics of the prototype, to investigate the properties of the system PZ plot is used. As seen in figure 3.1a the position of the pole on the right hand side of the imaginary axis indicates that this system is unstable. To make the system stable and move the unstable pole to the left half plane a negative feedback from a regulator can be implemented. Some systems can be unstable in open loop but stable in closed loop configuration [1]. To investigate if the system can be controlled with negative feedback of only the angle a root locus can be drawn, figure 3.1b. The pole on the right hand side 11 CHAPTER 3. DEMONSTRATOR Pole zero map for uncompensated system Root locus for open loop system 1 3 0.8 2 Imaginary Axis (seconds−1) Imaginary Axis (seconds−1) 0.6 0.4 0.2 0 −0.2 1 0 −1 −0.4 −0.6 −2 −0.8 −1 −6 −4 −2 0 2 4 6 −3 −30 8 Real Axis (seconds−1) −20 −10 0 10 20 30 Real Axis (seconds−1) (a) 3.1 Pole Zero map. (b) 3.1 Root Locus. of the imaginary axis in the s-plane has a branch that never passes the imaginary axis, and thus the system cannot be stable using a closed loop a configuration with loop gain of K [2]. Controlling this system require a more advanced controller. To move the poles of the PZ plot two methods were used. The first choice was the LQ method. This involves expressing the system in state space form and from this find a regulator sufficient for stabilisation. The second choice was using Matlab’s built in application PID Tuning. 3.3 Pole Placement State Space Model The state space was made based on the equations B.8 and B.24 and from those the following block diagram could be manufactured. The internal states x were chosen Figure 3.2: Block diagram for state space modelling. to Θ1 , θ1 and θ˙1 . Here θ1 is the angle of the pendulum and the only data measured. 12 3.3. POLE PLACEMENT Θ1 and θ˙1 was obtained through integration and derivation of the angle. In matrix form the states are θ1 Θ1 ˙ x = θ1 ẋ = θ1 . θ˙1 θ¨1 The state space model is defined through 2.3 and 2.4 as the following. ẋ = A x + B u y = Cx + Du With the specified states and the block diagram 3.2 the state space model could be generated. The first two values of ẋ are already defined within the x vector, however θ¨1 have to be computed through the block diagram and be expressed with the states in x and input signal Ua . The output signal is defined as the angle and second state θ1 0 1.0000 0 0 h i h i 0 1.0000 B= 0 C= 0 1 0 D= 0 A= 0 1.0435 41.8448 0.0018 0.1646 With the state space model it is then possible to construct a regulator if the system is controllable. This is determined by looking at the control matrix S, that is defined as S= B AB ... An−1 B . If the system only has one input signal, then this matrix is quadratic and controllable if the determinant of S 6= 0 [2]. The control matrix used in the project is S = −0.0045 6= 0 (3.2) thus controllable. The system seen in figure 3.1a is clearly unstable and it is needed to move the unstable pole to the left hand plane. This is done with negative feedback of the states and to calculate a sufficient regulator for this project a LQ regulator was chosen. Linear Quadratic Regulator Matlab was used to solve the Ricatti equation with the built in function care, using the generated state space model. Except from the solution to the Ricatti equation the care function also returns the gain matrix L and the eigenvalues of the closed loop system, i.e the poles. The gain matrix is the feedback needed to control the 13 CHAPTER 3. DEMONSTRATOR system. Where the input signal is determined through the gain matrix and states as the following u = Lx (3.3) h i where the gain matrix is calculated as L= 12.6818 529.3612 80.2195 . This is the feedback combining the states of the system with the input signal in volts, though the input is controlled with a resolution of maximum 12 volts in 8 bits. This can 12 the be accounted for with multiplication of this factor ( 255 ). hThe controlleri is now directly comparable with a PID controller with the values of Ki Kp Kd = h i 0.5968 24.9111 3.7750 . PID Tuning Matlab has an built in application that allow users to design a PID controller by importing their plant and choosing different parameters, such as speed, robustness and aggressiveness to optimize their controller. Using the transfer function B.25 and tuning the parameters to suitable characteristics, i.e low overshoot, fast rise time and settling time, the resulting PID became • Kp=651.3617 • Ki=616.3995 • Kd=84.0449 This can interpreted for the controller used in Arduino with the same multiplication 12 factor as for LQR, ( 255 ) as • Kp=30.6523 • Ki=29.0070 • Kd=3.9551 3.4 Friction Approximation A difficult task is to approximate friction within a system, it is often non linear but can be approximated as linear in many cases. Friction of the axis of the pendulum and within the sensor was approximated as bθ̇ and could be experimentally estimated with the model of the system. The differential equations for the pendulum seen in equation B.2 implemented in Matlab [16] gives the following plot. Comparing figure 3.3a with figure 3.3b for the measured values with the same initial conditions almost the same result is achieved. Here the vertical axis is the angle of the pendulum in radians and the horizontal axis is time in seconds. This shows that the mathematical models share mechanical properties and are comparable to each other. 14 3.5. ROTARY ENCODER 2 2 1.5 1.5 1 1 0.5 0.5 0 0 -0.5 -0.5 -1 -1 -1.5 -1.5 0 2 4 6 8 10 12 14 16 0 (a) 3.3 Matlab simulation. 3.5 2 4 6 8 10 12 14 16 (b) 3.3 Measured values. Rotary Encoder A rotary encoder is used to measure the angle of the pendulum. It is a noncontacting sensor with a 360 degrees position feedback, which in this project is beneficial because the friction should contribute as little as possible to the control of the pendulum. 360 degrees position feedback is useful to measure the angle both for a potential swing-up phase and the balancing phase as the angle should be measured at all time. The rotary encoder has a 12 bit resolution which gives an angle accuracy of 12 degree, which in this case is a sufficient accuracy for the pendulum’s angle. 3.6 Sensor Values The rotary encoder used gave an analogue output signal for any given angle of the pendulum, but with accurate examination of the output signal it was determined that the signal contained statics. The statics are harmful for the PID-controller due to that fluctuation of a small error is amplified with the derivative part of the controller. This was first tried to be avoided with a digital filter, the digitalSmooth function from the Arduino library was tested. This function saves a certain number of previous values of the output signal, sorts it by value, discards the extreme values and then the final output is the mean of the remaining values.[9] This will to a certain extent eliminate the effects of the static within the signal but the drawback is that it introduces a large delay in the output signal(approximately 6 [ms]) thus impairing the controller. 1 Instead an analogue RC-filter was chosen, where frequencies ω < RC leaves the output unaffected, thus reducing the effect of noise. The analogue low pass filter will not result in a delay of the output signal and is therefore much better than a 15 CHAPTER 3. DEMONSTRATOR digital filter in this instance. The cut-off frequency chosen was 140 [Hz] and was determined through experiments adequate. The result of the RC-filter can be seen in the following figure Figure 3.4: Filtering of sensor values. where the green signal is the unfiltered measurements and the yellow signal is the filtered measurements. As seen in figure 3.4 the statics are drastically reduced. 3.7 Hardware The mechanical construction consists mainly of three parts, the base on which the motor is attached, the crank arm on which the rotary encoder and housing of the bearing are attached and the axis where the sensor and pendulum are connected. The electrical components used in the demonstrator are the motor driver board for the L298n H-bridge [11], the Arduino microcontroller board [10] and the DC motor from Faulhaber, model 028c in the 2842 series [8]. For recreation purposes important features of the components can be found in appendix A. Figure 3.5: Blockscheme of electronics. 16 3.8. SOFTWARE 3.8 Software Simulating the transfer function and state space of the system can be accomplished through software. To generate working regulators for the system Matlab was used. Though Matlab has many working functions for analysing a system, some things are not taken in consideration. Simulink is a program more suitable for analysing system and has been developed for control theory. With Simulink statics, discrete signal and sample time can be simulated. These are concepts that are not shown in the mathematical model analysed in Matlab. Simulink was used to simulate the system B.23 and the controller seen in the Matlab code [16]. Each block represent a differential equation for each system. An impulse is used at the beginning to simulate a angle of the pendulum that is not zero. Negative feedback is used. With the two scopes the angle of the pendulum and the voltage given to the DC motor can be shown in a graph. (a) Simulink Model (b) Impulse Response 3.8.1 Arduino Arduino has a build in PWM call function, analogW rite(P IN, DU T Y Y CLE), which use the fast PWM mode, where the timers count from 0 to 255 with a frequency of 500 Hz. With a sample frequency of 330 Hz, this made the built in PWM frequency inadequate and instead pins on the ATmega328 was activated manually to increase the PWM frequency. 17 Chapter 4 Results To compare the two controllers the stability of the system is first analysed. This is done by investigate the position of the poles and zeroes of the system with a PZ plot. Next a impulse response of the controlled system will be looked at to determine the rise time, settling time and overshoot. The controllers were also tested on the demonstrator to examine if they correspond to the mathematical model. 4.1 Comparing Mathematical Controllers The Linear quadratic regulator and the PID controller successfully moved the poles so that the system is stable. Pole Zero plot with PID 4 0.6 3 0.4 2 Imaginary Axis (seconds−1) Imaginary Axis (seconds−1) Pole Zero Map with LQ Regulator 0.8 0.2 0 −0.2 1 0 −1 −0.4 −2 −0.6 −3 −0.8 −7 −6 −5 −4 −3 −2 −1 −4 −7 0 −1 −6 −5 −4 −3 −2 −1 0 −1 Real Axis (seconds ) Real Axis (seconds ) (a) 4.1 LQR. (b) 4.1 PID. As seen in figure 4.1a and 4.1b all poles are on the left hand side of the imaginary axis and thus the feedback system is stable. By analysing a impulse response of the stable system, characteristics such as rise time, overshoot and settling time can be obtained. The vertical axis in the plots corresponds to the calculated angle and the horizontal axis is time in seconds. In 19 CHAPTER 4. RESULTS the plots the systems are not affected by the same impulse thus not generating the same magnitude in peak value, this will be discussed later in discussions. Though all characteristics comparing the two systems are unaffected. The impulses are simulated with a built in function in Matlab. From figure 4.2a and 4.2b the following Impulse Response −3 Impulse Response with PID x 10 10 14 12 8 10 6 Amplitude Amplitude 8 4 6 4 2 2 0 0 −2 0 1 2 3 4 5 6 Time (seconds) 7 8 9 −2 10 0 0.5 (a) 4.2 LQR. 1 1.5 2 2.5 3 Time (seconds) 3.5 4 4.5 5 (b) 4.2 PID. results were collected Characteristics Rise time Settling time Overshoot 4.2 LQR 0.77 0.929 0.008 PID 0.0261 1.95 18.4 Unit s s % Comparing Controllers On Demonstrator These controllers were used on the demonstrator and the result can be seen in the following figures. In figures 4.3a, 4.3b and 4.4 the Digital value corresponds to angle of the pendulum. The pendulum was placed at a stable position with zero angle velocity before the regulator started. As seen in figure 4.3a the angle was kept stable for approximately 3.5 seconds. In figure 4.3b the angle was never kept stable. The reason for this behaviour will be discussed in discussions and conclusions. A controller was also made by trial and error method until the angle was shown stable [17]. A short measurement showing the stable system is seen in figure 4.4. 20 4.2. COMPARING CONTROLLERS ON DEMONSTRATOR ADC Testing ADC Testing 10 3 2 Digital Value Digital Value 5 1 0 0 -1 -2 -5 0.5 1 1.5 2 2.5 3 3.5 4 4.5 0 0.2 0.4 Time in seconds 0.6 0.8 1 (b) 4.3 PID. ADC Testing 25 20 15 10 5 0 -5 -10 -15 -20 -25 0 0.5 1.2 Time in seconds (a) 4.3 LQR. Digital Value 0 1 1.5 2 2.5 3 Time in seconds Figure 4.4: Own PID. 21 3.5 4 4.5 1.4 1.6 1.8 Chapter 5 Discussion and conclusions 5.1 Discussion The results when comparing the two regulators was that even though both were stable and had promising properties by analysing the model was when implemented on the prototype shown to not be stable. This must depend on that the model is not a good enough representation of the system. This chapter will try to answer how the analysis of the model diverges from the testing of the prototype. Sample Time, Sensor Values and Statics The development of the regulators was done in Matlab. Here the LQ-method produces a regulator that minimizes the cost function of the system and thus creating the state space regulator and through PID Tuning a regulator was created with desired properties. A large flaw in this way of producing a regulator is that Matlab only takes the transfer function in consideration. This is equivalent to a system were the measured values are continuous and all states has infinite high refresh rate and no statics occur. Another way to produce the controller would be to analyse with Simulink and with a trial and error approach finding a suitable regulator. With Simulink sample time, discrete values and statics can be generated to better simulate reality. The most critical property for the prototype is probably that the resolution of the sensor values are only 0.5 degrees and to approximate this with a continuous function is far from correct. Even though a filter was implemented small statics from the sensor data occurred. With a low resolution from the sensor combined with statics results in that the derivative part of the controller fluctuates drastically and therefore the controller acts much more unpredictable than what is shown when simulating in Matlab. 23 CHAPTER 5. DISCUSSION AND CONCLUSIONS Mechanical Modelling When computing the transfer function of the system approximations are inevitable. For all parameters within the model estimations are made. Estimations should be as accurate as possible and have to be in the right magnitude of the correct value. One of the few model validations that are made is shown when approximating the friction within bearing and sensor 3.3a, 3.3b. It is shown that the model is similar to the prototype but there are distinct differences in the period time and the shape of the transients. This might be because the approximation of the friction with bθ˙1 [16] is surely not an accurate approximation due to that the friction has a dynamic part and static part and those are not accounted for. Another reason to why the period time is incorrect is the approximation of moment of inertia for the rod. This is approximated with I0 = ZZZ r2 ρ dV = V (ρAl)l2 3 (5.1) where A is the base area, l is the length and ρ is the density of the rod. This is only for the pendulum and not the axis which the pendulum is attached to and thus a fault is generated. The Euler method approximates the derivatives and the smaller the step length the better estimation of the differential equation. In the Matlab code a step value of 0.001 seconds was chosen and the influence of this is negligible. So many approximations are made by estimating parameters and simulating systems. Though they are approximated they correspond well to the prototype and it is believed that the mechanical model is not the reason that the Matlab simulation does not correlate to the prototype. The two plots 4.2a and 4.2b was noted not to have the same magnitude in peak value. This was said to be due to different magnitude in impulses, but was never motivated. One could argue that this may in fact be due to that the regulators are not the same for the two systems. This is unlikely due to that the parameters Kp , Ki , Kd and the speed of the systems are all in the same magnitude. The difference between peak values in the plots are by a factor of 103 and therefore most probable affected by different impulses. The plots are comparable due to, if the impulse is scaled so is the input to the motor, and since a max value of the motor is not determined in Matlab the scaled impulse would not generate a different results in rise time and settling time, and the overshoot is in percent which also is unaffected. In other words, only the amplitude axis is scaled with a larger impulse. 5.2 Conclusions Constructing a regulator is not a simple procedure. Before choosing a regulator and work flow one must first understand which approach is the most effective for a 24 5.2. CONCLUSIONS certain application. In general a trial and error approach for constructing a PID regulator is recommended, as seen in figure 4.4. This is due to that for most applications it is possible to test its way towards a working regulator, without taking in consideration the mechanical model and simulations. This is often an easy and efficient way to develop a controller that is functional. With this said there are instances when analysing a system might be better. Suppose that a system that is extremely difficult to regulate must be controlled or that a regulator can not be generated through trial and error. Then the LQ method might be a much better way to generate a controller. Assuming that the system can be well approximated with a state space model and it is controllable, then the solution of the Riccati equation always makes the system stable. Finding this solution through calculations makes it possible to make a working regulator for any given system that is controllable, though it is difficult and time consuming. The same system can be simulated through PID-tuner which is an easy and user friendly way to generate a working regulator. The drawback is that it is also based on trial and error but for the transfer function and if the system is to difficult to control it might be a more time consuming approach than the LQ method. 25 Chapter 6 Recommendations and future work 6.1 Recommendations It is recommend to try and minimize the moment of inertia of the crank arm and pendulum. We believe that when the motor changes polarity the inertia of moving components generates a high current that can exceed the maximum allowed current of driver board. 6.2 Future work To try controlling the pendulum with also measuring the angle or angle velocity of the crankshaft and then comparing the performance between this and the regulator that only measures the angle of the pendulum. Construct a working swing-up method for the prototype. During the project it was tried to be achieved with a negative D regulator. This was proved effective but to hazardous for the driver board to continue. This can be done in other ways as well that might be more interesting in a control theory perspective and involve a very accurate mathematical model of the system. From the beginning of the project it was first desired to control a non stiff pendulum. This could possibly be done with analysis of mechanics of materials to determine the bow of the pendulum and therefore the angle to the center of mass given the angle of the pendulum and torque from the motor. It is a non linear problem that would be interesting to examine whether it would be able to control or not. 27 Appendix A Additional information A.1 Hardware Figure A.1: mount for the motor, is connected to the planet gear. 1 APPENDIX A. ADDITIONAL INFORMATION Figure A.2: Crank arm Figure A.3: The axis of the pendulum and rotary sensor. 2 A.1. HARDWARE Figure A.4: Crank arm 3 Appendix B Proofs B.1 B.1.1 Derivation of the systems transfer function Mechanical system Figure B.1: Figure of the crank-arm. This paragraph is how the transfer function of the system is obtained, step by step it is shown what approximations are made and how we combine the mechanical and electrical systems. y O: I2 θ¨2 = Mmotor − F lcrank (B.1) Torque equation of the crank-arm seen in figure Figure B.1 with regard to the motor axis (right figure). Where I2 , θ2 , lcrank is the inertia, angle, half the length of the crank-arm. F is the force from the pendulum on the crank-arm and Mmotor is the torque from the motor. The approximation that the friction from bearings within the motor is insignificant is made. 5 APPENDIX B. PROOFS Figure B.2: Figure of the pendulum. 2 F = m ẍ + m l1 θ¨1 cos θ1 − m l1 θ˙1 sin θ1 (B.2) B.2 is the force equation of the pendulum in horizontal direction seen in figure Figure B.2, m, θ1 , l1 is the mass, angle and half the length of the pendulum shown in the left figure. From equation B.1 and B.2 the following expression is obtained. I2 θ¨2 = Mmotor − lcrank m (ẍ + l1 θ¨1 cos θ1 − l1 θ˙1 sin θ1 ) 2 With substitution as in B.3 the following equation for the torque of the motor is derived. ẍ = θ¨2 lcrank (B.3) Substitution of the acceleration to polar coordinates. 2 Mmotor = θ¨2 (I2 + lcrank m) + lcrank m (ẍ + l1 θ¨1 cos θ1 − l1 θ˙1 sin θ1 ) 2 Torque equation for the pendulum with regard to the center of mass witch gives the following expression. Here the friction from the bearing is estimated with b θ˙1 . y mg: I1 θ¨1 = −l1 N sin θ1 − l1 F cos θ1 + b θ˙1 (B.4) This gives us the following expression for N sin θ1 + F cos θ1 . N sin θ1 + F cos θ1 = 6 −I1 θ¨1 + b θ˙1 l1 (B.5) B.1. DERIVATION OF THE SYSTEMS TRANSFER FUNCTION The sum of the forces perpendicular to the pendulum and substitution with equation B.3. N sin θ1 + F cos θ1 − m g sin θ1 = m l1 θ¨1 + m θ¨2 lcrank cos θ1 (B.6) With equations B.5 and B.6 combined results in. −I1 θ¨1 + b θ˙1 − m g sin θ1 = m l1 θ¨1 + m θ¨2 lcrank cos θ1 l1 Due to that the pendulum will be balanced at an angle π + θ where θ is small. linearisation with small angle approximations are made. θ≈0→ sin(π + θ) = −θ cos(π + θ) = −1 θ˙2 = 0 These approximations results in the following equations for the mechanical system. B.1.2 2 Mmotor = θ¨2 (I2 + lcrank m) − lcrank m l1 θ¨1 (B.7) −I1 b θ˙1 θ¨1 ( − m l1 ) + + m g θ1 = −m lcrank θ¨2 l1 l1 (B.8) Electrical system This is the derivation how the voltage to the motor transitions into mechanical torque. Figure B.3: Electrical circuit of DC motor.[4] Using Kirchoff´s voltage law on the circuit above following equations can be derived [5] Ua − URa − ULa − Ub = 0 (B.9) where Ua is a voltage source, i.e the voltage across the coil of the armature, URa is the voltage across the resistor R, ULa is the voltage across the inductor L and Ub 7 APPENDIX B. PROOFS is the back emf voltage. With Ohm’s law URa can be derived as URa = Ia Ra (B.10) where Ra is the resistance of of R and Ia is the armature current. The voltage across the inductator is ULa = La I˙a (B.11) where La is the inductance of the armature coil. The back emf Ub can be written as Ub = Ke θa (B.12) where Ke is the velocity constant and θa is the angular velocity of the armature, i.e the angular veocity of the motor axis. Equations B.10, B.11 and B.12 inserted in B.9 gives the following expression Ua − Ia Ra − La I˙a − Ke θa = 0 (B.13) To get the sum of the torques a energy balance is done. And the following expression is derived Te − Mmotor − Tω̇ − Tω = 0 (B.14) where Te is the electromagnetic torque, Tω̇ is the torque due to acceleration of the rotor, Tω is the torque due to the velocity of the rotor. The electromagnetic torque can be written as Te = Kt Ia (B.15) where Kt is the motor torque constant. Tω̇ Tω̇ = Ja θ¨a (B.16) where Ja is the inertia of the rotor and θ¨a is the angular acceleration of the rotor. Tω Tω = B θ˙a (B.17) where B is the motor viscous friction constant and θ˙a is the angular velocity of the rotor. Equations B.15, B.16 and B.17 in B.14 gives the following expression Kt Ia − Mmotor − Ja θ¨a − Bθa = 0 (B.18) Substituting Ia from B.13 gives the following expression Ia = Ua − La I˙a − Ke θ˙a Ra 8 (B.19) B.1. DERIVATION OF THE SYSTEMS TRANSFER FUNCTION Substituting Mmotor and B.19 inserted in B.18 Mmotor = Kt (Ua − La I˙A − Ke θ˙a ) − Ja θ¨a − B θ˙a Ra (B.20) As the DC motor used in this project also contains a gear θ˙a and θ¨a is not the same as θ˙2 and θ¨2 , and thus need to be converted to θ˙2 and θ¨2 via θ˙2 θ˙a = 14 (B.21) θ¨2 θ¨a = 14 (B.22) θ˙2 is divided by 14 because the gear ratio is 14:1. B.1.3 Combined systems in Laplace domain Now combine the mechanical and electrical systems and from B.7 and B.20 and derive an expression of how the input signal (voltage) affects the output signal (angle) θ˙2 Kt (Ua − La I˙A − Ke 14 ) θ¨2 θ˙2 2 − Ja − B = θ¨2 (I2 + lcrank m) − lcrank m l1 θ¨1 (B.23) Ra 14 14 If we do not want to control the system with respect to the angle of the crank-arm we could transform B.8 to the Laplace domain and solve θ2 as a function of θ1 . L → θ2 (s) = 1 s2 ( −I l1 − m l 1 ) + −m lcrank bs l1 s2 +mg θ1 (s) where we define the transfer function Gi (s) Gi (s) = 1 s2 ( −I l1 − m l 1 ) + −m lcrank bs l1 s2 +mg (B.24) From equation B.23 and B.24 the following expression is obtained for the combined system with regard to all motor parameters. L → (−Ua + La I˙A ) kt Ra (s2 (− Ja G14i (s) 2 + lcrank ml1 − Gi (s)(I2 + lcrank m)) − s( BG14i (s) + Kt Ke Gi (s) )) 14Ra If now we choose a motor where the material parameters La and B is small we can approximate the systems transfer function as the following. G(s) = −kt Ra (s2 (lcrank ml1 2 − Gi (s)(I2 + lcrank m+ 9 Ja 14 )) − sGi (s)Kt Ke ) 14Ra (B.25) = θ1 (s) APPENDIX B. PROOFS B.2 Pendulum Friction Approximation It is important that the mathematical model represent the physical system as good as possible. To do this it is needed to estimate the friction from in this case the bearing and rotary sensor attached to the axis of the pendulum. To approximate the friction the pendulum is dropped from a determined angle and then compared to the mathematical model of the system. The most optimal value of the friction constant is then through experiments chosen when comparing the transients of the model and system. To simplify the system the crank arm was in a fixed position. Now the differential equation for the angle of the pendulum is given by B.26. y mg: I1 θ¨1 = −l1 N sin θ1 − l1 F cos θ1 + b θ˙1 (B.26) Since the crank arm is in a fixed position the force F applied from the crank arm to the pendulum is 0. With the initial conditions the we drop the pendulum from a fixed angle the differential can be solved. −l1 N sin θ1 (t) − b θ˙1 (t) θ¨1 (t) = I1 π θ1 (0) = 8 θ˙1 (0) = 0 θ¨1 (0) = 0 This is a second order differential equation and is solved through Euler method in two stages. The Euler method is a way to approximate an ordinary differential equation and since this is a second order we can formulate two first order differential equations. First the variables are substituted. θ1 (t) = x1 (t) θ˙1 (t) = x2 (t) θ¨1 (t) = x3 (t) Euler method approximates the angle and angle velocity x1 (t + ∆t) ≈ x2 (t)∆t + x1 (t) x2 (t + ∆t) ≈ x3 (t)∆t + x2 (t) and the approximation of the angle acceleration are x3 (t) = −b x2 (t) − l1 N sin (x1 (t)) . I1 Now the angle as a function of time be calculated by step-wise calculating x3 and x2 to approximate the angle x1 given the initial conditions. The angle is then plotted as a function of time for a specific b, a friction value is chosen as the one most corresponding to the actual oscillation of the pendulum [16]. 10 Appendix C References 1. "Design and Implementation of Control System for Inverted Pendulum” [Raheel Tariq,Saad Rasheed,Sajid Ali] avaliable at http://sajidpak.weebly.com/ uploads/1/8/4/7/18473948/final_report_ip-1.pdf 2. Torkel Glad and Lennart Ljung. Reglerteknik grundläggande teori. 4:th edition. Demigraf Poland 2014. Studentlitteratur AB. 3. [Kwakernaak, Huibert and Sivan, Raphael] (1972). Linear Optimal Control Systems. First Edition. Wiley-Interscience. ISBN 0-471-51110-2 4. [Ivan Virgala, Peter Frankovský, Mária Kenderová]American Journal of Mechanical Engineering. 2013, avaliable at http://pubs.sciepub.com/ajme/1/1/1/figure/ 3 5. 24.509 Lecture Notes by Dr. J. R. White, UMass-Lowell (Spring 1997) avaliable at http://www.profjrwhite.com/system_dynamics/sdyn/s6/s6fmathm/s6fmathm. html 6. Datasheet Atmega [2009 Atmel Corporation] avaliable at https://www.sparkfun. com/datasheets/Components/SMD/ATMega328.pdf 7. Secrets of Arduino PWM [Ken Shirriff] avaliable at http://www.arduino.cc/ en/Tutorial/SecretsOfArduinoPWM 8. [MicroMo Electronics, Inc] DC-Micromotors data sheet avaliable at http: //www.me.mtu.edu/~wjendres/ProductRealization1Course/Motor_Specs.pdf 9. DigitalSmooth [Paul Badger](2007) avaliable at http://playground.arduino. cc/Main/DigitalSmooth 10.Arduino reference page avaliable at http://www.arduino.cc/en/Main/ArduinoBoardUno 11.L298N Motor Driver Board reference page avaliable at http://www.geeetech. com/wiki/index.php/L298N_Motor_Driver_Board 12.[Piher] MTS-360 data sheet avaliable at https://www1.elfa.se/data1/wwwroot/ assets/datasheets/MTS360_series_eng_tds.pdf 13.[faulhaber] planatary gearheads data sheet avaliable at https://fmcc.faulhaber. com/resources/img/EN_23-1_FMM.PDF 11 APPENDIX C. REFERENCES 14.F uruta, K., Y amakita, M.andKobayashi, S.(1992)ıSwing−upcontrolof invertedpendulumusingpseud statef eedback, Journalof SystemsandControlEngineering. 15. Hans Johansson.Elektroteknik. 2013 års upplaga. Instutionen för Maskinkonstruktion Mekatronik 2013. KTH 16.Dropbox project:"inverted pendulum"[Filip Stenbeck] (2015) avaliable at https: //www.dropbox.com/sh/dugu1enhohp0mie/AACE_windUtKmPTc8eorxcFJa?dl=0 17.["IVP-BOT", Filip Stenbeck, Aron Nygren](2015) avaliable at https://www. youtube.com/watch?v=wZ82Sgmk4pw 12 TRITA MMK 2015:21 MDAB074 www.kth.se