Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoPtu46Point2DArtVelocity.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 * tests the control law
32 * eye-in-hand control
33 * velocity computed in articular
34 */
35
45
55#include <visp3/core/vpConfig.h>
56#include <visp3/core/vpDebug.h> // Debug trace
57#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
58#include <unistd.h>
59#endif
60#include <signal.h>
61
62#if defined(VISP_HAVE_PTU46) && defined(VISP_HAVE_DC1394) && defined(VISP_HAVE_THREADS)
63
64#include <mutex>
65
66#include <visp3/core/vpDisplay.h>
67#include <visp3/core/vpImage.h>
68#include <visp3/gui/vpDisplayFactory.h>
69#include <visp3/sensor/vp1394TwoGrabber.h>
70
71#include <visp3/core/vpHomogeneousMatrix.h>
72#include <visp3/core/vpMath.h>
73#include <visp3/core/vpPoint.h>
74#include <visp3/visual_features/vpFeatureBuilder.h>
75#include <visp3/visual_features/vpFeaturePoint.h>
76#include <visp3/vs/vpServo.h>
77
78#include <visp3/robot/vpRobotPtu46.h>
79
80// Exception
81#include <visp3/core/vpException.h>
82#include <visp3/vs/vpServoDisplay.h>
83
84#include <visp3/blob/vpDot2.h>
85
86std::mutex mutexEndLoop;
87
88void signalCtrC(int signumber)
89{
90 (void)(signumber);
91 mutexEndLoop.unlock();
92 usleep(1000 * 10);
93 vpTRACE("Ctrl-C pressed...");
94}
95
96int main()
97{
98#ifdef ENABLE_VISP_NAMESPACE
99 using namespace VISP_NAMESPACE_NAME;
100#endif
101
102 std::cout << std::endl;
103 std::cout << "-------------------------------------------------------" << std::endl;
104 std::cout << " Test program for vpServo " << std::endl;
105 std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
106 std::cout << " Simulation " << std::endl;
107 std::cout << " task : servo a point " << std::endl;
108 std::cout << "-------------------------------------------------------" << std::endl;
109 std::cout << std::endl;
110
111#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
112 std::shared_ptr<vpDisplay> display;
113#else
114 vpDisplay *display = nullptr;
115#endif
116 try {
117 mutexEndLoop.lock();
118 signal(SIGINT, &signalCtrC);
119
120 vpRobotPtu46 robot;
121 {
122 vpColVector q(2);
123 q = 0;
124 robot.setRobotState(vpRobot::STATE_POSITION_CONTROL);
125 robot.setPosition(vpRobot::ARTICULAR_FRAME, q);
126 }
127
129
131
132 g.open(I);
133
134 try {
135 g.acquire(I);
136 }
137 catch (...) {
138 vpERROR_TRACE(" Error caught");
139 return EXIT_FAILURE;
140 }
141
142#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
143 display = vpDisplayFactory::createDisplay(I, 100, 100, "Servo Ptu");
144#else
145 display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Servo Ptu");
146#endif
147 vpTRACE(" ");
148
149 try {
152 }
153 catch (...) {
154 vpERROR_TRACE(" Error caught");
155#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
156 if (display != nullptr) {
157 delete display;
158 }
159#endif
160 return EXIT_FAILURE;
161 }
162
164
165 vpDot2 dot;
166
167 try {
168 vpERROR_TRACE("start dot.initTracking(I) ");
169 vpImagePoint germ;
170 vpDisplay::getClick(I, germ);
171 dot.setCog(germ);
172 vpDEBUG_TRACE(25, "Click!");
173 // dot.initTracking(I) ;
174 dot.track(I);
175 vpERROR_TRACE("after dot.initTracking(I) ");
176 }
177 catch (...) {
178 vpERROR_TRACE(" Error caught ");
179#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
180 if (display != nullptr) {
181 delete display;
182 }
183#endif
184 return EXIT_FAILURE;
185 }
186
188
189 vpTRACE("sets the current position of the visual feature ");
191 vpFeatureBuilder::create(p, cam, dot); // retrieve x,y and Z of the vpPoint structure
192
193 p.set_Z(1);
194 vpTRACE("sets the desired position of the visual feature ");
196 pd.buildFrom(0, 0, 1);
197
198 vpTRACE("define the task");
199 vpTRACE("\t we want an eye-in-hand control law");
200 vpTRACE("\t articular velocity are computed");
202 task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
203
204 vpTRACE("Set the position of the end-effector frame in the camera frame");
206 // robot.get_cMe(cMe) ;
207
209 robot.get_cVe(cVe);
210 std::cout << cVe << std::endl;
211 task.set_cVe(cVe);
212
214 vpTRACE("Set the Jacobian (expressed in the end-effector frame)");
215 vpMatrix eJe;
216 robot.get_eJe(eJe);
217 task.set_eJe(eJe);
218
219 vpTRACE("\t we want to see a point on a point..");
220 std::cout << std::endl;
221 task.addFeature(p, pd);
222
223 vpTRACE("\t set the gain");
224 task.setLambda(0.1);
225
226 vpTRACE("Display task information ");
227 task.print();
228
229 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
230
231 unsigned int iter = 0;
232 vpTRACE("\t loop");
233 while (0 != mutexEndLoop.trylock()) {
234 std::cout << "---------------------------------------------" << iter << std::endl;
235
236 g.acquire(I);
238
239 dot.track(I);
240
241 vpFeatureBuilder::create(p, cam, dot);
242
243 // get the jacobian
244 robot.get_eJe(eJe);
245 task.set_eJe(eJe);
246
247 // std::cout << (vpMatrix)cVe*eJe << std::endl ;
248
250 v = task.computeControlLaw();
251
252 vpServoDisplay::display(task, cam, I);
253 std::cout << v.t();
254 robot.setVelocity(vpRobot::ARTICULAR_FRAME, v);
256
257 vpTRACE("\t\t || s - s* || = %f ", (task.getError()).sumSquare());
258 }
259
260 vpTRACE("Display task information ");
261 task.print();
262 }
263 catch (const vpException &e) {
264 std::cout << "Sorry PtU46 not available. Got exception: " << e << std::endl;
265#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
266 if (display != nullptr) {
267 delete display;
268 }
269#endif
270 return EXIT_FAILURE
271 }
272#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
273 if (display != nullptr) {
274 delete display;
275 }
276#endif
277 return EXIT_SUCCESS;
278}
279
280#else
281int main() { std::cout << "You do not have an PTU46 PT robot connected to your computer..." << std::endl; }
282#endif
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void acquire(vpImage< unsigned char > &I)
void open(vpImage< unsigned char > &I)
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 bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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 setCog(const vpImagePoint &ip)
Definition vpDot2.h: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...
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
Definition vpImage.h:131
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
Interface for the Directed Perception ptu-46 pan, tilt head .
@ ARTICULAR_FRAME
Definition vpRobot.h:77
@ STATE_POSITION_CONTROL
Initialize the position controller.
Definition vpRobot.h:65
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
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
#define vpTRACE
Definition vpDebug.h:450
#define vpDEBUG_TRACE
Definition vpDebug.h:526
#define vpERROR_TRACE
Definition vpDebug.h:423
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.