| Real-Time Workshop® Embedded Coder™ | ![]() |
| On this page… |
|---|
In most cases, the easiest strategy for deploying your generated code is to use the Generate an example main program option to generate the ert_main.c or .cpp module (see Generating the Main Program Module).
However, if you turn the Generate an example main program option off, you can use the module matlabroot/rtw/c/ert/ert_main.c as a template example for developing your embedded applications. The module is not part of the generated code; it is provided as a basis for your custom modifications, and for use in simulation. If your existing applications, developed prior to this release, depend upon a static ert_main.c, you may need to continue using this module.
When developing applications using a static ert_main.c, you should copy this module to your working directory and rename it to model_ert_main.c before making modifications. Also, you must modify the template makefile such that the build process creates model_ert_main.obj (on Unix, model_ert_main.o) in the build directory.
The static ert_main.c contains
rt_OneStep, a timer interrupt service routine (ISR). rt_OneStep calls model_step to execute processing for one clock period of the model.
A skeletal main function. As provided, main is useful in simulation only. You must modify main for real-time interrupt-driven execution.
For single-rate models, the operation of rt_OneStep and the main function are essentially the same in the static version of ert_main.c as they are in the autogenerated version described in Stand-Alone Program Execution. For multirate, multitasking models, however, the static and generated code is slightly different. The next section describes this case.
Targets based on the ERT target sometimes use a static ert_main module and disallow use of the Generate an example main program option. This may be necessary because target-specific modifications have been added to the static ert_main.c, and these modifications would not be preserved if the main program were regenerated.
Your ert_main module may or may not use rate grouping compatible model_stepN functions. If your ert_main module is based on the static ert_main.c module, it does not use rate-specific model_stepN function calls. The static ert_main.c module uses the old-style model_step function, passing in a task identifier:
void model_step(int_T tid);
By default, when the Generate an example main program option is off, the ERT target generates a model_step "wrapper" for multirate, multitasking models. The purpose of the wrapper is to interface the rate-specific model_stepN functions to the old-style call. The wrapper code dispatches to the appropriate model_stepN call with a switch statement, as in the following example:
void mymodel_step(int_T tid) /* Sample time: */
{
switch(tid) {
case 0 :
mymodel_step0();
break;
case 1 :
mymodel_step1();
break;
case 2 :
mymodel_step2();
break;
default :
break;
}
}The following pseudocode shows how rt_OneStep calls model_step from the static main program in a multirate, multitasking model.
rt_OneStep()
{
Check for base-rate interrupt overflow
Enable "rt_OneStep" interrupt
Determine which rates need to run this time step
ModelStep(tid=0) --base-rate time step
For N=1:NumTasks-1 -- iterate over sub-rate tasks
Check for sub-rate interrupt overflow
If (sub-rate task N is scheduled)
ModelStep(tid=N) --sub-rate time step
EndIf
EndFor
}You can use the TLC variable RateBasedStepFcn to specify that only the rate-based step functions are generated, without the wrapper function. If your target calls the rate grouping compatible model_stepN function directly, set RateBasedStepFcn to 1. In this case, the wrapper function is not generated.
You should set RateBasedStepFcn prior to the %include "codegenentry.tlc" statement in your system target file. Alternatively, you can set RateBasedStepFcn in your target_settings.tlc file.
As in the generated ert_main.c, a few modifications to the main loop and rt_OneStep are necessary. See Guidelines for Modifying the Main Program and Guidelines for Modifying rt_OneStep.
Also, you should replace the rt_OneStep call in the main loop with a background task call or null statement.
Other modifications you may need to make are
If your model has multiple rates, the generated code does not operate correctly unless:
The multirate scheduling code is removed. The relevant code is tagged with the keyword REMOVE in comments (see also the Version 3.0 comments in ert_main.c).
Use the MODEL_SETEVENTS macro (defined in ert_main.c) to set the event flags instead of accessing the flags directly. The relevant code is tagged with the keyword REPLACE in comments.
Remove old #include ertformat.h directives. ertformat.h will be obsoleted in a future release. The following macros, formerly defined in ertformat.h, are now defined within ert_main.c:
EXPAND_CONCAT CONCAT MODEL_INITIALIZE MODEL_STEP MODEL_TERMINATE MODEL_SETEVENTS RT_OBJ
See also the comments in ertformat.h.
If applicable, follow comments in the code regarding where to add code for reading/writing model I/O and saving/restoring FPU context.
Note If you modify ert_main.c to read a value from a continuous output port after each base-rate model step, see the relevant cautionary guideline in Guidelines for Modifying rt_OneStep. |
When the Generate an example main program option is off, the Real-Time Workshop Embedded Coder software generates the file autobuild.h to provide an interface between the main module and generated model code. If you create your own static main program module, you would normally include autobuild.h.
Alternatively, you can suppress generation of autobuild.h, and include model.h directly in your main module. To suppress generation of autobuild.h, use the following statement in your system target file:
%assign AutoBuildProcedure = 0
If you have cleared the Terminate function required option, remove or comment out the following in your production version of ert_main.c:
The #if TERMFCN... compile-time error check
The call to MODEL_TERMINATE
If you do not want to combine output and update functions, clear the Single output/update function option and make the following changes in your production version of ert_main.c:
Replace calls to MODEL_STEP with calls to MODEL_OUTPUT and MODEL_UPDATE.
Remove the #if ONESTEPFCN... error check.
The static ert_main.c module does not support the Generate Reusable Code option. Use this option only if you are generating a main program. The following error check raises a compile-time error if Generate Reusable Code is used illegally.
#if MULTI_INSTANCE_CODE==1
The static ert_main.c module does not support the External mode option. Use this option only if you are generating a main program. The following error check raises a compile-time error if External mode is used illegally.
#ifdef EXT_MODE
![]() | Model Entry Points | Rate Grouping Compliance and Compatibility Issues | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |