Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoViper850FourPoints2DCamVelocityLs_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 */
46
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#if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394))
56
57#include <visp3/blob/vpDot2.h>
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/vision/vpPose.h>
68#include <visp3/visual_features/vpFeatureBuilder.h>
69#include <visp3/visual_features/vpFeaturePoint.h>
70#include <visp3/vs/vpServo.h>
71#include <visp3/vs/vpServoDisplay.h>
72
73#define L 0.05 // to deal with a 10cm by 10cm square
74
75#ifdef ENABLE_VISP_NAMESPACE
76using namespace VISP_NAMESPACE_NAME;
77#endif
78
100void compute_pose(vpPoint point[], vpDot2 dot[], int ndot, vpCameraParameters cam, vpHomogeneousMatrix &cMo, bool init)
101{
103 vpPose pose;
104 vpImagePoint cog;
105 for (int i = 0; i < ndot; i++) {
106
107 double x = 0, y = 0;
108 cog = dot[i].getCog();
110 y); // pixel to meter conversion
111 point[i].set_x(x); // projection perspective p
112 point[i].set_y(y);
113 pose.addPoint(point[i]);
114 }
115
116 if (init == true) {
118 }
119 else { // init = false; use of the previous pose to initialise VIRTUAL_VS
121 }
122}
123
124int main()
125{
126 // Log file creation in /tmp/$USERNAME/log.dat
127 // This file contains by line:
128 // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
129 // - the 6 measured joint velocities (m/s, rad/s)
130 // - the 6 measured joint positions (m, rad)
131 // - the 8 values of s - s*
132 std::string username;
133 // Get the user login name
134 vpIoTools::getUserName(username);
135
136 // Create a log filename to save velocities...
137 std::string logdirname;
138 logdirname = "/tmp/" + username;
139
140 // Test if the output path exist. If no try to create it
141 if (vpIoTools::checkDirectory(logdirname) == false) {
142 try {
143 // Create the dirname
144 vpIoTools::makeDirectory(logdirname);
145 }
146 catch (...) {
147 std::cerr << std::endl << "ERROR:" << std::endl;
148 std::cerr << " Cannot create " << logdirname << std::endl;
149 return EXIT_FAILURE;
150 }
151 }
152 std::string logfilename;
153 logfilename = logdirname + "/log.dat";
154
155 // Open the log file name
156 std::ofstream flog(logfilename.c_str());
157
158#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
159 std::shared_ptr<vpDisplay> display;
160#else
161 vpDisplay *display = nullptr;
162#endif
163
164 try {
165 vpRobotViper850 robot;
166 // Load the end-effector to camera frame transformation obtained
167 // using a camera intrinsic model with distortion
169 robot.init(vpRobotViper850::TOOL_PTGREY_FLEA2_CAMERA, projModel);
170
172
174 int i;
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
189
192
193 std::cout << std::endl;
194 std::cout << "-------------------------------------------------------" << std::endl;
195 std::cout << " Test program for vpServo " << std::endl;
196 std::cout << " Eye-in-hand task control, velocity computed in the camera space" << std::endl;
197 std::cout << " Use of the Viper850 robot " << std::endl;
198 std::cout << " task : servo 4 points on a square with dimension " << L << " meters" << std::endl;
199 std::cout << "-------------------------------------------------------" << std::endl;
200 std::cout << std::endl;
201
202 vpDot2 dot[4];
203 vpImagePoint cog;
204
205 std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
206
207 for (i = 0; i < 4; i++) {
208 dot[i].setGraphics(true);
209 dot[i].initTracking(I);
210 cog = dot[i].getCog();
213 }
214
216
217 // Update camera parameters
218 robot.getCameraParameters(cam, I);
219
220 cam.printParameters();
221
222 // Sets the current position of the visual feature
223 vpFeaturePoint p[4];
224 for (i = 0; i < 4; i++)
225 vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
226
227 // Set the position of the square target in a frame which origin is
228 // centered in the middle of the square
229 vpPoint point[4];
230 point[0].setWorldCoordinates(-L, -L, 0);
231 point[1].setWorldCoordinates(L, -L, 0);
232 point[2].setWorldCoordinates(L, L, 0);
233 point[3].setWorldCoordinates(-L, L, 0);
234
235 // Initialise a desired pose to compute s*, the desired 2D point features
237 vpTranslationVector cto(0, 0, 0.5); // tz = 0.5 meter
239 vpRotationMatrix cRo(cro); // Build the rotation matrix
240 cMo.buildFrom(cto, cRo); // Build the homogeneous matrix
241
242 // Sets the desired position of the 2D visual feature
243 vpFeaturePoint pd[4];
244 // Compute the desired position of the features from the desired pose
245 for (int i = 0; i < 4; i++) {
246 vpColVector cP, p;
247 point[i].changeFrame(cMo, cP);
248 point[i].projection(cP, p);
249
250 pd[i].set_x(p[0]);
251 pd[i].set_y(p[1]);
252 pd[i].set_Z(cP[2]);
253 }
254
255 // We want to see a point on a point
256 for (i = 0; i < 4; i++)
257 task.addFeature(p[i], pd[i]);
258
259 // Set the proportional gain
260 task.setLambda(0.3);
261
262 // Display task information
263 task.print();
264
265 // Define the task
266 // - we want an eye-in-hand control law
267 // - articular velocity are computed
269 task.setInteractionMatrixType(vpServo::CURRENT, vpServo::PSEUDO_INVERSE);
270 task.print();
271
272 // Initialise the velocity control of the robot
273 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
274
275 bool init_pose_from_linear_method = true;
276 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
277 for (;;) {
278 // Acquire a new image from the camera
279 g.acquire(I);
280
281 // Display this image
283
284 try {
285 // For each point...
286 for (i = 0; i < 4; i++) {
287 // Achieve the tracking of the dot in the image
288 dot[i].track(I);
289 // Display a green cross at the center of gravity position in the
290 // image
291 cog = dot[i].getCog();
293 }
294 }
295 catch (...) {
296 flog.close(); // Close the log file
297 vpTRACE("Error detected while tracking visual features");
298 robot.stopMotion();
299#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
300 if (display != nullptr) {
301 delete display;
302 }
303#endif
304 return EXIT_FAILURE;
305 }
306
307 // At first iteration, we initialise non linear pose estimation with a linear approach.
308 // For the other iterations, non linear pose estimation is initialized with the pose estimated at previous
309 // iteration of the loop
310 compute_pose(point, dot, 4, cam, cMo, init_pose_from_linear_method);
311 if (init_pose_from_linear_method) {
312 init_pose_from_linear_method = false;
313 }
314
315 for (i = 0; i < 4; i++) {
316 // Update the point feature from the dot location
317 vpFeatureBuilder::create(p[i], cam, dot[i]);
318 // Set the feature Z coordinate from the pose
319 vpColVector cP;
320 point[i].changeFrame(cMo, cP);
321
322 p[i].set_Z(cP[2]);
323 }
324
326 // Compute the visual servoing skew vector
327 v = task.computeControlLaw();
328
329 // Display the current and desired feature points in the image display
330 vpServoDisplay::display(task, cam, I);
331
332 // Apply the computed joint velocities to the robot
333 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
334
335 // Save velocities applied to the robot in the log file
336 // v[0], v[1], v[2] correspond to joint translation velocities in m/s
337 // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
338 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
339
340 // Get the measured joint velocities of the robot
341 vpColVector qvel;
342 robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
343 // Save measured joint velocities of the robot in the log file:
344 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
345 // velocities in m/s
346 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
347 // velocities in rad/s
348 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
349
350 // Get the measured joint positions of the robot
351 vpColVector q;
352 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
353 // Save measured joint positions of the robot in the log file
354 // - q[0], q[1], q[2] correspond to measured joint translation
355 // positions in m
356 // - q[3], q[4], q[5] correspond to measured joint rotation
357 // positions in rad
358 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
359
360 // Save feature error (s-s*) for the 4 feature points. For each feature
361 // point, we have 2 errors (along x and y axis). This error is
362 // expressed in meters in the camera frame
363 flog << task.getError() << std::endl;
364
365 // Flush the display
367
368 // std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<
369 // std::endl;
370 }
371
372 std::cout << "Display task information: " << std::endl;
373 task.print();
374 flog.close(); // Close the log file
375#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
376 if (display != nullptr) {
377 delete display;
378 }
379#endif
380 return EXIT_SUCCESS;
381 }
382 catch (const vpException &e) {
383 flog.close(); // Close the log file
384 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
385#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
386 if (display != nullptr) {
387 delete display;
388 }
389#endif
390 return EXIT_FAILURE;
391 }
392}
393
394#else
395int main()
396{
397 std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
398 return EXIT_SUCCESS;
399}
400#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 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
void setGraphics(bool activate)
Definition vpDot2.h:320
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...
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 double rad(double deg)
Definition vpMath.h:129
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
void set_x(double x)
Set the point x coordinate in the image plane.
Definition vpPoint.cpp:471
void projection(const vpColVector &_cP, vpColVector &_p) const VP_OVERRIDE
Definition vpPoint.cpp:252
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const VP_OVERRIDE
Definition vpPoint.cpp:273
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:116
void set_y(double y)
Set the point y coordinate in the image plane.
Definition vpPoint.cpp:473
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 S850 robot named Viper850.
@ 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.
Implementation of a rotation vector as Euler angle minimal representation.
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 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.