Commandline Equivilant of sisotool pidTuner

3 views (last 30 days)
I have a workflow for generating PID controllers parameters that I would like to script so that I no longer need to open and enter parameters in to sisotool GUI. However I cannot find a completly equivilant workflow on the CML side that replicates my results. I outline my workflow below, thanks in advance for the help!
1) First I run a script to define my transfer functions, then launch the sisotool.
2) Next I select Edit Architecture from the sisotool GUI, select my architecture, and insert the transfer function defined in my setup script into the approriate locations in the architecture. I only define the G and H blocks, the rest I do not provide input for.
3) With my architecture defined I select "Tuning Methods"->"PID Tuning". In the Specifications I select the "PID" controller type and "Design with first order derivative filter". For design mode I switch to "Frequency" and specify the bandwidth that I desier ( this is the parameter I particularly interested in )
.
4) Finaly I extract the optimized coefficent from the PID controller.
Things I have tried....
pidtuner function:
This function seemes promising however I do not see how I can create an architecture that is equivilent to the one I selected in the GUI with the feedback block H.
systune tool and setting TunningGoals:
I setup an equivilant architecture that I pluged into the systune tool. However I could not seupt appropiate TuningGoals to recreate the workflow that I have by hand.

Answers (1)

Altaïr
Altaïr on 17 Feb 2025
To programmatically tune a PID controller, the systune function offers a structured approach. Here's a step-by-step guide:
  1. Define the Plant and PID Systems: Create the plant system and a PID system using the tunablePID function.
  2. Form the Closed-Loop System: Utilize the feedback function to establish the closed-loop system.
  3. Set Tuning Requirements: Define tuning requirements using the TuningGoal object.
  4. Tune the Controller: Use the systune function to calculate the gains based on the specified tuning goals.
Here's an example script where a PID controller is designed for a response time of 1 second and a steady-state error of 0.1%:
% Define the plant transfer function G
G = tf([1], [1, 2, 1]); % Example plant transfer function
% Define the feedback transfer function H
H = tf([1], [1, 1]); % Example feedback transfer function
% Define a tunable PID controller with a first-order derivative filter
C = tunablePID('C', 'PID'); % 'PID' for standard PID controller
% Define the control architecture using a feedback loop
% Here, 'feedback' creates a closed-loop system with G and H
T0 = feedback(G*C, H);
T0.InputName = 'r';
T0.OutputName = 'y';
% The required response time is 1, in the time units of the control system being tuned.
% The maximum steady-state error is 0.1%.
Req = TuningGoal.Tracking('r', 'y', 1, 0.001);
% Perform the tuning using systune
[ST, fSoft] = systune(T0, Req);
Final: Soft = 1.07, Hard = -Inf, Iterations = 65
% Extract the tuned PID controller from the tuned system
C_tuned = getBlockValue(ST, 'C');
% Extract PID coefficients
Kp = C_tuned.Kp;
Ki = C_tuned.Ki;
Kd = C_tuned.Kd;
Tf = C_tuned.Tf; % Filter time constant
% Display the tuned PID parameters
fprintf('Tuned PID Parameters:\n');
Tuned PID Parameters:
fprintf('Kp: %.4f\n', Kp);
Kp: 1.6897
fprintf('Ki: %.4f\n', Ki);
Ki: 0.8781
fprintf('Kd: %.4f\n', Kd);
Kd: 2.4807
fprintf('Tf: %.4f\n', Tf);
Tf: 0.0000
% Create the closed-loop system with the tuned controller
T_cl = feedback(G*C_tuned, H);
% Plot the step response of the closed-loop system
figure;
step(T_cl);
title('Step Response of Tuned Closed-Loop System');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid on;
To specify requirements in terms of gain and phase margins, the TuningGoal.Margins function can be utilized. For further reading, here are some relevant links:
  2 Comments
Sam Chak
Sam Chak on 17 Feb 2025
Hi @Ashok,
In sisotool or controlSystemDesigner, @Justin Deterding can specify the desired closed-loop bandwidth of the control system. I believe he would like to know how to use the TuningGoal objects to establish a control design objective for a specific closed-loop bandwidth, such as 10 rad/s. Could you provide a demonstration in MATLAB?
Altaïr
Altaïr on 18 Feb 2025
Thanks for pointing that out, @Sam Chak.
The pidtune function and the pidTuner app, assume that the bandwidth and the crossover frequency are equal. This is apparent from the PID Controller Design in the Live Editor example.
Within the code generated in the above image, note that third input to the pidtune function, which corresponds to the crossover frequency, is 1.5 which is same as the bandwidth set in the live task. As a side note, the Tune PID Controller live task itself enables users to generate code that tunes a PID controllers and plots responses of the resulting system.
With a similar assumption (bandwidth = cross over frequency) one can use the TuningGoal.LoopShape object to specify the bandwidth (as the cross over frequency). Here's an example:
G = zpk(-5,[-1 -2 -3 -4],6);
C2 = tunablePID('C2', 'PID');
AP = AnalysisPoint('AP');
T2 = feedback(G*C2, AP);
T2.InputName = 'r';
T2.OutputName = 'y';
HardReqs = TuningGoal.LoopShape('AP',1);
SoftReqs = TuningGoal.Margins('AP',3,50);
[CL_sys,fSoft,gHard] = systune(T2,SoftReqs,HardReqs);
C2 = getBlockValue(CL_sys, 'C2');
T2 = feedback(G*C2, 1);
[y2,t2] = step(T2);
figure
plot(t2, y2, '--')
hold off
However, the TuningGoal.Margins object requires both gain and phase margins to be specified, which is why they are included as soft requirements in the above script. For specifying bandwidth and phase margin as requirements, the Tune PID Controller live task seems to be a great choice. It allows for interactive parameter adjustments and generates code for later programmatic use.

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!