Converting Models from Floating Point to Fixed Point for Production Code Generation



Similar documents
Integrating MATLAB into your C/C++ Product Development Workflow Andy Thé Product Marketing Image Processing Applications

Best Practices for Verification, Validation, and Test in Model- Based Design

Simulink Modeling Guidelines for High-Integrity Systems

Model Based System Engineering (MBSE) For Accelerating Software Development Cycle

Development of AUTOSAR Software Components within Model-Based Design

Best practices for developing DO-178 compliant software using Model-Based Design

Introduction to MATLAB Gergely Somlay Application Engineer

Product Development Flow Including Model- Based Design and System-Level Functional Verification

Caterpillar Automatic Code Generation

Schnell und effizient durch Automatische Codegenerierung

Software Development with Real- Time Workshop Embedded Coder Nigel Holliday Thales Missile Electronics. Missile Electronics

Software Development Principles Applied to Graphical Model Development

How To Build A Trading Engine In A Microsoft Microsoft Matlab (A Trading Engine)

Model-Driven Software Development for Robotics: an overview

Automating Code Reviews with Simulink Code Inspector

AUTOSAR Seminar WS2008/ Assignment: Simulation of Automotive Systems in the Context of AUTOSAR

A Case Study of Application Development and Production Code Generation for a Telematics ECU with Full Unified Diagnostics Services

Eli Levi Eli Levi holds B.Sc.EE from the Technion.Working as field application engineer for Systematics, Specializing in HDL design with MATLAB and

Power inverters: Efficient energy transformation through efficient TargetLink code

Parallel Computing with MATLAB

Design Data Management in Model-Based Design

Developing an Isolated Word Recognition System in MATLAB

I can make just such ones if I had tools, and I could make tools if I had tools. -Eli Whitney

Introduction to Xilinx System Generator Part II. Evan Everett and Michael Wu ELEC Spring 2013

Eliminate Memory Errors and Improve Program Stability

DAQ in MATLAB HANS-PETTER HALVORSEN,

PowerPC Microprocessor Clock Modes

Making model-based development a reality: The development of NEC Electronics' automotive system development environment in conjunction with MATLAB

Software Production. Industrialized integration and validation of TargetLink models for series production

Ingo Stürmer, Dietrich Travkin. Automated Transformation of MATLAB Simulink and Stateflow Models

Introduction to Simulink & Stateflow. Coorous Mohtadi

Data Analysis with MATLAB The MathWorks, Inc. 1

Simulink Getting Started Guide

Echtzeittesten mit MathWorks leicht gemacht Simulink Real-Time Tobias Kuschmider Applikationsingenieur

isolar Integrated Solution for AUTOSAR

Lecture 9 Modeling, Simulation, and Systems Engineering

Introduction to Object-Oriented Programming in MATLAB

MATLAB Distributed Computing Server Licensing Guide

OPC COMMUNICATION IN REAL TIME

Automotive Software Engineering

How To Use Safety System Software (S3)

Efficient and Faster PLC Software Development Process for Automotive industry. Demetrio Cortese IVECO Embedded Software Design

DS1104 R&D Controller Board

Product Information CANape Option Simulink XCP Server

AN EVALUATION OF MODEL-BASED SOFTWARE SYNTHESIS FROM SIMULINK MODELS FOR EMBEDDED VIDEO APPLICATIONS

Hardware in the Loop (HIL) Testing VU 2.0, , WS 2008/09

Why Adopt Model-Based Design for Embedded Control Software Development?

Verification by. Simulation. Verification by. Simulation. Verification by. Simulation / Model Check. Validation and Testing.

Installation, Licensing, and Activation License Administration Guide

From Signal Routing to complete AUTOSAR compliant CAN design with PREEvision (II)

Model, Analyze and Optimize the Supply Chain

Technical Data Sheet SCADE Suite R16. Software Prototyping and Design. Technical Data Sheet SCADE Suite R16 1

Process Models and Metrics

Embedded Vision on FPGAs The MathWorks, Inc. 1

Lecture 3 - Model-based Control Engineering

Model-based system-on-chip design on Altera and Xilinx platforms

Setting up a Local Interconnect Network (LIN) using dspace MicroAutoBox 1401/1501 Simulink Blocks

MathWorks Products and Prices North America Academic March 2013

Advanced Electronic Platform Technologies Supporting Development of Complicated Vehicle Control Software

What s New in MATLAB and Simulink

Quality Assurance Methods for Model-based Development: A Survey and Assessment

Machine Learning with MATLAB David Willingham Application Engineer

Automating Business Processes Using SharePoint Designer

WEB-8000 Series and WEBs-N4. Our newest products are open 4 innovation.

Comprehensive Static Analysis Using Polyspace Products. A Solution to Today s Embedded Software Verification Challenges WHITE PAPER

How to program a Zumo Robot with Simulink

Dials & Gauges Blockset

What is Automotive Software Engineering? What is Automotive Software Engineering? What is Automotive Software Engineering?

Product Information Services for Embedded Software

Complexity Analysis of Simulink Models to improve the Quality of Outsourcing in an Automotive Company. Jeevan Prabhu August 2010

Best Practises for LabVIEW FPGA Design Flow. uk.ni.com ireland.ni.com

Software House Embedded Systems

Advanced Techniques for Simulating ECU C-code on the PC

Integrated Model-based Software Development and Testing with CSD and MTest

Debugging A MotoHawk Application using the Application Monitor

Parallel Data Preparation with the DS2 Programming Language

EHOOKS Prototyping is Rapid Again

Model-Based Development of Safety-Critical Software: Safe and Effi cient

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Work with Arduino Hardware

Candle Plant process automation based on ABB 800xA Distributed Control Systems

How To Design An Image Processing System On A Chip

TÜ V Rheinland Industrie Service

Service Oriented Architecture for Agricultural Vehicles

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Testing and Inspecting to Ensure High Quality

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Entwicklung und Testen von Robotischen Anwendungen mit MATLAB und Simulink Maximilian Apfelbeck, MathWorks

VHDL Test Bench Tutorial

Visual Programming of Logic, Motion, and Robotics

Fast Arithmetic Coding (FastAC) Implementations

EE289 Lab Fall LAB 4. Ambient Noise Reduction. 1 Introduction. 2 Simulation in Matlab Simulink

ADI Simulation Systems Help Speed Aircraft Development from Preliminary Design through Certification

Image Acquisition Toolbox Adaptor Kit User's Guide

Embedded/Real-Time Software Development with PathMATE and IBM Rational Systems Developer

Transcription:

MATLAB Digest Converting Models from Floating Point to Fixed Point for Production Code Generation By Bill Chou and Tom Erkkinen An essential step in embedded software development, floating- to fixed-point conversion can be tedious, labor-intensive, and error-prone. System engineers frequently design algorithms in floating-point math, usually double-precision. This format represents the ideal algorithm behavior but takes little account of the algorithm s final realization in production software and hardware. Software engineers and suppliers in mass production environments often need to convert these algorithms to fixed-point math for their integer-only hardware. As a result, multiple iterations between system and software engineers are often required. Products Used Sim u lin k S t a t e f l o w Fi xe d-poin t To o l b ox Simulink Fixed Point Simulink Verification and Validation Real-Time Workshop Real-Time Workshop Embedded Coder S t a t e f l o w C o d e r 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 for production code generation. Topics covered include: Preparing the model data Analyzing, refining, and optimizing the fixed-point scaling Generating optimized code Verifying and validating the code The approach described here uses two tools in Simulink Fixed Point : Fixed-Point Advisor and Fixed-Point Tool. and several sensors. We concentrate on the ECU controller for fixed-point modeling, conversion, and code generation. Some preparation tasks are required even if the code generated from the ECU is to be deployed on a floating-point embedded micro-processor. Fixed-Point Advisor is an interactive tool designed to facilitate model preparation (Figure 1). Preparing the Model And Data For Conversion The fault-tolerant fuel system model contains three main components: an ECU controller, a plant for the engine gas dynamics, Figure 1. Model preparation using Fixed-Point Advisor. 1 MATLAB Digest www.mathworks.com

Figure 2. Block data type support table in Simulink. Using Fixed-Point Advisor, we will complete the following steps: Replace unsupported blocks Set up signal logging and create initial reference data Specify target hardware characteristics Prepare for and perform data typing and scaling Check model suitability for production code deployment Replacing Unsupported Blocks We begin by replacing blocks that do not support fixed-point data types including replacing continuous-time with discrete-time blocks. We can use rate transition blocks to convert the continuous-time signals to discrete-time signals sampled at 10ms, as required by the controller. At this stage, we must also analyze the effect of sampling and quantization on system performance and stability. Simulink provides a list of data types supported by each block (Figure 2). Most blocks support fixed-point data types. Stateflow fully supports fixed point. hundreds of functions that support fixed point, including those typically used in embedded algorithm design. Setting Up Signal Logging And Creating Initial Reference Data We log signals of interest from simulation for use in equivalence comparisons with the fixed-point model and for code generation. Typically, input and output signals are logged. Fixed-Point Advisor provides a list of additional signals that it might be helpful to log. To make signal logging easier, we can log all signals or select signals, including unnamed ones, from portions of the model subsystem hierarchy (Figure 3). Once we have configured the signals to be logged, we create and store the reference data using the floating-point model. Specifying Target Hardware Characteristics Model simulation behavior and code generation outputs are determined by the characteristics of the target hardware. Model Advisor reminds us to specify the correct word lengths for char, int, long, and other attributes unique to a particular embedded microprocessor so as to avoid producing incorrect results from simulation or code generation. If your model includes Embedded MATLAB functions, you can choose from Figure 3. Logging fixed-point data, including unnamed signals. 2 MATLAB Digest www.mathworks.com

Preparing For Data Typing And Scaling Data type inheritance and other propa- gation settings affect the time needed to convert the model from floating to fixed point. During the initial design phase, 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. Figure 4. Identifying issues that could lead to the generation of inefficient code. Fixed-Point Advisor facilitates this workflow by automating the following steps: Removing output data type inheritance to help avoid data type propagation conflicts Relaxing input data type settings or constraints that might lead to data-type propagation errors Verifying that state charts have strong data typing with Simulink We must specify design minimum and maximum values for inport blocks. We can also specify these values for other block outputs and parameters. Performing Initial Data Typing And Scaling Using Fixed-Point Advisor, we specify initial data typing and scaling for blocks. Based on our initial input and direction, Fixed-Point Advisor proposes data typing and initial scaling for inport blocks, constants, blocks that do not fall into either category, parameters, and blocks that use intermediate data types, such as the Sum and Product blocks. Fixed-Point Advisor uses either the design or simulation minimum and maximum from the floatingpoint data 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 Fixed-Point Advisor, we run final checks to determine the model s suitability for production code deployment (Figure 4). These include: Disabling signal logging to avoid declaring extra signal memory in the generated code Identifying blocks that generate expensive saturation and rounding code Identifying questionable fixed-point operations for example, ensuring that lookup tables are spaced so as to maximize code efficiency Analyzing, Refining, and Optimizing the Fixed-Point Scaling Using the Fixed-Point Tool s automatic scaling function, we analyze, refine, and optimize scaling for relevant blocks in the model that we initially scaled using Fixed- Point Advisor. We use the data type override feature to collect the dynamic range of signals in double precision. The Fixed-Point Tool uses this information to propose a more suitable fixed-point scaling for each block, based on the number of available bits. Individual blocks can be locked down to prevent them from being modified by the tool. We can then use automatic scaling with individually scaled blocks and accept or reject the proposed scaling for each signal. With the Fixed-Point Tool we can use one model for both the floating- and fixed-point designs, reducing the need to maintain separate models during design iterations. 3 MATLAB Digest www.mathworks.com

Figure 5. Automated scaling using the Fixed-Point Tool. Comparison plots provide a quick and easy way to analyze and contrast the behavior of the fixed- and floating-point designs. The Fixed-Point Tool records the number of overflows and saturations that occurred in the simulation. Figure 5 shows the tool and its proposed scaling for the Fuel System model. Further analysis showed the output of the Sum block saturated during simulation. The automatic scaler proposed changing the fraction length from 11 to 10 bits, increasing the output dynamic range to avoid saturation and retain maximum precision. Once we have made this change, the results match closely (Figure 6), and we are ready for code generation. Generating Optimized Code Before generating code for the system, we run checks in Simulink using Model Advisor. Some checks, such as identify questionable fixed-point operations and check hardware implementation, are crucial for fixed-point development. Additional model standards checks are available in Simulink Verification and Validation, including checks based on the MAAB guidelines and safety-related standards such as IEC 61508 and DO-178B. To generate code from an optimized design, we must first select a deployment target. Options range from the default ANSI/ISO C/C++ to a target with processor-optimized code. We could also target middleware or abstraction layers, such as AUTOSAR. For ANSI/ISO C code generation, we select the Embedded Real-Time Target (ERT) option in Real-Time Workshop Embedded Coder, which is optimized for fixed-point code (Figure 7). Other than word sizes and other target characteristic settings, this code is portable and can be deployed on any target with the specified word sizes. There are several capabilities for generating target-optimized code. The first is to have the generated code call an existing C function at the appropriate point within the algorithm, typically by using the Legacy Code Tool in Simulink. Figure 6. Comparison with original floating-point results. Figure 7. ANSI-C code optimized for a fixed-point target using Real-Time Workshop Embedded Coder. 4 MATLAB Digest www.mathworks.com

A second capability is to automatically replace generated math functions, math operators, and memory-allocation functions such as memcpy with target-specific versions. This is done using Target Function Libraries (TFL), tables mapping default functions and operators to their target-specific equivalents. Several pre-built TFLs are available (Figure 8). In addition, end users can create customized tables for their own targets. The TFL is then available as a codegeneration setting. Once a TFL is selected, the generated code incorporates the replacement items. One advantage of this option is that you can quickly generate optimized code for several different targets from the same model simply by changing the TFL. Figure 9 compares ANSI-C- and TriCore optimized code for fixed-point addition of 32-bit integers with saturation logic. The code is smaller and the execution time dramatically reduced by a factor of 17. Verifying and Validating the Production Code The reference data collected from the floating-point behavioral model can be reused for equivalence testing throughout the development process. We first use the data to compare the results of the initial fixed-point design to the original floating-point model. We do not need to generate code just to compare the model results because bit-accurate fixed-point simulation is supported by Simulink Fixed Point. Figure 8. Selecting optimized Target Function Libraries. int32 _ T _ add _ s32 _ s32 _ s32 _ sat(int32 _ T a, int32 _ T b) int32_ T tmp; tmp = a+b; if ((a < 0) && (b < 0) && (tmp >= 0)) tmp = MIN _ int32 _ T; } elseif ((a > 0) && (b > 0) && (tmp <= 0)) tmp = MAX _ int32_ T; } return tmp; } int32 _ Tadd _ s32 _ s32 _ s32 _ sat(int32_ T a, int32_ T b) return ( _sat int)a+b; } Figure 9. Fixed-point code optimized for ANSI-C (top) and TriCore (bottom) using TFL. 5 MATLAB Digest www.mathworks.com

Resources visit www.mathworks.com Technical Support www.mathworks.com/support Online User Community www.mathworks.com/matlabcentral Demos www.mathworks.com/demos Training Services www.mathworks.com/training Third-Party Products and Services www.mathworks.com/connections Worldwide CONTACTS www.mathworks.com/contact Figure 10. PIL testing. The scope (right) shows no difference between the signal output from the model and the generated object code using the PIL block. e-mail info@mathworks.com We test the code on the target processor using processor-in-the-loop (PIL) testing (Figure 10). PIL cosimulates the object code on an embedded hardware or instruction set simulator with the original plant model or test harness in Simulink. MathWorks link products, such as Embedded IDE Link MU (for Green Hills MULTI ), automate PIL testing using thirdparty integrated development environments (IDEs). It is possible to run PIL testing on processors supported by these IDEs, such as Freescale MPC 5500. Another way to verify the code is to use Polyspace products, which formally analyze code to identify defects such as fixedpoint overflow, division-by-zero, and array out-of-bounds. 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. 2008 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders. 80368V00 11/08 For More Information Using Diagnostics to Improve Your Simulink Model www.mathworks.com/diagnostics Fixed-Point Modeling and Code Generation Tips for Simulink 7 (R2008a) www.mathworks.com/simulink7-tips 6 MATLAB Digest www.mathworks.com