-
Notifications
You must be signed in to change notification settings - Fork 31
/
robot_command_helpers.cpp
274 lines (232 loc) · 12.6 KB
/
robot_command_helpers.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
/**
* Copyright (c) 2023 Boston Dynamics, Inc. All rights reserved.
*
* Downloading, reproducing, distributing or otherwise using the SDK Software
* is subject to the terms and conditions of the Boston Dynamics Software
* Development Kit License (20191101-BDSDK-SL).
*/
#include <chrono>
#include <bosdyn/api/arm_command.pb.h>
#include <bosdyn/api/gripper_command.pb.h>
#include "robot_command_helpers.h"
#include "bosdyn/client/error_codes/error_type_condition.h"
#include "bosdyn/client/error_codes/rpc_error_code.h"
#include "bosdyn/common/status.h"
#include "bosdyn/common/success_condition.h"
#include "bosdyn/common/time.h"
namespace bosdyn {
namespace client {
namespace {
struct BlockingRobotCommandErrorCodeCategory : std::error_category {
const char* name() const noexcept override;
std::string message(int value) const override;
bool equivalent(int valcode, const std::error_condition& cond) const noexcept override;
};
bool BlockingRobotCommandErrorCodeCategory::equivalent(
int valcode, const std::error_condition& cond) const noexcept {
if (cond == SuccessCondition::Success)
return (valcode ==
static_cast<int>(::bosdyn::client::BlockingRobotCommandErrorCode::Success));
if (cond == ErrorTypeCondition::SDKError) return true;
return false;
}
const char* BlockingRobotCommandErrorCodeCategory::name() const noexcept {
return "BlockingRobotCommandErrorCode";
}
std::string BlockingRobotCommandErrorCodeCategory::message(int value) const {
switch (static_cast<::bosdyn::client::BlockingRobotCommandErrorCode>(value)) {
case ::bosdyn::client::BlockingRobotCommandErrorCode::Success:
return "Success";
case ::bosdyn::client::BlockingRobotCommandErrorCode::CommandFeedbackError:
return "CommandFeedbackError";
case ::bosdyn::client::BlockingRobotCommandErrorCode::CommandTimeoutError:
return "CommandTimeoutError";
}
return "(BlockingRobotCommandErrorCode: unrecognized error)";
}
const BlockingRobotCommandErrorCodeCategory BlockingRobotCommandErrorCodeCategory_category{};
} // namespace
std::error_code make_error_code(BlockingRobotCommandErrorCode value) {
return {static_cast<int>(value), BlockingRobotCommandErrorCodeCategory_category};
}
const std::error_category& BlockingRobotCommandErrorCategory() {
return BlockingRobotCommandErrorCodeCategory_category;
}
RobotCommandFeedbackResultType BlockUntilArmArrives(
::bosdyn::client::RobotCommandClient& robot_command_client, int cmd_id, int timeout_sec,
int poll_period_msec) {
return BlockUntilArmArrives(robot_command_client, cmd_id, std::chrono::seconds(timeout_sec),
std::chrono::milliseconds(poll_period_msec));
}
RobotCommandFeedbackResultType BlockUntilArmArrives(
::bosdyn::client::RobotCommandClient& robot_command_client, int cmd_id,
::bosdyn::common::Duration timeout, ::bosdyn::common::Duration poll_period) {
::bosdyn::api::RobotCommandFeedbackRequest feedback_req;
feedback_req.set_robot_command_id(cmd_id);
::bosdyn::client::RobotCommandFeedbackResultType feedback_res;
auto start_time = ::bosdyn::common::NsecSinceEpoch();
const auto end_time = start_time + timeout;
while (timeout <= ::bosdyn::common::Duration(0) ||
::bosdyn::common::NsecSinceEpoch() < end_time) {
feedback_res = robot_command_client.RobotCommandFeedback(feedback_req);
// If the RPC timed out but this function hasn't timed out, keep trying.
if (!feedback_res.status &&
feedback_res.status.code() != ::bosdyn::client::RPCErrorCode::TimedOutError) {
return feedback_res;
}
auto arm_feedback =
feedback_res.response.feedback().synchronized_feedback().arm_command_feedback();
if (arm_feedback.has_arm_cartesian_feedback()) {
if (arm_feedback.arm_cartesian_feedback().status() ==
::bosdyn::api::ArmCartesianCommand::Feedback::STATUS_TRAJECTORY_COMPLETE) {
return {{BlockingRobotCommandErrorCode::Success}, feedback_res.move()};
} else if (
arm_feedback.arm_cartesian_feedback().status() ==
::bosdyn::api::ArmCartesianCommand::Feedback::STATUS_TRAJECTORY_STALLED ||
arm_feedback.arm_cartesian_feedback().status() ==
::bosdyn::api::ArmCartesianCommand::Feedback::STATUS_TRAJECTORY_CANCELLED) {
return {{BlockingRobotCommandErrorCode::CommandFeedbackError}, feedback_res.move()};
}
} else if (arm_feedback.has_arm_gaze_feedback()) {
if (arm_feedback.arm_gaze_feedback().status() ==
::bosdyn::api::GazeCommand::Feedback::STATUS_TRAJECTORY_COMPLETE) {
return {{BlockingRobotCommandErrorCode::Success}, feedback_res.move()};
} else if (arm_feedback.arm_gaze_feedback().status() ==
::bosdyn::api::GazeCommand::Feedback::STATUS_TOOL_TRAJECTORY_STALLED) {
return {{BlockingRobotCommandErrorCode::CommandFeedbackError}, feedback_res.move()};
}
} else if (arm_feedback.has_arm_impedance_feedback()) {
if (arm_feedback.arm_impedance_feedback().status() ==
::bosdyn::api::ArmImpedanceCommand::Feedback::STATUS_TRAJECTORY_COMPLETE) {
return {{BlockingRobotCommandErrorCode::Success}, feedback_res.move()};
} else if (arm_feedback.arm_impedance_feedback().status() ==
::bosdyn::api::ArmImpedanceCommand::Feedback::STATUS_TRAJECTORY_STALLED) {
return {{BlockingRobotCommandErrorCode::CommandFeedbackError}, feedback_res.move()};
}
} else if (arm_feedback.has_arm_joint_move_feedback()) {
if (arm_feedback.arm_joint_move_feedback().status() ==
::bosdyn::api::ArmJointMoveCommand::Feedback::STATUS_COMPLETE) {
return {{BlockingRobotCommandErrorCode::Success}, feedback_res.move()};
} else if (arm_feedback.arm_joint_move_feedback().status() ==
::bosdyn::api::ArmJointMoveCommand::Feedback::STATUS_STALLED) {
return {{BlockingRobotCommandErrorCode::CommandFeedbackError}, feedback_res.move()};
}
} else if (arm_feedback.has_named_arm_position_feedback()) {
if (arm_feedback.named_arm_position_feedback().status() ==
::bosdyn::api::NamedArmPositionsCommand::Feedback::STATUS_COMPLETE) {
return {{BlockingRobotCommandErrorCode::Success}, feedback_res.move()};
} else if (arm_feedback.named_arm_position_feedback().status() ==
::bosdyn::api::NamedArmPositionsCommand::Feedback::
STATUS_STALLED_HOLDING_ITEM) {
return {{BlockingRobotCommandErrorCode::CommandFeedbackError}, feedback_res.move()};
}
} else {
return {{BlockingRobotCommandErrorCode::CommandFeedbackError,
"Expected one of the following commands: ArmCartesianCommand, GazeCommand, "
"ArmJointMoveCommand, NamedArmPositionsCommand, or ArmImpedanceCommand."},
feedback_res.move()};
}
std::this_thread::sleep_for(poll_period);
}
return {{BlockingRobotCommandErrorCode::CommandTimeoutError, "ArmCommand failed or timed out."},
feedback_res.response};
}
RobotCommandFeedbackResultType BlockUntilGripperArrives(
::bosdyn::client::RobotCommandClient& robot_command_client, int cmd_id, int timeout_sec,
int poll_period_msec) {
return BlockUntilGripperArrives(robot_command_client, cmd_id, std::chrono::seconds(timeout_sec),
std::chrono::milliseconds(poll_period_msec));
}
RobotCommandFeedbackResultType BlockUntilGripperArrives(
::bosdyn::client::RobotCommandClient& robot_command_client, int cmd_id,
::bosdyn::common::Duration timeout, ::bosdyn::common::Duration poll_period) {
::bosdyn::api::RobotCommandFeedbackRequest feedback_req;
feedback_req.set_robot_command_id(cmd_id);
::bosdyn::client::RobotCommandFeedbackResultType feedback_res;
auto start_time = ::bosdyn::common::NsecSinceEpoch();
const auto end_time = start_time + timeout;
while (timeout <= ::bosdyn::common::Duration(0) ||
::bosdyn::common::NsecSinceEpoch() < end_time) {
feedback_res = robot_command_client.RobotCommandFeedback(feedback_req);
// If the RPC timed out but this function hasn't timed out, keep trying.
if (!feedback_res.status &&
feedback_res.status.code() != ::bosdyn::client::RPCErrorCode::TimedOutError) {
return feedback_res;
}
auto gripper_feedback = feedback_res.response.feedback()
.synchronized_feedback()
.gripper_command_feedback()
.claw_gripper_feedback();
if (gripper_feedback.status() ==
::bosdyn::api::ClawGripperCommand::Feedback::STATUS_AT_GOAL ||
gripper_feedback.status() ==
::bosdyn::api::ClawGripperCommand::Feedback::STATUS_APPLYING_FORCE) {
return {{BlockingRobotCommandErrorCode::Success}, feedback_res.move()};
}
std::this_thread::sleep_for(poll_period);
}
return {{BlockingRobotCommandErrorCode::CommandTimeoutError,
"The GripperCommand failed or timed out."},
feedback_res.move()};
}
RobotCommandFeedbackResultType BlockUntilStandComplete(
::bosdyn::client::RobotCommandClient& robot_command_client, int cmd_id,
::bosdyn::common::Duration timeout, ::bosdyn::common::Duration poll_period) {
::bosdyn::api::RobotCommandFeedbackRequest feedback_req;
feedback_req.set_robot_command_id(cmd_id);
::bosdyn::client::RobotCommandFeedbackResultType feedback_res;
auto start_time = ::bosdyn::common::NsecSinceEpoch();
const auto end_time = start_time + timeout;
while (timeout <= ::bosdyn::common::Duration(0) ||
::bosdyn::common::NsecSinceEpoch() < end_time) {
feedback_res = robot_command_client.RobotCommandFeedback(feedback_req);
// If the RPC timed out but this function hasn't timed out, keep trying.
if (!feedback_res.status &&
feedback_res.status.code() != ::bosdyn::client::RPCErrorCode::TimedOutError) {
return feedback_res;
}
auto stand_feedback = feedback_res.response.feedback()
.synchronized_feedback()
.mobility_command_feedback()
.stand_feedback();
if (stand_feedback.status() == ::bosdyn::api::StandCommand::Feedback::STATUS_IS_STANDING) {
return {{BlockingRobotCommandErrorCode::Success}, feedback_res.move()};
}
std::this_thread::sleep_for(poll_period);
}
return {{BlockingRobotCommandErrorCode::CommandTimeoutError,
"The StandCommand failed or timed out."},
feedback_res.move()};
}
RobotCommandFeedbackResultType BlockUntilSE2TrajectoryComplete(
::bosdyn::client::RobotCommandClient& robot_command_client, int cmd_id,
::bosdyn::common::Duration timeout, ::bosdyn::common::Duration poll_period) {
::bosdyn::api::RobotCommandFeedbackRequest feedback_req;
feedback_req.set_robot_command_id(cmd_id);
::bosdyn::client::RobotCommandFeedbackResultType feedback_res;
auto start_time = ::bosdyn::common::NsecSinceEpoch();
const auto end_time = start_time + timeout;
while (timeout <= ::bosdyn::common::Duration(0) ||
::bosdyn::common::NsecSinceEpoch() < end_time) {
feedback_res = robot_command_client.RobotCommandFeedback(feedback_req);
// If the RPC timed out but this function hasn't timed out, keep trying.
if (!feedback_res.status &&
feedback_res.status.code() != ::bosdyn::client::RPCErrorCode::TimedOutError) {
return feedback_res;
}
auto traj_feedback = feedback_res.response.feedback()
.synchronized_feedback()
.mobility_command_feedback()
.se2_trajectory_feedback();
if (traj_feedback.status() ==
::bosdyn::api::SE2TrajectoryCommand::Feedback::STATUS_STOPPED) {
return {{BlockingRobotCommandErrorCode::Success}, feedback_res.move()};
}
std::this_thread::sleep_for(poll_period);
}
return {{BlockingRobotCommandErrorCode::CommandTimeoutError,
"The TrajectoryCommand failed or timed out."},
feedback_res.move()};
}
} // namespace client
} // namespace bosdyn