Main Content

Generate C Code from Simulink Models

Use the Embedded Coder® product to generate C or C++ code that is optimized for deployment on rapid-prototyping boards, embedded processors, or microprocessors. If you are new to Embedded Coder or your application code customization requirements are minimal, you can use graphical tools and default code configuration settings to quickly generate production-quality code. If you need to produce customized code for integration with existing external code or you want to meet code guidelines and standards, tooling is available to configure the code generator to meet requirements for interfacing, code appearance, packaging, and optimizations.

Generating and reviewing code for deployment to an embedded system can be as simple as preparing the model for code generation with the Quick Start tool. Then, with code tools accessible from the Simulink Editor, you can configure code interfaces, initiate code generation, and review the generated code.

Prerequisites

To complete this tutorial, you must have:

  • MATLAB®

  • MATLAB Coder™

  • Simulink®

  • Simulink Coder

  • Embedded Coder

Example Models

The tutorial uses example models rtwdemo_roll and rtwdemo_roll_harness. The models have been verified for simulation.

Open model rtwdemo_roll.

This model implements a basic roll axis autopilot algorithm, which controls the aileron position of an aircraft.

The model represents one component in the greater control system of an aircraft. Through the HDG_Mode signal, the control system places the model in one of two operating modes: roll attitude hold or heading hold. The RollAngleReference and HeadingMode subsystems calculate a roll attitude setpoint that supports one of the operating modes. Then, the BasicRollMode subsystem, a PID controller, calculates an aileron position command based on the setpoint and on feedback that indicates the measured roll attitude and rate of change. The model is designed to operate at 40 Hz.

The tutorial uses model rtwdemo_roll_harness to test rtwdemo_roll.

You will learn how to:

  1. Generate code by using the Embedded Coder Quick Start tool.

  2. Configure the data interface.

  3. Configure a model parameter as a global variable for tuning during run time.

  4. Compare model simulation and generated code results for numeric equivalency.

  5. Deploy the generated code.

To start the tutorial, see Generate Code by Using Embedded Coder Quick Start.