Visual Servoing Platform  version 3.6.1 under development (2024-12-04)
servoPtu46Point2DArtVelocity.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * tests the control law
32  * eye-in-hand control
33  * velocity computed in articular
34  */
35 
55 #include <visp3/core/vpConfig.h>
56 #include <visp3/core/vpDebug.h> // Debug trace
57 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
58 #include <unistd.h>
59 #endif
60 #include <signal.h>
61 
62 #if defined(VISP_HAVE_PTU46) && defined(VISP_HAVE_DC1394) && defined(VISP_HAVE_THREADS)
63 
64 #include <mutex>
65 
66 #include <visp3/core/vpDisplay.h>
67 #include <visp3/core/vpImage.h>
68 #include <visp3/gui/vpDisplayX.h>
69 #include <visp3/sensor/vp1394TwoGrabber.h>
70 
71 #include <visp3/core/vpHomogeneousMatrix.h>
72 #include <visp3/core/vpMath.h>
73 #include <visp3/core/vpPoint.h>
74 #include <visp3/visual_features/vpFeatureBuilder.h>
75 #include <visp3/visual_features/vpFeaturePoint.h>
76 #include <visp3/vs/vpServo.h>
77 
78 #include <visp3/robot/vpRobotPtu46.h>
79 
80 // Exception
81 #include <visp3/core/vpException.h>
82 #include <visp3/vs/vpServoDisplay.h>
83 
84 #include <visp3/blob/vpDot2.h>
85 
86 std::mutex mutexEndLoop;
87 
88 void signalCtrC(int signumber)
89 {
90  (void)(signumber);
91  mutexEndLoop.unlock();
92  usleep(1000 * 10);
93  vpTRACE("Ctrl-C pressed...");
94 }
95 
96 int main()
97 {
98 #ifdef ENABLE_VISP_NAMESPACE
99  using namespace VISP_NAMESPACE_NAME;
100 #endif
101 
102  std::cout << std::endl;
103  std::cout << "-------------------------------------------------------" << std::endl;
104  std::cout << " Test program for vpServo " << std::endl;
105  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
106  std::cout << " Simulation " << std::endl;
107  std::cout << " task : servo a point " << std::endl;
108  std::cout << "-------------------------------------------------------" << std::endl;
109  std::cout << std::endl;
110 
111  try {
112  mutexEndLoop.lock();
113  signal(SIGINT, &signalCtrC);
114 
115  vpRobotPtu46 robot;
116  {
117  vpColVector q(2);
118  q = 0;
120  robot.setPosition(vpRobot::ARTICULAR_FRAME, q);
121  }
122 
124 
126 
127  g.open(I);
128 
129  try {
130  g.acquire(I);
131  }
132  catch (...) {
133  vpERROR_TRACE(" Error caught");
134  return EXIT_FAILURE;
135  }
136 
137  vpDisplayX display(I, 100, 100, "testDisplayX.cpp ");
138  vpTRACE(" ");
139 
140  try {
142  vpDisplay::flush(I);
143  }
144  catch (...) {
145  vpERROR_TRACE(" Error caught");
146  return EXIT_FAILURE;
147  }
148 
149  vpServo task;
150 
151  vpDot2 dot;
152 
153  try {
154  vpERROR_TRACE("start dot.initTracking(I) ");
155  vpImagePoint germ;
156  vpDisplay::getClick(I, germ);
157  dot.setCog(germ);
158  vpDEBUG_TRACE(25, "Click!");
159  // dot.initTracking(I) ;
160  dot.track(I);
161  vpERROR_TRACE("after dot.initTracking(I) ");
162  }
163  catch (...) {
164  vpERROR_TRACE(" Error caught ");
165  return EXIT_FAILURE;
166  }
167 
168  vpCameraParameters cam;
169 
170  vpTRACE("sets the current position of the visual feature ");
171  vpFeaturePoint p;
172  vpFeatureBuilder::create(p, cam, dot); // retrieve x,y and Z of the vpPoint structure
173 
174  p.set_Z(1);
175  vpTRACE("sets the desired position of the visual feature ");
176  vpFeaturePoint pd;
177  pd.buildFrom(0, 0, 1);
178 
179  vpTRACE("define the task");
180  vpTRACE("\t we want an eye-in-hand control law");
181  vpTRACE("\t articular velocity are computed");
184 
185  vpTRACE("Set the position of the end-effector frame in the camera frame");
187  // robot.get_cMe(cMe) ;
188 
190  robot.get_cVe(cVe);
191  std::cout << cVe << std::endl;
192  task.set_cVe(cVe);
193 
195  vpTRACE("Set the Jacobian (expressed in the end-effector frame)");
196  vpMatrix eJe;
197  robot.get_eJe(eJe);
198  task.set_eJe(eJe);
199 
200  vpTRACE("\t we want to see a point on a point..");
201  std::cout << std::endl;
202  task.addFeature(p, pd);
203 
204  vpTRACE("\t set the gain");
205  task.setLambda(0.1);
206 
207  vpTRACE("Display task information ");
208  task.print();
209 
211 
212  unsigned int iter = 0;
213  vpTRACE("\t loop");
214  while (0 != mutexEndLoop.trylock()) {
215  std::cout << "---------------------------------------------" << iter << std::endl;
216 
217  g.acquire(I);
219 
220  dot.track(I);
221 
222  vpFeatureBuilder::create(p, cam, dot);
223 
224  // get the jacobian
225  robot.get_eJe(eJe);
226  task.set_eJe(eJe);
227 
228  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
229 
230  vpColVector v;
231  v = task.computeControlLaw();
232 
233  vpServoDisplay::display(task, cam, I);
234  std::cout << v.t();
236  vpDisplay::flush(I);
237 
238  vpTRACE("\t\t || s - s* || = %f ", (task.getError()).sumSquare());
239  }
240 
241  vpTRACE("Display task information ");
242  task.print();
243  }
244  catch (const vpException &e) {
245  std::cout << "Sorry PtU46 not available. Got exception: " << e << std::endl;
246  return EXIT_FAILURE
247  }
248  return EXIT_SUCCESS;
249 }
250 
251 #else
252 int main() { std::cout << "You do not have an PTU46 PT robot connected to your computer..." << std::endl; }
253 #endif
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void acquire(vpImage< unsigned char > &I)
void open(vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpRowVector t() const
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:125
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:452
void setCog(const vpImagePoint &ip)
Definition: vpDot2.h:261
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
void set_Z(double Z)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
void get_eJe(vpMatrix &eJe) VP_OVERRIDE
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
Interface for the Directed Perception ptu-46 pan, tilt head .
Definition: vpRobotPtu46.h:78
@ ARTICULAR_FRAME
Definition: vpRobot.h:80
@ STATE_POSITION_CONTROL
Initialize the position controller.
Definition: vpRobot.h:68
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition: vpRobot.h:67
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:202
static void display(const vpServo &s, const vpCameraParameters &cam, const vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
@ EYEINHAND_L_cVe_eJe
Definition: vpServo.h:168
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:1038
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:171
void setLambda(double c)
Definition: vpServo.h:986
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:1101
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:510
@ PSEUDO_INVERSE
Definition: vpServo.h:235
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ DESIRED
Definition: vpServo.h:208
vpVelocityTwistMatrix get_cVe() const
Definition: vpUnicycle.h:72