To simulate the Forward Kinematics of a 2R Robotic Arm in MATLAB
Aim:
To write a program to simulate forward kinematics of a 2R robotic arm.
Objective:
- To perform forward kinematics on a robotic arm and simulate it’s workspace.
- To animate the motion of the robotic arm under the given set of conditions and save it as a video file.
Workflow:
Program Code
clear all
close all
clc
% program to perform forward kinematics of 2R robot arm
% Inputs
l1 = 1; % length of link 1 in m
l2 = 0.5; % length of link 2 in m
theta1 = linspace(0,90,10); % joint angle of link 1
theta2 = linspace(0,90,10); % joint angle of link 2
x0 = 0; % fixing the origin
y0 = 0;
ct = 1; % setting counter to 1
for i = 1:numel(theta1)
x1 = l1*cosd(theta1(i)); % calulating end co-ordinates of link 1
y1 = l1*sind(theta1(i));
for j = 1:numel(theta2)
x2 = x1 + l2*cosd(theta2(j)); % calculating end co-ordinates of link 2
y2 = y1 + l2*sind(theta2(j));
plot([x0,x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3); % plotting the links
axis([-0.1 1.6 -0.1 1.6]);
M(ct) = getframe(gcf); % capturing figure frame in M array
ct = ct+1;
end
end
movie(M); % saving animation as video file
videofile = VideoWriter('forward_kinematics.avi','uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Code Explanation:
1. Clearing workspace and setting input parameters like link length and angle range for the joints which can be changed as per the problem.
2. Fixing the origin for the first link.
3. Using for loop to calculate the end co-ordinates of the two links and plotting them.
4. The first loop fixes link 1 while the second loop iterates the different positions of link 1.
5. The counter is used to save the current figure frame in an array M.
6. Saving all the frames as a video file.
Errors:
Only typo errors were enountered which were resolved immediately.
Output:
The animation for links moving for different angular positions was obtained which was then saved as a video file attached in the link below.