forked from petercorke/robotics-toolbox-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlspb.m
161 lines (144 loc) · 4.51 KB
/
lspb.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
%LSPB Linear segment with parabolic blend
%
% [S,SD,SDD] = LSPB(S0, SF, M) is a scalar trajectory (Mx1) that varies
% smoothly from S0 to SF in M steps using a constant velocity segment and
% parabolic blends (a trapezoidal path). Velocity and acceleration can be
% optionally returned as SD (Mx1) and SDD (Mx1).
%
% [S,SD,SDD] = LSPB(S0, SF, M, V) as above but specifies the velocity of
% the linear segment which is normally computed automatically.
%
% [S,SD,SDD] = LSPB(S0, SF, T) as above but specifies the trajectory in
% terms of the length of the time vector T (Mx1).
%
% [S,SD,SDD] = LSPB(S0, SF, T, V) as above but specifies the velocity of
% the linear segment which is normally computed automatically and a time
% vector.
%
% LSPB(S0, SF, M, V) as above but plots S, SD and SDD versus time in a single
% figure.
%
% Notes::
% - Velocity is in units of distance per trajectory step, not per second.
% - Acceleration is in units of distance per trajectory step squared, not
% per second squared.
% - For some values of V no solution is possible and an error is flagged.
%
% References::
% - Robotics, Vision & Control, Chap 3,
% P. Corke, Springer 2011.
%
% See also TPOLY, JTRAJ.
% Copyright (C) 1993-2015, by Peter I. Corke
%
% This file is part of The Robotics Toolbox for MATLAB (RTB).
%
% RTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% RTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with RTB. If not, see <http://www.gnu.org/licenses/>.
%
% http://www.petercorke.com
%TODO
% add a 'dt' option, to convert to everything to units of seconds
function [s,sd,sdd] = lspb(q0, q1, t, V)
t0 = t;
if isscalar(t)
t = (0:t-1)';
else
t = t(:);
end
tf = max(t(:));
if nargin < 4
% if velocity not specified, compute it
V = (q1-q0)/tf * 1.5;
else
V = abs(V) * sign(q1-q0);
if abs(V) < abs(q1-q0)/tf
error('V too small');
elseif abs(V) > 2*abs(q1-q0)/tf
error('V too big');
end
end
if q0 == q1
s = ones(size(t)) * q0;
sd = zeros(size(t));
sdd = zeros(size(t));
return
end
tb = (q0 - q1 + V*tf)/V;
a = V/tb;
p = zeros(length(t), 1);
pd = p;
pdd = p;
for i = 1:length(t)
tt = t(i);
if tt <= tb
% initial blend
p(i) = q0 + a/2*tt^2;
pd(i) = a*tt;
pdd(i) = a;
elseif tt <= (tf-tb)
% linear motion
p(i) = (q1+q0-V*tf)/2 + V*tt;
pd(i) = V;
pdd(i) = 0;
else
% final blend
p(i) = q1 - a/2*tf^2 + a*tf*tt - a/2*tt^2;
pd(i) = a*tf - a*tt;
pdd(i) = -a;
end
end
switch nargout
case 0
if isscalar(t0)
% for scalar time steps, axis is labeled 1 .. M
xt = t+1;
else
% for vector time steps, axis is labeled by vector M
xt = t;
end
clf
subplot(311)
% highlight the accel, coast, decel phases with different
% colored markers
hold on
k = t<= tb;
plot(xt(k), p(k), 'r-o');
k = (t>=tb) & (t<= (tf-tb));
plot(xt(k), p(k), 'b-o');
k = t>= (tf-tb);
plot(xt(k), p(k), 'g-o');
grid; ylabel('$s$', 'FontSize', 16, 'Interpreter','latex');
hold off
subplot(312)
plot(xt, pd); grid; ylabel('$\dot{s}$', 'FontSize', 16, 'Interpreter','latex');
subplot(313)
plot(xt, pdd); grid; ylabel('$\ddot{s}$', 'FontSize', 16, 'Interpreter','latex');
if ~isscalar(t0)
xlabel('time')
else
for c=findobj(gcf, 'Type', 'axes')
set(c, 'XLim', [1 t0]);
end
end
shg
case 1
s = p;
case 2
s = p;
sd = pd;
case 3
s = p;
sd = pd;
sdd = pdd;
end