4 #include <iCub/ctrl/minJerkCtrl.h> 5 #include <iCub/iKin/iKinFwd.h> 6 #include <opencv2/core/core.hpp> 7 #include <opencv2/imgproc/imgproc.hpp> 8 #include <SuperimposeMesh/SISkeleton.h> 9 #include <yarp/dev/CartesianControl.h> 10 #include <yarp/dev/GazeControl.h> 11 #include <yarp/dev/PolyDriver.h> 12 #include <yarp/math/Math.h> 13 #include <yarp/math/SVD.h> 14 #include <yarp/os/BufferedPort.h> 15 #include <yarp/os/LogStream.h> 16 #include <yarp/os/Network.h> 17 #include <yarp/os/Property.h> 18 #include <yarp/os/RFModule.h> 19 #include <yarp/os/RpcClient.h> 20 #include <yarp/os/Time.h> 21 #include <yarp/sig/Image.h> 22 #include <yarp/sig/Matrix.h> 23 #include <yarp/sig/Vector.h> 38 if (!port_estimates_in_.open(
"/reaching/estimates:i"))
40 yError() <<
"Could not open /reaching/estimates:i port! Closing.";
44 if (!port_image_left_in_.open(
"/reaching/cam_left/img:i"))
46 yError() <<
"Could not open /reaching/cam_left/img:i port! Closing.";
50 if (!port_image_left_out_.open(
"/reaching/cam_left/img:o"))
52 yError() <<
"Could not open /reaching/cam_left/img:o port! Closing.";
56 if (!port_click_left_.open(
"/reaching/cam_left/click:i"))
58 yError() <<
"Could not open /reaching/cam_left/click:in port! Closing.";
62 if (!port_image_right_in_.open(
"/reaching/cam_right/img:i"))
64 yError() <<
"Could not open /reaching/cam_right/img:i port! Closing.";
68 if (!port_image_right_out_.open(
"/reaching/cam_right/img:o"))
70 yError() <<
"Could not open /reaching/cam_right/img:o port! Closing.";
74 if (!port_click_right_.open(
"/reaching/cam_right/click:i"))
76 yError() <<
"Could not open /reaching/cam_right/click:i port! Closing.";
80 if (!port_px_left_endeffector.open(
"/reaching/cam_left/x:o"))
82 yError() <<
"Could not open /reaching/cam_left/x:o port! Closing.";
86 if (!port_px_right_endeffector.open(
"/reaching/cam_right/x:o"))
88 yError() <<
"Could not open /reaching/cam_right/x:o port! Closing.";
92 if (!setGazeController())
return false;
94 if (!setTorsoRemoteControlboard())
return false;
96 if (!setRightArmRemoteControlboard())
return false;
98 if (!setRightArmCartesianController())
return false;
101 itf_gaze_->getInfo(btl_cam_info);
102 yInfo() <<
"[CAM INFO]" << btl_cam_info.toString();
103 Bottle* cam_left_info = btl_cam_info.findGroup(
"camera_intrinsics_left").get(1).asList();
104 Bottle* cam_right_info = btl_cam_info.findGroup(
"camera_intrinsics_right").get(1).asList();
106 float left_fx =
static_cast<float>(cam_left_info->get(0).asDouble());
107 float left_cx =
static_cast<float>(cam_left_info->get(2).asDouble());
108 float left_fy =
static_cast<float>(cam_left_info->get(5).asDouble());
109 float left_cy =
static_cast<float>(cam_left_info->get(6).asDouble());
111 left_proj_ = zeros(3, 4);
112 left_proj_(0, 0) = left_fx;
113 left_proj_(0, 2) = left_cx;
114 left_proj_(1, 1) = left_fy;
115 left_proj_(1, 2) = left_cy;
116 left_proj_(2, 2) = 1.0;
118 yInfo() <<
"left_proj_ =\n" << left_proj_.toString();
120 float right_fx =
static_cast<float>(cam_right_info->get(0).asDouble());
121 float right_cx =
static_cast<float>(cam_right_info->get(2).asDouble());
122 float right_fy =
static_cast<float>(cam_right_info->get(5).asDouble());
123 float right_cy =
static_cast<float>(cam_right_info->get(6).asDouble());
125 right_proj_ = zeros(3, 4);
126 right_proj_(0, 0) = right_fx;
127 right_proj_(0, 2) = right_cx;
128 right_proj_(1, 1) = right_fy;
129 right_proj_(1, 2) = right_cy;
130 right_proj_(2, 2) = 1.0;
132 yInfo() <<
"right_proj_ =\n" << right_proj_.toString();
137 itf_gaze_->getLeftEyePose(left_eye_x, left_eye_o);
141 itf_gaze_->getRightEyePose(right_eye_x, right_eye_o);
143 yInfo() <<
"left_eye_o =" << left_eye_o.toString();
144 yInfo() <<
"right_eye_o =" << right_eye_o.toString();
147 Matrix l_H_eye = axis2dcm(left_eye_o);
148 left_eye_x.push_back(1.0);
149 l_H_eye.setCol(3, left_eye_x);
150 Matrix l_H_r_to_eye = SE3inv(l_H_eye);
152 Matrix r_H_eye = axis2dcm(right_eye_o);
153 right_eye_x.push_back(1.0);
154 r_H_eye.setCol(3, right_eye_x);
155 Matrix r_H_r_to_eye = SE3inv(r_H_eye);
157 yInfo() <<
"l_H_r_to_eye =\n" << l_H_r_to_eye.toString();
158 yInfo() <<
"r_H_r_to_eye =\n" << r_H_r_to_eye.toString();
160 l_H_r_to_cam_ = left_proj_ * l_H_r_to_eye;
161 r_H_r_to_cam_ = right_proj_ * r_H_r_to_eye;
163 yInfo() <<
"l_H_r_to_cam_ =\n" << l_H_r_to_cam_.toString();
164 yInfo() <<
"r_H_r_to_cam_ =\n" << r_H_r_to_cam_.toString();
167 l_si_skel_ =
new SISkeleton(left_fx, left_fy, left_cx, left_cy);
168 r_si_skel_ =
new SISkeleton(right_fx, right_fy, right_cx, right_cy);
170 icub_index_ = iCubFinger(
"right_index");
171 std::deque<IControlLimits*> temp_lim;
172 temp_lim.push_front(itf_fingers_lim_);
173 if (!icub_index_.alignJointsBounds(temp_lim))
175 yError() <<
"Cannot set joint bound for index finger.";
179 handler_port_.open(
"/reaching/cmd:i");
180 attach(handler_port_);
191 while (!take_estimates_);
194 Vector* estimates = port_estimates_in_.read(
true);
217 if (should_stop_)
return false;
220 yInfo() <<
"estimates = [" << estimates->toString() <<
"]";
223 px_img_left.push_back(l_px_goal_[0]);
224 px_img_left.push_back(l_px_goal_[1]);
225 yInfo() <<
"px_img_left = [" << px_img_left.toString() <<
"]";
228 px_img_right.push_back(r_px_goal_[0]);
229 px_img_right.push_back(r_px_goal_[1]);
230 yInfo() <<
"px_img_right = [" << px_img_right.toString() <<
"]";
233 px_des.push_back(px_img_left[0]);
234 px_des.push_back(px_img_right[0]);
235 px_des.push_back(px_img_left[1]);
236 yInfo() <<
"px_des = [" << px_des.toString() <<
"]";
241 itf_gaze_->get2DPixel(LEFT, estimates->subVector(0, 2), px_ee_left);
242 yInfo() <<
"estimates(0, 2) = [" << estimates->subVector(0, 2).toString() <<
"]";
243 yInfo() <<
"px_ee_left = [" << px_ee_left.toString() <<
"]";
247 itf_gaze_->get2DPixel(RIGHT, estimates->subVector(6, 8), px_ee_right);
248 yInfo() <<
"estimates(6, 8) = [" << estimates->subVector(6, 8).toString() <<
"]";
249 yInfo() <<
"px_ee_right = [" << px_ee_right.toString() <<
"]";
255 itf_gaze_->getLeftEyePose(left_eye_x, left_eye_o);
257 Vector l_px = (l_H_r_to_cam_.submatrix(0, 2, 0, 2) * (estimates->subVector(0, 2) - left_eye_x));
258 yInfo() <<
"Proj left ee = [" << (l_px.subVector(0, 1) / l_px[2]).toString() <<
"]";
263 itf_gaze_->getRightEyePose(right_eye_x, right_eye_o);
265 Vector r_px = (r_H_r_to_cam_.submatrix(0, 2, 0, 2) * (estimates->subVector(6, 8) - right_eye_x));
266 yInfo() <<
"Proj right ee = [" << (r_px.subVector(0, 1) / r_px[2]).toString() <<
"]";
270 px_ee_now.push_back(l_px [0] / l_px[2]);
271 px_ee_now.push_back(r_px[0] / r_px[2]);
272 px_ee_now.push_back(l_px [1] / l_px[2]);
273 yInfo() <<
"px_ee_now = [" << px_ee_now.toString() <<
"]";
277 Matrix jacobian(3, 3);
279 Vector l_ee_x = estimates->subVector(0, 2);
280 l_ee_x.push_back(1.0);
282 double l_num_u = dot(l_H_r_to_cam_.subrow(0, 0, 4), l_ee_x);
283 double l_num_v = dot(l_H_r_to_cam_.subrow(1, 0, 4), l_ee_x);
284 double l_lambda = dot(l_H_r_to_cam_.subrow(2, 0, 4), l_ee_x);
285 double l_lambda_sq = pow(l_lambda, 2.0);
287 jacobian(0, 0) = (l_H_r_to_cam_(0, 0) * l_lambda - l_H_r_to_cam_(2, 0) * l_num_u) / l_lambda_sq;
288 jacobian(0, 1) = (l_H_r_to_cam_(0, 1) * l_lambda - l_H_r_to_cam_(2, 1) * l_num_u) / l_lambda_sq;
289 jacobian(0, 2) = (l_H_r_to_cam_(0, 2) * l_lambda - l_H_r_to_cam_(2, 2) * l_num_u) / l_lambda_sq;
291 jacobian(2, 0) = (l_H_r_to_cam_(1, 0) * l_lambda - l_H_r_to_cam_(2, 0) * l_num_v) / l_lambda_sq;
292 jacobian(2, 1) = (l_H_r_to_cam_(1, 1) * l_lambda - l_H_r_to_cam_(2, 1) * l_num_v) / l_lambda_sq;
293 jacobian(2, 2) = (l_H_r_to_cam_(1, 2) * l_lambda - l_H_r_to_cam_(2, 2) * l_num_v) / l_lambda_sq;
295 Vector r_ee_x = estimates->subVector(6, 8);
296 r_ee_x.push_back(1.0);
298 double r_num_u = dot(r_H_r_to_cam_.subrow(0, 0, 4), r_ee_x);
300 double r_lambda = dot(r_H_r_to_cam_.subrow(2, 0, 4), r_ee_x);
301 double r_lambda_sq = pow(r_lambda, 2.0);
303 jacobian(1, 0) = (r_H_r_to_cam_(0, 0) * r_lambda - r_H_r_to_cam_(2, 0) * r_num_u) / r_lambda_sq;
304 jacobian(1, 1) = (r_H_r_to_cam_(0, 1) * r_lambda - r_H_r_to_cam_(2, 1) * r_num_u) / r_lambda_sq;
305 jacobian(1, 2) = (r_H_r_to_cam_(0, 2) * r_lambda - r_H_r_to_cam_(2, 2) * r_num_u) / r_lambda_sq;
310 double v_max = 0.005;
313 while (!should_stop_ && !done)
315 Vector e = px_des - px_ee_now;
317 yInfo() <<
"e = [" << e.toString() <<
"]";
319 jacobian(0, 0) *= -2.0 * e[0];
320 jacobian(0, 1) *= -2.0 * e[0];
321 jacobian(0, 2) *= -2.0 * e[0];
323 jacobian(1, 0) *= -2.0 * e[1];
324 jacobian(1, 1) *= -2.0 * e[1];
325 jacobian(1, 2) *= -2.0 * e[1];
327 jacobian(2, 0) *= -2.0 * e[2];
328 jacobian(2, 1) *= -2.0 * e[2];
329 jacobian(2, 2) *= -2.0 * e[2];
331 Matrix inv_jacobian = luinv(jacobian);
336 Vector vel_x = -1.0 * K * inv_jacobian * e;
338 yInfo() <<
"vel_x = [" << vel_x.toString() <<
"]";
341 for (
size_t i = 0; i < vel_x.length(); ++i)
342 vel_x[i] = sign(vel_x[i]) * std::min(v_max, std::fabs(vel_x[i]));
344 yInfo() <<
"bounded vel_x = [" << vel_x.toString() <<
"]";
345 yInfo() <<
"px_des = [" << px_des.toString() <<
"]";
346 yInfo() <<
"px_ee_now = [" << px_ee_now.toString() <<
"]";
348 itf_rightarm_cart_->setTaskVelocities(vel_x, Vector(4, 0.0));
350 yInfo() <<
"Pixel error norm (0): " << std::abs(px_des(0) - px_ee_now(0));
351 yInfo() <<
"Pixel error norm (1): " << std::abs(px_des(1) - px_ee_now(1));
352 yInfo() <<
"Pixel error norm (2): " << std::abs(px_des(2) - px_ee_now(2));
353 yInfo() <<
"Poistion error norm: " << norm(vel_x);
357 done = ((std::abs(px_des(0) - px_ee_now(0)) < 1.0) && (std::abs(px_des(1) - px_ee_now(1)) < 1.0) && (std::abs(px_des(2) - px_ee_now(2)) < 1.0));
360 yInfo() <<
"\npx_des =" << px_des.toString() <<
"px_ee_now =" << px_ee_now.toString();
361 yInfo() <<
"Terminating!\n";
366 estimates = port_estimates_in_.read(
true);
392 yInfo() <<
"EE L cor: " << estimates->subVector(0, 2).toString();
393 yInfo() <<
"EE R cor: " << estimates->subVector(6, 8).toString() <<
"\n";
395 l_px = (l_H_r_to_cam_.submatrix(0, 2, 0, 2) * (estimates->subVector(0, 2) - left_eye_x));
396 r_px = (r_H_r_to_cam_.submatrix(0, 2, 0, 2) * (estimates->subVector(6, 8) - right_eye_x));
404 px_ee_now[0] = l_px [0];
405 px_ee_now[1] = r_px[0];
406 px_ee_now[2] = l_px [1];
410 Bottle& l_px_endeffector = port_px_left_endeffector.prepare();
411 l_px_endeffector.clear();
412 l_px_endeffector.addInt(l_px[0]);
413 l_px_endeffector.addInt(l_px[1]);
414 port_px_left_endeffector.write();
416 Bottle& r_px_endeffector = port_px_right_endeffector.prepare();
417 r_px_endeffector.clear();
418 r_px_endeffector.addInt(r_px[0]);
419 r_px_endeffector.addInt(r_px[1]);
420 port_px_right_endeffector.write();
424 jacobian = zeros(3, 3);
425 l_ee_x = estimates->subVector(0, 2);
426 l_ee_x.push_back(1.0);
428 l_num_u = dot(l_H_r_to_cam_.subrow(0, 0, 4), l_ee_x);
429 l_num_v = dot(l_H_r_to_cam_.subrow(1, 0, 4), l_ee_x);
430 l_lambda = dot(l_H_r_to_cam_.subrow(2, 0, 4), l_ee_x);
431 l_lambda_sq = pow(l_lambda, 2.0);
433 jacobian(0, 0) = (l_H_r_to_cam_(0, 0) * l_lambda - l_H_r_to_cam_(2, 0) * l_num_u) / l_lambda_sq;
434 jacobian(0, 1) = (l_H_r_to_cam_(0, 1) * l_lambda - l_H_r_to_cam_(2, 1) * l_num_u) / l_lambda_sq;
435 jacobian(0, 2) = (l_H_r_to_cam_(0, 2) * l_lambda - l_H_r_to_cam_(2, 2) * l_num_u) / l_lambda_sq;
437 jacobian(2, 0) = (l_H_r_to_cam_(1, 0) * l_lambda - l_H_r_to_cam_(2, 0) * l_num_v) / l_lambda_sq;
438 jacobian(2, 1) = (l_H_r_to_cam_(1, 1) * l_lambda - l_H_r_to_cam_(2, 1) * l_num_v) / l_lambda_sq;
439 jacobian(2, 2) = (l_H_r_to_cam_(1, 2) * l_lambda - l_H_r_to_cam_(2, 2) * l_num_v) / l_lambda_sq;
441 r_ee_x = estimates->subVector(6, 8);
442 r_ee_x.push_back(1.0);
444 r_num_u = dot(r_H_r_to_cam_.subrow(0, 0, 4), r_ee_x);
446 r_lambda = dot(r_H_r_to_cam_.subrow(2, 0, 4), r_ee_x);
447 r_lambda_sq = pow(r_lambda, 2.0);
449 jacobian(1, 0) = (r_H_r_to_cam_(0, 0) * r_lambda - r_H_r_to_cam_(2, 0) * r_num_u) / r_lambda_sq;
450 jacobian(1, 1) = (r_H_r_to_cam_(0, 1) * r_lambda - r_H_r_to_cam_(2, 1) * r_num_u) / r_lambda_sq;
451 jacobian(1, 2) = (r_H_r_to_cam_(0, 2) * r_lambda - r_H_r_to_cam_(2, 2) * r_num_u) / r_lambda_sq;
455 ImageOf<PixelRgb>* l_imgin = port_image_left_in_.read(
true);
456 ImageOf<PixelRgb>& l_imgout = port_image_left_out_.prepare();
458 cv::Mat l_img = cv::cvarrToMat(l_imgout.getIplImage());
460 cv::circle(l_img, cv::Point(l_px[0], l_px[1]), 4, cv::Scalar(0, 255, 0), 4);
461 cv::circle(l_img, cv::Point(l_px_goal_[0], l_px_goal_[1]), 4, cv::Scalar(0, 255, 0), 4);
463 port_image_left_out_.write();
466 ImageOf<PixelRgb>* r_imgin = port_image_right_in_.read(
true);
467 ImageOf<PixelRgb>& r_imgout = port_image_right_out_.prepare();
469 cv::Mat r_img = cv::cvarrToMat(r_imgout.getIplImage());
471 cv::circle(r_img, cv::Point(r_px[0], r_px[1]), 4, cv::Scalar(0, 255, 0), 4);
472 cv::circle(r_img, cv::Point(r_px_goal_[0], r_px_goal_[1]), 4, cv::Scalar(0, 255, 0), 4);
474 port_image_right_out_.write();
478 itf_rightarm_cart_->stopControl();
486 bool respond(
const Bottle& command, Bottle& reply)
488 int cmd = command.get(0).asVocab();
492 case VOCAB4(
'q',
'u',
'i',
't'):
494 itf_rightarm_cart_->stopControl();
496 take_estimates_ =
true;
499 this->interruptModule();
507 case VOCAB3(
'i',
'm',
'g'):
511 itf_gaze_->getLeftEyePose(left_eye_x, left_eye_o);
515 itf_gaze_->getRightEyePose(right_eye_x, right_eye_o);
517 yInfo() <<
"left_eye_o =" << left_eye_o.toString();
518 yInfo() <<
"right_eye_o =" << right_eye_o.toString();
521 Matrix l_H_eye = axis2dcm(left_eye_o);
522 left_eye_x.push_back(1.0);
523 l_H_eye.setCol(3, left_eye_x);
524 Matrix l_H_r_to_eye = SE3inv(l_H_eye);
526 Matrix r_H_eye = axis2dcm(right_eye_o);
527 right_eye_x.push_back(1.0);
528 r_H_eye.setCol(3, right_eye_x);
529 Matrix r_H_r_to_eye = SE3inv(r_H_eye);
531 yInfo() <<
"l_H_r_to_eye =\n" << l_H_r_to_eye.toString();
532 yInfo() <<
"r_H_r_to_eye =\n" << r_H_r_to_eye.toString();
535 l_H_r_to_cam_ = left_proj_ * l_H_r_to_eye;
536 r_H_r_to_cam_ = right_proj_ * r_H_r_to_eye;
539 Bottle* click_left = port_click_left_.read (
true);
540 Bottle* click_right = port_click_right_.read (
true);
542 l_px_goal_.resize(2);
543 l_px_goal_[0] = click_left->get(0).asDouble();
544 l_px_goal_[1] = click_left->get(1).asDouble();
546 r_px_goal_.resize(2);
547 r_px_goal_[0] = click_right->get(0).asDouble();
548 r_px_goal_[1] = click_right->get(1).asDouble();
556 case VOCAB4(
'g',
'o',
'a',
'l'):
560 itf_gaze_->getLeftEyePose(left_eye_x, left_eye_o);
564 itf_gaze_->getRightEyePose(right_eye_x, right_eye_o);
566 yInfo() <<
"left_eye_o =" << left_eye_o.toString();
567 yInfo() <<
"right_eye_o =" << right_eye_o.toString();
570 Matrix l_H_eye = axis2dcm(left_eye_o);
571 left_eye_x.push_back(1.0);
572 l_H_eye.setCol(3, left_eye_x);
573 Matrix l_H_r_to_eye = SE3inv(l_H_eye);
575 Matrix r_H_eye = axis2dcm(right_eye_o);
576 right_eye_x.push_back(1.0);
577 r_H_eye.setCol(3, right_eye_x);
578 Matrix r_H_r_to_eye = SE3inv(r_H_eye);
580 yInfo() <<
"l_H_r_to_eye =\n" << l_H_r_to_eye.toString();
581 yInfo() <<
"r_H_r_to_eye =\n" << r_H_r_to_eye.toString();
583 l_H_r_to_cam_ = left_proj_ * l_H_r_to_eye;
584 r_H_r_to_cam_ = right_proj_ * r_H_r_to_eye;
587 l_px_goal_.resize(2);
591 r_px_goal_.resize(2);
600 case VOCAB2(
'g',
'o'):
603 take_estimates_ =
true;
608 case VOCAB3(
'o',
'p',
'c'):
614 RpcClient port_memory;
615 port_memory.open(
"/reaching/tomemory");
616 yarp.connect(
"/reaching/tomemory",
"/memory/rpc");
618 cmd.addVocab(Vocab::encode(
"ask"));
619 Bottle &content = cmd.addList().addList();
620 content.addString(
"name");
621 content.addString(
"==");
622 content.addString(
"Duck");
624 port_memory.write(cmd, rep);
628 if (rep.get(0).asVocab() == Vocab::encode(
"ack"))
630 if (Bottle *idField = rep.get(1).asList())
632 if (Bottle *idValues = idField->get(1).asList())
634 int id = idValues->get(0).asInt();
638 cmd.addVocab(Vocab::encode(
"get"));
639 Bottle& content = cmd.addList();
640 Bottle& list_bid = content.addList();
642 list_bid.addString(
"id");
645 Bottle& list_propSet = content.addList();
646 list_propSet.addString(
"propSet");
648 Bottle& list_items = list_propSet.addList();
649 list_items.addString(
"position_2d_left");
652 port_memory.write(cmd, reply_prop);
654 if (reply_prop.get(0).asVocab() == Vocab::encode(
"ack"))
656 if (Bottle* propField = reply_prop.get(1).asList())
658 if (Bottle* position_2d = propField->find(
"position_2d_left").asList())
660 if (position_2d->size() == 4)
662 l_px_goal_.resize(2);
663 l_px_goal_[0] = position_2d->get(0).asDouble() + (position_2d->get(2).asDouble() - position_2d->get(0).asDouble()) / 2;
664 l_px_goal_[1] = position_2d->get(1).asDouble() + (position_2d->get(3).asDouble() - position_2d->get(1).asDouble()) / 2;
667 port_sfm.open(
"/reaching/tosfm");
668 yarp.connect(
"/reaching/tosfm",
"/SFM/rpc");
672 cmd.addInt(l_px_goal_[0]);
673 cmd.addInt(l_px_goal_[1]);
676 port_sfm.write(cmd, reply_pos);
678 if (reply_pos.size() == 5)
681 location_[0] = reply_pos.get(0).asDouble();
682 location_[1] = reply_pos.get(1).asDouble();
683 location_[2] = reply_pos.get(2).asDouble();
685 r_px_goal_.resize(2);
686 r_px_goal_[0] = reply_pos.get(3).asDouble();
687 r_px_goal_[1] = reply_pos.get(4).asDouble();
691 reply.addString(
"nack_9");
694 yarp.disconnect(
"/reaching/tosfm",
"/SFM/rpc");
699 reply.addString(
"nack_8");
704 reply.addString(
"nack_7");
709 reply.addString(
"nack_6");
714 reply.addString(
"nack_5");
719 reply.addString(
"nack_4");
724 reply.addString(
"nack_3");
729 reply.addString(
"nack_2");
734 reply.addString(
"nack_1");
736 yarp.disconnect(
"/reaching/tomemory",
"/memory/rpc");
739 yInfo() <<
"l_px_location: " << l_px_goal_.toString();
740 yInfo() <<
"r_px_location: " << r_px_goal_.toString();
741 yInfo() <<
"location: " << location_.toString();
743 reply.addString(
"ack");
748 case VOCAB3(
'p',
'o',
's'):
753 location_[0] = -0.412;
754 location_[1] = 0.0435;
755 location_[2] = 0.0524;
757 yInfo() <<
"location: " << location_.toString();
764 case VOCAB3(
's',
'f',
'm'):
771 Bottle* click_left = port_click_left_.read (
true);
773 l_px_goal_.resize(2);
774 l_px_goal_[0] = click_left->get(0).asDouble();
775 l_px_goal_[1] = click_left->get(1).asDouble();
779 port_sfm.open(
"/reaching/tosfm");
780 yarp.connect(
"/reaching/tosfm",
"/SFM/rpc");
784 cmd.addInt(l_px_goal_[0]);
785 cmd.addInt(l_px_goal_[1]);
788 port_sfm.write(cmd, reply_pos);
790 if (reply_pos.size() == 5)
793 location_[0] = reply_pos.get(0).asDouble();
794 location_[1] = reply_pos.get(1).asDouble();
795 location_[2] = reply_pos.get(2).asDouble();
797 yInfo() <<
"location: " << location_.toString();
799 r_px_goal_.resize(2);
800 r_px_goal_[0] = reply_pos.get(3).asDouble();
801 r_px_goal_[1] = reply_pos.get(4).asDouble();
805 reply.addString(
"nack");
808 yarp.disconnect(
"/reaching/tosfm",
"/SFM/rpc");
816 case VOCAB4(
'i',
'n',
'i',
't'):
823 Vector od = dcm2axis(Od);
832 double traj_time = 0.0;
833 itf_rightarm_cart_->getTrajTime(&traj_time);
835 if (traj_time == traj_time_)
839 icub_index_.getChainJoints(readRootToFingers().subVector(3, 18), chain_joints);
841 Matrix tip_pose_index = icub_index_.getH((M_PI/180.0) * chain_joints);
842 Vector tip_x = tip_pose_index.getCol(3);
843 Vector tip_o = dcm2axis(tip_pose_index);
844 itf_rightarm_cart_->attachTipFrame(tip_x, tip_o);
846 location_[0] += 0.03;
847 location_[1] += 0.07;
858 gaze_loc(0) = -0.579;
860 gaze_loc(2) = -0.280;
867 itf_rightarm_cart_->storeContext(&ctxt);
869 itf_rightarm_cart_->setLimits(0, 20.0, 20.0);
871 itf_rightarm_cart_->goToPoseSync(location_, od);
872 itf_rightarm_cart_->waitMotionDone();
873 itf_rightarm_cart_->stopControl();
875 itf_rightarm_cart_->restoreContext(ctxt);
876 itf_rightarm_cart_->deleteContext(ctxt);
878 itf_gaze_->lookAtFixationPointSync(gaze_loc);
879 itf_gaze_->waitMotionDone();
882 itf_rightarm_cart_->removeTipFrame();
884 reply.addString(
"ack");
888 reply.addString(
"nack");
894 case VOCAB4(
's',
't',
'o',
'p'):
896 if (itf_rightarm_cart_->stopControl())
898 itf_rightarm_cart_->removeTipFrame();
900 reply.addString(
"ack");
904 reply.addString(
"nack_2");
911 reply.addString(
"nack");
920 yInfo() <<
"Interrupting module.\nPort cleanup...";
924 port_estimates_in_.interrupt();
925 port_image_left_in_.interrupt();
926 port_image_left_out_.interrupt();
927 port_click_left_.interrupt();
928 port_image_right_in_.interrupt();
929 port_image_right_out_.interrupt();
930 port_click_right_.interrupt();
931 handler_port_.interrupt();
938 yInfo() <<
"Calling close functions...";
940 port_estimates_in_.close();
941 port_image_left_in_.close();
942 port_image_left_out_.close();
943 port_click_left_.close();
944 port_image_right_in_.close();
945 port_image_right_out_.close();
946 port_click_right_.close();
948 itf_rightarm_cart_->removeTipFrame();
950 if (rightarm_cartesian_driver_.isValid()) rightarm_cartesian_driver_.close();
951 if (gaze_driver_.isValid()) gaze_driver_.close();
953 handler_port_.close();
960 bool should_stop_ =
false;
999 double traj_time_ = 2.5;
1003 bool take_estimates_ =
false;
1013 Property rightarm_cartesian_options;
1014 rightarm_cartesian_options.put(
"device",
"cartesiancontrollerclient");
1015 rightarm_cartesian_options.put(
"local",
"/reaching/cart_right_arm");
1016 rightarm_cartesian_options.put(
"remote",
"/icub/cartesianController/right_arm");
1018 rightarm_cartesian_driver_.open(rightarm_cartesian_options);
1019 if (rightarm_cartesian_driver_.isValid())
1021 rightarm_cartesian_driver_.view(itf_rightarm_cart_);
1022 if (!itf_rightarm_cart_)
1024 yError() <<
"Error getting ICartesianControl interface.";
1027 yInfo() <<
"cartesiancontrollerclient succefully opened.";
1031 yError() <<
"Error opening cartesiancontrollerclient device.";
1035 if (!itf_rightarm_cart_->setTrajTime(traj_time_))
1037 yError() <<
"Error setting ICartesianControl trajectory time.";
1040 yInfo() <<
"Succesfully set ICartesianControl trajectory time!";
1042 if (!itf_rightarm_cart_->setInTargetTol(0.01))
1044 yError() <<
"Error setting ICartesianControl target tolerance.";
1047 yInfo() <<
"Succesfully set ICartesianControl target tolerance!";
1054 Property gaze_option;
1055 gaze_option.put(
"device",
"gazecontrollerclient");
1056 gaze_option.put(
"local",
"/reaching/gaze");
1057 gaze_option.put(
"remote",
"/iKinGazeCtrl");
1059 gaze_driver_.open(gaze_option);
1060 if (gaze_driver_.isValid())
1062 gaze_driver_.view(itf_gaze_);
1065 yError() <<
"Error getting IGazeControl interface.";
1071 yError() <<
"Gaze control device not available.";
1080 Property rightarm_remote_options;
1081 rightarm_remote_options.put(
"device",
"remote_controlboard");
1082 rightarm_remote_options.put(
"local",
"/reaching/control_right_arm");
1083 rightarm_remote_options.put(
"remote",
"/icub/right_arm");
1085 rightarm_remote_driver_.open(rightarm_remote_options);
1086 if (rightarm_remote_driver_.isValid())
1088 yInfo() <<
"Right arm remote_controlboard succefully opened.";
1090 rightarm_remote_driver_.view(itf_rightarm_enc_);
1091 if (!itf_rightarm_enc_)
1093 yError() <<
"Error getting right arm IEncoders interface.";
1097 rightarm_remote_driver_.view(itf_fingers_lim_);
1098 if (!itf_fingers_lim_)
1100 yError() <<
"Error getting IControlLimits interface.";
1106 yError() <<
"Error opening right arm remote_controlboard device.";
1115 Property torso_remote_options;
1116 torso_remote_options.put(
"device",
"remote_controlboard");
1117 torso_remote_options.put(
"local",
"/reaching/control_torso");
1118 torso_remote_options.put(
"remote",
"/icub/torso");
1120 torso_remote_driver_.open(torso_remote_options);
1121 if (torso_remote_driver_.isValid())
1123 yInfo() <<
"Torso remote_controlboard succefully opened.";
1125 torso_remote_driver_.view(itf_torso_enc_);
1126 if (!itf_torso_enc_)
1128 yError() <<
"Error getting torso IEncoders interface.";
1136 yError() <<
"Error opening Torso remote_controlboard device.";
1144 itf_rightarm_cart_->getDOF(curDOF);
1145 yInfo() <<
"Old DOF: [" + curDOF.toString(0) +
"].";
1146 yInfo() <<
"Setting iCub to use the DOF from the torso.";
1147 Vector newDOF(curDOF);
1151 if (!itf_rightarm_cart_->setDOF(newDOF, curDOF))
1153 yError() <<
"Cannot use torso DOF.";
1156 yInfo() <<
"Setting the DOF done.";
1157 yInfo() <<
"New DOF: [" + curDOF.toString(0) +
"]";
1165 itf_rightarm_cart_->getDOF(curDOF);
1166 yInfo() <<
"Old DOF: [" + curDOF.toString(0) +
"].";
1167 yInfo() <<
"Setting iCub to use the DOF from the torso.";
1168 Vector newDOF(curDOF);
1172 if (!itf_rightarm_cart_->setDOF(newDOF, curDOF))
1174 yError() <<
"Cannot use torso DOF.";
1177 yInfo() <<
"Setting the DOF done.";
1178 yInfo() <<
"New DOF: [" + curDOF.toString(0) +
"]";
1185 Vector torso_enc(3);
1186 itf_torso_enc_->getEncoders(torso_enc.data());
1188 std::swap(torso_enc(0), torso_enc(2));
1195 Vector rightarm_encoder(16);
1196 itf_rightarm_enc_->getEncoders(rightarm_encoder.data());
1198 Vector root_fingers_enc(19);
1199 root_fingers_enc.setSubvector(0, readTorso());
1201 root_fingers_enc.setSubvector(3, rightarm_encoder);
1203 return root_fingers_enc;
1211 if (!yarp.checkNetwork(3.0))
1213 yError() <<
"YARP seems unavailable!";
1214 return EXIT_FAILURE;
1219 reaching.runModule(rf);
1221 return EXIT_SUCCESS;
int main(int argc, char *argv[])
BufferedPort< Bottle > port_px_right_endeffector
IControlLimits * itf_fingers_lim_
bool setRightArmRemoteControlboard()
BufferedPort< ImageOf< PixelRgb > > port_image_right_in_
BufferedPort< Bottle > port_px_left_endeffector
PolyDriver rightarm_cartesian_driver_
bool configure(ResourceFinder &rf)
IEncoders * itf_rightarm_enc_
BufferedPort< ImageOf< PixelRgb > > port_image_left_out_
bool setTorsoRemoteControlboard()
BufferedPort< Bottle > port_click_left_
BufferedPort< ImageOf< PixelRgb > > port_image_left_in_
PolyDriver rightarm_remote_driver_
bool setRightArmCartesianController()
bool respond(const Bottle &command, Bottle &reply)
IEncoders * itf_torso_enc_
Vector readRootToFingers()
PolyDriver torso_remote_driver_
BufferedPort< ImageOf< PixelRgb > > port_image_right_out_
ICartesianControl * itf_rightarm_cart_
BufferedPort< Bottle > port_click_right_
BufferedPort< Vector > port_estimates_in_