This example shows how to generate code for a roll axis autopilot control system designed using Embedded Coder.
This model represents a basic roll axis autopilot with two operating modes: roll attitude hold and heading hold. The mode logic for these modes is external to this model. The model architecture represents the heading hold mode and basic roll attitude function as referenced models.
The roll attitude control function is a PID controller that uses roll attitude and roll rate feedback to produce an aileron command. The input to the controller will be either a basic roll angle reference or a roll command to track the desired heading.
Figure 1 shows the top level of the Simulink model.
Figure 1: Top-level diagram for the roll axis autopilot model
Figure 2 shows the basic roll angle reference calculation implemented as the virtual subsystem RollAngleReference. Embedded Coder will inline this calculation directly into the main function for rtwdemo_roll.
Figure 2: Virtual subsystem RollAngleReference implementing a roll angle reference
Figure 3 shows the contents of the model HeadingMode, which computes the roll command to track the desired heading. This is a separate model referenced by rtwdemo_roll.
Figure 3: Model HeadingMode, which computes the roll command to track desired heading.
Figure 4 shows the contents of the model BasicRollMode, which computes the roll attitude control function. This is a separate model referenced by rtwdemo_roll. The discrete-time integrator block is a masked subsystem built from block primitives.
Figure 4: Model BasicRollMode implementing roll attitude control function (PID)
Create a temporary folder (in your system's temporary folder) for the build process.
currentDir = pwd; [~,cgDir] = rtwdemodir();
The model is pre-configured to generate code using Embedded Coder. To generate code using Simulink Coder only, reconfigure the model or type rtwconfiguredemo('rtwdemo_roll','GRT') in MATLAB.
### Model reference RTW target (rtwdemo_attitude.c) for model rtwdemo_attitude is out of date because rtwdemo_attitude.c does not exist. ### Starting build procedure for model: rtwdemo_attitude ### Successful completion of build procedure for model: rtwdemo_attitude ### Model reference RTW target (rtwdemo_heading.c) for model rtwdemo_heading is out of date because rtwdemo_heading.c does not exist. ### Starting build procedure for model: rtwdemo_heading ### Successful completion of build procedure for model: rtwdemo_heading ### Starting build procedure for model: rtwdemo_roll ### Successful completion of build procedure for model: rtwdemo_roll
The figure below contains a portion of rtwdemo_roll.c
You can view the entire generated code in a detailed HTML report, with bi-directional traceability between model and code.
Close models and return to previous working directory.
close_system('rtwdemo_roll',0) close_system('rtwdemo_attitude',0) close_system('rtwdemo_heading',0) cd(currentDir); rtwdemoclean;