Troubleshoot Range Analysis of System Objects

When deriving ranges for a model that uses a system object, the analysis fails if the model contains variables that can refer to multiple handle objects. The following example shows how to reconfigure the code so that the Fixed-Point Tool can derive ranges for the model.

In this example, range analysis of the first model ex_HandleVariableRefersToMultipleObjects produces an error because there is a variable in the code that can refer to different system objects depending on other conditions. The model, ex_HandleVariableRefersToSingleObject is a rewrite of the first model with the same functionality, but the Fixed-Point Tool is able to derive ranges for the model.

  1. Open the first model. At the MATLAB® command line, enter:

    addpath(fullfile(docroot,'toolbox','fixpoint','examples'))
    ex_HandleVariableRefersToMultipleObjects
    

    The code inside the MATLAB Function block refers to the custom System Object fAddConstant.

    function y  = fcn(u, c)
    %#codegen
    
    persistent hSysObjAddTen
    persistent hSysObjAddNegTen
    persistent hSysObjForStep
    
    if isempty(hSysObjAddTen)
        hSysObjAddTen = fAddConstant(10);
    end
    
    if isempty(hSysObjAddNegTen)
        hSysObjAddNegTen = fAddConstant(-10);
    end
    
    if c > 0
        hSysObjForStep = hSysObjAddTen;
    else
        hSysObjForStep = hSysObjAddNegTen;
    end
    
    y = step(hSysObjForStep, u);

  2. From the Simulink® Apps tab, select Fixed-Point Tool.

  3. In the Fixed-Point Tool, under New workflow, select Iterative Fixed-Point Conversion.

  4. In the Fixed-Point Tool, under System Under Design (SUD), select ex_HandleVariableRefersToMultipleObjects as the system you want to convert.

  5. Under Range Collection Mode, select Derived ranges.

  6. Click the Collect Ranges button.

    The analysis fails because there is a handle variable in the code that can refer to different system objects depending on the value of c.

  7. You can rewrite the code inside the MATLAB Function block so that the Fixed-Point Tool is able to derive ranges for the System Object:

    function y  = fcn(u, c)
    %#codegen
    
    persistent hSysObjAddTen
    persistent hSysObjAddNegTen
    
    if isempty(hSysObjAddTen)
        hSysObjAddTen = fAddConstant(10);
    end
    if isempty(hSysObjAddNegTen)
        hSysObjAddNegTen = fAddConstant(-10);
    end
    
    if c > 0
        y = step(hSysObjAddTen, u);    
    else
        y = step(hSysObjAddNegTen, u);    
    end
    
  8. Close the Fixed-Point Tool and the ex_HandleVariableRefersToMultipleObjects model. Open the ex_HandleVariableRefersToSingleObject model, which contains the rewritten code. At the MATLAB command line, enter:

    ex_HandleVariableRefersToSingleObject

  9. From the Simulink Apps tab, select Fixed-Point Tool.

  10. In the Fixed-Point Tool, under New workflow, select Iterative Fixed-Point Conversion.

  11. Under Range Collection Mode, select Derived ranges.

  12. Click the Collect Ranges button.

    This time, the Fixed-Point Tool successfully derives ranges for the variables used in the model.