Firmware Development Using Simulink Code Generation¶
Background¶
Simulink provides a MATLAB-based graphical environment for modeling and simulating control systems. It is extensively used to model, simulate, and analyze complex dynamical systems, including motor drives. Its user-friendly block diagram environment in Simulink allows control algorithm to be developed and validated in simulation before they are implemented on the embedded hardware.
To apply the controller developed in Simulink to the AMDC platform, we use Simulink Code Generation and Embedded Coder capabilities provided by MathWorks to generate C code from Simulink models for embedded implementation. This article describes how these MathWorks code generation practices can be applied to the AMDC platform. In this article, this workflow is referred to as Autogen (Automatic Code Generation).
Development Approach with Simulink and AMDC¶
The figure below shows the Simulink + AMDC workflow, which separates control development into two domains:
Design domain (Simulink):
The control algorithm is developed and validated using a graphical model.Execution domain (AMDC):
The generated C code is executed in real time on the embedded controller at a fixed time interval.
Structure the Simulink model into three subsystems, as follows:
Input/Output (I/O): Use this for simulation and visualization only
Plant: Use this subsystem to represent the physical system for simulation
Controller: Place the control algorithm to be deployed in this subsystem
Within the AMDC domain, the deployed controller is implemented in the DSP user app. The user app communicates with the FPGA-based sensor and PWM interfaces. See the related articles for more information on User Apps and PWM interface.
Recommended Workflow¶
The recommended workflow for developing control code is:
Develop and validate the control algorithm in Simulink.
Convert the controller to an atomic subsystem and then to a referenced model.
Generate C code using Simulink Autogen.
Integrate the generated code into the AMDC project.
Execute and validate on hardware.
Key Implementation Requirements and Best Practices¶
For successful development and integration of control code, follow the implementations requirements and best practices below:
Discrete-Time Implementation: All blocks within the controller must be discrete-time, since the AMDC executes control logic at fixed sampling intervals.
Fixed-Step Solver: The Simulink model must use a fixed-step solver to ensure compatibility with real-time execution.
Consistent Sample Time: The entire controller subsystem should operate at a single, well-defined sample time before converting to an atomic subsystem and creating a referenced model.
Code Generation Settings: The code generation target should be set to Embedded Coder (
ert.tlc). The build configuration should enable “Generate Code Only”.Referenced Model Usage: The controller subsystem should be converted to an atomic subsystem, then converted to a referenced model. Any updates to model settings should be performed after opening the referenced model as the top model.
AMDC Integration Details
All generated source (
*.c) and header (*.h) files must be included in the AMDC project.The autogenerated folder must be added to the compiler include paths.
Do not delete any generated files, as some auxiliary files may be required during compilation.
File and Path Constraints
File paths must not contain whitespace.
The path to the folder containing the MATLAB script and the Simulink model, as well as any parent directory, must not include whitespace. Otherwise, it will result in a build error.
This organization ensures that the autogenerated controller code is correctly compiled and integrated into the AMDC firmware.
Example Model¶
An example tutorial, Tutorial: Autogen, is provided to demonstrate the Simulink code generation workflow for control implementation and its integration with the AMDC.
Conclusion¶
The Simulink workflow provides a structured and efficient approach for implementing control algorithms on the AMDC. By separating control design from embedded implementation, this approach enables:
Rapid development and iteration
Improved reliability through simulation
Clear mapping between design and execution
This methodology is recommended for developing advanced control systems on the AMDC platform.