-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathada_bindings.cpp
More file actions
382 lines (332 loc) · 10.8 KB
/
ada_bindings.cpp
File metadata and controls
382 lines (332 loc) · 10.8 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <aikido/rviz/InteractiveMarkerViewer.hpp>
#include <dart/dynamics/JacobianNode.hpp>
#include <pybind11/cast.h>
#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <aikido/planner/World.hpp>
#include "libada/Ada.hpp"
#include "aikido/constraint/Satisfied.hpp"
#include "aikido/planner/kunzretimer/KunzRetimer.hpp"
#include "aikido/planner/parabolic/ParabolicSmoother.hpp"
#include "aikido/statespace/ScopedState.hpp"
namespace py = pybind11;
using aikido::planner::kunzretimer::KunzRetimer;
using aikido::planner::parabolic::ParabolicSmoother;
//==============================================================================
// NOTE: These functions define the Python API for Ada.
aikido::constraint::TestablePtr get_self_collision_constraint(ada::Ada* self)
{
return self->getArm()->getSelfCollisionConstraint();
}
bool start_trajectory_controllers(ada::Ada* self)
{
return self->startTrajectoryControllers();
}
bool stop_trajectory_controllers(ada::Ada* self)
{
return self->stopTrajectoryControllers();
}
std::string get_name(ada::Ada* self)
{
return self->getName();
}
aikido::planner::WorldPtr get_world(ada::Ada* self)
{
return self->getWorld();
}
std::shared_ptr<ada::Ada::AdaHand> get_hand(ada::Ada* self)
{
return self->getHand();
}
dart::dynamics::MetaSkeletonPtr get_skeleton(ada::Ada* self)
{
return self->getMetaSkeleton();
}
dart::dynamics::MetaSkeletonPtr get_arm_skeleton(ada::Ada* self)
{
return self->getArm()->getMetaSkeleton();
}
aikido::statespace::dart::MetaSkeletonStateSpacePtr get_arm_state_space(
ada::Ada* self)
{
auto armSkeleton = self->getArm()->getMetaSkeleton();
auto armSpace
= std::make_shared<aikido::statespace::dart::MetaSkeletonStateSpace>(
armSkeleton.get());
return armSpace;
}
void set_arm_positions(ada::Ada* self, const Eigen::VectorXd& configuration)
{
auto arm = self->getArm();
auto armSkeleton = arm->getMetaSkeleton();
armSkeleton->setPositions(configuration);
}
Eigen::VectorXd get_arm_positions(ada::Ada* self)
{
return self->getArm()->getCurrentConfiguration();
}
aikido::constraint::TestablePtr get_world_collision_constraint(
ada::Ada* self, const std::vector<std::string> bodyNames)
{
return self->getArm()->getWorldCollisionConstraint(bodyNames);
}
aikido::trajectory::TrajectoryPtr compute_joint_space_path(
ada::Ada* self,
const std::vector<std::pair<double, Eigen::VectorXd>>& waypoints)
{
return self->computeArmJointSpacePath(waypoints);
}
aikido::trajectory::TrajectoryPtr compute_smooth_joint_space_path(
ada::Ada* self,
const std::vector<std::pair<double, Eigen::VectorXd>>& waypoints,
const aikido::constraint::dart::CollisionFreePtr& collisionFreeConstraint
= nullptr)
{
auto vLimits = self->getVelocityLimits(true);
auto aLimits = self->getAccelerationLimits(true);
ParabolicSmoother postprocessor(vLimits, aLimits, ada::SmoothParams());
auto traj = self->computeArmJointSpacePath(waypoints);
if (traj)
{
// Cast to interpolated or spline:
auto interpolated
= dynamic_cast<const aikido::trajectory::Interpolated*>(traj.get());
if (interpolated)
{
return postprocessor.postprocess(
*interpolated, *(self->cloneRNG().get()), collisionFreeConstraint);
}
auto spline = dynamic_cast<const aikido::trajectory::Spline*>(traj.get());
if (spline)
{
return postprocessor.postprocess(
*spline, *(self->cloneRNG().get()), collisionFreeConstraint);
}
// Else return null
}
return traj;
}
aikido::trajectory::TrajectoryPtr compute_retime_path(
ada::Ada* self, aikido::trajectory::InterpolatedPtr trajectory_ptr)
{
auto vLimits = self->getVelocityLimits(true);
auto aLimits = self->getAccelerationLimits(true);
KunzRetimer postprocessor(vLimits, aLimits, ada::KunzParams());
return postprocessor.postprocess(
*(trajectory_ptr),
*(self->cloneRNG().get()),
self->getSelfCollisionConstraint());
}
aikido::trajectory::TrajectoryPtr plan_to_configuration(
ada::Ada* self,
const Eigen::VectorXd& configuration,
aikido::constraint::TestablePtr& testableConstraint)
{
auto traj
= self->getArm()->planToConfiguration(configuration, testableConstraint);
return traj;
}
aikido::trajectory::TrajectoryPtr plan_to_configuration(
ada::Ada* self, const Eigen::VectorXd& configuration)
{
// Default to self collision constraint
auto traj = self->getArm()->planToConfiguration(configuration);
return traj;
}
aikido::trajectory::TrajectoryPtr plan_to_offset(
ada::Ada* self,
const std::string bodyNodeName,
const Eigen::Vector3d& offset,
aikido::constraint::TestablePtr& testableConstraint)
{
auto traj
= self->getArm()->planToOffset(bodyNodeName, offset, testableConstraint);
return traj;
}
aikido::trajectory::TrajectoryPtr plan_to_offset(
ada::Ada* self,
const std::string bodyNodeName,
const Eigen::Vector3d& offset)
{
// Default to self collision constraint
auto trajectory = self->getArm()->planToOffset(bodyNodeName, offset);
return trajectory;
}
aikido::trajectory::TrajectoryPtr plan_to_tsr(
ada::Ada* self,
const std::string bodyNodeName,
const aikido::constraint::dart::TSRPtr& tsr,
aikido::constraint::TestablePtr& testableConstraint)
{
auto traj = self->getArm()->planToTSR(bodyNodeName, tsr, testableConstraint);
return traj;
}
aikido::trajectory::TrajectoryPtr plan_to_tsr(
ada::Ada* self,
const std::string bodyNodeName,
const aikido::constraint::dart::TSRPtr& tsr)
{
// Default to self collision constraint
auto traj = self->getArm()->planToTSR(bodyNodeName, tsr);
return traj;
}
void execute_trajectory(
ada::Ada* self, const aikido::trajectory::TrajectoryPtr& trajectory)
{
auto future = self->getArm()->executeTrajectory(trajectory);
if (!future.valid())
{
std::__throw_future_error(0);
}
future.wait();
// Throw any exceptions
future.get();
}
std::shared_ptr<aikido::rviz::InteractiveMarkerViewer> start_viewer(
ada::Ada* self,
const std::string& topicName,
const std::string& baseFrameName)
{
auto viewer = std::make_shared<aikido::rviz::InteractiveMarkerViewer>(
topicName, baseFrameName, self->getWorld());
viewer->setAutoUpdate(true);
return viewer;
}
//==============================================================================
// NOTE: These functions define the Python API for AdaHand.
dart::dynamics::MetaSkeletonPtr hand_get_skeleton(ada::Ada::AdaHand* self)
{
return self->getMetaSkeleton();
}
aikido::statespace::dart::MetaSkeletonStateSpacePtr hand_get_state_space(
ada::Ada::AdaHand* self)
{
auto handSkeleton = self->getMetaSkeleton();
auto handSpace
= std::make_shared<aikido::statespace::dart::MetaSkeletonStateSpace>(
handSkeleton.get());
return handSpace;
}
void execute_preshape(ada::Ada::AdaHand* self, const Eigen::VectorXd& d)
{
auto future = self->executePreshape(d);
if (!future.valid())
{
std::__throw_future_error(0);
}
future.wait();
// Throw any exceptions
future.get();
}
void hand_open(ada::Ada::AdaHand* self)
{
auto future = self->executePreshape("open");
if (!future.valid())
{
std::__throw_future_error(0);
}
future.wait();
// Throw any exceptions
future.get();
}
void hand_close(ada::Ada::AdaHand* self)
{
auto future = self->executePreshape("closed");
if (!future.valid())
{
std::__throw_future_error(0);
}
future.wait();
// Throw any exceptions
future.get();
}
dart::dynamics::BodyNode* get_end_effector_body_node(ada::Ada::AdaHand* self)
{
return self->getEndEffectorBodyNode();
}
void grab(ada::Ada::AdaHand* self, dart::dynamics::SkeletonPtr object)
{
self->grab(object);
}
void ungrab(ada::Ada::AdaHand* self)
{
self->ungrab();
}
//====================================LIBADA====================================
void Ada(pybind11::module& m)
{
py::class_<ada::Ada, std::shared_ptr<ada::Ada>>(m, "Ada")
.def(py::init([](bool simulation) -> std::shared_ptr<ada::Ada> {
return std::make_shared<ada::Ada>(simulation);
}))
.def("get_self_collision_constraint", get_self_collision_constraint)
.def("start_trajectory_controllers", start_trajectory_controllers)
.def("stop_trajectory_controllers", stop_trajectory_controllers)
.def("get_name", get_name)
.def("get_world", get_world)
.def("get_hand", get_hand)
.def("get_skeleton", get_skeleton)
.def("get_arm_skeleton", get_arm_skeleton)
.def("get_arm_state_space", get_arm_state_space)
.def("set_arm_positions", set_arm_positions)
.def("get_arm_positions", get_arm_positions)
.def(
"get_world_collision_constraint",
get_world_collision_constraint,
"Get collision constraint with requested bodies in world (or all "
"bodies if left blank)",
py::arg("bodyNames") = std::vector<std::string>())
.def("compute_joint_space_path", compute_joint_space_path)
.def("compute_smooth_joint_space_path", compute_smooth_joint_space_path)
.def("compute_retime_path", compute_retime_path)
.def("execute_trajectory", execute_trajectory)
.def("start_viewer", start_viewer);
// overload plan_to_ methods to make collision constraint optional
.def(
"plan_to_configuration",
py::overload_cast<ada::Ada*, const Eigen::VectorXd&>(
&plan_to_configuration))
.def(
"plan_to_configuration",
py::overload_cast<
ada::Ada*,
const Eigen::VectorXd&,
aikido::constraint::TestablePtr&>(&plan_to_configuration))
.def(
"plan_to_offset",
py::overload_cast<
ada::Ada*,
const std::string,
const Eigen::Vector3d&>(&plan_to_offset))
.def(
"plan_to_offset",
py::overload_cast<
ada::Ada*,
const std::string,
const Eigen::Vector3d&,
aikido::constraint::TestablePtr&>(&plan_to_offset))
.def(
"plan_to_tsr",
py::overload_cast<
ada::Ada*,
const std::string,
const aikido::constraint::dart::TSRPtr&>(&plan_to_tsr))
.def(
"plan_to_tsr",
py::overload_cast<
ada::Ada*,
const std::string,
const aikido::constraint::dart::TSRPtr&,
aikido::constraint::TestablePtr&>(&plan_to_tsr))
py::class_<ada::Ada::AdaHand, std::shared_ptr<ada::Ada::AdaHand>>(
m, "AdaHand")
.def("get_skeleton", hand_get_skeleton)
.def("get_state_space", hand_get_state_space)
.def("execute_preshape", execute_preshape)
.def("open", hand_open)
.def("close", hand_close)
.def("get_end_effector_body_node", get_end_effector_body_node)
.def("grab", grab)
.def("ungrab", ungrab);
}