Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoViper850Point2DArtVelocity.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 <fstream>
49#include <iostream>
50#include <sstream>
51#include <stdio.h>
52#include <stdlib.h>
53
54#if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394))
55
56#include <visp3/core/vpDisplay.h>
57#include <visp3/core/vpHomogeneousMatrix.h>
58#include <visp3/core/vpImage.h>
59#include <visp3/core/vpIoTools.h>
60#include <visp3/core/vpMath.h>
61#include <visp3/core/vpPoint.h>
62#include <visp3/gui/vpDisplayFactory.h>
63#include <visp3/robot/vpRobotViper850.h>
64#include <visp3/sensor/vp1394TwoGrabber.h>
65#include <visp3/visual_features/vpFeatureBuilder.h>
66#include <visp3/visual_features/vpFeaturePoint.h>
67#include <visp3/vs/vpServo.h>
68
69// Exception
70#include <visp3/core/vpException.h>
71#include <visp3/vs/vpServoDisplay.h>
72
73#include <visp3/blob/vpDot2.h>
74
75int main()
76{
77#ifdef ENABLE_VISP_NAMESPACE
78 using namespace VISP_NAMESPACE_NAME;
79#endif
80
81 // Log file creation in /tmp/$USERNAME/log.dat
82 // This file contains by line:
83 // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
84 // - the 6 measured joint velocities (m/s, rad/s)
85 // - the 6 measured joint positions (m, rad)
86 // - the 2 values of s - s*
87 std::string username;
88 // Get the user login name
89 vpIoTools::getUserName(username);
90
91 // Create a log filename to save velocities...
92 std::string logdirname;
93 logdirname = "/tmp/" + username;
94
95 // Test if the output path exist. If no try to create it
96 if (vpIoTools::checkDirectory(logdirname) == false) {
97 try {
98 // Create the dirname
99 vpIoTools::makeDirectory(logdirname);
100 }
101 catch (...) {
102 std::cerr << std::endl << "ERROR:" << std::endl;
103 std::cerr << " Cannot create " << logdirname << std::endl;
104 return EXIT_FAILURE;
105 }
106 }
107 std::string logfilename;
108 logfilename = logdirname + "/log.dat";
109
110 // Open the log file name
111 std::ofstream flog(logfilename.c_str());
112
113#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
114 std::shared_ptr<vpDisplay> display;
115#else
116 vpDisplay *display = nullptr;
117#endif
118
119 try {
120 vpRobotViper850 robot;
121
123
125
126 bool reset = false;
127 vp1394TwoGrabber g(reset);
129 g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
130 g.open(I);
131
132 g.acquire(I);
133
134#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
135 display = vpDisplayFactory::createDisplay(I, 800, 100, "Current image");
136#else
137 display = vpDisplayFactory::allocateDisplay(I, 800, 100, "Current image");
138#endif
139
142 // exit(1) ;
143
144 std::cout << std::endl;
145 std::cout << "-------------------------------------------------------" << std::endl;
146 std::cout << " Test program for vpServo " << std::endl;
147 std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl;
148 std::cout << " Use of the Afma6 robot " << std::endl;
149 std::cout << " task : servo a point " << std::endl;
150 std::cout << "-------------------------------------------------------" << std::endl;
151 std::cout << std::endl;
152
153 vpDot2 dot;
154
155 std::cout << "Click on a dot..." << std::endl;
156 dot.initTracking(I);
157 vpImagePoint cog = dot.getCog();
160
162 // Update camera parameters
163 robot.getCameraParameters(cam, I);
164
165 vpTRACE("sets the current position of the visual feature ");
167 vpFeatureBuilder::create(p, cam, dot); // retrieve x,y and Z of the vpPoint structure
168
169 p.set_Z(1);
170 vpTRACE("sets the desired position of the visual feature ");
172 pd.buildFrom(0, 0, 1);
173
174 vpTRACE("define the task");
175 vpTRACE("\t we want an eye-in-hand control law");
176 vpTRACE("\t articular velocity are computed");
178 task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
179
180 vpTRACE("Set the position of the end-effector frame in the camera frame");
182 // robot.get_cMe(cMe) ;
183
185 robot.get_cVe(cVe);
186 std::cout << cVe << std::endl;
187 task.set_cVe(cVe);
188
189 // vpDisplay::getClick(I) ;
190 vpTRACE("Set the Jacobian (expressed in the end-effector frame)");
191 vpMatrix eJe;
192 robot.get_eJe(eJe);
193 task.set_eJe(eJe);
194
195 vpTRACE("\t we want to see a point on a point..");
196 std::cout << std::endl;
197 task.addFeature(p, pd);
198
199 vpTRACE("\t set the gain");
200 task.setLambda(0.8);
201
202 vpTRACE("Display task information ");
203 task.print();
204
205 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
206
207 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
208 for (;;) {
209 // Acquire a new image from the camera
210 g.acquire(I);
211
212 // Display this image
214
215 // Achieve the tracking of the dot in the image
216 dot.track(I);
217 cog = dot.getCog();
218
219 // Display a green cross at the center of gravity position in the image
221
222 // Update the point feature from the dot location
223 vpFeatureBuilder::create(p, cam, dot);
224
225 // Get the jacobian of the robot
226 robot.get_eJe(eJe);
227 // Update this jacobian in the task structure. It will be used to
228 // compute the velocity skew (as an articular velocity) qdot = -lambda *
229 // L^+ * cVe * eJe * (s-s*)
230 task.set_eJe(eJe);
231
232 // std::cout << (vpMatrix)cVe*eJe << std::endl ;
233
235 // Compute the visual servoing skew vector
236 v = task.computeControlLaw();
237
238 // Display the current and desired feature points in the image display
239 vpServoDisplay::display(task, cam, I);
240
241 // Apply the computed joint velocities to the robot
242 robot.setVelocity(vpRobot::ARTICULAR_FRAME, v);
243
244 // Save velocities applied to the robot in the log file
245 // v[0], v[1], v[2] correspond to joint translation velocities in m/s
246 // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
247 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
248
249 // Get the measured joint velocities of the robot
250 vpColVector qvel;
251 robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
252 // Save measured joint velocities of the robot in the log file:
253 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
254 // velocities in m/s
255 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
256 // velocities in rad/s
257 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
258
259 // Get the measured joint positions of the robot
260 vpColVector q;
261 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
262 // Save measured joint positions of the robot in the log file
263 // - q[0], q[1], q[2] correspond to measured joint translation
264 // positions in m
265 // - q[3], q[4], q[5] correspond to measured joint rotation
266 // positions in rad
267 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
268
269 // Save feature error (s-s*) for the feature point. For this feature
270 // point, we have 2 errors (along x and y axis). This error is
271 // expressed in meters in the camera frame
272 flog << (task.getError()).t() << std::endl; // s-s* for point
273
275
276 // std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<
277 // std::endl;
278 }
279
280 flog.close(); // Close the log file
281
282 std::cout << "Display task information: " << std::endl;
283 task.print();
284#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
285 if (display != nullptr) {
286 delete display;
287 }
288#endif
289 return EXIT_SUCCESS;
290 }
291 catch (const vpException &e) {
292 flog.close(); // Close the log file
293 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
294#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
295 if (display != nullptr) {
296 delete display;
297 }
298#endif
299 return EXIT_FAILURE;
300 }
301}
302
303#else
304int main()
305{
306 std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
307 return EXIT_SUCCESS;
308}
309#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.
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 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)
Implementation of an homogeneous matrix and operations on such kind of matrices.
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
#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.