Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
manSimu4Dots.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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 * Simulation of a visual servoing with visualization and image generation.
32 */
33
39
45
46#include <visp3/core/vpConfig.h>
47#include <visp3/core/vpDebug.h>
48
49#if (defined(VISP_HAVE_COIN3D_AND_GUI) && defined(VISP_HAVE_DISPLAY))
50
51#include <visp3/ar/vpSimulator.h>
52#include <visp3/core/vpCameraParameters.h>
53#include <visp3/core/vpImage.h>
54#include <visp3/core/vpImageConvert.h>
55#include <visp3/core/vpTime.h>
56
57#include <visp3/gui/vpDisplayFactory.h>
58// You may have strange compiler issues using the simulator based on SoQt
59// and the vpDisplayGTK. In that case prefer to use another display like
60// vpDisplayX under linux or vpDisplayGDI under Windows
61#include <visp3/blob/vpDot2.h>
62#include <visp3/core/vpHomogeneousMatrix.h>
63#include <visp3/core/vpIoTools.h>
64#include <visp3/core/vpMath.h>
65#include <visp3/robot/vpSimulatorCamera.h>
66#include <visp3/vision/vpPose.h>
67#include <visp3/visual_features/vpFeatureBuilder.h>
68#include <visp3/visual_features/vpFeaturePoint.h>
69#include <visp3/vs/vpServo.h>
70#include <visp3/vs/vpServoDisplay.h>
71
72#ifdef ENABLE_VISP_NAMESPACE
73using namespace VISP_NAMESPACE_NAME;
74#endif
75
76static void *mainLoop(void *_simu)
77{
78 // pointer copy of the vpSimulator instance
79 vpSimulator *simu = static_cast<vpSimulator *>(_simu);
80
81 // Simulation initialization
82 simu->initMainApplication();
83
85 // Set the initial camera location
87 vpHomogeneousMatrix wMo; // Set to identity
88 vpHomogeneousMatrix wMc; // Camera position in the world frame
89
91 // Initialize the robot
93 robot.setSamplingTime(0.04); // 40ms
94 wMc = wMo * cMo.inverse();
95 robot.setPosition(wMc);
96 // Send the robot position to the visualizator
97 simu->setCameraPosition(cMo);
98 // Initialize the camera parameters
100 simu->getCameraParameters(cam);
101
103 // Desired visual features initialization
104
105 // sets the points coordinates in the object frame (in meter)
106 vpPoint point[4];
107 point[0].setWorldCoordinates(-0.1, -0.1, 0);
108 point[1].setWorldCoordinates(0.1, -0.1, 0);
109 point[2].setWorldCoordinates(0.1, 0.1, 0);
110 point[3].setWorldCoordinates(-0.1, 0.1, 0);
111
112 // sets the desired camera location
113 vpHomogeneousMatrix cMo_d(0, 0, 1, 0, 0, 0);
114
115 // computes the 3D point coordinates in the camera frame and its 2D
116 // coordinates
117 for (int i = 0; i < 4; i++)
118 point[i].project(cMo_d);
119
120 // creates the associated features
121 vpFeaturePoint pd[4];
122 for (int i = 0; i < 4; i++)
123 vpFeatureBuilder::create(pd[i], point[i]);
124
126 // Current visual features initialization
127 unsigned int height = simu->getInternalHeight();
128 unsigned int width = simu->getInternalWidth();
129
130 // Create a greyscale image
131 vpImage<unsigned char> I(height, width);
132
133 // Display initialization
134#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
135 std::shared_ptr<vpDisplay> disp = vpDisplayFactory::createDisplay(I, 100, 100, "Simulation display");
136#else
137 vpDisplay *disp = vpDisplayFactory::allocateDisplay(I, 100, 100, "Simulation display");
138#endif
139
140 // Get the current image
141 vpTime::wait(500); // wait to be sure the image is generated
142 simu->getInternalImage(I);
143
144 // Display the current image
147
148 // Initialize the four dots tracker
149 std::cout << "A click in the four dots clockwise. " << std::endl;
150 vpDot2 dot[4];
151 vpFeaturePoint p[4];
152 for (int i = 0; i < 4; i++) {
153 dot[i].setGraphics(true);
154 // Call for a click
155 std::cout << "A click in the dot " << i << std::endl;
156 dot[i].initTracking(I);
157 // Create the associated feature
158 vpFeatureBuilder::create(p[i], cam, dot[i]);
159 // flush the display
161 }
162
164 // Task defintion
166 // we want an eye-in-hand control law ;
168 task.setInteractionMatrixType(vpServo::DESIRED);
169
170 // Set the position of the end-effector frame in the camera frame as identity
172 vpVelocityTwistMatrix cVe(cMe);
173 task.set_cVe(cVe);
174 // Set the Jacobian (expressed in the end-effector frame)
175 vpMatrix eJe;
176 robot.get_eJe(eJe);
177 task.set_eJe(eJe);
178
179 // we want to see a point on a point
180 for (int i = 0; i < 4; i++)
181 task.addFeature(p[i], pd[i]);
182 // Set the gain
183 task.setLambda(1.0);
184 // Print the current information about the task
185 task.print();
186
187 vpTime::wait(500);
188
190 // The control loop
191 int k = 0;
192 while (k++ < 200) {
193 double t = vpTime::measureTimeMs();
194
195 // Get the current internal camera view and display it
196 simu->getInternalImage(I);
198
199 // Track the four dots and update the associated visual features
200 for (int i = 0; i < 4; i++) {
201 dot[i].track(I);
202 vpFeatureBuilder::create(p[i], cam, dot[i]);
203 }
204
205 // Display the desired and current visual features
206 vpServoDisplay::display(task, cam, I);
208
209 // Update the robot Jacobian
210 robot.get_eJe(eJe);
211 task.set_eJe(eJe);
212
213 // Compute the control law
214 vpColVector v = task.computeControlLaw();
215
216 // Send the computed velocity to the robot and compute the new robot
217 // position
218 robot.setVelocity(vpRobot::ARTICULAR_FRAME, v);
219 wMc = robot.getPosition();
220 cMo = wMc.inverse() * wMo;
221
222 // Send the robot position to the visualizator
223 simu->setCameraPosition(cMo);
224
225 // Wait 40 ms
226 vpTime::wait(t, 40);
227 }
228 // Print information about the task
229 task.print();
230 simu->closeMainApplication();
231
232 void *a = nullptr;
233#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
234 if (disp != nullptr) {
235 delete disp;
236 }
237#endif
238 return a;
239}
240
241int main()
242{
243 try {
244 vpSimulator simu;
245
246 // Internal view initialization : view from the robot camera
247 simu.initInternalViewer(480, 360);
248 // External view initialization : view from an external camera
249 simu.initExternalViewer(300, 300);
250
251 // Inernal camera parameters initialization
252 vpCameraParameters cam(800, 800, 240, 180);
254
255 vpTime::wait(500);
256 // Load the scene
257
258 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
259 // environment variable value
260 std::string ipath = vpIoTools::getViSPImagesDataPath();
261 std::string filename = "./4points.iv";
262
263 // Set the default input path
264 if (!ipath.empty())
265 filename = vpIoTools::createFilePath(ipath, "iv/4points.iv");
266
267 std::cout << "Load : " << filename << std::endl << "This file should be in the working directory" << std::endl;
268
269 simu.load(filename.c_str());
270
271 // Run the main loop
272 simu.initApplication(&mainLoop);
273 // Run the simulator
274 simu.mainLoop();
275 return EXIT_SUCCESS;
276 }
277 catch (const vpException &e) {
278 std::cout << "Catch an exception: " << e << std::endl;
279 return EXIT_FAILURE;
280 }
281}
282
283#else
284int main()
285{
286 std::cout
287 << "You do not have X11, GTK, or OpenCV, or GDI (Graphical Device Interface) functionalities to display images..."
288 << std::endl;
289 std::cout << "Tip if you are on a unix-like system:" << std::endl;
290 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
291 std::cout << "Tip if you are on a windows-like system:" << std::endl;
292 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
293 return EXIT_SUCCESS;
294}
295#endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition vpDot2.h:127
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition vpDot2.cpp:441
void setGraphics(bool activate)
Definition vpDot2.h:320
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition vpDot2.cpp:263
error that can be emitted by ViSP classes.
Definition vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
Definition of the vpImage class member functions.
Definition vpImage.h:131
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
static double rad(double deg)
Definition vpMath.h:129
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:116
virtual void setSamplingTime(const double &delta_t)
@ ARTICULAR_FRAME
Definition vpRobot.h:77
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)
@ EYEINHAND_L_cVe_eJe
Definition vpServo.h:183
@ DESIRED
Definition vpServo.h:223
Class that defines the simplest robot: a free flying camera.
Implementation of a simulator based on Coin3d (www.coin3d.org).
Definition vpSimulator.h:95
unsigned int getInternalWidth() const
void load(const char *file_name)
load an iv file
void setInternalCameraParameters(vpCameraParameters &cam)
set internal camera parameters
virtual void mainLoop()
activate the mainloop
void getCameraParameters(vpCameraParameters &cam)
get the intrinsic parameters of the camera
void getInternalImage(vpImage< unsigned char > &I)
get an Image of the internal view
void initMainApplication()
perform some initialization in the main program thread
void initApplication(void *(*start_routine)(void *))
begin the main program
void setCameraPosition(vpHomogeneousMatrix &cMf)
set the camera position (from an homogeneous matrix)
void initExternalViewer(unsigned int nlig, unsigned int ncol)
initialize the external view
unsigned int getInternalHeight() const
virtual void initInternalViewer(unsigned int nlig, unsigned int ncol)
initialize the camera view
void closeMainApplication()
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)