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