Week 2- 2R Robotic Arm Challenge
A program to simulate the forward kinematics of a 2R Robotic Arm
Length of both arm has been given.
l1 = 1.5 meter
12 = 1 meter
Need to find out the starting & end points each arm using trigonometric function.
First arm:
Starting point of the first arm is fixed to the base, hence the starting coordinates of first arm will be (0,0).
x0 = 0
y0 = 0
Find out the end points of first arm using trigonometric function.
x1 = l1 cos(theta1)
y1 = 11 sin(theta1)
Assuming theta1 = 40 deg for testing purpose. where 'theta1' is the angle b/w base & first arm.
Test plotting first arm using line command.
Label the axis (x-axis, y-axis)
Test run: attachment (test_plot - 1st arm)
Second arm:
Starting points of the second arm will be same as end points of first arm.
Calculating end points of second arm
x2 = x1+ l2 cos(theta2)
y2 = y1+ l2 sin(theta2)
Assuming theta2 = 30 deg for testing purpose. where 'theta2' is the angle b/w base & second arm.
Test plotting both first arm & second arm using line command.
Test Run: attachment (test_run 2 - 1st & 2nd arm)
Changing the value of theta to an array using linspace command (0-90 deg, 10 angles) to get multiple positions of robotic arm to make animation.
Test run: incorrect output (attachment: test_run3 - error output)
Solution: need to use 'for loop' function to get the correct result with multiple values of theta.
Kept the coordinates inside the loop (i) and define variables called THETA1 & THETA2 for each value of (i).
Update the formulas with newly defined variables (THETA1 & THETA2).
Test run: unpresentable output (attachment: test_run 4)
Solution: program to be updated with sub-loop to get multiple position of 2nd arm for each position of 1st arm.
Define sub-loop (j) for the values of theta2
Test run: unpresentable output (attachment: test_run 5)
Solution: add 'figure' command and define variable 'ct' for each figure, and increment the value of 'ct' at the end of loop.
Reducing the number of values of theta to 3 for demonstration purpose.
Result: unpresentable output, received 9 individual figures (attachment: test_run 6)
Correction:
Changing the line command to Plot command
Remove command ‘figure’
Add 'hold on' command to keep all the generated plots
Add pause (0.3)
Test Run: unpresentable output, final frame displaying all the positions. (Attachment: test_run 7).
Correction:
Removing hold on command
Removing second plot command and make a single line command for both arms
Fixing the axis to make the output more presentable
Change the number of values of theta to 10 for demonstration purpose.
Change pause to 0.01
Test run: received animated figure output.
Making a movie out of received output
Define variable M(ct) to the current frame (note: to be defined before executing the incremental value of 'ct')
End of loop
Use command 'movie(M) to generate a movie
Create a videofile using 'videowriter' command with file name and file type
Open(videofile)
Create a movie using 'writevideo' command
close(videofile).
Error: (attachment: test_run 8, error output)
Videowritier command not working in Octave.
Solution: re-write the program with suitable command of octave.
Use 'sprintf' and 'saveas' command to save each output frames, inside the loop.
Test run: Each plots/ figure saved as individual png files, unpresentable. (Attachment: test run 9).
Solution: use any software to convert result png file into gif/video files.
Output: animation file generated
Youtube: https://youtu.be/MElKQsBj2sA
Drive: https://drive.google.com/file/d/1ltbvHQuxNwMWeECo2QQJQqDvRiM3iQj6/view?usp=sharing
%programme to simulate the forward kinematics of a 2R Robotic Arm
clear all
close all
clc
%inputs
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);
%l1 = length of first arm in meter
%l2 = length of second arm in meter
l1 = 1.5;
l2 = 1;
ct = 1;
for i = 1:length(theta1)
THETA1 = theta1(i);
for j = 1:length(theta2)
THETA2 = theta2(j);
%cooridnates
%the start point of the first arm will be fixed to base
x0 = 0;
y0 = 0;
%caculating end points of first arm
x1 = l1*cosd(THETA1);
y1 = l1*sind(THETA1);
%starting points of the second arm will be same as end points of first arm.
% calculating end points of second arm
x2 = x1+l2*cosd(THETA2);
y2 = y1+l2*sind(THETA2);
%Plotting
plot([x0 x1], [y0 y1],'linewidth',3, [x1 x2], [y1 y2],'linewidth',3)
axis([-0.1 3 0 3])
xlabel('X-axis')
ylabel('Y-axis')
pause(0.001)
file_text = sprintf('w2_challange%d.png',ct);
saveas(gca,file_text);
ct = ct+1;
end
end