Challenge

Week 2- 2R Robotic Arm Challenge

Objective: Create animation of the plot of 2R robotic arm

code:

clear all
close all
clc

l1=1;
l2=0.5;

theta1=linspace(0,90,10);
theta2=linspace(0,90,10);
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);
        
        plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',4)
        pause(0.2)
        m(ct)=getframe(gcf);
        ct=ct+1;
    end
end
movie(m)
videofile = VideoWriter('arm.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,m)
close(videofile)

Explanation of code:

  • clear all to clear variables in workspace
  • close all to close all plots
  • clc to clear command window
  • enter the lengths of two arms
  • theta1 and theta2 arrays are created by using linspace command with 10 divisions from 0 to 90degree.
  • then counter is created
  • for two arms robot , second arm's all position need to be plot for one position of first arm.
  • therefore we have to use for loop inside for loop.
  • In first for loop i variable is used to index each time running for loop
  • again new THETA1 variable is defind which will take i index value of array theta1
  • THETA2 variable is defind which will take j index value of array theta2
  • In second for loop all position for respective angles is calculated by defining x0,x1,x2,y0,y1 and y2.
  • plot command is used to plot respective positions for respective angles
  • pause function is used
  • m array is created of current figures by using getframe function of respective counter
  • counter is indexed to store further figures in array
  • both the loops are closed
  • movie of m array is created
  • videofile is created using VideoWriter function
  • Videofile is opend
  • videofile is being written by array of figures
  • videofile is closed

Errors:

  • Cannot find a exact case sensitive match for VideoWriter.m
  • mistakes like not using the variables as they are defined

Solution for errors:

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.