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 * Simulation of a visual servoing with display.
33 *
34 * Authors:
35 * Eric Marchand
36 * Fabien Spindler
37 *
38 *****************************************************************************/
39
40 /*!
41 \file manServo4PointsDisplay.cpp
42 \brief Visual servoing experiment on 4 points with a display.
43 */
44
45 /*!
46 \example manServo4PointsDisplay.cpp
47 Visual servoing experiment on 4 points with a display.
48 */
49
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
main()74 int main()
75 {
76 try {
77 //////////////////////////////////////////
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
83 ///////////////////////////////////
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
109 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
110 disp.init(I, 100, 100, "Simulation display");
111 #endif
112
113 ////////////////////////////////////////
114 // Desired visual features initialization
115
116 // sets the points coordinates in the object frame (in meter)
117 vpPoint point[4];
118 point[0].setWorldCoordinates(-0.1, -0.1, 0);
119 point[1].setWorldCoordinates(0.1, -0.1, 0);
120 point[2].setWorldCoordinates(0.1, 0.1, 0);
121 point[3].setWorldCoordinates(-0.1, 0.1, 0);
122
123 // sets the desired camera location
124 vpHomogeneousMatrix cMo_d(0, 0, 1, 0, 0, 0);
125
126 // computes the 3D point coordinates in the camera frame and its 2D
127 // coordinates
128 for (int i = 0; i < 4; i++)
129 point[i].project(cMo_d);
130
131 // creates the associated features
132 vpFeaturePoint pd[4];
133 for (int i = 0; i < 4; i++)
134 vpFeatureBuilder::create(pd[i], point[i]);
135
136 ///////////////////////////////////////
137 // Current visual features initialization
138
139 // computes the 3D point coordinates in the camera frame and its 2D
140 // coordinates
141 for (int i = 0; i < 4; i++)
142 point[i].project(cMo);
143
144 // creates the associated features
145 vpFeaturePoint p[4];
146 for (int i = 0; i < 4; i++)
147 vpFeatureBuilder::create(p[i], point[i]);
148
149 /////////////////////////////////
150 // Task defintion
151 vpServo task;
152 // we want an eye-in-hand control law ;
153 task.setServo(vpServo::EYEINHAND_L_cVe_eJe);
154 task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
155
156 // Set the position of the end-effector frame in the camera frame as identity
157 vpHomogeneousMatrix cMe;
158 vpVelocityTwistMatrix cVe(cMe);
159 task.set_cVe(cVe);
160 // Set the Jacobian (expressed in the end-effector frame)
161 vpMatrix eJe;
162 robot.get_eJe(eJe);
163 task.set_eJe(eJe);
164
165 // we want to see a point on a point
166 for (int i = 0; i < 4; i++)
167 task.addFeature(p[i], pd[i]);
168 // Set the gain
169 task.setLambda(1.0);
170 // Print the current information about the task
171 task.print();
172
173 ////////////////////////////////////////////////
174 // The control loop
175 int k = 0;
176 while (k++ < 200) {
177 double t = vpTime::measureTimeMs();
178
179 // Display the image background
180 vpDisplay::display(I);
181
182 // Update the current features
183 for (int i = 0; i < 4; i++) {
184 point[i].project(cMo);
185 vpFeatureBuilder::create(p[i], point[i]);
186 }
187
188 // Display the task features (current and desired)
189 vpServoDisplay::display(task, cam, I);
190 vpDisplay::flush(I);
191
192 // Update the robot Jacobian
193 robot.get_eJe(eJe);
194 task.set_eJe(eJe);
195
196 // Compute the control law
197 vpColVector v = task.computeControlLaw();
198
199 // Send the computed velocity to the robot and compute the new robot
200 // position
201 robot.setVelocity(vpRobot::ARTICULAR_FRAME, v);
202 wMc = robot.getPosition();
203 cMo = wMc.inverse() * wMo;
204
205 // Print the current information about the task
206 task.print();
207
208 // Wait 40 ms
209 vpTime::wait(t, 40);
210 }
211 return EXIT_SUCCESS;
212 } catch (const vpException &e) {
213 std::cout << "Catch an exception: " << e << std::endl;
214 return EXIT_FAILURE;
215 }
216 }
217
218 #else
main()219 int main()
220 {
221 std::cout << "You do not have X11, GTK, or OpenCV, or GDI (Graphical Device Interface) functionalities to display images..." << std::endl;
222 std::cout << "Tip if you are on a unix-like system:" << std::endl;
223 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
224 std::cout << "Tip if you are on a windows-like system:" << std::endl;
225 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
226 return EXIT_SUCCESS;
227 }
228 #endif
229