reading and navigating through code generated from matlab or simulink can be daunting at first
there are several files generated and if you don't fully know c or c plus code you
might want to give up after looking through a couple of the files generated that is why
in this video i will show you how to get started with understanding code generated from simulink
although i will be using simulink know that a lot of these concepts will also apply to matlab
for this video i will be using a model of the duffing oscillator which is a chaotic
dynamic system to view the code associated with this model i will go to the embedded coder app
and build the model note that to generate code you must set the model to a fixed step solver
when the model is finished building the c code generated will appear in this right hand panel
we can navigate through the files generated using this drop down menu
the file that always appears first is called model.c in my case this is duffing.c because
the name of my model is duffing before diving into the code i wanted to point out all these comments
all code files generated are commented throughout to make it easier to follow the code the model.c
file contains the entry point functions to our model there are three functions that will always
be generated in this file which are the initialize step and terminate functions scrolling down we
first see the step function which describes how the blocks in our model relate mathematically
let's explore this by looking at line 192 which corresponds to the subtract block signal
i know that this line corresponds to the subtract block first because of the name
all variables generated inherit the names of the block in the model i can also hover over
the variable in the code and the block will get highlighted this feature is called traceability
and it also works in reverse when i click on the subtract signal line 192 is highlighted
in the code as i said before the step function shows how the blocks are related mathematically
we can observe this by looking at the gamma signal which is cosine of omega which is 1.4
multiplied by time and all of that is multiplied by gamma which is 0.32 it's a little hard to see
this correlation at first because all the math for the subtract signal is being done
in one line of code so let's change that going into the model settings i will disable the enable
local block outputs option i will also change the parameter behavior from inlined to tunable
what this will do is change our parameters such as gain blocks from static numbers to variables
that are defined in memory after building this model again the code for the subtract block
is now simply gamma minus beta minus alpha minus delta and the code solving for each of
these signals can be found above we also see that the gain blocks are now being called as variables
scrolling down further in the code we can see the initialize function and the terminate function
which as the names imply initialize and terminate our model respectively
but where are these functions being called the answer is in the ert underscore main
dot c file this file is an example main showing how to call the entry point functions in order
to run the model the entry point functions are called in this main function we can see
initialize and terminate functions but the step function is being called in this rt underscore
one step function which is declared just above because rt underscore one step only runs one
step of our model we must attach it to a loop or a clock in order to have it run multiple time steps
there is one more file that i wanted to point out and that is the model.h file this is a header file
that defines the data structures for our model scrolling past the macros we see the definition
of these structures which declare the variables used for the blocks and signals in our model
the structures are separated by the block type for example gain and constant blocks are parameters
this output block is an external output and the signals coming out of the blocks are block signals
we can see what each structure is declaring by the comment above each structure definition
something to point out about these variables is that many of them are being declared as real
underscore t which if you know c code isn't a real data type so what kind of data is this by hovering
over the real underscore t variable we can see that it is a type definition of a double being
declared in the rtw types header file by clicking on this code we are taking to the rtw types file
which we can see contains the definitions of all the data types that simulink uses
with that you now have an overview of how to read and navigate co-generated from simulink but this
video only scratches the surface of cogeneration if you would like to know more or have any
unanswered questions about code generations from matlab or simulink check out the link in
the description for the code generation video tutorial series and if you like this video and
have suggestions on topics for other short videos be sure to let us know in the comments thank you