Run an animation during simulation in Simulink purely via MATLAB

26 visualizaciones (últimos 30 días)
Christos
Christos el 7 de Nov. de 2024 a las 19:38
Comentada: Christos el 8 de Nov. de 2024 a las 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.

Respuesta aceptada

Shubham
Shubham el 8 de Nov. de 2024 a las 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 comentario
Christos
Christos el 8 de Nov. de 2024 a las 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.

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

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by