/
kuka_plan_runner.cc
189 lines (160 loc) · 5.92 KB
/
kuka_plan_runner.cc
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
/// @file
///
/// kuka_plan_runner is designed to wait for LCM messages contraining
/// a robot_plan_t message, and then execute the plan on an iiwa arm
/// (also communicating via LCM using the
/// lcmt_iiwa_command/lcmt_iiwa_status messages).
///
/// When a plan is received, it will immediately begin executing that
/// plan on the arm (replacing any plan in progress).
#include <iostream>
#include <memory>
#include <lcm/lcm-cpp.hpp>
#include "robotlocomotion/robot_plan_t.hpp"
#include "drake/common/drake_assert.h"
#include "drake/common/find_resource.h"
#include "drake/common/trajectories/piecewise_polynomial.h"
#include "drake/common/trajectories/piecewise_polynomial_trajectory.h"
#include "drake/examples/kuka_iiwa_arm/iiwa_common.h"
#include "drake/lcmt_iiwa_command.hpp"
#include "drake/lcmt_iiwa_status.hpp"
#include "drake/multibody/joints/floating_base_types.h"
#include "drake/multibody/parsers/urdf_parser.h"
#include "drake/multibody/rigid_body_tree.h"
using Eigen::MatrixXd;
using Eigen::VectorXd;
using Eigen::VectorXi;
using drake::Vector1d;
using Eigen::Vector2d;
using Eigen::Vector3d;
namespace drake {
namespace examples {
namespace kuka_iiwa_arm {
namespace {
const char* const kLcmStatusChannel = "IIWA_STATUS";
const char* const kLcmCommandChannel = "IIWA_COMMAND";
const char* const kLcmPlanChannel = "COMMITTED_ROBOT_PLAN";
const int kNumJoints = 7;
typedef PiecewisePolynomial<double> PPType;
typedef PPType::PolynomialType PPPoly;
typedef PPType::PolynomialMatrix PPMatrix;
class RobotPlanRunner {
public:
/// tree is aliased
explicit RobotPlanRunner(const RigidBodyTree<double>& tree)
: tree_(tree), plan_number_(0) {
VerifyIiwaTree(tree);
lcm_.subscribe(kLcmStatusChannel,
&RobotPlanRunner::HandleStatus, this);
lcm_.subscribe(kLcmPlanChannel,
&RobotPlanRunner::HandlePlan, this);
}
void Run() {
int cur_plan_number = plan_number_;
int64_t cur_time_us = -1;
int64_t start_time_us = -1;
// Initialize the timestamp to an invalid number so we can detect
// the first message.
iiwa_status_.utime = cur_time_us;
lcmt_iiwa_command iiwa_command;
iiwa_command.num_joints = kNumJoints;
iiwa_command.joint_position.resize(kNumJoints, 0.);
iiwa_command.num_torques = 0;
iiwa_command.joint_torque.resize(kNumJoints, 0.);
while (true) {
// Call lcm handle until at least one status message is
// processed.
while (0 == lcm_.handleTimeout(10) || iiwa_status_.utime == -1) { }
cur_time_us = iiwa_status_.utime;
if (plan_) {
if (plan_number_ != cur_plan_number) {
std::cout << "Starting new plan." << std::endl;
start_time_us = cur_time_us;
cur_plan_number = plan_number_;
}
const double cur_traj_time_s =
static_cast<double>(cur_time_us - start_time_us) / 1e6;
const auto desired_next = plan_->value(cur_traj_time_s);
iiwa_command.utime = iiwa_status_.utime;
for (int joint = 0; joint < kNumJoints; joint++) {
iiwa_command.joint_position[joint] = desired_next(joint);
}
lcm_.publish(kLcmCommandChannel, &iiwa_command);
}
}
}
private:
void HandleStatus(const lcm::ReceiveBuffer*, const std::string&,
const lcmt_iiwa_status* status) {
iiwa_status_ = *status;
}
void HandlePlan(const lcm::ReceiveBuffer*, const std::string&,
const robotlocomotion::robot_plan_t* plan) {
std::cout << "New plan received." << std::endl;
if (iiwa_status_.utime == -1) {
std::cout << "Discarding plan, no status message received yet"
<< std::endl;
return;
} else if (plan->num_states < 2) {
std::cout << "Discarding plan, Not enough knot points." << std::endl;
return;
}
std::vector<Eigen::MatrixXd> knots(plan->num_states,
Eigen::MatrixXd::Zero(kNumJoints, 1));
std::map<std::string, int> name_to_idx =
tree_.computePositionNameToIndexMap();
for (int i = 0; i < plan->num_states; ++i) {
const auto& state = plan->plan[i];
for (int j = 0; j < state.num_joints; ++j) {
if (name_to_idx.count(state.joint_name[j]) == 0) {
continue;
}
// Treat the matrix at knots[i] as a column vector.
if (i == 0) {
// Always start moving from the position which we're
// currently commanding.
DRAKE_DEMAND(iiwa_status_.utime != -1);
knots[0](name_to_idx[state.joint_name[j]], 0) =
iiwa_status_.joint_position_commanded[j];
} else {
knots[i](name_to_idx[state.joint_name[j]], 0) =
state.joint_position[j];
}
}
}
for (int i = 0; i < plan->num_states; ++i) {
std::cout << knots[i] << std::endl;
}
std::vector<double> input_time;
for (int k = 0; k < static_cast<int>(plan->plan.size()); ++k) {
input_time.push_back(plan->plan[k].utime / 1e6);
}
const Eigen::MatrixXd knot_dot = Eigen::MatrixXd::Zero(kNumJoints, 1);
plan_.reset(new PiecewisePolynomialTrajectory(
PiecewisePolynomial<double>::Cubic(input_time, knots,
knot_dot, knot_dot)));
++plan_number_;
}
lcm::LCM lcm_;
const RigidBodyTree<double>& tree_;
int plan_number_{};
std::unique_ptr<PiecewisePolynomialTrajectory> plan_;
lcmt_iiwa_status iiwa_status_;
};
int do_main() {
auto tree = std::make_unique<RigidBodyTree<double>>();
parsers::urdf::AddModelInstanceFromUrdfFileToWorld(
FindResourceOrThrow("drake/manipulation/models/iiwa_description/urdf/"
"iiwa14_primitive_collision.urdf"),
multibody::joints::kFixed, tree.get());
RobotPlanRunner runner(*tree);
runner.Run();
return 0;
}
} // namespace
} // namespace kuka_iiwa_arm
} // namespace examples
} // namespace drake
int main() {
return drake::examples::kuka_iiwa_arm::do_main();
}