How to make a user input working with Simulink model generated code

9 Ansichten (letzte 30 Tage)
David Vincon
David Vincon am 1 Jul. 2017
Hi,
I want to generate code out of a really simple Simulink model and use it with Microsoft Visual Studio.
So, what I managed, was creating the code and open the .sln-File in MVS.
My problems are the following:
  1. It is hart to read the generated Code and all the files, is it possible to make a packaging which is more easy?
  2. I tried to implement my code in the model.c-File (I think this is the main File), but wasn't able to include the "using namespace std". The error "using" not defined appears, but it isn't possible to include iostream, too.
  3. How can I manage to cin information and cout the result? Should I create an extra .c-File with all my input and output staff? But then how can I give the information to my model.c?
  4. As you can see on the picture, I used "constants" and an "out", is this the right way?
I used:
  • grt.tlc Create Visual C/C++ Solution File for Simulink Coder
  • Generate code only
  • Template makefile: RTW.MSVCBuild
  • Make command: make_rtw DEBUG_BUILD=1
  • Code interface packaging: Nonreusable function
  • Simulation time: inf
  • It was just possible to generate C-Code, but I work with a c++-File in MVS

Antworten (2)

Abhisek Roy
Abhisek Roy am 4 Jul. 2017
Hi David,
Code customization is not possible with grt.tlc and it does not generate optimized code. If you have Embedded Coder, you can use ert.tlc and you will be able to customize the code e.g. use templates, code styles, code placements etc and also the code will be optimized for the target you specify.
I completely agree with you that generated codes are difficult to understand. Here, I can provide you few pointers to where to look for the main algorithm and few other main aspects-
  • (model_name).c contains the main algorithm and (model_name).h will declare the variables as extern variables and also they will be global variables. The variables will be in form of structures. For example, I have test_U as my input variables and it has fields In1 and In2 and their data type is real_T. Same goes for the output. So this will tell you about the input and output datatypes.
  • Now to know what real_T datatype is, you can go to 'builtin_typeid_types.h' header file and it contains the definition of them. You will see some thing like follows -
/* Enumeration of built-in data types */
typedef enum {
SS_DOUBLE = 0, /* real_T */
SS_SINGLE = 1, /* real32_T */
...
} BuiltInDTypeId;
This tells that real_T is actually double datatype.
So now that we know where to look for the input, output and the datatypes, let's consider the actual problem. Here you need to change the constant1 and constant2 to Inports. If you change them to Inports e.g. In1 and In2, they were appear in the generated code as test_U.In1 and test_U.In2 and we will be able to modify them.
If you look at the (model_name).c file, you will notice the following structure -
  • void (model_name)_step(void) : Step function, which will be called at every time step. This contains the main dynamics of the model.
  • void (model_name)_initialize(void) : Initialization function, will be called only once to initialize all of the variables.
  • void (model_name)_terminate(void) : cleans up everything.
So, (model_name)_step(void) function is the function where we can read inputs from external sources at every time step and produce output. In the (model_name).c you will notice the following code which adds the inputs-
test_Y.Out1 = test_U.In1 + test_U.In2;
We can read inputs here. This can be modified as follows,
scanf("%lf %lf",&(test_U.In1),&(test_U.In2));
printf("Input: %lf %lf\n",test_U.In1,test_U.In2);
test_Y.Out1 = test_U.In1 + test_U.In2;
printf("Ouput: %lf\n",test_Y.Out1);
I have used '%lf' as I wanted to use double datatype. If you want, you can use any other datatype as you wish. Now you can call (model_name).bat to compile and build the whole code and it will give you (model_name).exe. You don't have to include basic iostream header files as they are already taken care. Once you run this executable, you will see that it will prompt you to provide input for all the time steps in one go and once you give that, you will see the output. For my ease, I gave time step as 0.2 and stop time as 0.2. This will run the code for 2 times. Follows my command prompt -
>> !test.exe
1.2 2.3
1.2 3.4
** starting the model **
Input: 1.200000 2.300000
Ouput: 3.500000
Input: 1.200000 3.400000
Ouput: 4.600000
Hope this gives you a starting point to explore.
Regards,
Abhisek
  1 Kommentar
Rakshanda Narwate
Rakshanda Narwate am 26 Sep. 2023
Hello Abhishek,
I also have similar usecase to achieve , Through visual studio the C code I want to simulate inputs of the Matlab Simulink model.
Currently I have followed all the steps below :
1.Developed MATLAB Simulink model
2. Generated C code using Embedded coder module
3. Browsed the generated C code in the Microsoft visual studio code tool and I have edited the C code by adding the scan f statements for all the user inputs
now I'm able to run and execute the C code in the visual studio but I'm unable to simulate the MATLAB simulink model inputs itself how do I make my edited C code interractable with my simulink model in simulating input values ?
It would be great if you could kindly guide me through the steps.

Melden Sie sich an, um zu kommentieren.


David Vincon
David Vincon am 4 Jul. 2017
Bearbeitet: David Vincon am 4 Jul. 2017
Thanks for this answer. But, if I got you right, using things like "cin" "cout" are only possible with Code generated by embedded coder? Because, if I generate code with the simulink coder, it is not possible to implement Code into the void XXX_step(void).
  1 Kommentar
Abhisek Roy
Abhisek Roy am 4 Jul. 2017
No, you can do it even if they are generated using grt.tlc. I had generated code using grt.tlc for this model and edited the (model_name).c as mentioned above in Notepad and then compiled it using the generated (model_name).bat file which will be in the generated code folder. These are Windows commands and does not depend on MATLAB.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Simulink Coder finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by