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