-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsample.cpp
More file actions
288 lines (273 loc) · 10.4 KB
/
sample.cpp
File metadata and controls
288 lines (273 loc) · 10.4 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
#include <mc_control/mc_global_controller.h>
#include <mc_rbdyn/RobotLoader.h>
#include <SpaceVecAlg/PTransform.h>
#include <mc_moveit/Planner.h>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
std::string MainRobot = "Panda";
std::string EndEffector = "";
po::options_description desc("mc_moveit sample options");
// clang-format off
desc.add_options()
("help", "Show this help message")
("robot,r", po::value<std::string>(&MainRobot), "Robot used in this sample")
("end-effector,e", po::value<std::string>(&EndEffector), "End-effector used for planning");
// clang-format on
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
po::notify(vm);
if(vm.count("help"))
{
std::cout << desc << "\n";
return 0;
}
mc_control::MCGlobalController::GlobalConfiguration config("", mc_rbdyn::RobotLoader::get_robot_module(MainRobot));
config.include_halfsit_controller = false;
config.enabled_controllers = {"Posture"};
config.initial_controller = "Posture";
// Start a dummy controller to get a published state
mc_control::MCGlobalController controller(config);
auto & robot = controller.robot();
auto & frame = [&robot, &EndEffector]() -> mc_rbdyn::RobotFrame &
{
const std::string efFrameName = EndEffector.empty() ? "EndEffector" : EndEffector;
if(!robot.hasFrame(efFrameName))
{
// FIXME: only works for Panda
robot.makeFrame(efFrameName, robot.frame("panda_link7"), sva::PTransformd::Identity());
}
return robot.frame("EndEffector");
}();
std::vector<double> q;
{
auto & mbc = controller.robot().mbc();
const auto & rjo = controller.ref_joint_order();
for(const auto & jn : rjo)
{
if(controller.robot().hasJoint(jn))
{
for(auto & qj : mbc.q[controller.robot().jointIndexByName(jn)]) { q.push_back(qj); }
}
else
{
// FIXME This assumes that a joint that is in ref_joint_order but missing from the robot is of size 1 (very
// likely to be true)
q.push_back(0);
}
}
}
controller.setEncoderValues(q);
controller.init(q);
controller.running = true;
// Start running
auto now = std::chrono::steady_clock::now();
auto dt = std::chrono::duration<double, std::micro>(1e6 * controller.timestep());
auto & solver = controller.controller().solver();
auto posture_task = dynamic_cast<mc_tasks::PostureTask *>(solver.tasks()[0]);
posture_task->stiffness(10.0);
std::shared_ptr<mc_moveit::BSplineTrajectoryTask> ef_task = nullptr;
std::shared_ptr<mc_moveit::PostureTrajectoryTask> posture_trajectory_task = nullptr;
bool trajectory_posture_execution = true;
bool trajectory_executing = false;
std::string str_state = "Waiting for plan request";
auto clear_ef_task = [&]()
{
if(ef_task)
{
solver.removeTask(ef_task);
ef_task.reset();
}
if(posture_trajectory_task)
{
solver.removeTask(posture_trajectory_task);
posture_trajectory_task.reset();
}
solver.addTask(posture_task);
trajectory_executing = false;
};
bool do_quick_reset = false;
std::cout << "creating planner now" << std::endl;
auto planner = mc_moveit::Planner::create(controller.robot(), frame.body());
std::thread run_th(
[&]()
{
while(controller.running && controller.run())
{
if(ef_task && ef_task->timeElapsed())
{
posture_task->reset();
trajectory_executing = false;
}
if(posture_trajectory_task && posture_trajectory_task->timeElapsed())
{
posture_task->reset();
trajectory_executing = false;
}
if(do_quick_reset)
{
do_quick_reset = false;
clear_ef_task();
posture_task->target(robot.module().stance());
auto & mbc = controller.controller().robot().mbc();
mbc.zero(robot.mb());
for(const auto & [name, config] : robot.module().stance()) { mbc.q[robot.jointIndexByName(name)] = config; }
}
std::this_thread::sleep_until(now + dt);
now = std::chrono::steady_clock::now();
}
});
auto & gui = *controller.controller().gui();
sva::PTransformd target = frame.position();
std::map<std::string, std::vector<double>> posture_target = robot.module().stance();
mc_moveit::Planner::Trajectory trajectory;
auto make_plan = [&]()
{
if(trajectory_executing)
{
mc_rtc::log::error("Plan execution in progress");
return;
}
trajectory = planner->plan(frame, target);
if(trajectory.error == 1)
{
str_state = "Execution ready";
mc_rtc::log::success("Execution ready");
}
else { str_state = "Planning failed"; }
};
auto make_posture_plan = [&]()
{
if(trajectory_executing)
{
mc_rtc::log::error("Plan execution in progress");
return;
}
trajectory = planner->plan(frame, posture_target);
if(trajectory.error == 1)
{
str_state = "Execution ready";
mc_rtc::log::success("Execution ready");
}
else { str_state = "Planning failed"; }
};
auto execute_plan = [&]()
{
if(trajectory_executing)
{
mc_rtc::log::error("Plan is already executing");
return;
}
if(trajectory.error != 1)
{
mc_rtc::log::error("No valid plan loaded");
return;
}
if(trajectory.waypoints.size() <= 1)
{
mc_rtc::log::error("One point or less to play in the trajectory");
return;
}
if(ef_task || posture_trajectory_task) { clear_ef_task(); }
trajectory_executing = true;
solver.removeTask(posture_task);
if(trajectory_posture_execution)
{
posture_trajectory_task = trajectory.setup_posture_task(solver, robot, 1000.0, 1e5);
solver.addTask(posture_trajectory_task);
}
else
{
ef_task = trajectory.setup_bspline_task(frame, 1000.0, 1e5);
solver.addTask(ef_task);
}
};
auto update_obstacle_position = [&](const std::string & name, const sva::PTransformd & pos)
{ planner->update_obstacle(name, pos); };
auto remove_obstacle = [&](const std::string & name)
{
planner->remove_obstacle(name);
gui.removeCategory({"MoveIt", "Collisions", name});
};
auto add_obstacle = [&](const std::string & name, const Eigen::Vector3d & size)
{
rbd::parsers::Visual visual;
visual.name = name;
visual.origin = sva::PTransformd::Identity();
visual.geometry.type = rbd::parsers::Geometry::Type::BOX;
auto box = rbd::parsers::Geometry::Box{};
box.size = size;
visual.geometry.data = box;
visual.material.type = rbd::parsers::Material::Type::COLOR;
auto color = rbd::parsers::Material::Color{};
color.r = 0.0;
color.g = 1.0;
color.b = 0.0;
color.a = 0.7;
visual.material.data = color;
planner->add_obstacle(visual);
gui.addElement(
{"MoveIt", "Collisions", name}, mc_rtc::gui::Button("Remove", [&, name]() { remove_obstacle(name); }),
mc_rtc::gui::Transform(
"position", [&, name]() -> const sva::PTransformd & { return planner->get_obstacle(name).pose; },
[&, name](const sva::PTransformd & p) { update_obstacle_position(name, p); }),
mc_rtc::gui::Visual(
"visual", [&, name]() -> const rbd::parsers::Visual & { return planner->get_obstacle(name).object; },
[&, name]() -> const sva::PTransformd & { return planner->get_obstacle(name).pose; }));
moveit_msgs::msg::CollisionObject object;
};
int fake_source = 0;
std::function<void(const std::string &)> setup_interface;
setup_interface = [&](const std::string & mode)
{
gui.removeElements(&fake_source);
gui.addElement(&fake_source, {},
mc_rtc::gui::ComboInput(
"Planning mode", {"Target posture", "Target frame"}, [mode]() { return mode; },
[&setup_interface](const std::string & m) { setup_interface(m); }),
mc_rtc::gui::Label("State", [&]() -> const std::string & { return str_state; }),
mc_rtc::gui::Checkbox("Execute trajectory with posture", trajectory_posture_execution));
if(mode == "Target frame")
{
gui.addElement(&fake_source, {},
mc_rtc::gui::Transform(
"Target", [&]() { return target; }, [&](const sva::PTransformd & t) { target = t; }),
mc_rtc::gui::Button("Plan", [&]() { make_plan(); }));
}
else if(mode == "Target posture")
{
gui.addElement(&fake_source, {},
mc_rtc::gui::Button("Set posture target",
[&]()
{
for(size_t i = 0; i < robot.mb().joints().size(); ++i)
{
const auto & j = robot.mb().joint(i);
if(j.dof() == 1) { posture_target[j.name()] = robot.mbc().q[i]; }
}
}),
mc_rtc::gui::Button("Plan", [&]() { make_posture_plan(); }));
}
gui.addElement(&fake_source, {}, mc_rtc::gui::Button("Execute", [&]() { execute_plan(); }),
mc_rtc::gui::Button("Clear executing task", [&]() { clear_ef_task(); }),
mc_rtc::gui::Button("Clear executing task & reset robot",
[&]()
{
clear_ef_task();
posture_task->target(robot.module().stance());
}),
mc_rtc::gui::Button("Quick reset", [&]() { do_quick_reset = true; }),
mc_rtc::gui::Form(
"Add obstacle",
[&](const mc_rtc::Configuration & config) { add_obstacle(config("Name"), config("Size")); },
mc_rtc::gui::FormStringInput("Name", true, "Box"),
mc_rtc::gui::FormArrayInput("Size", true, Eigen::Vector3d{0.1, 0.1, 0.1})),
mc_rtc::gui::Button("Exit", [&]() { controller.running = false; }));
};
setup_interface("Target posture");
if(run_th.joinable()) { run_th.join(); }
rclcpp::shutdown();
return 0;
}