-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrigamiRobot.m
More file actions
245 lines (222 loc) · 8.72 KB
/
OrigamiRobot.m
File metadata and controls
245 lines (222 loc) · 8.72 KB
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
classdef OrigamiRobot < handle
properties
OD (1, 1) double = 1 % Robot outer diameter
s0 (1, 1) double = 1 % Robot initial length for rrt
smin (1, 1) double = 1 % Robot minimal length
smax (1, 1) double = 1 % Robot maximal length
end
methods
function obj = OrigamiRobot(OD, s0, smin, smax)
if nargin == 0
return
end
obj.OD = OD;
obj.s0 = s0;
obj.smin = smin;
obj.smax = smax;
end
%% Forward Kinematics
function [T, kappa] = fkin(obj, s, theta, phi, base_transformation)
% Calculate the forward kinematics of the origami robot:
%
% s -- arc length (1x1 double)
% theta -- bending angle (1x1 double in radians)
% phi -- bending direction (1x1 double in radians)
% base_transformation -- transformation matrix at base
% (4x4 SO(3) matrix)
arguments
obj
s (1,1) double
theta (1,1) double
phi (1,1) double
base_transformation (4,4) double
end
kappa = theta/s;
if theta == 0
T_temp = [1, 0, 0, 0;
0, 1, 0, 0;
0, 0, 1, s;
0, 0, 0, 1];
T = base_transformation * T_temp;
else
T11 = cos(phi)*cos(phi)*cos(theta) + sin(phi)*sin(phi);
T12 = cos(phi)*sin(phi)*(cos(theta) - 1);
T21 = T12;
T13 = cos(phi)*sin(theta);
T31 = -T13;
T14 = cos(phi)*(1 - cos(theta))/kappa;
T22 = sin(phi)*sin(phi)*cos(theta) + cos(phi)*cos(phi);
T23 = sin(phi)*sin(theta);
T32 = -T23;
T24 = sin(phi) * (1-cos(theta))/kappa;
T33 = cos(theta);
T34 = sin(theta)/kappa;
T41 = 0;
T42 = 0;
T43 = 0;
T44 = 1;
T = base_transformation * [T11, T12, T13, T14;
T21, T22, T23, T24;
T31, T32, T33, T34;
T41, T42, T43, T44];
end
end
%% Forward kinematic 3 modules
% function T = fkin_3modules(obj, parameters, T_base)
% arguments
% obj
% parameters (3, 3) double
% T_base (4, 4) double
% end
% p11 = parameters(1,1);
% p12 = parameters(2,1);
% p13 = parameters(3,1);
% p21 = parameters(1,2);
% p22 = parameters(2,2);
% p23 = parameters(3,2);
% p31 = parameters(1,3);
% p32 = parameters(2,3);
% p33 = parameters(3,3);
%
% % Calculate transformation matrices for each module
% T1 = obj.fkin(p11, p12, p13, T_base);
% T2 = obj.fkin(p21, p22, p23, T1);
% T3 = obj.fkin(p31, p32, p33, T2);
%
% % disp("T3 in 3module Fkin code:")
% % disp(T3)
%
% % Combine transformations to get the final transformation matrix
% T = T3;
%
% end
function T = fkin_nmodules(obj, parameters, T_base)
% General forward kinematics for N serial continuum modules
%
% parameters : (3 x N) double
% parameters(:,i) = [S_i; Theta_i; Phi_i]
%
% T_base : (4 x 4) double
% T : (4 x 4) double (end-effector transform)
arguments
obj
parameters double
T_base (4,4) double
end
% Validate parameter size
assert(size(parameters,1) == 3, ...
'parameters must be a 3xN matrix [S;Theta;Phi]');
nModules = size(parameters,2);
% Initialize transform
T = T_base;
% Chain forward kinematics
for i = 1:nModules
S = parameters(1,i);
Theta = parameters(2,i);
Phi = parameters(3,i);
T = obj.fkin(S, Theta, Phi, T);
end
end
%% Backbone Generation as an instance method
function positions = backbone(obj, s, theta, phi, base_transformation, sampling)
% Calculates the backbone location of the robot:
%
% s -- arc length
% theta -- bending angle
% phi -- bending direction
% base_transformation -- transformation at base
% sampling -- number of sampling points along backbone
arguments
obj
s (1,1) double
theta (1,1) double
phi (1,1) double
base_transformation (4,4) double
sampling (1,1) double
end
nPoints = sampling;
s_values = linspace(0, s, nPoints);
positions = zeros(3, nPoints); % Pre-allocate
for i = 1:nPoints
% Interpolate bending angle for current segment:
theta_i = (s_values(i) / s) * theta;
% Use forward kinematics for current arc length and angle:
[T, ~] = obj.fkin(s_values(i), theta_i, phi, base_transformation);
% Extract position from transformation matrix:
positions(:, i) = T(1:3, 4);
end
end
%% Frame Plotting as an instance method
function frame(obj, T, scale)
% draw the XYZ-frame at location:
%
% T -- current transformation
% scale -- scaling factor for frame size
arguments
obj
T (4,4) double = eye(4)
scale (1,1) double = 0.5
end
origin_base = T(1:3,4);
R_base = T(1:3,1:3);
quiver3(origin_base(1), origin_base(2), origin_base(3), ...
scale*R_base(1,1), scale*R_base(2,1), scale*R_base(3,1), ...
'r', 'LineWidth',2, 'MaxHeadSize',0.5, 'HandleVisibility','off');
quiver3(origin_base(1), origin_base(2), origin_base(3), ...
scale*R_base(1,2), scale*R_base(2,2), scale*R_base(3,2), ...
'g', 'LineWidth',2, 'MaxHeadSize',0.5, 'HandleVisibility','off');
quiver3(origin_base(1), origin_base(2), origin_base(3), ...
scale*R_base(1,3), scale*R_base(2,3), scale*R_base(3,3), ...
'b', 'LineWidth',2, 'MaxHeadSize',0.5, 'HandleVisibility','off');
end
%% Robot Body
function body = robot_body(obj, bb, NBetween, NArround)
% Draw robot body based on OD and backbones
% Requires add-on function: gencyl
%
% bb -- backbone
% NBetween -- see documentation of gencyl
% NArround -- see documentation of gencyl
arguments
obj
bb
NBetween (1,1) double = 2
NArround (1,1) double = 20
end
% Constructs a robot body by sweeping a circular cross-section along a backbone.
% "radii" is defined as OD/2 at every backbone point.
radii = (obj.OD/2) * ones(1, size(bb, 2));
[X, Y, Z] = gencyl(bb, radii, NBetween, NArround);
body.X = X;
body.Y = Y;
body.Z = Z;
end
%% Create mesh for Collision Detection
function mesh_ans = mesh(obj, body)
% Create the robot body mesh in definition of OcTree Collision
% Detection:
%
% body -- output of robot_body
arguments
obj
body
end
[w, l] = size(body.X');
mesh_ans = [reshape(body.X', [w*l, 1]), reshape(body.Y', [w*l, 1]), reshape(body.Z', [w*l, 1])];
end
%% Generate Parameters based on Cable Length
function [S, theta, phi] = CableParameters(obj, l1, l2, l3)
% Create the robot parameters for Fkin based on cable length
arguments
obj
l1
l2
l3
end
S = (3 * obj.smin + l1 + l2 + l3)/3;
theta = 2 * sqrt(l1^2 + l1^2 + l1^2 - l1*l2 -l1*l3 - l2*l3)/ (3 * 0.12);
% phi = atan(sqrt(3) * (l3 - l2) / (l2 + l3 - 2*l1));
phi = 2 * pi * rand(1);
end
end
end