Challenge

Week 2- 2R Robotic Arm Challenge

Aim:

Write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm, as shown in the video.

  • Create an animation file of the plot.

What is forward kinematics ?

Forward kinematics refers to the use of the kinematic equations of a robot to compute the position of the end-effector from specified values for the joint parameters.

The kinematics equations of the robot are used in robotics, computer games and animation. The reverse process that computes the joint parameters that achieve a specified position of the end-effector is known as inverse kinematics.

Here we have to create a program to simulate the forward kinematics of a 2R Robotic Arm, where 2R refers to 2 rotating links.

Theory:

Here a schematic representation of robotic arm with 2 links of length l1 and l2 is given

And the angles of both links l1 and l2 are known theta1 and theta 2 respectively

There two steps to find the position of links

Step 1

Two end points

  1. one at the base ==> (x0,y0)
  2. one joining the link2 ==> (x1,y1)

Coordinates

  1. (x0,y0) = (0,0)
  2. (x1,y1) = (l1*cos(theta1), l1*sin(theta1))

Step 2

Two end points

  1. one joining the link2 ==> (x1,y1)
  2. one joining the manipulator ==> (x2,y2)

Coordinates

  1. (x1,y1) = (l1*cos(theta1), l1*sin(theta1))
  2. (x2,y2) = (x1+l2*cos(theta2), y1+l2*sin(theta2))

Now we have all 3 Co-ordinates

(x0,y0) (x1,y1) (x2,y2)

## Code for simulating the Forward Kinematics of a 2R Robotic Arm ##

% Code for the Forward Kynamatics of a Robotic Arm Animation
clear all
close all
clc
% Inputs
% Let the length of the link arms be
l1 =3;
l2 =2;
% Both link arms rotate upto 90 degree hence the angle of the links are 0 to 90 degree
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);​
% Adding the counter (ct) for the for loop so thet we can get all the frames of plot
ct = 1;
% give the range of angles for link 1
% give the range of angles for link 2
for i = 1:length(theta1)
THETA1 = theta1(i);
for j = 1:length(theta2)
THETA2 = theta2(j);​
% Give the coordinates for node (x0,y0)
% Give the coordinates for node (x1,y1)
% Give the coordinates for node (x2,y2)
x0 = 0;
y0 = 0;
x1 = l1*cosd(THETA1);
y1 = l1*sind(THETA1);
x2 = x1 + l2*cosd(THETA2);
y2 = y1 + l2*sind(THETA2);​
% Graph
% plot graph with the coordinates
% specify axis length
% use pause to get a smooth animation
% use getframe to store the plot frames in a variable (M)
% make the counter ct to advance in for loop
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],LineWidth=3,Marker="o")
axis([-1 6 -1 6])
pause(0.002)
M(ct) = getframe(gcf);
ct = ct+1;​
% End the loop
end
end
% Make a animated moveiof the frames inside of M using movie
% Use VideoWriter to creat a video file in avi formate
% open videofile
% write/insert the frames of M in videofile
% close videofile
movie(M)
videofile_robtic_arm = VideoWriter('videofile_robotic_arm.avi','Uncompressed AVI');
open(videofile_robtic_arm)
writeVideo(videofile_robtic_arm,M)
close(videofile_robtic_arm)​
Result:
We get an smooth animated video of simulation the Forward Kinematics of a 2R Robotic Arm
youtube link is given below for the video
Errors faced:
1. For theta the cos and sin are calculated in radian by defaut. So, we should use cosd(theta) for code so that we can get values in degrees
2. VideoWriter, writeVideo, etc commands are all case sensitive. Use the correct syntax and letters
Youtube link for the video :https://youtu.be/JdzZzEK61FE
1 comment
  1. Shaik FarazWow great!! Very good job Self ME
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.