-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
kuka_plan_runner.cc
203 lines (171 loc) · 6.31 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/// @file
///
/// kuka_plan_runner is designed to wait for LCM messages constraining
/// a lcmt_robot_plan 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).
///
/// If a stop message is received, it will immediately discard the
/// current plan and wait until a new plan is received.
#include <iostream>
#include <memory>
#include "lcm/lcm-cpp.hpp"
#include "drake/common/drake_assert.h"
#include "drake/common/fmt_eigen.h"
#include "drake/common/trajectories/piecewise_polynomial.h"
#include "drake/lcmt_iiwa_command.hpp"
#include "drake/lcmt_iiwa_status.hpp"
#include "drake/lcmt_robot_plan.hpp"
#include "drake/multibody/parsing/parser.h"
#include "drake/multibody/plant/multibody_plant.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 char* const kLcmStopChannel = "STOP";
const int kNumJoints = 7;
using trajectories::PiecewisePolynomial;
typedef PiecewisePolynomial<double> PPType;
typedef Polynomial<double> PPPoly;
typedef PPType::PolynomialMatrix PPMatrix;
class RobotPlanRunner {
public:
/// plant is aliased
explicit RobotPlanRunner(const multibody::MultibodyPlant<double>& plant)
: plant_(plant), plan_number_(0) {
lcm_.subscribe(kLcmStatusChannel,
&RobotPlanRunner::HandleStatus, this);
lcm_.subscribe(kLcmPlanChannel,
&RobotPlanRunner::HandlePlan, this);
lcm_.subscribe(kLcmStopChannel,
&RobotPlanRunner::HandleStop, 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 lcmt_robot_plan* 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));
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 (!plant_.HasJointNamed(state.joint_name[j])) {
continue;
}
const multibody::Joint<double>& joint =
plant_.GetJointByName(state.joint_name[j]);
DRAKE_DEMAND(joint.num_positions() == 1);
const int idx = joint.position_start();
DRAKE_DEMAND(idx < kNumJoints);
// 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](idx, 0) = iiwa_status_.joint_position_commanded[j];
} else {
knots[i](idx, 0) = state.joint_position[j];
}
}
}
for (int i = 0; i < plan->num_states; ++i) {
fmt::print("{}\n", fmt_eigen(knots[i]));
}
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 PiecewisePolynomial<double>(
PiecewisePolynomial<double>::CubicWithContinuousSecondDerivatives(
input_time, knots, knot_dot, knot_dot)));
++plan_number_;
}
void HandleStop(const ::lcm::ReceiveBuffer*, const std::string&,
const lcmt_robot_plan*) {
std::cout << "Received stop command. Discarding plan." << std::endl;
plan_.reset();
}
::lcm::LCM lcm_;
const multibody::MultibodyPlant<double>& plant_;
int plan_number_{};
std::unique_ptr<PiecewisePolynomial<double>> plan_;
lcmt_iiwa_status iiwa_status_;
};
int do_main() {
multibody::MultibodyPlant<double> plant(0.0);
multibody::Parser(&plant).AddModelsFromUrl(
"package://drake/manipulation/models/iiwa_description/urdf/"
"iiwa14_no_collision.urdf");
plant.WeldFrames(plant.world_frame(),
plant.GetBodyByName("base").body_frame());
plant.Finalize();
RobotPlanRunner runner(plant);
runner.Run();
return 0;
}
} // namespace
} // namespace kuka_iiwa_arm
} // namespace examples
} // namespace drake
int main() {
return drake::examples::kuka_iiwa_arm::do_main();
}