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