Challenge

Week 2- 2R Robotic Arm Challenge

% Animation of robotic arm

clear all
close all
clc

%Inputs
l1= 1;
l2= 0.5;
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);

%counter
ct=1

for i= 1:length(theta1)
    THETA1= theta1(i);
    for j= 1:length(theta2)
    THETA2= theta2(j);
    
    x0=0;
    y0=0;

     x1 = l1*cosd(THETA1);
     y1 = l1*sind(THETA1);

     x2= x1 + l2*cosd(THETA2);
     y2= y1 + l2*sind(THETA2);

     %Plotting
     plot([x0,x1],[y0,y1],[x1,x2],[y1,y2],'linewidth',3)
     axis([-0.1 1.5 0 1.5])
      M(ct) = getframe(gcf);
      ct = ct + 1;
     pause(0.03)
    end
    
end
movie(M)
videofile = VideoWriter('Forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Code Explanation

1. Start with clearing the Workspace, closing the plots if any, and clearing the commond window

2. Give inputs (Lengths or arms/links l1 and l2)

3. Provide various angles made by links with x-axis using linspace command. Took angles between 0 and 90 degree with a gap of 10 degrees.

4. For each angle/position of link/arm1, we need to show all the positions of link2...for each position of link1 we need to show 10 positions of link2.

5. So we need two for loops to achive above task

6. theta1, theta2 are arrays, where as THETA1, THETA2 represents single value.

7. Define the coordinates of link1 as (x0,y0) and (x1,y1). Link 2 as (x1,y1), (x2,y2)

8. We can now use plot command to plot lines joining these coordinates

9. limits to x axis and y axis are also set using axis command

10. As we need to create a animation of these plots, we need to save each plot in a frame. Here we using a counter (ct) to assign with each plot.

11. All plots are saved in Variable M using M(ct) = getframe(gcf) command

12. a pause of 0.03 sec is used to get the animation feel

13. Frames M are saved in a movie

14. Video file is created using videoWriter command

15. The Frames (M) are then written into Videofile using WriteVideo command

16. No errors were observed as i followed step by step procedure from the lecture.

The Youtube link to the animation uploaded Is https://youtu.be/lHelYTDNHZc

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.