System Simulation using Matlab

EE4314 Spring 2009 System Simulation using Matlab The purpose of this laboratory work is to provide experience with the Matlab software for system sim...
Author: Alannah Newton
12 downloads 2 Views 310KB Size
EE4314 Spring 2009 System Simulation using Matlab The purpose of this laboratory work is to provide experience with the Matlab software for system simulation. The laboratory work contains a guide for solving the following general problem: Given the dynamics of a system in the form of a set of differential equations, use Matlab to find what the response of the system is, i.e. x(t )ÎR n or y (t )ÎR p , to a given input, u (t )ÎR m , considering certain initial values for the states of the system, x(0) = x0 . I. Simulation of dynamical systems using ode integration in Matlab The first method used to obtain in simulation the response of a system involves the use of the “ode” functions in Matlab. The “ode”-type of functions provide solutions to Ordinary Differential Equations, such as the ones which describe the systems in our consideration. We will restrict our study to the use of the “ode23” Matlab function. To find out more about the “ode” functions type for example “help ode23” in the Matlab command window and read the help file. The first paragraph in the help file describes how the “ode23” function can be called and what parameters it returns: “ODE23 Solve non-stiff differential equations, low order method. [TOUT,YOUT] = ODE23(ODEFUN,TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates the system of differential equations y' = f(t,y) from time T0 to TFINAL with initial conditions Y0. ODEFUN is a function handle. For a scalar T and a vector Y, ODEFUN(T,Y) must return a column vector corresponding to f(t,y). Each row in the solution array YOUT corresponds to a time returned in the column vector TOUT. To obtain solutions at specific times T0,T1,...,TFINAL (all increasing or all decreasing), use TSPAN = [T0 T1 ... TFINAL]. ” The first parameter that the “ode23” function requires, i.e. ODEFUN, is in fact a handle to a Matlab function which describes the dynamics of the system to be simulated, i.e. the differential equations y' = f(t,y). Say for example that we are given the system described by the differential equation: m1a12 q&&1 + m1 ga1 cos(q1 ) =t This is the differential equation which describes the dynamics of a one-link planar rotational stiff robotic arm, in an environment without friction. The q1 is the angle of the robotic arm with respect to the horizontal, t is the torque that is rotating the arm, a1 is the length of the arm, m1 is the mass of the arm and g is the acceleration of gravity. This equation has been obtained using Lagrange’s equation (as discussed in class). The ode23 function can only integrate first order differential equations, and for this reason we will introduce a second state to be able to describe this system with a system of two first order differential equations. Thus, with the notations, x1 = q1 , x2 = q&1 , u =t , we obtain 1

ì x&1 = x2 ï í x& = - g cos( x ) + 1 u . 1 ï 2 a1 m1a12 î Using these two first order differential equations we can now write the Matlab function which describes the dynamics of this system. function xdot=robot(t,x); %the x input variable of the function is in fact a vector containing the values %of the states x1 and x2; x1=x(1) and x2=x(2) g=9.8 ; a1=1 ; m1=10 ; %defines the values for the constant parameters in the system dynamics xdot= [x(2) ; -(g/a1)*cos(x(1))+u/(m1*a1^2)]; %defines a vector which is in fact the derivative of the states x1_dot and x2_dot

Note that there is a variable which we have not defined. That is u, which is the input function. Say for example that we what to simulate the behavior of the system in response to a input torque t = 20*e-0.2t cos(2p t ) , which is a weighted attenuated cosine function with a period of one second. Then in our program we have to insert a line which will calculate the value of the input at each given moment in time. We now have function xdot=robot(t,x); g=9.8 ; a1=1 ; m1=10 ; u=20*exp(-t*0.2)*cos(2*pi*t); % notice that the time is the other input variable in out function xdot= [x(2) ; -(g/a1)*cos(x(1))+u/(m1*a1^2)];

At this point we have set up a function which describes the system dynamics. Save this file with the name “robot.m” in your working folder (you can save it directly in your personal folders). In order to simulate our system, and actually get the system response, we have to use the ode23 function. The second parameter of the ode function is the time interval during which we want to obtain the system response. Say for example that we want to see the system states during 10 seconds. Then we define the simulation time span as [0 10]. The last parameter that the ode function requires is the initial conditions of the system. Say for example that the robotic arm is in position indicated by a pi/2rad angle and 0 rad/sec angular velocity. Then Y0 is [pi/2 ; 0]. The ode23 function will return two matrices, which in the help file are named TOUT and YOUT. The first matrix is in fact the time vector which contains all the time points between 0 and 10 at which the system state, which is YOUT, was measured. In our case since the system has 2 states then YOUT will be a matrix with 2 columns (one for each state) and the same number of lines as the TOUT vector. We can now write a program that will simulate our system. (this is a separate file that the file that you created for the robot dynamics) 2

function main clear all; % clears the workspace close all; % closes all open figures clc; % clears the command window % these three commands are not required for your program to work tint= [0 10] ; % defines the time interval [t0 tf], we will simulate % the system for 10 seconds x0= [pi/2 0]' ; % initial conditions [t,x]= ode23(‘robot’, tint, x0); plot(t,x); % plots the system states versus time

But this type of plot does not give much information to an untrained eye. To get a nicer visual result one can add the following to the main program a1=1; x1=cos(x(:,1))*a1; y1=sin(x(:,1))*a1; figure(3); for i=1:length(x1), figure(3); plot(0,0,'o',x1(i),y1(i),'o',[0 x1(i)],[0 y1(i)],'-r'); axis([-1.2 1.2 -1.2 1.2]); pause(t(i+1)-t(i)); end

The answers to the following questions should be turned in at the end of the class. Use the provided page at the end of this document. A. Explain, in the provided space on the last page, what is the effect of each of the code lines in the above program section. B. Make a simulation of the system considering 0 input (u(t)=0) and initial conditions given as [0 0] (which is: zero angle and zero angular velocity). Explain the behavior of the system. II. Simulation of dynamical systems using block diagrams in Matlab Simulink To simulate the dynamics of the same one link robot system one can also use the Simulink environment and the block diagram description of the system. Start by typing “simulink” in the Matlab command window. The Simulink library browser opens. Create a new simulink file. Start by inserting as many integrators as the number of states of the system. The simulink file will look like the one in Figure 1 (as the system has two states).

3

Figure 1 The output of the first integrator is x1 and the output of the second integrator is x2 . Knowing that x&1 = x2 , one can simply add the connection line between the input of the first integrator and the output of the second one (as presented in Figure 2)

Figure 2 The input of the second integrator is given by the relation x&2 = -

g 1 cos( x1 ) + u. a1 m1a12

Note that it depends

on the input signal u and the output of the first integrator. First we will group these two signals x1 , u in a vector of signals [ x1 u ] . For this you can use the “Mux” block which can be found in the “signal routing library” in Simulink (as per Figure 3). This new vector of signals will serve as input to a block which will have to calculate the value of x&2 according to the equation x&2 = -

g 1 cos( x1 ) + u. a1 m1a12

You can find such a block in the “user defined

functions” tab of the simulink library. You now obtain the schematic in Figure 4.

4

Figure 3

Figure 4 To introduce the function that the “Fcn” block has to perform you can double-click on the “Fcn” block and insert the appropriate function in the window that appeared.

Figure 5 In this case the relation to be inserted is (-9.8/1)*cos(u(1))+(1/(10*1^2))*u(2). In this relation u(1) carries the information on x1(t) and u(2) carries the information on u(t). We now need to define our input signal u (t ) . One can define this input signal in many different ways using a number of combinations of Simulink blocks. One of them is given in Figure 6, where the function block denoted “Fcn1” performs the following calculation depending on its input 20*exp(-0.2*u(1))*cos(2*pi*u(1)). 5

Figure 6 This simulink schematic for simulating our robotic system is now complete. We only need to visualize the result. We thus insert in the schematic a “Scope” block which will present the two states.

Figure 7 You can now run the simulation by hitting the “play” button in the menu. It will simulate the system behavior for 10 seconds. If you double-click on the scope block, a window will appear which will present the evolution of the two states of the system; just like the following line of Matlab code did. plot(t,x); % plots the system states versus time

You can now save this data from Simulink and use it for plot in Matlab. If you hit the parameters button (second button) in the scope menu the following window will appear (which has 2 tabs).

Figure 8 6

In the “data history” tab select the “save data to workspace” tick. In this case, after you run the simulation one more time, in the Matlab workspace will appear a data structure called ScopeData. To access the time vector you use the name “ScopeData.time” and to access the values of the states you use the name “ScopeData.signals.values”. If now you run the following code you will plot the result, which was presented in the Scope, in a Matlab figure. x=ScopeData.signals.values; t=ScopeData.time; plot(t,x); Make a simulation of the system considering 0 input (u(t)=0) and initial conditions given as [0 0] (which is: zero angle and zero angular velocity). To introduce the initial states in the simulink block diagram, double-click the two integrators and introduce in the appropriate field the values for the initial states. Matlab questions A. Describe the effect of each of the lines of code in this program. function main tint= [0 10] ; % defines the time interval [t0 tf], we will simulate % the system for 10 seconds x0= [pi/2 0]' ; % initial conditions [t,x]= ode23(‘robot’, tint, x0); plot(t,x); % plots the system states versus time a1=3; x1=cos(x(:,1))*a1; y1=sin(x(:,1))*a1; figure(3); for i=1:length(x1), figure(3); plot(0,0,'o',x1(i),y1(i),'o',[0 x1(i)],[0 y1(i)],'-r'); axis([-1.2 1.2 -1.2 1.2]); pause(t(i+1)-t(i)); end

B. Based on the above simulation, explain the behavior of the one link robotic system in response to 0 input (u(t)=0) and initial conditions given as [0 0] (which is: zero angle and zero angular velocity).

7

Suggest Documents