Borrar filtros
Borrar filtros

Raising errors in Stateflow in accelerated model reference

13 visualizaciones (últimos 30 días)
Carl Banks
Carl Banks el 25 de Ag. de 2023
Comentada: Carl Banks el 7 de Dic. de 2023
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?

Respuesta aceptada

Ayush
Ayush el 1 de Dic. de 2023
Editada: Ayush el 1 de Dic. de 2023
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:
  • 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
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
  1 comentario
Carl Banks
Carl Banks el 7 de Dic. de 2023
Unfortunately, we can't really use this in general (though in some other cases we have used data stores to smuggle a status out of a deeply nested block inside a model reference), but it's a well thought out workaround so I'll accept it.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink Functions en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by