Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoViper850FourPoints2DArtVelocityLs_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 */
45
46#include <visp3/core/vpConfig.h>
47#include <visp3/core/vpDebug.h> // Debug trace
48
49#include <fstream>
50#include <iostream>
51#include <sstream>
52#include <stdio.h>
53#include <stdlib.h>
54#if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394))
55
56#include <visp3/blob/vpDot2.h>
57#include <visp3/core/vpDisplay.h>
58#include <visp3/core/vpHomogeneousMatrix.h>
59#include <visp3/core/vpImage.h>
60#include <visp3/core/vpIoTools.h>
61#include <visp3/core/vpMath.h>
62#include <visp3/core/vpPoint.h>
63#include <visp3/gui/vpDisplayFactory.h>
64#include <visp3/robot/vpRobotViper850.h>
65#include <visp3/sensor/vp1394TwoGrabber.h>
66#include <visp3/vision/vpPose.h>
67#include <visp3/visual_features/vpFeatureBuilder.h>
68#include <visp3/visual_features/vpFeaturePoint.h>
69#include <visp3/vs/vpServo.h>
70#include <visp3/vs/vpServoDisplay.h>
71
72#define L 0.05 // to deal with a 10cm by 10cm square
73
74#ifdef ENABLE_VISP_NAMESPACE
75using namespace VISP_NAMESPACE_NAME;
76#endif
77
99void compute_pose(vpPoint point[], vpDot2 dot[], int ndot, vpCameraParameters cam, vpHomogeneousMatrix &cMo, bool init)
100{
102 vpPose pose;
103 vpImagePoint cog;
104 for (int i = 0; i < ndot; i++) {
105
106 double x = 0, y = 0;
107 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 { // init = false; use of the previous pose to initialise LOWE
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 joint 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
163 try {
164 vpRobotViper850 robot;
165 // Load the end-effector to camera frame transformation obtained
166 // using a camera intrinsic model with distortion
168 robot.init(vpRobotViper850::TOOL_PTGREY_FLEA2_CAMERA, projModel);
169
171
173 int i;
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
188
191
192 std::cout << std::endl;
193 std::cout << "-------------------------------------------------------" << std::endl;
194 std::cout << " Test program for vpServo " << std::endl;
195 std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl;
196 std::cout << " Use of the Afma6 robot " << std::endl;
197 std::cout << " task : servo 4 points on a square with dimension " << L << " meters" << std::endl;
198 std::cout << "-------------------------------------------------------" << std::endl;
199 std::cout << std::endl;
200
201 vpDot2 dot[4];
202 vpImagePoint cog;
203
204 std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
205
206 for (i = 0; i < 4; i++) {
207 dot[i].setGraphics(true);
208 dot[i].initTracking(I);
209 cog = dot[i].getCog();
212 }
213
215
216 // Update camera parameters
217 robot.getCameraParameters(cam, I);
218
219 cam.printParameters();
220
221 // Sets the current position of the visual feature
222 vpFeaturePoint p[4];
223 for (i = 0; i < 4; i++)
224 vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
225
226 // Set the position of the square target in a frame which origin is
227 // centered in the middle of the square
228 vpPoint point[4];
229 point[0].setWorldCoordinates(-L, -L, 0);
230 point[1].setWorldCoordinates(L, -L, 0);
231 point[2].setWorldCoordinates(L, L, 0);
232 point[3].setWorldCoordinates(-L, L, 0);
233
234 // Initialise a desired pose to compute s*, the desired 2D point features
236 vpTranslationVector cto(0, 0, 0.5); // tz = 0.5 meter
238 vpRotationMatrix cRo(cro); // Build the rotation matrix
239 cMo.buildFrom(cto, cRo); // Build the homogeneous matrix
240
241 // Sets the desired position of the 2D visual feature
242 vpFeaturePoint pd[4];
243 // Compute the desired position of the features from the desired pose
244 for (int i = 0; i < 4; i++) {
245 vpColVector cP, p;
246 point[i].changeFrame(cMo, cP);
247 point[i].projection(cP, p);
248
249 pd[i].set_x(p[0]);
250 pd[i].set_y(p[1]);
251 pd[i].set_Z(cP[2]);
252 }
253
254 // We want to see a point on a point
255 for (i = 0; i < 4; i++)
256 task.addFeature(p[i], pd[i]);
257
258 // Set the proportional gain
259 task.setLambda(0.3);
260
261 // Display task information
262 task.print();
263
264 // Define the task
265 // - we want an eye-in-hand control law
266 // - articular velocity are computed
268 task.setInteractionMatrixType(vpServo::CURRENT, vpServo::PSEUDO_INVERSE);
269 task.print();
270
272 robot.get_cVe(cVe);
273 task.set_cVe(cVe);
274 task.print();
275
276 // Set the Jacobian (expressed in the end-effector frame)
277 vpMatrix eJe;
278 robot.get_eJe(eJe);
279 task.set_eJe(eJe);
280 task.print();
281
282 // Initialise the velocity control of the robot
283 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
284
285 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
286 bool init_pose_from_linear_method = true;
287 for (;;) {
288 // Acquire a new image from the camera
289 g.acquire(I);
290
291 // Display this image
293
294 try {
295 // For each point...
296 for (i = 0; i < 4; i++) {
297 // Achieve the tracking of the dot in the image
298 dot[i].track(I);
299 // Display a green cross at the center of gravity position in the
300 // image
301 cog = dot[i].getCog();
303 }
304 }
305 catch (...) {
306 flog.close(); // Close the log file
307 vpTRACE("Error detected while tracking visual features");
308 robot.stopMotion();
309#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
310 if (display != nullptr) {
311 delete display;
312 }
313#endif
314 return EXIT_FAILURE;
315 }
316
317 // At first iteration, we initialise non linear pose estimation with a linear approach.
318 // For the other iterations, non linear pose estimation is initialized with the pose estimated at previous
319 // iteration of the loop
320 compute_pose(point, dot, 4, cam, cMo, init_pose_from_linear_method);
321 if (init_pose_from_linear_method) {
322 init_pose_from_linear_method = false;
323 }
324
325 for (i = 0; i < 4; i++) {
326 // Update the point feature from the dot location
327 vpFeatureBuilder::create(p[i], cam, dot[i]);
328 // Set the feature Z coordinate from the pose
329 vpColVector cP;
330 point[i].changeFrame(cMo, cP);
331
332 p[i].set_Z(cP[2]);
333 }
334
335 // Get the jacobian of the robot
336 robot.get_eJe(eJe);
337 // Update this jacobian in the task structure. It will be used to
338 // compute the velocity skew (as an articular velocity) qdot = -lambda *
339 // L^+ * cVe * eJe * (s-s*)
340 task.set_eJe(eJe);
341
343 // Compute the visual servoing skew vector
344 v = task.computeControlLaw();
345
346 // Display the current and desired feature points in the image display
347 vpServoDisplay::display(task, cam, I);
348
349 // Apply the computed joint velocities to the robot
350 robot.setVelocity(vpRobot::ARTICULAR_FRAME, v);
351
352 // Save velocities applied to the robot in the log file
353 // v[0], v[1], v[2] correspond to joint translation velocities in m/s
354 // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
355 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
356
357 // Get the measured joint velocities of the robot
358 vpColVector qvel;
359 robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
360 // Save measured joint velocities of the robot in the log file:
361 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
362 // velocities in m/s
363 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
364 // velocities in rad/s
365 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
366
367 // Get the measured joint positions of the robot
368 vpColVector q;
369 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
370 // Save measured joint positions of the robot in the log file
371 // - q[0], q[1], q[2] correspond to measured joint translation
372 // positions in m
373 // - q[3], q[4], q[5] correspond to measured joint rotation
374 // positions in rad
375 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
376
377 // Save feature error (s-s*) for the 4 feature points. For each feature
378 // point, we have 2 errors (along x and y axis). This error is
379 // expressed in meters in the camera frame
380 flog << (task.getError()).t() << std::endl;
381
382 // Flush the display
384
385 // std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<
386 // std::endl;
387 }
388
389 std::cout << "Display task information: " << std::endl;
390 task.print();
391 flog.close(); // Close the log file
392#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
393 if (display != nullptr) {
394 delete display;
395 }
396#endif
397 return EXIT_SUCCESS;
398 }
399 catch (const vpException &e) {
400 flog.close(); // Close the log file
401 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
402#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
403 if (display != nullptr) {
404 delete display;
405 }
406#endif
407 return EXIT_FAILURE;
408 }
409}
410
411#else
412int main()
413{
414 std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
415 return EXIT_SUCCESS;
416}
417#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 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
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 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
@ 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_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 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.