Robotices_report
Robotices_report
Robotics project
MATLAB codes
• Forward
clear all;
clc;
l0 = 0.09;
l1 = 0.12;
l2 = 0.118;
while(1)
B1=thetamat(1)
B2=thetamat(2)
B3=thetamat(3)
end
6
• Inverse kinematics
while true
% Input target coordinates
X = input('Enter the X coordinate: ');
Y = input('Enter the Y coordinate: ');
Z = input('Enter the Z coordinate: ');
k1 = L2 + L3 * cosd(Theta_3_inv);
k2 = L3 * sind(Theta_3_inv);
Theta_2_inv = Gamma - atan2d(k2, k1);
end
• Circular path
fclose(serial("COM4"));
clear;
a = arduino('COM4', 'Mega2560', 'Libraries', 'Servo');
s1 = servo(a, 'D5'); % base
s2 = servo(a, 'D7'); % shoulder
s3 = servo(a, 'D6'); % wrist
% Link lengths
a1 = 4;
a2 = 12;
a3 = 11.8;
while true
% Loop over angle t from 0 to 2*pi
for t = 0:0.05:2*pi % Smaller step size for smoother circle
% Calculate the current target point on the circle
x = R * cos(t) + a_center;
y = R * sin(t) + b_center;
z = 10; % Z height (constant for a flat circle)
% Adjust angles and ensure they are within valid servo ranges
angle1 = theta1 / 180;
angle2 = (theta2 + 40) / 180;
angle3 = (theta3 - 45) / 180;
end
end
• square on floor
• clear;
• clc;
•
• % Initialize Arduino and Servo Connections
• a = arduino('COM4', 'Mega2560', 'Libraries', 'Servo');
•
• base = servo(a, 'D5');
• shoulder = servo(a, 'D7');
• effector = servo(a, 'D6');
• writePosition(base,10/180)
• pause(1)
• writePosition(shoulder,40/180)
• pause(1)
• writePosition(effector,150/180)
•
• % Arm Segment Lengths
• L1 = 9; % Length of link 1
• L2 = 12; % Length of link 2
• L3 = 11.8; % Length of link 3
•
• % Set of target coordinates
• coordinates = [
• -2.3913, -0.42165, 14.6644;
• -2.2818, -0.83049, 14.6644;
• -2.2818, -0.83049, 18.7625;
• -2.3913, -0.42165, 18.7625
• ];
•
• % Loop over each set of coordinates
• while 1
• for i = 1:size(coordinates, 1)
• X = coordinates(i, 1);
• Y = coordinates(i, 2);
• Z = coordinates(i, 3);
•
• % Inverse kinematics
• s = Z - L1;
• r = sqrt(X^2 + Y^2);
• K = sqrt(r^2 + s^2);
• D = ((K^2) - (L2^2) - (L3^2)) / (-2 * L2 * L3);
• Beta = atan2d(sqrt(1 - D^2), D);
•
• if (r^2 + s^2) > (L2 + L3)^2 || (r^2 + s^2) < (abs(L2 - L3))^2
• error('Target position unreachable with given link lengths');
10
• end
•
• Gamma = atand(s / r);
•
• % Inverse kinematics output
• Theta_1_inv = atan2d(Y, X);
• Theta_3_inv = 180 - Beta;
•
• k1 = L2 + L3 * cosd(Theta_3_inv);
• k2 = L3 * sind(Theta_3_inv);
• Theta_2_inv = Gamma - atan2d(k2, k1);
•
• % Adjusting for servo position range
• Theta_1_inv = Theta_1_inv + 180;
•
• % Display the calculated angles
• disp(['Coordinate set ', num2str(i), ':']);
• disp(['Theta_1 (Base Angle) = ', num2str(Theta_1_inv), ' degrees']);
• disp(['Theta_3 (Effector Angle) = ', num2str(Theta_3_inv), ' degrees']);
• disp('------------------------------');
•
• % Writing Servo Positions (Convert angles to the [0, 1] range for servos)
• writePosition(base, Theta_1_inv / 180);
• writePosition(effector, Theta_3_inv / 180);
•
• % Pause to allow motion
• pause(1);
• end
• end
• square on wall
• clear;
• clc;
•
• % Initialize Arduino and Servo Connections
• a = arduino('COM4', 'Mega2560', 'Libraries', 'Servo');
•
• base = servo(a, 'D5');
• shoulder = servo(a, 'D7');
• effector = servo(a, 'D6');
• writePosition(base,10/180)
• pause(1)
• writePosition(shoulder,120/180)
• pause(1)
• writePosition(effector,150/180)
•
• % Arm Segment Lengths
11
• L1 = 9; % Length of link 1
• L2 = 12; % Length of link 2
• L3 = 11.8; % Length of link 3
•
• % Set of target coordinates
• coordinates = [
• -2.3913, -0.42165, 14.6644;
• -2.2818, -0.83049, 14.6644;
• -2.2818, -0.83049, 18.7625;
• -2.3913, -0.42165, 18.7625
• ];
•
• % Loop over each set of coordinates
• while 1
• for i = 1:size(coordinates, 1)
• X = coordinates(i, 1);
• Y = coordinates(i, 2);
• Z = coordinates(i, 3);
•
• % Inverse kinematics
• s = Z - L1;
• r = sqrt(X^2 + Y^2);
• K = sqrt(r^2 + s^2);
• D = ((K^2) - (L2^2) - (L3^2)) / (-2 * L2 * L3);
• Beta = atan2d(sqrt(1 - D^2), D);
•
• if (r^2 + s^2) > (L2 + L3)^2 || (r^2 + s^2) < (abs(L2 - L3))^2
• error('Target position unreachable with given link lengths');
• end
•
• Gamma = atand(s / r);
•
• % Inverse kinematics output
• Theta_1_inv = atan2d(Y, X);
• Theta_3_inv = 180 - Beta;
•
• k1 = L2 + L3 * cosd(Theta_3_inv);
• k2 = L3 * sind(Theta_3_inv);
• Theta_2_inv = Gamma - atan2d(k2, k1);
•
• % Adjusting for servo position range
• Theta_1_inv = Theta_1_inv + 180;
•
• % Display the calculated angles
• disp(['Coordinate set ', num2str(i), ':']);
• disp(['Theta_1 (Base Angle) = ', num2str(Theta_1_inv), ' degrees']);
• disp(['Theta_3 (Effector Angle) = ', num2str(Theta_3_inv), ' degrees']);
• disp('------------------------------');
•
• % Writing Servo Positions (Convert angles to the [0, 1] range for servos)
• writePosition(base, Theta_1_inv / 180);
12
• VISION
clear all
clc
L1 = 9; % Length of link 1
L2 = 12; % Length of link 2
L3 = 11.8; % Length of link 3
a = arduino('COM4', 'Mega2560', 'Libraries', 'Servo');
writePosition(base,60/180)
writePosition(shoulder,60/180)
writePosition(effector,60/180)
for i=0:1:4
% Define initial servo positions
P1=30; % Gripper 30 is open, 100 is closed
P2=85; % Fixed position
P3=100; % Initial position
P4=50; % Increase (y=>inc z=>dec)
P5=30; % Increase (y=>inc z=>dec)
P6=0; % Increase (dec x)
% Control constants
K_p=0.1; % Proportional gain
T=3; % Threshold for position adjustment
% Initialize video input object
v = videoinput('winvideo', 1, 'MJPG_640x480');
bc=[0 0]; % Position at the webcam
q=[0 0];
target =[85 70]; % Desired x y position in the webcam
R=[0 0 0 0 0 0]; % Servo position array
% Set video object properties
set(v, 'FramesPerTrigger', Inf);
set(v, 'ReturnedColorspace', 'rgb')
v.FrameGrabInterval = 5;
% Start video acquisition
start(v)
while(v.FramesAcquired<=1000)
13
x = round(bc(1));
y = round(bc(2));
z = 30;
Matrix_Camera_to_Base = [0 0 -1 0.6 ;
-1 0 0 0 ;
0 -1 0 0.23 ;
0 0 0 1 ] ;
Point_Camera = [ x/100;
y/100;
z/100;
1 ];
display(Base_Camera_Relation);
end
% Go to a middle point after task completion (not shown in the code provided)