Force macro definition in C-code generated with SImulink - c

I am building a Simulink model in order it generates the C-code equivalent. Actually, the C-code doing that already exists but I want to visualize it with Simulink. I know that the code Simulink will generate will not be the same than the one it is based on, but there are some functionalities I want to keep.
For example, I use a lot of Macro (#define) in the C code, to be more readable and I really do not know how to force Simulink to define these macro's. So my question is, which object/structure/data should I create and where (model workspace or Matlab workspace?) such that when C code will be generated, all my macro's will be defined with preprocessor commands.
I thank you a lot for any lead you could propose.

Finally I found my answer in this huge user guide
https://engineering.purdue.edu/~dionysis/EE452/Lab2/Lab2_Supporting_Materials/Embedded%20Coder_Users%20Guide.pdf
I had to use mpt.Parameter Imported from file and check Inline parameter option.

Related

How to convert MATLAB Coder C Code to MATLAB Original Code [duplicate]

I have lot of .c file which is autogenerated by simulink coder. Analyzing those file to understand simulink model is some what time consuming, As per my knowledge there is no reverse tool which converts c code to simulink model.
so is there any possiblity to convert the auto generated c file to simulink model using any parser?
Short answer: No.
The real question is: why would you want to do that? The golden reference (the design if you want) is the model from which the code is generated, it's a one-way system. If you want to change the design, change the model, re-generate the code. That's how it's designed and intended to work, not the other way round.
You can however integrate C code (functions) into a Simulink model the legacy code tool, but that's a different exercise to what you are asking. You can also call external C code from a Simulink model using a MATLAB Function block and the coder.ceval command, see Integrate C Code Using the MATLAB Function Block in the documentation for more detail.s.
There is some research like this one in that area, but any approach I know attempts to convert general c code to a model. The "best" performance I have ever heard of was a ready to use and no longer available prototype which generated incredible huge simulink models out of some lines of c code.
To have any practical use in your use case, a translator which recognizes the original model blocks would be required. Such a software does not exist.
You can try Modelify.
Modelify is a new tool that converts legacy C code into Simulink models. Instead of a verbatim conversion, Modelify transforms low-level logic from C into higher level constructs in Simulink.
Website Link

Is it possible to see the macros of a compiled C program?

I am trying to learn C and I have this C file that I want view the macros of. Is there a tool to view the macros of the compiled C file.
No. That's literally impossible.
The preprocessor is a textual replacement that happens before the main compile pass. There is no difference between using a macro and putting the code the macro expands to in its place.*
*Ignoring the debugger output. But even then you can do it if you know the right #pragma to tell it the file and line number.
They're always defined in the header file(s) that you've imported with #include, or that those files in turn #include.
This may involve a lot of digging. It may involve going into files that make no sense to you because they're not written for casual inspection.
Any macros of any importance are usually documented. They may use other more complex implementation-specific macros that you shouldn't concern yourself with ordinarily, but if you're curious how they work the source is all there.
That being said, this is only relevant if you have the source and more specifically a complete build environment. Once compiled all these definitions, like the source itself, do not appear in the executable and cannot be inferred directly from the executable, especially not a release build.
Unlike Java or C#, C compiles directly to machine code so there's no way to easily reverse that back to the source. There are "decompilers" that try, but they can only really guess as to the original source. VM-based languages like Java and C# only lightly compile the code, sot here are a lot of hints as to how that code was generated and reversing it is an easier process.

How to manage automatically generated c-files from matlab coder?

I have an existing dll with multiple functions, let's call it mylib. I decided to use matlab coder to help me write my c-files. These c-files are compiled and linked to generate mylib using VS 2015.
So I test this theory i first started by converting funcA.m to funcA.c using matlab coder ver 3.4 (R2017b) . funcA is ust a simple mathematical function. Then I compiled and linked all the c-files generated by matlab coder to include funcA inside mylib. And this worked perfectly. Meaning, funcA which was originally a m-file was compiled, linked to mylib and had produced the accurate results.
The problem arises when I use matlab coder to generate my second function. Let's call the second function -> funcB
After converting funcA.m to funcA.c, matlab coder had generated other c-files which includes xgeqp3.c and xnmr2.c and its corresponding header files. I consider them non-readable by humans because they were automatically generated by matlab coder and it is not human friendly to read them as you can tell by the name of the c-files. However, they are being called by funcA so they have to be compiled into the library as well.
These matlab generated c-file caused me problems because when I converted funcB.m to funcB.c, it had also generated xgeqp3.c and xnmr2.c. Although the files names are identical, its contents are different, with different formal parameters and of course different c-codes, even though they have the same function name and file name.
So the same library cannot contain these two functions generated by matlab coder because the declarations for xgeqp3.c and xnmr2.c contradicts. I'm sure other people might have the same problem. Is there a way I can manually name the extra c-files generated my matlab coder so that I can avoid such duplicate names? Or is there a way I can manage the c-files with duplicate names but different codes? The easier option is to create separate libraries for the two functions. But I want all my mathematical functions to be in the same library.
How should I handle this situation? I'm open to new ideas. Thank you in advance.
You should generate the c code together for all the functions that you want compile in the same dll.
If you are using command-line you should do:
codegen funcA, funcB
If you are using the Matlab Coder App, add both functions as entry point files.

simulink coders c file to simulink model

I have lot of .c file which is autogenerated by simulink coder. Analyzing those file to understand simulink model is some what time consuming, As per my knowledge there is no reverse tool which converts c code to simulink model.
so is there any possiblity to convert the auto generated c file to simulink model using any parser?
Short answer: No.
The real question is: why would you want to do that? The golden reference (the design if you want) is the model from which the code is generated, it's a one-way system. If you want to change the design, change the model, re-generate the code. That's how it's designed and intended to work, not the other way round.
You can however integrate C code (functions) into a Simulink model the legacy code tool, but that's a different exercise to what you are asking. You can also call external C code from a Simulink model using a MATLAB Function block and the coder.ceval command, see Integrate C Code Using the MATLAB Function Block in the documentation for more detail.s.
There is some research like this one in that area, but any approach I know attempts to convert general c code to a model. The "best" performance I have ever heard of was a ready to use and no longer available prototype which generated incredible huge simulink models out of some lines of c code.
To have any practical use in your use case, a translator which recognizes the original model blocks would be required. Such a software does not exist.
You can try Modelify.
Modelify is a new tool that converts legacy C code into Simulink models. Instead of a verbatim conversion, Modelify transforms low-level logic from C into higher level constructs in Simulink.
Website Link

Compile-time lookup array creation for ANSI-C?

A previous programmer preferred to generate large lookup tables (arrays of constants) to save runtime CPU cycles rather than calculating values on the fly. He did this by creating custom Visual C++ projects that were unique for each individual lookup table... which generate array files that are then #included into a completely separate ANSI-C micro-controller (Renesas) project.
This approach is fine for his original calculation assumptions, but has become tedious when the input parameters need to be modified, requiring me to recompile all of the Visual C++ projects and re-import those files into the ANSI-C project. What I would like to do is port the Visual C++ source directly into the ANSI-C microcontroller project and let the compiler create the array tables.
So, my question is: Can ANSI-C compilers compute and generate lookup arrays during compile time? And if so, how should I go about it?
Thanks in advance for your help!
Is there some reason you can't import his code generation architecture to your build system?
I mean, in make I might consider something like:
TABLES:=$(wildcard table_*)
TABLE_INCS:=$(foreach dir,$TABLES,$dir/$dir.h)
include $(foreach dir,$TABLES,dir/makefile.inc)
$MAIN: $(SRS) $(TABLE_INCS)
where each table_* contains a complete code generation project whose sole purpose is tho build table_n/table_n.h. Also in each table directory a makefile fragment named makefile.inc which provides the dependency lines for generated include files, and now I've removed the recursivity.
Done right (and this implementation isn't finished, in part because the point is clearer this way but mostly because I am lazy), you could edit table_3/table_3.input, type make in the main directory and get table_3/table_3.h rebuilt and the program incrementally recompiled.
I guess that depends on the types of value you need to look up. If the processing to compute each value demands more than e.g. constant-expression evaluation can deliver, you're going to have problems.
Check out the Boost preprocessor library. It's written for C++ but as far as I'm aware, the two preprocessors are pretty much identical, and it can do this sort of thing.

Resources