visual-tracking-control
VisualServoingServer.h
Go to the documentation of this file.
1 #ifndef VISUALSERVOINGSERVER_H
2 #define VISUALSERVOINGSERVER_H
3 
4 #include "thrift/VisualServoingIDL.h"
5 
6 #include <array>
7 #include <cmath>
8 #include <mutex>
9 #include <thread>
10 #include <vector>
11 
12 #include <yarp/dev/CartesianControl.h>
13 #include <yarp/dev/GazeControl.h>
14 #include <yarp/dev/DeviceDriver.h>
15 #include <yarp/dev/IEncoders.h>
16 #include <yarp/dev/IVisualServoing.h>
17 #include <yarp/dev/PolyDriver.h>
18 #include <yarp/math/Math.h>
19 #include <yarp/os/Bottle.h>
20 #include <yarp/os/BufferedPort.h>
21 #include <yarp/os/ConstString.h>
22 #include <yarp/os/LogStream.h>
23 #include <yarp/os/Port.h>
24 #include <yarp/os/RpcClient.h>
25 #include <yarp/os/Thread.h>
26 #include <yarp/os/Searchable.h>
27 #include <yarp/sig/Image.h>
28 #include <yarp/sig/Matrix.h>
29 #include <yarp/sig/Vector.h>
30 
31 
32 class VisualServoingServer : public yarp::dev::DeviceDriver,
33  public yarp::dev::IVisualServoing,
34  protected yarp::os::Thread,
35  public VisualServoingIDL
36 {
37 public:
38  /* Ctors and Dtors */
40 
42 
43 
44  /* DeviceDriver overrides */
45  bool open(yarp::os::Searchable &config) override;
46 
47  bool close() override;
48 
49 
50  /* IVisualServoing overrides */
51  bool initFacilities(const bool use_direct_kin) override;
52 
53  bool resetFacilities() override;
54 
55  bool stopFacilities() override;
56 
57  bool goToGoal(const yarp::sig::Vector& vec_x, const yarp::sig::Vector& vec_o) override;
58 
59  bool goToGoal(const std::vector<yarp::sig::Vector>& vec_px_l, const std::vector<yarp::sig::Vector>& vec_px_r) override;
60 
61  bool setModality(const std::string& mode) override;
62 
63  bool setVisualServoControl(const std::string& control) override;
64 
65  bool setControlPoint(const yarp::os::ConstString& point) override;
66 
67  bool getVisualServoingInfo(yarp::os::Bottle& info) override;
68 
69  bool setGoToGoalTolerance(const double tol = 15.0) override;
70 
71  bool checkVisualServoingController() override;
72 
73  bool waitVisualServoingDone(const double period = 0.1, const double timeout = 0.0) override;
74 
75  bool stopController() override;
76 
77  bool setTranslationGain(const double K_x_1 = 1.0, const double K_x_2 = 0.25) override;
78 
79  bool setMaxTranslationVelocity(const double max_x_dot) override;
80 
81  bool setTranslationGainSwitchTolerance(const double K_x_tol = 30.0) override;
82 
83  bool setOrientationGain(const double K_x_1 = 1.5, const double K_x_2 = 0.375) override;
84 
85  bool setMaxOrientationVelocity(const double max_o_dot) override;
86 
87  bool setOrientationGainSwitchTolerance(const double K_o_tol = 30.0) override;
88 
89  std::vector<yarp::sig::Vector> get3DGoalPositionsFrom3DPose(const yarp::sig::Vector& x, const yarp::sig::Vector& o) override;
90 
91  std::vector<yarp::sig::Vector> getGoalPixelsFrom3DPose(const yarp::sig::Vector& x, const yarp::sig::Vector& o, const CamSel& cam) override;
92 
93 
94  /* TO BE DEPRECATED */
95  bool storedInit(const std::string& label) override;
96 
97  bool storedGoToGoal(const std::string& label) override;
98 
99  bool goToSFMGoal() override;
100 
101 protected:
102  /* Thread overrides */
103  void beforeStart() override;
104 
105  bool threadInit() override;
106 
107  void run() override;
108 
109  void afterStart(bool success) override;
110 
111  void onStop() override;
112 
113  void threadRelease() override;
114 
115 
116  /* VisualServoingIDL overrides */
117  bool init_facilities(const bool use_direct_kin) override;
118 
119  bool reset_facilities() override;
120 
121  bool stop_facilities() override;
122 
123  bool go_to_px_goal(const std::vector<std::vector<double>>& vec_px_l, const std::vector<std::vector<double>>& vec_px_r) override;
124 
125  bool go_to_pose_goal(const std::vector<double>& vec_x, const std::vector<double>& vec_o) override;
126 
127  bool set_modality(const std::string& mode) override;
128 
129  bool set_visual_servo_control(const std::string& control) override;
130 
131  bool set_control_point(const std::string& point) override;
132 
133  std::vector<std::string> get_visual_servoing_info() override;
134 
135  bool set_go_to_goal_tolerance(const double tol) override;
136 
137  bool check_visual_servoing_controller() override;
138 
139  bool wait_visual_servoing_done(const double period, const double timeout) override;
140 
141  bool stop_controller() override;
142 
143  bool set_translation_gain(const double K_x_1, const double K_x_2) override;
144 
145  bool set_max_translation_velocity(const double max_x_dot) override;
146 
147  bool set_translation_gain_switch_tolerance(const double K_x_tol) override;
148 
149  bool set_orientation_gain(const double K_o_1, const double K_o_2) override;
150 
151  bool set_max_orientation_velocity(const double max_o_dot) override;
152 
153  bool set_orientation_gain_switch_tolerance(const double K_o_tol) override;
154 
155  std::vector<std::vector<double>> get_3D_goal_positions_from_3D_pose(const std::vector<double>& x, const std::vector<double>& o) override;
156 
157  std::vector<std::vector<double>> get_goal_pixels_from_3D_pose(const std::vector<double> & x, const std::vector<double> & o, const std::string& cam) override;
158 
159  bool quit() override;
160 
161  /* TO BE DEPRECATED */
162  bool stored_init(const std::string& label) override;
163 
164  bool stored_go_to_goal(const std::string& label) override;
165 
166  bool get_goal_from_sfm() override;
167 
168 
169  /* Enum helpers */
171 
172  enum class PixelControlMode { all, x, o };
173 
174  enum class OperatingMode { position, orientation, pose };
175 
176 private:
177  bool verbosity_ = false;
178  bool sim_ = false;
179  yarp::os::ConstString robot_name_ = "icub";
180 
183 
184  yarp::dev::PolyDriver rightarm_cartesian_driver_;
185  yarp::dev::ICartesianControl* itf_rightarm_cart_;
186 
187  yarp::dev::PolyDriver gaze_driver_;
188  yarp::dev::IGazeControl* itf_gaze_;
189 
190  bool vs_control_running_ = false;
191  bool vs_goal_reached_ = false;
192  const double Ts_ = 0.1; /* [s] */
193  std::array<double, 2> K_x_ = {{0.5, 0.25}};
194  std::array<double, 2> K_o_ = {{3.5, 0.5}};
195  double max_x_dot_ = 0.035; /* [m/s] */
196  double max_o_dot_ = 10.0 * M_PI / 180.0; /* [rad/s] */
197  double K_x_tol_ = 10.0; /* [pixel] */
198  double K_o_tol_ = 10.0; /* [pixel] */
199  double K_position_tol_ = 0.03; /* [m] */
200  double K_orientation_tol_ = 9.0 * M_PI / 180.0; /* [rad] */
201  double K_angle_tol_ = 3.0 * M_PI / 180.0; /* [rad] */
202  bool K_x_hysteresis_ = false;
203  bool K_o_hysteresis_ = false;
204  bool K_pose_hysteresis_ = false;
205  double tol_px_ = 1.0; /* [pixel] */
206  double tol_position_ = 0.01; /* [m] */
207  double tol_orientation_ = 3.0 * M_PI / 180.0; /* [rad] */
208  double tol_angle_ = 1.0 * M_PI / 180.0; /* [rad] */
209  double traj_time_ = 1.0; /* [s] */
210 
211  yarp::sig::Matrix l_proj_;
212  yarp::sig::Matrix r_proj_;
213 
214  yarp::sig::Vector goal_pose_ = yarp::math::zeros(7);
215  yarp::sig::Vector px_des_ = yarp::math::zeros(16);
216  yarp::sig::Matrix l_H_eye_to_r_ = yarp::math::zeros(4, 4);
217  yarp::sig::Matrix r_H_eye_to_r_ = yarp::math::zeros(4, 4);
218  yarp::sig::Matrix l_H_r_to_cam_ = yarp::math::zeros(4, 4);
219  yarp::sig::Matrix r_H_r_to_cam_ = yarp::math::zeros(4, 4);
220 
221  std::mutex mtx_px_des_;
222  std::mutex mtx_H_eye_cam_;
223 
225 
226  std::vector<yarp::sig::Vector> l_px_goal_ = std::vector<yarp::sig::Vector>(4);
227  std::vector<yarp::sig::Vector> r_px_goal_ = std::vector<yarp::sig::Vector>(4);
228 
231 
232  yarp::os::BufferedPort<yarp::sig::Vector> port_pose_left_in_;
233  yarp::os::BufferedPort<yarp::sig::Vector> port_pose_right_in_;
234 
235  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb>> port_image_left_in_;
236  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb>> port_image_left_out_;
237  yarp::os::BufferedPort<yarp::os::Bottle> port_click_left_;
238 
239  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb>> port_image_right_in_;
240  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb>> port_image_right_out_;
241  yarp::os::BufferedPort<yarp::os::Bottle> port_click_right_;
242 
243  yarp::os::Port port_rpc_command_;
244  yarp::os::RpcClient port_rpc_tracker_left_;
245  yarp::os::RpcClient port_rpc_tracker_right_;
246 
247  /* Private class methods */
249 
251 
253 
255 
256  bool setGazeController();
257 
258  bool setCommandPort();
259 
260  bool setTorsoDOF();
261 
262  bool unsetTorsoDOF();
263 
264  std::vector<yarp::sig::Vector> getPixelsFromPose(const yarp::sig::Vector& pose, const CamSel& cam);
265 
266  std::vector<yarp::sig::Vector> getControlPixelsFromPose(const yarp::sig::Vector& pose, const CamSel& cam, const PixelControlMode& mode);
267 
268  std::vector<yarp::sig::Vector> getControlPointsFromPose(const yarp::sig::Vector& pose);
269 
270  yarp::sig::Vector getPixelFromPoint(const CamSel& cam, const yarp::sig::Vector& p) const;
271 
272  yarp::sig::Vector getControlPixelFromPoint(const CamSel& cam, const yarp::sig::Vector& p) const;
273 
274  void getCurrentStereoFeaturesAndJacobian(const std::vector<yarp::sig::Vector>& left_px, const std::vector<yarp::sig::Vector>& right_px,
275  yarp::sig::Vector& features, yarp::sig::Matrix& jacobian);
276 
277  yarp::sig::Vector getJacobianU(const CamSel& cam, const yarp::sig::Vector& px);
278 
279  yarp::sig::Vector getJacobianV(const CamSel& cam, const yarp::sig::Vector& px);
280 
282 
283  bool setPoseGoal(const yarp::sig::Vector& goal_x, const yarp::sig::Vector& goal_o);
284 
285  bool setPixelGoal(const std::vector<yarp::sig::Vector>& l_px_goal, const std::vector<yarp::sig::Vector>& r_px_goal);
286 
289 
290  yarp::sig::Vector averagePose(const yarp::sig::Vector& l_pose, const yarp::sig::Vector& r_pose) const;
291 
292  bool checkVisualServoingStatus(const yarp::sig::Vector& px_cur, const double tol);
293 
294  bool checkVisualServoingStatus(const yarp::sig::Vector& pose_cur, const double tol_position, const double tol_orientation, const double tol_angle);
295 
296 
297  /* LOG */
298  void yInfoVerbose (const yarp::os::ConstString& str) const { if(verbosity_) yInfo() << str; };
299  void yWarningVerbose(const yarp::os::ConstString& str) const { if(verbosity_) yWarning() << str; };
300  void yErrorVerbose (const yarp::os::ConstString& str) const { if(verbosity_) yError() << str; };
301 
302  yarp::sig::Vector stdVectorOfVectorsToVector(const std::vector<yarp::sig::Vector>& vectors);
303 
304  yarp::os::BufferedPort<yarp::sig::Vector> port_pose_px_l_;
305  yarp::os::BufferedPort<yarp::sig::Vector> port_pose_px_r_;
306 
307  yarp::os::BufferedPort<yarp::sig::Vector> port_kin_px_l_;
308  yarp::os::BufferedPort<yarp::sig::Vector> port_kin_px_r_;
309 
310  yarp::os::BufferedPort<yarp::sig::Vector> port_goal_px_l_;
311  yarp::os::BufferedPort<yarp::sig::Vector> port_goal_px_r_;
312 
313  yarp::os::BufferedPort<yarp::sig::Vector> port_pose_avg_;
314  yarp::os::BufferedPort<yarp::sig::Vector> port_pose_kin_;
315  yarp::os::BufferedPort<yarp::sig::Vector> port_pose_goal_;
316 };
317 
318 #endif /* VISUALSERVOINGSERVER_H */
bool checkVisualServoingStatus(const yarp::sig::Vector &px_cur, const double tol)
bool set_translation_gain(const double K_x_1, const double K_x_2) override
Set the translation gains of the visual servoing control algorithm.
bool set_max_translation_velocity(const double max_x_dot) override
Set the maximum translation velocity of the visual servoing control algorithm (same for each axis)...
bool initFacilities(const bool use_direct_kin) override
yarp::os::BufferedPort< yarp::sig::Vector > port_kin_px_l_
yarp::os::RpcClient port_rpc_tracker_left_
bool check_visual_servoing_controller() override
Check once whether the visual servoing controller is running or not.
yarp::os::BufferedPort< yarp::sig::ImageOf< yarp::sig::PixelRgb > > port_image_right_out_
bool setOrientationGain(const double K_x_1=1.5, const double K_x_2=0.375) override
bool stop_facilities() override
Stop and disconnect support modules and connections used for visual servoing.
std::vector< yarp::sig::Vector > get3DGoalPositionsFrom3DPose(const yarp::sig::Vector &x, const yarp::sig::Vector &o) override
void getCurrentStereoFeaturesAndJacobian(const std::vector< yarp::sig::Vector > &left_px, const std::vector< yarp::sig::Vector > &right_px, yarp::sig::Vector &features, yarp::sig::Matrix &jacobian)
yarp::os::BufferedPort< yarp::sig::Vector > port_pose_px_l_
bool getVisualServoingInfo(yarp::os::Bottle &info) override
bool set_orientation_gain(const double K_o_1, const double K_o_2) override
Set the orientation gains of the visual servoing control algorithm.
yarp::sig::Vector getJacobianU(const CamSel &cam, const yarp::sig::Vector &px)
bool setVisualServoControl(const std::string &control) override
bool storedInit(const std::string &label) override
bool reset_facilities() override
Reset support modules and connections to perform the current initialized visual servoing task...
bool set_modality(const std::string &mode) override
Set visual servoing operating mode between:
yarp::sig::Vector stdVectorOfVectorsToVector(const std::vector< yarp::sig::Vector > &vectors)
yarp::os::BufferedPort< yarp::sig::Vector > port_pose_avg_
yarp::os::BufferedPort< yarp::sig::ImageOf< yarp::sig::PixelRgb > > port_image_right_in_
void afterStart(bool success) override
yarp::os::BufferedPort< yarp::sig::Vector > port_goal_px_r_
bool setOrientationGainSwitchTolerance(const double K_o_tol=30.0) override
bool waitVisualServoingDone(const double period=0.1, const double timeout=0.0) override
yarp::sig::Matrix l_proj_
yarp::os::BufferedPort< yarp::sig::Vector > port_pose_goal_
bool stored_init(const std::string &label) override
Initialize the robot to an initial position.
bool set_go_to_goal_tolerance(const double tol) override
Set visual servoing goal tolerance.
std::vector< std::string > get_visual_servoing_info() override
Return useful information for visual servoing.
bool stored_go_to_goal(const std::string &label) override
Set the robot visual servoing goal.
bool checkVisualServoingController() override
yarp::sig::Vector px_des_
yarp::sig::Matrix l_H_r_to_cam_
yarp::os::BufferedPort< yarp::sig::Vector > port_pose_right_in_
bool setPoseGoal(const yarp::sig::Vector &goal_x, const yarp::sig::Vector &goal_o)
yarp::os::BufferedPort< yarp::sig::ImageOf< yarp::sig::PixelRgb > > port_image_left_in_
bool set_translation_gain_switch_tolerance(const double K_x_tol) override
Set the tolerance, in pixels, at which the translation control law swithces its gain value...
yarp::os::BufferedPort< yarp::sig::Vector > port_kin_px_r_
bool set_orientation_gain_switch_tolerance(const double K_o_tol) override
Set the tolerance, in pixels, at which the orientation control law swithces its gain value...
std::vector< yarp::sig::Vector > getGoalPixelsFrom3DPose(const yarp::sig::Vector &x, const yarp::sig::Vector &o, const CamSel &cam) override
bool setTranslationGainSwitchTolerance(const double K_x_tol=30.0) override
void yWarningVerbose(const yarp::os::ConstString &str) const
yarp::sig::Vector getJacobianV(const CamSel &cam, const yarp::sig::Vector &px)
yarp::sig::Matrix r_H_r_to_cam_
bool stop_controller() override
Ask for an immediate stop of the visual servoing controller.
std::vector< std::vector< double > > get_3D_goal_positions_from_3D_pose(const std::vector< double > &x, const std::vector< double > &o) override
Helper function: extract four Cartesian points lying on the plane defined by the frame o in the posit...
void yInfoVerbose(const yarp::os::ConstString &str) const
yarp::dev::IGazeControl * itf_gaze_
yarp::os::ConstString robot_name_
std::thread * thr_background_update_params_
yarp::sig::Vector getControlPixelFromPoint(const CamSel &cam, const yarp::sig::Vector &p) const
std::vector< yarp::sig::Vector > getPixelsFromPose(const yarp::sig::Vector &pose, const CamSel &cam)
VisualServoControl vs_control_
yarp::os::BufferedPort< yarp::sig::ImageOf< yarp::sig::PixelRgb > > port_image_left_out_
yarp::os::RpcClient port_rpc_tracker_right_
yarp::os::BufferedPort< yarp::sig::Vector > port_pose_px_r_
std::vector< std::vector< double > > get_goal_pixels_from_3D_pose(const std::vector< double > &x, const std::vector< double > &o, const std::string &cam) override
Helper function: extract four 2D pixel points lying on the plane defined by the frame o in the positi...
bool setGoToGoalTolerance(const double tol=15.0) override
bool setTranslationGain(const double K_x_1=1.0, const double K_x_2=0.25) override
bool setPixelGoal(const std::vector< yarp::sig::Vector > &l_px_goal, const std::vector< yarp::sig::Vector > &r_px_goal)
yarp::sig::Matrix l_H_eye_to_r_
yarp::sig::Vector goal_pose_
std::vector< yarp::sig::Vector > l_px_goal_
bool set_visual_servo_control(const std::string &control) override
Set visual servo control law between:
yarp::os::BufferedPort< yarp::sig::Vector > port_pose_kin_
yarp::os::Port port_rpc_command_
VisualServoingIDL IDL Interface to ServerVisualServoing functionalities.
yarp::dev::PolyDriver rightarm_cartesian_driver_
yarp::dev::PolyDriver gaze_driver_
std::vector< yarp::sig::Vector > getControlPixelsFromPose(const yarp::sig::Vector &pose, const CamSel &cam, const PixelControlMode &mode)
bool go_to_pose_goal(const std::vector< double > &vec_x, const std::vector< double > &vec_o) override
Set the goal point (3D for the position + 4D axis-angle for the orientation) and start visual servoin...
yarp::os::BufferedPort< yarp::os::Bottle > port_click_left_
std::vector< yarp::sig::Vector > r_px_goal_
yarp::os::BufferedPort< yarp::sig::Vector > port_goal_px_l_
yarp::sig::Matrix r_proj_
bool setModality(const std::string &mode) override
bool init_facilities(const bool use_direct_kin) override
Initialize support modules and connections to perform a visual servoing task.
bool goToGoal(const yarp::sig::Vector &vec_x, const yarp::sig::Vector &vec_o) override
bool go_to_px_goal(const std::vector< std::vector< double >> &vec_px_l, const std::vector< std::vector< double >> &vec_px_r) override
yarp::sig::Vector getPixelFromPoint(const CamSel &cam, const yarp::sig::Vector &p) const
yarp::dev::ICartesianControl * itf_rightarm_cart_
bool setControlPoint(const yarp::os::ConstString &point) override
bool quit() override
Gently close the visual servoing device, deallocating resources.
yarp::sig::Matrix r_H_eye_to_r_
bool wait_visual_servoing_done(const double period, const double timeout) override
Wait until visual servoing reaches the goal.
yarp::sig::Vector averagePose(const yarp::sig::Vector &l_pose, const yarp::sig::Vector &r_pose) const
bool get_goal_from_sfm() override
Get goal point from SFM module.
bool storedGoToGoal(const std::string &label) override
yarp::os::BufferedPort< yarp::os::Bottle > port_click_right_
bool setMaxOrientationVelocity(const double max_o_dot) override
bool set_control_point(const std::string &point) override
Set the point controlled during visual servoing.
bool set_max_orientation_velocity(const double max_o_dot) override
Set the maximum angular velocity of the axis-angle velocity vector of the visual servoing control alg...
bool open(yarp::os::Searchable &config) override
yarp::os::BufferedPort< yarp::sig::Vector > port_pose_left_in_
std::vector< yarp::sig::Vector > getControlPointsFromPose(const yarp::sig::Vector &pose)
bool setMaxTranslationVelocity(const double max_x_dot) override
void yErrorVerbose(const yarp::os::ConstString &str) const
std::array< double, 2 > K_x_
std::array< double, 2 > K_o_