Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoViper650FourPoints2DArtVelocityLs_cur.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 */
50
51#include <fstream>
52#include <iostream>
53#include <sstream>
54#include <stdio.h>
55#include <stdlib.h>
56
57#include <visp3/core/vpConfig.h>
58
59#if defined(VISP_HAVE_VIPER650) && defined(VISP_HAVE_DC1394) && defined(VISP_HAVE_DISPLAY)
60
61#include <visp3/blob/vpDot2.h>
62#include <visp3/core/vpHomogeneousMatrix.h>
63#include <visp3/core/vpIoTools.h>
64#include <visp3/core/vpPoint.h>
65#include <visp3/gui/vpDisplayFactory.h>
66#include <visp3/robot/vpRobotViper650.h>
67#include <visp3/sensor/vp1394TwoGrabber.h>
68#include <visp3/vision/vpPose.h>
69#include <visp3/visual_features/vpFeatureBuilder.h>
70#include <visp3/visual_features/vpFeaturePoint.h>
71#include <visp3/vs/vpServo.h>
72#include <visp3/vs/vpServoDisplay.h>
73
74#define L 0.05 // to deal with a 10cm by 10cm square
75
76#ifdef ENABLE_VISP_NAMESPACE
77using namespace VISP_NAMESPACE_NAME;
78#endif
79
98void compute_pose(std::vector<vpPoint> &point, std::vector<vpDot2> &dot, vpCameraParameters cam,
99 vpHomogeneousMatrix &cMo, bool init)
100{
101 vpPose pose;
102
103 for (size_t i = 0; i < point.size(); i++) {
104
105 double x = 0, y = 0;
106 vpImagePoint cog = dot[i].getCog();
108 y); // pixel to meter conversion
109 point[i].set_x(x); // projection perspective p
110 point[i].set_y(y);
111 pose.addPoint(point[i]);
112 }
113
114 if (init == true) {
116 }
117 else {
119 }
120}
121
122int main()
123{
124 // Log file creation in /tmp/$USERNAME/log.dat
125 // This file contains by line:
126 // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
127 // - the 6 measured joint velocities (m/s, rad/s)
128 // - the 6 measured joint positions (m, rad)
129 // - the 8 values of s - s*
130 std::string username;
131 // Get the user login name
132 vpIoTools::getUserName(username);
133
134 // Create a log filename to save velocities...
135 std::string logdirname;
136 logdirname = "/tmp/" + username;
137
138 // Test if the output path exist. If no try to create it
139 if (vpIoTools::checkDirectory(logdirname) == false) {
140 try {
141 // Create the dirname
142 vpIoTools::makeDirectory(logdirname);
143 }
144 catch (...) {
145 std::cerr << std::endl << "ERROR:" << std::endl;
146 std::cerr << " Cannot create " << logdirname << std::endl;
147 return EXIT_FAILURE;
148 }
149 }
150 std::string logfilename;
151 logfilename = logdirname + "/log.dat";
152
153 // Open the log file name
154 std::ofstream flog(logfilename.c_str());
155
156#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
157 std::shared_ptr<vpDisplay> display;
158#else
159 vpDisplay *display = nullptr;
160#endif
161 try {
162 vpRobotViper650 robot;
163 // Load the end-effector to camera frame transformation obtained
164 // using a camera intrinsic model with distortion
166 robot.init(vpRobotViper650::TOOL_PTGREY_FLEA2_CAMERA, projModel);
168 robot.get_eMc(eMc);
169 std::cout << "Camera extrinsic parameters (eMc): \n" << eMc << std::endl;
170
172
174
175 bool reset = false;
176 vp1394TwoGrabber g(reset);
178 g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
179 g.open(I);
180
181 g.acquire(I);
182
183#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
184 display = vpDisplayFactory::createDisplay(I, 100, 100, "Current image");
185#else
186 display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Current image");
187#endif
190
191 std::vector<vpDot2> dot(4);
192
193 vpImagePoint cog;
194
195 std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
196
197 for (size_t i = 0; i < dot.size(); i++) {
198 dot[i].setGraphics(true);
199 dot[i].initTracking(I);
200 vpImagePoint cog = dot[i].getCog();
203 }
204
206
207 // Update camera parameters
208 robot.getCameraParameters(cam, I);
209 std::cout << "Camera intrinsic parameters: \n" << cam << std::endl;
210
211 // Sets the current position of the visual feature
212 vpFeaturePoint p[4];
213 for (size_t i = 0; i < dot.size(); i++)
214 vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
215
216 // Set the position of the square target in a frame which origin is
217 // centered in the middle of the square
218 std::vector<vpPoint> point(4);
219 point[0].setWorldCoordinates(-L, -L, 0);
220 point[1].setWorldCoordinates(L, -L, 0);
221 point[2].setWorldCoordinates(L, L, 0);
222 point[3].setWorldCoordinates(-L, L, 0);
223
224 // Compute target initial pose
226 compute_pose(point, dot, cam, cMo, true);
227 std::cout << "Initial camera pose (cMo): \n" << cMo << std::endl;
228
229 // Initialise a desired pose to compute s*, the desired 2D point features
230 vpHomogeneousMatrix cMo_d(vpTranslationVector(0, 0, 0.5), // tz = 0.5 meter
231 vpRotationMatrix()); // no rotation
232
233 // Sets the desired position of the 2D visual feature
234 vpFeaturePoint pd[4];
235 // Compute the desired position of the features from the desired pose
236 for (int i = 0; i < 4; i++) {
237 vpColVector cP, p;
238 point[i].changeFrame(cMo_d, cP);
239 point[i].projection(cP, p);
240
241 pd[i].set_x(p[0]);
242 pd[i].set_y(p[1]);
243 pd[i].set_Z(cP[2]);
244 }
245
246 // We want to see a point on a point
247 for (size_t i = 0; i < dot.size(); i++)
248 task.addFeature(p[i], pd[i]);
249
250 // Set the proportional gain
251 task.setLambda(0.3);
252
253 // Define the task
254 // - we want an eye-in-hand control law
255 // - articular velocity are computed
257 task.setInteractionMatrixType(vpServo::CURRENT, vpServo::PSEUDO_INVERSE);
258
260 robot.get_cVe(cVe);
261 task.set_cVe(cVe);
262
263 // Set the Jacobian (expressed in the end-effector frame)
264 vpMatrix eJe;
265 robot.get_eJe(eJe);
266 task.set_eJe(eJe);
267 task.print();
268
269 // Initialise the velocity control of the robot
270 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
271
272 std::cout << "\nHit CTRL-C or click in the image to stop the loop...\n" << std::flush;
273 for (;;) {
274 // Acquire a new image from the camera
275 g.acquire(I);
276
277 // Display this image
279
280 try {
281 // For each point...
282 for (size_t i = 0; i < dot.size(); i++) {
283 // Achieve the tracking of the dot in the image
284 dot[i].track(I);
285 // Display a green cross at the center of gravity position in the
286 // image
287 vpImagePoint cog = dot[i].getCog();
289 }
290 }
291 catch (...) {
292 std::cout << "Error detected while tracking visual features.." << std::endl;
293 break;
294 }
295
296 // During the servo, we compute the pose using LOWE method. For the
297 // initial pose used in the non linear minimization we use the pose
298 // computed at the previous iteration.
299 compute_pose(point, dot, cam, cMo, false);
300
301 for (size_t i = 0; i < dot.size(); i++) {
302 // Update the point feature from the dot location
303 vpFeatureBuilder::create(p[i], cam, dot[i]);
304 // Set the feature Z coordinate from the pose
305 vpColVector cP;
306 point[i].changeFrame(cMo, cP);
307
308 p[i].set_Z(cP[2]);
309 }
310
311 // Get the jacobian of the robot
312 robot.get_eJe(eJe);
313 // Update this jacobian in the task structure. It will be used to
314 // compute the velocity skew (as an articular velocity) qdot = -lambda *
315 // L^+ * cVe * eJe * (s-s*)
316 task.set_eJe(eJe);
317
318 // Compute the visual servoing skew vector
319 vpColVector v = task.computeControlLaw();
320
321 // Display the current and desired feature points in the image display
322 vpServoDisplay::display(task, cam, I);
323
324 // Apply the computed joint velocities to the robot
325 robot.setVelocity(vpRobot::ARTICULAR_FRAME, v);
326
327 // Save velocities applied to the robot in the log file
328 // v[0], v[1], v[2] correspond to joint translation velocities in m/s
329 // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
330 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
331
332 // Get the measured joint velocities of the robot
333 vpColVector qvel;
334 robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
335 // Save measured joint velocities of the robot in the log file:
336 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
337 // velocities in m/s
338 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
339 // velocities in rad/s
340 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
341
342 // Get the measured joint positions of the robot
343 vpColVector q;
344 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
345 // Save measured joint positions of the robot in the log file
346 // - q[0], q[1], q[2] correspond to measured joint translation
347 // positions in m
348 // - q[3], q[4], q[5] correspond to measured joint rotation
349 // positions in rad
350 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
351
352 // Save feature error (s-s*) for the 4 feature points. For each feature
353 // point, we have 2 errors (along x and y axis). This error is
354 // expressed in meters in the camera frame
355 flog << (task.getError()).t() << std::endl;
356
357 vpDisplay::displayText(I, 10, 10, "Click to quit...", vpColor::red);
358 if (vpDisplay::getClick(I, false))
359 break;
360
361 // Flush the display
363
364 // std::cout << "\t\t || s - s* || = " << ( task.getError()
365 // ).sumSquare() << std::endl;
366 }
367
368 std::cout << "Display task information: " << std::endl;
369 task.print();
370 flog.close(); // Close the log file
371#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
372 if (display != nullptr) {
373 delete display;
374 }
375#endif
376 return EXIT_SUCCESS;
377 }
378 catch (const vpException &e) {
379 flog.close(); // Close the log file
380 std::cout << "Catched an exception: " << e.getMessage() << std::endl;
381#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
382 if (display != nullptr) {
383 delete display;
384 }
385#endif
386 return EXIT_FAILURE;
387 }
388}
389
390#else
391int main()
392{
393 std::cout << "You do not have an Viper 650 robot connected to your computer..." << std::endl;
394 return EXIT_SUCCESS;
395}
396#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 red
Definition vpColor.h:198
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 bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
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...
void set_y(double y)
void set_x(double x)
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 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
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition vpPose.h:82
void addPoint(const vpPoint &P)
Definition vpPose.cpp:96
@ DEMENTHON_LAGRANGE_VIRTUAL_VS
Definition vpPose.h:103
@ VIRTUAL_VS
Definition vpPose.h:97
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, FuncCheckValidityPose func=nullptr)
Definition vpPose.cpp:385
Control of Irisa's Viper S650 robot named Viper650.
@ ARTICULAR_FRAME
Definition vpRobot.h:77
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
Implementation of a rotation matrix and operations on such kind of matrices.
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
@ CURRENT
Definition vpServo.h:217
Class that consider the case of a translation vector.
@ TOOL_PTGREY_FLEA2_CAMERA
Definition vpViper650.h:122
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.