Matlab and Simulink for Modeling and Control Robert Babuška and Stefano Stramigioli November 999 Delft Delft Univerity of Technology Control Laboratory Faculty of Information Technology and Sytem Delft Univerity of Technology P.O. Box 53, 26 GA Delft, The Netherland
Introduction With the help of two example, a DC motor and a magnetic levitation ytem, the ue of MATLAB and Simulink for modeling, analyi and control deign i demontrated. It i aumed that the reader already ha baic knowledge of MATLAB and Simulink. The main focu i on the ue of the Control Sytem Toolbox function. We recommend the reader to try the command out directly in MATLAB while reading thi text. The example have been implemented by the author and can be downloaded from http://lcewww.et.tudelft.nl/ et492. The implementation i done in MATLAB verion 5.3 and ha alo been teted in verion 5.2. 2 Modeling a DC Motor In thi example we will learn how to develop a linear model for a DC motor, how to analyze the model under MATLAB (pole and zero, frequency repone, time-domain repone, etc.), how to deign a controller, and how to imulate the open-loop and cloed-loop ytem under SIMULINK. 2. Phyical Sytem Conider a DC motor, whoe electric circuit of the armature and the free body diagram of the rotor are hown in Figure. R L V + V b = Kω + T J - - bω Figure : Schematic repreentation of the conidered DC motor. The rotor and the haft are aumed to be rigid. Conider the following value for the phyical parameter: moment of inertia of the rotor J =. kg m 2 damping (friction) of the mechanical ytem b =. Nm (back-)electromotive force contant K =. Nm/A electric reitance R = Ω electric inductance L =.5 H The input i the armature voltage V in Volt (driven by a voltage ource). Meaured variable are the angular velocity of the haft ω in radian per econd, and the haft angle θ in radian. 2.2 Sytem Equation The motor torque, T, i related to the armature current, i, by a contant factor K: The back electromotive force (emf), V b, i related to the angular velocity by: T = Ki. () V b = Kω = K dθ dt. (2)
From Figure we can write the following equation baed on the Newton law combined with the Kirchhoff law: 2.3 Tranfer Function Uing the Laplace tranform, equation (3) and (4) can be written a: J d2 θ dt 2 + bdθ dt = Ki, (3) L di + Ri dt = dθ V K dt. (4) J 2 θ()+bθ() = KI(), (5) LI()+RI() = V () Kθ(), (6) where denote the Laplace operator. From (6) we can expre I(): and ubtitute it in (5) to obtain: I() = V () Kθ(), (7) R + L J 2 V () Kθ() θ()+bθ() =K. (8) R + L Thi equation for the DC motor i hown in the block diagram in Figure 2. Voltage V () Armature K Torque T () Load Velocity Angle L+R J+b V b( ) Back emf K Figure 2: A block diagram of the DC motor. From equation (8), the tranfer function from the input voltage, V (), to the output angle, θ, directly follow: G a () = θ() V () = K [(R + L)(J + b)+k 2 ]. (9) From the block diagram in Figure 2, it i eay to ee that the tranfer function from the input voltage, V (),to the angular velocity, ω, i: G v () = ω() V () = K (R + L)(J + b)+k 2. () 3 MATLAB Repreentation The above tranfer function can be entered into Matlab by defining the numerator and denominator polynomial, uing the convention of the MATLAB Control Toolbox. The coefficient of a polynomial in are 2
entered in a decending order of the power of. Example: The polynomial A =3 3 +2 +i in MATLAB entered a: A = [3 2 ]. Furthermore, we will make ue of the function conv(a,b), which compute the product (convolution) of the polynomial A and B. Open the M-file motor.m. It already contain the definition of the motor contant: J=.; b=.; K=.; R=; L=.5; The tranfer function (9) can be entered in MATLAB in a number of different way.. A G a () can be expreed a G v (), we can enter thee two tranfer function eparately and combine them in erie: aux Gv Ga = tf(k,conv([l R],[J b])) = feedback(aux,k); = tf(,[ ])*Gv; Here, we made ue of the function feedback to create a feedback connection of two tranfer function and the multiplication operator *, which i overloaded by the LTI cla of the Control Sytem Toolbox uch that i compute the product of two tranfer function. 2. Intead of uing convolution, the firt of the above three command can be replaced by the product of two tranfer function: aux = tf(k,[l R])*tf(,[J b]); 3. Another poibility (perhap the mot convenient one) i to define the tranfer function in a ymbolic way. Firt introduce a ytem repreenting the Laplace operator (differentiator) and then enter the tranfer function a an algebraic expreion: = tf([ ],); Gv = K/((L* + R)*(J* + b) + Kˆ2); Ga = Gv/; It i convenient to label the input and output by the name of the phyical variable they repreent: Gv.InputName = Voltage ; Gv.OutputName = Velocity ; Ga.InputName = Voltage ; Ga.OutputName = Angle ; Now by calling motor from the workpace, we have both the velocity (Gv) and the poition (Ga) tranfer function defined in the workpace. 3. Exercie. Convert Gv and Ga into their repective tate-pace (function ) and zero-pole-gain (function zpk) repreentation. 2. What are the pole and zero of the ytem? I the ytem table? Why? 3. How can you ue MATLAB to find out whether the ytem i obervable and controllable? 3
4 Analyi The Control Sytem Toolbox offer a variety of function that allow u to examine the ytem characteritic. 4. Time-Domain and Frequency Repone A we may want plot the repone for the velocity and angle in one figure, it convenient to group the two tranfer function into a ingle ytem with one input, the voltage, and two output, the velocity and the angle: G = [Gv; Ga]; Another way i to firt convert G a into it tate-pace repreentation and then add one extra output being equal to the econd tate (the velocity): G = (Ga); et(g, c,[ ; ], d,[;], OutputName,{ Velocity ; Angle }); Note that thi extenion of the tate-pace model with an extra output ha to be done in one et command in order to keep the dimenion conitent. Now, we can plot the tep, impule and frequency repone of the motor model: figure(); tep(g); figure(2); impule(g); figure(3); bode(g); You hould get the plot given in Figure 3 and Figure 4. Step Repone Impule Repone. From: Voltage.2 From: Voltage.8.5 To: Velocity.6.4 To: Velocity..2.5 Amplitude.25 Amplitude..2.8 To: Angle.5. To: Angle.6.4.5.2.5.5 2 2.5 3.5.5 2 2.5 3 Time (ec.) Time (ec.) Figure 3: Step and impule repone. 4.2 Exercie. Simulate and plot in MATLAB the time repone of the velocity and of the angle for an input ignal co 2πt,wheret goe from to 5 econd. 4
Bode Diagram From: Voltage 5 Phae (deg); Magnitude (db) To: Velocity 2 2 2 To: Angle 2 5 Control Deign 4 2 2 Frequency (rad/ec) Figure 4: Bode diagram. Let u deign a PID feedback controller to control the velocity of the DC motor. Recall that the tranfer function of a PID controller i: C() = U() E() = K p + K i + K d = K d 2 + K p + K i, () where u i the controller output (in our cae the voltage V ), e = u c y i the controller input (the control error), and K p, K d, K i are the proportional, derivative and integral gain, repectively. A block diagram of the cloed-loop ytem i given in Figure 5. r + e PID Voltage V DC Motor Velocity ω Figure 5: Cloed-loop ytem with a PID controller. 5. Proportional Control Firt, try a imple proportional controller with ome etimated gain, ay,. To compute the cloed-loop tranfer function, ue the feedback command. Add the following line to your m-file: Kp = ; Gc = feedback(gv*kp,); Gc.InputName = Deired velocity ; 5
Here Gc i the cloed-loop tranfer function. To ee the tep repone of the cloed-loop ytem, enter: figure(4); tep(gc,:.:2); You hould get the plot given in Figure 6:.4 Step Repone From: Deired velocity.2 Amplitude To: Velocity.8.6.4.2.2.4.6.8.2.4.6.8 2 Time (ec.) Figure 6: Cloed-loop tep repone with a P controller. To eliminate the teady-tate error, an integral action mut be ued. To reduce the overhoot, a derivative action can be employed. In the following ection, a complete PID controller i deigned. 5.2 PID Control Let u try a PID controller. Edit your M-file o that it contain the following command: Kp = ; Ki =.8; Kd =.3; C = tf([kd Kp Ki],[ ]); rlocu(ga*c); Kp = rlocfind(ga*c); Gc = feedback(ga*c*kp,); figure(9); tep(gc,:.:5) The rlocu and rlocfind function are ued to elect the overall gain of the PID controller, uch that the controller i table and ha the deired location of the pole (within the defined ratio among the K p, K i and K d contant). If the deign i not atifactory, thi ratio can be changed, of coure. We hould obtain a plot imilar to the one in Figure 7: 5.3 Exercie. Ue the root locu and the Nyquit criterion to find out for what value of the gain K p the proportional controller for the angle G a () become untable. 6
Step Repone.4.2 Amplitude To: Angle.8.6.4.2.5.5 2 2.5 3 3.5 4 4.5 5 Time (ec.) 6 SIMULINK Model Figure 7: Cloed-loop tep repone with a PID controller. The block diagram from Figure 2 can be directly implemented in SIMULINK, a hown in the figure Figure 8: Step Input + Armature K() L+R Load J+b angle K theta To Workpace Clock To Workpace t angular peed omega To Workpace Figure 8: SIMULINK block diagram of the DC motor. Set the imulation parameter and run the imulation to ee the tep repone. Compare with the repone in Figure 3. Save the file under a new name and remove the poition integrator along with the Graph and To Workpace block. Group the block decribing the DC motor into a ingle block and add a PID controller according to Figure 5. The correponding SIMULINK diagram i given in Figure 9. Experiment with the controller. Compare the repone with thoe obtained previouly in MATLAB. 7 Obtaining MATLAB Repreentation from a SIMULINK Model From a SIMULINK diagram, a MATLAB repreentation (tate pace, tranfer function, etc.) can be obtained. The Inport and Outport block mut be added to the SIMULINK diagram, a hown in Figure. Then we can ue the linmod command to obtain a tate-pace repreentation of the diagram: [A,B,C,D] = linmod( filename ); where filename i the name of the SIMULINK file. 7
Step Input + Sum PID DC motor angular peed omega t To Workpace Clock To Workpace Figure 9: SIMULINK block diagram of the DC motor with a PID controller. Armature Load Inport + K() L+R J+b Outport K Figure : SIMULINK block diagram of the DC motor with Inport and Outport block. 7. Exercie. Convert the four matrice A, B C and D into a correponding tate pace LTI object. Convert thi one into a tranfer function and compare the reult with the tranfer function entered previouly in MATLAB. 8 Linearization of Nonlinear Simulink Model In thi ection, we will ue an example of a highly nonlinear ytem to how how to linearize a nonlinear Simulink model and extract the linearized model to MATLAB for control deign purpoe. 8. Magnetic Levitation Sytem Magnetic levitation a a friction-le upport for high-peed train, in bearing of low-energy motor, etc. It conit of an electromagnet which i attracted to an object made of a magnetic material (uch a a rail). The control goal i to keep the air gap between thi material and the electromagnet contant by controlling the current in the coil. A chematic drawing i given in Figure. Rail F R Airgap A-D z Computer D-A Current i F grav F dit Figure : Schematic drawing of the magnetic levitation ytem. The poition and the motion of the object in the magnetic field are dependent on the force that act on it. Thee force are: (i) the gravitational force, (ii) the electromagnetic force, and (iii) a diturbance force. The 8
dynamic equation of the ytem i derived from the baic law F = ma, d 2 dt 2 y(t) = m (F grav + F dit F R ), (2) where F grav = mg, F dit i an unknown diturbance, and the electromagnetic force i F R = µ N 2 Ai 2 (t) 2y 2 (t) = K mag i 2 (t) y 2 (t). (3) In our example, we ue K mag =7.8 µh and m =8kg. 8.2 Nonlinear Simulink Model The nonlinear equation (2) i implemented in a Simulink model given in Figure 2. /u^2 Current 2 Diturbance force u 2 /m Kmag/m g y y Check limit Air gap Figure 2: Nonlinear Simulink model (bearing.mdl) of the magnetic levitation ytem. 8.3 Linearization Let u linearize the nonlinear model around an operating point y =2mm. There are two poibilitie to linearize a nonlinear model: Analytically: by hand or uing ymbolic math oftware uch a Mathematica, Maple or the Symbolic Toolbox of MATLAB. Numerically by applying the trim and linmod function of MATLAB. The econd poibility will be explored here (you can do the firt one a an exercie). Let u ue the following cript (lin.m): param; % a cript with definition of ytem parameter file = bearing ; % nonlinear Simulink model to be linearized u = [; ]; % initial input gue [input; diturbance] y =.2; % initial output gue x = [y ] ; % initial tate gue [x,u]=trim(file,x,u,y,[],[2],[]); [A,B,C,D] = linmod(file,x,u); y = (A,B,C,D); % make an LTI object 9
The trim function numerically earche for an equilibrium of the nonlinear ytem. A reaonable initial gue (x, u and y) mut be provided. The additional parameter of thi function are indice of the input, tate and output that are not free to vary during the earch. A typical example of uch a variable i the tate variable correponding to the operating point. The linmod function extract the matrice of a linear model obtained by numerical linearization at the equilibrium. Once thi model i available, it can be ued for analyi or control deign. 8.4 Exercie. Chooe another operating point and extract a linear model at that point. Compare to the model obtained above in term their gain, pole and zero. 9 Concluding Remark The author hope that thi text ha been ueful and would appreciate receiving feedback from you. Let u know if you have found any error and omiion or if you have uggetion for improvement. Send them preferable by e-mail to: R.Babuka@ITS.TUDelft.NL.