-
Notifications
You must be signed in to change notification settings - Fork 0
/
Traj.m
137 lines (122 loc) · 5.23 KB
/
Traj.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
classdef Traj < handle
properties
joints;
angles;
end
methods
function traj = Traj(joints)
traj.joints = joints;
traj.angles = zeros(0,length(joints));
end
% Return number of joints in trajectory
function l = getWidth(traj)
l = length(traj.joints);
end
% Return number of lines in trajectory
function l = getLength(traj)
l = length(traj.angles);
end
% Return column of trajectory corresponding to given joint or an
% empty array if joint is not in trajectory
function col = getColumn(traj, joint)
i = strmatch(joint,traj.joints);
if(i)
col = traj.angles(:,i);
return;
end
col = [];
end
% Delete a column corresponding to a given joint
function traj = deleteColumn(traj, joint)
i = strmatch(joint, traj.joints);
if(i)
traj = traj(:, [1:i-1,i+1:length(traj(1))]);
end
end
% Replace a joint's column with provided array
function traj = replaceColumn(traj, joint, angles)
i = strmatch(joint,traj.joints);
if(i)
if(length(angles) == length(traj.angles(:,i)))
traj.angles(:,i) = angles;
else
error('myApp:argChk','Length of new column does not match old');
end
end
end
% Append a line on the end of the trajectory, provided the joints
% match
function traj = addLine(traj, data)
if(length(data) == length(traj.joints))
traj.angles = [traj.angles; data];
else
error('myApp:argChk','Number of joints did not match trajectory');
end
end
% Combine trajectories side by side. If trajectories are not the
% same length, the last line of the shorter trajectory is repeated
% until the trajectories have the same length
function traj = joinFromStart(traj, traj1, traj2)
% Check that no joints are shared
if(not(isempty(intersect(traj1.joints, traj2.joints))))
error('myApp:argChk','Error, joint(s) shared between traj1 and traj2')
end
% Check if trajectories are same length
if(length(traj1.angles) < length(traj2.angles))
disp(length(traj1.angles))
disp(length(traj2.angles))
diff = length(traj2.angles) - length(traj1.angles);
lastline = traj1.angles(length(traj1.angles),:);
traj1 = [traj1.angles;repmat(lastline, diff, 1)];
else
if(length(traj1.angles) > length(traj2.angles))
diff = length(traj1.angles) - length(traj2.angles);
lastline = traj2.angles(length(traj2.angles),:);
traj2 = [traj2.angles;repmat(lastline, diff, 1)];
end
end
traj.joints = [traj1.joints; traj2.joints];
traj.angles = cat(2, traj1.angles, traj2.angles);
end
% Combine trajectories side by side. If trajectories are not the
% same length, the first line from the shorter trajectory is
% repeated to line up the end of each sub-trajectory
function traj = joinFromEnd(traj, traj1, traj2)
% Check that no joints are shared
if(not(isempty(intersect(traj1.joints, traj2.joints))))
error('myApp:argChk','Error, joint(s) shared between traj1 and traj2')
end
% Check if trajectories are same length
if(length(traj1.angles) < length(traj2.angles))
diff = length(traj2.angles) - length(traj1.angles);
lastline = traj1.angles(length(traj1.angles),:);
traj1 = [repmat(lastline, diff, 1); traj1.angles];
else
if(length(traj1.angles) > length(traj2.angles))
diff = length(traj1.angles) - length(traj2.angles);
lastline = traj2.angles(length(traj2.angles),:);
traj2 = [repmat(lastline, diff, 1); traj2.angles];
end
end
traj.joints = [traj1.joints; traj2.joints];
traj.angles = cat(2, traj1.angles, traj2.angles);
end
% NOT YET IMPLEMENTED
% Adds lines of trajectory 2 to the end of trajectory 1. Original
% sub-trajectories are unchanged,
function traj = append(traj, traj1, traj2)
t1 = traj1.angles;
t2 = traj2.angles;
% Check if trajectories share joints
if(not(isempty(intersect(traj1.joints, traj2.joints))))
% Rearrange headers and angles for trajectories to be
% concatenated
end
traj.angles = [t1;t2];
end
% Create trajectory file. saveTraj function created by William Hilton
function createTrajectory(traj, filename)
saveTraj(filename, traj.angles, traj.joints)
end
end
end