Visual Servoing Platform version 3.5.0
servoViper850Point2DArtVelocity-jointAvoidance-gpa.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See http://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * tests the control law
33 * eye-in-hand control
34 * velocity computed in articular
35 *
36 * Authors:
37 * Eric Marchand
38 * Fabien Spindler
39 *
40 *****************************************************************************/
41
50#include <visp3/core/vpConfig.h>
51#include <visp3/core/vpDebug.h> // Debug trace
52
53#include <fstream>
54#include <iostream>
55#include <sstream>
56#include <stdio.h>
57#include <stdlib.h>
58
59#if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394) && defined(VISP_HAVE_DISPLAY))
60
61#include <visp3/blob/vpDot2.h>
62#include <visp3/core/vpDisplay.h>
63#include <visp3/core/vpException.h>
64#include <visp3/core/vpHomogeneousMatrix.h>
65#include <visp3/core/vpImage.h>
66#include <visp3/core/vpIoTools.h>
67#include <visp3/core/vpMath.h>
68#include <visp3/core/vpPoint.h>
69#include <visp3/gui/vpDisplayGTK.h>
70#include <visp3/gui/vpDisplayOpenCV.h>
71#include <visp3/gui/vpDisplayX.h>
72#include <visp3/gui/vpPlot.h>
73#include <visp3/robot/vpRobotViper850.h>
74#include <visp3/sensor/vp1394TwoGrabber.h>
75#include <visp3/visual_features/vpFeatureBuilder.h>
76#include <visp3/visual_features/vpFeaturePoint.h>
77#include <visp3/vs/vpServo.h>
78#include <visp3/vs/vpServoDisplay.h>
79
80int main()
81{
82 try {
83 vpRobotViper850 robot;
84
85 vpServo task;
86
88
89 bool reset = false;
90 vp1394TwoGrabber g(reset);
93 g.open(I);
94
95 g.acquire(I);
96
97#ifdef VISP_HAVE_X11
98 vpDisplayX display(I, 800, 100, "Current image");
99#elif defined(VISP_HAVE_OPENCV)
100 vpDisplayOpenCV display(I, 800, 100, "Current image");
101#elif defined(VISP_HAVE_GTK)
102 vpDisplayGTK display(I, 800, 100, "Current image");
103#endif
104
107
108 vpColVector jointMin(6), jointMax(6);
109 jointMin = robot.getJointMin();
110 jointMax = robot.getJointMax();
111
112 vpColVector Qmin(6), tQmin(6);
113 vpColVector Qmax(6), tQmax(6);
114 vpColVector Qmiddle(6);
115 vpColVector data(10);
116
117 double rho = 0.15;
118 for (unsigned int i = 0; i < 6; i++) {
119 Qmin[i] = jointMin[i] + 0.5 * rho * (jointMax[i] - jointMin[i]);
120 Qmax[i] = jointMax[i] - 0.5 * rho * (jointMax[i] - jointMin[i]);
121 }
122 Qmiddle = (Qmin + Qmax) / 2.;
123 double rho1 = 0.1;
124
125 for (unsigned int i = 0; i < 6; i++) {
126 tQmin[i] = Qmin[i] + 0.5 * (rho1) * (Qmax[i] - Qmin[i]);
127 tQmax[i] = Qmax[i] - 0.5 * (rho1) * (Qmax[i] - Qmin[i]);
128 }
129
130 vpColVector q(6);
131
132 // Create a window with two graphics
133 // - first graphic to plot q(t), Qmin, Qmax, tQmin and tQmax
134 // - second graphic to plot the cost function h_s
135 vpPlot plot(2);
136
137 // The first graphic contains 10 data to plot: q(t), Qmin, Qmax, tQmin and
138 // tQmax
139 plot.initGraph(0, 10);
140 // The second graphic contains 1 curve, the cost function h_s
141 plot.initGraph(1, 1);
142
143 // For the first graphic :
144 // - along the x axis the expected values are between 0 and 200
145 // - along the y axis the expected values are between -1.2 and 1.2
146 plot.initRange(0, 0., 200., -1.2, 1.2);
147 plot.setTitle(0, "Joint behavior");
148
149 // For the second graphic :
150 // - along the x axis the expected values are between 0 and 200 and
151 // the step is 1
152 // - along the y axis the expected values are between 0 and 0.0001 and the
153 // step is 0.00001
154 plot.initRange(1, 0., 200., 0., 1e-4);
155 plot.setTitle(1, "Cost function");
156
157 // For the first graphic, set the curves legend
158 char legend[10];
159 for (unsigned int i = 0; i < 6; i++) {
160 sprintf(legend, "q%u", i + 1);
161 plot.setLegend(0, i, legend);
162 }
163 plot.setLegend(0, 6, "tQmin");
164 plot.setLegend(0, 7, "tQmax");
165 plot.setLegend(0, 8, "Qmin");
166 plot.setLegend(0, 9, "Qmax");
167
168 // Set the curves color
169 plot.setColor(0, 0, vpColor::red);
170 plot.setColor(0, 1, vpColor::green);
171 plot.setColor(0, 2, vpColor::blue);
172 plot.setColor(0, 3, vpColor::orange);
173 plot.setColor(0, 4, vpColor(0, 128, 0));
174 plot.setColor(0, 5, vpColor::cyan);
175 for (unsigned int i = 6; i < 10; i++)
176 plot.setColor(0, i, vpColor::black); // for Q and tQ [min,max]
177
178 // For the second graphic, set the curves legend
179 plot.setLegend(1, 0, "h_s");
180
181 double beta = 1;
182
183 // Set the amplitude of the control law due to the secondary task
184 std::cout << " Give the parameters beta (1) : ";
185 std::cin >> beta;
186
187 vpDot2 dot;
188
189 std::cout << "Click on a dot..." << std::endl;
190 dot.initTracking(I);
191 vpImagePoint cog = dot.getCog();
194
196 // Update camera parameters
197 robot.getCameraParameters(cam, I);
198
199 // sets the current position of the visual feature
201 vpFeatureBuilder::create(p, cam, dot); // retrieve x,y and Z of the vpPoint structure
202
203 p.set_Z(1);
204 // sets the desired position of the visual feature
206 pd.buildFrom(0, 0, 1);
207
208 // Define the task
209 // - we want an eye-in-hand control law
210 // - articular velocity are computed
213
215 robot.get_cVe(cVe);
216 std::cout << cVe << std::endl;
217 task.set_cVe(cVe);
218
219 // - Set the Jacobian (expressed in the end-effector frame)") ;
220 vpMatrix eJe;
221 robot.get_eJe(eJe);
222 task.set_eJe(eJe);
223
224 // - we want to see a point on a point..") ;
225 std::cout << std::endl;
226 task.addFeature(p, pd);
227
228 // - set the gain
229 task.setLambda(0.8);
230
231 // Display task information " ) ;
232 task.print();
233
235
236 int iter = 0;
237 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
238 for (;;) {
239 iter++;
240 // Acquire a new image from the camera
241 g.acquire(I);
242
243 // Display this image
245
246 // Achieve the tracking of the dot in the image
247 dot.track(I);
248 cog = dot.getCog();
249
250 // Display a green cross at the center of gravity position in the image
252
253 // Get the measured joint positions of the robot
254 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
255
256 // Update the point feature from the dot location
257 vpFeatureBuilder::create(p, cam, dot);
258
259 // Get the jacobian of the robot
260 robot.get_eJe(eJe);
261 // Update this jacobian in the task structure. It will be used to
262 // compute the velocity skew (as an articular velocity) qdot = -lambda *
263 // L^+ * cVe * eJe * (s-s*)
264 task.set_eJe(eJe);
265
266 vpColVector prim_task;
267 vpColVector e2(6);
268 // Compute the visual servoing skew vector
269 prim_task = task.computeControlLaw();
270
271 vpColVector sec_task(6);
272 double h_s = 0;
273 {
274 // joint limit avoidance with secondary task
275
276 vpColVector de2dt(6);
277 de2dt = 0;
278 e2 = 0;
279 for (unsigned int i = 0; i < 6; i++) {
280 double S = 0;
281 if (q[i] > tQmax[i])
282 S = q[i] - tQmax[i];
283 if (q[i] < tQmin[i])
284 S = q[i] - tQmin[i];
285 double D = (Qmax[i] - Qmin[i]);
286 h_s += vpMath::sqr(S) / D;
287 e2[i] = S / D;
288 }
289 h_s = beta * h_s / 2.0; // cost function
290 e2 *= beta;
291 // std::cout << e2.t() << std::endl;
292 std::cout << "Cost function h_s: " << h_s << std::endl;
293
294 sec_task = task.secondaryTask(e2, de2dt);
295 }
296
297 vpColVector v;
298 v = prim_task + sec_task;
299
300 // Display the current and desired feature points in the image display
301 vpServoDisplay::display(task, cam, I);
302
303 // Apply the computed joint velocities to the robot
305
306 {
307 // Add the material to plot curves
308
309 // q normalized between (entre -1 et 1)
310 for (unsigned int i = 0; i < 6; i++) {
311 data[i] = (q[i] - Qmiddle[i]);
312 data[i] /= (Qmax[i] - Qmin[i]);
313 data[i] *= 2;
314 }
315 unsigned int joint = 2;
316 data[6] = 2 * (tQmin[joint] - Qmiddle[joint]) / (Qmax[joint] - Qmin[joint]);
317 data[7] = 2 * (tQmax[joint] - Qmiddle[joint]) / (Qmax[joint] - Qmin[joint]);
318 data[8] = -1;
319 data[9] = 1;
320 plot.plot(0, iter, data); // plot q, Qmin, Qmax, tQmin, tQmax
321 plot.plot(1, 0, iter, h_s); // plot the cost function
322 }
323
325 }
326
327 // Display task information
328 task.print();
329 return EXIT_SUCCESS;
330 }
331 catch (const vpException &e) {
332 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
333 return EXIT_FAILURE;
334 }
335}
336
337#else
338int main()
339{
340 std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
341 return EXIT_SUCCESS;
342}
343#endif
Class for firewire ieee1394 video devices using libdc1394-2.x api.
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
Class to define RGB colors available for display functionnalities.
Definition: vpColor.h:158
static const vpColor red
Definition: vpColor.h:217
static const vpColor black
Definition: vpColor.h:211
static const vpColor cyan
Definition: vpColor.h:226
static const vpColor orange
Definition: vpColor.h:227
static const vpColor blue
Definition: vpColor.h:223
static const vpColor green
Definition: vpColor.h:220
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:135
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
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:180
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:253
error that can be emited by ViSP classes.
Definition: vpException.h:72
const char * getMessage() const
Definition: vpException.cpp:90
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...
void buildFrom(double x, double y, double Z)
void set_Z(double Z)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
static double sqr(double x)
Definition: vpMath.h:116
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:116
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void get_eJe(vpMatrix &eJe)
@ ARTICULAR_FRAME
Definition: vpRobot.h:78
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition: vpRobot.h:66
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
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)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
@ EYEINHAND_L_cVe_eJe
Definition: vpServo.h:159
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:448
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
void setLambda(double c)
Definition: vpServo.h:404
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:506
vpColVector secondaryTask(const vpColVector &de2dt, const bool &useLargeProjectionOperator=false)
Definition: vpServo.cpp:1446
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
@ PSEUDO_INVERSE
Definition: vpServo.h:202
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
@ DESIRED
Definition: vpServo.h:186
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
vpVelocityTwistMatrix get_cVe() const
Definition: vpUnicycle.h:82