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