Friday, December 27, 2019

Generating code with reusable function

Recently, I needed to re-use Aerospace blockset's ISA atmosphere as a function in C code. When you just generate code, the atmosphere functionality will be embedded inside the simulation step() function. To generate a function that does not depend on simulation, you do the following (Matlab R2019a):

First, put the ISA atmosphere block inside a subsystem (so that it can be made atomic). Then right click on the subsytem:

Generated C code contains the names I set in Simulink model (with "rtu_" added for inputs and "rty_" added for outputs) and the code interface is simple. Contents of SamilsISAAtmosphere.h:

extern void SamilsISAAtmosphere(real_T rtu_hMSL_m, real_T *rty_Temperature_K, real_T *rty_speedOfSound_mps, real_T *rty_pressure_Pa, real_T *rty_airDensity_kgpm3);