How to generate bool type identifier 'true' and 'false' C code

22 vues (au cours des 30 derniers jours)
dongshan wang
dongshan wang le 22 Déc 2023
I got some Simulink.Parameter with data type boolean in Simulink model, and i m tring to generate C code with Code generater. But everytime i got code such as:
const Boolean_T c_flag = 1;
// or
const Boolean_T c_flag = 0;
however what i want is things like :
const Boolean_T c_flag = true;
// or
const Boolean_T c_flag = false;
// or
const Boolean_T c_flag = Boolean(1);
// or
const Boolean_T c_flag = Boolean(0);
i have tried to set the parameter value to '1', 'true', '=true' or even '=boolean(1)',but nothing works.
is that possible in Code Generater/

Réponses (2)

madhan ravi
madhan ravi le 22 Déc 2023
  4 commentaires
madhan ravi
madhan ravi le 22 Déc 2023
Could you show what you have tried and the generated code?
dongshan wang
dongshan wang le 25 Déc 2023
For some reasons i cnt not upload figures, anyway i will try to provide a detailed description about what i have tried.
First of all I set a <Simulink.Parameters> which named 'b_Flag', you can get a same variable using commands follow:
b_Flag = Simulink.Parameter;
b_Flag.Value = false;
b_Flag.DataType = 'boolean'
b_Flag.CoderInfo.StorageClass = 'Custom';
b_Flag.CoderInfo.CustomStorageClass = 'Const';
b_Flag.CoderInfo.CustomAttributes.HeaderFile = 'calibration.h';
b_Flag.CoderInfo.CustomAttributes.DefinitionFile = 'calibration.c';
I added that variable to a matlab example model (which you can find here) for a demonstration.
The generated code are as follow, there are two bool type Macros 'true' and 'false' in <rtwtypes.h>.
// File: rtwtypes.h
1 /*
2 * File: rtwtypes.h
3 *
4 * Code generated for Simulink model 'rtwdemo_roll'.
5 *
6 * Model version : 1.151
7 * Simulink Coder version : 9.1 (R2019a) 23-Nov-2018
8 * C/C++ source code generated on : Mon Dec 25 10:45:14 2023
9 */
10
11 #ifndef RTWTYPES_H
12 #define RTWTYPES_H
13
14 /* Logical type definitions */
15 #if (!defined(__cplusplus))
16 # ifndef false
17 # define false (0U)
18 # endif
19
20 # ifndef true
21 # define true (1U)
22 # endif
23 #endif
...
42 typedef signed char int8_T;
43 typedef unsigned char uint8_T;
44 typedef short int16_T;
45 typedef unsigned short uint16_T;
46 typedef int int32_T;
47 typedef unsigned int uint32_T;
48 typedef long long int64_T;
49 typedef unsigned long long uint64_T;
50 typedef float real32_T;
51 typedef double real64_T;
52
...
and the declaration in <calibration.h> looks just fine (line 18):
// File: calibration.h
1 /*
2 * File: calibration_public.h
3 *
4 * Code generated for Simulink model 'rtwdemo_roll'.
5 *
6 * Model version : 1.151
7 * Simulink Coder version : 9.1 (R2019a) 23-Nov-2018
8 * C/C++ source code generated on : Mon Dec 25 10:45:14 2023
9 */
10
11 #ifndef RTW_HEADER_calibration_public_h_
12 #define RTW_HEADER_calibration_public_h_
13 #include "rtwtypes.h"
14
15 /* Const memory section */
16 /* Exported data declaration */
17 /* Declaration for custom storage class: Const */
18 extern const boolean_T b_Flag;
19
20 #endif /* RTW_HEADER_calibration_public_h_ */
21
22 /*
23 * File trailer for generated code.
24 *
25 * [EOF]
26 */
27
However this variable is defined as '0' but bot 'false' (line 26). That's the problem has been bothering me all along, all i want is a line of code with identifier 'false'.
// File: calibration_public.c
1 /*
2 * File: calibration_public.c
3 *
4 * Code generated for Simulink model 'rtwdemo_roll'.
5 *
6 * Model version : 1.151
7 * Simulink Coder version : 9.1 (R2019a) 23-Nov-2018
8 * C/C++ source code generated on : Mon Dec 25 11:10:46 2023
9 *
10 * Target selection: ert.tlc
11 * Embedded hardware selection: Intel->x86-64 (Windows64)
12 * Code generation objectives:
13 * 1. Execution efficiency
14 * 2. Traceability
15 * 3. Safety precaution
16 * 4. RAM efficiency
17 * Validation result: Not run
18 */
19
20 #include "rtwdemo_roll.h"
21
22 /* Exported data definition */
23
24 /* Const memory section */
25 /* Definition for custom storage class: Const */
26 const boolean_T b_Flag = 1;
27
28 /*
29 * File trailer for generated code.
30 *
31 * [EOF]
32 */

Connectez-vous pour commenter.


Brandon Stevens
Brandon Stevens le 6 Août 2024
Please see the documentation page below for instructions on how to do this in Embedded Coder under the section titled "Boolean Identifiers"

Catégories

En savoir plus sur Simulink Coder dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by