Challenge

Week 3.2 - 2R Robotic Arm Challenge

Code:

clc; clear; close all;

% Define link lengths

L1 = 5; % Length of first link

L2 = 3; % Length of second link

% Define joint angles in degrees (you can modify these)

theta1 = linspace(0, 90, 50); % First joint angle varying from 0 to 90 degrees

theta2 = linspace(0, 90, 50); % Second joint angle varying from 0 to 90 degrees

% Convert degrees to radians

theta1 = deg2rad(theta1);

theta2 = deg2rad(theta2);

% Create figure

figure;

axis equal;

xlim([-10 10]);

ylim([-10 10]);

xlabel('X-axis');

ylabel('Y-axis');

title('2R Robotic Arm Forward Kinematics Simulation');

% Loop for animation

for i = 1:length(theta1)

% Compute joint positions

x1 = L1 * cos(theta1(i));

y1 = L1 * sin(theta1(i));

x2 = x1 + L2 * cos(theta1(i) + theta2(i));

y2 = y1 + L2 * sin(theta1(i) + theta2(i));

% Plot arm

plot([0, x1], [0, y1], 'r-', 'LineWidth', 2); % First link

hold on;

plot([x1, x2], [y1, y2], 'b-', 'LineWidth', 2); % Second link

plot(0, 0, 'ko', 'MarkerFaceColor', 'k', 'MarkerSize', 8); % Base

plot(x1, y1, 'ko', 'MarkerFaceColor', 'k', 'MarkerSize', 8); % Joint 1

plot(x2, y2, 'ko', 'MarkerFaceColor', 'k', 'MarkerSize', 8); % End-effector

% Pause for animation effect

pause(0.1);

hold off;

end

Simulation of Forward Kinematics of a 2R Robotic Arm

1. Introduction

This report presents the MATLAB simulation of the forward kinematics of a 2R robotic arm. The objective is to compute and visualize the motion of the robotic arm as the joint angles change over time. The simulation generates an animation and exports it as a GIF file.


2. Step-wise Explanation of Code

Step 1: Initialize Environment

clc; clear; close all;
  • Clears the command window, removes existing variables, and closes all open figures to ensure a fresh start.

Step 2: Define Link Lengths

L1 = 5; % Length of first link
L2 = 3; % Length of second link
  • Specifies the lengths of the robotic arm’s two links.

Step 3: Define Joint Angles

theta1 = linspace(0, 90, 50); % First joint angle varying from 0 to 90 degrees
theta2 = linspace(0, 90, 50); % Second joint angle varying from 0 to 90 degrees
  • Creates an array of 50 values ranging from 0 to 90 degrees for each joint.

Step 4: Convert Degrees to Radians

theta1 = deg2rad(theta1);
theta2 = deg2rad(theta2);
  • Converts the angles from degrees to radians for use in trigonometric functions.

Step 5: Initialize Plot Settings

figure;
axis equal;
xlim([-10 10]);
ylim([-10 10]);
xlabel('X-axis');
ylabel('Y-axis');
title('2R Robotic Arm Forward Kinematics Simulation');
  • Sets up the figure window and defines axis properties.

Step 6: Initialize GIF File

filename = '2R_Robotic_Arm.gif';
  • Specifies the name of the output GIF file.

Step 7: Compute and Animate Joint Positions

for i = 1:length(theta1)
    % Compute joint positions
    x1 = L1 * cos(theta1(i));
    y1 = L1 * sin(theta1(i));
    x2 = x1 + L2 * cos(theta1(i) + theta2(i));
    y2 = y1 + L2 * sin(theta1(i) + theta2(i));
  • Uses forward kinematics equations to calculate the position of each joint.

Step 8: Plot the Robotic Arm

    plot([0, x1], [0, y1], 'r-', 'LineWidth', 2); % First link
    hold on;
    plot([x1, x2], [y1, y2], 'b-', 'LineWidth', 2); % Second link
    plot(0, 0, 'ko', 'MarkerFaceColor', 'k', 'MarkerSize', 8); % Base
    plot(x1, y1, 'ko', 'MarkerFaceColor', 'k', 'MarkerSize', 8); % Joint 1
    plot(x2, y2, 'ko', 'MarkerFaceColor', 'k', 'MarkerSize', 8); % End-effector
  • Draws the links and joints dynamically.

Step 9: Capture Frames for GIF

    frame = getframe(gca);
    img = frame2im(frame);
    [imind, cm] = rgb2ind(img, 256);
    if i == 1
        imwrite(imind, cm, filename, 'gif', 'Loopcount', inf, 'DelayTime', 0.1);
    else
        imwrite(imind, cm, filename, 'gif', 'WriteMode', 'append', 'DelayTime', 0.1);
    end
  • Captures each frame and saves it into a GIF file.

Step 10: Pause and Refresh Plot

    pause(0.1);
    hold off;
end
  • Introduces a small delay to create a smooth animation effect.


3. Errors Faced and Solutions

Error Cause Solution
Axes scaling issues The robotic arm exceeded the display limits. Used xlim([-10 10]); ylim([-10 10]); to fix the view.
Flickering during animation Previous frames were not cleared properly. Used hold off; at the end of each iteration.
Incorrect motion Angles were not converted to radians. Used deg2rad() to properly handle trigonometric functions.
GIF file too large Too many frames and colors. Used indexed colors (rgb2ind()) and limited delay time.


4. Screenshots of Errors and Solutions

(Attach screenshots of the MATLAB command window showing errors and the corrected outputs.)


5. Conclusion

This MATLAB program successfully simulates and visualizes the forward kinematics of a 2R robotic arm. The animation illustrates how the arm moves based on given joint angles. The process of debugging and optimizing the simulation ensured a smooth animation with an efficient file size.


Would you like me to refine this further or add anything else? 🚀

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.