-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathsample_hand_eye_calibration.cpp
More file actions
447 lines (393 loc) · 17.7 KB
/
sample_hand_eye_calibration.cpp
File metadata and controls
447 lines (393 loc) · 17.7 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include <cmath>
#include <rclcpp/rclcpp.hpp>
#include <sstream>
#include <std_srvs/srv/trigger.hpp>
#include <stdexcept>
#include <tf2/LinearMath/Transform.hpp>
#include <tf2/convert.hpp>
#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
#include <zivid_interfaces/srv/hand_eye_calibration_calibrate.hpp>
#include <zivid_interfaces/srv/hand_eye_calibration_capture.hpp>
#include <zivid_interfaces/srv/hand_eye_calibration_load.hpp>
#include <zivid_interfaces/srv/hand_eye_calibration_start.hpp>
static const auto read_only_parameter =
rcl_interfaces::msg::ParameterDescriptor{}.set__read_only(true);
[[noreturn]] void fatal_error(const rclcpp::Logger & logger, const std::string & message)
{
RCLCPP_ERROR_STREAM(logger, message);
throw std::runtime_error(message);
}
std::vector<int> sanitize_marker_ids(
const rclcpp::Logger & logger, const std::vector<int64_t> & values)
{
// We use an integer array with a single value [-1] as a placeholder for an empty array, since
// an empty integer array cannot be specified directly.
// See: https://github.com/ros2/rclcpp/issues/1955
if (values.empty() || (values.size() == 1 && values.at(0) == -1)) {
return {};
}
for (const auto & value : values) {
if (value < 0) {
fatal_error(logger, "Negative marker IDs are not allowed.");
}
if (value > std::numeric_limits<int>::max()) {
fatal_error(logger, "Marker ID " + std::to_string(value) + " is too large.");
}
}
return std::vector<int>(values.begin(), values.end());
}
std::string format_transform(const geometry_msgs::msg::Transform & transform)
{
std::ostringstream ss;
ss << "{ translation in m: {" << transform.translation.x << ", " << transform.translation.y
<< ", " << transform.translation.z << "}, rotation as quaternion: {" << transform.rotation.x
<< ", " << transform.rotation.y << ", " << transform.rotation.z << ", " << transform.rotation.w
<< "}}";
return ss.str();
}
template <typename List, typename ToStringFunc>
std::string join_list_to_string(const List & list, ToStringFunc && to_string_func)
{
return list.empty()
? std::string()
: std::accumulate(
std::next(std::begin(list)), std::end(list), to_string_func(*std::begin(list)),
[&](const std::string & str, const auto & entry) {
return str + ", " + to_string_func(entry);
});
}
template <typename Enum>
Enum get_parameter_enum(
rclcpp::Node & node, const std::string & name, const std::map<std::string, Enum> & name_value_map)
{
const auto value = node.declare_parameter<std::string>(name, "", read_only_parameter);
const auto it = name_value_map.find(value);
if (it == name_value_map.end()) {
const std::string valid_values =
join_list_to_string(name_value_map, [](auto && pair) { return pair.first; });
fatal_error(
node.get_logger(), "Invalid value for parameter '" + name + "': '" + value +
"'. Expected one of: " + valid_values + ".");
}
RCLCPP_INFO(node.get_logger(), "%s", ("Using parameter " + name + ":=" + value).c_str());
return it->second;
}
enum class CalibrationConfiguration
{
EyeToHand,
EyeInHand,
};
const std::map<std::string, CalibrationConfiguration> calibration_configuration_map = {
{"eye_to_hand", CalibrationConfiguration::EyeToHand},
{"eye_in_hand", CalibrationConfiguration::EyeInHand},
};
template <typename Service>
std::shared_ptr<rclcpp::Client<Service>> create_service_client(
const rclcpp::Node::SharedPtr & node, const std::string & service_name)
{
auto client = node->create_client<Service>(service_name);
while (!client->wait_for_service(std::chrono::seconds(3))) {
if (!rclcpp::ok()) {
fatal_error(node->get_logger(), "Client interrupted while waiting for service to appear.");
}
RCLCPP_INFO(
node->get_logger(), "Waiting for the %s service to appear...", client->get_service_name());
}
return client;
}
void set_settings(const std::shared_ptr<rclcpp::Node> & node)
{
// The following settings are for exposition only. Please refer to the Zivid knowledge base on how
// to get good quality data for hand-eye calibration:
// https://support.zivid.com/en/latest/academy/applications/hand-eye/how-to-get-good-quality-data-on-zivid-calibration-board.html
RCLCPP_INFO(node->get_logger(), "Setting parameter 'settings_yaml'");
const std::string settings_yml =
R"(
__version__:
serializer: 1
data: 22
Settings:
Acquisitions:
- Acquisition:
Aperture: 5.66
ExposureTime: 8333
Processing:
Filters:
Outlier:
Removal:
Enabled: yes
Threshold: 5
)";
auto param_client = std::make_shared<rclcpp::AsyncParametersClient>(node, "zivid_camera");
while (!param_client->wait_for_service(std::chrono::seconds(3))) {
if (!rclcpp::ok()) {
fatal_error(node->get_logger(), "Client interrupted while waiting for service to appear.");
}
RCLCPP_INFO(node->get_logger(), "Waiting for the parameters client to appear...");
}
auto result = param_client->set_parameters({rclcpp::Parameter("settings_yaml", settings_yml)});
if (
rclcpp::spin_until_future_complete(node, result, std::chrono::seconds(30)) !=
rclcpp::FutureReturnCode::SUCCESS) {
fatal_error(node->get_logger(), "Failed to set `settings_yaml` parameter");
}
}
void set_srgb(const std::shared_ptr<rclcpp::Node> & node)
{
auto param_client = std::make_shared<rclcpp::AsyncParametersClient>(node, "zivid_camera");
while (!param_client->wait_for_service(std::chrono::seconds(3))) {
if (!rclcpp::ok()) {
fatal_error(node->get_logger(), "Client interrupted while waiting for service to appear.");
}
RCLCPP_INFO(node->get_logger(), "Waiting for the parameters client to appear...");
}
auto result = param_client->set_parameters({rclcpp::Parameter("color_space", "srgb")});
if (
rclcpp::spin_until_future_complete(node, result, std::chrono::seconds(30)) !=
rclcpp::FutureReturnCode::SUCCESS) {
fatal_error(node->get_logger(), "Failed to set `color_space` parameter");
}
}
bool request_start(
const rclcpp::Node::SharedPtr & node,
const std::shared_ptr<rclcpp::Client<zivid_interfaces::srv::HandEyeCalibrationStart>> & client,
const std::vector<int> marker_ids, const std::string & working_directory)
{
auto request = std::make_shared<zivid_interfaces::srv::HandEyeCalibrationStart::Request>();
if (!working_directory.empty()) {
RCLCPP_INFO(node->get_logger(), "Setting working directory to: %s", working_directory.c_str());
request->working_directory = working_directory;
}
if (marker_ids.empty()) {
request->calibration_objects.type =
zivid_interfaces::msg::HandEyeCalibrationObjects::CALIBRATION_BOARD;
} else {
request->calibration_objects.type =
zivid_interfaces::msg::HandEyeCalibrationObjects::FIDUCIAL_MARKERS;
request->calibration_objects.marker_dictionary = "aruco4x4_50";
request->calibration_objects.marker_ids = marker_ids;
}
auto future = client->async_send_request(request);
if (rclcpp::spin_until_future_complete(node, future) != rclcpp::FutureReturnCode::SUCCESS) {
fatal_error(node->get_logger(), "Failed to call the hand-eye calibration start service");
}
auto result = future.get();
RCLCPP_INFO(node->get_logger(), "Hand-eye calibration start (%s):", client->get_service_name());
RCLCPP_INFO(node->get_logger(), " Success: %s", result->success ? "true" : "false");
RCLCPP_INFO(
node->get_logger(), " Message: %s",
result->message.empty() ? "" : (R"(""")" + result->message + R"(""")").c_str());
return result->success;
}
std::string detection_status_to_string(
const std::shared_ptr<rclcpp::Node> & node,
const zivid_interfaces::msg::DetectionResultCalibrationBoard & msg)
{
using zivid_interfaces::msg::DetectionResultCalibrationBoard;
switch (msg.status) {
case DetectionResultCalibrationBoard::STATUS_NOT_SET:
return "STATUS_NOT_SET";
case DetectionResultCalibrationBoard::STATUS_OK:
return "STATUS_OK";
case DetectionResultCalibrationBoard::STATUS_NO_VALID_FIDUCIAL_MARKER_DETECTED:
return "STATUS_NO_VALID_FIDUCIAL_MARKER_DETECTED";
case DetectionResultCalibrationBoard::STATUS_MULTIPLE_VALID_FIDUCIAL_MARKERS_DETECTED:
return "STATUS_MULTIPLE_VALID_FIDUCIAL_MARKERS_DETECTED";
case DetectionResultCalibrationBoard::STATUS_BOARD_DETECTION_FAILED:
return "STATUS_BOARD_DETECTION_FAILED";
case DetectionResultCalibrationBoard::STATUS_INSUFFICIENT_3D_QUALITY:
return "STATUS_INSUFFICIENT_3D_QUALITY";
default:
fatal_error(node->get_logger(), "Invalid status: " + std::to_string(msg.status));
}
}
void print_detection_result_calibration_board(
const std::shared_ptr<rclcpp::Node> & node,
const zivid_interfaces::msg::DetectionResultCalibrationBoard & detection_result)
{
RCLCPP_INFO(node->get_logger(), " Detection result:");
RCLCPP_INFO(
node->get_logger(), " Status: %s",
detection_status_to_string(node, detection_result).c_str());
RCLCPP_INFO(
node->get_logger(), " Status description: %s", detection_result.status_description.c_str());
RCLCPP_INFO(
node->get_logger(), " Centroid in meter: %g, %g, %g", detection_result.centroid.x,
detection_result.centroid.y, detection_result.centroid.z);
RCLCPP_INFO(
node->get_logger(),
" Pose: {{ Position in meter: %g, %g, %g }, { Orientation as quaternion: %g, %g, %g, %g }}",
detection_result.pose.position.x, detection_result.pose.position.y,
detection_result.pose.position.z, detection_result.pose.orientation.x,
detection_result.pose.orientation.y, detection_result.pose.orientation.z,
detection_result.pose.orientation.w);
}
void print_detection_result_fiducial_markers(
const std::shared_ptr<rclcpp::Node> & node,
const zivid_interfaces::msg::DetectionResultFiducialMarkers & detection_result)
{
RCLCPP_INFO(node->get_logger(), " Detected markers:");
for (const auto & marker : detection_result.detected_markers) {
RCLCPP_INFO(node->get_logger(), " - ID: %d", marker.id);
RCLCPP_INFO(
node->get_logger(),
" Pose: {{ Position in meter: %g, %g, %g }, { Orientation as quaternion: %g, %g, %g, %g "
"}}",
marker.pose.position.x, marker.pose.position.y, marker.pose.position.z,
marker.pose.orientation.x, marker.pose.orientation.y, marker.pose.orientation.z,
marker.pose.orientation.w);
}
}
bool request_capture_and_print_response(
const rclcpp::Node::SharedPtr & node,
const std::shared_ptr<rclcpp::Client<zivid_interfaces::srv::HandEyeCalibrationCapture>> & client,
const geometry_msgs::msg::Pose & pose)
{
auto request = std::make_shared<zivid_interfaces::srv::HandEyeCalibrationCapture::Request>();
request->robot_pose = pose;
auto future = client->async_send_request(request);
if (rclcpp::spin_until_future_complete(node, future) != rclcpp::FutureReturnCode::SUCCESS) {
fatal_error(node->get_logger(), "Failed to call the hand-eye calibration capture service");
}
auto result = future.get();
RCLCPP_INFO(node->get_logger(), "Hand-eye calibration capture (%s):", client->get_service_name());
RCLCPP_INFO(node->get_logger(), " Success: %s", result->success ? "true" : "false");
RCLCPP_INFO(
node->get_logger(), " Message: %s",
result->message.empty() ? "" : (R"(""")" + result->message + R"(""")").c_str());
RCLCPP_INFO(node->get_logger(), " Capture handle: %d", result->capture_handle);
if (
result->detection_result_calibration_board.status !=
zivid_interfaces::msg::DetectionResultCalibrationBoard::STATUS_NOT_SET) {
print_detection_result_calibration_board(node, result->detection_result_calibration_board);
}
if (!result->detection_result_fiducial_markers.detected_markers.empty()) {
print_detection_result_fiducial_markers(node, result->detection_result_fiducial_markers);
}
return result->success;
}
bool request_calibration_and_print_response(
const rclcpp::Node::SharedPtr & node,
const std::shared_ptr<rclcpp::Client<zivid_interfaces::srv::HandEyeCalibrationCalibrate>> &
client,
const CalibrationConfiguration configuration)
{
RCLCPP_INFO(node->get_logger(), "--- Starting hand-eye calibration ---");
using SrvRequest = zivid_interfaces::srv::HandEyeCalibrationCalibrate::Request;
auto request = std::make_shared<SrvRequest>();
request->configuration =
(configuration == CalibrationConfiguration::EyeToHand ? SrvRequest::EYE_TO_HAND
: SrvRequest::EYE_IN_HAND);
auto future = client->async_send_request(request);
if (rclcpp::spin_until_future_complete(node, future) != rclcpp::FutureReturnCode::SUCCESS) {
fatal_error(node->get_logger(), "Failed to call the hand-eye calibration calibrate service");
}
auto result = future.get();
RCLCPP_INFO(node->get_logger(), "Hand-eye calibration capture (%s):", client->get_service_name());
RCLCPP_INFO(node->get_logger(), " Success: %s", result->success ? "true" : "false");
RCLCPP_INFO(
node->get_logger(), " Message: %s",
result->message.empty() ? "" : (R"(""")" + result->message + R"(""")").c_str());
RCLCPP_INFO(node->get_logger(), " Transform: %s", format_transform(result->transform).c_str());
RCLCPP_INFO(
node->get_logger(), " Residuals: %s",
join_list_to_string(
result->residuals,
[](const zivid_interfaces::msg::HandEyeCalibrationResidual & residual) {
return "{ rotation in deg: " + std::to_string(residual.rotation) +
", translation in m: " + std::to_string(residual.translation) + " }";
})
.c_str());
return result->success;
}
geometry_msgs::msg::Pose get_simulated_robot_pose(double simulated_time)
{
// This simulated response is for exposition only. On a real system, this method should be
// replaced with the actual robot pose.
const double yaw = simulated_time * 0.1;
const double translation_x = std::sin(simulated_time);
tf2::Transform transform;
tf2::Quaternion q;
q.setRPY(0.0, 0.0, yaw);
transform.setRotation(q);
transform.setOrigin(tf2::Vector3(translation_x, 0.0, 0.0));
const geometry_msgs::msg::Transform msg_transform = tf2::toMsg(transform);
geometry_msgs::msg::Pose pose;
pose.orientation = msg_transform.rotation;
pose.position.x = msg_transform.translation.x;
pose.position.y = msg_transform.translation.y;
pose.position.z = msg_transform.translation.z;
return pose;
}
void start_hand_eye_calibration(
const rclcpp::Node::SharedPtr & node, const std::vector<int> & marker_ids,
const std::string & working_directory)
{
auto start_client = create_service_client<zivid_interfaces::srv::HandEyeCalibrationStart>(
node, "hand_eye_calibration/start");
RCLCPP_INFO(node->get_logger(), "--- Starting hand-eye calibration ---");
if (!request_start(node, start_client, marker_ids, working_directory)) {
fatal_error(node->get_logger(), "Could not start hand-eye calibration. Aborting.");
}
RCLCPP_INFO(node->get_logger(), "--- Setting capture settings ---");
set_settings(node);
}
void perform_captures_for_hand_eye_calibration(const rclcpp::Node::SharedPtr & node)
{
auto capture_client = create_service_client<zivid_interfaces::srv::HandEyeCalibrationCapture>(
node, "hand_eye_calibration/capture");
constexpr int wait_seconds = 5;
constexpr int num_successful_captures_target = 6;
RCLCPP_INFO_STREAM(
node->get_logger(), "--- Starting captures, will proceed until " +
std::to_string(num_successful_captures_target) +
" captures have completed with a detected calibration object ---");
bool sleep_before_capture = false;
for (int num_successful_captures = 0; num_successful_captures < num_successful_captures_target;) {
if (sleep_before_capture) {
RCLCPP_INFO_STREAM(
node->get_logger(), "--- Waiting for " + std::to_string(wait_seconds) +
" seconds before taking the next capture ---");
rclcpp::spin_until_future_complete(
node, std::promise<bool>().get_future(), std::chrono::seconds(wait_seconds));
} else {
sleep_before_capture = true;
}
const double simulated_time = 0.5 * static_cast<double>(num_successful_captures);
const auto robot_pose = get_simulated_robot_pose(simulated_time);
const bool success = request_capture_and_print_response(node, capture_client, robot_pose);
if (success) {
num_successful_captures += 1;
}
}
RCLCPP_INFO(node->get_logger(), "--- Captures complete ---");
}
void perform_hand_eye_calibration(
const rclcpp::Node::SharedPtr & node, const CalibrationConfiguration configuration)
{
RCLCPP_INFO(node->get_logger(), "--- Performing hand-eye calibration with captured data ---");
auto calibrate_client = create_service_client<zivid_interfaces::srv::HandEyeCalibrationCalibrate>(
node, "hand_eye_calibration/calibrate");
request_calibration_and_print_response(node, calibrate_client, configuration);
RCLCPP_INFO(node->get_logger(), "--- Calibration complete ---");
}
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
auto node = std::make_shared<rclcpp::Node>("sample_hand_eye_calibration");
set_srgb(node);
const auto configuration =
get_parameter_enum(*node, "configuration", calibration_configuration_map);
const auto marker_ids = sanitize_marker_ids(
node->get_logger(),
node->declare_parameter<std::vector<int64_t>>("marker_ids", {}, read_only_parameter));
const auto working_directory =
node->declare_parameter<std::string>("working_directory", "", read_only_parameter);
start_hand_eye_calibration(node, marker_ids, working_directory);
perform_captures_for_hand_eye_calibration(node);
perform_hand_eye_calibration(node, configuration);
RCLCPP_INFO(node->get_logger(), "Spinning node.. Press Ctrl+C to abort.");
rclcpp::spin(node);
rclcpp::shutdown();
return EXIT_SUCCESS;
}