ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testFeatureSegment.cpp
1 /****************************************************************************
2  *
3  * $Id: testFeature.cpp 3530 2012-01-03 10:52:12Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Visual feature manipulation (segment).
36  *
37  * Author:
38  * Filip Novotny
39  *
40  *****************************************************************************/
41 
42 #include <fstream>
43 #include <iostream>
44 #include <vector>
45 #include <numeric>
46 
47 #include <visp/vpConfig.h>
48 #include <visp/vpCameraParameters.h>
49 #include <visp/vpDisplay.h>
50 #include <visp/vpDisplayGDI.h>
51 #include <visp/vpDisplayX.h>
52 #include <visp/vpFeatureBuilder.h>
53 #include <visp/vpFeatureSegment.h>
54 #include <visp/vpHomogeneousMatrix.h>
55 #include <visp/vpImage.h>
56 #include <visp/vpMath.h>
57 #include <visp/vpParseArgv.h>
58 #include <visp/vpPlot.h>
59 #include <visp/vpPoint.h>
60 #include <visp/vpRobotCamera.h>
61 #include <visp/vpServo.h> //visual servoing task
62 
70 int main(int argc, const char **argv)
71 {
72 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
73  int opt_display = 1;
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_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_display;
103  std::cout << " - display : " << opt_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_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_display)
124  display->init(I);
125 #endif
126 
127  vpHomogeneousMatrix cMo (-0.5, 0.5, 4., vpMath::rad(10), vpMath::rad(20), vpMath::rad(90));
128  vpHomogeneousMatrix cdMo(0., 0., 1., vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
129 
130  vpPoint P[4]; // 4 points in the object frame
131  P[0].setWorldCoordinates( .1, .1, 0.);
132  P[1].setWorldCoordinates(-.1, .1, 0.);
133  P[2].setWorldCoordinates(-.1, -.1, 0.);
134  P[3].setWorldCoordinates( .1, -.1, 0.);
135 
136  vpPoint Pd[4]; // 4 points in the desired camera frame
137  for (int i=0; i<4; i++) {
138  Pd[i] = P[i];
139  Pd[i].project(cdMo);
140  }
141  vpPoint Pc[4]; // 4 points in the current camera frame
142  for (int i=0; i<4; i++) {
143  Pc[i] = P[i];
144  Pc[i].project(cMo);
145  }
146 
147  vpFeatureSegment seg_cur[2], seg_des[2]; // Current and desired features
148  for (int i=0; i <2; i++)
149  {
150  if (opt_normalized) {
151  seg_cur[i].setNormalized(true);
152  seg_des[i].setNormalized(true);
153  }
154  else {
155  seg_cur[i].setNormalized(false);
156  seg_des[i].setNormalized(false);
157  }
158  vpFeatureBuilder::create(seg_cur[i], Pc[i*2], Pc[i*2+1]);
159  vpFeatureBuilder::create(seg_des[i], Pd[i*2], Pd[i*2+1]);
160  seg_cur[i].print();
161  seg_des[i].print();
162  }
163 
164  //define visual servoing task
165  vpServo task;
168  task.setLambda(1) ;
169 
170  for (int i=0; i <2; i++)
171  task.addFeature(seg_cur[i], seg_des[i]);
172 
173 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
174  if (opt_display) {
176  for (int i=0; i <2; i++) {
177  seg_cur[i].display(cam, I, vpColor::red);
178  seg_des[i].display(cam, I, vpColor::green);
179  vpDisplay::flush(I);
180  }
181  }
182 #endif
183 
184 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
185  vpPlot *graph = NULL;
186  if (opt_curves)
187  {
188  //Create a window (700 by 700) at position (100, 200) with two graphics
189  graph = new vpPlot(2, 500, 500, 700, 10, "Curves...");
190 
191  //The first graphic contains 3 curve and the second graphic contains 3 curves
192  graph->initGraph(0,6);
193  graph->initGraph(1,8);
194 // graph->setTitle(0, "Velocities");
195 // graph->setTitle(1, "Error s-s*");
196  }
197 #endif
198 
199  //param robot
200  vpRobotCamera robot ;
201  float sampling_time = 0.010f ; // Sampling period in seconds
202  robot.setSamplingTime(sampling_time) ;
203  robot.setPosition(cMo) ;
204  int iter=0;
205 
206  do{
207  double t = vpTime::measureTimeMs();
208  robot.getPosition(cMo);
209  for (int i=0; i <4; i++)
210  Pc[i].project(cMo);
211 
212  for (int i=0; i <2; i++)
213  vpFeatureBuilder::create(seg_cur[i], Pc[i*2], Pc[i*2+1]);
214 
215 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
216  if (opt_display) {
218  for (int i=0; i <2; i++) {
219  seg_cur[i].display(cam, I, vpColor::red);
220  seg_des[i].display(cam, I, vpColor::green);
221  vpDisplay::flush(I);
222  }
223  }
224 #endif
225 
226  vpColVector v = task.computeControlLaw();
228 
229 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
230  if (opt_curves)
231  {
232  graph->plot(0, iter, v); // plot velocities applied to the robot
233  graph->plot(1, iter, task.getError()); // plot error vector
234  }
235 #endif
236 
237  vpTime::wait(t, sampling_time * 1000); // Wait 10 ms
238  iter ++;
239 
240  } while(( task.getError() ).sumSquare() > 0.0005);
241 
242  // A call to kill() is requested here to destroy properly the current
243  // and desired feature lists.
244  task.kill();
245 
246 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
247  if (graph != NULL)
248  delete graph;
249 #endif
250 #if (defined (VISP_HAVE_X11) || defined (VISP_HAVE_GDI))
251  if (opt_display && display != NULL)
252  delete display;
253 #endif
254 
255  std::cout << "final error=" << ( task.getError() ).sumSquare() << std::endl;
256 }
virtual void init(vpImage< unsigned char > &I, int x=-1, int y=-1, const char *title=NULL)=0
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:175
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void setPosition(const vpHomogeneousMatrix &cMw)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
create a new ste of two visual features
Definition: vpServo.cpp:444
void setLambda(double _lambda)
set the gain lambda
Definition: vpServo.h:253
void plot(const unsigned int graphNum, const unsigned int curveNum, const double x, const double y)
Definition: vpPlot.cpp:300
static double measureTimeMs()
Definition: vpTime.cpp:86
static int wait(double t0, double t)
Definition: vpTime.cpp:149
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static const vpColor red
Definition: vpColor.h:167
Class that defines what is a point.
Definition: vpPoint.h:65
virtual void setSamplingTime(const double &delta_t)
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector getError() const
Definition: vpServo.h:301
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
Class that defines a 2D segment visual features. This class allow to consider two sets of visual feat...
Class that defines the simplest robot: a free flying camera.
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
Generic class defining intrinsic camera parameters.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Set the type of the interaction matrix (current, mean, desired, user).
Definition: vpServo.cpp:509
static double rad(double deg)
Definition: vpMath.h:100
void initGraph(unsigned int graphNum, unsigned int curveNbr)
Definition: vpPlot.cpp:217
void getPosition(vpHomogeneousMatrix &cMw) const
void setNormalized(bool normalized)
Command line argument parsing.
Definition: vpParseArgv.h:133
Class that provides a data structure for the column vectors as well as a set of operations on these v...
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:117
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class required to compute the visual servoing control law descbribed in and .
Definition: vpServo.h:153
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &v)
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74
void print(const unsigned int select=FEATURE_ALL) const