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?
댓글 수: 2
답변 (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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Animation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!