-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathKdlTrajectory.cpp
328 lines (268 loc) · 8.86 KB
/
KdlTrajectory.cpp
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
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#include "KdlTrajectory.hpp"
#include <kdl/trajectory_segment.hpp>
#include <kdl/path_line.hpp>
#include <kdl/rotational_interpolation_sa.hpp>
#include <kdl/velocityprofile_trap.hpp>
#include <kdl/velocityprofile_rect.hpp>
#include <kdl/utilities/error.h>
#include <yarp/os/LogStream.h>
#include "KdlVectorConverter.hpp"
// -----------------------------------------------------------------------------
roboticslab::KdlTrajectory::KdlTrajectory()
: duration(DURATION_NOT_SET),
maxVelocity(DEFAULT_CARTESIAN_MAX_VEL),
maxAcceleration(DEFAULT_CARTESIAN_MAX_ACC),
configuredPath(false),
configuredVelocityProfile(false),
velocityDrivenPath(false),
created(false),
currentTrajectory(0),
path(0),
velocityProfile(0)
{}
// -----------------------------------------------------------------------------
roboticslab::KdlTrajectory::~KdlTrajectory()
{
destroy();
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlTrajectory::getDuration(double* duration) const
{
*duration = currentTrajectory->Duration();
return true;
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlTrajectory::getPosition(double movementTime, std::vector<double>& position)
{
try
{
const KDL::Frame & xFrame = currentTrajectory->Pos(movementTime);
position = KdlVectorConverter::frameToVector(xFrame);
return true;
}
catch (const KDL::Error_MotionPlanning &e)
{
yError() << "Unable to retrieve position at" << movementTime;
return false;
}
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlTrajectory::getVelocity(double movementTime, std::vector<double>& velocity)
{
try
{
const KDL::Twist & xdotFrame = currentTrajectory->Vel(movementTime);
velocity = KdlVectorConverter::twistToVector(xdotFrame);
return true;
}
catch (const KDL::Error_MotionPlanning &e)
{
yError() << "Unable to retrieve velocity at" << movementTime;
return false;
}
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlTrajectory::getAcceleration(double movementTime, std::vector<double>& acceleration)
{
try
{
const KDL::Twist & xdotdotFrame = currentTrajectory->Acc(movementTime);
acceleration = KdlVectorConverter::twistToVector(xdotdotFrame);
return true;
}
catch (const KDL::Error_MotionPlanning &e)
{
yError() << "Unable to retrieve acceleration at" << movementTime;
return false;
}
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlTrajectory::setDuration(double duration)
{
this->duration = duration;
return true;
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlTrajectory::setMaxVelocity(double maxVelocity)
{
this->maxVelocity = maxVelocity;
return true;
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlTrajectory::setMaxAcceleration(double maxAcceleration)
{
this->maxAcceleration = maxAcceleration;
return true;
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlTrajectory::addWaypoint(const std::vector<double>& waypoint,
const std::vector<double>& waypointVelocity,
const std::vector<double>& waypointAcceleration)
{
KDL::Frame frame = KdlVectorConverter::vectorToFrame(waypoint);
frames.push_back(frame);
KDL::Twist twist;
if ( ! waypointVelocity.empty() )
{
twist = KdlVectorConverter::vectorToTwist(waypointVelocity);
}
twists.push_back(twist);
return true;
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlTrajectory::configurePath(int pathType)
{
if ( configuredPath )
{
yWarning() << "Path already configured";
return false;
}
switch( pathType )
{
case ICartesianTrajectory::LINE:
{
if ( frames.empty() || frames.size() > 2 || ( frames.size() == 1 && twists.empty() ) )
{
yError("Need 2 waypoints (or 1 with initial twist) for Cartesian line (have %zu)!", frames.size());
return false;
}
KDL::RotationalInterpolation * orient = new KDL::RotationalInterpolation_SingleAxis();
double eqradius = 1.0; //0.000001;
if ( frames.size() == 1 )
{
velocityDrivenPath = true;
path = new KDL::Path_Line(frames[0], twists[0], orient, eqradius);
}
else
{
velocityDrivenPath = false;
path = new KDL::Path_Line(frames[0], frames[1], orient, eqradius);
}
break;
}
default:
yError() << "Only LINE cartesian path implemented for now!";
return false;
}
configuredPath = true;
return true;
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlTrajectory::configureVelocityProfile(int velocityProfileType)
{
if ( configuredVelocityProfile )
{
yWarning() << "Velocity profile already configured";
return false;
}
switch( velocityProfileType )
{
case ICartesianTrajectory::TRAPEZOIDAL:
{
velocityProfile = new KDL::VelocityProfile_Trap(maxVelocity, maxAcceleration);
break;
}
case ICartesianTrajectory::RECTANGULAR:
{
velocityProfile = new KDL::VelocityProfile_Rectangular(maxVelocity);
break;
}
default:
yError() << "Only TRAPEZOIDAL and RECTANGULAR cartesian velocity profiles implemented for now!";
return false;
}
configuredVelocityProfile = true;
return true;
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlTrajectory::create()
{
if( created )
{
yWarning() << "Trajectory already created";
return false;
}
if( ! configuredPath )
{
yError() << "Path not configured!";
return false;
}
if( ! configuredVelocityProfile )
{
yError() << "Velocity profile not configured!";
return false;
}
if( duration == DURATION_NOT_SET )
{
if (velocityDrivenPath)
{
// assume we'll execute this trajectory indefinitely; since velocity
// depends on the path to travel along and the total duration, let's
// fix both to adjust the resulting velocity as requested by the user
double vel = path->PathLength(); // distance traveled during 1 time unit
double dummyGoal = 1e9; // somewhere far away
double dummyDuration = dummyGoal / vel;
velocityProfile->SetProfileDuration(0, dummyGoal, dummyDuration);
currentTrajectory = new KDL::Trajectory_Segment(path, velocityProfile);
}
else
{
velocityProfile->SetProfile(0, path->PathLength());
currentTrajectory = new KDL::Trajectory_Segment(path, velocityProfile);
}
}
else
{
if (velocityDrivenPath)
{
// execute the trajectory given an initial velocity and duration
double vel = path->PathLength(); // distance traveled during 1 time unit
double guessedGoal = vel * duration;
velocityProfile->SetProfileDuration(0, guessedGoal, duration);
currentTrajectory = new KDL::Trajectory_Segment(path, velocityProfile);
}
else
{
// velocity profile is set under the hood
currentTrajectory = new KDL::Trajectory_Segment(path, velocityProfile, duration);
}
}
created = true;
return true;
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlTrajectory::destroy()
{
if ( currentTrajectory )
{
// deletes aggregated path and profile instances, too
delete currentTrajectory;
currentTrajectory = 0;
path = 0;
velocityProfile = 0;
}
else
{
if ( path )
{
delete path;
path = 0;
}
if ( velocityProfile )
{
delete velocityProfile;
velocityProfile = 0;
}
}
duration = DURATION_NOT_SET;
maxVelocity = DEFAULT_CARTESIAN_MAX_VEL;
maxAcceleration = DEFAULT_CARTESIAN_MAX_ACC;
configuredPath = configuredVelocityProfile = false;
velocityDrivenPath = false;
created = false;
frames.clear();
twists.clear();
return true;
}
// -----------------------------------------------------------------------------