Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoViper850FourPoints2DArtVelocityLs_des.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 the articular frame
34 */
45
46#include <visp3/core/vpConfig.h>
47#include <visp3/core/vpDebug.h> // Debug trace
48
49#include <fstream>
50#include <iostream>
51#include <sstream>
52#include <stdio.h>
53#include <stdlib.h>
54#if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394))
55
56#include <visp3/blob/vpDot2.h>
57#include <visp3/core/vpDisplay.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/robot/vpRobotViper850.h>
65#include <visp3/sensor/vp1394TwoGrabber.h>
66#include <visp3/vision/vpPose.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 // Log file creation in /tmp/$USERNAME/log.dat
79 // This file contains by line:
80 // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
81 // - the 6 measured joint velocities (m/s, rad/s)
82 // - the 6 measured joint positions (m, rad)
83 // - the 8 values of s - s*
84 std::string username;
85 // Get the user login name
86 vpIoTools::getUserName(username);
87
88 // Create a log filename to save velocities...
89 std::string logdirname;
90 logdirname = "/tmp/" + username;
91
92 // Test if the output path exist. If no try to create it
93 if (vpIoTools::checkDirectory(logdirname) == false) {
94 try {
95 // Create the dirname
96 vpIoTools::makeDirectory(logdirname);
97 }
98 catch (...) {
99 std::cerr << std::endl << "ERROR:" << std::endl;
100 std::cerr << " Cannot create " << logdirname << std::endl;
101 return EXIT_FAILURE;
102 }
103 }
104 std::string logfilename;
105 logfilename = logdirname + "/log.dat";
106
107 // Open the log file name
108 std::ofstream flog(logfilename.c_str());
109
110#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
111 std::shared_ptr<vpDisplay> display;
112#else
113 vpDisplay *display = nullptr;
114#endif
115
116 try {
117// Define the square CAD model
118// Square dimension
119// #define L 0.075
120#define L 0.05
121// Distance between the camera and the square at the desired
122// position after visual servoing convergence
123#define D 0.5
124
125 vpRobotViper850 robot;
126 // Load the end-effector to camera frame transformation obtained
127 // using a camera intrinsic model with distortion
129 robot.init(vpRobotViper850::TOOL_PTGREY_FLEA2_CAMERA, projModel);
130
132
134 int i;
135
136 bool reset = false;
137 vp1394TwoGrabber g(reset);
139 g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
140 g.open(I);
141
142 g.acquire(I);
143
144#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
145 display = vpDisplayFactory::createDisplay(I, 100, 100, "Current image");
146#else
147 display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Current image");
148#endif
149
152
153 std::cout << std::endl;
154 std::cout << "-------------------------------------------------------" << std::endl;
155 std::cout << " Test program for vpServo " << std::endl;
156 std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl;
157 std::cout << " Use of the Afma6 robot " << std::endl;
158 std::cout << " task : servo 4 points on a square with dimension " << L << " meters" << std::endl;
159 std::cout << "-------------------------------------------------------" << std::endl;
160 std::cout << std::endl;
161
162 vpDot dot[4];
163 vpImagePoint cog;
164
165 std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
166
167 for (i = 0; i < 4; i++) {
168 dot[i].setGraphics(true);
169 dot[i].initTracking(I);
170 cog = dot[i].getCog();
173 }
174
176
177 // Update camera parameters
178 robot.getCameraParameters(cam, I);
179
180 cam.printParameters();
181
182 // Sets the current position of the visual feature
183 vpFeaturePoint p[4];
184 for (i = 0; i < 4; i++)
185 vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y and Z of the vpPoint structure
186
187 // sets the desired position of the visual feature
188 vpFeaturePoint pd[4];
189
190 pd[0].buildFrom(-L, -L, D);
191 pd[1].buildFrom(L, -L, D);
192 pd[2].buildFrom(L, L, D);
193 pd[3].buildFrom(-L, L, D);
194
195 // We want to see a point on a point
196 std::cout << std::endl;
197 for (i = 0; i < 4; i++)
198 task.addFeature(p[i], pd[i]);
199
200 // Set the proportional gain
201 task.setLambda(0.4);
202
203 // Display task information
204 task.print();
205
206 // Define the task
207 // - we want an eye-in-hand control law
208 // - articular velocity are computed
210 task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
211 task.print();
212
214 robot.get_cVe(cVe);
215 task.set_cVe(cVe);
216 task.print();
217
218 // Set the Jacobian (expressed in the end-effector frame)
219 vpMatrix eJe;
220 robot.get_eJe(eJe);
221 task.set_eJe(eJe);
222 task.print();
223
224 // Initialise the velocity control of the robot
225 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
226
227 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
228 for (;;) {
229 // Acquire a new image from the camera
230 g.acquire(I);
231
232 // Display this image
234
235 try {
236 // For each point...
237 for (i = 0; i < 4; i++) {
238 // Achieve the tracking of the dot in the image
239 dot[i].track(I);
240 // Display a green cross at the center of gravity position in the
241 // image
242 cog = dot[i].getCog();
244 }
245 }
246 catch (...) {
247 flog.close(); // Close the log file
248 vpTRACE("Error detected while tracking visual features");
249 robot.stopMotion();
250#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
251 if (display != nullptr) {
252 delete display;
253 }
254#endif
255 exit(1);
256 }
257
258 // Update the point feature from the dot location
259 for (i = 0; i < 4; i++)
260 vpFeatureBuilder::create(p[i], cam, dot[i]);
261
262 // Get the jacobian of the robot
263 robot.get_eJe(eJe);
264 // Update this jacobian in the task structure. It will be used to
265 // compute the velocity skew (as an articular velocity) qdot = -lambda *
266 // L^+ * cVe * eJe * (s-s*)
267 task.set_eJe(eJe);
268
270 // Compute the visual servoing skew vector
271 v = task.computeControlLaw();
272
273 // Display the current and desired feature points in the image display
274 vpServoDisplay::display(task, cam, I);
275
276 // Apply the computed joint velocities to the robot
277 robot.setVelocity(vpRobot::ARTICULAR_FRAME, v);
278
279 // Save velocities applied to the robot in the log file
280 // v[0], v[1], v[2] correspond to joint translation velocities in m/s
281 // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
282 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
283
284 // Get the measured joint velocities of the robot
285 vpColVector qvel;
286 robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
287 // Save measured joint velocities of the robot in the log file:
288 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
289 // velocities in m/s
290 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
291 // velocities in rad/s
292 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
293
294 // Get the measured joint positions of the robot
295 vpColVector q;
296 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
297 // Save measured joint positions of the robot in the log file
298 // - q[0], q[1], q[2] correspond to measured joint translation
299 // positions in m
300 // - q[3], q[4], q[5] correspond to measured joint rotation
301 // positions in rad
302 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
303
304 // Save feature error (s-s*) for the 4 feature points. For each feature
305 // point, we have 2 errors (along x and y axis). This error is
306 // expressed in meters in the camera frame
307 flog << (task.getError()).t() << std::endl;
308
309 // Flush the display
311
312 // std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<
313 // std::endl;
314 }
315
316 std::cout << "Display task information: " << std::endl;
317 task.print();
318 flog.close(); // Close the log file
319#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
320 if (display != nullptr) {
321 delete display;
322 }
323#endif
324 return EXIT_SUCCESS;
325 }
326 catch (const vpException &e) {
327 flog.close(); // Close the log file
328 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
329#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
330 if (display != nullptr) {
331 delete display;
332 }
333#endif
334 return EXIT_FAILURE;
335 }
336}
337
338#else
339int main()
340{
341 std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
342 return EXIT_SUCCESS;
343}
344#endif
Class for firewire ieee1394 video devices using libdc1394-2.x api.
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
Implementation of column vector and the associated operations.
vpRowVector t() const
static const vpColor blue
Definition vpColor.h:204
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 dot (connected pixels with same gray level) on a vpImage.
Definition vpDot.h:123
void initTracking(const vpImage< unsigned char > &I)
Definition vpDot.cpp:630
void setGraphics(bool activate)
Definition vpDot.h:362
vpImagePoint getCog() const
Definition vpDot.h:255
void track(const vpImage< unsigned char > &I)
Definition vpDot.cpp:760
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
static bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static void makeDirectory(const std::string &dirname)
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
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
@ TOOL_PTGREY_FLEA2_CAMERA
Definition vpViper850.h:122
#define vpTRACE
Definition vpDebug.h:450
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.