Main Content

Generate MATLAB Code from Control System Tuner for Command-Line Tuning

You can generate a MATLAB® script in Control System Tuner for tuning a control system at the command line. Generated scripts are useful when you want to programmatically reproduce a result you obtained interactively. A generated MATLAB script also enables you to programmatically perform multiple tuning operations with variations in tuning goals, system parameters, or model conditions such as operating point.

Tip

You can also save a Control System Tuner session to reproduce within Control System Tuner. To do so, in the Control System tab, click Save Session.

To generate a MATLAB script in Control System Tuner, in the Tuning tab, click Tune. Select Script with current values.

The MATLAB Editor displays the generated script, which script reproduces programmatically the current tuning configuration of Control System Tuner.

For example, suppose you generate a MATLAB script after completing all steps in the example Control of a Linear Electric Actuator Using Control System Tuner. The generated script computes the operating point used for tuning, designates the blocks to tune, creates the tuning goals, and performs other operations to reproduce the result at the command line.

The first section of the script creates the slTuner (Simulink Control Design) interface to the Simulink® model (rct_linact in this example). The slTuner interface stores a linearization of the model and parameterizations of the blocks to tune.

%% Create system data with slTuner interface
TunedBlocks = {'rct_linact/Current Controller/Current PID'; ...
               'rct_linact/Speed Controller/Speed PID'};
AnalysisPoints = {'rct_linact/Speed Demand (rpm)/1'; ...
                  'rct_linact/Current Sensor/1'; ...
                  'rct_linact/Hall Effect Sensor/1'; ...
                  'rct_linact/Speed Controller/Speed PID/1'; ...
                  'rct_linact/Current Controller/Current PID/1'};
OperatingPoints = 0.5;
% Specify the custom options
Options = slTunerOptions('AreParamsTunable',false);
% Create the slTuner object
CL0 = slTuner('rct_linact',TunedBlocks,AnalysisPoints,OperatingPoints,Options);

The slTuner interface also specifies the operating point at which the model is linearized, and marks as analysis points all the signal locations required to specify the tuning goals for the example. (See Create and Configure slTuner Interface to Simulink Model.)

If you are tuning a control system modeled in MATLAB instead of Simulink, the first section of the script constructs a genss model that has equivalent dynamics and parameterization to the genss model of the control system that you specified Control System Tuner.

Next, the script creates the three tuning goals specified in the example. The script uses TuningGoal objects to capture these tuning goals. For instance, the script uses TuningGoal.Tracking to capture the Tracking Goal of the example.

%% Create tuning goal to follow reference commands with prescribed performance
% Inputs and outputs
Inputs = {'rct_linact/Speed Demand (rpm)/1'};
Outputs = {'rct_linact/Hall Effect Sensor/1[rpm]'};
% Tuning goal specifications
ResponseTime = 0.1; % Approximately reciprocal of tracking bandwidth
DCError = 0.001; % Maximum steady-state error
PeakError = 1; % Peak error across frequency
% Create tuning goal for tracking
TR = TuningGoal.Tracking(Inputs,Outputs,ResponseTime,DCError,PeakError);
TR.Name = 'TR'; % Tuning goal name

After creating the tuning goals, the script sets any algorithm options you had set in Control System Tuner. The script also designates tuning goals as soft or hard goals, according to the configuration of tuning goals in Control System Tuner. (See Manage Tuning Goals.)

%% Create option set for systune command
Options = systuneOptions();

%% Set soft and hard goals
SoftGoals = [ TR ; ...
              MG1 ; ...
              MG2 ];
HardGoals = [];

In this example, all the goals are designated as soft goals when the script is generated. Therefore, HardGoals is empty.

Finally, the script tunes the control system by calling systune (Simulink Control Design) on the slTuner interface using the tuning goals and options.

%% Tune the parameters with soft and hard goals
[CL1,fSoft,gHard,Info] = systune(CL0,SoftGoals,HardGoals,Options);

The script also includes an optional call to viewGoal, which displays graphical representations of the tuning goals to aid you in interpreting and validating the tuning results. Uncomment this line of code to generate the plots.

%% View tuning results
% viewGoal([SoftGoals;HardGoals],CL1);

You can add calls to functions such getIOTransfer (Simulink Control Design) to make the script generate additional analysis plots.

Related Topics