63 #include <visp/vpConfig.h>
64 #include <visp/vpDebug.h>
66 #if (defined (VISP_HAVE_AFMA4) && defined (VISP_HAVE_DC1394_2))
68 #include <visp/vp1394TwoGrabber.h>
69 #include <visp/vpImage.h>
70 #include <visp/vpDisplay.h>
71 #include <visp/vpDisplayX.h>
72 #include <visp/vpDisplayOpenCV.h>
73 #include <visp/vpDisplayGTK.h>
75 #include <visp/vpMath.h>
76 #include <visp/vpHomogeneousMatrix.h>
77 #include <visp/vpFeaturePoint.h>
78 #include <visp/vpPoint.h>
79 #include <visp/vpServo.h>
80 #include <visp/vpFeatureBuilder.h>
81 #include <visp/vpRobotAfma4.h>
82 #include <visp/vpIoTools.h>
83 #include <visp/vpException.h>
84 #include <visp/vpMatrixException.h>
85 #include <visp/vpServoDisplay.h>
86 #include <visp/vpParseArgv.h>
87 #include <visp/vpDot2.h>
88 #include <visp/vpAdaptiveGain.h>
89 #include <visp/vpLinearKalmanFilterInstantiation.h>
93 #define GETOPTARGS "hK:l:"
110 void usage(
const char *name,
const char *badparam,
114 Tests a control law with the following characteristics:\n\
115 - eye-in-hand control\n\
116 - camera velocity are computed\n\
117 - servo on 1 points.\n\
118 - Kalman filtering\n\
121 %s [-K <0|1|2|3>] [-h]\n", name);
126 Set the constant gain. By default adaptive gain. \n\
132 2: acceleration model\n\
135 Print the help.\n", (
int) kalman);
138 fprintf(stderr,
"ERROR: \n" );
139 fprintf(stderr,
"\nBad parameter [%s]\n", badparam);
158 bool getOptions(
int argc,
const char **argv, KalmanType &kalman,
159 bool &doAdaptativeGain,
168 kalman = (KalmanType) atoi(optarg);
171 doAdaptativeGain =
false;
174 case 'h': usage(argv[0], NULL, kalman);
178 usage(argv[0], optarg, kalman);
183 if ((c == 1) || (c == -1)) {
185 usage(argv[0], NULL, kalman);
186 std::cerr <<
"ERROR: " << std::endl;
187 std::cerr <<
" Bad argument " << optarg << std::endl << std::endl;
195 main(
int argc,
const char ** argv)
197 KalmanType opt_kalman = K_NONE;
199 bool doAdaptativeGain =
true;
201 int opt_cam_frequency = 60;
204 if (getOptions(argc, argv, opt_kalman, doAdaptativeGain, lambda) ==
false) {
214 std::string username;
219 std::string logdirname;
220 logdirname =
"/tmp/" + username;
229 std::cerr << std::endl
230 <<
"ERROR:" << std::endl;
231 std::cerr <<
" Cannot create " << logdirname << std::endl;
235 std::string logfilename;
236 logfilename = logdirname +
"/log.dat";
239 std::ofstream flog(logfilename.c_str());
247 switch(opt_cam_frequency) {
254 for (
int i=0; i < 10; i++)
258 vpDisplayX display(I,100,100,
"Current image") ;
259 #elif defined(VISP_HAVE_OPENCV)
261 #elif defined(VISP_HAVE_GTK)
268 std::cout << std::endl ;
269 std::cout <<
"-------------------------------------------------------" << std::endl ;
270 std::cout <<
"Test program for target motion compensation using a Kalman filter " <<std::endl ;
271 std::cout <<
"Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
272 std::cout <<
"Task : servo a point \n" << std::endl ;
277 std::cout <<
"Servo with no target motion compensation (see -K option)\n";
280 std::cout <<
"Servo with target motion compensation using a Kalman filter\n"
281 <<
"with constant velocity modelization (see -K option)\n";
284 std::cout <<
"Servo with target motion compensation using a Kalman filter\n"
285 <<
"with constant acceleration modelization (see -K option)\n";
288 std::cout <<
"-------------------------------------------------------" << std::endl ;
289 std::cout << std::endl ;
293 std::cout <<
"Click on the dot..." << std::endl;
324 std::cout << std::endl ;
325 task.addFeature(p,pd) ;
328 task.setLambda(lambda) ;
341 unsigned int nsignal = 2;
345 unsigned int state_size = 0;
352 sigma_state.
resize(state_size*nsignal);
353 sigma_state = 0.00001;
354 sigma_measure = 0.05;
356 kalman.
initFilter(nsignal, sigma_state, sigma_measure, rho, dummy);
360 case K_ACCELERATION: {
364 sigma_state.
resize(state_size*nsignal);
365 sigma_state = 0.00001;
366 sigma_measure = 0.05;
367 double dt = 1./opt_cam_frequency;
368 kalman.
initFilter(nsignal, sigma_state, sigma_measure, rho, dt );
379 double t_0, t_1, Tv, Tv_0, Tv_1;
397 std::cout <<
"\nHit CTRL-C to stop the loop...\n" << std::flush;
401 Tv = (double)(t_0 - t_1) / 1000.0;
435 v1 = task.computeControlLaw() ;
450 dedt_mes = (err_0 - err_1)/(Tv_1) - task.J1*vm_0;
468 for (
unsigned int i=0; i < nsignal; i++) {
469 dedt_filt[i] = kalman.
Xest[i*state_size];
475 vpMatrix J1p = task.getTaskJacobianPseudoInverse();
476 v2 = - J1p*dedt_filt;
500 flog << v[0] <<
" " << v[1] <<
" " << v[2] <<
" "
501 << v[3] <<
" " << v[4] <<
" " << v[5] <<
" ";
506 flog << task.error[0] <<
" " << task.error[1] <<
" ";
513 flog << dedt_mes[0] <<
" " << dedt_mes[1] <<
" ";
516 flog << dedt_filt[0] <<
" " << dedt_filt[1] <<
" ";
553 vpERROR_TRACE(
"You do not have an afma4 robot or a firewire framegrabber connected to your computer...");