This example shows how to tune PID controllers for plants that cannot be linearized. You use the **Linear Analysis Tool** to identify the frequency model of the plant, and then you use the **PID Tuner** to tune the PI controllers for the plant.

Copyright 2018, The MathWorks, Inc.

This example uses the power factor correction circuit described in the Simscape™ Electrical™ example Power Factor Correction for CCM Boost Converter (Simscape Electrical). Power factor correction preconverters correct the power factor of loads thus increasing the efficiency of the distribution system. This correction is useful when non-linear impedances such as switched mode power supplies are connected to the AC grid. This model uses a power rectifier and a switched mode power supply to convert a 120V AC supply to a regulated DC supply of 400V. The semiconductor components are modeled using MOSFETs rather than ideal switches to ensure that the device on-resistances are correctly represented.

```
open_system('Active_PFC.slx')
```

The Controls subsystem has the cascaded PI controller to control the inductor current and output voltage.

```
open_system('Active_PFC/Controls')
```

The controllers are initialized with preconfigured gains that result in poor power factor correction, as can be seen from the line current in the graph.

In general, the workflow for tuning this cascaded controller architecture is:

Estimate the plant frequency model for the inner current loop between the PWM generator in the Controls subsystem and the inductor current measurement block.

Use the PID Tuner to import this model and tune the current PI controller.

Estimate the plant frequency model for the outer voltage loop between the voltage PI controller to be put in and the measured voltage.

Use the PID Tuner to import this model and tune the voltage PI controller.

In this example, you use the **Linear Analysis Tool** to estimate the frequency response data of the inner current loop. Modify the setup to make the plant run to steady-state in open loop. A steady-state duty cycle of 0.72 is required to obtain the voltage requirement of 400V with a DC supply of 120V. The steady-state values of the inductor current and the load voltage are measured to be 7.266A and 405.5V, respectively.

The linear analysis points to be used in the frequency response estimation need to be established between the PWM generator to the measured inductor current, that is input perturbation and output measurement. To set these analysis points, first open the **Linearization** tab. To do so, in the Simulink model window, in the **Apps** gallery, click **Linearization Manager**.

To set the input perturbation, click the output signal from the constant block. Then, on the **Linearization Tab**, in the **Insert Analysis Points** gallery, select **Input Perturbation**.

To set the output measurement, click the signal from the inductor current measurement block, and select **Output Measurement** from the **Insert Analysis Points** gallery.

After setting up the model to collect the frequency response data, open the **Linear Analysis Tool**. In the Simulink model window, in the **Apps** gallery, click **Frequency Response Estimator**.

To analyze the power factor correction circuit, the frequency response is estimated from a steady-state operating point. To do so, you create a new operating point from a simulation snapshot.

In the **Linear Analysis Tool**, on the **Estimation** tab, in the **Operating Point** drop down list, select **Take Simulation Snapshot**.

In the Enter snapshot times to linearize dialog box, in the **Simulation snapshot times** field, enter 0.15 seconds, which is enough time for the open-loop system to reach steady state.

Click **Take Snapshots**.

To inject a fixed step sinestream into the model for frequency response estimation, on the **Input Signal** drop-down list, select **Fixed Sample Time Sinestream**.

In the Specify fixed sample time dialog box for the sinestream signal, specify a **Sample time** of 2e-7 seconds.

Click **OK**.

In the Create sinestream input with fixed sample time dialog box, configure the parameters of the sinestream signal.

To specify the frequency units for estimation, in the Frequency units drop-down list, select Hz.

To select the frequencies at which to estimate the plant response, click the **+** icon.

In the Add frequencies dialog box, specify 30 logarithmically spaced frequencies ranging from 10 Hz to 15 kHz.

Set the amplitude at all frequencies to 0.036, which is 5% of the steady state duty cycle to ensure that the system is properly excited. If the input amplitude is too large, the operating point of the power factor corrector changes. If the input amplitude is too small, the sinestream is indistinguishable from ripples in the power electronics circuits. Both these situations result in inaccurate frequency response estimation.

Leave all other sinestream settings at their default values.

To estimate and plot the frequency response, on the **Estimation** tab, click **Bode**.

After estimating the frequency response, export the estimation to the MATLAB workspace before tuning the PID controller. In the Data Browser, drag **estsys1** from linear analysis workspace to MATLAB Workspace.

Set up the current PI controller in the subsystem **Controls** to control the duty cycle of the MOSFET, as follows

To begin tuning the controller, open the Block Parameters dialog box for the PID controller block **PI Current**.

To open the **PID Tuner**, click **Tune**.

The PID Tuner automatically tries to linearize the plant under consideration, but the linearization is zero because of the PWM switching components. To import the frequency response data for the plant, in **PID Tuner**, on the **Plant** drop-down list, select **Import**.

In the Obtain plant model dialog box, select **Importing an LTI System**, and, in the table, select estsys1.

Click **OK**.

To use frequency-domain requirements to tune the controller, in the **Domain** section, select **Frequency**. To examine the open-loop response in the frequency domain, create a Bode plot by clicking **Add Plot** and selecting **Open-loop**.

The Bode plot shows a block response (dashed line) and a tuned response (solid line). The block response is the open-loop response for the current PI gains in the PI Controller block. The tuned response is the open-loop response using the tuned PI gains in PID Tuner.

You can graphically tune the gains further by adjusting the bandwidth and phase margin to have the desired crossover frequency so that the controller has good current reference tracking. To do this, set the bandwidth and phase margin to 23600 rad/s and around 60 degrees, respectively.

To update the PID Controller block with the tuned gains, click **Update Block**.

Simulating the model with these tuned gains provides good reference current tracking.

The process of estimating the plant frequency response to tune the outer voltage loop is similar to that of the inner loop.

Set the linear analysis I/O points for the outer loop between the output of the PI controller block that you enter, in the Controls subsystem and the load voltage in the plant model.

Next, in the **Linear Analysis Tool**, set the simulation snapshot at 0.4 seconds, which is enough time for the plant to attain steady state.

Create a sinestream input with 30 logarithmic frequencies sweeping from 10Hz to 5kHz. Set the amplitude of all these frequencies to 0.1, which is 10% of the steady-state input from the PI controller.

With these values set, calculate the estimated frequency response **estsys2**.

Export this model to the MATLAB workspace to be used with the PID tuner.

Set up the voltage loop PI controller for the plant to work in closed loop, as follows

Import the frequency data into PID tuner to allow the gains to be automatically tuned for the outer loop. You can switch the supply to the AC source to test against the original setup. The gains are fine-tuned with the crossover frequency chosen so that the controller can follow the output voltage but reject the 120Hz oscillations of the voltage output. Set the bandwidth to 342.5 rad/sec and the phase margin to 60 degrees to meet this requirement.

Now, with the tuned PI gains for both the controllers you should see an inductor current waveform that better conforms with the reference current. The output voltage waveform has a better rise time with minimal overshoot and a good settling time to the steady-state value.