Floating- to fixed-point conversion, an essential step in embedded software development, can be tedious, labor-intensive, and error-prone. Algorithms designed in floating-point math represent the ideal algorithm behavior and must often be converted to fixed-point math for more cost-effective, integer-only, mass-production hardware. Once converted, bit-accurate fixed-point simulations let you explore the behavior of fixed-point data types before embedding the design onto the hardware.
Using a fault-tolerant fuel system model as an example, this article describes tools and a workflow for converting models from floating-point to fixed-point representation for production code generation. Topics covered include preparing the model and data; analyzing, refining, and optimizing the fixed-point scaling; and generating optimized code.
The approach described here uses two tools in Simulink Fixed Point: Fixed-Point Advisor and Fixed-Point Tool.
Preparing model and data for conversion
A fault-tolerant fuel-system model contains three main components: an electronic control unit (ECU) controller, a plant for the engine gas dynamics, and several sensors. We focus on the ECU controller in this article.
The Fixed-Point Advisor, an interactive tool designed to facilitate model preparation, can be used to complete the steps discussed in more detail below:
Replacing unsupported blocks—The Fixed Point Advisor checks your model for blocks that do not support fixed-point data types. Continuous-time blocks will need to be replaced by their discrete-time equivalents. A list of supported data types for Simulink blocks shows most blocks support fixed-point data types. Stateflow fully supports fixed-point data types, as do hundreds of Embedded MATLAB functions, including those typically used in embedded algorithm design.
Setting up signal logging and creating initial reference data—Signals of interest, such as I/O signals, are logged during simulation for use in equivalence comparisons with the fixed-point model and for code generation. We can log all signals or select signals, including unnamed ones, from portions of the model subsystem hierarchy.
Specifying target hardware characteristics—The Fixed Point Advisor reminds us to specify the correct word lengths for char, int, long, and other attributes unique to a particular processor to avoid producing incorrect results from simulation or code generation.
Preparing for data typing and scaling—During the initial design, engineers frequently use inherited data-type propagation to speed up prototyping and to quickly iterate several designs. As the project approaches production, they fine-tune and specify individual data types and scaling to optimize fixed-point results.
The Fixed-Point Advisor facilitates this workflow by automating the following steps:
• Remove output data-type inheritance to help avoid data-type propagation conflicts.
• Relax input data-type settings or constraints that might lead to data-type propagation errors.
• Identify blocks that require us to specify design minimum and maximum values for fixed-point conversion.
Performing initial data typing and scaling—Based on our initial input, the Fixed-Point Advisor proposes data typing and initial scaling for inport blocks, constants, parameters, and blocks with intermediate data types like the Sum blocks. The tool uses either the design or simulation floating-point minimum and maximum values to propose the initial fixed-point scaling. The tool reports scaling conflicts and suggests ways to resolve them. It then checks for numerical errors and analyzes the logged signals.
An initially scaled fixed-point model is then produced, together with plots comparing the floating-to-fixed-point model results.
Checking the model’s suitability for production code deployment—Using the Fixed-Point Advisor, we run final checks to determine the model’s suitability for production code deployment. These include:
• Identify blocks that generate expensive saturation and rounding code.
• Identify questionable fixed-point operations, such as ensuring that fixed-point lookup tables are spaced to maximize code efficiency.
Analyzing, refining, and optimizing fixed-point scaling
The Fixed-Point Tool lets you interactively analyze, refine, and optimize the fixed-point scaling in your model. The tool first identifies fixed-point values that overflowed and/or saturated. It then finds the ideal dynamic range for scaling by temporarily converting all signals to floating-point precision and executing another simulation run. The ideal dynamic range is used to calculate more suitable fixed-point scaling for each block. We then accept or reject the proposed scaling for each signal. We can also configure the tool to ignore selected blocks, enabling you to use automatic scaling with individually scaled blocks.
Note that the above steps operate on the same model, which enables you to use one model for both the floating- and fixed-point designs and reduce the need to maintain separate models during design iterations.
Throughout the process, comparison plots are used to provide a quick and easy way to analyze and contrast the behavior of the fixed- and floating-point designs.
Generating optimized code
We can use Real-Time Workshop Embedded Coder to generate code for different targets. If we choose the default ANSI/ISO C-code option, we can deploy the portable code on any target with the specified word sizes. We can also make use of processor-optimized code by using Target Function Libraries to automatically replace generated math functions, math operators, and memory-allocation functions, such as memcpy, with target-specific versions.
Bit-accurate fixed-point simulation helps you model designs within the Simulink environment. Tools provided by Simulink Fixed Point let you automate time-consuming parts of the fixed-point conversion workflow and explore designs to further refine the fixed-point performance. Real-Time Workshop Embedded Coder provides an automated path to production code deployment.
Products used were Simulink, Stateflow, Fixed-Point Toolbox, Simulink Fixed Point, Simulink Verification and Validation, Real-Time Workshop, Real-Time Workshop Embedded Coder, and Stateflow Coder.
For more information on the fuel system model referenced in this article, visit www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=18439&objectType=FILE. For more on fixed-point modeling and code generation tips for Simulink 7 (R2008a), see www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=19835&objectType=FILE.
Bill Chou and Tom Erkkinen of The Mathworks wrote this article for Automotive Engineering.