-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathex_double_arm_teach_repeat.cpp
More file actions
415 lines (369 loc) · 13.5 KB
/
Copy pathex_double_arm_teach_repeat.cpp
File metadata and controls
415 lines (369 loc) · 13.5 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* This file runs a teach_repeat program with two 6-DoF Arms with grippers,
* allowing you to physically set waypoints for the arms to then move through
* when you enter playback mode. The particular choice of PID control gains
* can affect the performance of this demo.
*/
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include "double_arm_teach_repeat_state.hpp"
#include "double_arm_teach_repeat_display.hpp"
#include "group_command.hpp"
#include "group_feedback.hpp"
#include "robot_model.hpp"
#include "arm/arm.hpp"
#include "util/mobile_io.hpp"
namespace arm = hebi::experimental::arm;
namespace hebi {
namespace examples {
enum class DemoMode
{
TrainingLeft,
TrainingRight,
TrainingNone,
Load,
Save,
PlaybackSingle,
PlaybackRepeat
};
// We don't follow the same pattern for the save/load
// values as the button ordering, so we can use the
// right column for the "back" and "exit" state.
// This mapping handles the converstion from
// mobile IO button -> displayed value.
const std::map<int, std::string> SaveLoadButtonMap
{
{1, "1"},
{2, "3"},
{3, "2"},
{4, "4"},
{5, "5"},
{7, "6"},
};
template <class CanLoadGains>
bool tryLoadGains(CanLoadGains& obj, const std::string& gains) {
for (int i = 0; i < 3; ++i)
{
if (obj->loadGains(gains))
return true;
}
return false;
}
} // namespace hebi
} // namespace examples
using namespace hebi;
using namespace hebi::examples;
int main(int argc, char* argv[])
{
//////////////////////////
///// Arm Setup //////////
//////////////////////////
const std::string family = "Arm";
const std::string l_family = "LeftArm";
const std::string r_family = "RightArm";
// Setup Module Family and Module Names
arm::Arm::Params l_params, r_params;
l_params.families_ = {l_family};
r_params.families_ = {r_family};
l_params.names_ = r_params.names_ = {"J1_base", "J2_shoulder", "J3_elbow", "J4_wrist1", "J5_wrist2", "J6_wrist3"};
// Read HRDF file to setup a RobotModel object for the 6-DoF Arm
// Make sure you are running this from the correct directory!
l_params.hrdf_file_ = "kits/arm/hrdf/A-2085-06G.hrdf";
r_params.hrdf_file_ = "kits/arm/hrdf/A-2085-06G.hrdf";
// Add the gripper
std::shared_ptr<hebi::experimental::arm::EndEffectorBase> l_gripper_shared, r_gripper_shared;
{
auto l_gripper = hebi::experimental::arm::EffortEndEffector<1>::create({l_family}, {"gripperSpool"});
auto r_gripper = hebi::experimental::arm::EffortEndEffector<1>::create({r_family}, {"gripperSpool"});
while (!l_gripper)
{
std::cout << "Waiting for left gripper!\n";
l_gripper = hebi::experimental::arm::EffortEndEffector<1>::create({l_family}, {"gripperSpool"});
}
while (!r_gripper)
{
std::cout << "Waiting for right gripper!\n";
r_gripper = hebi::experimental::arm::EffortEndEffector<1>::create({r_family}, {"gripperSpool"});
}
if (!tryLoadGains(l_gripper, "kits/arm/gains/gripper_spool_gains.xml"))
{
std::cerr << "Could not load gains for left gripper!\n";
return 1;
}
if (!tryLoadGains(r_gripper, "kits/arm/gains/gripper_spool_gains.xml"))
{
std::cerr << "Could not load gains for right gripper!\n";
return 1;
}
l_gripper_shared.reset(l_gripper.release());
r_gripper_shared.reset(r_gripper.release());
l_params.end_effector_ = l_gripper_shared;
r_params.end_effector_ = r_gripper_shared;
}
// Create the Arm Object
auto l_arm = arm::Arm::create(l_params);
auto r_arm = arm::Arm::create(r_params);
while (!l_arm) {
std::cout << "waiting for left arm...\n";
l_arm = arm::Arm::create(l_params);
}
while (!r_arm) {
std::cout << "waiting for right arm...\n";
r_arm = arm::Arm::create(r_params);
}
// Load the gains file that is approriate to the arm
if (!tryLoadGains(l_arm, "kits/arm/gains/A-2085-06.xml"))
{
std::cerr << "Could not load gains for left arm!\n";
return 1;
}
if (!tryLoadGains(r_arm, "kits/arm/gains/A-2085-06.xml"))
{
std::cerr << "Could not load gains for left arm!\n";
return 1;
}
if (!l_arm->update() || !r_arm->update())
{
std::cerr << "could not get initial feedback from arms!\n";
return 1;
}
/////////////////////////
//// MobileIO Setup /////
/////////////////////////
// Create the MobileIO object
auto mobile = util::MobileIO::create(family, "mobileIO");
while (!mobile)
{
std::cout << "Waiting for mobile IO device!\n";
mobile = util::MobileIO::create(family, "mobileIO");
}
// Initialize cleanly!
mobile->resetUI();
// Demo variables
bool abort_flag = false;
auto mode = DemoMode::TrainingNone;
// Print instructions on mobile IO display
MobileIoState current_state = trainingDisplay(true);
bool mobile_io_view_updated = current_state.sendTo(*mobile, 3);
// Populate "last state" for diffs (not really necessary, but helps if
// program started with button pressed on mobile IO)
mobile->update();
/////////////////////////
/// Main Control Loop ///
/////////////////////////
// Teach Repeat Variables
DemoState state(*l_arm, *l_gripper_shared, *r_arm, *r_gripper_shared);
// initialize
state.stopArm(*l_arm, state.left_.current_gripper_state_);
state.stopArm(*r_arm, state.right_.current_gripper_state_);
int mobile_io_stale_count = 0;
while (!abort_flag)
{
l_arm->update();
r_arm->update();
bool updated_mobile = mobile->update(0);
if (!updated_mobile)
{
++mobile_io_stale_count;
if (mobile_io_stale_count >= 5)
std::cerr << "Failed to get feedback from mobile I/O in last 5 attempts; check connection!\n";
}
else
{
if (mobile->getButtonDiff(8) == util::MobileIO::ButtonState::ToOn)
{
abort_flag = true;
std::cout << "Quitting!\n";
break;
}
// Coming back from away -- resend state in case we've reset the app for some reason:
if (mobile_io_stale_count >= 5 || !mobile_io_view_updated)
{
// Just send once, though; don't want to hang things too long...
mobile_io_view_updated = current_state.sendTo(*mobile);
}
mobile_io_stale_count = 0;
auto lr_select = mobile->getAxis(3);
auto speed = mobile->getAxis(4);
bool repeat = mobile->getAxis(5) <= 0;
if (mode == DemoMode::TrainingLeft ||
mode == DemoMode::TrainingRight ||
mode == DemoMode::TrainingNone)
{
// INTERNAL TRANSITIONS:
// Update L/R mode based on slider
if (lr_select > 0.3) {
mobile_io_view_updated = mobile->setAxisLabel(3, "Left") && mobile_io_view_updated;
current_state.setAxisLabel(3, "Left");
if (mode == DemoMode::TrainingRight) {
state.stopArm(*r_arm, state.right_.current_gripper_state_);
}
if (mode != DemoMode::TrainingLeft) {
l_arm->cancelGoal();
}
mode = DemoMode::TrainingLeft;
} else if (lr_select < -0.3) {
mobile_io_view_updated = mobile->setAxisLabel(3, "Right") && mobile_io_view_updated;
current_state.setAxisLabel(3, "Right");
if (mode == DemoMode::TrainingLeft) {
state.stopArm(*l_arm, state.left_.current_gripper_state_);
}
if (mode != DemoMode::TrainingRight) {
r_arm->cancelGoal();
}
mode = DemoMode::TrainingRight;
} else {
mobile_io_view_updated = mobile->setAxisLabel(3, "None") && mobile_io_view_updated;
current_state.setAxisLabel(3, "None");
if (mode == DemoMode::TrainingLeft) {
state.stopArm(*l_arm, state.left_.current_gripper_state_);
}
if (mode == DemoMode::TrainingRight) {
state.stopArm(*r_arm, state.right_.current_gripper_state_);
}
mode = DemoMode::TrainingNone;
}
// Update Single/Repeat mode based on slider
if (repeat) {
mobile_io_view_updated = mobile->setAxisLabel(5, "Repeat") && mobile_io_view_updated;
mobile->setAxisLabel(5, "Repeat");
} else {
mobile_io_view_updated = mobile->setAxisLabel(5, "Single") && mobile_io_view_updated;
mobile->setAxisLabel(5, "Single");
}
// Button B1 - Add stop Waypoint
if (mobile->getButtonDiff(1) == util::MobileIO::ButtonState::ToOn)
{
state.addWaypoint(speed, true);
}
// Button B2 - Toggle the gripper; state will apply for next waypoint.
if (mobile->getButtonDiff(2) == util::MobileIO::ButtonState::ToOn)
{
if (mode == DemoMode::TrainingLeft)
state.toggleGripper(LeftRight::Left);
else if (mode == DemoMode::TrainingRight)
state.toggleGripper(LeftRight::Right);
}
// Button B3 - Add Through Waypoint
if (mobile->getButtonDiff(3) == util::MobileIO::ButtonState::ToOn)
{
state.addWaypoint(speed, false);
}
// Button B4 - Clear waypoints
if (mobile->getButtonDiff(4) == util::MobileIO::ButtonState::ToOn)
{
std::cout << "Waypoints cleared\n";
state.reset();
}
// Button B5 - Save Waypoints
if (mobile->getButtonDiff(5) == util::MobileIO::ButtonState::ToOn) {
std::cout << "Transitioning to save waypoints menu\n";
mode = DemoMode::Save;
// Stop the arm if not already stopped:
if (mode == DemoMode::TrainingLeft)
state.stopArm(*l_arm, state.left_.current_gripper_state_);
else if (mode == DemoMode::TrainingLeft)
state.stopArm(*r_arm, state.right_.current_gripper_state_);
current_state = saveDisplay(SaveLoadButtonMap, state.listSavedWaypoints(SaveLoadButtonMap));
mobile_io_view_updated = current_state.sendTo(*mobile);
}
// Button B6 - Load Waypoints
else if (mobile->getButtonDiff(6) == util::MobileIO::ButtonState::ToOn) {
std::cout << "Transitioning to load waypoints menu\n";
mode = DemoMode::Load;
current_state = loadDisplay(SaveLoadButtonMap, state.listSavedWaypoints(SaveLoadButtonMap));
mobile_io_view_updated = current_state.sendTo(*mobile);
}
// Button B7 - Toggle Training/Playback
else if (mobile->getButtonDiff(7) == util::MobileIO::ButtonState::ToOn) {
if (state.numWaypoints() <= 2) {
std::cout << "At least two waypoint are needed\n";
}
else {
std::cout << "Transitioning to playback mode\n";
if (repeat)
mode = DemoMode::PlaybackRepeat;
else
mode = DemoMode::PlaybackSingle;
current_state = playbackDisplay();
mobile_io_view_updated = current_state.sendTo(*mobile);
state.playWaypoints(repeat);
}
}
}
else if (mode == DemoMode::PlaybackSingle || mode == DemoMode::PlaybackRepeat)
{
// B7 toggle training/playback
if (mobile->getButtonDiff(7) == util::MobileIO::ButtonState::ToOn)
{
std::cout << "Transitioning to training mode\n";
current_state = trainingDisplay(mode == DemoMode::PlaybackRepeat);
mobile_io_view_updated = current_state.sendTo(*mobile);
mode = DemoMode::TrainingNone;
// Cancel any old goal that is set, stopping the arm so it holds position
state.stopArm(*l_arm, state.left_.current_gripper_state_);
state.stopArm(*r_arm, state.right_.current_gripper_state_);
}
else
{
// Replay through the path again once the goal has been reached
// (these should reach the goal at the same time, since this is based
// on commands)
if (mode == DemoMode::PlaybackRepeat && (l_arm->atGoal() || r_arm->atGoal()))
{
state.playWaypoints(true);
}
}
}
else if (mode == DemoMode::Load)
{
bool loaded_waypoints = false;
for (auto kvp : SaveLoadButtonMap)
{
if (mobile->getButtonDiff(kvp.first) == util::MobileIO::ButtonState::ToOn)
{
loaded_waypoints = state.loadWaypoints(kvp.second) == DemoState::LoadResult::Success;
break;
}
}
// B6 Go back to training mode
if (loaded_waypoints || mobile->getButtonDiff(6) == util::MobileIO::ButtonState::ToOn)
{
std::cout << "Transitioning to training mode\n";
current_state = trainingDisplay(repeat);
mobile_io_view_updated = current_state.sendTo(*mobile);
mode = DemoMode::TrainingNone;
state.stopArm(*l_arm, state.left_.current_gripper_state_);
state.stopArm(*r_arm, state.right_.current_gripper_state_);
}
}
else if (mode == DemoMode::Save)
{
bool saved_waypoints = false;
for (auto kvp : SaveLoadButtonMap)
{
if (mobile->getButtonDiff(kvp.first) == util::MobileIO::ButtonState::ToOn)
{
saved_waypoints = state.saveWaypoints(kvp.second);
break;
}
}
// B6 Go back to training mode
if (saved_waypoints || mobile->getButtonDiff(6) == util::MobileIO::ButtonState::ToOn)
{
std::cout << "Transitioning to training mode\n";
current_state = trainingDisplay(repeat);
mobile_io_view_updated = current_state.sendTo(*mobile);
mode = DemoMode::TrainingNone;
}
}
}
// Send latest commands to the arm
l_arm->send();
r_arm->send();
}
mobile->resetUI();
return 0;
}