Week 2- 2R Robotic Arm Challenge
Aim ;
To simulate the forward kinematics of a 2R Robotic arm
- Create an animation file of the plot.
` - Upload an animation on You tube and provide the You tube link for an animation in your report.
- You can also provide the google drive link.Note: Make sure that you provide the access to the video, while pasting the google drive link.
Equations used in the project ;
`y_1 = l_1 xx sintheta_1`,`x_1 = l_1 xx costheta_1` , `x_2 = x_1+l_2 xx costheta_2` , `y_2 = y_1+l_2 xx sintheta_2`
from trigonometric relations .here x1,y1,x2,y2, are x and y coordinates of the robotic arm linkages.
Objectives of the project:
- 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.
Body of the content:
We have used Matlab to write the code to simulate the forward kinematics of a 2R robotic arm, Matlab uses C++ in its interface.
The code to simulate the forward kinematics of a 2R robotic arm is given below
% program to simulate forward kinematics of a 2R robotic arm.
clear all
close all
clc
%inputs
l1=1;
l2=0.5;
ct=1;
%theta1 = t1
t1=linspace(0,90,10);
%theta2 = t2
t2=linspace(0,90,10);
% code to simulate the Forward kinematics of 2R robotic arm
for i=1:length(t1)
T1=t1(i);
for j=1:length(t2)
T2=t2(j);
%x,y coordinates of the robotic arm
x0=0;
y0=0;
x1=l1*cosd(T1);
y1=l1*sind(T1);
x2=x1+l2*cosd(T2);
y2=y1+l2*sind(T2);
%ploting
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3)
axis([-0.1 1.5 0 1.5])
pause(0.03)
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 of the code:
- We used a “ % ” to write a comment and we used “ clear all ” command to make sure that the work space is clear and we used “ close all ” command to make sure to close all plots and figures from previous run. And “ clc ” command to make sure the command window is clean.
- We have given the inputs “ l1 , l2 , t1 , t2 , ct ” where “ l1 ” and “ l2 ” are the length of the linkages 1 and 2 of the robotic arm. “ t1 ” ,” t2 ” are the angles made by the linkages 1 and 2. And “ ct ” is the counter variable used for getting individual frames of the result.
- While assigning values for t1 and t2 we used a command called “ linspace ” which generates an array of equally spaced elements between two limits.
- We used two for loops in the code so that we can obtain : different positions of link2 for the same position of link 1.
- In the 2nd for loops we took two temporary variables T1 and T2 to avoid confusions.
- We assigned the coordinates x0 , y0 , x1 , y1 , x2 , y2 of the linkages 1 and 2 inside the second loop.
- And we plot the positions of the two linkages by using the command “ plot ”
- The “axis” command generates an axis to the plot.
- We used pause command to delay the plot change so that it looks like an animation.
- To create an animation file we 1st made an array of different figure frames called “M” by using “getframe” command and used a counter variable “ct” to give an increment inside the loop.
- “movie” command converts all the frames into an animation. Then we created a video file specifying the tittle of the video file and type of the video file using the command “VideoWriter”.
- Then we opened the video file using the command “open” and we wrote the “M” animation into the video file converting the animation into a video file using “writeVideo” command
- And we closed the video file using “close” command
link for result:
https://drive.google.com/file/d/1EOoRyC5cC-tcOzdHoWxqwJwuXUebmp2s/view?usp=sharing
Errors faced while programming:
- I made syntax error while using “plot” command
- I made mistake while assigning t1 and t2 values inside the loop
- I made a syntax error while using the “movie” command.
Conclusion:
- To make the animation smoother we need more number of positions or theta values.
- Matlab can be used to simulate the forward kinematics of the 2R robotic arm.
- The more number of frames involved in the video the more time it takes to generate the video.