Visual Servoing Platform version 3.5.0
servoViper850Point2DCamVelocityKalman.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 http://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 * Authors:
37 * Eric Marchand
38 * Fabien Spindler
39 *
40 *****************************************************************************/
41
53#include <visp3/core/vpConfig.h>
54#include <visp3/core/vpDebug.h> // Debug trace
55
56#include <fstream>
57#include <iostream>
58#include <sstream>
59#include <stdio.h>
60#include <stdlib.h>
61
62#if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394))
63
64#include <visp3/blob/vpDot2.h>
65#include <visp3/core/vpDisplay.h>
66#include <visp3/core/vpException.h>
67#include <visp3/core/vpHomogeneousMatrix.h>
68#include <visp3/core/vpImage.h>
69#include <visp3/core/vpIoTools.h>
70#include <visp3/core/vpLinearKalmanFilterInstantiation.h>
71#include <visp3/core/vpMath.h>
72#include <visp3/core/vpPoint.h>
73#include <visp3/gui/vpDisplayGTK.h>
74#include <visp3/gui/vpDisplayOpenCV.h>
75#include <visp3/gui/vpDisplayX.h>
76#include <visp3/io/vpImageIo.h>
77#include <visp3/robot/vpRobotViper850.h>
78#include <visp3/sensor/vp1394TwoGrabber.h>
79#include <visp3/visual_features/vpFeatureBuilder.h>
80#include <visp3/visual_features/vpFeaturePoint.h>
81#include <visp3/vs/vpAdaptiveGain.h>
82#include <visp3/vs/vpServo.h>
83#include <visp3/vs/vpServoDisplay.h>
84
85int main()
86{
87 // Log file creation in /tmp/$USERNAME/log.dat
88 // This file contains by line:
89 // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
90 // - the 6 mesured joint velocities (m/s, rad/s)
91 // - the 6 mesured joint positions (m, rad)
92 // - the 2 values of s - s*
93 std::string username;
94 // Get the user login name
95 vpIoTools::getUserName(username);
96
97 // Create a log filename to save velocities...
98 std::string logdirname;
99 logdirname = "/tmp/" + username;
100
101 // Test if the output path exist. If no try to create it
102 if (vpIoTools::checkDirectory(logdirname) == false) {
103 try {
104 // Create the dirname
105 vpIoTools::makeDirectory(logdirname);
106 } catch (...) {
107 std::cerr << std::endl << "ERROR:" << std::endl;
108 std::cerr << " Cannot create " << logdirname << std::endl;
109 exit(-1);
110 }
111 }
112 std::string logfilename;
113 logfilename = logdirname + "/log.dat";
114
115 // Open the log file name
116 std::ofstream flog(logfilename.c_str());
117
118 vpServo task;
119
120 try {
121 // Initialize linear Kalman filter
123
124 // Initialize the kalman filter
125 unsigned int nsignal = 2; // The two values of dedt
126 double rho = 0.3;
127 vpColVector sigma_state;
128 vpColVector sigma_measure(nsignal);
129 unsigned int state_size = 0; // Kalman state vector size
130
132 state_size = kalman.getStateSize();
133 sigma_state.resize(state_size * nsignal);
134 sigma_state = 0.00001; // Same state variance for all signals
135 sigma_measure = 0.05; // Same measure variance for all the signals
136 double dummy = 0; // non used parameter dt for the velocity state model
137 kalman.initFilter(nsignal, sigma_state, sigma_measure, rho, dummy);
138
139 // Initialize the robot
140 vpRobotViper850 robot;
141
143
144 bool reset = false;
145 vp1394TwoGrabber g(reset);
146
147#if 1
149 g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
150#else
153#endif
154 g.open(I);
155
156 double Tloop = 1. / 80.f;
157
159 g.getFramerate(fps);
160 switch (fps) {
162 Tloop = 1.f / 15.f;
163 break;
165 Tloop = 1.f / 30.f;
166 break;
168 Tloop = 1.f / 60.f;
169 break;
171 Tloop = 1.f / 120.f;
172 break;
173 default:
174 break;
175 }
176
177#ifdef VISP_HAVE_X11
178 vpDisplayX display(I, (int)(100 + I.getWidth() + 30), 200, "Current image");
179#elif defined(VISP_HAVE_OPENCV)
180 vpDisplayOpenCV display(I, (int)(100 + I.getWidth() + 30), 200, "Current image");
181#elif defined(VISP_HAVE_GTK)
182 vpDisplayGTK display(I, (int)(100 + I.getWidth() + 30), 200, "Current image");
183#endif
184
187
188 vpDot2 dot;
189 vpImagePoint cog;
190
191 dot.setGraphics(true);
192
193 for (int i = 0; i < 10; i++)
194 g.acquire(I);
195
196 std::cout << "Click on a dot..." << std::endl;
197 dot.initTracking(I);
198
199 cog = dot.getCog();
202
204 // Update camera parameters
205 robot.getCameraParameters(cam, I);
206
207 // sets the current position of the visual feature
209 // retrieve x,y and Z of the vpPoint structure
210 vpFeatureBuilder::create(p, cam, dot);
211
212 // sets the desired position of the visual feature
214 pd.buildFrom(0, 0, 1);
215
216 // define the task
217 // - we want an eye-in-hand control law
218 // - robot is controlled in the camera frame
221
222 // - we want to see a point on a point
223 task.addFeature(p, pd);
224
225 // - set the constant gain
226 vpAdaptiveGain lambda;
227 lambda.initStandard(4, 0.2, 30);
228 task.setLambda(lambda);
229
230 // Display task information
231 task.print();
232
233 // Now the robot will be controlled in velocity
235
236 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
237 vpColVector v, v1, v2;
238 int iter = 0;
239 vpColVector vm(6);
240 double t_0, t_1, Tv;
241 vpColVector err(2), err_1(2);
242 vpColVector dedt_filt(2), dedt_mes(2);
243 dc1394video_frame_t *frame = NULL;
244
245 t_1 = vpTime::measureTimeMs();
246
247 for (;;) {
248 try {
249 t_0 = vpTime::measureTimeMs(); // t_0: current time
250
251 // Update loop time in second
252 Tv = (double)(t_0 - t_1) / 1000.0;
253
254 // Update time for next iteration
255 t_1 = t_0;
256
258
259 // Acquire a new image from the camera
260 frame = g.dequeue(I);
261
262 // Display this image
264
265 // Achieve the tracking of the dot in the image
266 dot.track(I);
267
268 // Get the dot cog
269 cog = dot.getCog();
270
271 // Display a green cross at the center of gravity position in the
272 // image
274
275 // Update the point feature from the dot location
276 vpFeatureBuilder::create(p, cam, dot);
277
278 // Compute the visual servoing skew vector
279 v1 = task.computeControlLaw();
280
281 // Get the error ||s-s*||
282 err = task.getError();
283
285 if (iter == 0) {
286 err_1 = 0;
287 dedt_mes = 0;
288 } else {
289 vpMatrix J1 = task.getTaskJacobian();
290 dedt_mes = (err - err_1) / (Tv)-J1 * vm;
291 err_1 = err;
292 }
293
294 // Filter de/dt
295 if (iter < 2)
296 dedt_mes = 0;
297 kalman.filter(dedt_mes);
298 // Get the filtered values
299 for (unsigned int i = 0; i < nsignal; i++) {
300 dedt_filt[i] = kalman.Xest[i * state_size];
301 }
302 if (iter < 2)
303 dedt_filt = 0;
304
306 v2 = -J1p * dedt_filt;
307
308 // Update the robot camera velocity
309 v = v1 + v2;
310
311 // Display the current and desired feature points in the image display
312 vpServoDisplay::display(task, cam, I);
313
314 // Apply the computed camera velocities to the robot
316
317 iter++;
318 // Synchronize the loop with the image frame rate
319 vpTime::wait(t_0, 1000. * Tloop);
320 // Release the ring buffer used for the last image to start a new acq
321 g.enqueue(frame);
322 } catch (...) {
323 std::cout << "Tracking failed... Stop the robot." << std::endl;
324 v = 0;
325 // Stop robot
327 return 0;
328 }
329
330 // Save velocities applied to the robot in the log file
331 // v[0], v[1], v[2] correspond to camera translation velocities in m/s
332 // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
333 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
334
335 // Get the measured joint velocities of the robot
336 vpColVector qvel;
338 // Save measured joint velocities of the robot in the log file:
339 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
340 // velocities in m/s
341 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
342 // velocities in rad/s
343 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
344
345 // Get the measured joint positions of the robot
346 vpColVector q;
347 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
348 // Save measured joint positions of the robot in the log file
349 // - q[0], q[1], q[2] correspond to measured joint translation
350 // positions in m
351 // - q[3], q[4], q[5] correspond to measured joint rotation
352 // positions in rad
353 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
354
355 // Save feature error (s-s*) for the feature point. For this feature
356 // point, we have 2 errors (along x and y axis). This error is
357 // expressed in meters in the camera frame
358 flog << (task.getError()).t() << std::endl; // s-s* for point
359
360 // Flush the display
362 }
363
364 flog.close(); // Close the log file
365
366 // Display task information
367 task.print();
368
369 return EXIT_SUCCESS;
370 }
371 catch (const vpException &e) {
372 flog.close(); // Close the log file
373 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
374 return EXIT_FAILURE;
375 }
376}
377
378#else
379int main()
380{
381 std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
382 return EXIT_SUCCESS;
383}
384#endif
Class for firewire ieee1394 video devices using libdc1394-2.x api.
Adaptive gain computation.
void initStandard(double gain_at_zero, double gain_at_infinity, double slope_at_zero)
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:310
static const vpColor blue
Definition: vpColor.h:223
static const vpColor green
Definition: vpColor.h:220
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:135
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
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:314
vpImagePoint getCog() const
Definition: vpDot2.h:180
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:253
error that can be emited by ViSP classes.
Definition: vpException.h:72
const char * getMessage() const
Definition: vpException.cpp:90
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: vpImagePoint.h:88
unsigned int getWidth() const
Definition: vpImage.h:246
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:420
static std::string getUserName()
Definition: vpIoTools.cpp:316
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:570
vpColVector Xest
unsigned int getStateSize()
This class provides an implementation of some specific linear Kalman filters.
void initFilter(unsigned int nsignal, vpColVector &sigma_state, vpColVector &sigma_measure, double rho, double dt)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
@ ARTICULAR_FRAME
Definition: vpRobot.h:78
@ CAMERA_FRAME
Definition: vpRobot.h:82
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition: vpRobot.h:66
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
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)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
@ EYEINHAND_CAMERA
Definition: vpServo.h:155
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
void setLambda(double c)
Definition: vpServo.h:404
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
vpMatrix getTaskJacobian() const
Definition: vpServo.cpp:1755
vpColVector getError() const
Definition: vpServo.h:278
@ PSEUDO_INVERSE
Definition: vpServo.h:202
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
vpMatrix getTaskJacobianPseudoInverse() const
Definition: vpServo.cpp:1775
@ DESIRED
Definition: vpServo.h:186
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()