ViSP  2.6.2
servoViper850Point2DCamVelocityKalman.cpp
1 /****************************************************************************
2  *
3  * $Id: servoViper850Point2DCamVelocityKalman.cpp 3616 2012-03-09 14:31:52Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 
88 int
89 main()
90 {
91  // Log file creation in /tmp/$USERNAME/log.dat
92  // This file contains by line:
93  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
94  // - the 6 mesured joint velocities (m/s, rad/s)
95  // - the 6 mesured joint positions (m, rad)
96  // - the 2 values of s - s*
97  std::string username;
98  // Get the user login name
99  vpIoTools::getUserName(username);
100 
101  // Create a log filename to save velocities...
102  std::string logdirname;
103  logdirname ="/tmp/" + username;
104 
105  // Test if the output path exist. If no try to create it
106  if (vpIoTools::checkDirectory(logdirname) == false) {
107  try {
108  // Create the dirname
109  vpIoTools::makeDirectory(logdirname);
110  }
111  catch (...) {
112  std::cerr << std::endl
113  << "ERROR:" << std::endl;
114  std::cerr << " Cannot create " << logdirname << std::endl;
115  exit(-1);
116  }
117  }
118  std::string logfilename;
119  logfilename = logdirname + "/log.dat";
120 
121  // Open the log file name
122  std::ofstream flog(logfilename.c_str());
123 
124  vpServo task ;
125 
126  try {
127  // Initialize linear Kalman filter
129 
130  // Initialize the kalman filter
131  unsigned int nsignal = 2; // The two values of dedt
132  double rho = 0.3;
133  vpColVector sigma_state;
134  vpColVector sigma_measure(nsignal);
135  unsigned int state_size = 0; // Kalman state vector size
136 
138  state_size = kalman.getStateSize();
139  sigma_state.resize(state_size*nsignal);
140  sigma_state = 0.00001; // Same state variance for all signals
141  sigma_measure = 0.05; // Same measure variance for all the signals
142  double dummy = 0; // non used parameter dt for the velocity state model
143  kalman.initFilter(nsignal, sigma_state, sigma_measure, rho, dummy);
144 
145  // Initialize the robot
146  vpRobotViper850 robot ;
147 
148 
150 
151  bool reset = false;
152  vp1394TwoGrabber g(reset);
153 
154 #if 1
156  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
157 #else
159  g.setColorCoding(vp1394TwoGrabber::vpCOLOR_CODING_MONO8);
160 #endif
161  g.open(I) ;
162 
163  double Tloop = 1./80.f;
164 
166  g.getFramerate(fps);
167  switch(fps) {
168  case vp1394TwoGrabber::vpFRAMERATE_15 : Tloop = 1.f/15.f; break;
169  case vp1394TwoGrabber::vpFRAMERATE_30 : Tloop = 1.f/30.f; break;
170  case vp1394TwoGrabber::vpFRAMERATE_60 : Tloop = 1.f/60.f; break;
171  case vp1394TwoGrabber::vpFRAMERATE_120: Tloop = 1.f/120.f; break;
172  default: break;
173  }
174 
175  vpDisplayX display(I, (int)(100+I.getWidth()+30), 200,"Current image") ;
176 
177  vpDisplay::display(I) ;
178  vpDisplay::flush(I) ;
179 
180  vpDot2 dot ;
181  vpImagePoint cog;
182 
183  dot.setGraphics(true);
184 
185  for (int i=0; i< 10; i++)
186  g.acquire(I) ;
187 
188  std::cout << "Click on a dot..." << std::endl;
189  dot.initTracking(I) ;
190 
191  cog = dot.getCog();
193  vpDisplay::flush(I);
194 
195  vpCameraParameters cam ;
196  // Update camera parameters
197  robot.getCameraParameters (cam, I);
198 
199  // sets the current position of the visual feature
200  vpFeaturePoint p ;
201  // retrieve x,y and Z of the vpPoint structure
202  vpFeatureBuilder::create(p,cam, dot);
203 
204  // sets the desired position of the visual feature
205  vpFeaturePoint pd ;
206  pd.buildFrom(0,0,1) ;
207 
208  // define the task
209  // - we want an eye-in-hand control law
210  // - robot is controlled in the camera frame
211  task.setServo(vpServo::EYEINHAND_CAMERA) ;
212 
213  // - we want to see a point on a point
214  task.addFeature(p,pd) ;
215 
216  // - set the constant gain
217  vpAdaptiveGain lambda;
218  lambda.initStandard(4, 0.2, 30);
219  task.setLambda(lambda) ;
220 
221  // Display task information
222  task.print() ;
223 
224  // Now the robot will be controlled in velocity
226 
227  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
228  vpColVector v, v1, v2 ;
229  int iter = 0;
230  vpColVector vm(6);
231  double t_0, t_1, Tv;
232  vpColVector err(2), err_1(2);
233  vpColVector dedt_filt(2), dedt_mes(2);
234  dc1394video_frame_t *frame = NULL;
235 
236  t_1 = vpTime::measureTimeMs();
237 
238  for ( ; ; ) {
239  try {
240  t_0 = vpTime::measureTimeMs(); // t_0: current time
241 
242  // Update loop time in second
243  Tv = (double)(t_0 - t_1) / 1000.0;
244 
245  // Update time for next iteration
246  t_1 = t_0;
247 
249 
250  // Acquire a new image from the camera
251  frame = g.dequeue(I);
252 
253  // Display this image
254  vpDisplay::display(I) ;
255 
256  // Achieve the tracking of the dot in the image
257  dot.track(I) ;
258 
259  // Get the dot cog
260  cog = dot.getCog();
261 
262  // Display a green cross at the center of gravity position in the image
264 
265  // Update the point feature from the dot location
266  vpFeatureBuilder::create(p, cam, dot);
267 
268  // Compute the visual servoing skew vector
269  v1 = task.computeControlLaw() ;
270 
271  // Get the error ||s-s*||
272  err = task.getError();
273 
275  if (iter==0){
276  err_1 = 0;
277  dedt_mes = 0;
278  }
279  else{
280  err_1 = err;
281  vpMatrix J1 = task.getTaskJacobian();
282  dedt_mes = (err_1 - err)/(Tv) - J1 *vm;
283  }
284 
285  // Filter de/dt
286  if (iter < 2)
287  dedt_mes = 0;
288  kalman.filter(dedt_mes);
289  // Get the filtered values
290  for (unsigned int i=0; i < nsignal; i++) {
291  dedt_filt[i] = kalman.Xest[i*state_size];
292  }
293  if (iter < 2)
294  dedt_filt = 0;
295 
296  vpMatrix J1p = task.getTaskJacobianPseudoInverse();
297  v2 = - J1p*dedt_filt;
298 
299  // Update the robot camera velocity
300  v = v1 + v2;
301 
302  // Display the current and desired feature points in the image display
303  vpServoDisplay::display(task, cam, I) ;
304 
305  // Apply the computed camera velocities to the robot
307 
308  iter ++;
309  // Synchronize the loop with the image frame rate
310  vpTime::wait(t_0, 1000.*Tloop);
311  // Release the ring buffer used for the last image to start a new acq
312  g.enqueue(frame);
313  }
314  catch(...) {
315  std::cout << "Tracking failed... Stop the robot." << std::endl;
316  v = 0;
317  // Stop robot
319  // Kill the task
320  task.kill();
321  return 0;
322  }
323 
324  // Save velocities applied to the robot in the log file
325  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
326  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
327  flog << v[0] << " " << v[1] << " " << v[2] << " "
328  << v[3] << " " << v[4] << " " << v[5] << " ";
329 
330  // Get the measured joint velocities of the robot
331  vpColVector qvel;
333  // Save measured joint velocities of the robot in the log file:
334  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
335  // velocities in m/s
336  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
337  // velocities in rad/s
338  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
339  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
340 
341  // Get the measured joint positions of the robot
342  vpColVector q;
344  // Save measured joint positions of the robot in the log file
345  // - q[0], q[1], q[2] correspond to measured joint translation
346  // positions in m
347  // - q[3], q[4], q[5] correspond to measured joint rotation
348  // positions in rad
349  flog << q[0] << " " << q[1] << " " << q[2] << " "
350  << q[3] << " " << q[4] << " " << q[5] << " ";
351 
352  // Save feature error (s-s*) for the feature point. For this feature
353  // point, we have 2 errors (along x and y axis). This error is expressed
354  // in meters in the camera frame
355  flog << ( task.getError() ).t() << std::endl; // s-s* for point
356 
357  // Flush the display
358  vpDisplay::flush(I) ;
359 
360  }
361 
362  flog.close() ; // Close the log file
363 
364  // Display task information
365  task.print() ;
366 
367  // Kill the task
368  task.kill();
369 
370  return 0;
371  }
372  catch (...)
373  {
374  flog.close() ; // Close the log file
375  // Kill the task
376  task.kill();
377  vpERROR_TRACE(" Test failed") ;
378  return 0;
379  }
380 }
381 
382 #else
383 int
384 main()
385 {
386  vpERROR_TRACE("You do not have a Viper robot or a firewire framegrabber connected to your computer...");
387 }
388 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
Adaptive gain computation.
unsigned int getStateSize()
static void display(vpServo &s, const vpCameraParameters &cam, vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:289
unsigned int getWidth() const
Definition: vpImage.h:154
void initFilter(unsigned int nsignal, vpColVector &sigma_state, vpColVector &sigma_measure, double rho, double dt)
#define vpERROR_TRACE
Definition: vpDebug.h:379
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:168
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:114
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:439
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1964
vpImagePoint getCog() const
Definition: vpDot2.h:254
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
Initialize the velocity controller.
Definition: vpRobot.h:70
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:186
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:136
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 getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height)
Definition: vpViper850.cpp:576
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:240
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)
Class required to compute the visual servoing control law.
Definition: vpServo.h:150
void initStandard(double en_zero, double en_infini, double pente_en_zero)
void setGraphics(const bool activate)
Definition: vpDot2.h:178
static const vpColor blue
Definition: vpColor.h:171
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94