Visual Servoing Platform version 3.5.0
servoViper850Point2DArtVelocity-jointAvoidance-large.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 * Giovanni Claudio
40 *
41 *****************************************************************************/
42
51#include <visp3/core/vpConfig.h>
52#include <visp3/core/vpDebug.h> // Debug trace
53
54#include <fstream>
55#include <iostream>
56#include <sstream>
57#include <stdio.h>
58#include <stdlib.h>
59
60#if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394_2) && defined(VISP_HAVE_DISPLAY))
61
62#include <visp3/blob/vpDot2.h>
63#include <visp3/core/vpDisplay.h>
64#include <visp3/core/vpException.h>
65#include <visp3/core/vpHomogeneousMatrix.h>
66#include <visp3/core/vpImage.h>
67#include <visp3/core/vpIoTools.h>
68#include <visp3/core/vpMath.h>
69#include <visp3/core/vpPoint.h>
70#include <visp3/gui/vpDisplayGTK.h>
71#include <visp3/gui/vpDisplayOpenCV.h>
72#include <visp3/gui/vpDisplayX.h>
73#include <visp3/gui/vpPlot.h>
74#include <visp3/robot/vpRobotViper850.h>
75#include <visp3/sensor/vp1394TwoGrabber.h>
76#include <visp3/visual_features/vpFeatureBuilder.h>
77#include <visp3/visual_features/vpFeaturePoint.h>
78#include <visp3/vs/vpServo.h>
79#include <visp3/vs/vpServoDisplay.h>
80
81int main()
82{
83 try {
84 vpRobotViper850 robot;
85
86 vpServo task;
87
89
90 bool reset = false;
91 vp1394TwoGrabber g(reset);
94 g.open(I);
95
96 g.acquire(I);
97
98#ifdef VISP_HAVE_X11
99 vpDisplayX display(I, 800, 100, "Current image");
100#elif defined(VISP_HAVE_OPENCV)
101 vpDisplayOpenCV display(I, 800, 100, "Current image");
102#elif defined(VISP_HAVE_GTK)
103 vpDisplayGTK display(I, 800, 100, "Current image");
104#endif
105
108
109 vpColVector jointMin(6), jointMax(6);
110 jointMin = robot.getJointMin();
111 jointMax = robot.getJointMax();
112
113 vpColVector Qmiddle(6);
114 vpColVector data(12);
115
116 Qmiddle = (jointMin + jointMax) / 2.;
117 // double rho1 = 0.1 ;
118
119 double rho = 0.1;
120 double rho1 = 0.3;
121
122 vpColVector q(6);
123
124 // Create a window with two graphics
125 // - first graphic to plot q(t), Qmin, Qmax, Ql0min, Ql1min, Ql0max and
126 // Ql1max
127 vpPlot plot(2);
128
129 // The first graphic contains 12 data to plot: q(t), Low Limits, Upper
130 // Limits, ql0min, ql1min, ql0max and ql1max
131 plot.initGraph(0, 12);
132 // The second graphic contains the values of the secondaty task velocities
133 plot.initGraph(1, 6);
134
135 // For the first graphic :
136 // - along the x axis the expected values are between 0 and 200
137 // - along the y axis the expected values are between -1.2 and 1.2
138 plot.initRange(0, 0., 200., -1.2, 1.2);
139 plot.setTitle(0, "Joint behavior");
140
141 // For the second graphic :
142 plot.setTitle(1, "Q secondary task");
143
144 // For the first and second graphic, set the curves legend
145 char legend[10];
146 for (unsigned int i = 0; i < 6; i++) {
147 sprintf(legend, "q%u", i + 1);
148 plot.setLegend(0, i, legend);
149 plot.setLegend(1, i, legend);
150 }
151 plot.setLegend(0, 6, "Low Limit");
152 plot.setLegend(0, 7, "Upper Limit");
153 plot.setLegend(0, 8, "ql0 min");
154 plot.setLegend(0, 9, "ql0 max");
155 plot.setLegend(0, 10, "ql1 min");
156 plot.setLegend(0, 11, "ql1 max");
157
158 // Set the curves color
159 plot.setColor(0, 0, vpColor::red);
160 plot.setColor(0, 1, vpColor::green);
161 plot.setColor(0, 2, vpColor::blue);
162 plot.setColor(0, 3, vpColor::orange);
163 plot.setColor(0, 4, vpColor(0, 128, 0));
164 plot.setColor(0, 5, vpColor::cyan);
165 for (unsigned int i = 6; i < 12; i++)
166 plot.setColor(0, i, vpColor::black); // for Q and tQ [min,max]
167
168 vpColVector sec_task(6);
169
170 vpDot2 dot;
171
172 std::cout << "Click on a dot..." << std::endl;
173 dot.initTracking(I);
174 vpImagePoint cog = dot.getCog();
177
179 // Update camera parameters
180 robot.getCameraParameters(cam, I);
181
182 // sets the current position of the visual feature
184 vpFeatureBuilder::create(p, cam, dot); // retrieve x,y and Z of the vpPoint structure
185
186 p.set_Z(1);
187 // sets the desired position of the visual feature
189 pd.buildFrom(0, 0, 1);
190
191 // Define the task
192 // - we want an eye-in-hand control law
193 // - articular velocity are computed
196
198 robot.get_cVe(cVe);
199 std::cout << cVe << std::endl;
200 task.set_cVe(cVe);
201
202 // - Set the Jacobian (expressed in the end-effector frame)") ;
203 vpMatrix eJe;
204 robot.get_eJe(eJe);
205 task.set_eJe(eJe);
206
207 // - we want to see a point on a point..") ;
208 std::cout << std::endl;
209 task.addFeature(p, pd);
210
211 // - set the gain
212 task.setLambda(0.8);
213
214 // Display task information " ) ;
215 task.print();
216
218
219 int iter = 0;
220 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
221 for (;;) {
222 iter++;
223 // Acquire a new image from the camera
224 g.acquire(I);
225
226 // Display this image
228
229 // Achieve the tracking of the dot in the image
230 dot.track(I);
231 cog = dot.getCog();
232
233 // Display a green cross at the center of gravity position in the image
235
236 // Get the measured joint positions of the robot
237 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
238
239 // Update the point feature from the dot location
240 vpFeatureBuilder::create(p, cam, dot);
241
242 // Get the jacobian of the robot
243 robot.get_eJe(eJe);
244 // Update this jacobian in the task structure. It will be used to
245 // compute the velocity skew (as an articular velocity) qdot = -lambda *
246 // L^+ * cVe * eJe * (s-s*)
247 task.set_eJe(eJe);
248
249 vpColVector prim_task;
250 // Compute the visual servoing skew vector
251 prim_task = task.computeControlLaw();
252
253 // Compute the secondary task for the joint limit avoidance
254 sec_task = task.secondaryTaskJointLimitAvoidance(q, prim_task, jointMin, jointMax, rho, rho1);
255
256 vpColVector v;
257 v = prim_task + sec_task;
258
259 // Display the current and desired feature points in the image display
260 vpServoDisplay::display(task, cam, I);
261
262 // Apply the computed joint velocities to the robot
264
265 {
266 // Add the material to plot curves
267
268 // q normalized between (entre -1 et 1)
269 for (unsigned int i = 0; i < 6; i++) {
270 data[i] = (q[i] - Qmiddle[i]);
271 data[i] /= (jointMax[i] - jointMin[i]);
272 data[i] *= 2;
273 }
274
275 data[6] = -1.0;
276 data[7] = 1.0;
277
278 unsigned int joint = 2;
279 double tQmin_l0 = jointMin[joint] + rho * (jointMax[joint] - jointMin[joint]);
280 double tQmax_l0 = jointMax[joint] - rho * (jointMax[joint] - jointMin[joint]);
281
282 double tQmin_l1 = tQmin_l0 - rho * rho1 * (jointMax[joint] - jointMin[joint]);
283 double tQmax_l1 = tQmax_l0 + rho * rho1 * (jointMax[joint] - jointMin[joint]);
284
285 data[8] = 2 * (tQmin_l0 - Qmiddle[joint]) / (jointMax[joint] - jointMin[joint]);
286 data[9] = 2 * (tQmax_l0 - Qmiddle[joint]) / (jointMax[joint] - jointMin[joint]);
287 data[10] = 2 * (tQmin_l1 - Qmiddle[joint]) / (jointMax[joint] - jointMin[joint]);
288 data[11] = 2 * (tQmax_l1 - Qmiddle[joint]) / (jointMax[joint] - jointMin[joint]);
289 plot.plot(0, iter, data); // plot q(t), Low Limits, Upper Limits,
290 // ql0min, ql1min, ql0max and ql1max
291 plot.plot(1, iter, sec_task); // plot secondary task velocities
292 }
293
295 }
296
297 // Display task information
298 task.print();
299 return EXIT_SUCCESS;
300 }
301 catch (const vpException &e) {
302 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
303 return EXIT_FAILURE;
304 }
305}
306
307#else
308int main()
309{
310 std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
311 return EXIT_SUCCESS;
312}
313#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
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
vpColVector secondaryTaskJointLimitAvoidance(const vpColVector &q, const vpColVector &dq, const vpColVector &jointMin, const vpColVector &jointMax, const double &rho=0.1, const double &rho1=0.3, const double &lambda_tune=0.7)
Definition: vpServo.cpp:1630
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
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