- An approach would be to leverage the MATLAB function block to output a signal/flag whenever a fatal condition is encountered by the Stateflow chart.
- In addition to this, an event listener script in MATLAB that monitors the simulation for a specific error flag to be raised/change in signal can be used.Please refer to the below documentation to know more about the “add_exec_event_listener” function: https://www.mathworks.com/help/simulink/slref/add_exec_event_listener.html
- Lastly detecting the change in the error flag from the Data Store Write block and stoping the simulation. Please refer to the below documentation to know more about the “Data Store Write” block: https://www.mathworks.com/help/simulink/slref/datastorewrite.html
Raising errors in Stateflow in accelerated model reference
13 次查看(过去 30 天)
显示 更早的评论
Okay, so in Stateflow and in Matlab Function blocks, you can cause the smiulation to terminate with an error message by calling the error() function in an action. I have used this from time-to-time to detect fatal conditions that shouldn't arise, it's not really for common user errors but more like a failsafe.
The issue is: when the Stateflow is built into an accelerated model reference, the call to error() is just flat-out ignored. (I'd expect no less from The Mathworks than to decide it's no big deal to just ignore calls to error() without even a warning. It's not like that error statement was protecting against a buffer overrun or anything, or that it crashed Matlab for no apparent reason, or that we spent a whole day trying to debug the issue. But whatever, that's not my question.)
We know not to rely on calling error() in Stateflow now, but we need a workaround.
The obvious solution is to have stateflow output a error_status flag, but then how to terminate the simulation with an error message?
- Assertion Block is the only way I know of, but we don't like this block for three reasons: 1. it can be disabled (since we're protecting against a Matlab crash it's not really a great idea to be able to disable it), 2.the error message it emits is not useful or customizable, and 3. the input to the assertion is considered a test point with no way to turn it off (so analyst will see all these assertion signals when reviewing data).
- The Stop block will terminate the simulation but produces no error message.
- I also tried using an S-function (figuring Mathworks can't ignore errors hand-coded in C, no matter how much they want to), but that has a limitation that the model reference has to be single-instance.
So, any other ideas?
0 个评论
采纳的回答
Ayush
2023-12-1
编辑:Ayush
2023-12-1
Hi Carl,
I understand that you want to terminate your simulations with an error message that is generated from a Stateflow chart contained inside a model reference.
Based on your description to rule out the Assertion Block, Stop Block and S-function due to their limitations, here is a possible workaround:
Here is a code snippet for your reference:
% Define the data store name
dataStoreName = 'errorFlagDataStore';
% Create the event listener
listener = add_exec_event_listener(bdroot, 'PostOutputs', @(src, event) checkErrorCondition(dataStoreName));
function checkErrorCondition(dataStoreName)
% Get the value of the data store
errorFlag = evalin('base', dataStoreName);
% If the error flag is set, stop the simulation and throw an error
if errorFlag
set_param(<Parent Model Name>, 'SimulationCommand', 'stop');
error('A fatal error condition was detected.');
end
end
A “Data Store Write” block is used to write the error flag and make it accessible in the MATLAB workspace for the event listener to perform the necessary actions based on the error condition. It then terminates the simulation by throwing the desired error message.
Hope it helps,
Regards,
Ayush Misra
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!