How do I implement typedefs of unions + structure variables in Simulink?
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
How can I implement a union + structure variable in Simulink so that when I generate code I will get a variable structure with shared access?
I want to duplicate the behaviour of the C-code below so I can use typedef struct in several similar data sets.
%Code using C
typedef struct st_module_data{
struct st_FaultInfo{
union{
uint8 AllFaults;
struct {
uint8 OVP :1;
uint8 OVW :1;
uint8 UVP :1;
uint8 UVW :1;
uint8 OCP :1;
uint8 OCW :1;
uint8 OTP :1;
uint8 OTW :1;
}
}
}Fault;
struct st_MeasuredData{
uint8 voltage = 0;
uint8 current = 0;
uint8 temperature = 0;
}Measured;
};
st_module_data s_module_01;
s_module_01.Fault.AllFaults = 0; % Clears all flags
s_module_01.Fault.OVP = 1; % Sets only the OVP flag
s_module_01.Measured.voltage = ui8_ADC_5V; % Sets the measured voltage to 5V
I was able to recreate the structure and "typedef"-ish callbacks in matlab by using bus editor:
But I am unable to get the union thing working and can only access the lowest heirarchy of the structure - e.g.:
s_module_01.Fault.AllFaults = 0; % How do I implement this?
s_module_01.Fault = 0; % Does not work...
s_module_01.Fault.OVP = 1; % OK - works in Matlab Functions, Simulink and Stateflow
s_module_01.Measured.voltage = ui8_ADC_5V; % OK - works in Matlab Functions, Simulink and Stateflow
0 comentarios
Respuestas (1)
Prateekshya
el 27 de Sept. de 2023
Hi Paul,
As per my understanding you want to implement "union" data structure in Simulink. Simulink does not support direct use of such a data structure. However, there is some workaround available for the same. You can follow this link for more information: https://in.mathworks.com/matlabcentral/fileexchange/116520-use-union-datatype-in-simulink
I hope this helps!
1 comentario
Meghna
el 17 de Jul. de 2024
Could an example for the headerfile linked in this example be provided?
Ver también
Categorías
Más información sobre Simulink Coder en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!