ViSP  2.9.0
servoViper850Point2DCamVelocityKalman.cpp
1 /****************************************************************************
2  *
3  * $Id: servoViper850Point2DCamVelocityKalman.cpp 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * tests the control law
36  * eye-in-hand control
37  * velocity computed in camera frame
38  *
39  * Authors:
40  * Eric Marchand
41  * Fabien Spindler
42  *
43  *****************************************************************************/
44 
45 
57 #include <visp/vpConfig.h>
58 #include <visp/vpDebug.h> // Debug trace
59 
60 #include <stdlib.h>
61 #include <stdio.h>
62 #include <iostream>
63 #include <fstream>
64 #include <sstream>
65 
66 #if (defined (VISP_HAVE_VIPER850) && defined (VISP_HAVE_DC1394_2))
67 
68 #include <visp/vp1394TwoGrabber.h>
69 #include <visp/vpImage.h>
70 #include <visp/vpMath.h>
71 #include <visp/vpHomogeneousMatrix.h>
72 #include <visp/vpFeaturePoint.h>
73 #include <visp/vpPoint.h>
74 #include <visp/vpServo.h>
75 #include <visp/vpFeatureBuilder.h>
76 #include <visp/vpRobotViper850.h>
77 #include <visp/vpIoTools.h>
78 #include <visp/vpException.h>
79 #include <visp/vpMatrixException.h>
80 #include <visp/vpServoDisplay.h>
81 #include <visp/vpImageIo.h>
82 #include <visp/vpDot2.h>
83 #include <visp/vpAdaptiveGain.h>
84 #include <visp/vpLinearKalmanFilterInstantiation.h>
85 #include <visp/vpDisplay.h>
86 #include <visp/vpDisplayX.h>
87 #include <visp/vpDisplayOpenCV.h>
88 #include <visp/vpDisplayGTK.h>
89 
90 int
91 main()
92 {
93  // Log file creation in /tmp/$USERNAME/log.dat
94  // This file contains by line:
95  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
96  // - the 6 mesured joint velocities (m/s, rad/s)
97  // - the 6 mesured joint positions (m, rad)
98  // - the 2 values of s - s*
99  std::string username;
100  // Get the user login name
101  vpIoTools::getUserName(username);
102 
103  // Create a log filename to save velocities...
104  std::string logdirname;
105  logdirname ="/tmp/" + username;
106 
107  // Test if the output path exist. If no try to create it
108  if (vpIoTools::checkDirectory(logdirname) == false) {
109  try {
110  // Create the dirname
111  vpIoTools::makeDirectory(logdirname);
112  }
113  catch (...) {
114  std::cerr << std::endl
115  << "ERROR:" << std::endl;
116  std::cerr << " Cannot create " << logdirname << std::endl;
117  exit(-1);
118  }
119  }
120  std::string logfilename;
121  logfilename = logdirname + "/log.dat";
122 
123  // Open the log file name
124  std::ofstream flog(logfilename.c_str());
125 
126  vpServo task ;
127 
128  try {
129  // Initialize linear Kalman filter
131 
132  // Initialize the kalman filter
133  unsigned int nsignal = 2; // The two values of dedt
134  double rho = 0.3;
135  vpColVector sigma_state;
136  vpColVector sigma_measure(nsignal);
137  unsigned int state_size = 0; // Kalman state vector size
138 
140  state_size = kalman.getStateSize();
141  sigma_state.resize(state_size*nsignal);
142  sigma_state = 0.00001; // Same state variance for all signals
143  sigma_measure = 0.05; // Same measure variance for all the signals
144  double dummy = 0; // non used parameter dt for the velocity state model
145  kalman.initFilter(nsignal, sigma_state, sigma_measure, rho, dummy);
146 
147  // Initialize the robot
148  vpRobotViper850 robot ;
149 
150 
152 
153  bool reset = false;
154  vp1394TwoGrabber g(reset);
155 
156 #if 1
158  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
159 #else
161  g.setColorCoding(vp1394TwoGrabber::vpCOLOR_CODING_MONO8);
162 #endif
163  g.open(I) ;
164 
165  double Tloop = 1./80.f;
166 
168  g.getFramerate(fps);
169  switch(fps) {
170  case vp1394TwoGrabber::vpFRAMERATE_15 : Tloop = 1.f/15.f; break;
171  case vp1394TwoGrabber::vpFRAMERATE_30 : Tloop = 1.f/30.f; break;
172  case vp1394TwoGrabber::vpFRAMERATE_60 : Tloop = 1.f/60.f; break;
173  case vp1394TwoGrabber::vpFRAMERATE_120: Tloop = 1.f/120.f; break;
174  default: 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 
185  vpDisplay::display(I) ;
186  vpDisplay::flush(I) ;
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();
201  vpDisplay::flush(I);
202 
203  vpCameraParameters cam ;
204  // Update camera parameters
205  robot.getCameraParameters (cam, I);
206 
207  // sets the current position of the visual feature
208  vpFeaturePoint p ;
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
213  vpFeaturePoint pd ;
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
219  task.setServo(vpServo::EYEINHAND_CAMERA) ;
220  task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE) ;
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
263  vpDisplay::display(I) ;
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 image
273 
274  // Update the point feature from the dot location
275  vpFeatureBuilder::create(p, cam, dot);
276 
277  // Compute the visual servoing skew vector
278  v1 = task.computeControlLaw() ;
279 
280  // Get the error ||s-s*||
281  err = task.getError();
282 
284  if (iter==0){
285  err_1 = 0;
286  dedt_mes = 0;
287  }
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 
305  vpMatrix J1p = task.getTaskJacobianPseudoInverse();
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  }
323  catch(...) {
324  std::cout << "Tracking failed... Stop the robot." << std::endl;
325  v = 0;
326  // Stop robot
328  // Kill the task
329  task.kill();
330  return 0;
331  }
332 
333  // Save velocities applied to the robot in the log file
334  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
335  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
336  flog << v[0] << " " << v[1] << " " << v[2] << " "
337  << v[3] << " " << v[4] << " " << v[5] << " ";
338 
339  // Get the measured joint velocities of the robot
340  vpColVector qvel;
342  // Save measured joint velocities of the robot in the log file:
343  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
344  // velocities in m/s
345  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
346  // velocities in rad/s
347  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
348  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
349 
350  // Get the measured joint positions of the robot
351  vpColVector 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] << " "
359  << q[3] << " " << q[4] << " " << q[5] << " ";
360 
361  // Save feature error (s-s*) for the feature point. For this feature
362  // point, we have 2 errors (along x and y axis). This error is expressed
363  // in meters in the camera frame
364  flog << ( task.getError() ).t() << std::endl; // s-s* for point
365 
366  // Flush the display
367  vpDisplay::flush(I) ;
368  }
369 
370  flog.close() ; // Close the log file
371 
372  // Display task information
373  task.print() ;
374 
375  // Kill the task
376  task.kill();
377 
378  return 0;
379  }
380  catch (...)
381  {
382  flog.close() ; // Close the log file
383  // Kill the task
384  task.kill();
385  vpERROR_TRACE(" Test failed") ;
386  return 0;
387  }
388 }
389 
390 #else
391 int
392 main()
393 {
394  vpERROR_TRACE("You do not have a Viper robot or a firewire framegrabber connected to your computer...");
395 }
396 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
Adaptive gain computation.
unsigned int getStateSize()
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
unsigned int getWidth() const
Definition: vpImage.h:159
void initFilter(unsigned int nsignal, vpColVector &sigma_state, vpColVector &sigma_measure, double rho, double dt)
#define vpERROR_TRACE
Definition: vpDebug.h:395
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper850.cpp:580
Control of Irisa's Viper S850 robot named Viper850.
Define the X11 console to display images.
Definition: vpDisplayX.h:152
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static double measureTimeMs()
Definition: vpTime.cpp:86
static int wait(double t0, double t)
Definition: vpTime.cpp:149
vpColVector Xest
static const vpColor green
Definition: vpColor.h:170
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)
Definition: vpDot2.cpp:465
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
vpImagePoint getCog() const
Definition: vpDot2.h:163
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
void initStandard(double gain_at_zero, double gain_at_infinity, double slope_at_zero)
Initialize the velocity controller.
Definition: vpRobot.h:70
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
Generic class defining intrinsic camera parameters.
static std::string getUserName()
Definition: vpIoTools.cpp:140
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
void buildFrom(const double x, const double y, const double Z)
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:266
This class provides an implementation of some specific linear Kalman filters.
Class for firewire ieee1394 video devices using libdc1394-2.x api.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
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 setGraphics(const bool activate)
Definition: vpDot2.h:312
static const vpColor blue
Definition: vpColor.h:173
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94