Challenge

Week 2- 2R Robotic Arm Challenge

In this project, a simple code to create an animation of a 2R robotic arm will be created in MATLAB.

A 2R robotic arm consists of a base, two arms and two rotational joints. Arm 1 is attached with the help of a rotational joint. Arm 1 makes an angle of `theta_1` with the horizontal axis and has a length of `L_1`. The other end of arm 1 contains another rotational joint, which houses the end of arm 2. Arm 2 makes an angle of `theta_2` with the horizontal axis and has a length of `L_2`. A simplified illustration is below [1]:

The fixed end of arm 1 has the coordinates `x_0`, `y_0`, the moving end of arm 1/rotating joint 2 has the coordinates `x_1`, `y_1`, while the furthest end of arm 2 has the coordinates `x_2`, `y_2`.

The goal of this project is to calculate `x_0`, `y_0`, `x_1`, `y_1`,`x_2`,`y_2`, and to create a pleasing animation of the area of effect of the robotic arm.

Using basic trignometry, it can be calculated:

`x_1 = x_0 + L_1 * cos theta_1`

`y_1 = y_0 + L_1 * sin theta_1`

`x_2 = x_1 + L_2 * cos theta_2`

`y_2 = y_1 + L_2 * sin theta_2`

Code (with explanation)

clear all
close all
clc

% input data
l1 = 1; % length of arm 1 (m)
l2 = 0.5; % length of arm 2 (m)
theta1 = linspace(0, 90, 31); % angles of rotation for arm 1, starting from 0 to 90 in increments of 3 degrees
theta2 = linspace(0, 90, 31); % angles of rotation for arm 2, starting from 0 to 90 in increments of 3 degrees

% base of arm 1 (origin)
x0 = 0; y0 = 0;

% counter variable to count number of frames for animation
ct = 1;

for i = 1:length(theta1)
    % for loop to calculate end points of arm 1 for each angle of arm 1
    x1 = x0 + l1 * cosd(theta1(i));
    y1 = y0 + l1 * sind(theta1(i));

    for j = 1:length(theta2)
        % for loop to calculate end points of arm 2 for each angle of arm
        % 2, based on arm 1's current end points
        x2 = x1 + l2*cosd(theta2(j));
        y2 = y1 + l2*sind(theta2(j));
     
        % plotting the arms
        figure(1)

        % plotting arm 1 with red colour, and a line width of 3 in the
        % prescribed axis
        axis([-0.1 1.6 0 1.6])
        plot([x0 x1],[y0 y1],'linewidth',3,'color','r') 
        hold on % to prevent erasure of previous plot

        % plotting arm 2 with blue colour, and a line width of 3 in the
        % prescribed axis
        axis([-0.1 1.6 0 1.6])
        plot([x1 x2],[y1 y2],'linewidth',3,'color','b')

        % delay of 0.03 to create a smooth animation
        pause (0.03)

        % storing current figure frame into variable M at counter variable
        % location
        M(ct)= getframe(gcf);

        % incrementing counter variable so M can store current figure frame
        % for next iteration
        ct=ct+1;

        % clearing current figure so next iteration figure can be plotted
        % on a clear figure
        clf
    end
end

% creating variable videofile with file name and file format
videofile = VideoWriter('foward_kinematics.avi','Uncompressed AVI');

% opening videofile, and writing figure frames for each iteration stored in
% variable M into videofile and closing videofile.
open(videofile)
writeVideo(videofile,M)
close(videofile)

Output

Animation: https://youtu.be/1y4YOaeHwR0

Errors Encountered

Steps taken to fix errors

Movie(M) was removed since the default MATLAB video player displayed the video at an abysmal frame rate, causing the video to play for a long time. Instead, the generated animation was viewed in a separate video player.

References

1. A Two-Link Robot Manipulator: Simulation and Control Design by Mahboub Baccouch and Stephen Dodds http://doi.org/10.35840/2631-5106/4128

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.