-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot5_v3_10.m
417 lines (357 loc) · 15.5 KB
/
robot5_v3_10.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
% This code runs well on ROBOT 1
clc;
clear all;
mylego = legoev3('usb');
global PBase
global PERIOD
PERIOD = 0.1;
global grBase
global grLink
grBase = 3; % gear ratio of base motor
grLink = 5; % gear ration of link motor
% Link length in (mm)
global l1
l1=50;
global l2
l2=95;
global l3
l3=185;
global l4
l4=110;
global h;
h = 70;
global gripper
global link_motor
global base_motor
% assigned the ports to respective motors for actuation
gripper = motor(mylego, 'A');
link_motor = motor(mylego, 'B');
base_motor = motor(mylego, 'C');
global gripper_Status;
gripper_Status = 0;
% connection for reading status of touch sensors 1 and 3
touchSensor1 = touchSensor(mylego,1);
touchSensor3 = touchSensor(mylego,3);
sonic2 = sonicSensor(mylego,2); % connect the sonic sensor to prot 2
% initially stop the motors
stop(gripper);
stop(link_motor);
stop(base_motor);
global angC;
angC = 0;
clc;
% set the speeds of motor
motorA_Speed = 55;
motorB_UpSpeed = 40;
motorB_DownSpeed = 15;
motorC_Speed = 30;
rc = 0;
touch_value1 = 0; % set the value of touch sensor 1 to 0
touch_value3 = 0; % set the value of touch sensor 3 to 0
global angB;
global dist;
global angA;
start(gripper); % Strat the gripper motor
gripper.Speed = 8; % Open gripper
pause(2);
gripper.Speed = -8; % close gripper
pause(2);
% set then gripper for operation such as open and close
gripper_operations("SET_INITIALLY", gripper)
pause(2);
gripper_operations("OPEN", gripper)
pause(2);
gripper_operations("CLOSE", gripper)
% at first set all heights for position A, B, and C to 0
dist_b_down = 0.0;
dist_c_down = 0.0;
dist_a_down = 0.0;
% Task 4: Command the manipulator to Home position
home(motorC_Speed, base_motor, link_motor, touch_value1, touch_value3, angC, motorB_UpSpeed, touchSensor1, touchSensor3);
pause(2); % 2 sec pause to stable the sensor and smooth operation
% now read the distances for each position (A, B, and C) by sonic sensor
dist_b_down = readDistance(sonic2)*1000; % dist*1000 to convert into mm
disp('Distance at b = ');
disp(dist_b_down);
pause(2); % 2 sec pause to stable the sensor and smooth operation
turnC(base_motor, motorC_Speed, angC)
pause(2); % 2 sec pause to stable the sensor and smooth operation
dist_c_down = readDistance(sonic2)*1000; % dist*1000 to convert into mm
disp('Distance at c = ');
disp(dist_c_down);
turn_a(base_motor, motorC_Speed, touch_value1, touchSensor1)
pause(2); % 2 sec pause to stable the sensor and smooth operation
dist_a_down = readDistance(sonic2)*1000; % dist*1000 to convert into mm
disp('Distance at a = ');
disp(dist_a_down);
gripper_operations("OPEN", gripper) % Open jaw of gripper
pause(2);
% from inverse kinematics function reads the theta1 and theta2 angles
% for all positions based on the heaight measured by sonic sensor
[thta1a, thta2a] = inv_kine("A", l1, l2, l3, l4, h, dist_a_down);
[thta1b, thta2b] = inv_kine("B", l1, l2, l3, l4, h, dist_b_down);
[thta1c, thta2c] = inv_kine("C", l1, l2, l3, l4, h, dist_c_down);
disp('thta1a');
disp(thta1a);
disp('thta2a');
disp(thta2a);
disp('thta1b');
disp(thta1b);
disp('thta2b');
disp(thta2b);
disp('thta1c');
disp(thta1c);
disp('thta2c');
disp(thta2c);
% Task 5: Started
% pick the ball from station B and place it on station C.
home(motorC_Speed, base_motor, link_motor, touch_value1, touch_value3, angC, motorB_UpSpeed, touchSensor1, touchSensor3);
armDown(motorB_DownSpeed, link_motor, angB,thta2b, touch_value3)
gripper_operations("CLOSE", gripper) % gripper close pick the ball
pause(1); % pause for smoother operation
arm_up(motorB_UpSpeed, link_motor, sonic2, touchSensor3, touch_value3) % arm moves upward
disp('Ball picked up'); % display the string
turnC(base_motor, motorC_Speed, angC) % turn at C position
armDown(motorB_DownSpeed, link_motor, angB,thta2c, touch_value3) % arm moves downward
gripper_operations("OPEN", gripper) % gripper open, ball place
disp('Ball drop at c'); % display the string
arm_up(motorB_UpSpeed, link_motor, sonic2, touchSensor3, touch_value3) % arm moves upward
turn_b_fromC(base_motor, motorC_Speed, angC) % return to Home Position after operation
pause(1);
disp('Reached at position B')
disp('Return at Home position: Task 5 Completed')
% Task 5: Completed
% Task 6: Started
% Pick the ball from Position C and place it on station A.
turnC(base_motor, motorC_Speed, angC) % Turn at C position
armDown(motorB_DownSpeed, link_motor, angB,thta2c, touch_value3) % arm moves downwards
gripper_operations("CLOSE", gripper) % gripper close pick the ball
arm_up(motorB_UpSpeed, link_motor, sonic2, touchSensor3, touch_value3) % arm moves upward
turn_a(base_motor, motorC_Speed, touch_value1, touchSensor1)
armDown(motorB_DownSpeed, link_motor, angB,thta2a, touch_value3) % arm moves downwards
gripper_operations("OPEN", gripper) % gripper open, ball place
home(motorC_Speed, base_motor, link_motor, touch_value1, touch_value3, angC, motorB_UpSpeed, touchSensor1, touchSensor3)
pause(1); % return to Home position
disp('Place the ball at position A and return to Home position: Task 6 Completed')
% Task 6: Completed
% Task 7: Started
% Pick the ball from station A and plcae it on position B.
turn_a(base_motor, motorC_Speed, touch_value1, touchSensor1) % turn at position A
armDown(motorB_DownSpeed, link_motor, angB,thta2a, touch_value3) % arm moves downwards
gripper_operations("CLOSE", gripper) % gripper close pick the ball
home(motorC_Speed, base_motor, link_motor, touch_value1, touch_value3, angC, motorB_UpSpeed, touchSensor1, touchSensor3)
armDown(motorB_DownSpeed, link_motor, angB,thta2b, touch_value3) % arm moves downwards
gripper_operations("OPEN", gripper) % gripper open, ball place
arm_up(motorB_UpSpeed, link_motor, sonic2, touchSensor3, touch_value3) % arm moves upward
disp('Pick the ball fom A and place it on station B and return to Home position: Task 7 Completed');
% Task 7: Completed
% Task 8: Started
% Pick the ball from Position B and Place it on station A.
armDown(motorB_DownSpeed, link_motor, angB,thta2b, touch_value3) % arm moves downwards
gripper_operations("CLOSE", gripper) % gripper close pick the ball
arm_up(motorB_UpSpeed, link_motor, sonic2, touchSensor3, touch_value3) % arm moves upward
turn_a(base_motor, motorC_Speed, touch_value1, touchSensor1)
armDown(motorB_DownSpeed, link_motor, angB,thta2a, touch_value3) % arm moves downwards
gripper_operations("OPEN", gripper) % gripper open, ball place
home(motorC_Speed, base_motor, link_motor, touch_value1, touch_value3, angC, motorB_UpSpeed, touchSensor1, touchSensor3);
disp('Pick the ball from B and place it on A and return to Home: Task 8 Completed');
% Task 8: Completed
% Task 9: Started
% Pick the ball from station A and place it on station C.
turn_a(base_motor, motorC_Speed, touch_value1, touchSensor1)
armDown(motorB_DownSpeed, link_motor, angB,thta2a, touch_value3) % arm moves downwards
gripper_operations("CLOSE", gripper) % gripper close pick the ball
arm_up(motorB_UpSpeed, link_motor, sonic2, touchSensor3, touch_value3) % arm moves upward
turn_c_fromA(base_motor, motorC_Speed, angC)
armDown(motorB_DownSpeed, link_motor, angB,thta2c, touch_value3) % arm moves downwards
gripper_operations("OPEN", gripper) % gripper open, ball place
arm_up(motorB_UpSpeed, link_motor, sonic2, touchSensor3, touch_value3) % arm moves upward
turn_b_fromC(base_motor, motorC_Speed, angC)
disp('Pick the ball from A and place it on C and return to Home position: Task 9 Completed')
% Task 9: Completed
% Task 10: Started
% Task 10: Pick the ball from station C and place it on position B.
turnC(base_motor, motorC_Speed, angC)
armDown(motorB_DownSpeed, link_motor, angB,thta2c, touch_value3) % arm moves downwards
gripper_operations("CLOSE", gripper) % gripper close pick the ball
arm_up(motorB_UpSpeed, link_motor, sonic2, touchSensor3, touch_value3) % arm moves upward
turn_b_fromC(base_motor, motorC_Speed, angC)
armDown(motorB_DownSpeed, link_motor, angB,thta2b, touch_value3) % arm moves downwards
gripper_operations("OPEN", gripper) % gripper open, ball place
arm_up(motorB_UpSpeed, link_motor, sonic2, touchSensor3, touch_value3) % arm moves upward
disp('Pick the ball from C and place it on B and return to Home position: Task 10 Completed')
% Task 10: Completed
% all functions are defined below ****************************************
% Function to move the arm down by the theta2 angle
function armDown(linkspeed, lmotor, angleB, thet_2, ts3)
lmotor.Speed = linkspeed; % speed given to arm motor
start(lmotor); % arm motor started
resetRotation(lmotor); % rotation reset to zero for link motor
angleB = readRotation(lmotor);
disp(angleB);
while (angleB <= thet_2) % condition for arm to move downwards
angleB = readRotation(lmotor);
%distance = readDistance(sonic2);
disp(angleB);
end
lmotor.Speed = 0; % arm motor stoped
disp('ANGLE B');
disp(angleB); % shows current angle of arm link
pause(2);
ts3 = 0; % upper sensor value set to zero
end
% Function to move to Home position from any position that means
% Gripper is at Position B at top most position and gripper status open
function home(base_speed, baseM, linkM, ts1 ,ts3, base_angle, link_speed, touchSensor1, touchSensor3);
% arm link moves up
linkM.Speed = -1 *abs(link_speed); % link motor started
start(linkM);
ts3 = readTouch(touchSensor3); % touchvalue stored in ts3
while(ts3 ~= 1) % condition for arm motor to stop
ts3 = readTouch(touchSensor3);
end
stop(linkM); % link motor stoped
pause(1);
baseM.Speed = base_speed; % speed given to the base motor
start(baseM) % base motor started
ts1 = readTouch(touchSensor1); % touchvalue stored in ts1
while (ts1 ~= 1) % condition for base motor halt
ts1 = readTouch(touchSensor1);
end
baseM.Speed = 0; % base motor speed set to zero
pause(1)
resetRotation(baseM) % reset rotation value of base motor
base_angle = readRotation(baseM); % rotation value asssigned to base angle
disp(base_angle);
baseM.Speed = -1*abs(base_speed);
%start(base_motor)
base_angle = readRotation(baseM);
while (base_angle >= -300) % conditon for base motor to stop at 90 degree anticlockwise
base_angle = readRotation(baseM);
end
baseM.Speed = 0;
pause(1);
resetRotation(baseM); % rotation value of base motor reseted
base_angle = readRotation(baseM);
disp(base_angle)
ts1 = 0;
end
%Function to operate the gripper in OPEN, CLSOE position
function gripper_operations(operation, gripperM)
if (operation == "SET_INITIALLY") % set the gripper initially
resetRotation(gripperM);
angA_ = readRotation(gripperM);
start(gripperM); % started the gripper motor
gripperM.Speed = 7;
while ((angA_ <= 40) && (angA_ >= -40)) % conditon to stop gripper
angA_ = readRotation(gripperM);
end
gripperM.Speed = 0;
elseif (operation == "OPEN") % gripper in close operation
angA_ = readRotation(gripperM);
start(gripperM); % gripper motor started
gripperM.Speed = 15;
while ((angA_ <= 70) && (angA_ >= -70)) % condition to stop gripper in open position
angA_ = readRotation(gripperM);
end
gripperM.Speed = 0;
elseif (operation == "CLOSE") % gripper operation in close position
angA_ = readRotation(gripperM);
start(gripperM);
gripperM.Speed = -60;
while ((angA_ >= 25) || (angA_ <= -25)) % condition of gripper in close position
angA_ = readRotation(gripperM);
end
gripperM.Speed = 0; % gripper motor stopped
else
gripperM = 0;
end
end
% Functionn to move the arm upward
function arm_up(link_speed, linkM, sonic2, touchSensor3, ts3)
linkM.Speed = -1 * abs(link_speed); % negative speed given to arm motor
start(linkM) % arm motor started
ts3 = readTouch(touchSensor3);
while(ts3 ~=1) % condition for arm motor to stop
ts3 = readTouch(touchSensor3);
end
linkM.Speed = 0; % arm motor stoped
pause(2)
end
% Function to calculate theta 2 angle and theta 1 angle from HEIGHT
function [theta1, theta2] = inv_kine(Position, l1, l2, l3, l4, h, z1)
theta1 = 0;
theta2 = 0;
gr1 = 3; % gear ration for base motor
gr2 = 5; % gear ration for arm motor
if (Position == "A") % X, Y, and Z position for A station
x = 111.7094;
y = 0;
z = z1;
elseif (Position == "B") % X, Y, and Z position for B station
x = 0;
y = 111.7094;
z = z1;
elseif (Position == "C") % X, Y, and Z position for C station
x = -111.7094;
y = -5;
z = z1;
end
theta1 = atand(y/x)*gr1*1.15; % 1.15 is correction factor for the base motor
disp('coordinates')
disp(x)
disp(y)
disp(z)
if (Position== "A")
theta1 = atand(y/x)*gr1*1.15; % 1.15 is correction factor for the base motor
elseif (Position == "B")
theta1 = atand(y/x)*gr1*1.15; % 1.15 is correction factor for the base motor
elseif (Position == "C")
theta1 = (atand(y/x)+180)*gr1*1.15; % 1.15 is correction factor for the base motor
end
alpha = asind((z-h-l1-l2*sind(45)+l4)/l3); % based on the station height we calculated alpha angle
theta2 = (alpha + 45)*gr2*0.7; % correction factor for ear ration / gear play = 0.7 depends on the robot
end
% Fucntion to turn the robot gripper at Position C
function turnC (baseM, base_speed, angleC)
baseM.Speed = -1 * abs(base_speed); % base motor started
resetRotation(baseM); % reset the rotation
angleC = readRotation(baseM);
while (angleC >= -288*0.96) % 0.96 is the correction factor due to play in plastic gears
angleC = readRotation(baseM);
end
baseM.Speed = 0; % base motor stopped
pause(1);
end
% Function for the base motor to move from b position to C position
function turn_b_fromC(baseM, base_speed, angleC)
baseM.Speed = abs(base_speed); % base motor started
resetRotation(baseM);
angleC = readRotation(baseM);
while (angleC <= 288) % conditon to stop the motor when angleC is gretaer than 288
angleC = readRotation(baseM);
end
baseM.Speed = 0;
pause(1);
end
% Function to move the base motor at postion A
function turn_a(baseM, base_speed, ts1, touchSensor1)
baseM.Speed = base_speed;
ts1 = readTouch(touchSensor1);
while (ts1 ~= 1)
ts1 = readTouch(touchSensor1);
end
baseM.Speed = 0;
pause(1);
end
% Function to turn gripper from station C to station A
function turn_c_fromA(baseM, base_speed, angleC)
baseM.Speed = -1 * abs(base_speed);
resetRotation(baseM);
angleC = readRotation(baseM);
while (angleC >= -600*0.97)
angleC = readRotation(baseM);
end
baseM.Speed = 0;
pause(1);
end