-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathRosTrajectoryExecutor.cpp
321 lines (251 loc) · 9.42 KB
/
RosTrajectoryExecutor.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
#include <aikido/ros_control/RosTrajectoryExecutor.hpp>
#include <aikido/statespace/SO2.hpp>
#include <aikido/statespace/Rn.hpp>
#include <trajectory_msgs/JointTrajectory.h>
#include <chrono>
#include <thread>
#include <Eigen/Core>
#include <algorithm>
namespace aikido{
namespace ros_control{
RosTrajectoryExecutor::RosTrajectoryExecutor(
::dart::dynamics::SkeletonPtr skeleton, std::chrono::milliseconds period,
std::string serverName, ros::NodeHandle node, double trajTimeStep):
mSkeleton(std::move(skeleton)),
mSpinMutex(),
mRunning(true),
mPromise(nullptr),
mPeriod(period),
mServerName(std::move(serverName)),
mNode(std::move(node)),
mTrajClientPtr(new TrajectoryClient(serverName)),
mTrajTimeStep(trajTimeStep)
{
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
if (!mSkeleton)
throw std::invalid_argument("Skeleton is null.");
if (mPeriod.count() <= 0)
throw std::invalid_argument("Period must be positive.");
mThread = std::thread(&RosTrajectoryExecutor::spin, this);
// Set up action client (needs to be done AFTER previous line I think ????? )
mTrajClientPtr.reset(new TrajectoryClient(serverName));
mTrajClientPtr->waitForServer(); //Here
}
RosTrajectoryExecutor::~RosTrajectoryExecutor()
{
{
std::lock_guard<std::mutex> lockSpin(mSpinMutex);
mRunning = false;
}
mCv.notify_one();
mThread.join();
}
std::future<void> RosTrajectoryExecutor::execute(
trajectory::TrajectoryPtr _traj)
{
using statespace::dart::MetaSkeletonStateSpace;
if(!_traj)
throw std::invalid_argument("Traj is null.");
auto space = std::dynamic_pointer_cast<
MetaSkeletonStateSpace>(_traj->getStateSpace());
if(!space)
throw std::invalid_argument("Trajectory does not operate in this Executor's"
" MetaSkeletonStateSpace.");
auto metaSkeleton = space->getMetaSkeleton();
control_msgs::FollowJointTrajectoryGoal traj_goal;
std::unique_lock<std::mutex> skeleton_lock(mSkeleton->getMutex());
for (auto dof : metaSkeleton->getDofs())
{
auto name = dof->getName();
auto dof_in_skeleton = mSkeleton->getDof(name);
if(!dof_in_skeleton){
std::stringstream msg;
msg << "_traj contrains dof [" << name
<< "], which is not in mSkeleton.";
throw std::invalid_argument(msg.str());
}
// Appropriate place to do this?
traj_goal.trajectory.joint_names.push_back(name);
}
skeleton_lock.unlock();
{
std::lock_guard<std::mutex> lockSpin(mSpinMutex);
if(mTraj)
throw std::runtime_error("Another trajectory in execution.");
mPromise.reset(new std::promise<void>());
mTraj = _traj;
}
mCv.notify_one();
// Convert aikido trajectory to a set of trajectory_msgs/JointTrajectoryPoint
// and put in traj_goal.trajectory
// Assume that the subspace is either rn or so2
auto subspace_rn = space->getSubspace<aikido::statespace::Rn>(0);
auto subspace_so2 = space->getSubspace<aikido::statespace::SO2>(0);
std::shared_ptr<aikido::statespace::StateSpace> subspace;
// Either of the above pointers will be null
// Note to Mike - It seems with all the if conditioning, this block
// is only useful for the error throw and getDimension() general call
if(!subspace_rn && !subspace_so2)
{
throw std::runtime_error("Space cannot be casted to either Rn or SO2");
}
else{
if(!subspace_so2)
auto subspace = subspace_rn;
else
auto subspace = subspace_so2;
}
size_t n_dims = subspace->getDimension();
size_t n_ders = mTraj->getNumDerivatives();
double start = mTraj->getStartTime();
double end = mTraj->getEndTime();
double duration = mTraj->getDuration();
int time_steps = (int)(duration/mTrajTimeStep);
for(int i=0; i <= time_steps; i++)
{
double time_val = start + i*mTrajTimeStep;
trajectory_msgs::JointTrajectoryPoint jtpoint;
if (!subspace_rn)
{
auto state = subspace_so2->createState();
mTraj->evaluate(time_val, state);
double angle_value = subspace_so2->getAngle(state);
jtpoint.positions.push_back(angle_value);
}
else
{
auto state = subspace_rn->createState();
mTraj->evaluate(time_val, state);
Eigen::VectorXd state_vect = subspace_rn->getValue(state);
double* state_values = state_vect.data();
// Is this correct??????
jtpoint.positions.assign(state_values, state_values+n_dims);
}
//Assign derivatives if applicable
int min_ders = std::min(2,(int)n_ders);
for (int j=1; j <= min_ders; j++)
{
Eigen::VectorXd der_vect;
mTraj->evaluateDerivative(time_val,j,der_vect);
double *der_values = der_vect.data();
if(j==1){
jtpoint.velocities.assign(der_values,der_values+n_dims);
}
else{
jtpoint.accelerations.assign(der_values,der_values+n_dims);
}
}
jtpoint.time_from_start = ros::Duration(time_val - start);
traj_goal.trajectory.points.push_back(jtpoint);
}
if(duration - time_steps*mTrajTimeStep > std::numeric_limits<double>::epsilon())
{
// Not a perfect division of time-steps
trajectory_msgs::JointTrajectoryPoint jtpoint;
if (!subspace_rn)
{
auto state = subspace_so2->createState();
mTraj->evaluate(end, state);
double angle_value = subspace_so2->getAngle(state);
jtpoint.positions.push_back(angle_value);
}
else
{
auto state = subspace_rn->createState();
mTraj->evaluate(end, state);
Eigen::VectorXd state_vect = subspace_rn->getValue(state);
double* state_values = state_vect.data();
// Is this correct??????
jtpoint.positions.assign(state_values, state_values+n_dims);
}
//Assign derivatives if applicable
int min_ders = std::min(2,(int)n_ders);
for (int j=1; j <= min_ders; j++)
{
Eigen::VectorXd der_vect;
mTraj->evaluateDerivative(end,j,der_vect);
double *der_values = der_vect.data();
if(j==1){
jtpoint.velocities.assign(der_values,der_values+n_dims);
}
else{
jtpoint.accelerations.assign(der_values,der_values+n_dims);
}
}
jtpoint.time_from_start = ros::Duration(duration);
traj_goal.trajectory.points.push_back(jtpoint);
}
// Send goal to server
mTrajClientPtr -> sendGoal(traj_goal);
//Wait for Result - need a TIMEOUT parameter
double timeout = 30.0;
bool finished_before_timeout = mTrajClientPtr -> waitForResult(ros::Duration(timeout));
if(finished_before_timeout)
{
actionlib::SimpleClientGoalState client_state = mTrajClientPtr->getState();
ROS_INFO("Action finished: %s",client_state.toString().c_str());
}
else{
ROS_INFO("Action did not finish before the time out.");
}
return mPromise->get_future();
}
void RosTrajectoryExecutor::spin()
{
using std::chrono::duration;
using std::chrono::duration_cast;
using statespace::dart::MetaSkeletonStateSpace;
using dart::common::make_unique;
using std::chrono::system_clock;
system_clock::time_point startTime;
bool trajInExecution = false;
while(true)
{
// Terminate the thread if mRunning is false.
std::unique_lock<std::mutex> lockSpin(mSpinMutex);
mCv.wait(lockSpin, [&] {
// Reset startTime at the beginning of mTraj's execution.
if (!trajInExecution && this->mTraj)
{
startTime = system_clock::now();
trajInExecution = true;
}
return this->mTraj || !mRunning;
});
if (!mRunning)
{
if (this->mTraj){
mPromise->set_exception(std::make_exception_ptr(
std::runtime_error("Trajectory terminated while in execution.")));
this->mTraj.reset();
}
break;
}
// Can't do static here because MetaSkeletonStateSpace inherits
// CartesianProduct which inherits virtual StateSpace
auto space = std::dynamic_pointer_cast<
MetaSkeletonStateSpace>(mTraj->getStateSpace());
auto metaSkeleton = space->getMetaSkeleton();
auto state = space->createState();
// Get current time on mTraj.
system_clock::time_point const now = system_clock::now();
double t = duration_cast<duration<double> >(now - startTime).count();
mTraj->evaluate(t, state);
// Lock the skeleton, set state.
std::unique_lock<std::mutex> skeleton_lock(mSkeleton->getMutex());
space->setState(state);
skeleton_lock.unlock();
// Check if trajectory has completed.
bool const is_done = (t >= mTraj->getEndTime());
if (is_done) {
mTraj.reset();
mPromise->set_value();
trajInExecution = false;
}
std::this_thread::sleep_until(now + mPeriod);
}
}
}
}