Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoViper850Point2DCamVelocity.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 camera frame
34 */
35
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
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/vpException.h>
60#include <visp3/core/vpHomogeneousMatrix.h>
61#include <visp3/core/vpImage.h>
62#include <visp3/core/vpIoTools.h>
63#include <visp3/core/vpMath.h>
64#include <visp3/core/vpPoint.h>
65#include <visp3/gui/vpDisplayFactory.h>
66#include <visp3/io/vpImageIo.h>
67#include <visp3/robot/vpRobotViper850.h>
68#include <visp3/sensor/vp1394TwoGrabber.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
74int main()
75{
76#ifdef ENABLE_VISP_NAMESPACE
77 using namespace VISP_NAMESPACE_NAME;
78#endif
79
80 // Log file creation in /tmp/$USERNAME/log.dat
81 // This file contains by line:
82 // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
83 // - the 6 measured joint velocities (m/s, rad/s)
84 // - the 6 measured joint positions (m, rad)
85 // - the 2 values of s - s*
86 std::string username;
87 // Get the user login name
88 vpIoTools::getUserName(username);
89
90 // Create a log filename to save velocities...
91 std::string logdirname;
92 logdirname = "/tmp/" + username;
93
94 // Test if the output path exist. If no try to create it
95 if (vpIoTools::checkDirectory(logdirname) == false) {
96 try {
97 // Create the dirname
98 vpIoTools::makeDirectory(logdirname);
99 }
100 catch (...) {
101 std::cerr << std::endl << "ERROR:" << std::endl;
102 std::cerr << " Cannot create " << logdirname << std::endl;
103 return EXIT_FAILURE;
104 }
105 }
106 std::string logfilename;
107 logfilename = logdirname + "/log.dat";
108
109 // Open the log file name
110 std::ofstream flog(logfilename.c_str());
111
112#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
113 std::shared_ptr<vpDisplay> display;
114#else
115 vpDisplay *display = nullptr;
116#endif
117
118 try {
119 vpRobotViper850 robot;
120
122
124
125 bool reset = false;
126 vp1394TwoGrabber g(reset);
127
128#if 1
130 g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
131#else
134#endif
135 g.open(I);
136
137#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
138 display = vpDisplayFactory::createDisplay(I, static_cast<int>(100 + I.getWidth() + 30), 200, "Current image");
139#else
140 display = vpDisplayFactory::allocateDisplay(I, static_cast<int>(100 + I.getWidth() + 30), 200, "Current image");
141#endif
142
145
146 vpDot2 dot;
147 vpImagePoint cog;
148
149 dot.setGraphics(true);
150
151 for (int i = 0; i < 10; i++)
152 g.acquire(I);
153
154 std::cout << "Click on a dot..." << std::endl;
155 dot.initTracking(I);
156
157 cog = dot.getCog();
160
162 // Update camera parameters
163 robot.getCameraParameters(cam, I);
164
165 // sets the current position of the visual feature
167 // retrieve x,y and Z of the vpPoint structure
168 vpFeatureBuilder::create(p, cam, dot);
169
170 // sets the desired position of the visual feature
172 pd.buildFrom(0, 0, 1);
173
174 // define the task
175 // - we want an eye-in-hand control law
176 // - robot is controlled in the camera frame
178
179 // - we want to see a point on a point
180 task.addFeature(p, pd);
181
182 // - set the constant gain
183 task.setLambda(0.8);
184
185 // Display task information
186 task.print();
187
188 // Now the robot will be controlled in velocity
189 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
190
191 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
193 for (;;) {
194 try {
195 // Acquire a new image from the camera
196 g.acquire(I);
197
198 // Display this image
200
201 // Achieve the tracking of the dot in the image
202 dot.track(I);
203
204 // Get the dot cog
205 cog = dot.getCog();
206
207 // Display a green cross at the center of gravity position in the
208 // image
210
211 // Update the point feature from the dot location
212 vpFeatureBuilder::create(p, cam, dot);
213
214 // Compute the visual servoing skew vector
215 v = task.computeControlLaw();
216
217 // Display the current and desired feature points in the image display
218 vpServoDisplay::display(task, cam, I);
219
220 // Apply the computed camera velocities to the robot
221 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
222 }
223 catch (...) {
224 std::cout << "Tracking failed... Stop the robot." << std::endl;
225 v = 0;
226 // Stop robot
227 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
228#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
229 if (display != nullptr) {
230 delete display;
231 }
232#endif
233 return EXIT_FAILURE;
234 }
235
236 // Save velocities applied to the robot in the log file
237 // v[0], v[1], v[2] correspond to camera translation velocities in m/s
238 // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
239 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
240
241 // Get the measured joint velocities of the robot
242 vpColVector qvel;
243 robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
244 // Save measured joint velocities of the robot in the log file:
245 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
246 // velocities in m/s
247 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
248 // velocities in rad/s
249 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
250
251 // Get the measured joint positions of the robot
252 vpColVector q;
253 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
254 // Save measured joint positions of the robot in the log file
255 // - q[0], q[1], q[2] correspond to measured joint translation
256 // positions in m
257 // - q[3], q[4], q[5] correspond to measured joint rotation
258 // positions in rad
259 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
260
261 // Save feature error (s-s*) for the feature point. For this feature
262 // point, we have 2 errors (along x and y axis). This error is
263 // expressed in meters in the camera frame
264 flog << (task.getError()).t() << std::endl; // s-s* for point
265
266 // Flush the display
268 }
269
270 flog.close(); // Close the log file
271
272 // Display task information
273 task.print();
274
275#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
276 if (display != nullptr) {
277 delete display;
278 }
279#endif
280
281 return EXIT_SUCCESS;
282 }
283 catch (const vpException &e) {
284 flog.close(); // Close the log file
285 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
286#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
287 if (display != nullptr) {
288 delete display;
289 }
290#endif
291 return EXIT_FAILURE;
292 }
293}
294
295#else
296int main()
297{
298 std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
299 return EXIT_SUCCESS;
300}
301#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.
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...
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
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)
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
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
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.