visual-tracking-control
VisualProprioception.cpp
Go to the documentation of this file.
1 #include <VisualProprioception.h>
2 
3 #include <cmath>
4 #include <exception>
5 #include <iostream>
6 #include <utility>
7 
8 #include <iCub/ctrl/math.h>
9 #include <yarp/math/Math.h>
10 #include <yarp/os/LogStream.h>
11 #include <yarp/os/Property.h>
12 #include <yarp/os/ResourceFinder.h>
13 
14 using namespace cv;
15 using namespace Eigen;
16 using namespace iCub::iKin;
17 using namespace iCub::ctrl;
18 using namespace yarp::dev;
19 using namespace yarp::os;
20 using namespace yarp::math;
21 using yarp::sig::Vector;
22 
23 
25  const bool use_forearm,
26  const int num_images,
27  const double resolution_ratio,
28  const ConstString& cam_sel,
29  const ConstString& laterality,
30  const ConstString& context) :
31  laterality_(laterality),
32  icub_arm_(iCubArm(laterality+"_v2")),
33  icub_kin_finger_{iCubFinger(laterality+"_thumb"), iCubFinger(laterality+"_index"), iCubFinger(laterality+"_middle")},
34  cam_sel_(cam_sel),
35  resolution_ratio_(resolution_ratio),
36  use_thumb_(use_thumb),
37  use_forearm_(use_forearm)
38 {
39  ResourceFinder rf;
40 
41  icub_kin_eye_ = iCubEye(cam_sel_+"_v2");
42  icub_kin_eye_.setAllConstraints(false);
43  icub_kin_eye_.releaseLink(0);
44  icub_kin_eye_.releaseLink(1);
45  icub_kin_eye_.releaseLink(2);
46 
47  if (openGazeController())
48  {
49  Bottle cam_info;
50  itf_gaze_->getInfo(cam_info);
51  yInfo() << log_ID_ << "[CAM PARAMS]" << cam_info.toString();
52  Bottle* cam_sel_intrinsic = cam_info.findGroup("camera_intrinsics_" + cam_sel_).get(1).asList();
53  cam_width_ = cam_info.findGroup("camera_width_" + cam_sel_).get(1).asInt();
54  cam_height_ = cam_info.findGroup("camera_height_" + cam_sel_).get(1).asInt();
55  cam_fx_ = static_cast<float>(cam_sel_intrinsic->get(0).asDouble());
56  cam_cx_ = static_cast<float>(cam_sel_intrinsic->get(2).asDouble());
57  cam_fy_ = static_cast<float>(cam_sel_intrinsic->get(5).asDouble());
58  cam_cy_ = static_cast<float>(cam_sel_intrinsic->get(6).asDouble());
59  }
60  else
61  {
62  yWarning() << log_ID_ << "[CAM PARAMS]" << "No intrinisc camera information could be found by the ctor. Looking for fallback values in config.ini.";
63 
64  rf.setVerbose();
65  rf.setDefaultContext(context);
66  rf.setDefaultConfigFile("config.ini");
67  rf.configure(0, YARP_NULLPTR);
68 
69  Bottle* fallback_intrinsic = rf.findGroup("FALLBACK").find("intrinsic_" + cam_sel_).asList();
70  if (fallback_intrinsic)
71  {
72  yInfo() << log_ID_ << "[FALLBACK][CAM PARAMS]" << fallback_intrinsic->toString();
73 
74  cam_width_ = static_cast<unsigned int>(fallback_intrinsic->get(0).asInt());
75  cam_height_ = static_cast<unsigned int>(fallback_intrinsic->get(1).asInt());
76  cam_fx_ = static_cast<float>(fallback_intrinsic->get(2).asDouble());
77  cam_cx_ = static_cast<float>(fallback_intrinsic->get(3).asDouble());
78  cam_fy_ = static_cast<float>(fallback_intrinsic->get(4).asDouble());
79  cam_cy_ = static_cast<float>(fallback_intrinsic->get(5).asDouble());
80  }
81  else
82  {
83  yWarning() << log_ID_ << "[CAM PARAMS]" << "No fallback values could be found in config.ini by the ctor for the intrinisc camera parameters. Falling (even more) back to iCub_SIM values.";
84  cam_width_ = 320;
85  cam_height_ = 240;
86  cam_fx_ = 257.34;
87  cam_cx_ = 160.0;
88  cam_fy_ = 257.34;
89  cam_cy_ = 120.0;
90  }
91  }
92 
93  yInfo() << log_ID_ << "[CAM]" << "Found camera information:";
94  yInfo() << log_ID_ << "[CAM]" << " - width:" << cam_width_;
95  yInfo() << log_ID_ << "[CAM]" << " - height:" << cam_height_;
96  yInfo() << log_ID_ << "[CAM]" << " - fx:" << cam_fx_;
97  yInfo() << log_ID_ << "[CAM]" << " - fy:" << cam_fy_;
98  yInfo() << log_ID_ << "[CAM]" << " - cx:" << cam_cx_;
99  yInfo() << log_ID_ << "[CAM]" << " - cy:" << cam_cy_;
100 
101  cam_width_ /= resolution_ratio_;
102  cam_height_ /= resolution_ratio_;
103  cam_fx_ /= resolution_ratio_;
104  cam_cx_ /= resolution_ratio_;
105  cam_fy_ /= resolution_ratio_;
106  cam_cy_ /= resolution_ratio_;
107 
108  yInfo() << log_ID_ << "[CAM]" << "Running with:";
109  yInfo() << log_ID_ << "[CAM]" << " - resolution_ratio:" << resolution_ratio_;
110  yInfo() << log_ID_ << "[CAM]" << " - width:" << cam_width_;
111  yInfo() << log_ID_ << "[CAM]" << " - height:" << cam_height_;
112  yInfo() << log_ID_ << "[CAM]" << " - fx:" << cam_fx_;
113  yInfo() << log_ID_ << "[CAM]" << " - fy:" << cam_fy_;
114  yInfo() << log_ID_ << "[CAM]" << " - cx:" << cam_cx_;
115  yInfo() << log_ID_ << "[CAM]" << " - cy:" << cam_cy_;
116 
117  cam_x_[0] = 0;
118  cam_x_[1] = 0;
119  cam_x_[2] = 0;
120 
121  cam_o_[0] = 0;
122  cam_o_[1] = 0;
123  cam_o_[2] = 0;
124  cam_o_[3] = 0;
125 
126  /* Comment/Uncomment to add/remove limbs */
127  rf.setDefaultContext(context + "/mesh");
128 
129  cad_obj_["palm"] = rf.findFileByName("r_palm.obj");
130  if (!file_found(cad_obj_["palm"]))
131  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_palm.obj not found!");
132 
133  if (use_thumb)
134  {
135  cad_obj_["thumb1"] = rf.findFileByName("r_tl0.obj");
136  if (!file_found(cad_obj_["thumb1"]))
137  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_tl0.obj not found!");
138  cad_obj_["thumb2"] = rf.findFileByName("r_tl1.obj");
139  if (!file_found(cad_obj_["thumb2"]))
140  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_tl1.obj not found!");
141  cad_obj_["thumb3"] = rf.findFileByName("r_tl2.obj");
142  if (!file_found(cad_obj_["thumb3"]))
143  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_tl2.obj not found!");
144  cad_obj_["thumb4"] = rf.findFileByName("r_tl3.obj");
145  if (!file_found(cad_obj_["thumb4"]))
146  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_tl3.obj not found!");
147  cad_obj_["thumb5"] = rf.findFileByName("r_tl4.obj");
148  if (!file_found(cad_obj_["thumb5"]))
149  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_tl4.obj not found!");
150  }
151 
152  cad_obj_["index0"] = rf.findFileByName("r_indexbase.obj");
153  if (!file_found(cad_obj_["index0"]))
154  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_indexbase.obj not found!");
155  cad_obj_["index1"] = rf.findFileByName("r_ail0.obj");
156  if (!file_found(cad_obj_["index1"]))
157  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_ail0.obj not found!");
158  cad_obj_["index2"] = rf.findFileByName("r_ail1.obj");
159  if (!file_found(cad_obj_["index2"]))
160  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_ail1.obj not found!");
161  cad_obj_["index3"] = rf.findFileByName("r_ail2.obj");
162  if (!file_found(cad_obj_["index3"]))
163  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_ail2.obj not found!");
164  cad_obj_["index4"] = rf.findFileByName("r_ail3.obj");
165  if (!file_found(cad_obj_["index4"]))
166  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_ail3.obj not found!");
167 
168  cad_obj_["medium0"] = rf.findFileByName("r_ml0.obj");
169  if (!file_found(cad_obj_["medium0"]))
170  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_ml0.obj not found!");
171  cad_obj_["medium1"] = rf.findFileByName("r_ml1.obj");
172  if (!file_found(cad_obj_["medium1"]))
173  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_ml1.obj not found!");
174  cad_obj_["medium2"] = rf.findFileByName("r_ml2.obj");
175  if (!file_found(cad_obj_["medium2"]))
176  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_ml2.obj not found!");
177  cad_obj_["medium3"] = rf.findFileByName("r_ml3.obj");
178  if (!file_found(cad_obj_["medium3"]))
179  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_ml3.obj not found!");
180 
181  if (use_forearm)
182  {
183  cad_obj_["forearm"] = rf.findFileByName("r_forearm.obj");
184  if (!file_found(cad_obj_["forearm"]))
185  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::FILE\nERROR: 3D mesh file r_forearm.obj not found!");
186  }
187 
188  rf.setDefaultContext(context + "/shader");
189  ConstString shader_path = rf.findFileByName("shader_model.vert");
190  if (!file_found(shader_path))
191  throw std::runtime_error("ERROR::VISUALPROPRIOCEPTION::CTOR::DIR\nERROR: shader directory not found!");
192  size_t rfind_slash = shader_path.rfind("/");
193  size_t rfind_backslash = shader_path.rfind("\\");
194  shader_path = shader_path.substr(0, rfind_slash > rfind_backslash ? rfind_slash : rfind_backslash);
195 
196  try
197  {
198  si_cad_ = new SICAD(cad_obj_,
199  cam_width_, cam_height_, cam_fx_, cam_fy_, cam_cx_, cam_cy_,
200  num_images,
201  {1.0, 0.0, 0.0, static_cast<float>(M_PI)},
202  shader_path,
203  false);
204  }
205  catch (const std::runtime_error& e)
206  {
207  throw std::runtime_error(e.what());
208  }
209 
210  icub_kin_finger_[0].setAllConstraints(false);
211  icub_kin_finger_[1].setAllConstraints(false);
212  icub_kin_finger_[2].setAllConstraints(false);
213 
214  icub_arm_.setAllConstraints(false);
215  icub_arm_.releaseLink(0);
216  icub_arm_.releaseLink(1);
217  icub_arm_.releaseLink(2);
218 
219  /* Set analogs (optional by default) */
220  right_hand_analogs_bounds_ = yarp::sig::Matrix(15, 2);
221 
222  right_hand_analogs_bounds_(0, 0) = 218.0; right_hand_analogs_bounds_(0, 1) = 61.0;
223  right_hand_analogs_bounds_(1, 0) = 210.0; right_hand_analogs_bounds_(1, 1) = 20.0;
224  right_hand_analogs_bounds_(2, 0) = 234.0; right_hand_analogs_bounds_(2, 1) = 16.0;
225 
226  right_hand_analogs_bounds_(3, 0) = 224.0; right_hand_analogs_bounds_(3, 1) = 15.0;
227  right_hand_analogs_bounds_(4, 0) = 206.0; right_hand_analogs_bounds_(4, 1) = 18.0;
228  right_hand_analogs_bounds_(5, 0) = 237.0; right_hand_analogs_bounds_(5, 1) = 23.0;
229 
230  right_hand_analogs_bounds_(6, 0) = 250.0; right_hand_analogs_bounds_(6, 1) = 8.0;
231  right_hand_analogs_bounds_(7, 0) = 195.0; right_hand_analogs_bounds_(7, 1) = 21.0;
232  right_hand_analogs_bounds_(8, 0) = 218.0; right_hand_analogs_bounds_(8, 1) = 0.0;
233 
234  right_hand_analogs_bounds_(9, 0) = 220.0; right_hand_analogs_bounds_(9, 1) = 39.0;
235  right_hand_analogs_bounds_(10, 0) = 160.0; right_hand_analogs_bounds_(10, 1) = 10.0;
236  right_hand_analogs_bounds_(11, 0) = 209.0; right_hand_analogs_bounds_(11, 1) = 101.0;
237  right_hand_analogs_bounds_(12, 0) = 224.0; right_hand_analogs_bounds_(12, 1) = 63.0;
238  right_hand_analogs_bounds_(13, 0) = 191.0; right_hand_analogs_bounds_(13, 1) = 36.0;
239  right_hand_analogs_bounds_(14, 0) = 232.0; right_hand_analogs_bounds_(14, 1) = 98.0;
240 
241  /* Head encoders: /icub/head/state:o
242  Arm encoders: /icub/right_arm/state:o
243  Torso encoders: /icub/torso/state:o */
244  port_head_enc_.open ("/hand-tracking/VisualProprioception/" + cam_sel_ + "/head:i");
245  port_arm_enc_.open ("/hand-tracking/VisualProprioception/" + cam_sel_ + "/" + laterality_ + "_arm:i");
246  port_torso_enc_.open("/hand-tracking/VisualProprioception/" + cam_sel_ + "/torso:i");
247 }
248 
249 
251 {
252  drv_gaze_.close();
253  drv_right_hand_analog_.close();
254 
255  port_head_enc_.close();
256  port_arm_enc_.close();
257  port_torso_enc_.close();
258 
259  delete si_cad_;
260 }
261 
262 
264  cam_width_(proprio.cam_width_),
265  cam_height_(proprio.cam_height_),
266  cam_fx_(proprio.cam_fx_),
267  cam_cx_(proprio.cam_cx_),
268  cam_fy_(proprio.cam_fy_),
269  cam_cy_(proprio.cam_cy_),
270  cad_obj_(proprio.cad_obj_),
271  si_cad_(proprio.si_cad_)
272 {
273  cam_x_[0] = proprio.cam_x_[0];
274  cam_x_[1] = proprio.cam_x_[1];
275  cam_x_[2] = proprio.cam_x_[2];
276 
277  cam_o_[0] = proprio.cam_o_[0];
278  cam_o_[1] = proprio.cam_o_[1];
279  cam_o_[2] = proprio.cam_o_[2];
280  cam_o_[3] = proprio.cam_o_[3];
281 
282  icub_kin_finger_[0] = proprio.icub_kin_finger_[0];
283  icub_kin_finger_[1] = proprio.icub_kin_finger_[1];
284  icub_kin_finger_[2] = proprio.icub_kin_finger_[2];
285 
286  icub_arm_ = proprio.icub_arm_;
287 }
288 
289 
291  icub_arm_(std::move(proprio.icub_arm_)),
292  cam_width_(std::move(proprio.cam_width_)),
293  cam_height_(std::move(proprio.cam_height_)),
294  cam_fx_(std::move(proprio.cam_fx_)),
295  cam_cx_(std::move(proprio.cam_cx_)),
296  cam_fy_(std::move(proprio.cam_fy_)),
297  cam_cy_(std::move(proprio.cam_cy_)),
298  cad_obj_(std::move(proprio.cad_obj_)),
299  si_cad_(std::move(proprio.si_cad_))
300 {
301  cam_x_[0] = proprio.cam_x_[0];
302  cam_x_[1] = proprio.cam_x_[1];
303  cam_x_[2] = proprio.cam_x_[2];
304 
305  cam_o_[0] = proprio.cam_o_[0];
306  cam_o_[1] = proprio.cam_o_[1];
307  cam_o_[2] = proprio.cam_o_[2];
308  cam_o_[3] = proprio.cam_o_[3];
309 
310  icub_kin_finger_[0] = std::move(proprio.icub_kin_finger_[0]);
311  icub_kin_finger_[1] = std::move(proprio.icub_kin_finger_[1]);
312  icub_kin_finger_[2] = std::move(proprio.icub_kin_finger_[2]);
313 
314  proprio.cam_x_[0] = 0.0;
315  proprio.cam_x_[1] = 0.0;
316  proprio.cam_x_[2] = 0.0;
317 
318  proprio.cam_o_[0] = 0.0;
319  proprio.cam_o_[1] = 0.0;
320  proprio.cam_o_[2] = 0.0;
321  proprio.cam_o_[3] = 0.0;
322 }
323 
324 
326 {
327  VisualProprioception tmp(proprio);
328  *this = std::move(tmp);
329 
330  return *this;
331 }
332 
333 
335 {
336  icub_arm_ = std::move(proprio.icub_arm_);
337 
338  icub_kin_finger_[0] = std::move(proprio.icub_kin_finger_[0]);
339  icub_kin_finger_[1] = std::move(proprio.icub_kin_finger_[1]);
340  icub_kin_finger_[2] = std::move(proprio.icub_kin_finger_[2]);
341 
342  cam_x_[0] = proprio.cam_x_[0];
343  cam_x_[1] = proprio.cam_x_[1];
344  cam_x_[2] = proprio.cam_x_[2];
345 
346  cam_o_[0] = proprio.cam_o_[0];
347  cam_o_[1] = proprio.cam_o_[1];
348  cam_o_[2] = proprio.cam_o_[2];
349  cam_o_[3] = proprio.cam_o_[3];
350 
351  cam_width_ = std::move(proprio.cam_width_);
352  cam_height_ = std::move(proprio.cam_height_);
353  cam_fx_ = std::move(proprio.cam_fx_);
354  cam_cx_ = std::move(proprio.cam_cx_);
355  cam_fy_ = std::move(proprio.cam_fy_);
356  cam_cy_ = std::move(proprio.cam_cy_);
357 
358  cad_obj_ = std::move(proprio.cad_obj_);
359  si_cad_ = std::move(proprio.si_cad_);
360 
361  proprio.cam_x_[0] = 0.0;
362  proprio.cam_x_[1] = 0.0;
363  proprio.cam_x_[2] = 0.0;
364 
365  proprio.cam_o_[0] = 0.0;
366  proprio.cam_o_[1] = 0.0;
367  proprio.cam_o_[2] = 0.0;
368  proprio.cam_o_[3] = 0.0;
369 
370  return *this;
371 }
372 
373 
374 void VisualProprioception::getModelPose(const Ref<const MatrixXf>& cur_states, std::vector<Superimpose::ModelPoseContainer>& hand_poses)
375 {
376  for (int j = 0; j < cur_states.cols(); ++j)
377  {
378  Superimpose::ModelPoseContainer hand_pose;
379  Superimpose::ModelPose pose;
380  Vector ee_t(4);
381  Vector ee_o(4);
382 
383 
384  ee_t(0) = cur_states(0, j);
385  ee_t(1) = cur_states(1, j);
386  ee_t(2) = cur_states(2, j);
387  ee_t(3) = 1.0;
388 
389  ee_o(0) = cur_states(3, j);
390  ee_o(1) = cur_states(4, j);
391  ee_o(2) = cur_states(5, j);
392  ee_o(3) = cur_states(6, j);
393 
394  pose.assign(ee_t.data(), ee_t.data()+3);
395  pose.insert(pose.end(), ee_o.data(), ee_o.data()+4);
396  hand_pose.emplace("palm", pose);
397 
398  /* Change index to add/remove limbs */
399  yarp::sig::Matrix Ha = axis2dcm(ee_o);
400  Ha.setCol(3, ee_t);
401  for (unsigned int fng = (use_thumb_ ? 0 : 1); fng < 3; ++fng)
402  {
403  std::string finger_s;
404  pose.clear();
405  if (fng != 0)
406  {
407  Vector j_x = (Ha * (icub_kin_finger_[fng].getH0().getCol(3))).subVector(0, 2);
408  Vector j_o = dcm2axis(Ha * icub_kin_finger_[fng].getH0());
409 
410  if (fng == 1) { finger_s = "index0"; }
411  else if (fng == 2) { finger_s = "medium0"; }
412 
413  pose.assign(j_x.data(), j_x.data()+3);
414  pose.insert(pose.end(), j_o.data(), j_o.data()+4);
415  hand_pose.emplace(finger_s, pose);
416  }
417 
418  for (size_t i = 0; i < icub_kin_finger_[fng].getN(); ++i)
419  {
420  Vector j_x = (Ha * (icub_kin_finger_[fng].getH(i, true).getCol(3))).subVector(0, 2);
421  Vector j_o = dcm2axis(Ha * icub_kin_finger_[fng].getH(i, true));
422 
423  if (fng == 0) { finger_s = "thumb"+std::to_string(i+1); }
424  else if (fng == 1) { finger_s = "index"+std::to_string(i+1); }
425  else if (fng == 2) { finger_s = "medium"+std::to_string(i+1); }
426 
427  pose.assign(j_x.data(), j_x.data()+3);
428  pose.insert(pose.end(), j_o.data(), j_o.data()+4);
429  hand_pose.emplace(finger_s, pose);
430  }
431  }
432  if (use_forearm_)
433  {
434  yarp::sig::Matrix invH6 = Ha *
435  getInvertedH(-0.0625, -0.02598, 0, -M_PI, -icub_arm_.getAng(9)) *
436  getInvertedH( 0, 0, -M_PI_2, -M_PI_2, -icub_arm_.getAng(8)) *
437  getInvertedH( 0, 0.1413, -M_PI_2, M_PI_2, 0);
438  Vector j_x = invH6.getCol(3).subVector(0, 2);
439  Vector j_o = dcm2axis(invH6);
440  pose.clear();
441  pose.assign(j_x.data(), j_x.data()+3);
442  pose.insert(pose.end(), j_o.data(), j_o.data()+4);
443  hand_pose.emplace("forearm", pose);
444  }
445 
446  hand_poses.push_back(hand_pose);
447  }
448 }
449 
450 
451 void VisualProprioception::observe(const Ref<const MatrixXf>& cur_states, OutputArray observations)
452 {
453  std::vector<Superimpose::ModelPoseContainer> hand_poses;
454  getModelPose(cur_states, hand_poses);
455 
456  observations.create(cam_height_ * si_cad_->getTilesRows(), cam_width_ * si_cad_->getTilesCols(), CV_8UC3);
457  Mat hand_ogl = observations.getMat();
458 
459  si_cad_->superimpose(hand_poses, cam_x_, cam_o_, hand_ogl);
460 }
461 
462 
463 bool VisualProprioception::setProperty(const std::string property)
464 {
465  if (property == "VP_PARAMS")
466  return setiCubParams();
467 
468  if (property == "VP_ANALOGS_ON")
469  return openAnalogs();
470  if (property == "VP_ANALOGS_OFF")
471  return closeAnalogs();
472 
473  return false;
474 }
475 
476 
478 {
479  Vector left_eye_pose = icub_kin_eye_.EndEffPose(CTRL_DEG2RAD * readRootToEye(cam_sel_));
480 
481  cam_x_[0] = left_eye_pose(0);
482  cam_x_[1] = left_eye_pose(1);
483  cam_x_[2] = left_eye_pose(2);
484 
485  cam_o_[0] = left_eye_pose(3);
486  cam_o_[1] = left_eye_pose(4);
487  cam_o_[2] = left_eye_pose(5);
488  cam_o_[3] = left_eye_pose(6);
489 
490 
491  Vector q = readRootToFingers();
492 // q(10) = 32.0;
493 // q(11) = 30.0;
494 // q(12) = 0.0;
495 // q(13) = 0.0;
496 // q(14) = 0.0;
497 // q(15) = 0.0;
498 // q(16) = 0.0;
499 // q(17) = 0.0;
500 
501 
502  if (analogs_)
503  {
504  Vector analogs;
505  itf_right_hand_analog_->read(analogs);
507  }
508  else
509  setArmJoints(q);
510 
511  return true;
512 }
513 
514 
515 void VisualProprioception::setArmJoints(const Vector& q)
516 {
517  icub_arm_.setAng(q.subVector(0, 9) * (M_PI/180.0));
518 
519  Vector chainjoints;
520  for (size_t i = 0; i < 3; ++i)
521  {
522  icub_kin_finger_[i].getChainJoints(q.subVector(3, 18), chainjoints);
523  icub_kin_finger_[i].setAng(chainjoints * (M_PI/180.0));
524  }
525 }
526 
527 
528 void VisualProprioception::setArmJoints(const Vector& q, const Vector& analogs, const yarp::sig::Matrix& analog_bounds)
529 {
530  icub_arm_.setAng(q.subVector(0, 9) * (M_PI/180.0));
531 
532  Vector chainjoints;
533  for (size_t i = 0; i < 3; ++i)
534  {
535  icub_kin_finger_[i].getChainJoints(q.subVector(3, 18), analogs, chainjoints, analog_bounds);
536  icub_kin_finger_[i].setAng(chainjoints * (M_PI/180.0));
537  }
538 }
539 
540 
541 bool VisualProprioception::file_found(const ConstString& file)
542 {
543  if (!file.empty())
544  {
545  yInfo() << log_ID_ << "File " + file.substr(file.rfind("/") + 1) + " found.";
546  return true;
547  }
548 
549  return false;
550 }
551 
552 
554 {
555  return si_cad_->getTilesNumber();
556 }
557 
558 
560 {
561  return si_cad_->getTilesRows();
562 }
563 
564 
566 {
567  return si_cad_->getTilesCols();
568 }
569 
570 
572 {
573  return cam_width_;
574 }
575 
576 
578 {
579  return cam_height_;
580 }
581 
582 
584 {
585  return cam_fx_;
586 }
587 
588 
590 {
591  return cam_fy_;
592 }
593 
594 
596 {
597  return cam_cx_;
598 }
599 
600 
602 {
603  return cam_cy_;
604 }
605 
606 
607 yarp::sig::Matrix VisualProprioception::getInvertedH(const double a, const double d, const double alpha, const double offset, const double q)
608 {
623  yarp::sig::Matrix H(4, 4);
624 
625  double theta = offset + q;
626  double c_th = cos(theta);
627  double s_th = sin(theta);
628  double c_al = cos(alpha);
629  double s_al = sin(alpha);
630 
631  H(0,0) = c_th;
632  H(0,1) = -s_th;
633  H(0,2) = 0;
634  H(0,3) = a;
635 
636  H(1,0) = s_th * c_al;
637  H(1,1) = c_th * c_al;
638  H(1,2) = -s_al;
639  H(1,3) = -d * s_al;
640 
641  H(2,0) = s_th * s_al;
642  H(2,1) = c_th * s_al;
643  H(2,2) = c_al;
644  H(2,3) = d * c_al;
645 
646  H(3,0) = 0;
647  H(3,1) = 0;
648  H(3,2) = 0;
649  H(3,3) = 1;
650 
651  return H;
652 }
653 
654 
656 {
657  Property opt_gaze;
658  opt_gaze.put("device", "gazecontrollerclient");
659  opt_gaze.put("local", "/hand-tracking/VisualProprioception/" + cam_sel_ + "/gaze:i");
660  opt_gaze.put("remote", "/iKinGazeCtrl");
661 
662  if (drv_gaze_.open(opt_gaze))
663  {
664  drv_gaze_.view(itf_gaze_);
665  if (!itf_gaze_)
666  {
667  yError() << log_ID_ << "Cannot get head gazecontrollerclient interface!";
668 
669  drv_gaze_.close();
670  return false;
671  }
672  }
673  else
674  {
675  yError() << log_ID_ << "Cannot open head gazecontrollerclient!";
676 
677  return false;
678  }
679 
680  return true;
681 }
682 
683 
685 {
686  if (!analogs_)
687  {
688  Property opt_right_hand_analog;
689  opt_right_hand_analog.put("device", "analogsensorclient");
690  opt_right_hand_analog.put("local", "/hand-tracking/VisualProprioception/" + cam_sel_ + "/" + laterality_ + "_hand/analog:i");
691  opt_right_hand_analog.put("remote", "/icub/right_hand/analog:o");
692 
693  if (drv_right_hand_analog_.open(opt_right_hand_analog))
694  {
697  {
698  yError() << log_ID_ << "Cannot get right hand analogsensorclient interface!";
699 
700  drv_right_hand_analog_.close();
701  return false;
702  }
703  }
704  else
705  {
706  yError() << log_ID_ << "Cannot open right hand analogsensorclient!";
707 
708  return false;
709  }
710 
711  analogs_ = true;
712 
713  return true;
714  }
715  else return false;
716 }
717 
718 
720 {
721  if (analogs_)
722  {
723  drv_right_hand_analog_.close();
724 
725  analogs_ = false;
726 
727  return true;
728  }
729  else return false;
730 }
731 
732 
734 {
735  Bottle* b = port_torso_enc_.read(true);
736  if (!b) return Vector(3, 0.0);
737 
738  Vector torso_enc(3);
739  torso_enc(0) = b->get(2).asDouble();
740  torso_enc(1) = b->get(1).asDouble();
741  torso_enc(2) = b->get(0).asDouble();
742 
743  return torso_enc;
744 }
745 
746 
748 {
749  Bottle* b = port_arm_enc_.read(true);
750  if (!b) return Vector(19, 0.0);
751 
752  Vector root_fingers_enc(19);
753  root_fingers_enc.setSubvector(0, readTorso());
754  for (size_t i = 0; i < 16; ++i)
755  root_fingers_enc(3 + i) = b->get(i).asDouble();
756 
757  return root_fingers_enc;
758 }
759 
760 
761 Vector VisualProprioception::readRootToEye(const ConstString cam_sel)
762 {
763  Bottle* b = port_head_enc_.read(true);
764  if (!b) return Vector(8, 0.0);
765 
766  Vector root_eye_enc(8);
767  root_eye_enc.setSubvector(0, readTorso());
768  for (size_t i = 0; i < 4; ++i)
769  root_eye_enc(3 + i) = b->get(i).asDouble();
770 
771  if (cam_sel == "left") root_eye_enc(7) = b->get(4).asDouble() + b->get(5).asDouble()/2.0;
772  if (cam_sel == "right") root_eye_enc(7) = b->get(4).asDouble() - b->get(5).asDouble()/2.0;
773 
774  return root_eye_enc;
775 }
yarp::dev::IGazeControl * itf_gaze_
bool setProperty(const std::string property) override
yarp::sig::Vector readRootToFingers()
iCub::iKin::iCubEye icub_kin_eye_
const yarp::os::ConstString log_ID_
Definition: PlayGatePose.h:41
yarp::sig::Matrix right_hand_analogs_bounds_
yarp::os::ConstString laterality_
void observe(const Eigen::Ref< const Eigen::MatrixXf > &cur_states, cv::OutputArray observations) override
VisualProprioception & operator=(const VisualProprioception &proprio)
yarp::dev::PolyDriver drv_right_hand_analog_
iCub::iKin::iCubFinger icub_kin_finger_[3]
yarp::os::BufferedPort< yarp::os::Bottle > port_arm_enc_
SICAD::ModelPathContainer cad_obj_
yarp::sig::Vector readRootToEye(const yarp::os::ConstString cam_sel)
yarp::os::ConstString laterality_
Definition: PlayGatePose.h:44
void setArmJoints(const yarp::sig::Vector &q)
yarp::os::BufferedPort< yarp::os::Bottle > port_torso_enc_
Definition: PlayGatePose.h:29
yarp::os::ConstString cam_sel_
yarp::sig::Matrix getInvertedH(const double a, const double d, const double alpha, const double offset, const double q)
yarp::os::ConstString log_ID_
void getModelPose(const Eigen::Ref< const Eigen::MatrixXf > &cur_states, std::vector< Superimpose::ModelPoseContainer > &hand_poses)
yarp::os::BufferedPort< yarp::os::Bottle > port_head_enc_
bool file_found(const yarp::os::ConstString &file)
yarp::dev::PolyDriver drv_gaze_
yarp::dev::IAnalogSensor * itf_right_hand_analog_
yarp::os::BufferedPort< yarp::os::Bottle > port_torso_enc_
yarp::sig::Vector readTorso()
yarp::os::BufferedPort< yarp::os::Bottle > port_arm_enc_
Definition: PlayGatePose.h:30
iCub::iKin::iCubArm icub_arm_
VisualProprioception(const bool use_thumb, const bool use_forearm, const int num_images, const double resolution_ratio, const yarp::os::ConstString &cam_sel, const yarp::os::ConstString &laterality, const yarp::os::ConstString &context)