Visual Servoing Platform  version 3.1.0
manServo4PointsDisplay.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 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  * Simulation of a visual servoing with display.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
50 #include <visp3/core/vpConfig.h>
51 #include <visp3/core/vpDebug.h>
52 
53 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
54 
55 #include <visp3/core/vpCameraParameters.h>
56 #include <visp3/core/vpImage.h>
57 #include <visp3/core/vpImageConvert.h>
58 #include <visp3/core/vpTime.h>
59 #include <visp3/gui/vpDisplayGDI.h>
60 #include <visp3/gui/vpDisplayGTK.h>
61 #include <visp3/gui/vpDisplayOpenCV.h>
62 #include <visp3/gui/vpDisplayX.h>
63 
64 #include <visp3/core/vpHomogeneousMatrix.h>
65 #include <visp3/core/vpIoTools.h>
66 #include <visp3/core/vpMath.h>
67 #include <visp3/robot/vpSimulatorCamera.h>
68 #include <visp3/vision/vpPose.h>
69 #include <visp3/visual_features/vpFeatureBuilder.h>
70 #include <visp3/visual_features/vpFeaturePoint.h>
71 #include <visp3/vs/vpServo.h>
72 #include <visp3/vs/vpServoDisplay.h>
73 
74 int main()
75 {
76  try {
78  // sets the initial camera location
79  vpHomogeneousMatrix cMo(0.3, 0.2, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(40));
80  vpHomogeneousMatrix wMo; // Set to identity
81  vpHomogeneousMatrix wMc; // Camera position in the world frame
82 
84  // initialize the robot
85  vpSimulatorCamera robot;
86  robot.setSamplingTime(0.04); // 40ms
87  wMc = wMo * cMo.inverse();
88  robot.setPosition(wMc);
89 
90  // initialize the camera parameters
91  vpCameraParameters cam(800, 800, 240, 180);
92 
93  // Image definition
94  unsigned int height = 360;
95  unsigned int width = 480;
96  vpImage<unsigned char> I(height, width);
97 
98 // Display initialization
99 #if defined(VISP_HAVE_X11)
100  vpDisplayX disp;
101 #elif defined(VISP_HAVE_GTK)
102  vpDisplayGTK disp;
103 #elif defined(VISP_HAVE_GDI)
104  vpDisplayGDI disp;
105 #elif defined(VISP_HAVE_OPENCV)
106  vpDisplayOpenCV disp;
107 #endif
108  disp.init(I, 100, 100, "Simulation display");
109 
111  // Desired visual features initialization
112 
113  // sets the points coordinates in the object frame (in meter)
114  vpPoint point[4];
115  point[0].setWorldCoordinates(-0.1, -0.1, 0);
116  point[1].setWorldCoordinates(0.1, -0.1, 0);
117  point[2].setWorldCoordinates(0.1, 0.1, 0);
118  point[3].setWorldCoordinates(-0.1, 0.1, 0);
119 
120  // sets the desired camera location
121  vpHomogeneousMatrix cMo_d(0, 0, 1, 0, 0, 0);
122 
123  // computes the 3D point coordinates in the camera frame and its 2D
124  // coordinates
125  for (int i = 0; i < 4; i++)
126  point[i].project(cMo_d);
127 
128  // creates the associated features
129  vpFeaturePoint pd[4];
130  for (int i = 0; i < 4; i++)
131  vpFeatureBuilder::create(pd[i], point[i]);
132 
134  // Current visual features initialization
135 
136  // computes the 3D point coordinates in the camera frame and its 2D
137  // coordinates
138  for (int i = 0; i < 4; i++)
139  point[i].project(cMo);
140 
141  // creates the associated features
142  vpFeaturePoint p[4];
143  for (int i = 0; i < 4; i++)
144  vpFeatureBuilder::create(p[i], point[i]);
145 
147  // Task defintion
148  vpServo task;
149  // we want an eye-in-hand control law ;
152 
153  // Set the position of the camera in the end-effector frame
155  vpVelocityTwistMatrix cVe(cMe);
156  task.set_cVe(cVe);
157  // Set the Jacobian (expressed in the end-effector frame)
158  vpMatrix eJe;
159  robot.get_eJe(eJe);
160  task.set_eJe(eJe);
161 
162  // we want to see a point on a point
163  for (int i = 0; i < 4; i++)
164  task.addFeature(p[i], pd[i]);
165  // Set the gain
166  task.setLambda(1.0);
167  // Print the current information about the task
168  task.print();
169 
171  // The control loop
172  int k = 0;
173  while (k++ < 200) {
174  double t = vpTime::measureTimeMs();
175 
176  // Display the image background
178 
179  // Update the current features
180  for (int i = 0; i < 4; i++) {
181  point[i].project(cMo);
182  vpFeatureBuilder::create(p[i], point[i]);
183  }
184 
185  // Display the task features (current and desired)
186  vpServoDisplay::display(task, cam, I);
187  vpDisplay::flush(I);
188 
189  // Update the robot Jacobian
190  robot.get_eJe(eJe);
191  task.set_eJe(eJe);
192 
193  // Compute the control law
194  vpColVector v = task.computeControlLaw();
195 
196  // Send the computed velocity to the robot and compute the new robot
197  // position
199  wMc = robot.getPosition();
200  cMo = wMc.inverse() * wMo;
201 
202  // Print the current information about the task
203  task.print();
204 
205  // Wait 40 ms
206  vpTime::wait(t, 40);
207  }
208  task.kill();
209  return 0;
210  } catch (vpException &e) {
211  std::cout << "Catch an exception: " << e << std::endl;
212  return 1;
213  }
214 }
215 
216 #else
217 int main() { vpTRACE("You should install GTK"); }
218 #endif
void setPosition(const vpHomogeneousMatrix &wMc)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
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:129
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:508
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
error that can be emited by ViSP classes.
Definition: vpException.h:71
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
vpHomogeneousMatrix inverse() const
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
vpHomogeneousMatrix getPosition() const
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
Class that defines what is a point.
Definition: vpPoint.h:58
virtual void setSamplingTime(const double &delta_t)
void kill()
Definition: vpServo.cpp:192
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
#define vpTRACE
Definition: vpDebug.h:416
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:406
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:574
static double rad(double deg)
Definition: vpMath.h:102
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:113
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:450
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:313
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void get_eJe(vpMatrix &eJe)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223
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)