How do I implement typedefs of unions + structure variables in Simulink?
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 StateflowHow 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 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 simulink, stateflow, simulink bus editor, structures, union MATLAB Answers — New Questions