Challenge

Week 3.2 - 2R Robotic Arm Challenge

Aim

To simulate the forward kinematics of a 2R Robotic arm

Objective

  • To understand and implement forward kinematics for a 2R robotic arm.
  • To visualise the motion of the robotic arm as the joint angles change.
  • To generate an animation of the arm's movement and save it as a videofile.

Methodology

  1. Define the length of the two links.
  2. Generate a range of angles for each joint.
  3. Compute the position of the robotic arm’s joints using trigonometric calculations.
  4. Plot and animate the robotic arm’s motion frame by frame.
  5. Capture and save the animation as a video file.

Code

% Code to simulate the forward kinematics of a 2R Robotic Arm

clear all

close all

clc

% Length of the arms
l1 = 1;
l2 = 0.5;
% Angle of the arms with respect to the x-axis
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);
% Setting Up counter for figures
ct = 1;
% For loop for providing the angles of link 1 with respect to the x-axis
for i = 1:length(theta1)
THETA1 = theta1(i);
% For loop for providing the angles of link 2 with respect to the link 1
for j = 1:length(theta2)
THETA2 = theta2(j);
% The base of the robotic arm
x_0 = 0;
y_0 = 0;
% The position of the first joint
x_1 = l1*cosd(THETA1);
y_1 = l1*sind(THETA1);
% The position of the second joint end
x_2 = x_1 + l2*cosd(THETA2);
y_2 = y_1 + l2*sind(THETA2);
% Figures for each frame
figure(1)
% Creating links
plot([x_0,x_1],[y_0,y_1],[x_1,x_2],[y_1,y_2],'LineWidth',3)
% Axis limits for better visualization.
axis([-0.1 1.5 0 1.5])
pause(0.03)
% Captures the current frame and stores it in M for animation.
M(ct) = getframe(gcf);
ct = ct+1;
end
end
% Creating a movie using the saved frames
movie(M)
% Creating a video file named Forward_Kinematics.avi using the frames
% stored
videofile = VideoWriter('Forward_Kinematics.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

Errors faced while programming

An issue arose where link two was getting longer and longer, eventually reaching the same length of the first link. After a step-by-step line check of the code, it was discovered that the length of link two was mistakenly used for the y-coordinate of the end of link one. Correcting this mistake ensured accurate link positioning and proper simulation.

Additionally, when creating figures, using a counter to store individual frames resulted in multiple figures being created, leading to increased execution time. By changing it to figure(1), the process became more efficient, reducing unnecessary figures and making it easier to access and update the figure during simulation.

Results

The simulation successfully visualises the movement of the 2R robotic arm as the joint angles vary. The trajectory and motion of the end effector demonstrate the expected kinematic behaviour. The captured frames are compiled into an animation, stored as a video file.

https://youtu.be/9eoSNlF9ugA

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.