Simulation Results

Using Simulink and the Fixed-Point Blockset, you can easily transition from a digital controller described in the ideal world of double precision numbers to one realized in the world of fixed-point numbers. The simulation approach used in this tutorial follows these steps:

  1. Take an initial guess at the scaling. For this tutorial, an initial "proof of concept" simulation using a reasonable guess at the fixed-point word size and scaling is the first step in simulating the digital controller. This step is included only to illustrate how difficult it is to guess the best scaling.

  2. Perform a global override of the fixed-point data types and scaling using double precision numbers. The maximum and minimum simulation values for each digital controller block are logged to the workspace.

  3. Use the automatic scaling procedure. This procedure uses the doubles simulation values previously logged to the MATLAB workspace, and changes the scaling for each block that does not have its scaling fixed.

  4. Perform a simulation on the "fixed" hardware block by overriding the data type with doubles. This simulation determines whether the A/D hardware warrants modification or replacement.

The feedback controller simulation is performed with the Fixed-Point Blockset Interface tool. You launch the Interface tool by selecting the FixPt GUI block within the fxpdemo_feedback model, by selecting Fixed-Point from the Tools menu in the model window, or by typing

fxptdlg('fxpdemo_feedback')

at the command line. The four simulation trials are described in the following sections. The quality of the simulation results is determined by examining the input and output of the analog plant.

Simulation 1: Initial Guess at Scaling

The first simulation uses guesses for the scaling. In general, you won't need to perform this step, and this simulation is included to illustrate the difficulty of guessing at scaling.

After you launch the Interface tool, press the Run button to run the simulation. When the simulation is finished, the interface displays the block name, the maximum and minimum simulation results, the data type, and the scaling for each block. You can then easily plot the results by pressing the Plot button, which launches the Plot System interface. The procedure for this simulation, and the simulation results are shown below.

The display shows that the Up Cast block saturated 23 times, indicating a poor guess for the scaling. Refer to Logging Simulation Results to learn about logging overflow information to the workspace.

The Plot System interface is shown below. This interface displays all MATLAB variable names that contain Scope block data for the current model. You configure the variable name with the Scope block's Properties dialog box, which you launch by choosing the Properties toolbar button.

To plot the simulation results, select one or more variable names, and then select the appropriate plot button. This simulation plots the fixed-point signals for the plant input and the plant output.

The plant input signal and plant output signals are shown below. These signals reflect 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. The nonlinear features are due to significant quantization effects. An important part of fixed-point design is finding scalings that reduce quantization effects to acceptable levels.

Simulation 2: Global Override

Prior to using the automatic scaling tool, a global override with doubles of the fixed-point data type is performed for every block. Using this feature, you can obtain ideal simulation limits. Additionally, you must log maximum and minimum simulation values for all blocks that are to be scaled. This is accomplished by checking the Log minimums and maximums check box for the relevant blocks, and accepting the Log Min, Max default value Use block params.

Global override with doubles is accomplished by configuring Doubles Override to All, and then running the simulation by selecting the Run button. The ideal and fixed-point plant output signals are then compared using the Plot System interface. The procedure for this simulation, and the simulation results are shown below.

The Plot System interface is shown below. This simulation plots both the fixed-point and ideal (double precision) signals for the plant output.

The ideal and fixed-point plant output signals are shown below. The ideal signal is produced by overriding the block output scaling.

Simulation 3: 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.

Automatic scaling is performed for the Controller block. This block is a subsystem representing software running on the target, and requires optimization. Automatic scaling consists of these steps:

  1. Configure Autoscale % Safety Margin to 20. This sets scaling so that the largest simulation value seen is at least 20% smaller than the maximum value allowed.

  2. The Autoscale % Safety Margin parameter value multiplies the "raw" simulation values by a factor of 1.2. Configuring this parameter to a value greater than 1 guarantees the simulation covers the largest possible range, although it does not necessarily mean the resolution improves. Since there is always some uncertainty when representing a real-world value with a fixed-point number with only a few simulations, using this parameter is recommended.

  3. Run the autofixexp M-file script by selecting the Autoscale button. This script automatically changes the scaling on all fixed-point blocks that do not have their scaling locked, and that have their output data type specified as a generalized fixed-point number. It uses the minimum and maximum data logged from the previous simulation. The scaling changes such that the precision is maximized while the full range of simulation values are spanned for each block.

  4. Turn off the global override with doubles by configuring Doubles Override to Use block params.

  5. Run the simulation by selecting the Run button. The automatic scaling results generated by step 2 are captured from the MATLAB workspace and applied to the simulation.

  6. Launch the Plot System interface and plot the plant output signal.

The procedure and results for this simulation are shown below.

Each block is scaled based on its own maximum and minimum values obtained from the previous simulation using double-precision numbers. As shown above, the interface displays the new scaling for each block that had its scaling changed. This scaling is based on the raw simulation values multiplied by 1.2. Note that no saturations or overflows are reported.

A close-up of the plant output signal is shown below. Note that a steady-state has been achieved, but a small limit cycle is present in the steady state due to poor A/D design.

As shown below, the scaling of the A2D did not change because it was locked.

Simulation 4: Individual Override

The previous simulation optimized results for the fixed-point digital controller. In this simulation, the A2D block is configured so that it feeds the digital controller with doubles. This represents overriding the A/D hardware constraints, and is accomplished by checking the Override with doubles check box as shown below as shown below.

You can display the A2D dialog box, by double-clicking on the A2D entry in the interface.

The procedure and results for this simulation are shown below.

A close-up of the plant output signal is shown below. The limit cycle is no longer present in the steady state - confirmation of a poor A/D design. This means you should replace the hardware, amplify the signal, or do some digital processing to better condition the signal.

Â