Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
manServo4PointsDisplay.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 display.
32 */
33
38
43
44#include <visp3/core/vpConfig.h>
45#include <visp3/core/vpDebug.h>
46
47#if defined(VISP_HAVE_DISPLAY)
48
49#include <visp3/core/vpCameraParameters.h>
50#include <visp3/core/vpImage.h>
51#include <visp3/core/vpImageConvert.h>
52#include <visp3/core/vpTime.h>
53#include <visp3/gui/vpDisplayFactory.h>
54
55#include <visp3/core/vpHomogeneousMatrix.h>
56#include <visp3/core/vpIoTools.h>
57#include <visp3/core/vpMath.h>
58#include <visp3/robot/vpSimulatorCamera.h>
59#include <visp3/vision/vpPose.h>
60#include <visp3/visual_features/vpFeatureBuilder.h>
61#include <visp3/visual_features/vpFeaturePoint.h>
62#include <visp3/vs/vpServo.h>
63#include <visp3/vs/vpServoDisplay.h>
64
65int main()
66{
67#ifdef ENABLE_VISP_NAMESPACE
68 using namespace VISP_NAMESPACE_NAME;
69#endif
70
71#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
72 std::shared_ptr<vpDisplay> disp = vpDisplayFactory::createDisplay();
73#else
75#endif
76 try {
78 // sets the initial camera location
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
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_DISPLAY)
100 disp->init(I, 100, 100, "Simulation display");
101#endif
102
104 // Desired visual features initialization
105
106 // sets the points coordinates in the object frame (in meter)
107 vpPoint point[4];
108 point[0].setWorldCoordinates(-0.1, -0.1, 0);
109 point[1].setWorldCoordinates(0.1, -0.1, 0);
110 point[2].setWorldCoordinates(0.1, 0.1, 0);
111 point[3].setWorldCoordinates(-0.1, 0.1, 0);
112
113 // sets the desired camera location
114 vpHomogeneousMatrix cMo_d(0, 0, 1, 0, 0, 0);
115
116 // computes the 3D point coordinates in the camera frame and its 2D
117 // coordinates
118 for (int i = 0; i < 4; i++)
119 point[i].project(cMo_d);
120
121 // creates the associated features
122 vpFeaturePoint pd[4];
123 for (int i = 0; i < 4; i++)
124 vpFeatureBuilder::create(pd[i], point[i]);
125
127 // Current visual features initialization
128
129 // computes the 3D point coordinates in the camera frame and its 2D
130 // coordinates
131 for (int i = 0; i < 4; i++)
132 point[i].project(cMo);
133
134 // creates the associated features
135 vpFeaturePoint p[4];
136 for (int i = 0; i < 4; i++)
137 vpFeatureBuilder::create(p[i], point[i]);
138
140 // Task defintion
142 // we want an eye-in-hand control law ;
144 task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
145
146 // Set the position of the end-effector frame in the camera frame as identity
148 vpVelocityTwistMatrix cVe(cMe);
149 task.set_cVe(cVe);
150 // Set the Jacobian (expressed in the end-effector frame)
151 vpMatrix eJe;
152 robot.get_eJe(eJe);
153 task.set_eJe(eJe);
154
155 // we want to see a point on a point
156 for (int i = 0; i < 4; i++)
157 task.addFeature(p[i], pd[i]);
158 // Set the gain
159 task.setLambda(1.0);
160 // Print the current information about the task
161 task.print();
162
164 // The control loop
165 int k = 0;
166 while (k++ < 200) {
167 double t = vpTime::measureTimeMs();
168
169 // Display the image background
171
172 // Update the current features
173 for (int i = 0; i < 4; i++) {
174 point[i].project(cMo);
175 vpFeatureBuilder::create(p[i], point[i]);
176 }
177
178 // Display the task features (current and desired)
179 vpServoDisplay::display(task, cam, I);
181
182 // Update the robot Jacobian
183 robot.get_eJe(eJe);
184 task.set_eJe(eJe);
185
186 // Compute the control law
187 vpColVector v = task.computeControlLaw();
188
189 // Send the computed velocity to the robot and compute the new robot
190 // position
191 robot.setVelocity(vpRobot::ARTICULAR_FRAME, v);
192 wMc = robot.getPosition();
193 cMo = wMc.inverse() * wMo;
194
195 // Print the current information about the task
196 task.print();
197
198 // Wait 40 ms
199 vpTime::wait(t, 40);
200 }
201#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
202 if (disp != nullptr) {
203 delete disp;
204 }
205#endif
206 return EXIT_SUCCESS;
207 }
208 catch (const vpException &e) {
209 std::cout << "Catch an exception: " << e << std::endl;
210#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
211 if (disp != nullptr) {
212 delete disp;
213 }
214#endif
215 return EXIT_FAILURE;
216 }
217}
218
219#else
220int main()
221{
222 std::cout
223 << "You do not have X11, GTK, or OpenCV, or GDI (Graphical Device Interface) functionalities to display images..."
224 << std::endl;
225 std::cout << "Tip if you are on a unix-like system:" << std::endl;
226 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
227 std::cout << "Tip if you are on a windows-like system:" << std::endl;
228 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
229 return EXIT_SUCCESS;
230}
231#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)
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 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
@ PSEUDO_INVERSE
Definition vpServo.h:250
@ DESIRED
Definition vpServo.h:223
Class that defines the simplest robot: a free flying camera.
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)