Tune PID Controller from Measured Plant Data in the Live Editor
This example shows how to use Live Editor tasks to tune a PID controller for a plant, starting from the measured plant response to a known input signal. In this example, you use theEstimate State-Space Modelfo任务生成代码r estimating a parametric plant model. Then, you use theConvert Model Ratetask to discretize the continuous-time identified model. Finally, you use theTune PID Controllertask to design a PID controller to achieve a closed-loop response that meets your design requirements. (UsingEstimate State-Space Modelrequires a System Identification Toolbox™ license.)
Live Editor tasks let you interactively iterate on parameters and settings while observing their effects on the result of your computation. The tasks then automatically generate MATLAB® code that achieves the displayed results. To experiment with the Live Editor tasks in this script, open this example. For more information about Live Editor Tasks generally, seeAdd Interactive Tasks to a Live Script.
Load Plant Data
Load the measured input-output data. In this example, the data consists of the response of an engine to a chirp input. The inputu
is a vector containing the input signal sampled every 0.04 seconds. The output vectory
contains the corresponding measured response.
loadIdentPlantPIDExampleuyt = 0.04*(0:length(u)-1); plot(t,u,t,y) legend('input u','response y')
Estimate State-Space Model
To estimate a state-space model from this data, use theEstimate State-Space Model(System Identification Toolbox)Live Editor task. You can insert a task into your script using theTaskmenu in the Live Editor. In this script,Estimate State-Space Modelis already inserted. Open the example to experiment with the task.
To perform the estimation, in the task, specify the input and output signals you loaded,u
andy
, and the sample time, 0.04 seconds. (For this example, you do not have validation data.) You also need to specify a plant order. Typically, you can guess the plant order based upon your knowledge of your system. In general, you want to use the lowest plant order that gives a reasonably good estimation fit. In theEstimate State-Space Modeltask, experiment with different plant order values and observe the fit result, displayed in the output plot. For details about the available options and parameters, see theEstimate State-Space Model(System Identification Toolbox)task reference page.
As you vary parameters in the task, it automatically updates the generated code for performing the estimation and creating the plot. (To see the generated code, clickat the bottom of the task.)
% Create object for time series dataestimationData = iddata(y,u,0.04);% Estimate state-space modelsys_id = ssest(estimationData,4);% Display resultscompare(estimationData,sys_id); clearestimationData; title('Estimation data:');
For this example, at plant order 4, the estimation fit is about 72%. Try increasing the plant order to see that doing so does not improve the fit much. Therefore, use the fourth-order plant. The code produces an identified state-space model with the variable name that you type into the summary line of theEstimate State-Space Modeltask. For this example, usesys_id
. After you finish experimenting with the task, the identified state-space modelsys_id
is in the MATLAB® workspace, and you can use it for additional design and analysis in the same way you use any other LTI model object. For instance, examine the frequency response of the identified state-space modelsys_id
.
bode(sys_id) gridon
Discretize Model
Suppose that you want to discretize this model before you design a PID controller for it. To do so, use theConvert Model Ratetask. In the task, select the identified modelsys_id
. Specify a sample time fast enough to accommodate the resonance in the identified model response, such as 0.025 s. You can also choose a different conversion method to better match the frequency response in the vicinity of the resonance. For instance, try settingMethodtoBilinear (Tustin) approximation
with a prewarp frequency of 38.4 rad/s, the location of the peak response. As you experiment with settings in the task, compare the original and converted models in a Bode plot to make sure you are satisfied with the match. (For more information about the parameters and options, see theConvert Model Ratetask reference page.)
Convert Model Rategenerates code that produces the discretized model with the variable name that you type into the summary line of the task. For this example, usesys_d
.
% Convert model from continuous to discrete timesys_d = c2d(sys_id,0.025);% Visualize the resultsbodeplot(sys_id,sys_d); legend('Original Model','Converted Model'); gridon;
To confirm that the discretized model captures the transient response due to the resonance, compare the first few seconds of the step responses of the original identified modelsys_id
and the discretized modelsys_d
.
step(sys_id,sys_d,3) legend('identified model sys_id','discretized model sys_d')
Tune Controller for Discretized Plant Model
Finally, use theTune PID Controllerfo任务生成代码r tuning a PI or PID controller for the discretized plantsys_d
. The task designs a PID controller for a specified plant assuming the standard unit-feedback control configuration of the following diagram.
In the task, selectsys_d
as the plant and experiment with settings such as controller type and response time. As you change settings, select output plots on which to observe the closed-loop response generated by the task. CheckSystem response characteristicsto generate a numerical display of closed-loop step-response characteristics such as rise time and overshoot.
% Convert Response Time to Bandwidth% Bandwidth is equivalent to 2 divided by the Response Timewc = 2/11;% PID tuning algorithm for linear plant model[C,pidInfo] = pidtune(sys_d,'PIDF',wc);% Clear Temporary Variablesclearwc% Get desired loop responseResponse = getPIDLoopResponse(C,sys_d,'closed-loop');% Plot the resultstepplot(响应)标题('Step Plot: Reference tracking') gridon
% Display system response characteristicsdisp(stepinfo(Response))
RiseTime: 8.3250 TransientTime: 43.2750 SettlingTime: 43.3500 SettlingMin: 0.9008 SettlingMax: 1.0741 Overshoot: 7.4059 Undershoot: 0.5440 Peak: 1.0741 PeakTime: 22.3250
% Clear Temporary VariablesclearResponse
For this example, suppose that you want the closed-loop system to settle within 50 seconds, and that the system can tolerate overshoot of no more than 10%. Adjust controller settings such asController TypeandResponse Timeto achieve that target. For more information about the available parameters and options, see theTune PID Controllertask reference page.
Further Analysis of Design
Like the other Live Editor tasks,Tune PID Controllergenerates code that produces a tuned controller with the variable name that you type into the summary line of the task. For this example, useC
. The tuned controllerC
is apid
model object in the MATLAB workspace that you can use for further analysis. For example, compute the closed-loop response to a disturbance at the output of the plantsys_d
, using this controller. Examine the response and its characteristics.
CLdist = getPIDLoopResponse(C,sys_d,"output-disturbance"); step(CLdist) gridon
You can use the modelssys_id
,sys_d
, andC
for any other control design or analysis tasks.
See Also
Live Editor Tasks
- Estimate State-Space Model(System Identification Toolbox)|Convert Model Rate|Tune PID Controller