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