Simulink to C-Code, Problem with "Variable Transport Delay" (Access Violation)

3 views (last 30 days)
Hello,
I have a problem embedding c-code which was generated by Matlab/Simulink in a VS C++ 2010 Express-project:
What I did:
- Use "Code Generation"/"Build" to create c-code (using grt.tlc)
- Create new c-project using Visual C++ 2010 Express
- copy all files to the project and include "mySimulinkFunction" in "myProgramm.c"
.
Sequence of execution (myProgramm.c):
main() {
mySimulinkFunction_initialize(1)
rtmSetTFinal(mySimulinkFunction_M,10)
while (t<tFinal){
mySimulinkFunction_output(1)
mySimulinkFunction_update(1)
}
}
.
This one worked fine for a simple Simulink-model. Now I have another simple model which contains a "variable transport delay" (Input->VTD->Output). Unfortunately, I can't execute this one, if I set the time-delay > 0. The error occurs in this line (looking at mySimulinkFunction.c):
if ((tMinusDelay <= tBuf[oldestIdx]))
...
with tMinusDelay > 0 and oldestIdx = 0 and tBuf = 0x00000000
There is an access violation while reading at position 0x0 (code 0xC0000005).
Can anyone help me figure out how to solve the problem? Because the code was generated automatically there are 2 possible solutions:
- I have to make some preparations before generate the code (in Simulink)
- My way of embedding the code is wrong and I have to do some rework there
Robert
PS: By the time this problem is solved, I also may need some help embedding "Simulink with Simscape" generated c-code in a vs-project. This one did not work at all (until now).

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 23 Nov 2011
The main() used for executing the generated code is in $matlabroot/rtw/c/grt/grt_main.c - perhaps you can follow the same sequence to make sure that all initialization is done correctly?
Just FYI, if you plan to do this type of customization more extensively, I would recommend using ert.tlc which ships with Embedded Coder - the generated code is easier to read and more optimized.

More Answers (1)

Robert
Robert on 28 Nov 2011
Hey, thanks a lot! Using the given main.c-file, the simulation seems to work.
Here is just one more thing: I added
printf(rtmGetT(S))
to see the simulation time increase with a stepsize of 0.001. Unfortunately, the simulation runs forever (using tFinal = 10) because the simulation-time does not increase (it stays 0.000).
Because grt_main.c is using:
while (tFinal-rtmGetT(S) > rtmGetT(S)) )
->while ([10-0 > 0])
the simulation keeps calculating forever.
Looking at timing.ClockTick0 the simulation-time increases like it is supposted to. Any ideas about this one?
  2 Comments
Kaustubha Govind
Kaustubha Govind on 28 Nov 2011
What version of MATLAB are you using? I see the following in R2011b:
rtmGetTFinal(S)-rtmGetT(S) > rtmGetT(S)*DBL_EPSILON
(The DBL_EPSILON is missing in what you pasted)
Also, how do you pass in tFinal? Do you observe the same issue when using the exe generated directly from Simulink? Perhaps there are macros/symbols defined in the generated makefile that are required. Try re-compiling your version of grt_main.c using the generated makefile.
Robert
Robert on 5 Dec 2011
Hey,
you're right I did not paste "DBL_EPSILON". I am using R2011b, too. I pass in tFinal at the very beginning of main() in grt_main.c:
real_T finaltime = 10;
I really don't know what the problem was, but now (after 1 week of ignoring this problem and doing something else and no major change in the software) it is finally working :-)
Unfortunately, I really can't tell how to fix it the next time, because I just don't know.. Maybe I can figure it out withing the next fey days!
I will also have a closer look at running "Simulink with Simscape" generated code in VS2010. Maybe I will post a new question during the next weeks ;-)
Thanks for the given help and clues!
Robert

Sign in to comment.

Categories

Find more on General Applications in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!