Static Main Program Module

Overview

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

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.

Rate Grouping and the Static Main Program

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.

Modifying the Static Main Program

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

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS