Run an animation during simulation in Simulink purely via MATLAB

26 vues (au cours des 30 derniers jours)
Christos
Christos le 7 Nov 2024 à 19:38
Commenté : Christos le 8 Nov 2024 à 9:06
Hi,
I am trying to run an animation (which does not need any Simulink data) when the simulation status of a model is "running".
I am aware of the callback function StartFcn but this only runs during the initialization phase and not during the running phase.
I am also aware that I could run the animation by creating a MATLAB function within simulink and update the animation as the simulink model is running.
But I do not want this setup. I want to find out if I can invoke a MATLAB function outside simulink with a while loop using the simulink's simulation running status.
I tried parfeval but I cannot make it work because I believe I need to invoke the background process somehow within the simulink model (which I do not want). Then I found out about the timer function, but I am not sure if it can work in this setup either (or I cannot think of how to make it run).
Is what I ask possible? Or we always need to invoke the animation within the simulink model?
Many thanks.

Réponse acceptée

Shubham
Shubham le 8 Nov 2024 à 8:39
Hi Christos,
To run the animation while the simulation status of model is running, using a MATLAB function outside Simulink, you can follow these steps:
  1. Create a timer object that checks the simulation status at regular intervals.
  2. Check the simulation status using get_param to see if the model's status is "running."
  3. Execute animation code if the model is running
Here's an example of how to implement this:
% Define the animation function
function runAnimation()
% Your animation code here
disp('Running animation...');
end
% Timer callback function
function checkSimulationStatus(timerObj, ~)
modelName = 'your_model_name'; % Replace with your model's name
status = get_param(modelName, 'SimulationStatus');
if strcmp(status, 'running')
runAnimation();
end
end
% Set up the timer
animationTimer = timer('ExecutionMode', 'fixedRate', ...
'Period', 1, ... % Check every second
'TimerFcn', @checkSimulationStatus);
% Start the timer
start(animationTimer);
% Remember to stop the timer once the simulation ends
% stop(animationTimer);
% delete(animationTimer);
This setup allows you to run an external animation without embedding it into the Simulink model.
For more information, refer to the following documentation links:
Hope this helps.
  1 commentaire
Christos
Christos le 8 Nov 2024 à 9:06
Thank you Shubham.
I made it work with the timer function.
A few comments for people that might want to replicate this:
  • Set up the timer in the StartFcn Simulink's callback function.
  • Don't create the animation function without extra arguments because they are needed from the timer function (i.e. runAnimation(~,~)).
  • Stop and delete the timer in the StopFcn callback function of Simulink.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simulink Functions dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by