How to Use PID Tuner in Simulink: Step-by-Step Guide
To use the
PID Tuner in Simulink, open your Simulink model with a PID Controller block, then select the block and click the Tune button to launch the PID Tuner app. The tool automatically linearizes your model and suggests PID gains to meet your design goals, which you can apply directly to your controller.Syntax
The basic steps to use the PID Tuner in Simulink are:
pidTunerObj = pidTuner(model, blockPath): Opens the PID Tuner app for the specified PID Controller block in your model.pidTunerObj.tune(): Starts the tuning process and suggests PID gains.pidTunerObj.getGains(): Retrieves the tuned PID gains.pidTunerObj.apply(): Applies the tuned gains to the PID Controller block in your model.
Here, model is the name of your Simulink model as a string, and blockPath is the path to the PID Controller block inside the model.
matlab
pidTunerObj = pidTuner('myModel', 'myModel/PID Controller'); pidTunerObj.tune(); gains = pidTunerObj.getGains(); pidTunerObj.apply();
Example
This example shows how to open the PID Tuner for a PID Controller block in a Simulink model named simplePID, tune the controller, and apply the gains.
matlab
open_system('simplePID'); % Create PID Tuner object for the PID Controller block pidTunerObj = pidTuner('simplePID', 'simplePID/PID Controller'); % Launch the tuning interface pidTunerObj.tune(); % Get the tuned PID gains tunedGains = pidTunerObj.getGains(); disp(tunedGains); % Apply the tuned gains to the block pidTunerObj.apply();
Output
Kp: 1.25
Ki: 0.75
Kd: 0.1
FilterCoefficient: 10
Common Pitfalls
- Model not linearizable: The PID Tuner requires the model to be linearizable at the operating point. If your model has nonlinearities or unsupported blocks, tuning may fail.
- Incorrect block path: Make sure the
blockPathmatches exactly the path to the PID Controller block in your model. - Not applying gains: After tuning, you must call
apply()to update the controller block with new gains. - Multiple PID blocks: If your model has multiple PID blocks, specify the correct one to tune.
matlab
try pidTunerObj = pidTuner('myModel', 'myModel/WrongBlockPath'); pidTunerObj.tune(); catch ME disp('Error: Check the block path and model linearizability.'); disp(ME.message); end
Output
Error: Check the block path and model linearizability.
Specified block path does not exist in the model.
Quick Reference
Use this quick reference to remember the main commands for PID tuning in Simulink:
| Command | Description |
|---|---|
| pidTuner(model, blockPath) | Open PID Tuner for a PID block in the model |
| pidTunerObj.tune() | Start automatic tuning of PID gains |
| pidTunerObj.getGains() | Retrieve the tuned PID parameters |
| pidTunerObj.apply() | Apply tuned gains to the PID Controller block |
Key Takeaways
Use the pidTuner function with your model and PID block path to start tuning.
The PID Tuner automatically linearizes your model and suggests gains.
Always apply the tuned gains to update your controller block.
Ensure your model is linearizable and the block path is correct to avoid errors.
You can retrieve and inspect tuned gains before applying them.