Challenge

Week 2- 2R Robotic Arm Challenge

Aim:

Write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm

Program:

% 2R Robotic Arm Challenge

clear all
close all
clc

% Inputs

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(theta1)
        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.6 0 1.6])
        xlabel('x limit')
        ylabel('y limit')
        pause(0.2)
        M(ct) = getframe(gcf);
        ct = ct+1;

    end
end

movie(M)
videofile = VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Explanation:

l1 and l2 will be defined at first

theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);

theta 1 and theta 2 holds the angle of the links in 10 instances

for i= 1:length(theta1)
    THETA1 = theta1(i);
    for j= 1:length(theta1)
        THETA2 = theta2(j);
value of i and j we can intimates the each coordinates of link 2 for each link 1
        x0 = 0;
        y0 = 0;

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

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

formulas are used to find x and y coordinates for both links on different angles of link.

plot ( [x0 x1] , [y0 y1] , [x1 x2] , [y1 y2] , 'LineWidth', 3 )
        axis[-0.1 1.6 0 1.6]
        xlabel('x limit')
        ylabel('y limit')

plot command used to plot the links

axis command is used set x and y limit values while plotting

xlabel and ylabel is used to label x and y

ct = ct+1;

incrementing the link will be used to move the animation to next angle

movie(M)
videofile = VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

above commands will be used to write the plotted links to video

Errors faced:

In line 33

Instead of - axis([-0.1 1.6 0 1.6])

I have typed as - axis[-0.1 1.6 0 1.6]

and then solved

Youtube Video link:

https://youtu.be/Lk-hrOamUqU

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.