Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
manSimu4Points.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.
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))
50
51#include <visp3/ar/vpSimulator.h>
52#include <visp3/core/vpCameraParameters.h>
53#include <visp3/core/vpImage.h>
54#include <visp3/core/vpTime.h>
55
56#include <visp3/core/vpHomogeneousMatrix.h>
57#include <visp3/core/vpIoTools.h>
58#include <visp3/core/vpMath.h>
59#include <visp3/io/vpParseArgv.h>
60#include <visp3/robot/vpSimulatorCamera.h>
61#include <visp3/visual_features/vpFeatureBuilder.h>
62#include <visp3/visual_features/vpFeaturePoint.h>
63#include <visp3/vs/vpServo.h>
64
65#ifdef ENABLE_VISP_NAMESPACE
66using namespace VISP_NAMESPACE_NAME;
67#endif
68
69static void *mainLoop(void *_simu)
70{
71 // pointer copy of the vpSimulator instance
72 vpSimulator *simu = static_cast<vpSimulator *>(_simu);
73
74 // Simulation initialization
75 simu->initMainApplication();
76
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
86 robot.setSamplingTime(0.04); // 40ms
87 wMc = wMo * cMo.inverse();
88 robot.setPosition(wMc);
89 // Send the robot position to the visualizator
90 simu->setCameraPosition(cMo);
91 // Initialize the camera parameters
93 simu->getCameraParameters(cam);
94
96 // Desired visual features initialization
97
98 // sets the points coordinates in the object frame (in meter)
99 vpPoint point[4];
100 point[0].setWorldCoordinates(-0.1, -0.1, 0);
101 point[1].setWorldCoordinates(0.1, -0.1, 0);
102 point[2].setWorldCoordinates(0.1, 0.1, 0);
103 point[3].setWorldCoordinates(-0.1, 0.1, 0);
104
105 // sets the desired camera location
106 vpHomogeneousMatrix cMo_d(0, 0, 1, 0, 0, 0);
107
108 // computes the 3D point coordinates in the camera frame and its 2D
109 // coordinates
110 for (int i = 0; i < 4; i++)
111 point[i].project(cMo_d);
112
113 // creates the associated features
114 vpFeaturePoint pd[4];
115 for (int i = 0; i < 4; i++)
116 vpFeatureBuilder::create(pd[i], point[i]);
117
119 // Current visual features initialization
120
121 // computes the 3D point coordinates in the camera frame and its 2D
122 // coordinates
123 for (int i = 0; i < 4; i++)
124 point[i].project(cMo);
125
126 // creates the associated features
127 vpFeaturePoint p[4];
128 for (int i = 0; i < 4; i++)
129 vpFeatureBuilder::create(p[i], point[i]);
130
132 // Task defintion
134 // we want an eye-in-hand control law ;
136 task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
137
138 // Set the position of the end-effector frame in the camera frame as identity
140 vpVelocityTwistMatrix cVe(cMe);
141 task.set_cVe(cVe);
142 // Set the Jacobian (expressed in the end-effector frame)
143 vpMatrix eJe;
144 robot.get_eJe(eJe);
145 task.set_eJe(eJe);
146
147 // we want to see a point on a point
148 for (int i = 0; i < 4; i++)
149 task.addFeature(p[i], pd[i]);
150 // Set the gain
151 task.setLambda(1.0);
152 // Print the current information about the task
153 task.print();
154
155 vpTime::wait(500);
157 // The control loop
158 int k = 0;
159 while (k++ < 200) {
160 double t = vpTime::measureTimeMs();
161
162 // Update the current features
163 for (int i = 0; i < 4; i++) {
164 point[i].project(cMo);
165 vpFeatureBuilder::create(p[i], point[i]);
166 }
167
168 // Update the robot Jacobian
169 robot.get_eJe(eJe);
170 task.set_eJe(eJe);
171
172 // Compute the control law
173 vpColVector v = task.computeControlLaw();
174
175 // Send the computed velocity to the robot and compute the new robot
176 // position
177 robot.setVelocity(vpRobot::ARTICULAR_FRAME, v);
178 wMc = robot.getPosition();
179 cMo = wMc.inverse() * wMo;
180
181 // Send the robot position to the visualizator
182 simu->setCameraPosition(cMo);
183
184 // Print the current information about the task
185 task.print();
186
187 // Wait 40 ms
188 vpTime::wait(t, 40);
189 }
190 simu->closeMainApplication();
191
192 void *a = nullptr;
193 return a;
194 // return (void *);
195}
196
197int main()
198{
199 try {
200 vpSimulator simu;
201
202 // Internal view initialization : view from the robot camera
203 simu.initInternalViewer(480, 360);
204 // External view initialization : view from an external camera
205 simu.initExternalViewer(300, 300);
206
207 // Inernal camera parameters initialization
208 vpCameraParameters cam(800, 800, 240, 180);
210
211 vpTime::wait(1000);
212 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
213 // environment variable value
214 std::string ipath = vpIoTools::getViSPImagesDataPath();
215 std::string filename = "./4points.iv";
216
217 // Set the default input path
218 if (!ipath.empty())
219 filename = vpIoTools::createFilePath(ipath, "iv/4points.iv");
220
221 std::cout << "Load : " << filename << std::endl << "This file should be in the working directory" << std::endl;
222
223 simu.load(filename.c_str());
224
225 // Run the main loop
226 simu.initApplication(&mainLoop);
227 // Run the simulator
228 simu.mainLoop();
229 return EXIT_SUCCESS;
230 }
231 catch (const vpException &e) {
232 std::cout << "Catch an exception: " << e << std::endl;
233 return EXIT_FAILURE;
234 }
235}
236
237#else
238int main()
239{
240 std::cout << "You do not have Coin3D and SoQT or SoWin or SoXt functionalities enabled..." << std::endl;
241 std::cout << "Tip:" << std::endl;
242 std::cout
243 << "- Install Coin3D and SoQT or SoWin or SoXt, configure ViSP again using cmake and build again this example"
244 << std::endl;
245 return EXIT_SUCCESS;
246}
247#endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
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
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
@ EYEINHAND_L_cVe_eJe
Definition vpServo.h:183
@ PSEUDO_INVERSE
Definition vpServo.h:250
@ 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
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 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
virtual void initInternalViewer(unsigned int nlig, unsigned int ncol)
initialize the camera view
void closeMainApplication()
VISP_EXPORT double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)