Servomechanism Controller - MATLAB & Simulink - MathWorks India
Servomechanism Controller - MATLAB & Simulink - MathWorks India
Servomechanism Controller
On this page
System Model Control Objectives and Constraints Defining the Plant Model Controller Design Using MPCTOOL Using Model Predictive Control Toolbox Commands Using MPC Tools in Simulink
System Model
A position servomechanism consists of a DC motor, gearbox, elastic shaft, and a load. Position Servom echanism Schem atic
is the motor
shaft's angular velocity, and the other symbols represent constant parameters (see Parameters Used in the Servomechanism Model for more information on these). If you define the state variables as form: , then you can convert the above model to an LTI state-space
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
1/18
3/16/2014
k kT JM JL M L R
Torsional rigidity Motor constant Motor inertia Load inertia Gear ratio Motor viscous friction coefficient Load viscous friction coefficient Armature resistance
3/16/2014
t a u a m = 5 0 * 1 e 6 ; M m o t o r = 1 0 0 ; R m o t o r = . 1 ; J m o t o r = . 5 * M m o t o r * R m o t o r ^ 2 ; m o m e n to fi n e r t i a B m o t o r = 0 . 1 ; C A S O ) R = 2 0 ; K t = 1 0 ; g e a r = 2 0 ; J l o a d = 5 0 * J m o t o r ; B l o a d = 2 5 ; I p = p i / 3 2 * d s h a f t ^ 4 ; o fs h a f t( c i r c u l a r )s e c t i o n K t h = G * I p / L s h a f t ; r i g i d i t y( T o r q u e / a n g l e ) V s h a f t = p i * ( d s h a f t ^ 2 ) / 4 * L s h a f t ; v o l u m e M s h a f t = s h a f t r h o * V s h a f t ; m a s s J s h a f t = M s h a f t * . 5 * ( d s h a f t ^ 2 / 4 ) ; m o m e n to fi n e r t i a J M = J m o t o r ; J L = J l o a d + J s h a f t ; V m a x = t a u a m * p i * d s h a f t ^ 3 / 1 6 ; a d m i s s i b l et o r q u e V m i n = V m a x ;
% S h e a rs t r e n g t h % R o t o rm a s s % R o t o rr a d i u s % R o t o ra x i a l % R o t o rv i s c o u sf r i c t i o nc o e f f i c i e n t( A % R e s i s t a n c eo fa r m a t u r e % M o t o rc o n s t a n t % G e a rr a t i o % L o a di n e r t i a % L o a dv i s c o u sf r i c t i o nc o e f f i c i e n t % P o l a rm o m e n t u m % T o r s i o n a l % S h a f t % S h a f t % S h a f t
% M a x i m u m
% I n p u t / S t a t e / O u t p u tc o n t i n u o u st i m ef o r m % A A = [ 0 1 0 0 ; K t h / J L B l o a d / J L K t h / ( g e a r * J L ) 0 ; 0 0 0 1 ; K t h / ( J M * g e a r )0 K t h / ( J M * g e a r ^ 2 ) ( B m o t o r + K t ^ 2 / R ) / J M ] ; B B = [ 0 ; 0 ; 0 ; K t / ( R * J M ) ] ; H y d = [ 1000 ] ; H v d = [ K t h0K t h / g e a r0 ] ; D y d = 0 ; D v d = 0 ; %D e f i n et h eL T Is t a t e s p a c em o d e l s y s = s s ( A A , B B , [ H y d ; H v d ] , [ D y d ; D v d ] ) ;
3/16/2014
selected model's key attributes. Im port Dialog Box w ith the Servom echanism Model Selected
Make sure your servomechanism model, s y s , is selected. Then click the Im port button. You w on't be importing more models, so close the import dialog box. Meanw hile, the model has loaded, and tables now appear in the design tool's main w indow (see the figure below ). Note the previous diagram enumerates the model's input and output signals. Design Tool After Im porting the Plant Model
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
4/18
3/16/2014
Specifying Signal Properties It's essential to specify signal types before going on. By default, the design tool assumes all plant inputs are manipulated, w hich is correct in this case. But it also assumes all outputs are measured, w hich is not. Specify that the second output is unmeasured by clicking on the appropriate table cell and selecting the Unm easured option. You also have the option to change the default signal names ( I n 1 ,O u t 1 ,O u t 2 ) to something more meaningful (e.g., V ,T h e t a L , T ), enter descriptive information in the blank Description and Units columns, and specify a nominal initial value for each signal (the default is zero). After you've entered all your changes, the design tool resembles the follow ing figure. Design Tool After Specifying Signal Properties
Navigation Using the Tree View Now consider the design tool's left-hand frame. This tree is an ordered arrangement of nodes . Selecting (clicking) a node causes the corresponding view to appear in the right-hand frame. When the design tool starts, it creates a root node named MPC Design Task and selects it, as in Design Tool After Importing the Plant Model. The Plant m odels node is next in the hierarchy. Click on it to list the plant models being used in your design. (Each model name is editable.) The middle section displays the selected model's properties. There is also a space to enter notes describing the model's special features. Buttons allow you to import a new model or delete one you no longer need. The next node is Controllers . You might see a + sign to its left, indicating that it contains subnodes. If so, click on the + sign to expand the tree (as show n in Design Tool After Importing the Plant Model). All the controllers in your design w ill appear here. By default, you have one: MPC1. In general, you might opt to design and test several alternatives. Select Controllers to see a list of all controllers, similar to the Plant m odels view . The table columns show important controller settings: the plant model being used, the controller sampling period, and the prediction and control horizons. All are editable. For now , leave them at their default values. The buttons on the Controllers view allow you to: Im port a controller designed previously and stored either in your w orkspace or in a MAT-file. Export the selected controller to your w orkspace. Create a New controller, w hich w ill be initialized to the Model Predictive Control Toolbox defaults. Copy the selected controller to create a duplicate that you can modify. Delete the selected controller. Specifying Controller Properties Select the MPC1 subnode. The main pane should change to the controller design. If the selected Prediction m odel is continuous-time, as in this example, the Control interval (sampling period) defaults to 1. You need to change this to an application-appropriate value. Set it to 0.1 seconds (as show n in Controller Design View , Models and Horizons Pane). Leave the other values at their defaults for now . Controller Design View , Models and Horizons Pane
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
5/18
3/16/2014
Specifying Constraints Next, click the Constraints tab. The view show n in Controller Design View , Constraints Pane appears. Enter the appropriate constraint values. Leaving a field blank implies that there is no constraint. Controller Design View , Constraints Pane
In general, it's good practice to include all know n manipulated variable constraints, but it's unw ise to enter constraints on outputs unless they are an essential aspect of your application. The limit on applied torque is such a constraint, as are the limits on applied voltage. The angular position has physical limits but the controller shouldn't attempt to enforce them, so you should leave the corresponding fields blank (see Controller Design View , Constraints Pane). The Max dow n rate should be nonpositive (or blank). It limits the amount a manipulated variable can decrease in a single control interval. Similarly, the Max up rate should be nonnegative. It limits the increasing rate. Leave both unconstrained (i.e., blank). The shaded columns can't be edited. If you w ant to change this descriptive information, select the root node view and edit its tables. Such changes apply to all controllers in the design. Weight Tuning Next, click the Weight Tuning tab. The weights specify trade-offs in the controller design. First consider the Output w eights . The controller w ill try to minimize the deviation of each output from its setpoint or reference value. For each sampling instant in the prediction horizon, the controller multiplies predicted deviations for each output by the output's w eight, squares the result, and sums over all sampling instants and all outputs. One of the controller's objectives is to minimize this sum, i.e., to provide good setpoint tracking. (See Optimization
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html 6/18
3/16/2014
Problem for more details.) Here, the angular position should track its setpoint, but the applied torque can vary, provided that it stays w ithin the specified constraints. Therefore, set the torque's w eight to zero, w hich tells the controller that setpoint tracking is unnecessary for this output. Similarly, it's acceptable for the applied voltage to deviate from nominal (it must in order to change the angular position!). Its w eight should be zero (the default for manipulated variables). On the other hand, it's probably undesirable for the controller to make drastic changes in the applied voltage. The Rate w eight penalizes such changes. Use the default, 0.1. When setting the rates, the relative magnitudes are more important than the absolute values, and you must account for differences in the measurement scales of each variable. For example, if a deviation of 0.1 units in variable A is just as important as a deviation of 100 units in variable B, variable A's w eight must be 1000 times larger than that for variable B. Controller Design View , Weight Tuning Pane
The tables allow you to w eight individual variables. The slider at the top adjusts an overall trade-off betw een controller aggressiveness and setpoint tracking. Moving the slider to the left places a larger overall penalty on manipulated variable changes, making them smaller. This usually increases controller robustness, but setpoint tracking becomes more sluggish. The Estim ation (Advanced) tab allow s you to adjust the controller's response to unmeasured disturbances (not used in this example). Defining a Sim ulation Scenario If you haven't already done so, expand the Scenarios node to show the Scenario1 subnode (see Design Tool After Importing the Plant Model). Select Scenario1. A scenario is a set of simulation conditions. As show n in Simulation Settings View for "Scenario1", you choose the controller to be used (from among controllers in your design), the model to act as the plant, and the simulation duration. You must also specify all setpoints and disturbance inputs. Duplicate the settings show n in Simulation Settings View for "Scenario1", w hich w ill test the controller's servo response to a unitstep change in the angular position setpoint. All other inputs are being held constant at their nominal values. Sim ulation Settings View for "Scenario1"
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
7/18
3/16/2014
Note The ThetaL and V unmeasured disturbances allow you to simulate additive disturbances to these variables. By default, these disturbances are turned off, i.e., zero. The Look ahead option designates that all future setpoint variations are know n. In that case, the controller can adjust the manipulated variable(s) in advance to improve setpoint tracking. This w ould be unusual in practice, and is not being used here. Running a Sim ulation Once you're ready to run the scenario, click the Sim ulate button or the green arrow on the toolbar. Note The green arrow tool is available from any view once you've defined at least one scenario. It runs the active scenario, i.e., the one most recently selected or modified. We obtain the results show n in Response to Unit Step in the Angular Position Setpoint. The blue curves are the output signals, and the gray curves are the corresponding setpoints. The response is very sluggish, and hasn't settled w ithin the 30-second simulation period. Response to Unit Step in the Angular Position Setpoint
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
8/18
3/16/2014
Note The w indow show n in Response to Unit Step in the Angular Position Setpoint provides many of the customization features available in Control System Toolbox l t i v i e wand s i s o t o o ldisplays. Try clicking a curve to obtain the
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html 9/18
3/16/2014
numerical characteristics of the selected point, or right-clicking in the plot area to open a customization menu. The corresponding applied voltage adjustments appear in a separate w indow and are also very sluggish. On the positive side, the applied torque stays w ell w ithin bounds, as does the applied voltage. Retuning to Achieve a Faster Servo Response To obtain a more rapid servo response, navigate to the MPC1 Weight Tuning pane (select the MPC1 node to get the controller design view , then click the Weight Tuning tab) and move the slider all the w ay to the right. Then click the green arrow in the toolbar. Your results should now resemble Faster Servo Response and Manipulated Variable Adjustments. The angular position now settles w ithin 10 seconds follow ing the step. The torque approaches its low er limit, but doesn't exceed it (see Faster Servo Response) and the applied voltage stays w ithin its limits (see Manipulated Variable Adjustments). Faster Servo Response
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
10/18
3/16/2014
Modifying the Scenario Finally, increase the step size to radians (select the Scenario1 node and edit the tabular value). As show n in Servo Response for Step Increase of Radians and Voltage Adjustments, the servo response is essentially as good as before, and w e avoid exceeding the torque constraint at 78.5 Nm, even though the applied voltage is saturated for about 2.5 seconds (see Voltage Adjustments). Servo Response for Step Increase of Radians
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
11/18
3/16/2014
3/16/2014
Once you're satisfied w ith a controller's performance, you can export it to the w orkspace, for use in a Simulink block diagram or for analysis (or you can save it in a MAT-file). To export a controller, right-click its node and select Export from the resulting menu (or select the Controllers node, select the controller in the list, and click the Export button). A dialog box like that show n in Exporting a Controller to the Workspace w ill appear. The Controller source is the design from w hich you w ant to extract a controller. There's only one in this example, but in general you might be w orking on several simultaneously. The Controller to export choice defaults to the controller most recently selected. Again, there's no choice in this case, but there could be in general. The Nam e to assign edit box allow s you to rename the exported controller. (This w ill not change its name in the design tool.) Exporting a Controller to the Workspace
Note When you exit the design tool, you w ill be prompted to save the entire design in a MAT file. This allow s you to reload it later using the File/Load menu option or the Load icon on the toolbar.
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
13/18
3/16/2014
Like the LTI objects used to define linear, time-invariant dynamic models, an MPC object contains a complete definition of a controller. Setting, Getting, and Displaying Object Properties Once you've constructed an MPC object, you can change its properties as you w ould for other objects. For example, to change the prediction horizon, you could use one of the follow ing commands: S e r v o M P C . P r e d i c t i o n H o r i z o n=1 2 ; s e t ( S e r v o M P C ,' P r e d i c t i o n H o r i z o n ' ,1 2 ) ; For a listing of all the object's properties, you could type: g e t ( S e r v o M P C ) To access a particular property (e.g., the control horizon), you could type either: M=g e t ( S e r v o M P C ,' C o n t r o l H o r i z o n ' ) ; M=S e r v o M P C . C o n t r o l H o r i z o n ; You can also set multiple properties simultaneously. Set the follow ing properties before continuing w ith this example: s e t ( S e r v o M P C ,' W e i g h t s ' ,W e i g h t s ,. . . ' M a n i p u l a t e d V a r i a b l e s ' ,M a n i p u l a t e d V a r i a b l e s ,. . . ' O u t p u t V a r i a b l e s ' ,O u t p u t V a r i a b l e s ) ; Typing the name of an object w ithout a terminating semicolon generates a formatted display of the object's properties. You can achieve the same effect using the display command: d i s p l a y ( S e r v o M P C ) Running a Sim ulation The s i mcommand performs a linear simulation. For example, the follow ing code sequence defines constant setpoints for the tw o outputs, then runs a simulation: T i m e S t e p s=r o u n d ( 1 0 / T s ) ; r=[ p i0 ] ; [ y ,t ,u ]=s i m ( S e r v o M P C ,T i m e S t e p s ,r ) ; By default, the model used to design the controller (stored in ServoMPC) also represents the plant. The s i mcommand saves the output and manipulated variable sequences in variables yand u . For example, s u b p l o t ( 3 1 1 ) p l o t ( t ,y ( : , 1 ) ,[ 0t ( e n d ) ] ,p i * [ 11 ] ) t i t l e ( ' A n g u l a rP o s i t i o n( r a d i a n s ) ' ) ; s u b p l o t ( 3 1 2 ) p l o t ( t ,y ( : , 2 ) ,[ 0t ( e n d ) ] ,[ 7 8 . 57 8 . 5 ] ) t i t l e ( ' T o r q u e( n M ) ' ) s u b p l o t ( 3 1 3 ) s t a i r s ( t ,u ) t i t l e ( ' A p p l i e dV o l t a g e( v o l t s ) ' ) x l a b e l ( ' E l a p s e dT i m e( s e c o n d s ) ' ) produces the custom plot show n in Plotting the Output of the sim Command. The plot includes the angular position's setpoint. The servo response settles w ithin 5 seconds w ith no overshoot. It also displays the torque's low er bound, w hich becomes active after about 0.9 seconds but isn't exceeded. The applied voltage saturates betw een about 0.5 and 2.8 seconds, but the controller performs w ell despite this. Plotting the Output of the sim Com m and
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
14/18
3/16/2014
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
15/18
3/16/2014
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
16/18
3/16/2014
The key features of the diagram are as follow s: The MPC Controller output is the plant input. The Voltage Scope block plots it (yellow curve). Minimum and maximum voltage values are show n as magenta and cyan curves. The plant output is a vector signal. The first element is the measured angular position. The second is the unmeasured torque. A Demux block separates them. The angular position feeds back to the controller and plots on the Angle scope (yellow curve). The torque plots on the Torque scope (w ith its low er and upper bounds). The position setpoint varies sinusoidally w ith amplitude radians and frequency 0.4 rad/s. It also appears on the Angle scope (magenta curve).
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
17/18
3/16/2014
The angular position tracks the sinusoidal setpoint variations w ell despite saturation of the applied voltage. The setpoint variations are more gradual than the step changes used previously, so the torque stays w ell w ithin its bounds.
http://www.mathworks.in/help/mpc/ug/servomechanism-controller.html
18/18