1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  *   tests the control law
33  *   eye-in-hand control
34  *   velocity computed in articular
35  *
36  * Authors:
37  * Fabien Spindler
38  *
39  *****************************************************************************/
40 
41 /*!
42   \file servoPtu46Point2DArtVelocity.cpp
43 
44   \brief Example of eye-in-hand control law. We control here a real robot, the
45   ptu-46 robot (pan-tilt head provided by Directed Perception). The velocity
46   is computed in articular. The visual feature is the center of gravity of a
47   point.
48 
49 */
50 
51 /*!
52   \example servoPtu46Point2DArtVelocity.cpp
53 
54   Example of eye-in-hand control law. We control here a real robot, the ptu-46
55   robot (pan-tilt head provided by Directed Perception). The velocity is
56   computed in articular. The visual feature is the center of gravity of a
57   point.
58 
59 */
60 #include <visp3/core/vpConfig.h>
61 #include <visp3/core/vpDebug.h> // Debug trace
62 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
63 #include <unistd.h>
64 #endif
65 #include <signal.h>
66 
67 #if (defined(VISP_HAVE_PTU46) & defined(VISP_HAVE_DC1394))
68 
69 #ifdef VISP_HAVE_PTHREAD
70 #include <pthread.h>
71 #endif
72 
73 #include <visp3/core/vpDisplay.h>
74 #include <visp3/core/vpImage.h>
75 #include <visp3/gui/vpDisplayX.h>
76 #include <visp3/sensor/vp1394TwoGrabber.h>
77 
78 #include <visp3/core/vpHomogeneousMatrix.h>
79 #include <visp3/core/vpMath.h>
80 #include <visp3/core/vpPoint.h>
81 #include <visp3/visual_features/vpFeatureBuilder.h>
82 #include <visp3/visual_features/vpFeaturePoint.h>
83 #include <visp3/vs/vpServo.h>
84 
85 #include <visp3/robot/vpRobotPtu46.h>
86 
87 // Exception
88 #include <visp3/core/vpException.h>
89 #include <visp3/vs/vpServoDisplay.h>
90 
91 #include <visp3/blob/vpDot2.h>
92 
93 #ifdef VISP_HAVE_PTHREAD
94 pthread_mutex_t mutexEndLoop = PTHREAD_MUTEX_INITIALIZER;
95 #endif
96 
signalCtrC(int signumber)97 void signalCtrC(int signumber)
98 {
99   (void)(signumber);
100 #ifdef VISP_HAVE_PTHREAD
101   pthread_mutex_unlock(&mutexEndLoop);
102 #endif
103   usleep(1000 * 10);
104   vpTRACE("Ctrl-C pressed...");
105 }
106 
main()107 int main()
108 {
109   std::cout << std::endl;
110   std::cout << "-------------------------------------------------------" << std::endl;
111   std::cout << " Test program for vpServo " << std::endl;
112   std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
113   std::cout << " Simulation " << std::endl;
114   std::cout << " task : servo a point " << std::endl;
115   std::cout << "-------------------------------------------------------" << std::endl;
116   std::cout << std::endl;
117 
118   try {
119 
120 #ifdef VISP_HAVE_PTHREAD
121     pthread_mutex_lock(&mutexEndLoop);
122 #endif
123     signal(SIGINT, &signalCtrC);
124 
125     vpRobotPtu46 robot;
126     {
127       vpColVector q(2);
128       q = 0;
129       robot.setRobotState(vpRobot::STATE_POSITION_CONTROL);
130       robot.setPosition(vpRobot::ARTICULAR_FRAME, q);
131     }
132 
133     vpImage<unsigned char> I;
134 
135     vp1394TwoGrabber g;
136 
137     g.open(I);
138 
139     try {
140       g.acquire(I);
141     } catch (...) {
142       vpERROR_TRACE(" Error caught");
143       return (-1);
144     }
145 
146     vpDisplayX display(I, 100, 100, "testDisplayX.cpp ");
147     vpTRACE(" ");
148 
149     try {
150       vpDisplay::display(I);
151       vpDisplay::flush(I);
152     } catch (...) {
153       vpERROR_TRACE(" Error caught");
154       return (-1);
155     }
156 
157     vpServo task;
158 
159     vpDot2 dot;
160 
161     try {
162       vpERROR_TRACE("start dot.initTracking(I) ");
163       vpImagePoint germ;
164       vpDisplay::getClick(I, germ);
165       dot.setCog(germ);
166       vpDEBUG_TRACE(25, "Click!");
167       // dot.initTracking(I) ;
168       dot.track(I);
169       vpERROR_TRACE("after dot.initTracking(I) ");
170     } catch (...) {
171       vpERROR_TRACE(" Error caught ");
172       return (-1);
173     }
174 
175     vpCameraParameters cam;
176 
177     vpTRACE("sets the current position of the visual feature ");
178     vpFeaturePoint p;
179     vpFeatureBuilder::create(p, cam, dot); // retrieve x,y and Z of the vpPoint structure
180 
181     p.set_Z(1);
182     vpTRACE("sets the desired position of the visual feature ");
183     vpFeaturePoint pd;
184     pd.buildFrom(0, 0, 1);
185 
186     vpTRACE("define the task");
187     vpTRACE("\t we want an eye-in-hand control law");
188     vpTRACE("\t articular velocity are computed");
189     task.setServo(vpServo::EYEINHAND_L_cVe_eJe);
190     task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
191 
192     vpTRACE("Set the position of the end-effector frame in the camera frame");
193     vpHomogeneousMatrix cMe;
194     //  robot.get_cMe(cMe) ;
195 
196     vpVelocityTwistMatrix cVe;
197     robot.get_cVe(cVe);
198     std::cout << cVe << std::endl;
199     task.set_cVe(cVe);
200 
201     vpDisplay::getClick(I);
202     vpTRACE("Set the Jacobian (expressed in the end-effector frame)");
203     vpMatrix eJe;
204     robot.get_eJe(eJe);
205     task.set_eJe(eJe);
206 
207     vpTRACE("\t we want to see a point on a point..");
208     std::cout << std::endl;
209     task.addFeature(p, pd);
210 
211     vpTRACE("\t set the gain");
212     task.setLambda(0.1);
213 
214     vpTRACE("Display task information ");
215     task.print();
216 
217     robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
218 
219     unsigned int iter = 0;
220     vpTRACE("\t loop");
221 #ifdef VISP_HAVE_PTHREAD
222     while (0 != pthread_mutex_trylock(&mutexEndLoop))
223 #else
224     for (;;)
225 #endif
226     {
227       std::cout << "---------------------------------------------" << iter << std::endl;
228 
229       g.acquire(I);
230       vpDisplay::display(I);
231 
232       dot.track(I);
233 
234       //    vpDisplay::displayCross(I,(int)dot.I(), (int)dot.J(),
235       //			   10,vpColor::green) ;
236 
237       vpFeatureBuilder::create(p, cam, dot);
238 
239       // get the jacobian
240       robot.get_eJe(eJe);
241       task.set_eJe(eJe);
242 
243       //  std::cout << (vpMatrix)cVe*eJe << std::endl ;
244 
245       vpColVector v;
246       v = task.computeControlLaw();
247 
248       vpServoDisplay::display(task, cam, I);
249       std::cout << v.t();
250       robot.setVelocity(vpRobot::ARTICULAR_FRAME, v);
251       vpDisplay::flush(I);
252 
253       vpTRACE("\t\t || s - s* || = %f ", (task.getError()).sumSquare());
254     }
255 
256     vpTRACE("Display task information ");
257     task.print();
258   }
259   catch (const vpException &e) {
260     std::cout << "Sorry PtU46 not available. Got exception: " << e << std::endl;
261     return EXIT_FAILURE
262   }
263   return EXIT_SUCCESS;
264 }
265 
266 #else
main()267 int main()
268 {
269   std::cout << "You do not have an PTU46 PT robot connected to your computer..." << std::endl;
270 }
271 #endif
272