Challenge

Week 2- 2R Robotic Arm Challenge

% Simulation of forward kinematics of a 2R robotic arm
close all
clear all
clc

% Inputs
l1 = 1; % length of link 1 in m
l2 = 1; % length of link 2 in m

theta1 = linspace(0,90,30); % angle of link 1 varying from 0 to 90 with 10 divisions
theta2 = linspace(0,90,30); % angle of link 2 varying from 0 to 90 with 10 divisions

ct = 1; % Counter variable

for i = 1:length(theta1); % Loop 1
    THETA1 = theta1(i); % THETA1 stores the value of theta1 based on the counter variable i
    for j = 1:length(theta2) % Loop 2
        THETA2 = theta2(j); % THETA2 stores the value of theta2 based on the counter variable j
        % Defining the coordinates
        x0 = 0;
        y0 = 0;
        x1 = l1*cosd(THETA1);
        y1 = l2*sind(THETA1);
        x2 = x1 + l2*cosd(THETA2);
        y2 = y1 + l2*sind(THETA2);
        
        % Plot
        plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',4);
        axis([-0.1 2 0 2]); % defining the axis
        pause(0.05) % to create a pause between the frames
        M(ct) = getframe(gcf); % Takes the frames of the plot and stores it in M
        ct = ct + 1;
    end
end

movie(M) % to create a movie based on the frames stored in M
video = VideoWriter('forward_kinematics_2R_robotic_arm','Uncompressed AVI') % to create a video file of the specified format

open(video)
writeVideo(video,M) % writes the frames in M into the file 'video'
close(video)
        

The above program is to simulate the forward kinematics of a 2R robotic arm. The robotic arm is assumed to move in an arbitrary x-y Coordinate system.

The necessary inputs for the program are:

  • Length of the links – There are two links here and their lengths are defined as 1m each respectively.
  • Angle of the links – The angle of the links is defined such that it varies from 0° to 90° with 30 divisions. The angle is made with the x-axis of the coordinate system. The more the divisions, better the final animation will be, as the motion can be visualized better.
  • A counter variable

To properly execute this program, two for loops are necessary. The first for loop is to show the variation of the angle of link 1 and the second one is to show the corresponding variation of the angle of link 2. Since the angles are defined with 10 divisions each, there will be around 100 combinations comprising of the different angle combinations.

As the first loop is executed the angle of link 1 is stored into a new variable THETA1 and then begins the second loop. In the second loop, the angle of link 2 is stored into THETA2, another variable and then begins the main part of the simulation.

In the second for loop, the coordinates representing the start and end positions of the links are initialised.

  • x0 and y0 represents the starting position of link 1.
  • x1 and y1 represents the ending position of link 1. Since the link 1 is at an angle THETA1, the corresponding end coordinates of link 1 will be ((l1*cos(THETA1),(l1*sin(THETA1)).
  • The start coordinates of l2 is same as the end coordinates of l1, which means that the starting position of l2 is nothing but x1 and y1.
  • x2 and y2 represents the ending position of link 2. It can be defined by adding the length of l2, which is at an angle THETA2 to the x-axis, to the start coordinates x1 and y1. So x2 = x1 + l2*cos(THETA2) and y2 = y1 + l2*sin(THETA2).

Now that the coordinates are fully defined, a plot of it can be made. Using the command ‘plot’ all the coordinates can be given in as input and a plot can be created. To get a good overview of how the robotic arm works, a simple plot of the coordinates isn’t enough as it won’t be able to show how the arm moves at different angles. Also obtaining the plot as separate figure is also not meaningful as there will be more than 100 figures for this simulation (it could be even more depending on how the angle is defined) and it will be difficult to comprehend how the arm moves, through all these plots.

In such a situation it is better to create an animation to show how the robotic arms move when the angle of its links changes. To create an animation what we do is overlap the figures that are obtained through the ‘plot’ command and then compile them together to create an animation. For that we first need to store each figure that the ‘plot’ command creates into a variable and then use it to create a movie. A ‘getframe(gcf)’ command is used to take the frames of the generated figures and these frames are stored in the variable M. Now using the command ‘movie(M)’, a movie based on the frames that are stored in variable M can be created. To create a movie, a video file needs to be created at first. This is done by using the command ‘VideoWriter(‘name’,’format’)’. This command creates a video file with a specified name and format. It is then assigned to a variable ‘video’. So ‘video’ now contains an actual video file. Now to add the captured frame to the video file, we must first open the video file (using ‘open(filename)’) and write the frames into the video file (using ‘writeVideo(filename,M)’) and finally close the video file (using ‘close(filename)’).

When the program is executed, it displays the movement of the robotic arm in the Matlab window and finally a video file will be created in the folder in which the program is saved. The output clearly shows the movement of the links when they are at specific angles and provides a much better visualisation of the motion than the stationary images that are normally obtained.

I have uploaded the video to google drive and it can be accessed through the following link.

Output Video: https://drive.google.com/file/d/1hu09ghAEJFoZbzMKE0X1y-9sVbsvKnMQ/view?usp=sharing

Errors faced:

When the program was run for first time, I got an error regarding the loop variable ‘i’. I had initialised the loop variable to run from 0 to the length(theta1). From the error message I understood that the indices in Matlab must be always a positive integer. I changed the start value of ‘i’ to 1 and the program ran smoothly. I have attached a screenshot of the error along with this report.

Inference:

Through this simulation I was able to get an idea about how an animation can be created using Matlab. I got an idea about the codes that are involved – How to take frames of the figures that are created (‘getframe()’), how to make a movie (‘movie()’), how to create a video file (‘VideoWriter()’) and how to add frames to it (‘videoWriter()’).

Another thing that I noted was the video that was created after the first execution lasted only for 3s and it was too fast. After having a chat with Rajkumar, I understood that by increasing the number of images that are used to create the animation, it can become smoother and run for a prolonged time. So, I increased the number of divisions of the angle to 50 (it was 10 at first) and the output video obtained was around 1 minute long. Then I reduced the number of angle divisions to 30 and it is now around 30s long and the pace of the video is enough to visualize the motion properly.

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.

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.