Visual Servoing Platform  version 3.0.1
testFeatureSegment.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Visual feature manipulation (segment).
32  *
33  * Author:
34  * Filip Novotny
35  *
36  *****************************************************************************/
37 
38 #include <fstream>
39 #include <iostream>
40 #include <vector>
41 #include <numeric>
42 
43 #include <visp3/core/vpConfig.h>
44 
45 #ifdef VISP_HAVE_MODULE_ROBOT
46 
47 #include <visp3/core/vpCameraParameters.h>
48 #include <visp3/core/vpDisplay.h>
49 #include <visp3/gui/vpDisplayGDI.h>
50 #include <visp3/gui/vpDisplayX.h>
51 #include <visp3/visual_features/vpFeatureBuilder.h>
52 #include <visp3/visual_features/vpFeatureSegment.h>
53 #include <visp3/core/vpHomogeneousMatrix.h>
54 #include <visp3/core/vpImage.h>
55 #include <visp3/core/vpMath.h>
56 #include <visp3/io/vpParseArgv.h>
57 #include <visp3/gui/vpPlot.h>
58 #include <visp3/core/vpPoint.h>
59 #include <visp3/robot/vpSimulatorCamera.h>
60 #include <visp3/vs/vpServo.h> //visual servoing task
61 
69 int main(int argc, const char **argv)
70 {
71  try {
72 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
73  int opt_no_display = 0;
74  int opt_curves = 1;
75 #endif
76  int opt_normalized = 1;
77 
78  // Parse the command line to set the variables
79  vpParseArgv::vpArgvInfo argTable[] =
80  {
81  #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
82  {"-d", vpParseArgv::ARGV_CONSTANT, 0, (char *) &opt_no_display,
83  "Disable display and graphics viewer."},
84  #endif
85  {"-normalized", vpParseArgv::ARGV_INT, (char*) NULL, (char *) &opt_normalized,
86  "1 to use normalized features, 0 for non normalized."},
87  {"-h", vpParseArgv::ARGV_HELP, (char*) NULL, (char *) NULL,
88  "Print the help."},
89  {(char*) NULL, vpParseArgv::ARGV_END, (char*) NULL, (char*) NULL, (char*) NULL}
90  } ;
91 
92  // Read the command line options
93  if(vpParseArgv::parse(&argc, argv, argTable,
97  return (false);
98  }
99 
100  std::cout << "Used options: " << std::endl;
101 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
102  opt_curves = (opt_no_display == 0) ? 1 : 0;
103  std::cout << " - no display: " << opt_no_display << std::endl;
104  std::cout << " - curves : " << opt_curves << std::endl;
105 #endif
106  std::cout << " - normalized: " << opt_normalized << std::endl;
107 
108  vpCameraParameters cam(640.,480.,320.,240.);
109 
110 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
111  vpDisplay *display = NULL;
112  if (!opt_no_display) {
113 #if defined(VISP_HAVE_X11)
114  display = new vpDisplayX;
115 #elif defined VISP_HAVE_GDI
116  display = new vpDisplayGDI;
117 #endif
118  }
119 #endif
120  vpImage<unsigned char> I(480,640,0);
121 
122 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
123  if (!opt_no_display)
124  display->init(I);
125 #endif
126 
127  vpHomogeneousMatrix wMo; // Set to indentity. Robot world frame is equal to object frame
128  vpHomogeneousMatrix cMo (-0.5, 0.5, 2., vpMath::rad(10), vpMath::rad(20), vpMath::rad(30));
129  vpHomogeneousMatrix cdMo(0., 0., 1., vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
130  vpHomogeneousMatrix wMc; // Camera location in the robot world frame
131 
132  vpPoint P[4]; // 4 points in the object frame
133  P[0].setWorldCoordinates( .1, .1, 0.);
134  P[1].setWorldCoordinates(-.1, .1, 0.);
135  P[2].setWorldCoordinates(-.1, -.1, 0.);
136  P[3].setWorldCoordinates( .1, -.1, 0.);
137 
138  vpPoint Pd[4]; // 4 points in the desired camera frame
139  for (int i=0; i<4; i++) {
140  Pd[i] = P[i];
141  Pd[i].project(cdMo);
142  }
143  vpPoint Pc[4]; // 4 points in the current camera frame
144  for (int i=0; i<4; i++) {
145  Pc[i] = P[i];
146  Pc[i].project(cMo);
147  }
148 
149  vpFeatureSegment seg_cur[2], seg_des[2]; // Current and desired features
150  for (int i=0; i <2; i++)
151  {
152  if (opt_normalized) {
153  seg_cur[i].setNormalized(true);
154  seg_des[i].setNormalized(true);
155  }
156  else {
157  seg_cur[i].setNormalized(false);
158  seg_des[i].setNormalized(false);
159  }
160  vpFeatureBuilder::create(seg_cur[i], Pc[i*2], Pc[i*2+1]);
161  vpFeatureBuilder::create(seg_des[i], Pd[i*2], Pd[i*2+1]);
162  seg_cur[i].print();
163  seg_des[i].print();
164  }
165 
166  //define visual servoing task
167  vpServo task;
170  task.setLambda(2.) ;
171 
172  for (int i=0; i <2; i++)
173  task.addFeature(seg_cur[i], seg_des[i]);
174 
175 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
176  if (!opt_no_display) {
178  for (int i=0; i <2; i++) {
179  seg_cur[i].display(cam, I, vpColor::red);
180  seg_des[i].display(cam, I, vpColor::green);
181  vpDisplay::flush(I);
182  }
183  }
184 #endif
185 
186 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
187  vpPlot *graph = NULL;
188  if (opt_curves)
189  {
190  //Create a window (700 by 700) at position (100, 200) with two graphics
191  graph = new vpPlot(2, 500, 500, 700, 10, "Curves...");
192 
193  //The first graphic contains 3 curve and the second graphic contains 3 curves
194  graph->initGraph(0,6);
195  graph->initGraph(1,8);
196  // graph->setTitle(0, "Velocities");
197  // graph->setTitle(1, "Error s-s*");
198  }
199 #endif
200 
201  //param robot
202  vpSimulatorCamera robot;
203  float sampling_time = 0.02f; // Sampling period in seconds
204  robot.setSamplingTime(sampling_time);
205  robot.setMaxTranslationVelocity(5.);
207  wMc = wMo * cMo.inverse();
208  robot.setPosition(wMc);
209  int iter=0;
210 
211  do {
212  double t = vpTime::measureTimeMs();
213  wMc = robot.getPosition();
214  cMo = wMc.inverse() * wMo;
215  for (int i=0; i <4; i++)
216  Pc[i].project(cMo);
217 
218  for (int i=0; i <2; i++)
219  vpFeatureBuilder::create(seg_cur[i], Pc[i*2], Pc[i*2+1]);
220 
221 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
222  if (!opt_no_display) {
224  for (int i=0; i <2; i++) {
225  seg_cur[i].display(cam, I, vpColor::red);
226  seg_des[i].display(cam, I, vpColor::green);
227  vpDisplay::flush(I);
228  }
229  }
230 #endif
231 
232  vpColVector v = task.computeControlLaw();
234 
235 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
236  if (opt_curves)
237  {
238  graph->plot(0, iter, v); // plot velocities applied to the robot
239  graph->plot(1, iter, task.getError()); // plot error vector
240  }
241 #endif
242 
243  vpTime::wait(t, sampling_time * 1000); // Wait 10 ms
244  iter ++;
245 
246  } while(( task.getError() ).sumSquare() > 0.0005);
247 
248  // A call to kill() is requested here to destroy properly the current
249  // and desired feature lists.
250  task.kill();
251 
252 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
253  if (graph != NULL)
254  delete graph;
255 #endif
256 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
257  if (!opt_no_display && display != NULL)
258  delete display;
259 #endif
260 
261  std::cout << "final error=" << ( task.getError() ).sumSquare() << std::endl;
262  return 0;
263  }
264  catch(vpException &e) {
265  std::cout << "Catch an exception: " << e << std::endl;
266  return 1;
267  }
268 }
269 
270 #else
271 int main()
272 {
273  std::cout << "Test empty since visp_robot module is not available.\n" << std::endl;
274  return 0;
275 }
276 
277 #endif
void setPosition(const vpHomogeneousMatrix &wMc)
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:157
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:169
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void setMaxTranslationVelocity(const double maxVt)
Definition: vpRobot.cpp:238
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines the simplest robot: a free flying camera.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:512
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
error that can be emited by ViSP classes.
Definition: vpException.h:73
vpHomogeneousMatrix inverse() const
void plot(const unsigned int graphNum, const unsigned int curveNum, const double x, const double y)
Definition: vpPlot.cpp:286
vpHomogeneousMatrix getPosition() const
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
static const vpColor red
Definition: vpColor.h:163
Class that defines what is a point.
Definition: vpPoint.h:59
virtual void setSamplingTime(const double &delta_t)
void kill()
Definition: vpServo.cpp:191
void print(const unsigned int select=FEATURE_ALL) const
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
Class that defines a 2D segment visual features. This class allow to consider two sets of visual feat...
static void display(const vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:391
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
static double rad(double deg)
Definition: vpMath.h:104
void setMaxRotationVelocity(const double maxVr)
Definition: vpRobot.cpp:262
void initGraph(unsigned int graphNum, unsigned int curveNbr)
Definition: vpPlot.cpp:203
void setNormalized(bool normalized)
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:111
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:113
vpColVector getError() const
Definition: vpServo.h:271
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222