visual-tracking-control
VisualServoingClient.cpp
Go to the documentation of this file.
1 #include "VisualServoingClient.h"
2 
3 #include <iCub/ctrl/minJerkCtrl.h>
4 #include <opencv2/core/core.hpp>
5 #include <opencv2/imgproc/imgproc.hpp>
6 #include <yarp/math/Math.h>
7 #include <yarp/math/SVD.h>
8 #include <yarp/os/Network.h>
9 #include <yarp/os/Property.h>
10 #include <yarp/os/RpcClient.h>
11 #include <yarp/os/Time.h>
12 
13 using namespace yarp::dev;
14 using namespace yarp::math;
15 using namespace yarp::os;
16 using namespace yarp::sig;
17 using namespace iCub::ctrl;
18 
19 
20 /* Ctors and Dtors */
22 {
23  yInfo() << "*** Invoked VisualServoingClient ctor ***";
24  yInfo() << "*** VisualServoingClient constructed ***";
25 }
26 
27 
29 {
30  yInfo() << "*** Invoked VisualServoingClient dtor ***";
31  yInfo() << "*** VisualServoingClient destructed ***";
32 }
33 
34 
35 /* DeviceDriver overrides */
36 bool VisualServoingClient::open(Searchable &config)
37 {
38  verbosity_ = config.check("verbosity", Value(false)).asBool();
39  yInfo() << "|> Verbosity: " + ConstString(verbosity_? "ON" : "OFF");
40 
41 
42  yInfoVerbose("*** Configuring VisualServoingClient ***");
43 
44  local_ = config.check("local", Value("")).asString();
45  yInfoVerbose("|> Local port prefix: " + local_);
46  if (local_ == "")
47  {
48  yErrorVerbose("Invalid local port prefix name.");
49  return false;
50  }
51  local_ += "/cmd:o";
52  yInfoVerbose("|> Local port command name: " + local_);
53 
54  remote_ = config.check("remote", Value("")).asString();
55  yInfoVerbose("|> Remote port name: " + remote_);
56  if (remote_ == "")
57  {
58  yErrorVerbose("Invalid remote port prefix name.");
59  return false;
60  }
61  remote_ += "/cmd:i";
62  yInfoVerbose("|> Remote port command name: " + remote_);
63 
64  if (!port_rpc_command_.open(local_))
65  {
66  yErrorVerbose("Could not open " + local_ + " port.");
67  return false;
68  }
69 
70  if (!Network::connect(local_, remote_, "tcp", verbosity_))
71  {
72  yErrorVerbose("Could not connect to " + local_ + " remote port.");
73  return false;
74  }
75 
76  if (!visualservoing_control.yarp().attachAsClient(port_rpc_command_))
77  {
78  yErrorVerbose("Cannot attach the RPC client command port.");
79  return false;
80  }
81 
82  yInfoVerbose("*** VisualServoingClient configured! ***");
83 
84  return true;
85 }
86 
87 
89 {
90  yInfoVerbose("*** Interrupting VisualServoingClient ***");
91 
92  yInfoVerbose("Interrupting ports.");
93  port_rpc_command_.interrupt();
94 
95  yInfoVerbose("*** Interrupting VisualServoingClient done! ***");
96 
97 
98  yInfoVerbose("*** Closing VisualServoingClient ***");
99 
100  yInfoVerbose("Closing ports.");
101  port_rpc_command_.close();
102 
103  yInfoVerbose("*** Closing VisualServoingClient done! ***");
104 
105 
106  return true;
107 }
108 
109 
110 /* IVisualServoing overrides */
111 bool VisualServoingClient::initFacilities(const bool use_direct_kin)
112 {
113  return visualservoing_control.init_facilities(use_direct_kin);
114 }
115 
116 
118 {
119  return visualservoing_control.reset_facilities();
120 }
121 
122 
124 {
125  return visualservoing_control.stop_facilities();
126 }
127 
128 
129 bool VisualServoingClient::goToGoal(const Vector& vec_x, const Vector& vec_o)
130 {
131  if (vec_x.size() != 3 || vec_o.size() != 4)
132  return false;
133 
134  std::vector<double> std_vec_x(vec_x.data(), vec_x.data() + vec_x.size());
135  std::vector<double> std_vec_o(vec_o.data(), vec_o.data() + vec_o.size());
136 
137  return visualservoing_control.go_to_pose_goal(std_vec_x, std_vec_o);
138 }
139 
140 
141 bool VisualServoingClient::goToGoal(const std::vector<Vector>& vec_px_l, const std::vector<Vector>& vec_px_r)
142 {
143  if (vec_px_l.size() != vec_px_r.size())
144  return false;
145 
146  const size_t num_points = vec_px_l.size();
147 
148  std::vector<std::vector<double>> std_vec_px_l(num_points);
149  std::vector<std::vector<double>> std_vec_px_r(num_points);
150 
151  for (size_t i = 0; i < num_points; ++i)
152  {
153  const Vector& yv_l = vec_px_l[i];
154  std::vector<double>& v_l = std_vec_px_l[i];
155 
156  const Vector& yv_r = vec_px_r[i];
157  std::vector<double>& v_r = std_vec_px_r[i];
158 
159  v_l = std::vector<double>(yv_l.data(), yv_l.data() + yv_l.size());
160  v_r = std::vector<double>(yv_r.data(), yv_r.data() + yv_r.size());
161  }
162 
163  return visualservoing_control.go_to_px_goal(std_vec_px_l, std_vec_px_r);
164 }
165 
166 
167 bool VisualServoingClient::setModality(const std::string& mode)
168 {
169  return visualservoing_control.set_modality(mode);
170 }
171 
172 
173 bool VisualServoingClient::setVisualServoControl(const std::string& control)
174 {
175  return visualservoing_control.set_visual_servo_control(control);
176 }
177 
178 
179 bool VisualServoingClient::setControlPoint(const yarp::os::ConstString& point)
180 {
181  yWarningVerbose("*** Service setControlPoint is unimplemented. ***");
182 
183  return false;
184 }
185 
186 
188 {
189  yWarningVerbose("*** Service getVisualServoingInfo is unimplemented. ***");
190 
191  return false;
192 }
193 
194 
196 {
197  return visualservoing_control.set_go_to_goal_tolerance(tol);
198 }
199 
200 
202 {
203  return visualservoing_control.check_visual_servoing_controller();
204 }
205 
206 
207 bool VisualServoingClient::waitVisualServoingDone(const double period, const double timeout)
208 {
209  return visualservoing_control.wait_visual_servoing_done(period, timeout);
210 }
211 
212 
214 {
215  return visualservoing_control.stop_controller();
216 }
217 
218 
219 bool VisualServoingClient::setTranslationGain(const double K_x_1, const double K_x_2)
220 {
221  return visualservoing_control.set_translation_gain(K_x_1, K_x_2);
222 }
223 
224 
226 {
227  return visualservoing_control.set_max_translation_velocity(max_x_dot);
228 }
229 
230 
232 {
233  return visualservoing_control.set_translation_gain_switch_tolerance(K_x_tol);
234 }
235 
236 
237 bool VisualServoingClient::setOrientationGain(const double K_o_1, const double K_o_2)
238 {
239  return visualservoing_control.set_orientation_gain(K_o_1, K_o_2);
240 }
241 
242 
244 {
245  return visualservoing_control.set_max_orientation_velocity(max_o_dot);
246 }
247 
248 
250 {
251  return visualservoing_control.set_orientation_gain_switch_tolerance(K_o_tol);
252 }
253 
254 
255 std::vector<Vector> VisualServoingClient::get3DGoalPositionsFrom3DPose(const Vector& x, const Vector& o)
256 {
257  std::vector<double> std_x(x.data(), x.data() + x.size());
258  std::vector<double> std_o(o.data(), o.data() + o.size());
259 
260  std::vector<std::vector<double>> std_vec_goal_points = visualservoing_control.get_3D_goal_positions_from_3D_pose(std_x, std_o);
261 
262  size_t num_points = std_vec_goal_points.size();
263  std::vector<Vector> vec_goal_points(num_points);
264  for (size_t i = 0; i < num_points; ++i)
265  vec_goal_points[i] = Vector(std_vec_goal_points[i].size(), std_vec_goal_points[i].data());
266 
267  return vec_goal_points;
268 }
269 
270 
271 std::vector<Vector> VisualServoingClient::getGoalPixelsFrom3DPose(const Vector& x, const Vector& o, const CamSel& cam)
272 {
273  std::vector<double> std_x(x.data(), x.data() + x.size());
274  std::vector<double> std_o(o.data(), o.data() + o.size());
275 
276  std::vector<std::vector<double>> std_vec_goal_points = visualservoing_control.get_goal_pixels_from_3D_pose(std_x, std_o, (cam == CamSel::left ? "left" : "right"));
277 
278  size_t num_points = std_vec_goal_points.size();
279  std::vector<Vector> vec_goal_points(num_points);
280  for (size_t i = 0; i < num_points; ++i)
281  vec_goal_points[i] = Vector(std_vec_goal_points[i].size(), std_vec_goal_points[i].data());
282 
283  return vec_goal_points;
284 }
285 
286 
287 bool VisualServoingClient::storedInit(const std::string& label)
288 {
289  return visualservoing_control.stored_init(label);
290 }
291 
292 
293 bool VisualServoingClient::storedGoToGoal(const std::string& label)
294 {
295  return visualservoing_control.stored_go_to_goal(label);
296 }
297 
298 
300 {
301  return visualservoing_control.get_goal_from_sfm();
302 }
bool setTranslationGainSwitchTolerance(const double K_x_tol=30.0) override
bool initFacilities(const bool use_direct_kin) override
bool goToGoal(const yarp::sig::Vector &vec_x, const yarp::sig::Vector &vec_o) override
bool setTranslationGain(const double K_x_1=1.0, const double K_x_2=0.25) override
bool setVisualServoControl(const std::string &control) override
bool checkVisualServoingController() override
bool open(yarp::os::Searchable &config) override
bool setControlPoint(const yarp::os::ConstString &point) override
bool setModality(const std::string &mode) override
bool setMaxTranslationVelocity(const double max_x_dot) override
bool storedGoToGoal(const std::string &label) override
bool setOrientationGainSwitchTolerance(const double K_o_tol=30.0) override
bool setMaxOrientationVelocity(const double max_o_dot) override
std::vector< yarp::sig::Vector > getGoalPixelsFrom3DPose(const yarp::sig::Vector &x, const yarp::sig::Vector &o, const CamSel &cam) override
std::vector< yarp::sig::Vector > get3DGoalPositionsFrom3DPose(const yarp::sig::Vector &x, const yarp::sig::Vector &o) override
bool setOrientationGain(const double K_o_1=1.5, const double K_o_2=0.375) override
bool waitVisualServoingDone(const double period=0.1, const double timeout=0.0) override
bool storedInit(const std::string &label) override
bool getVisualServoingInfo(yarp::os::Bottle &info) override
bool setGoToGoalTolerance(const double tol=15.0) override