Simulink UCGN Hello World

From UcgnWiki

Jump to: navigation, search

Up: Main Page



The SimulinkUcgnDistribution contains a "Hello World" example, in the simulink/Examples/HelloWorld subfolder. You will find a simple Simulink model showing how to set up a design and then process it through the stages of model transformation and code-generation. The model is intended to show some of the basic concepts available.

The following files are part of the design (located in ./simulink/Examples/HelloWorld)

  • TST_HelloWorldModel.mdl - the simulink model
  • GBL_Constants.m - showing how workspace parameters are used as "tunable parameters"
  • FCS_FlightControlSystemDefinitions.m - showing how to work with bus objects
  • GBL_Definitions.m - showing how to emulate enumerated types using workspace parameters

The following set of files controls the processing

  • SlcGenExport_TST_HelloWorldModel.m - creates the domain-specific XML model from the Simulink model
  • SlcProject.xml - project file for the UCGN Simulink model transformation
  • SimulinkUcgn.bat - batchfile to run model transformation and code generator

All files listed above are located in the ./simulink/Examples/HelloWorld folder.


  • Java JRE 1.6 or newer
  • Matlab R2009b, Simulink 7.4

(most of the testing has been done using Simulink 7.4. Settings in the file SlcConfiguration.xml check for simulink version, but you can override it by performing a global search and replace, replacing 7.4 with your version in SlcConfiguration.xml)


  • Extract to a location of your choice
    We'll refer to that folder location as UCGN_SIMULINK_BASE
  • Make a copy of the folder work_template called work, at the same level
  • Modify SlcGenExport_TST_HelloWorldModel.m, to point to your
    UcgnBasePath = '<your UCGN_SIMULINK_BASE>';
  • Modify SlcProject.xml, to point to UCGN_SIMULINK_BASE/work as the Working Directory
    <workingDirectory><your UCGN_SIMULINK_BASE>\work</workingDirectory>
  • Modify SimulinkUcgn.bat, to point to your UCGN_SIMULINK_BASE
    set UCGN_SIMULINK_BASE=<your UCGN_SIMULINK_BASE> (enclose in quotes if your path containes spaces!)

Running the example

  • In Matlab, run SlcGenExport_TST_HelloWorldModel.m. This will export the model (as well as workspace parameter and bus object definitions) to TST_HelloWorldModel.xml. The xml file is located in UCGN_SIMULINK_BASE/work/mdl
  • From a DOS command window run SimulinkUcgn.bat. This will start the java-based model transformation and code generation, using SlcProject.xml as the project file.

The model

Generated source code

C-language source code will be generated to <UCGN_SIMULINK_BASE>/work/src/lang/c

Here is an excerpt from the header file TST_HelloWorldModel.h

   (SubSystem) Declaration of TST_HelloWorldModel.TST_HelloWorldModel
TST_HelloWorldModel_Calculate(FCS_FlightControlSystem_StatusStructuredType *FcsStatus,            /* Input  */
                              GBL_Types_Double                              TerrainElevationM,    /* Input  */
                              GBL_Definitions_EjectionCmdEnumeratedType    *PilotRecommendation); /* Output */

And a section from the source file TST_HelloWorldModel.c:

      (RelationalOperator) TST_HelloWorldModel.TST_HelloWorldModel.LowAltitude
       -> inlined (Constant) Constant250
       -> inlined (Gain) PressureAltitude_M
       -> inlined (BusSelector) Selector
       -> inlined (Sum) AltitudeAboveGround_M
   LowAltitude = ((GBL_Constants_Feet2Meter * FcsStatus->PressureAltitude_Ft) - TerrainElevationM) <= 250.0;
      (Switch) TST_HelloWorldModel.TST_HelloWorldModel.PilotRecommendationSwitch
       -> inlined (Constant) DontEject
       -> inlined (BusSelector) Selector
       -> inlined (Logic) EngineFailure
       -> inlined (BusSelector) Selector
       -> inlined (BusSelector) Selector
       -> inlined (Logic) InTrouble
       -> inlined (Constant) EjectAsap
   if (FcsStatus->InAir && (FcsStatus->LeftEngFailed || FcsStatus->RightEngFailed) && LowAltitude) {
      *(PilotRecommendation) = GBL_Definitions_EjectionCmd_EjectAsap;
   } /* end of if */
   else {
      *(PilotRecommendation) = GBL_Definitions_EjectionCmd_DontEject;
   } /* end of else */

This is the type definition resulting from the bus object FCS_FlightControlSystem_Status

typedef struct FCS_FlightControlSystem_StatusStructuredTypeTag {
   GBL_Types_Boolean InAir;                  
   GBL_Types_Boolean LeftEngFailed;          
   GBL_Types_Boolean RightEngFailed;         
   GBL_Types_Single  PressureAltitude_Ft;    
   GBL_Types_Single  IndicatedAirspeed_Knots;
} FCS_FlightControlSystem_StatusStructuredType;

The (emulated) enumerated type definition in GBL_Definitions looks like this

typedef enum GBL_Definitions_EjectionCmdEnumeratedTypeTag {
   GBL_Definitions_EjectionCmd_DontEject    = 0,
   GBL_Definitions_EjectionCmd_EjectAsap    = 1,
   GBL_Definitions_EjectionCmd_EjectWarning = 2 
} GBL_Definitions_EjectionCmdEnumeratedType;
Personal tools