animation with the command show(robot) is very, very slow
10 次查看(过去 30 天)
显示 更早的评论
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?
回答(1 个)
Simar
2024-5-6
编辑:Simar
2024-5-13
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 -
- Animatedline- https://www.mathworks.com/help/matlab/ref/animatedline.html?searchHighlight=animated%20line&s_tid=srchtitle_support_results_3_animated%20line
- Profiler- https://www.mathworks.com/help/matlab/ref/profiler-app.html?searchHighlight=profiler&s_tid=srchtitle_support_results_2_profiler
- Profile code to improve performance- https://www.mathworks.com/help/matlab/matlab_prog/profiling-for-improving-performance.html?searchHighlight=profiler&s_tid=srchtitle_support_results_3_profiler
- Addpoints- https://www.mathworks.com/help/matlab/ref/addpoints.html?searchHighlight=addpoints&s_tid=srchtitle_support_results_1_addpoints
Hope this helps!
Best Regards,
Simar
7 个评论
Umar
2024-7-1
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!