2R Robotic Arm Simulation using MATLAB
Code and Description:
The project involves simulating a 2-joint robotic arm.
% Lengths
l1 = 1;
l2 = 0.5;
% Angles
ang1 = linspace (0, 90, 20);
ang2 = linspace (0, 90, 20);
count = 1;
% loop
for i = 1:length(ang1)
ANG1 = ang1(i);
for j = 1:length(ang2)
ANG2 = ang2(j);
x0 = 0;
y0 = 0;
x1 = l1*cosd(ANG1);
y1 = l1*sind(ANG1);
x2 = x1 + l2*cosd(ANG2);
y2 = y1 + l2*sind(ANG2);
% figure(count)
% hold on
plot([x0 x1],[y0 y1],'linewidth',3,'color','r',[x1 x2], [y1 y2], 'linewidth',2,'color','b')
axis([-0.5 1.5 0 1.5])
pause (0.03)
file_text = sprintf('img%d.png',count);
saveas(gca,file_text)
count = count + 1;
endfor
endfor
Firstly, we are going to run the simulation for positions of the arm at angles from 0 to 90 degrees. Lengths are taken as 1 and 0.5.
Angle 1 is the angle of the base link 1 and angle 2 is that of link 2, connected to the manipulator. We are simulating positions at every instance of angle 1 of link 1.
The inside nested loop simulates link 2, that is, the manipulator-connected-link, by plotting it's position at different inclinations for a single inclination of link 1. The positions are calculated and lines are plotted, which become the arms.
Animation is then saved in octave using sprintf to print multiple images, and then an mp4 video is compiled using imagemagick:
magick img%d.png[1-400] 2RRobotArmAnim.mp4
(400 frames were generated and then compiled)
Errors and Correction:
Below are some errors:
1. Length error: took the wrong length
Solution: changed l2 to l1, since the points x1 and y1 correspond to length of link 1.
2. "Hold on" command in effect; superimposed frames:
Solution: Removed "hold on" so plot is overwritten everytime.
3. Incorrect axis command:
Solution: hold on removed and axis adjusted to push the plot forward.
Below is the animation of the code.