Week 2- 2R Robotic Arm Challenge
Explanation:
Forward kinematics:
Forward kinematics refers to process of obtaining position and velocity of end effector, given the known joint angles and angular velocities.
For example, if shoulder and elbow joint angles are given for arm in sagittal plane, the goal is to find Cartesian coordinates of wrist/fist.
Coordinates are considered as follows:
For link/arm 1:
- Starting point (x0,y0)
x0=0,yo=0;
- Length of link1 is l1
- Angle with horizontal is theta1
- Ending point (x1,y1)
x1=l1*cos(theta1); y1=l1*sin(theta1)
For link/arm 2:
- Starting point (x1,y1)
- Length of link2 is l2
- Angle with horizontal is theta2
- Ending point (x2,y2)
x2=x1+l2*cos(theta2); y1=y1+l2*sin(theta2);
Development of MATLAB code:
- Link2 is attached to link1. Both links are freely turn within the workspace. Our aim is to obtain the end effector position in the workspace for various angles of link2 corresponding to the varying angles of link1.
% links
l1=1;
l2=0.5;
theta1=linspace(0,90,40);
theta2=linspace(0,45,40);
ct=1;
- For each angle of link1, link2 have multiple positions. So, for the complete turning of link1 various position of link2 is to be obtained. LOOP concept is used to obtain the various positions of link1 and link2.
% for loop is taken for the lenght of i
for i=1:length(theta1)
THETA1=theta1(i);
% for loop is taken for the length of j
for j=1:length(theta2)
THETA2=theta2(j);
x0=0;
y0=0;
x1=x0+cosd(THETA1);
y1=y0+sind(THETA1);
x2=x1+cosd(THETA2);
y2=y1+sind(THETA2);
% plotting of links on the xy plane
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3);
axis([-0.1 1.5 0.1 1.5])
pause(0.01)
M(ct)=getframe(gcf);
ct=ct+1;
end
end
- To capture the entire moment, we create a plot it runs for the entire loop condition. And it is store in a variable. Finally, we create a movie file to record the animation.
% to create animation
movie(M)
videofile=VideoWriter('roboticarm_animation.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
https://drive.google.com/file/d/1gt_fmwOJ991Ioea0KazUU_ySGfEQ1fq0/view?usp=sharing
clear all
close all
clc
% robotic arm animation
% links
l1=2;
l2=1;
theta1=linspace(0,90,40);
theta2=linspace(0,45,40);
ct=1;
% for loop is taken for the lenght of i
for i=1:length(theta1)
THETA1=theta1(i);
% for loop is taken for the length of j
for j=1:length(theta2)
THETA2=theta2(j);
x0=0;
y0=0;
x1=x0+cosd(THETA1);
y1=y0+sind(THETA1);
x2=x1+cosd(THETA2);
y2=y1+sind(THETA2);
% plotting of links on the xy plane
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3);
axis([-1 2 0.1 2])
pause(0.01)
M(ct)=getframe(gcf);
ct=ct+1;
end
end
% to create animation
movie(M)
videofile=VideoWriter('two_r_roboticarm_animation.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)