Tutorial: Feedback Controller

Before You Begin

The tutorial that follows shows you how to use the Fixed-Point Tool to refine the scaling of fixed-point data types associated with a feedback controller model (see Introduction to the Tutorial). Although the tool enables multiple workflows for converting a digital controller described in ideal double-precision numbers to one realized in fixed-point numbers, this tutorial demonstrates the following approach:

  1. Initial Guess at Scaling. Run an initial "proof of concept" simulation using a reasonable guess at the fixed-point word size and scaling. This task illustrates how difficult it is to guess the best scaling.

  2. Data Type Override. Perform a global override of the fixed-point data types using double-precision numbers. The Simulink® software logs the simulation results to the MATLAB® workspace, and the Fixed-Point Tool displays them.

  3. Automatic Scaling. Perform the automatic scaling procedure, which uses the double-precision simulation results to propose fixed-point scaling for appropriately configured blocks. The Fixed-Point Tool allows you to accept and apply the scaling proposals selectively. Afterward, you determine the quality of the results by examining the input and output of the model's analog plant.

To begin, open both the fxpdemo_feedback model and the Fixed-Point Tool.

Initial Guess at Scaling

Initial guesses for the scaling of each block are already specified in each block mask in the model. This task is included only to illustrate the difficulty of guessing the best scaling.

  1. In the Fixed-Point Tool, click the Start button .

    The Simulink software simulates the fxpdemo_feedback model. Afterward, the Fixed-Point Tool displays in its Contents pane the simulation results for each block that logged fixed-point data. The tool stores the results as an active run, denoted by the Active label in the Run column. The OvfSat column reveals that the Up Cast block saturated 23 times, which indicates a poor guess for its scaling.

  2. In the Contents pane of the Fixed-Point Tool, select the Transfer Fcn block named Analog Plant and then click the Time Series Plot button .

    The Fixed-Point Tool plots the signal associated with the plant output.

The preceding plot of the plant output signal reflects the initial guess at scaling. The Bode plot design sought to produce a well-behaved linear response for the closed-loop system. Clearly, the response is nonlinear. Significant quantization effects cause the nonlinear features. An important part of fixed-point design is finding a scaling that reduces quantization effects to acceptable levels.

Data Type Override

Data type override mode enables you to perform a global override of the fixed-point data types and scaling using double-precision numbers, thereby avoiding quantization effects. The Fixed-Point Tool will use these simulation results when performing automatic scaling to propose higher fidelity fixed-point scaling.

  1. In the Dialog pane of the Fixed-Point Tool:

    The Fixed-Point Tool appears as follows.

  2. In the Fixed-Point Tool, click the Start button .

    The Simulink software simulates the fxpdemo_feedback model in data type override mode and stores the results as an active run. Afterward, the Fixed-Point Tool displays in its Contents pane the active run results along with those of the reference run that you generated previously (see Initial Guess at Scaling). The SimDT (simulation data type) column for the active run shows that the model's blocks used a double data type during simulation.

  3. In the Contents pane of the Fixed-Point Tool, select the Transfer Fcn block named Analog Plant that corresponds with either the active or the reference run, and then click the Time Series Difference (A-R) Plot button .

    The Fixed-Point Tool plots both the active and reference versions of the signal associated with the plant output (upper axes), and plots the difference between the active and reference versions of that signal (lower axes). Compare the ideal (double data type) plant output signal with its fixed-point version.

Automatic Scaling

Using the automatic scaling procedure, you can easily maximize the precision of the output data type while spanning the full simulation range. For a complex model, the absence of such a procedure can make achieving this goal tedious and time consuming.

Perform automatic scaling for the Controller block. This block is a subsystem that represents software running on the target, and it requires optimization.

  1. In the Model Hierarchy pane of the Fixed-Point Tool, select the Controller subsystem. In the Dialog pane, specify the Percent safety margin parameter as 20.

    The Percent safety margin parameter value multiplies the "raw" simulation values by a factor of 1.2. Setting this parameter to a value greater than 1 decreases the likelihood that an overflow will occur when fixed-point data types are being used.

    Because of the nonlinear effects of quantization, a fixed-point simulation will produce results that are different from an idealized, doubles-based simulation. Signals in a fixed-point simulation can cover a larger or smaller range than in a doubles-based simulation. If the range increases enough, overflows or saturations could occur. A safety margin decreases the likelihood of this happening, but it might also decrease the precision of the simulation.

  2. In the Fixed-Point Tool, click the Propose fraction lengths button .

    The Fixed-Point Tool analyzes the scaling of all fixed-point blocks whose:

    The Fixed-Point Tool uses the minimum and maximum values stored in the active run to propose each block's scaling such that the precision is maximized while the full range of simulation values is spanned. The tool displays the proposed scaling in its Contents pane as shown here:

  3. Review the scaling that the Fixed-Point Tool proposes. You can choose to accept the scaling proposal for each block by selecting the corresponding Accept check box in the Contents pane. By default, the Fixed-Point Tool accepts all scaling proposals that differ from the current scaling. For this example, ensure that the Accept check box associated with the active run is selected for each of the Controller subsystem's blocks.

  4. In the Fixed-Point Tool, click the Apply accepted fraction lengths button .

    The Fixed-Point Tool applies to the Controller subsystem's blocks the scaling proposals that you accepted in the previous step.

  5. In the Model Hierarchy pane of the Fixed-Point Tool, select the fxpdemo_feedback system. In the Dialog pane:

  6. In the Fixed-Point Tool, click the Start button .

    The Simulink software simulates the fxpdemo_feedback model using the new scaling that you applied in step 4. Afterward, the Fixed-Point Tool displays in its Contents pane information about blocks that logged fixed-point data. The SimDT (simulation data type) column for the active run shows that the Controller subsystem's blocks used fixed-point data types with the new scaling.

  7. In the Model Hierarchy pane of the Fixed-Point Tool, select the fxpdemo_feedback system. In the Contents pane, select the Transfer Fcn block named Analog Plant that corresponds with either the active or the reference run, and then click the Time Series Difference (A-R) Plot button .

    The Fixed-Point Tool plots the active and reference versions of the plant output signal, as well as their difference. Compare the ideal plant output signal (of data type double) in the reference run with its fixed-point version in the active run.

  8. Optionally, you can zoom in to view the steady-state region with greater detail. From the Tools menu of the figure window, select Zoom In and then drag the cursor to draw a box around the area you want to view more closely. For instance, the following figure shows a close-up view of a portion of the previous plot.

    Note that the plant output signal represented by the active run achieves a steady state, but a small limit cycle is present because of poor A/D design.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS