Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
servoViper650Point2DCamVelocity.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See https://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * tests the control law
33 * eye-in-hand control
34 * velocity computed in camera frame
35 *
36*****************************************************************************/
37
53#include <fstream>
54#include <iostream>
55#include <sstream>
56#include <stdio.h>
57#include <stdlib.h>
58
59#include <visp3/core/vpConfig.h>
60
61#if defined(VISP_HAVE_VIPER650) && defined(VISP_HAVE_DC1394) && defined(VISP_HAVE_X11)
62
63#include <visp3/blob/vpDot2.h>
64#include <visp3/core/vpHomogeneousMatrix.h>
65#include <visp3/core/vpIoTools.h>
66#include <visp3/core/vpPoint.h>
67#include <visp3/gui/vpDisplayX.h>
68#include <visp3/robot/vpRobotViper650.h>
69#include <visp3/sensor/vp1394TwoGrabber.h>
70#include <visp3/vision/vpPose.h>
71#include <visp3/visual_features/vpFeatureBuilder.h>
72#include <visp3/visual_features/vpFeaturePoint.h>
73#include <visp3/vs/vpServo.h>
74#include <visp3/vs/vpServoDisplay.h>
75
76int main()
77{
78 // Log file creation in /tmp/$USERNAME/log.dat
79 // This file contains by line:
80 // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
81 // - the 6 mesured joint velocities (m/s, rad/s)
82 // - the 6 mesured joint positions (m, rad)
83 // - the 2 values of s - s*
84 std::string username;
85 // Get the user login name
86 vpIoTools::getUserName(username);
87
88 // Create a log filename to save velocities...
89 std::string logdirname;
90 logdirname = "/tmp/" + username;
91
92 // Test if the output path exist. If no try to create it
93 if (vpIoTools::checkDirectory(logdirname) == false) {
94 try {
95 // Create the dirname
96 vpIoTools::makeDirectory(logdirname);
97 } catch (...) {
98 std::cerr << std::endl << "ERROR:" << std::endl;
99 std::cerr << " Cannot create " << logdirname << std::endl;
100 return EXIT_FAILURE;
101 }
102 }
103 std::string logfilename;
104 logfilename = logdirname + "/log.dat";
105
106 // Open the log file name
107 std::ofstream flog(logfilename.c_str());
108
109 try {
110 vpRobotViper650 robot;
111
112 vpServo task;
113
115
116 bool reset = false;
117 vp1394TwoGrabber g(reset);
118
119#if 1
121 g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
122#else
125#endif
126 g.open(I);
127
128 vpDisplayX display(I, 100, 100, "Current image");
131
132 vpDot2 dot;
133
134 dot.setGraphics(true);
135
136 for (int i = 0; i < 10; i++) // warm up the camera
137 g.acquire(I);
138
139 std::cout << "Click on a dot..." << std::endl;
140 dot.initTracking(I);
141
142 vpImagePoint cog = dot.getCog();
145
147 // Update camera parameters
148 robot.getCameraParameters(cam, I);
149
150 // sets the current position of the visual feature
152 // retrieve x,y and Z of the vpPoint structure
153 vpFeatureBuilder::create(p, cam, dot);
154
155 // sets the desired position of the visual feature
157 pd.buildFrom(0, 0, 1);
158
159 // define the task
160 // - we want an eye-in-hand control law
161 // - robot is controlled in the camera frame
163
164 // - we want to see a point on a point
165 task.addFeature(p, pd);
166
167 // - set the constant gain
168 task.setLambda(0.8);
169
170 // Display task information
171 task.print();
172
173 // Now the robot will be controlled in velocity
175
176 std::cout << "\nHit CTRL-C or click in the image to stop the loop...\n" << std::flush;
177
178 for (;;) {
179 // Acquire a new image from the camera
180 g.acquire(I);
181
182 // Display this image
184
185 try {
186 // Achieve the tracking of the dot in the image
187 dot.track(I);
188 } catch (...) {
189 std::cout << "Error detected while tracking visual features.." << std::endl;
190 break;
191 }
192
193 // Get the dot cog
194 cog = dot.getCog();
195
196 // Display a green cross at the center of gravity position in the image
198
199 // Update the point feature from the dot location
200 vpFeatureBuilder::create(p, cam, dot);
201
202 // Compute the visual servoing skew vector
204
205 // Display the current and desired feature points in the image display
206 vpServoDisplay::display(task, cam, I);
207
208 // Apply the computed camera velocities to the robot
210
211 // Save velocities applied to the robot in the log file
212 // v[0], v[1], v[2] correspond to camera translation velocities in m/s
213 // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
214 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
215
216 // Get the measured joint velocities of the robot
217 vpColVector qvel;
219 // Save measured joint velocities of the robot in the log file:
220 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
221 // velocities in m/s
222 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
223 // velocities in rad/s
224 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
225
226 // Get the measured joint positions of the robot
227 vpColVector q;
228 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
229 // Save measured joint positions of the robot in the log file
230 // - q[0], q[1], q[2] correspond to measured joint translation
231 // positions in m
232 // - q[3], q[4], q[5] correspond to measured joint rotation
233 // positions in rad
234 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
235
236 // Save feature error (s-s*) for the feature point. For this feature
237 // point, we have 2 errors (along x and y axis). This error is
238 // expressed in meters in the camera frame
239 flog << (task.getError()).t() << std::endl; // s-s* for point
240
241 vpDisplay::displayText(I, 10, 10, "Click to quit...", vpColor::red);
242 if (vpDisplay::getClick(I, false))
243 break;
244
245 // Flush the display
247 }
248
249 robot.stopMotion();
250
251 flog.close(); // Close the log file
252
253 // Display task information
254 task.print();
255
256 return EXIT_SUCCESS;
257 } catch (const vpException &e) {
258 flog.close(); // Close the log file
259 std::cout << "Catched an exception: " << e.getMessage() << std::endl;
260 return EXIT_FAILURE;
261 }
262}
263
264#else
265int main()
266{
267 std::cout << "You do not have an Viper 650 robot connected to your computer..." << std::endl;
268 return EXIT_SUCCESS;
269}
270#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 red
Definition vpColor.h:211
static const vpColor blue
Definition vpColor.h:217
static const vpColor green
Definition vpColor.h:214
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:132
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)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition vpDot2.h:124
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition vpDot2.cpp:441
void setGraphics(bool activate)
Definition vpDot2.h:311
vpImagePoint getCog() const
Definition vpDot2.h:177
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition vpDot2.cpp:252
error that can be emitted by ViSP classes.
Definition vpException.h:59
const char * getMessage() const
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 buildFrom(double x, double y, 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:135
static bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static void makeDirectory(const std::string &dirname)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Control of Irisa's Viper S650 robot named Viper650.
@ ARTICULAR_FRAME
Definition vpRobot.h:76
@ CAMERA_FRAME
Definition vpRobot.h:80
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition vpRobot.cpp:198
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:151
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition vpServo.cpp:299
void setLambda(double c)
Definition vpServo.h:403
void setServo(const vpServoType &servo_type)
Definition vpServo.cpp:210
vpColVector getError() const
Definition vpServo.h:276
vpColVector computeControlLaw()
Definition vpServo.cpp:930
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition vpServo.cpp:487