visual-tracking-control
iCubGatePose.cpp
Go to the documentation of this file.
1 #include <iCubGatePose.h>
2 
3 #include <iCub/ctrl/math.h>
4 #include <yarp/eigen/Eigen.h>
5 #include <yarp/os/LogStream.h>
6 #include <yarp/os/Property.h>
7 
8 using namespace bfl;
9 using namespace cv;
10 using namespace Eigen;
11 using namespace iCub::ctrl;
12 using namespace iCub::iKin;
13 using namespace yarp::eigen;
14 using namespace yarp::math;
15 using namespace yarp::os;
16 using namespace yarp::sig;
17 
18 
19 iCubGatePose::iCubGatePose(std::unique_ptr<PFVisualCorrection> visual_correction,
20  const double gate_x, const double gate_y, const double gate_z,
21  const double gate_rotation,
22  const double gate_aperture,
23  const yarp::os::ConstString& robot, const yarp::os::ConstString& laterality, const yarp::os::ConstString& port_prefix) :
24  GatePose(std::move(visual_correction),
25  gate_x, gate_y, gate_z,
26  gate_rotation,
27  gate_aperture),
28  icub_kin_arm_(iCubArm(laterality + "_v2")), robot_(robot), laterality_(laterality), port_prefix_(port_prefix)
29 {
30  icub_kin_arm_.setAllConstraints(false);
31  icub_kin_arm_.releaseLink(0);
32  icub_kin_arm_.releaseLink(1);
33  icub_kin_arm_.releaseLink(2);
34 
35 
36  Property opt_arm_enc;
37  opt_arm_enc.put("device", "remote_controlboard");
38  opt_arm_enc.put("local", "/hand-tracking/" + ID_ + "/" + port_prefix + "/control_" + laterality_ + "_arm");
39  opt_arm_enc.put("remote", "/" + robot_ + "/" + laterality_ + "_arm");
40 
41  yInfo() << log_ID_ << "Opening " + laterality_ + " arm remote_controlboard driver...";
42  if (drv_arm_enc_.open(opt_arm_enc))
43  {
44  yInfo() << log_ID_ << "Succesfully opened " + laterality_ + " arm remote_controlboard interface.";
45 
46  yInfo() << log_ID_ << "Getting " + laterality_ + " arm encoder interface...";
47 
49  if (!itf_arm_enc_)
50  {
51  yError() << log_ID_ << "Cannot get " + laterality_ + " arm encoder interface!";
52 
53  drv_arm_enc_.close();
54  throw std::runtime_error("ERROR::" + ID_ + "::CTOR::INTERFACE\nERROR: cannot get " + laterality_ + " arm encoder interface!");
55  }
56 
57  yInfo() << log_ID_ << "Succesfully got " + laterality_ + " arm encoder interface.";
58  }
59  else
60  {
61  yError() << log_ID_ << "Cannot open " + laterality_ + " arm remote_controlboard!";
62 
63  throw std::runtime_error("ERROR::" + ID_ + "::CTOR::DRIVER\nERROR: cannot open " + laterality_ + " arm remote_controlboard!");
64  }
65 
66 
67  Property opt_torso_enc;
68  opt_torso_enc.put("device", "remote_controlboard");
69  opt_torso_enc.put("local", "/hand-tracking/" + ID_ + "/" + port_prefix + "/control_torso");
70  opt_torso_enc.put("remote", "/" + robot_ + "/torso");
71 
72  yInfo() << log_ID_ << "Opening torso remote_controlboard driver...";
73 
74  if (drv_torso_enc_.open(opt_torso_enc))
75  {
76  yInfo() << log_ID_ << "Succesfully opened torso remote_controlboard driver.";
77 
78  yInfo() << log_ID_ << "Getting torso encoder interface...";
79 
81  if (!itf_torso_enc_)
82  {
83  yError() << log_ID_ << "Cannot get torso encoder interface!";
84 
85  drv_torso_enc_.close();
86  throw std::runtime_error("ERROR::" + ID_ + "::CTOR::INTERFACE\nERROR: cannot get torso encoder interface!");
87  }
88 
89  yInfo() << log_ID_ << "Succesfully got torso encoder interface.";
90  }
91  else
92  {
93  yError() << log_ID_ << "Cannot open torso remote_controlboard!";
94 
95  throw std::runtime_error("ERROR::" + ID_ + "::CTOR::DRIVER\nERROR: cannot open torso remote_controlboard!");
96  }
97 
98  yInfo() << log_ID_ << "Succesfully initialized.";
99 }
100 
101 
102 iCubGatePose::iCubGatePose(std::unique_ptr<PFVisualCorrection> visual_correction,
103  const yarp::os::ConstString& robot, const yarp::os::ConstString& laterality, const yarp::os::ConstString& port_prefix) :
104  iCubGatePose(std::move(visual_correction), 0.1, 0.1, 0.1, 5, 30, robot, laterality, port_prefix) { }
105 
106 
108 
109 
111 {
112  int torso_enc_num;
113  itf_torso_enc_->getAxes(&torso_enc_num);
114  Vector enc_torso(torso_enc_num);
115 
116  while (!itf_torso_enc_->getEncoders(enc_torso.data()));
117 
118  std::swap(enc_torso(0), enc_torso(2));
119 
120  return enc_torso;
121 }
122 
123 
125 {
126  int arm_enc_num;
127  itf_arm_enc_->getAxes(&arm_enc_num);
128  Vector enc_arm(arm_enc_num);
129 
130  Vector root_ee_enc(10);
131 
132  root_ee_enc.setSubvector(0, readTorso());
133 
134  while (!itf_arm_enc_->getEncoders(enc_arm.data()));
135 
136  for (size_t i = 0; i < 7; ++i)
137  root_ee_enc(i + 3) = enc_arm(i);
138 
139  return root_ee_enc;
140 }
141 
142 
144 {
145  Vector ee_pose = icub_kin_arm_.EndEffPose(CTRL_DEG2RAD * readRootToEE());
146 
147  return toEigen(ee_pose);
148 }
iCubGatePose(std::unique_ptr< PFVisualCorrection > visual_correction, const double gate_x, const double gate_y, const double gate_z, const double gate_aperture, const double gate_rotation, const yarp::os::ConstString &robot, const yarp::os::ConstString &laterality, const yarp::os::ConstString &port_prefix)
iCub::iKin::iCubArm icub_kin_arm_
Definition: iCubGatePose.h:33
yarp::sig::Vector readTorso()
yarp::dev::IEncoders * itf_torso_enc_
Definition: iCubGatePose.h:31
~iCubGatePose() noexcept override
const yarp::os::ConstString ID_
Definition: iCubGatePose.h:42
yarp::os::ConstString robot_
Definition: iCubGatePose.h:45
yarp::os::ConstString laterality_
Definition: iCubGatePose.h:46
yarp::sig::Vector readRootToEE()
Eigen::VectorXd readPose() override
yarp::dev::IEncoders * itf_arm_enc_
Definition: iCubGatePose.h:29
const yarp::os::ConstString log_ID_
Definition: iCubGatePose.h:43
yarp::dev::PolyDriver drv_arm_enc_
Definition: iCubGatePose.h:28
yarp::dev::PolyDriver drv_torso_enc_
Definition: iCubGatePose.h:30