Assertion block does not stop simulation if I run the model with "sim" function
6 views (last 30 days)
Show older comments
Hi, I'm having issues with the Assertion block in Simulink when it comes to pause the current simulation.
Please refer to the following toy-example, which is enough to show the problem. Here, the assertion should be triggered after one second of simulation.
Here is what I'm trying to do:
- Run the simulation programmatically, using a Matlab script
- When a certain condition is met (here: after one seconds of simulation), pause the simulation in order to trigger the model callback "PauseFcn"
- After performing some operations within the callback, resume the simulation
Following the documentation, I configured the Assertion block in order to monitor the desired condition and pause the model. Specifically, I set the Assertion block callback to:
set_param(bdroot,'SimulationCommand','pause')
---------------------------------------
If I run the model using Simulink GUI, everything works fine: the model pauses after one second and the "PauseFcn" callabck is executed.
However, if I run the model programmatically using
MyOut = sim("MyModelName");
Then the following happens:
- The Assertion block triggers a warning after one second, as it should; the warning is dispayed in the Command Window
- Despite this, the simulation does not pause: it proceeds beyond one second.
- As an additional consequence, the model callback "PauseFcn" is not triggered (as confirmed by tracing the callbacks)
Do you have any suggestions?
I'm using Matlab R2024a
1 Comment
Umar
on 7 Jun 2024
When dealing with Assertion blocks in Simulink and trying to pause the simulation programmatically, there are a few considerations to ensure the desired behavior is achieved. Let's delve into some troubleshooting steps to address the issue you are facing.
1. Check Simulation Configuration
Ensure that the model configuration settings are correctly set up to allow for pausing the simulation. Verify that the model is configured to allow pauses during simulation and that the "PauseFcn" callback is properly defined.
2. Verify Assertion Block Configuration
Double-check the configuration of the Assertion block. Confirm that the parameters are set correctly to trigger the pause command after one second of simulation. Ensure that the Assertion block is set up to pause the simulation when the specified condition is met.
3. Debugging the Simulation
To troubleshoot the issue, you can add additional logging or display messages within the model callbacks to track the flow of execution. This can help in identifying where the problem lies and whether the "PauseFcn" callback is being called at all.
4. Simulation Command Execution
When running the simulation programmatically using sim("MyModelName"), ensure that the command to pause the simulation is executed at the right time. It's essential to synchronize the Assertion block's triggering of the pause command with the simulation execution.
5. Timing and Synchronization
Check the timing of events in your simulation. Ensure that the Assertion block's condition is evaluated at the correct time and that the pause command is issued promptly. Timing issues can lead to discrepancies in the simulation behavior.
6. MATLAB Version Compatibility
Since you mentioned using Matlab R2024a, ensure that there are no compatibility issues with the version you are using. Check for any updates or patches that might address similar problems with Assertion blocks or simulation pausing.
By following these troubleshooting steps and ensuring proper configuration of the Assertion block and simulation settings, you should be able to resolve the issue of the simulation not pausing as expected when running it programmatically. If the problem persists, further debugging and detailed analysis of the simulation flow may be necessary to pinpoint the root cause of the issue.
Answers (1)
Paul
on 8 Jun 2024
According to this doc page, pausing a simualtion while it's running is not supported when using the sim command.
If in your actual application the simulation is to be paused at a known time, it's possible that the new simulation object is what's needed.
If not, then need a bit more detail on what you're actually trying to do as there may be alternatives to the Assertion block, even if that was the best approach in the first place (though it sounds to me like it might not be).
See Also
Categories
Find more on Schedule Model Components in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!