-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharmSim.m
168 lines (119 loc) · 4.99 KB
/
armSim.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
function armSim(app, event)
hold(app.UIAxes, 'off');cla(app.UIAxes);
hold(app.UIAxesTheta1, 'off');cla(app.UIAxesTheta1);
hold(app.UIAxesTheta2, 'off');cla(app.UIAxesTheta2);
% Link Configuration
l1 = app.Link1lengthEditField.Value;
l2 = app.Link2lengthEditField.Value;
% Circle Configuration
radius = app.CircleradiusEditField.Value;
center_x = app.CircleCenterxEditField.Value;
center_y = app.CircleCenteryEditField.Value;
% Simulation Configuration
num_points = app.NumofSampleEditField.Value;
% graph Configuration
axisLimit= l1+l2+5;
theta_circle = linspace(0, 2*pi, num_points);
% Circle points in Cartesian
circle_x = center_x + radius * cos(theta_circle);
circle_y = center_y + radius * sin(theta_circle);
theta1_values = zeros(1, num_points);
theta2_values = zeros(1, num_points);
% Inv Kinematics (Calculate thetas for two links from x, y)
for i = 1:num_points
xd = circle_x(i);
yd = circle_y(i);
D = (xd^2 + yd^2 - l1^2 - l2^2) / (2 * l1 * l2);
try
theta2 = atan2(sqrt(1 - D^2), D);
theta1 = atan2(yd, xd) - atan2(l2 * sin(theta2), l1 + l2 * cos(theta2));
catch
% print in console
app.LogsTextArea.Value = sprintf('[Error] Unreachable area!!\nThe Arm cannot reach the point (%0.1f, %0.1f)', xd, yd);
% Enable red lamp
app.StateLamp.Enable = "on";
app.StateLamp.Color = [1, 0, 0];
% return from function
return ;
end
theta1_values(i) = theta1;
theta2_values(i) = theta2;
end
% Axis limits of arm graph
xlim(app.UIAxes,[-axisLimit, axisLimit]);
ylim(app.UIAxes,[-axisLimit, axisLimit]);
axis(app.UIAxes, 'equal')
% Axis limits of theta 1 graph
xlim(app.UIAxesTheta1,[0, num_points]);
ylim(app.UIAxesTheta1,[min(theta1_values)-0.25, max(theta1_values)+0.25]);
% Axis limits of theta 2 graph
xlim(app.UIAxesTheta2,[0, num_points]);
ylim(app.UIAxesTheta2,[min(theta2_values)-0.25, max(theta2_values)+0.25]);
hold(app.UIAxes, 'on');
hold(app.UIAxesTheta1, 'on');
hold(app.UIAxesTheta2, 'on');
% Enable green lamp
app.StateLamp.Enable = "on";
app.StateLamp.Color = [0, 1, 0];
% Lock edit field
app.Link1lengthEditField.Editable ="off";
app.Link2lengthEditField.Editable ="off";
app.CircleCenterxEditField.Editable ="off";
app.CircleCenteryEditField.Editable ="off";
app.CircleradiusEditField.Editable ="off";
app.NumofSampleEditField.Editable ="off";
% print in console
app.LogsTextArea.Value = sprintf('[INFO] Sumulation is Running!!');
while ( "On" == app.Switch.Value )
for t = 1:num_points
if ("On" ~= app.Switch.Value)
break;
end
% get Thetas for current time
theta1 = theta1_values(t);
theta2 = theta2_values(t);
% Calc first link coordinates
xl1 = l1 * cos(theta1);
yl1 = l1 * sin(theta1);
% Calc second link coordinates
xl2 = xl1 + l2 * cos(theta1 + theta2);
yl2 = yl1 + l2 * sin(theta1 + theta2);
xlim(app.UIAxes,[-axisLimit, axisLimit]);
ylim(app.UIAxes,[-axisLimit, axisLimit]);
% First link
plot(app.UIAxes,[0, xl1], [0, yl1], 'color', 'black', 'LineWidth', 2.5);
% Second link
plot(app.UIAxes, [xl1, xl2], [yl1, yl2], 'color', 'blue', ...
'LineWidth', 2.5);
% Circle
plot(app.UIAxes, circle_x, circle_y, '--', 'color', 'red', ...
'LineWidth', 1);
% End effector marker
plot(app.UIAxes, xl2, yl2, 'o', 'MarkerSize', 4, 'MarkerFaceColor', ...
'r', 'MarkerEdgeColor', 'k');
% Circle center marker
plot(app.UIAxes, center_x, center_y, 'x', 'MarkerSize', 8, ...
'MarkerEdgeColor', 'k');
% Plot theta 1
plot(app.UIAxesTheta1, 1:t, theta1_values(1:t), 'color','k','LineWidth', 1);
% Plot theta 2
plot(app.UIAxesTheta2, 1:t, theta2_values(1:t),'color','b', 'LineWidth', 1);
pause(0.02);
cla(app.UIAxes);
cla(app.UIAxesTheta1);
cla(app.UIAxesTheta2);
end
end
% Unlock edit field
app.Link1lengthEditField.Editable ="on";
app.Link2lengthEditField.Editable ="on";
app.CircleCenterxEditField.Editable ="on";
app.CircleCenteryEditField.Editable ="on";
app.CircleradiusEditField.Editable ="on";
app.NumofSampleEditField.Editable ="on";
% print in console
app.LogsTextArea.Value = sprintf('[INFO] Sumulation is stopped!!');
% Enable yellow lamp
app.StateLamp.Enable = "on";
app.StateLamp.Color = [1, 1, 0];
end