Simulation of the motion of a Robotic Arm
AIM – Program to simulate the forward kinematics of a 2R Robotic Arm
Objective – 1. To write a program for the forward kinematics of a 2R Robotic Arm.
2. To create a simulation showing the motion of Robotic Arm.
Forward kinematics is used to determine the position and the motion of end-effector with the help of kinematics equations. The kinematics equations for the series chain of a robot are obtained using a rigid transformation to characterize the relative movement allowed at each joint and separate rigid transformation to define the dimensions of each link. For example if the object to be animated is an arm with the shoulder remaining at a fixed location, the location of the tip of the thumb would be calculated from the angles of the shoulder, elbow, wrist, thumb and knuckle joints.
Code –
%Inputs
l1 = 2;
l2 = 1;
theta1 = linspace(0,180,10);
theta2 = linspace(0,180,30);
ct=1;
for i = 1:length(theta1)
THETA1 = theta1(i);
for j = 1:length(theta2)
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([-3.0 3.0 0 6])
pause(0.01)
M(ct) = getframe(gcf);
ct=ct+1;
end
end
movie(M)
videofile = VideoWriter('forward_kinematic.avi','UNCOMPRESSED AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Explanation -
First of all, we put inputs which are known such as l1, l2,θ1(theta1) and θ2(theta2). We can easily find out the location of end-point with a simple code written below-
%Inputs
l1 = 2.0;
l2 = 1.0;
theta1 = [60];
theta2 = [30];
%coordinates
x0 = 0
y0 = 0
x1 = l1*cosd(theta1);
y1 = l1*sind(theta2);
x2 = x1 + l2*cosd(theta2);
y2 = y1 + l2*sind(theta2);
%Plotting
line([x0 x1],[y0 y1],'linewidth',3,'color','b')
line([x1 x2],[y1 y2],'linewidth',3,'color','r')
- But since we want a simulation we have to use the ‘for’ loop.
- In for loop, the loop counter variable (i) has values equal to the length of θ1(theta1). This means that the link 1 can take multiple positions. We denote the value of θ1(theta1) by ‘THETA1’.
- For every position of link 1, we want link 2 to have multiple positions too. Hence we need to use another ‘for’ loop inside the first ‘for’ loop.
- Then we introduced a new loop counter variable (j) which has values equal to the length of θ2(theta2) and denote the value of θ2(theta2) by ‘THETA2’.
- Then we write the equation for different coordinates and use plot command for line l1, l2. If we run the program now, we are going to get multiple lines of l1 and for each value of l1, we are going to get multiple lines for l2.
- So we use the counter(ct). Counter represents all the different frames which are going to be formed by both the links. Counter starts from 1 and goes all the way upto the no. of total combination means we are going to get different figures in only one plot one by one.
- Remember to use only one plot command as only the last plot command is going to be executed and also define the axis coordinates.
- We use the pause command with pause interval of 0.01 seconds. This results in an animation. Then to create a movie, we save all the frames in an array ‘M’ with the help of ‘getframe’ command.
- Now if we use movie(M), we will get the required simulation.
- Using command ‘VideoWriter’, we can dump all the frames into the videofile. Then we use ‘open’, ’writeVideo’ and ‘close’ command to run the simulation.
Result – The youtube link for the animation is written below –
Conclusion -
The Forward Kinematics is simple mechanism that contains the necessary calculations for accepting joint coordinates as input, and producing position and orientation parameters as output, for any of the segmental links in the chain. This code written in this report is only for 2 links but can be used similarly for more links.