From Simulink to C-Code and back to Simulink
9 views (last 30 days)
I am trying to generate a C code for a controller in a Simulink Model, that I want to replace in the original Simulink model for the C code in order to test it.
This are the steps I followed:
C code creation:
- Check "treat as atomic unit" in the controller subsystem and then convert to "Referenced Model". Opened the control as top Model.
- Opened Simulink coder app and went to "C code generation settings", used grt.tlc (I am using Simulink coder and it doesnt let me change to ert.tlc even though I have embedded coder installed)
- Build the model and get all the .c and .h codes (no main.c is created!)
C Code insertion in Simulink.
- On the comlpete model, I go to simulation target settings, include the header file "control.h" and the source file "control.c" that were generated by the simulink coder. I also include the directories were all the h and c files are.
- Since the step function is void in the definition and the inputs and outputs were declared as external, I check on the "Enable custom code globals as function interface".
- I call "C caller" in simulink and try to get the functions, which dont appear. I click on the refresh custom code and find that "rt_logging.h" cannot be found but it was declared in "control.h". Since this "rt_logging.h" was not generated (MAT file Logging was checked), I comment it out from the "control.h" and now I can see all the functions that were created in "control.c". Remember that this "control.c" is the generated file from the simulink coder.
- Once I can see all the functions, I decide to select the step one, which is the main one, and the external inputs and outpus are now accesible in the c caller block.
- I create the signal bus and connect the c caller in place in the Simulink model and run it.
- I find varios erros corresponding to "undifined reference" that I have identified to be other functions in the "control.c" but outside the "control_Step.c". One of them however is linked to a function defined in "rt_logging.c" which was actually created but in the html folder, but again no "rt_logging.h" was found.
So my question is what I am doing wrong? I guess it should be in step 4 of the second part, where I only insert the "control_Step.c".
How can I insert in the C caller something that runs all that I created with simulink coder?
Included are some pictures of the model with the C caller already implemented and also the simulink model of what is included in the control.
NOTE: I also tried code generation with Embedded Coder App using ert.tlc but the problem that appears is that the code generation uses some types definitions that generate error: byte_T, creal32_T and creal64_T. I found out that these are "Reserved Keywords for Code Generation" and even though I tried copying this keywords in the identifiers section in code generation settings, the code still appears with this words and keep throwing the error.
Mark McBroom on 6 Mar 2021
"SIL" mode addresses this use case and eliminates all of the uncessary steps you are trying to follow. SIL mode allows you to replace a top model or referenced model with the generated code and then run your simulation using the generated code.