Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoViper850Point2DArtVelocity-jointAvoidance-basic.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
44
45#include <visp3/core/vpConfig.h>
46#include <visp3/core/vpDebug.h> // Debug trace
47
48#include <cmath> // std::fabs
49#include <fstream>
50#include <iostream>
51#include <limits> // numeric_limits
52#include <sstream>
53#include <stdio.h>
54#include <stdlib.h>
55
56#if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394) && defined(VISP_HAVE_DISPLAY))
57
58#include <visp3/blob/vpDot2.h>
59#include <visp3/core/vpDisplay.h>
60#include <visp3/core/vpException.h>
61#include <visp3/core/vpHomogeneousMatrix.h>
62#include <visp3/core/vpImage.h>
63#include <visp3/core/vpIoTools.h>
64#include <visp3/core/vpMath.h>
65#include <visp3/core/vpPoint.h>
66#include <visp3/gui/vpDisplayFactory.h>
67#include <visp3/gui/vpPlot.h>
68#include <visp3/robot/vpRobotViper850.h>
69#include <visp3/sensor/vp1394TwoGrabber.h>
70#include <visp3/visual_features/vpFeatureBuilder.h>
71#include <visp3/visual_features/vpFeaturePoint.h>
72#include <visp3/vs/vpServo.h>
73#include <visp3/vs/vpServoDisplay.h>
74
75int main()
76{
77#ifdef ENABLE_VISP_NAMESPACE
78 using namespace VISP_NAMESPACE_NAME;
79#endif
80
81#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
82 std::shared_ptr<vpDisplay> display;
83#else
84 vpDisplay *display = nullptr;
85#endif
86
87 try {
88 vpRobotViper850 robot;
89
91
93
94 bool reset = false;
95 vp1394TwoGrabber g(reset);
98 g.open(I);
99
100 g.acquire(I);
101
102 double Tloop = 1. / 60.f;
103
105 g.getFramerate(fps);
106 switch (fps) {
108 Tloop = 1.f / 15.f;
109 break;
111 Tloop = 1.f / 30.f;
112 break;
114 Tloop = 1.f / 60.f;
115 break;
117 Tloop = 1.f / 120.f;
118 break;
119 default:
120 break;
121 }
122 std::cout << "Tloop: " << Tloop << std::endl;
123
124#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
125 display = vpDisplayFactory::createDisplay(I, 800, 100, "Current image");
126#else
127 display = vpDisplayFactory::allocateDisplay(I, 800, 100, "Current image");
128#endif
129
132
133 vpColVector jointMin(6), jointMax(6);
134 jointMin = robot.getJointMin();
135 jointMax = robot.getJointMax();
136
137 vpColVector Qmin(6), tQmin(6);
138 vpColVector Qmax(6), tQmax(6);
139 vpColVector Qmiddle(6);
140 vpColVector data(10);
141
142 double rho = 0.25;
143 for (unsigned int i = 0; i < 6; i++) {
144 Qmin[i] = jointMin[i] + 0.5 * rho * (jointMax[i] - jointMin[i]);
145 Qmax[i] = jointMax[i] - 0.5 * rho * (jointMax[i] - jointMin[i]);
146 }
147 Qmiddle = (Qmin + Qmax) / 2.;
148 double rho1 = 0.1;
149
150 for (unsigned int i = 0; i < 6; i++) {
151 tQmin[i] = Qmin[i] + 0.5 * (rho1) * (Qmax[i] - Qmin[i]);
152 tQmax[i] = Qmax[i] - 0.5 * (rho1) * (Qmax[i] - Qmin[i]);
153 }
154
155 vpColVector q(6);
156
157 // Create a window with two graphics
158 // - first graphic to plot q(t), Qmin, Qmax, tQmin and tQmax
159 // - second graphic to plot the cost function h_s
160 vpPlot plot(2);
161
162 // The first graphic contains 10 data to plot: q(t), Qmin, Qmax, tQmin and
163 // tQmax
164 plot.initGraph(0, 10);
165 plot.initGraph(1, 6);
166
167 // For the first graphic :
168 // - along the x axis the expected values are between 0 and 200 and
169 // the step is 1
170 // - along the y axis the expected values are between -1.2 and 1.2 and the
171 // step is 0.1
172 plot.initRange(0, 0, 200, 1, -1.2, 1.2, 0.1);
173 plot.setTitle(0, "Joint behavior");
174 plot.initRange(1, 0, 200, 1, -0.01, 0.01, 0.05);
175 plot.setTitle(1, "Joint velocity");
176
177 // For the first graphic, set the curves legend
178 std::string legend;
179 for (unsigned int i = 0; i < 6; i++) {
180 legend = "q" + i + 1;
181 plot.setLegend(0, i, legend);
182 plot.setLegend(1, i, legend);
183 }
184 plot.setLegend(0, 6, "tQmin");
185 plot.setLegend(0, 7, "tQmax");
186 plot.setLegend(0, 8, "Qmin");
187 plot.setLegend(0, 9, "Qmax");
188
189 // Set the curves color
190 plot.setColor(0, 0, vpColor::red);
191 plot.setColor(0, 1, vpColor::green);
192 plot.setColor(0, 2, vpColor::blue);
193 plot.setColor(0, 3, vpColor::orange);
194 plot.setColor(0, 4, vpColor(0, 128, 0));
195 plot.setColor(0, 5, vpColor::cyan);
196 for (unsigned int i = 6; i < 10; i++)
197 plot.setColor(0, i, vpColor::black); // for Q and tQ [min,max]
198 // Set the curves color
199
200 plot.setColor(1, 0, vpColor::red);
201 plot.setColor(1, 1, vpColor::green);
202 plot.setColor(1, 2, vpColor::blue);
203 plot.setColor(1, 3, vpColor::orange);
204 plot.setColor(1, 4, vpColor(0, 128, 0));
205 plot.setColor(1, 5, vpColor::cyan);
206 vpDot2 dot;
207
208 std::cout << "Click on a dot..." << std::endl;
209 dot.initTracking(I);
210 vpImagePoint cog = dot.getCog();
213
215 // Update camera parameters
216 robot.getCameraParameters(cam, I);
217
218 // sets the current position of the visual feature
220 vpFeatureBuilder::create(p, cam, dot); // retrieve x,y and Z of the vpPoint structure
221
222 p.set_Z(1);
223 // sets the desired position of the visual feature
225 pd.buildFrom(0, 0, 1);
226
227 // Define the task
228 // - we want an eye-in-hand control law
229 // - articular velocity are computed
231 task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
232
234 robot.get_cVe(cVe);
235 std::cout << cVe << std::endl;
236 task.set_cVe(cVe);
237
238 // - Set the Jacobian (expressed in the end-effector frame)") ;
239 vpMatrix eJe;
240 robot.get_eJe(eJe);
241 task.set_eJe(eJe);
242
243 // - we want to see a point on a point..") ;
244 std::cout << std::endl;
245 task.addFeature(p, pd);
246
247 // - set the gain
248 double lambda = 0.8;
249 // set to -1 to suppress the lambda used in the
250 // vpServo::computeControlLaw()
251 task.setLambda(-1);
252
253 // Display task information " ) ;
254 task.print();
255
256 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
257
258 int iter = 0;
259 double t_1 = vpTime::measureTimeMs();
260
261 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
262 for (;;) {
263 iter++;
264
265 double t_0 = vpTime::measureTimeMs(); // t_0: current time
266
267 // Update loop time in second
268 double Tv = static_cast<double>(t_0 - t_1) / 1000.0;
269 std::cout << "Tv: " << Tv << std::endl;
270
271 // Update time for next iteration
272 t_1 = t_0;
273
274 // Acquire a new image from the camera
275 dc1394video_frame_t *frame = g.dequeue(I);
276
277 // Display this image
279
280 // Achieve the tracking of the dot in the image
281 dot.track(I);
282 cog = dot.getCog();
283
284 // Display a green cross at the center of gravity position in the image
286
287 // Get the measured joint positions of the robot
288 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
289
290 // Update the point feature from the dot location
291 vpFeatureBuilder::create(p, cam, dot);
292
293 // Get the jacobian of the robot
294 robot.get_eJe(eJe);
295 // Update this jacobian in the task structure. It will be used to
296 // compute the velocity skew (as an articular velocity) qdot = -lambda *
297 // L^+ * cVe * eJe * (s-s*)
298 task.set_eJe(eJe);
299
300 vpColVector prim_task;
301 vpColVector e2(6);
302 // Compute the visual servoing skew vector
303 prim_task = task.computeControlLaw();
304
305 vpColVector qpre(6);
306
307 qpre = q;
308 qpre += -lambda * prim_task * (4 * Tloop);
309
310 // Identify the joints near the limits
311 vpColVector pb(6);
312 pb = 0;
313 unsigned int npb = 0;
314 for (unsigned int i = 0; i < 6; i++) {
315 if (q[i] < tQmin[i])
316 if (fabs(Qmin[i] - q[i]) > fabs(Qmin[i] - qpre[i])) {
317 pb[i] = 1;
318 npb++;
319 std::cout << "Joint " << i << " near limit " << std::endl;
320 }
321 if (q[i] > tQmax[i]) {
322 if (fabs(Qmax[i] - q[i]) > fabs(Qmax[i] - qpre[i])) {
323 pb[i] = 1;
324 npb++;
325 std::cout << "Joint " << i << " near limit " << std::endl;
326 }
327 }
328 }
329
330 vpColVector a0;
331 vpMatrix J1 = task.getTaskJacobian();
332 vpMatrix kernelJ1;
333 J1.kernel(kernelJ1);
334
335 unsigned int dimKernelL = kernelJ1.getCols();
336 if (npb != 0) {
337 // Build linear system a0*E = S
338 vpMatrix E(npb, dimKernelL);
339 vpColVector S(npb);
340
341 unsigned int k = 0;
342
343 for (unsigned int j = 0; j < 6; j++) // j is the joint
344 if (std::fabs(pb[j] - 1) <= std::numeric_limits<double>::epsilon()) {
345 for (unsigned int i = 0; i < dimKernelL; i++)
346 E[k][i] = kernelJ1[j][i];
347
348 S[k] = -prim_task[j];
349 k++;
350 }
351 vpMatrix Ep;
352 // vpTRACE("nbp %d", npb);
353 Ep = E.t() * (E * E.t()).pseudoInverse();
354 a0 = Ep * S;
355
356 e2 = (kernelJ1 * a0);
357 // cout << "e2 " << e2.t() ;
358 }
359 else {
360 e2 = 0;
361 }
362 // std::cout << "e2: " << e2.t() << std::endl;
363
365 v = -lambda * (prim_task + e2);
366
367 // Display the current and desired feature points in the image display
368 vpServoDisplay::display(task, cam, I);
369
370 // Apply the computed joint velocities to the robot
371 robot.setVelocity(vpRobot::ARTICULAR_FRAME, v);
372
373 {
374 // Add the material to plot curves
375
376 // q normalized between (entre -1 et 1)
377 for (unsigned int i = 0; i < 6; i++) {
378 data[i] = (q[i] - Qmiddle[i]);
379 data[i] /= (Qmax[i] - Qmin[i]);
380 data[i] *= 2;
381 }
382 unsigned int joint = 2;
383 data[6] = 2 * (tQmin[joint] - Qmiddle[joint]) / (Qmax[joint] - Qmin[joint]);
384 data[7] = 2 * (tQmax[joint] - Qmiddle[joint]) / (Qmax[joint] - Qmin[joint]);
385 data[8] = -1;
386 data[9] = 1;
387
388 plot.plot(0, iter, data); // plot q, Qmin, Qmax, tQmin, tQmax
389 plot.plot(1, iter, v); // plot joint velocities applied to the robot
390 }
391
393
394 // Synchronize the loop with the image frame rate
395 vpTime::wait(t_0, 1000. * Tloop);
396 // Release the ring buffer used for the last image to start a new acq
397 g.enqueue(frame);
398 }
399
400 // Display task information
401 task.print();
402#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
403 if (display != nullptr) {
404 delete display;
405 }
406#endif
407 return EXIT_SUCCESS;
408 }
409 catch (const vpException &e) {
410 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
411#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
412 if (display != nullptr) {
413 delete display;
414 }
415#endif
416 return EXIT_FAILURE;
417 }
418}
419
420#else
421int main()
422{
423 std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
424 return EXIT_SUCCESS;
425}
426#endif
Class for firewire ieee1394 video devices using libdc1394-2.x api.
unsigned int getCols() const
Definition vpArray2D.h:423
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Class to define RGB colors available for display functionalities.
Definition vpColor.h:157
static const vpColor red
Definition vpColor.h:198
static const vpColor cyan
Definition vpColor.h:207
static const vpColor orange
Definition vpColor.h:208
static const vpColor blue
Definition vpColor.h:204
static const vpColor black
Definition vpColor.h:192
static const vpColor green
Definition vpColor.h:201
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
static void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
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
vpImagePoint getCog() const
Definition vpDot2.h:183
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...
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
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
unsigned int kernel(vpMatrix &kerAt, double svThreshold=1e-6) const
vpMatrix pseudoInverse(double svThreshold=1e-6) const
vpMatrix t() const
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition vpPlot.h:117
Control of Irisa's Viper S850 robot named Viper850.
@ ARTICULAR_FRAME
Definition vpRobot.h:77
@ 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
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)