animation with the command show(robot) is very, very slow

Within a robotics class, in the control loop, I have the following code to render the robot movement:
show(robot,q_Gen,'PreservePlot',true,'Frames','off');
hold on
plot3(xd(1,1:i),xd(2,1:i),xd(3,1:i),'r')
axis([-.4 .4 -.4 .4 -.1 1])
view(45,35)
drawnow
hold off
The robot has been defined outside of the loop by the command
robot = loadrobot('kinovaGen3','DataFormat','row','Gravity',[0 0 -9.81]);
The animation is very, very slow in any o.s. and PC where it has been tried (Linux, Windows, MAC).
Is it a drawback that can not be avoided?

2 Commenti

Simulink is better for this kind of animation.
could you please elaborate this?

Accedi per commentare.

Risposte (1)

Simar
Simar il 6 Mag 2024
Modificato: Simar il 13 Mag 2024
Hi,
I understand that you are experiencing slow animation speeds across different operating systems and PCs, is not an unavoidable drawback.The performance issue stems from how rendering and updating of the robot's position are handled within the loop. Here are several strategies worth considering to improve the performance:
1. Reduce Rendering Frequency:
If the loop iterates very quickly, consider updating the plot less frequently. For example, only update the robot's position every nth iteration.
2. Optimize Plot Updates:
Instead of redrawing the entire plot in every iteration (which puts load on performance), update only the necessary elements. MATLAB allows you to modify existing plot objects. For instance, update the XData, YData, and ZData properties of the plot object instead of re-plotting it.
3. Use animatedline Function:
The animatedline function is more efficient for animations where one is appending data points in a loop. It is designed to handle dynamic updates more efficiently than redrawing the plot with plot3 in each iteration.
4. Limit the Complexity of the Scene:
Ensure that the scene being rendered is not overly complex. Simplifying the scene can significantly improve rendering times. This includes reducing the complexity of the robot model if possible.
5. Profiling and Optimization:
Use MATLAB profiler (profile on, profile viewer) to identify the bottlenecks in the code. There might be other parts in the script that are unexpectedly time-consuming.
6. Hardware Acceleration:
Ensure that MATLAB is using the hardware effectively. For instance, MATLAB can leverage GPU for certain operations, which might help in rendering complex scenes more efficiently.
Here is a small adjustment using animatedline for the plotting part of the code. This assumes xd contains the trajectory points you want to animate.
% Assuming xd is defined
h = animatedline('Color','r','LineWidth',2);
axis([-.4 .4 -.4 .4 -.1 1])
view(45,35)
for i = 1:size(xd, 2)
% Update robot position here
% show(robot, q_Gen(i,:), 'PreservePlot', true, 'Frames', 'off');
% Add points to animated line
addpoints(h, xd(1,i), xd(2,i), xd(3,i));
drawnow limitrate % This helps manage the update rate and improve performance
end
By optimizing code and using MATLAB more efficient functions for animations, one should be able to improve the rendering speed of robot's movements.
Please refer to the following documentation links -
Hope this helps!
Best Regards,
Simar

7 Commenti

Thanks for your reply. In my understanding:
  1. ok. it works
  2. I'm not really sure it is efficient code writing when resorting to an existing library such as the robotic toolbox. In such a case the more convinient is to simply call <show>
  3. see 2
  4. the scene is empty and the robot model is an existing one. I wouldn't like to enter in the code of an existing library
  5. Profiler already gave me the info that it was actually the command <show> the issue
  6. I should investigate this
thanks again,
g.
Hi G.A.
To optimize the animation and improve performance, we can make the following modifications to the code:
% Preallocate memory for xd to improve performance
xd = zeros(3, numIterations); % Assuming numIterations is the total number of iterations
% Move the 'show' and 'plot3' functions outside the loop for efficiency
show(robot, q_Gen, 'PreservePlot', true, 'Frames', 'off');
hold on
for i = 1:numIterations
% Update robot position or configuration
% Update xd values
xd(:, i) = [x_value; y_value; z_value]; % Update with actual values
% Plot the updated trajectory
plot3(xd(1, 1:i), xd(2, 1:i), xd(3, 1:i), 'r');
axis([-.4 .4 -.4 .4 -.1 1]);
view(45, 35);
drawnow;
end
hold off
By preallocating memory for xd and moving the show and plot3 functions outside the loop, we reduce unnecessary computations and improve the animation's efficiency. This optimization should help in achieving smoother and faster rendering of the robot movement.
Hope this will help resolve your problem.
Sorry for unformatted code
I think it should be more like
% Preallocate memory for xd to improve performance
xd = zeros(3, numIterations); % Assuming numIterations is the total number of iterations
% Move the 'show' and 'plot3' functions outside the loop for efficiency
show(robot, q_Gen, 'PreservePlot', true, 'Frames', 'off');
hold on
i = 1;
xd(:, i) = [x_value; y_value; z_value]; % Update with actual values
h = plot3(xd(1, 1:i), xd(2, 1:i), xd(3, 1:i), 'r');
axis([-.4 .4 -.4 .4 -.1 1]);
view(45, 35);
drawnow;
for i = 2:numIterations
% Update robot position or configuration
% Update xd values
xd(:, i) = [x_value; y_value; z_value]; % Update with actual values
% Plot the updated trajectory
set(h, 'XData', xd(1, 1:i), 'YData', xd(2, 1:i), 'ZData', xd(3, 1:i));
drawnow;
end
hold off
thanks
actually, having the command
showrobot
outside of the loop does not animate the robot... am I missing something?
Hi everyone,
I know it is none of my business to interfere but I could not help notice the show(robot, q_Gen, 'PreservePlot', true, 'Frames', 'off'); function is placed outside the loop but is not animating the robot as expected. So, would it be a better idea to move the show(robot, q_Gen, 'PreservePlot', true, 'Frames', 'off'); function inside the loop. This way, the robot will be animated at each iteration, showing its movement over time.
We are probably back to the problem of poor performance.

Accedi per commentare.

Categorie

Prodotti

Release

R2022a

Richiesto:

il 30 Apr 2024

Community Treasure Hunt

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

Start Hunting!

Translated by