ViSP  2.6.2
vpSimulatorAfma6.cpp
1 /****************************************************************************
2  *
3  * $Id: vpSimulatorAfma6.cpp 2595 2010-06-02 08:50:59Z nmelchio $
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  * Class which provides a simulator for the robot Afma6.
36  *
37  * Authors:
38  * Nicolas Melchior
39  *
40  *****************************************************************************/
41 
42 
43 
44 #include <visp/vpConfig.h>
45 #if defined(WIN32) || defined(VISP_HAVE_PTHREAD)
46 #include <visp/vpSimulatorAfma6.h>
47 #include <visp/vpTime.h>
48 #include <visp/vpImagePoint.h>
49 #include <visp/vpPoint.h>
50 #include <visp/vpMeterPixelConversion.h>
51 #include <visp/vpRobotException.h>
52 #include <visp/vpIoTools.h>
53 #include <cmath> // std::fabs
54 #include <limits> // numeric_limits
55 #include <string>
57 
58 
63 {
64  init();
65  initDisplay();
66 
68 
69  #if defined(WIN32)
70  mutex_fMi = CreateMutex(NULL,FALSE,NULL);
71  mutex_artVel = CreateMutex(NULL,FALSE,NULL);
72  mutex_artCoord = CreateMutex(NULL,FALSE,NULL);
73  mutex_velocity = CreateMutex(NULL,FALSE,NULL);
74  mutex_display = CreateMutex(NULL,FALSE,NULL);
75 
76 
77  DWORD dwThreadIdArray;
78  hThread = CreateThread(
79  NULL, // default security attributes
80  0, // use default stack size
81  launcher, // thread function name
82  this, // argument to thread function
83  0, // use default creation flags
84  &dwThreadIdArray); // returns the thread identifier
85  #elif defined (VISP_HAVE_PTHREAD)
86  pthread_mutex_init(&mutex_fMi, NULL);
87  pthread_mutex_init(&mutex_artVel, NULL);
88  pthread_mutex_init(&mutex_artCoord, NULL);
89  pthread_mutex_init(&mutex_velocity, NULL);
90  pthread_mutex_init(&mutex_display, NULL);
91 
92  pthread_attr_init(&attr);
93  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
94 
95  pthread_create(&thread, NULL, launcher, (void *)this);
96  #endif
97 
98  compute_fMi();
99 }
100 
105 {
106  init();
107  initDisplay();
108 
110 
111  #if defined(WIN32)
112  mutex_fMi = CreateMutex(NULL,FALSE,NULL);
113  mutex_artVel = CreateMutex(NULL,FALSE,NULL);
114  mutex_artCoord = CreateMutex(NULL,FALSE,NULL);
115  mutex_velocity = CreateMutex(NULL,FALSE,NULL);
116  mutex_display = CreateMutex(NULL,FALSE,NULL);
117 
118 
119  DWORD dwThreadIdArray;
120  hThread = CreateThread(
121  NULL, // default security attributes
122  0, // use default stack size
123  launcher, // thread function name
124  this, // argument to thread function
125  0, // use default creation flags
126  &dwThreadIdArray); // returns the thread identifier
127  #elif defined(VISP_HAVE_PTHREAD)
128  pthread_mutex_init(&mutex_fMi, NULL);
129  pthread_mutex_init(&mutex_artVel, NULL);
130  pthread_mutex_init(&mutex_artCoord, NULL);
131  pthread_mutex_init(&mutex_velocity, NULL);
132  pthread_mutex_init(&mutex_display, NULL);
133 
134  pthread_attr_init(&attr);
135  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
136 
137  pthread_create(&thread, NULL, launcher, (void *)this);
138  #endif
139 
140  compute_fMi();
141 }
142 
147 {
148  robotStop = true;
149 
150  #if defined(WIN32)
151  WaitForSingleObject(hThread,INFINITE);
152  CloseHandle(hThread);
153  CloseHandle(mutex_fMi);
154  CloseHandle(mutex_artVel);
155  CloseHandle(mutex_artCoord);
156  CloseHandle(mutex_velocity);
157  CloseHandle(mutex_display);
158  #elif defined(VISP_HAVE_PTHREAD)
159  pthread_attr_destroy(&attr);
160  pthread_join(thread, NULL);
161  pthread_mutex_destroy(&mutex_fMi);
162  pthread_mutex_destroy(&mutex_artVel);
163  pthread_mutex_destroy(&mutex_artCoord);
164  pthread_mutex_destroy(&mutex_velocity);
165  pthread_mutex_destroy(&mutex_display);
166  #endif
167 
168  if (robotArms != NULL)
169  {
170  for(int i = 0; i < 6; i++)
171  free_Bound_scene (&(robotArms[i]));
172  }
173 
174  delete[] robotArms;
175  delete[] fMi;
176 }
177 
186 void
188 {
189  // set arm_dir from #define VISP_ROBOT_ARMS_DIR if it exists
190  if (vpIoTools::checkDirectory(VISP_ROBOT_ARMS_DIR) == true) // directory exists
191  arm_dir = VISP_ROBOT_ARMS_DIR;
192  else {
193  try {
194  arm_dir = vpIoTools::getenv("VISP_ROBOT_ARMS_DIR");
195  std::cout << "The simulator uses data from VISP_ROBOT_ARMS_DIR=" << arm_dir << std::endl;
196  }
197  catch (...) {
198  std::cout << "Cannot get VISP_ROBOT_ARMS_DIR environment variable" << std::endl;
199  }
200  }
201 
202  this->init(vpAfma6::TOOL_CCMOP);
203  toolCustom = false;
204 
205  size_fMi = 8;
206  fMi = new vpHomogeneousMatrix[8];
209 
210  zeroPos.resize(njoint);
211  zeroPos = 0;
212  reposPos.resize(njoint);
213  reposPos = 0;
214  reposPos[1] = -M_PI/2; reposPos[2] = M_PI; reposPos[4] = M_PI/2;
215 
216  artCoord = zeroPos;
217  artVel = 0;
218 
219  q_prev_getdis.resize(njoint);
220  q_prev_getdis = 0;
221  first_time_getdis = true;
222 
223  positioningVelocity = defaultPositioningVelocity ;
224 
227 
228  // Software joint limits in radians
229  //_joint_min.resize(njoint);
230  _joint_min[0] = -0.6501;
231  _joint_min[1] = -0.6001;
232  _joint_min[2] = -0.5001;
233  _joint_min[3] = -2.7301;
234  _joint_min[4] = -0.1001;
235  _joint_min[5] = -1.5901;
236  //_joint_max.resize(njoint);
237  _joint_max[0] = 0.7001;
238  _joint_max[1] = 0.5201;
239  _joint_max[2] = 0.4601;
240  _joint_max[3] = 2.7301;
241  _joint_max[4] = 2.4801;
242  _joint_max[5] = 1.5901;
243 }
244 
248 void
250 {
251  robotArms = NULL;
252  robotArms = new Bound_scene[6];
253  initArms();
255  cameraParam.initPersProjWithoutDistortion (558.5309599, 556.055053, 320, 240);
257  vpCameraParameters tmp;
258  getCameraParameters(tmp,640,480);
259  px_int = tmp.get_px();
260  py_int = tmp.get_py();
261  sceneInitialized = true;
262 }
263 
264 
280 void
283 {
284  this->projModel = projModel;
285 
286  // Use here default values of the robot constant parameters.
287  switch (tool) {
288  case vpAfma6::TOOL_CCMOP: {
289  _erc[0] = vpMath::rad(164.35); // rx
290  _erc[1] = vpMath::rad( 89.64); // ry
291  _erc[2] = vpMath::rad(-73.05); // rz
292  _etc[0] = 0.0117; // tx
293  _etc[1] = 0.0033; // ty
294  _etc[2] = 0.2272; // tz
295 
296  setCameraParameters(vpCameraParameters(1109.5735473989, 1112.1520168160, 320, 240));
297 
298  if (robotArms != NULL)
299  {
300  while (get_displayBusy()) vpTime::wait(2);
301  free_Bound_scene (&(robotArms[5]));
302  char name_arm[FILENAME_MAX];
303  strcpy(name_arm, arm_dir.c_str());
304  strcat(name_arm,"/afma6_tool_ccmop.bnd");
305  set_scene(name_arm, robotArms+5, 1.0);
306  set_displayBusy(false);
307  }
308  break;
309  }
310  case vpAfma6::TOOL_GRIPPER: {
311  _erc[0] = vpMath::rad( 88.33); // rx
312  _erc[1] = vpMath::rad( 72.07); // ry
313  _erc[2] = vpMath::rad( 2.53); // rz
314  _etc[0] = 0.0783; // tx
315  _etc[1] = 0.1234; // ty
316  _etc[2] = 0.1638; // tz
317 
318  setCameraParameters(vpCameraParameters(852.6583228197, 854.8084224761, 320, 240));
319 
320  if (robotArms != NULL)
321  {
322  while (get_displayBusy()) vpTime::wait(2);
323  free_Bound_scene (&(robotArms[5]));
324  char name_arm[FILENAME_MAX];
325  strcpy(name_arm, arm_dir.c_str());
326  strcat(name_arm,"/afma6_tool_gripper.bnd");
327  set_scene(name_arm, robotArms+5, 1.0);
328  set_displayBusy(false);
329  }
330  break;
331  }
332  case vpAfma6::TOOL_VACUUM: {
333  _erc[0] = vpMath::rad( 90.40); // rx
334  _erc[1] = vpMath::rad( 75.11); // ry
335  _erc[2] = vpMath::rad( 0.18); // rz
336  _etc[0] = 0.0038; // tx
337  _etc[1] = 0.1281; // ty
338  _etc[2] = 0.1658; // tz
339 
340  setCameraParameters(vpCameraParameters(853.4876600807, 856.0339170706, 320, 240));
341 
342  if (robotArms != NULL)
343  {
344  while (get_displayBusy()) vpTime::wait(2);
345  free_Bound_scene (&(robotArms[5]));
346  char name_arm[FILENAME_MAX];
347  strcpy(name_arm, arm_dir.c_str());
348  strcat(name_arm,"/afma6_tool_vacuum.bnd");
349  set_scene(name_arm, robotArms+5, 1.0);
350  set_displayBusy(false);
351  }
352  break;
353  }
355  std::cout << "The generic camera is not handled in vpSimulatorAfma6.cpp" << std::endl;
356  }
357  }
358 
359  vpRotationMatrix eRc(_erc);
360  this->_eMc.buildFrom(_etc, eRc);
361 
362  setToolType(tool);
363  return ;
364 }
365 
376 void
378  const unsigned int &image_width,
379  const unsigned int &image_height)
380 {
381  if (toolCustom)
382  {
383  vpCameraParameters(px_int,py_int,image_width/2,image_height/2);
384  }
385  // Set default parameters
386  switch (getToolType()) {
387  case vpAfma6::TOOL_CCMOP: {
388  // Set default intrinsic camera parameters for 640x480 images
389  if (image_width == 640 && image_height == 480)
390  {
391  std::cout << "Get default camera parameters for camera \""
392  << vpAfma6::CONST_CCMOP_CAMERA_NAME << "\"" << std::endl;
393  cam.initPersProjWithoutDistortion(1109.5735473989, 1112.1520168160, 320, 240);
394  }
395  else {
396  vpTRACE("Cannot get default intrinsic camera parameters for this image resolution");
397  }
398  break;
399  }
400  case vpAfma6::TOOL_GRIPPER: {
401  // Set default intrinsic camera parameters for 640x480 images
402  if (image_width == 640 && image_height == 480) {
403  std::cout << "Get default camera parameters for camera \""
404  << vpAfma6::CONST_GRIPPER_CAMERA_NAME << "\"" << std::endl;
405  cam.initPersProjWithoutDistortion(852.6583228197, 854.8084224761, 320, 240);
406  }
407  else {
408  vpTRACE("Cannot get default intrinsic camera parameters for this image resolution");
409  }
410  break;
411  }
412  case vpAfma6::TOOL_VACUUM: {
413  std::cout << "The generic camera is not handled in vpSimulatorAfma6.cpp" << std::endl;
414  break;
415  }
416  default:
417  vpERROR_TRACE ("This error should not occur!");
418  break;
419  }
420  return;
421 }
422 
431 void
433  const vpImage<unsigned char> &I)
434 {
436 }
437 
446 void
448  const vpImage<vpRGBa> &I)
449 {
451 }
452 
453 
459 void
461 {
462  px_int = cam.get_px();
463  py_int = cam.get_py();
464  toolCustom = true;
465 }
466 
467 
471 void
473 {
474  double tcur_1 = tcur;// temporary variable used to store the last time since the last command
475 
476  while (!robotStop)
477  {
478  //Get current time
479  tprev = tcur_1;
481 
483  setVelocityCalled = false;
484 
486 
487  double ellapsedTime = tcur - tprev;
488  if(constantSamplingTimeMode){//if we want a constant velocity, we force the ellapsed time to the given samplingTime
489  ellapsedTime = samplingTime;
490  }
491 
492  vpColVector articularCoordinates = get_artCoord();
493  articularCoordinates = get_artCoord();
494  vpColVector articularVelocities = get_artVel();
495 
496  if (jointLimit)
497  {
498  double art = articularCoordinates[jointLimitArt-1] + ellapsedTime*articularVelocities[jointLimitArt-1]*1e-3;
499  if (art <= _joint_min[jointLimitArt-1] || art >= _joint_max[jointLimitArt-1])
500  articularVelocities = 0.0;
501  else
502  jointLimit = false;
503  }
504 
505  articularCoordinates[0] = articularCoordinates[0] + ellapsedTime*articularVelocities[0]*1e-3;
506  articularCoordinates[1] = articularCoordinates[1] + ellapsedTime*articularVelocities[1]*1e-3;
507  articularCoordinates[2] = articularCoordinates[2] + ellapsedTime*articularVelocities[2]*1e-3;
508  articularCoordinates[3] = articularCoordinates[3] + ellapsedTime*articularVelocities[3]*1e-3;
509  articularCoordinates[4] = articularCoordinates[4] + ellapsedTime*articularVelocities[4]*1e-3;
510  articularCoordinates[5] = articularCoordinates[5] + ellapsedTime*articularVelocities[5]*1e-3;
511 
512  int jl = isInJointLimit();
513 
514  if (jl != 0 && jointLimit == false)
515  {
516  if (jl < 0)
517  ellapsedTime = (_joint_min[(unsigned int)(-jl-1)] - articularCoordinates[(unsigned int)(-jl-1)])/(articularVelocities[(unsigned int)(-jl-1)]*1e-3);
518  else
519  ellapsedTime = (_joint_max[(unsigned int)(jl-1)] - articularCoordinates[(unsigned int)(jl-1)])/(articularVelocities[(unsigned int)(jl-1)]*1e-3);
520 
521  for (unsigned int i = 0; i < 6; i++)
522  articularCoordinates[i] = articularCoordinates[i] + ellapsedTime*articularVelocities[i]*1e-3;
523 
524  jointLimit = true;
525  jointLimitArt = (unsigned int)fabs((double)jl);
526  }
527 
528  set_artCoord(articularCoordinates);
529  set_artVel(articularVelocities);
530 
531  compute_fMi();
532 
533  if (displayAllowed)
534  {
538  }
539 
541  {
542  while (get_displayBusy()) vpTime::wait(2);
544  set_displayBusy(false);
545  }
546 
547 
548  if (0/*displayType == MODEL_DH && displayAllowed*/)
549  {
550  vpHomogeneousMatrix fMit[8];
551  get_fMi(fMit);
552 
553  //vpDisplay::displayFrame(I,getExternalCameraPosition ()*fMi[6],cameraParam,0.2,vpColor::none);
554 
555  vpImagePoint iP, iP_1;
556  vpPoint pt;
557  pt.setWorldCoordinates (0,0,0);
558 
561  pt.track(getExternalCameraPosition ()*fMit[0]);
564  for (unsigned int k = 1; k < 7; k++)
565  {
566  pt.track(getExternalCameraPosition ()*fMit[k-1]);
568 
569  pt.track(getExternalCameraPosition ()*fMit[k]);
571 
573  }
575  }
576 
578 
579 
581  tcur_1 = tcur;
582  }else{
583  vpTime::wait(tcur, 4);
584  }
585  }
586 }
587 
595 void
597 {
598  //vpColVector q = get_artCoord();
599  vpColVector q(6);//; = get_artCoord();
600  q = get_artCoord();
601 
602  vpHomogeneousMatrix fMit[8];
603 
604  double q1 = q[0];
605  double q2 = q[1];
606  double q3 = q[2];
607  double q4 = q[3];
608  double q5 = q[4];
609  double q6 = q[5];
610 
611  double c4 = cos(q4);
612  double s4 = sin(q4);
613  double c5 = cos(q5);
614  double s5 = sin(q5);
615  double c6 = cos(q6);
616  double s6 = sin(q6);
617 
618  fMit[0][0][0] = 1;
619  fMit[0][1][0] = 0;
620  fMit[0][2][0] = 0;
621  fMit[0][0][1] = 0;
622  fMit[0][1][1] = 1;
623  fMit[0][2][1] = 0;
624  fMit[0][0][2] = 0;
625  fMit[0][1][2] = 0;
626  fMit[0][2][2] = 1;
627  fMit[0][0][3] = q1;
628  fMit[0][1][3] = 0;
629  fMit[0][2][3] = 0;
630 
631  fMit[1][0][0] = 1;
632  fMit[1][1][0] = 0;
633  fMit[1][2][0] = 0;
634  fMit[1][0][1] = 0;
635  fMit[1][1][1] = 1;
636  fMit[1][2][1] = 0;
637  fMit[1][0][2] = 0;
638  fMit[1][1][2] = 0;
639  fMit[1][2][2] = 1;
640  fMit[1][0][3] = q1;
641  fMit[1][1][3] = q2;
642  fMit[1][2][3] = 0;
643 
644  fMit[2][0][0] = 1;
645  fMit[2][1][0] = 0;
646  fMit[2][2][0] = 0;
647  fMit[2][0][1] = 0;
648  fMit[2][1][1] = 1;
649  fMit[2][2][1] = 0;
650  fMit[2][0][2] = 0;
651  fMit[2][1][2] = 0;
652  fMit[2][2][2] = 1;
653  fMit[2][0][3] = q1;
654  fMit[2][1][3] = q2;
655  fMit[2][2][3] = q3;
656 
657  fMit[3][0][0] = s4;
658  fMit[3][1][0] = -c4;
659  fMit[3][2][0] = 0;
660  fMit[3][0][1] = c4;
661  fMit[3][1][1] = s4;
662  fMit[3][2][1] = 0;
663  fMit[3][0][2] = 0;
664  fMit[3][1][2] = 0;
665  fMit[3][2][2] = 1;
666  fMit[3][0][3] = q1;
667  fMit[3][1][3] = q2;
668  fMit[3][2][3] = q3;
669 
670  fMit[4][0][0] = s4*s5;
671  fMit[4][1][0] = -c4*s5;
672  fMit[4][2][0] = c5;
673  fMit[4][0][1] = s4*c5;
674  fMit[4][1][1] = -c4*c5;
675  fMit[4][2][1] = -s5;
676  fMit[4][0][2] = c4;
677  fMit[4][1][2] = s4;
678  fMit[4][2][2] = 0;
679  fMit[4][0][3] = c4*this->_long_56+q1;
680  fMit[4][1][3] = s4*this->_long_56+q2;
681  fMit[4][2][3] = q3;
682 
683  fMit[5][0][0] = s4*s5*c6+c4*s6;
684  fMit[5][1][0] = -c4*s5*c6+s4*s6;
685  fMit[5][2][0] = c5*c6;
686  fMit[5][0][1] = -s4*s5*s6+c4*c6;
687  fMit[5][1][1] = c4*s5*s6+s4*c6;
688  fMit[5][2][1] = -c5*s6;
689  fMit[5][0][2] = -s4*c5;
690  fMit[5][1][2] = c4*c5;
691  fMit[5][2][2] = s5;
692  fMit[5][0][3] = c4*this->_long_56+q1;
693  fMit[5][1][3] = s4*this->_long_56+q2;
694  fMit[5][2][3] = q3;
695 
696  fMit[6][0][0] = fMit[5][0][0];
697  fMit[6][1][0] = fMit[5][1][0];
698  fMit[6][2][0] = fMit[5][2][0];
699  fMit[6][0][1] = fMit[5][0][1];
700  fMit[6][1][1] = fMit[5][1][1];
701  fMit[6][2][1] = fMit[5][2][1];
702  fMit[6][0][2] = fMit[5][0][2];
703  fMit[6][1][2] = fMit[5][1][2];
704  fMit[6][2][2] = fMit[5][2][2];
705  fMit[6][0][3] = fMit[5][0][3];
706  fMit[6][1][3] = fMit[5][1][3];
707  fMit[6][2][3] = fMit[5][2][3];
708 
709 // vpHomogeneousMatrix cMe;
710 // get_cMe(cMe);
711 // cMe = cMe.inverse();
712 // fMit[7] = fMit[6] * cMe;
713  get_fMc(q,fMit[7]);
714 
715  #if defined(WIN32)
716  WaitForSingleObject(mutex_fMi,INFINITE);
717  for (int i = 0; i < 8; i++)
718  fMi[i] = fMit[i];
719  ReleaseMutex(mutex_fMi);
720  #elif defined(VISP_HAVE_PTHREAD)
721  pthread_mutex_lock (&mutex_fMi);
722  for (int i = 0; i < 8; i++)
723  fMi[i] = fMit[i];
724  pthread_mutex_unlock (&mutex_fMi);
725  #endif
726 }
727 
728 
736 {
737  switch (newState) {
738  case vpRobot::STATE_STOP: {
739  // Start primitive STOP only if the current state is Velocity
741  stopMotion();
742  }
743  break;
744  }
747  std::cout << "Change the control mode from velocity to position control.\n";
748  stopMotion();
749  }
750  else {
751  //std::cout << "Change the control mode from stop to position control.\n";
752  }
753  break;
754  }
757  std::cout << "Change the control mode from stop to velocity control.\n";
758  }
759  break;
760  }
762  default:
763  break ;
764  }
765 
766  return vpRobot::setRobotState (newState);
767 }
768 
838 void
840 {
842  vpERROR_TRACE ("Cannot send a velocity to the robot "
843  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
845  "Cannot send a velocity to the robot "
846  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
847  }
848 
849  vpColVector vel_sat(6);
850 
851  double scale_trans_sat = 1;
852  double scale_rot_sat = 1;
853  double scale_sat = 1;
854  double vel_trans_max = getMaxTranslationVelocity();
855  double vel_rot_max = getMaxRotationVelocity();
856 
857  double vel_abs; // Absolute value
858 
859  // Velocity saturation
860  switch(frame)
861  {
862  // saturation in cartesian space
863  case vpRobot::CAMERA_FRAME :
865  {
866  if (vel.getRows() != 6)
867  {
868  vpERROR_TRACE ("The velocity vector must have a size of 6 !!!!");
869  throw;
870  }
871 
872  for (unsigned int i = 0 ; i < 3; ++ i)
873  {
874  vel_abs = fabs (vel[i]);
875  if (vel_abs > vel_trans_max && !jointLimit)
876  {
877  vel_trans_max = vel_abs;
878  vpERROR_TRACE ("Excess velocity %g m/s in TRANSLATION "
879  "(axis nr. %d).", vel[i], i+1);
880  }
881 
882  vel_abs = fabs (vel[i+3]);
883  if (vel_abs > vel_rot_max && !jointLimit) {
884  vel_rot_max = vel_abs;
885  vpERROR_TRACE ("Excess velocity %g rad/s in ROTATION "
886  "(axis nr. %d).", vel[i+3], i+4);
887  }
888  }
889 
890  if (vel_trans_max > getMaxTranslationVelocity())
891  scale_trans_sat = getMaxTranslationVelocity() / vel_trans_max;
892 
893  if (vel_rot_max > getMaxRotationVelocity())
894  scale_rot_sat = getMaxRotationVelocity() / vel_rot_max;
895 
896  if ( (scale_trans_sat < 1) || (scale_rot_sat < 1) )
897  {
898  if (scale_trans_sat < scale_rot_sat)
899  scale_sat = scale_trans_sat;
900  else
901  scale_sat = scale_rot_sat;
902  }
903  break;
904  }
905 
906  // saturation in joint space
908  {
909  if (vel.getRows() != 6)
910  {
911  vpERROR_TRACE ("The velocity vector must have a size of 6 !!!!");
912  throw;
913  }
914  for (unsigned int i = 0 ; i < 6; ++ i)
915  {
916  vel_abs = fabs (vel[i]);
917  if (vel_abs > vel_rot_max && !jointLimit)
918  {
919  vel_rot_max = vel_abs;
920  vpERROR_TRACE ("Excess velocity %g rad/s in ROTATION "
921  "(axis nr. %d).", vel[i], i+1);
922  }
923  }
924  if (vel_rot_max > getMaxRotationVelocity())
925  scale_rot_sat = getMaxRotationVelocity() / vel_rot_max;
926  if ( scale_rot_sat < 1 )
927  scale_sat = scale_rot_sat;
928  break;
929  }
930  case vpRobot::MIXT_FRAME :
931  {
932  break;
933  }
934  }
935 
936  set_velocity (vel * scale_sat);
937  setRobotFrame (frame);
938  setVelocityCalled = true;
939 }
940 
941 
945 void
947 {
949 
950  double scale_rot_sat = 1;
951  double scale_sat = 1;
952  double vel_rot_max = getMaxRotationVelocity();
953  double vel_abs;
954 
955  vpColVector articularCoordinates = get_artCoord();
956  vpColVector velocityframe = get_velocity();
957  vpColVector articularVelocity;
958 
959  switch(frame)
960  {
961  case vpRobot::CAMERA_FRAME :
962  {
963  vpMatrix eJe;
965  vpAfma6::get_eJe(articularCoordinates,eJe);
966  eJe = eJe.pseudoInverse();
968  singularityTest(articularCoordinates,eJe);
969  articularVelocity = eJe*eVc*velocityframe;
970  set_artVel (articularVelocity);
971  break;
972  }
974  {
975  vpMatrix fJe;
976  vpAfma6::get_fJe(articularCoordinates,fJe);
977  fJe = fJe.pseudoInverse();
979  singularityTest(articularCoordinates,fJe);
980  articularVelocity = fJe*velocityframe;
981  set_artVel (articularVelocity);
982  break;
983  }
985  {
986  articularVelocity = velocityframe;
987  set_artVel (articularVelocity);
988  break;
989  }
990  case vpRobot::MIXT_FRAME :
991  {
992  break;
993  }
994  }
995 
996 
997 
998  switch(frame)
999  {
1000  case vpRobot::CAMERA_FRAME :
1002  {
1003  for (unsigned int i = 0 ; i < 6; ++ i)
1004  {
1005  vel_abs = fabs (articularVelocity[i]);
1006  if (vel_abs > vel_rot_max && !jointLimit)
1007  {
1008  vel_rot_max = vel_abs;
1009  vpERROR_TRACE ("Excess velocity %g rad/s in ROTATION "
1010  "(axis nr. %d).", articularVelocity[i], i+1);
1011  }
1012  }
1013  if (vel_rot_max > getMaxRotationVelocity())
1014  scale_rot_sat = getMaxRotationVelocity() / vel_rot_max;
1015  if ( scale_rot_sat < 1 )
1016  scale_sat = scale_rot_sat;
1017 
1018  set_artVel(articularVelocity * scale_sat);
1019  break;
1020  }
1022  case vpRobot::MIXT_FRAME :
1023  {
1024  break;
1025  }
1026  }
1027 }
1028 
1029 
1076 void
1078 {
1079  vel.resize(6);
1080 
1081  vpColVector articularCoordinates = get_artCoord();
1082  vpColVector articularVelocity = get_artVel();
1083 
1084  switch(frame)
1085  {
1086  case vpRobot::CAMERA_FRAME :
1087  {
1088  vpMatrix eJe;
1090  vpAfma6::get_eJe(articularCoordinates,eJe);
1091  vel = cVe*eJe*articularVelocity;
1092  break ;
1093  }
1094  case vpRobot::ARTICULAR_FRAME :
1095  {
1096  vel = articularVelocity;
1097  break ;
1098  }
1099  case vpRobot::REFERENCE_FRAME :
1100  {
1101  vpMatrix fJe;
1102  vpAfma6::get_fJe(articularCoordinates,fJe);
1103  vel = fJe*articularVelocity;
1104  break ;
1105  }
1106  case vpRobot::MIXT_FRAME :
1107  {
1108  break ;
1109  }
1110  default:
1111  {
1112  vpERROR_TRACE ("Error in spec of vpRobot. "
1113  "Case not taken in account.");
1114  return;
1115  }
1116  }
1117 }
1118 
1163 {
1164  vpColVector velocity(6);
1165  getVelocity (frame, velocity);
1166 
1167  return velocity;
1168 }
1169 
1170 
1171 void
1173 {
1174  double vel_rot_max = getMaxRotationVelocity();
1175  double velmax = fabs(q[0]);
1176  for (unsigned int i = 1; i < 6; i++)
1177  {
1178  if (velmax < fabs(q[i]))
1179  velmax = fabs(q[i]);
1180  }
1181 
1182  double alpha = (getPositioningVelocity() * vel_rot_max) / (velmax*100);
1183  q = q * alpha;
1184 }
1185 
1260 void
1262 {
1264  {
1265  vpERROR_TRACE ("Robot was not in position-based control\n"
1266  "Modification of the robot state");
1267  // setRobotState(vpRobot::STATE_POSITION_CONTROL) ;
1268  }
1269 
1270  vpColVector articularCoordinates = get_artCoord();
1271 
1272  vpColVector error(6);
1273  double errsqr = 0;
1274  switch(frame)
1275  {
1276  case vpRobot::CAMERA_FRAME :
1277  {
1278  int nbSol;
1279  vpColVector qdes(6);
1280 
1281  vpTranslationVector txyz;
1282  vpRxyzVector rxyz;
1283  for (unsigned int i=0; i < 3; i++)
1284  {
1285  txyz[i] = q[i];
1286  rxyz[i] = q[i+3];
1287  }
1288 
1289  vpRotationMatrix cRc2(rxyz);
1290  vpHomogeneousMatrix cMc2(txyz, cRc2);
1291 
1293  vpAfma6::get_fMc(articularCoordinates, fMc);
1294 
1295  vpHomogeneousMatrix fMc2 = fMc * cMc2;
1296 
1297  do
1298  {
1299  articularCoordinates = get_artCoord();
1300  qdes = articularCoordinates;
1301  nbSol = getInverseKinematics(fMc2, qdes);
1302  setVelocityCalled = true;
1303  if (nbSol > 0)
1304  {
1305  error = qdes - articularCoordinates;
1306  errsqr = error.sumSquare();
1307  //findHighestPositioningSpeed(error);
1308  set_artVel(error);
1309  if (errsqr < 1e-4)
1310  {
1311  set_artCoord (qdes);
1312  error = 0;
1313  set_artVel(error);
1314  set_velocity(error);
1315  break;
1316  }
1317  }
1318  else
1319  {
1320  vpERROR_TRACE ("Positionning error.");
1322  "Position out of range.");
1323  }
1324  }while (errsqr > 1e-8 && nbSol > 0);
1325 
1326  break ;
1327  }
1328 
1330  {
1331  do
1332  {
1333  articularCoordinates = get_artCoord();
1334  error = q - articularCoordinates;
1335  errsqr = error.sumSquare();
1336  //findHighestPositioningSpeed(error);
1337  set_artVel(error);
1338  setVelocityCalled = true;
1339  if (errsqr < 1e-4)
1340  {
1341  set_artCoord (q);
1342  error = 0;
1343  set_artVel(error);
1344  set_velocity(error);
1345  break;
1346  }
1347  }while (errsqr > 1e-8);
1348  break ;
1349  }
1350 
1352  {
1353  int nbSol;
1354  vpColVector qdes(6);
1355 
1356  vpTranslationVector txyz;
1357  vpRxyzVector rxyz;
1358  for (unsigned int i=0; i < 3; i++)
1359  {
1360  txyz[i] = q[i];
1361  rxyz[i] = q[i+3];
1362  }
1363 
1364  vpRotationMatrix fRc(rxyz);
1365  vpHomogeneousMatrix fMc(txyz, fRc);
1366 
1367  do
1368  {
1369  articularCoordinates = get_artCoord();
1370  qdes = articularCoordinates;
1371  nbSol = getInverseKinematics(fMc, qdes);
1372  setVelocityCalled = true;
1373  if (nbSol > 0)
1374  {
1375  error = qdes - articularCoordinates;
1376  errsqr = error.sumSquare();
1377  //findHighestPositioningSpeed(error);
1378  set_artVel(error);
1379  if (errsqr < 1e-4)
1380  {
1381  set_artCoord (qdes);
1382  error = 0;
1383  set_artVel(error);
1384  set_velocity(error);
1385  break;
1386  }
1387  }
1388  else
1389  vpERROR_TRACE ("Positionning error. Position unreachable");
1390  }while (errsqr > 1e-8 && nbSol > 0);
1391  break ;
1392  }
1393  case vpRobot::MIXT_FRAME:
1394  {
1395  vpERROR_TRACE ("Positionning error. Mixt frame not implemented");
1397  "Positionning error: "
1398  "Mixt frame not implemented.");
1399  break ;
1400  }
1401  }
1402 }
1403 
1468  const double pos1,
1469  const double pos2,
1470  const double pos3,
1471  const double pos4,
1472  const double pos5,
1473  const double pos6)
1474 {
1475  try{
1476  vpColVector position(6) ;
1477  position[0] = pos1 ;
1478  position[1] = pos2 ;
1479  position[2] = pos3 ;
1480  position[3] = pos4 ;
1481  position[4] = pos5 ;
1482  position[5] = pos6 ;
1483 
1484  setPosition(frame, position) ;
1485  }
1486  catch(...)
1487  {
1488  vpERROR_TRACE("Error caught");
1489  throw ;
1490  }
1491 }
1492 
1528 void vpSimulatorAfma6::setPosition(const char *filename)
1529 {
1530  vpColVector q;
1531  bool ret;
1532 
1533  ret = this->readPosFile(filename, q);
1534 
1535  if (ret == false) {
1536  vpERROR_TRACE ("Bad position in \"%s\"", filename);
1538  "Bad position in filename.");
1539  }
1542 }
1543 
1603 void
1605 {
1606  q.resize(6);
1607 
1608  switch(frame)
1609  {
1610  case vpRobot::CAMERA_FRAME :
1611  {
1612  q = 0;
1613  break ;
1614  }
1615 
1617  {
1618  q = get_artCoord();
1619  break ;
1620  }
1621 
1623  {
1625  get_fMc (get_artCoord(), fMc);
1626 
1627  vpRotationMatrix fRc;
1628  fMc.extract(fRc);
1629  vpRxyzVector rxyz(fRc);
1630 
1631  vpTranslationVector txyz;
1632  fMc.extract(txyz);
1633 
1634  for (unsigned int i=0; i <3; i++)
1635  {
1636  q[i] = txyz[i];
1637  q[i+3] = rxyz[i];
1638  }
1639  break ;
1640  }
1641 
1642  case vpRobot::MIXT_FRAME:
1643  {
1644  vpERROR_TRACE ("Positionning error. Mixt frame not implemented");
1646  "Positionning error: "
1647  "Mixt frame not implemented.");
1648  break ;
1649  }
1650  }
1651 }
1652 
1653 
1664 void
1666  vpPoseVector &position)
1667 {
1668  vpColVector posRxyz;
1669  //recupere position en Rxyz
1670  this->getPosition(frame,posRxyz);
1671 
1672  //recupere le vecteur thetaU correspondant
1673  vpThetaUVector RtuVect(posRxyz[3],posRxyz[4],posRxyz[5]);
1674 
1675  //remplit le vpPoseVector avec translation et rotation ThetaU
1676  for(unsigned int j=0;j<3;j++)
1677  {
1678  position[j]=posRxyz[j];
1679  position[j+3]=RtuVect[j];
1680  }
1681 }
1682 
1689 void
1691 {
1692  if (limitMin.getRows() != 6 || limitMax.getRows() != 6)
1693  {
1694  vpTRACE("Joint limit vector has not a size of 6 !");
1695  return;
1696  }
1697 
1698  _joint_min[0] = limitMin[0];
1699  _joint_min[1] = limitMin[1];
1700  _joint_min[2] = limitMin[2];
1701  _joint_min[3] = limitMin[3];
1702  _joint_min[4] = limitMin[4];
1703  _joint_min[5] = limitMin[5];
1704 
1705  _joint_max[0] = limitMax[0];
1706  _joint_max[1] = limitMax[1];
1707  _joint_max[2] = limitMax[2];
1708  _joint_max[3] = limitMax[3];
1709  _joint_max[4] = limitMax[4];
1710  _joint_max[5] = limitMax[5];
1711 
1712 }
1713 
1719 bool
1721 {
1722  double q5 = q[4];
1723 
1724  bool cond = fabs(q5-M_PI/2) < 1e-1;
1725 
1726  if(cond)
1727  {
1728  J[0][3] = 0;
1729  J[0][4] = 0;
1730  J[1][3] = 0;
1731  J[1][4] = 0;
1732  J[3][3] = 0;
1733  J[3][4] = 0;
1734  J[5][3] = 0;
1735  J[5][4] = 0;
1736  return true;
1737  }
1738 
1739  return false;
1740 }
1741 
1745 int
1747 {
1748  int artNumb = 0;
1749  double diff = 0;
1750  double difft = 0;
1751 
1752  vpColVector articularCoordinates = get_artCoord();
1753 
1754  for (unsigned int i = 0; i < 6; i++)
1755  {
1756  if (articularCoordinates[i] <= _joint_min[i])
1757  {
1758  difft = _joint_min[i] - articularCoordinates[i];
1759  if (difft > diff)
1760  {
1761  diff = difft;
1762  artNumb = -(int)i-1;
1763  }
1764  }
1765  }
1766 
1767  for (unsigned int i = 0; i < 6; i++)
1768  {
1769  if (articularCoordinates[i] >= _joint_max[i])
1770  {
1771  difft = articularCoordinates[i] - _joint_max[i];
1772  if (difft > diff)
1773  {
1774  diff = difft;
1775  artNumb = (int)(i+1);
1776  }
1777  }
1778  }
1779 
1780  if (artNumb != 0)
1781  std::cout << "\nWarning: Velocity control stopped: axis " << fabs((float)artNumb) << " on joint limit!" <<std::endl;
1782 
1783  return artNumb;
1784 }
1785 
1797 void
1799 {
1800  getDisplacement(vpRobot::CAMERA_FRAME, displacement);
1801 }
1802 
1812 void
1814 {
1816 }
1817 
1836 void
1838  vpColVector &displacement)
1839 {
1840  displacement.resize (6);
1841  displacement = 0;
1842  vpColVector q_cur(6);
1843 
1844  q_cur = get_artCoord();
1845 
1846  if ( ! first_time_getdis )
1847  {
1848  switch (frame)
1849  {
1850  case vpRobot::CAMERA_FRAME:
1851  {
1852  std::cout << "getDisplacement() CAMERA_FRAME not implemented\n";
1853  return;
1854  break ;
1855  }
1856 
1858  {
1859  displacement = q_cur - q_prev_getdis;
1860  break ;
1861  }
1862 
1864  {
1865  std::cout << "getDisplacement() REFERENCE_FRAME not implemented\n";
1866  return;
1867  break ;
1868  }
1869 
1870  case vpRobot::MIXT_FRAME:
1871  {
1872  std::cout << "getDisplacement() MIXT_FRAME not implemented\n";
1873  return;
1874  break ;
1875  }
1876  }
1877  }
1878  else
1879  {
1880  first_time_getdis = false;
1881  }
1882 
1883  // Memorize the joint position for the next call
1884  q_prev_getdis = q_cur;
1885 }
1886 
1933 bool
1935 {
1936 
1937  FILE * fd ;
1938  fd = fopen(filename, "r") ;
1939  if (fd == NULL)
1940  return false;
1941 
1942  char line[FILENAME_MAX];
1943  char dummy[FILENAME_MAX];
1944  char head[] = "R:";
1945  bool sortie = false;
1946 
1947  do {
1948  // Saut des lignes commencant par #
1949  if (fgets (line, FILENAME_MAX, fd) != NULL) {
1950  if ( strncmp (line, "#", 1) != 0) {
1951  // La ligne n'est pas un commentaire
1952  if ( strncmp (line, head, sizeof(head)-1) == 0) {
1953  sortie = true; // Position robot trouvee.
1954  }
1955 // else
1956 // return (false); // fin fichier sans position robot.
1957  }
1958  }
1959  else {
1960  return (false); /* fin fichier */
1961  }
1962 
1963  }
1964  while ( sortie != true );
1965 
1966  // Lecture des positions
1967  q.resize(njoint);
1968  sscanf(line, "%s %lf %lf %lf %lf %lf %lf",
1969  dummy,
1970  &q[0], &q[1], &q[2],
1971  &q[3], &q[4], &q[5]);
1972 
1973  // converts rotations from degrees into radians
1974  //q.deg2rad();
1975 
1976  q[3] = vpMath::rad(q[3]);
1977  q[4] = vpMath::rad(q[4]);
1978  q[5] = vpMath::rad(q[5]);
1979 
1980  fclose(fd) ;
1981  return (true);
1982 }
1983 
2006 bool
2007 vpSimulatorAfma6::savePosFile(const char *filename, const vpColVector &q)
2008 {
2009 
2010  FILE * fd ;
2011  fd = fopen(filename, "w") ;
2012  if (fd == NULL)
2013  return false;
2014 
2015  fprintf(fd, "\
2016 #AFMA6 - Position - Version 2.01\n\
2017 #\n\
2018 # R: X Y Z A B C\n\
2019 # Joint position: X, Y, Z: translations in meters\n\
2020 # A, B, C: rotations in degrees\n\
2021 #\n\
2022 #\n\n");
2023 
2024  // Save positions in mm and deg
2025  fprintf(fd, "R: %lf %lf %lf %lf %lf %lf\n",
2026  q[0],
2027  q[1],
2028  q[2],
2029  vpMath::deg(q[3]),
2030  vpMath::deg(q[4]),
2031  vpMath::deg(q[5]));
2032 
2033  fclose(fd) ;
2034  return (true);
2035 }
2036 
2044 void
2045 vpSimulatorAfma6::move(const char *filename)
2046 {
2047  vpColVector q;
2048 
2049  try
2050  {
2051  this->readPosFile(filename, q);
2054  }
2055  catch(...)
2056  {
2057  throw;
2058  }
2059 }
2060 
2070 void
2072 {
2073  vpAfma6::get_cMe(cMe) ;
2074 }
2075 
2083 void
2085 {
2086  vpHomogeneousMatrix cMe ;
2087  vpAfma6::get_cMe(cMe) ;
2088 
2089  cVe.buildFrom(cMe) ;
2090 }
2091 
2101 void
2103 {
2104  try
2105  {
2106  vpAfma6::get_eJe(get_artCoord(), eJe) ;
2107  }
2108  catch(...)
2109  {
2110  vpERROR_TRACE("catch exception ") ;
2111  throw ;
2112  }
2113 }
2114 
2125 void
2127 {
2128  try
2129  {
2130  vpColVector articularCoordinates = get_artCoord();
2131  vpAfma6::get_fJe(articularCoordinates, fJe) ;
2132  }
2133  catch(...)
2134  {
2135  vpERROR_TRACE("Error caught");
2136  throw ;
2137  }
2138 }
2139 
2143 void
2145 {
2147  return;
2148 
2149  vpColVector stop(6);
2150  stop = 0;
2151  set_artVel(stop);
2152  set_velocity(stop);
2154 }
2155 
2156 
2157 
2158 /**********************************************************************************/
2159 /**********************************************************************************/
2160 /**********************************************************************************/
2161 /**********************************************************************************/
2162 
2171 void
2173 {
2174  // set scene_dir from #define VISP_SCENE_DIR if it exists
2175  std::string scene_dir;
2176  if (vpIoTools::checkDirectory(VISP_SCENES_DIR) == true) // directory exists
2177  scene_dir = VISP_SCENES_DIR;
2178  else {
2179  try {
2180  scene_dir = vpIoTools::getenv("VISP_SCENES_DIR");
2181  std::cout << "The simulator uses data from VISP_SCENES_DIR=" << scene_dir << std::endl;
2182  }
2183  catch (...) {
2184  std::cout << "Cannot get VISP_SCENES_DIR environment variable" << std::endl;
2185  }
2186  }
2187 
2188  char name_cam[FILENAME_MAX];
2189 
2190  strcpy(name_cam, scene_dir.c_str());
2191  strcat(name_cam,"/camera.bnd");
2192  set_scene(name_cam,&camera,cameraFactor);
2193 
2194  char name_arm[FILENAME_MAX];
2195  strcpy(name_arm, arm_dir.c_str());
2196  strcat(name_arm,"/afma6_gate.bnd");
2197  set_scene(name_arm, robotArms, 1.0);
2198  strcpy(name_arm, arm_dir.c_str());
2199  strcat(name_arm,"/afma6_arm1.bnd");
2200  set_scene(name_arm, robotArms+1, 1.0);
2201  strcpy(name_arm, arm_dir.c_str());
2202  strcat(name_arm,"/afma6_arm2.bnd");
2203  set_scene(name_arm, robotArms+2, 1.0);
2204  strcpy(name_arm, arm_dir.c_str());
2205  strcat(name_arm,"/afma6_arm3.bnd");
2206  set_scene(name_arm, robotArms+3, 1.0);
2207  strcpy(name_arm, arm_dir.c_str());
2208  strcat(name_arm,"/afma6_arm4.bnd");
2209  set_scene(name_arm, robotArms+4, 1.0);
2210 
2212  tool = getToolType();
2213  strcpy(name_arm, arm_dir.c_str());
2214  switch (tool) {
2215  case vpAfma6::TOOL_CCMOP: {
2216  strcat(name_arm,"/afma6_tool_ccmop.bnd");
2217  break;
2218  }
2219  case vpAfma6::TOOL_GRIPPER: {
2220  strcat(name_arm,"/afma6_tool_gripper.bnd");
2221  break;
2222  }
2223  case vpAfma6::TOOL_VACUUM: {
2224  strcat(name_arm,"/afma6_tool_vacuum.bnd");
2225  break;
2226  }
2228  std::cout << "The generic camera is not handled in vpSimulatorAfma6.cpp" << std::endl;
2229  break;
2230  }
2231  }
2232  set_scene(name_arm, robotArms+5, 1.0);
2233 
2234  add_rfstack(IS_BACK);
2235 
2236  add_vwstack ("start","depth", 0.0, 100.0);
2237  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
2238  add_vwstack ("start","type", PERSPECTIVE);
2239 //
2240 // sceneInitialized = true;
2241 // displayObject = true;
2242  displayCamera = true;
2243 }
2244 
2245 
2246 void
2248 {
2249  bool changed = false;
2250  vpHomogeneousMatrix displacement = navigation(I,changed);
2251 
2252  //if (displacement[2][3] != 0)
2253  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon())
2254  camMf2 = camMf2*displacement;
2255 
2256  f2Mf = camMf2.inverse()*camMf;
2257 
2258  camMf = camMf2* displacement * f2Mf;
2259 
2260  double u;
2261  double v;
2262  //if(px_ext != 1 && py_ext != 1)
2263  // we assume px_ext and py_ext > 0
2264  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
2265  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
2266  {
2267  u = (double)I.getWidth()/(2*px_ext);
2268  v = (double)I.getHeight()/(2*py_ext);
2269  }
2270  else
2271  {
2272  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
2273  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
2274  }
2275 
2276  float w44o[4][4],w44cext[4][4],x,y,z;
2277 
2278  vp2jlc_matrix(camMf.inverse(),w44cext);
2279 
2280  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
2281  x = w44cext[2][0] + w44cext[3][0];
2282  y = w44cext[2][1] + w44cext[3][1];
2283  z = w44cext[2][2] + w44cext[3][2];
2284  add_vwstack ("start","vrp", x,y,z);
2285  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
2286  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
2287  add_vwstack ("start","window", -u, u, -v, v);
2288 
2289  vpHomogeneousMatrix fMit[8];
2290  get_fMi(fMit);
2291 
2292  vp2jlc_matrix(vpHomogeneousMatrix(0,0,0,0,0,0),w44o);
2293  display_scene(w44o,robotArms[0],I, curColor);
2294 
2295  vp2jlc_matrix(fMit[0],w44o);
2296  display_scene(w44o,robotArms[1],I, curColor);
2297 
2298  vp2jlc_matrix(fMit[2],w44o);
2299  display_scene(w44o,robotArms[2],I, curColor);
2300 
2301  vp2jlc_matrix(fMit[3],w44o);
2302  display_scene(w44o,robotArms[3],I, curColor);
2303 
2304  vp2jlc_matrix(fMit[4],w44o);
2305  display_scene(w44o,robotArms[4],I, curColor);
2306 
2307  vp2jlc_matrix(fMit[5],w44o);
2308  display_scene(w44o,robotArms[5],I, curColor);
2309 
2310  if (displayCamera)
2311  {
2312  vpHomogeneousMatrix cMe;
2313  get_cMe(cMe);
2314  cMe = cMe.inverse();
2315  cMe = fMit[6] * cMe;
2316  vp2jlc_matrix(cMe,w44o);
2317  display_scene(w44o,camera, I, camColor);
2318  }
2319 
2320  if (displayObject)
2321  {
2322  vp2jlc_matrix(fMo,w44o);
2323  display_scene(w44o,scene,I, curColor);
2324  }
2325 }
2326 
2334 void
2336 {
2337  vpColVector stop(6);
2338  stop = 0;
2339  set_artVel(stop);
2340  set_velocity(stop);
2342  fMc = fMo * cMo.inverse();
2343 
2344  vpColVector articularCoordinates = get_artCoord();
2345  int nbSol = getInverseKinematics(fMc, articularCoordinates);
2346 
2347  if (nbSol == 0)
2348  vpERROR_TRACE ("Positionning error. Position unreachable");
2349 
2350  set_artCoord(articularCoordinates);
2351 
2352  compute_fMi();
2353 }
2354 
2362 void
2364 {
2365  vpColVector stop(6);
2366  stop = 0;
2367  set_artVel(stop);
2368  set_velocity(stop);
2369  vpHomogeneousMatrix fMit[8];
2370  get_fMi(fMit);
2371  fMo = fMit[7] * cMo;
2372 }
2373 
2384 bool
2386 {
2387  // get rid of max velocity
2388  double vMax = getMaxTranslationVelocity();
2389  double wMax = getMaxRotationVelocity();
2390  setMaxTranslationVelocity(10.*vMax);
2391  setMaxRotationVelocity(10.*wMax);
2392 
2393  vpColVector v(3),w(3),vel(6);
2394  vpHomogeneousMatrix cdMc;
2396  vpColVector err(6);err=1.;
2397  const double lambda = 5.;
2398  double t;
2399 
2401  vpMatrix eJe;
2402 
2403  unsigned int i,iter=0;
2404  while((iter++<300) & (err.euclideanNorm()>errMax))
2405  {
2406  t = vpTime::measureTimeMs();
2407 
2408  // update image
2409  if(Iint != NULL)
2410  {
2411  vpDisplay::display(*Iint);
2412  getInternalView(*Iint);
2413  vpDisplay::flush(*Iint);
2414  }
2415 
2416  // update pose error
2417  cdMc = cdMo*get_cMo().inverse();
2418  cdMc.extract(cdRc);
2419  cdMc.extract(cdTc);
2420  cdTUc.buildFrom(cdRc);
2421 
2422  // compute v,w and velocity
2423  v = -lambda*cdRc.t()*cdTc;
2424  w = -lambda*cdTUc;
2425  for(i=0;i<3;++i)
2426  {
2427  vel[i] = v[i];
2428  vel[i+3] = w[i];
2429  err[i] = cdTc[i];
2430  err[i+3] = cdTUc[i];
2431  }
2432 
2433  // update feat
2435 
2436  // wait for it
2437  vpTime::wait(t,10);
2438  }
2439  vel=0.;
2440  set_velocity(vel);
2441  set_artVel(vel);
2443  setMaxRotationVelocity(wMax);
2444 
2445  //std::cout << "setPosition: final error " << err.t() << std::endl;
2446  return(err.euclideanNorm()<= errMax);
2447 }
2448 
2449 #endif
unsigned int jointLimitArt
vpHomogeneousMatrix get_cMo()
Modelisation of Irisa's gantry robot named Afma6.
Definition: vpAfma6.h:69
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
void initialiseObjectRelativeToCamera(vpHomogeneousMatrix cMo)
vpRxyzVector _erc
Definition: vpAfma6.h:188
vpHomogeneousMatrix * fMi
vpTranslationVector _etc
Definition: vpAfma6.h:187
static const unsigned int njoint
Number of joint.
Definition: vpAfma6.h:174
void initialiseCameraRelativeToObject(vpHomogeneousMatrix cMo)
Error that can be emited by the vpRobot class and its derivates.
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:289
vpColVector get_velocity()
unsigned int getWidth() const
Definition: vpImage.h:154
void setMaxTranslationVelocity(const double maxVt)
Definition: vpRobot.cpp:196
void get_eJe(vpMatrix &eJe)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpERROR_TRACE
Definition: vpDebug.h:379
#define vpTRACE
Definition: vpDebug.h:401
void get_cVe(vpVelocityTwistMatrix &cVe)
void move(const char *filename)
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Point coordinates conversion from normalized coordinates in meter to pixel coordinates ...
vpHomogeneousMatrix get_fMc(const vpColVector &q)
Definition: vpAfma6.cpp:729
static void displayCamera(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color)
Definition: vpDisplay.cpp:482
virtual vpRobotStateType getRobotState(void)
Definition: vpRobot.h:148
vpHomogeneousMatrix navigation(vpImage< vpRGBa > &I, bool &changed)
void set_artVel(const vpColVector &vel)
static std::string getenv(const char *env)
Definition: vpIoTools.cpp:204
double getMaxTranslationVelocity(void) const
Definition: vpRobot.cpp:208
static const vpColor none
Definition: vpColor.h:177
Initialize the position controller.
Definition: vpRobot.h:71
vpAfma6ToolType
List of possible tools that can be attached to the robot end-effector.
Definition: vpAfma6.h:108
void track(const vpHomogeneousMatrix &cMo)
vpHomogeneousMatrix getExternalCameraPosition() const
static const double defaultPositioningVelocity
void getCameraDisplacement(vpColVector &displacement)
vpRotationMatrix t() const
transpose
vpControlFrameType
Definition: vpRobot.h:83
double get_py() const
void getExternalImage(vpImage< vpRGBa > &I)
vpCameraParameters::vpCameraParametersProjType projModel
Definition: vpAfma6.h:196
static double measureTimeMs()
Definition: vpTime.cpp:86
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.h:145
static int wait(double t0, double t)
Definition: vpTime.cpp:149
double sumSquare() const
return sum of the Aij^2 (for all i, for all j)
Definition: vpMatrix.cpp:758
static const vpColor green
Definition: vpColor.h:168
vpHomogeneousMatrix fMo
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1964
double getMaxRotationVelocity(void) const
Definition: vpRobot.cpp:233
vpThetaUVector buildFrom(const vpHomogeneousMatrix &M)
void set_artCoord(const vpColVector &coord)
void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &q)
vpAfma6ToolType getToolType()
Get the current tool type.
Definition: vpAfma6.h:147
vpRobot::vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
static DWORD WINAPI launcher(LPVOID lpParam)
static bool readPosFile(const char *filename, vpColVector &q)
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:152
Class that defines what is a point.
Definition: vpPoint.h:65
void setToolType(vpAfma6::vpAfma6ToolType tool)
Set the current tool type.
Definition: vpAfma6.h:172
static const char *const CONST_GRIPPER_CAMERA_NAME
Definition: vpAfma6.h:95
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:137
The vpRotationMatrix considers the particular case of a rotation matrix.
vpColVector get_artCoord()
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
void get_cMe(vpHomogeneousMatrix &cMe)
Definition: vpAfma6.cpp:845
void getDisplacement(const vpRobot::vpControlFrameType frame, vpColVector &displacement)
vpControlFrameType getRobotFrame(void)
Definition: vpRobot.h:147
bool singularityTest(const vpColVector q, vpMatrix &J)
This class aims to be a basis used to create all the simulators of robots.
Initialize the velocity controller.
Definition: vpRobot.h:70
void get_eJe(const vpColVector &q, vpMatrix &eJe)
Definition: vpAfma6.cpp:883
vpRobotStateType
Definition: vpRobot.h:66
Initialize the acceleration controller.
Definition: vpRobot.h:72
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
bool constantSamplingTimeMode
Flag used to force the sampling time in the thread computing the robot's displacement to a constant v...
vpHomogeneousMatrix f2Mf
void getArticularDisplacement(vpColVector &displacement)
unsigned int size_fMi
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:186
Bound_scene * robotArms
void set_velocity(const vpColVector &vel)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
Generic class defining intrinsic camera parameters.
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &q)
void setExternalCameraPosition(const vpHomogeneousMatrix camMf)
vpImage< vpRGBa > I
vpHomogeneousMatrix cMo
void extract(vpRotationMatrix &R) const
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.h:143
void setCameraParameters(const vpCameraParameters cam)
void getInternalView(vpImage< vpRGBa > &I)
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:148
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
vpColVector velocity
vpColVector artCoord
bool setVelocityCalled
Flag used to specify to the thread managing the robot displacements that the setVelocity() method has...
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &q)
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness=1)
Definition: vpDisplay.cpp:346
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
double get_px() const
static double rad(double deg)
Definition: vpMath.h:100
vpColVector get_artVel()
void display_scene(Matrix mat, Bound_scene &sc, vpImage< vpRGBa > &I, vpColor color)
int getInverseKinematics(const vpHomogeneousMatrix &fMc, vpColVector &q, const bool &nearest=true)
Definition: vpAfma6.cpp:555
void setMaxRotationVelocity(const double maxVr)
Definition: vpRobot.cpp:220
double getPositioningVelocity(void)
double _long_56
Definition: vpAfma6.h:183
vpHomogeneousMatrix camMf
vpCameraParameters cameraParam
vpHomogeneousMatrix fMc
static double deg(double rad)
Definition: vpMath.h:93
vpMatrix eJe
robot Jacobian expressed in the end-effector frame
Definition: vpRobot.h:113
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
double euclideanNorm() const
Definition: vpMatrix.cpp:2785
void get_fJe(vpMatrix &fJe)
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92
vpHomogeneousMatrix inverse() const
vpHomogeneousMatrix _eMc
Definition: vpAfma6.h:190
void get_fJe(const vpColVector &q, vpMatrix &fJe)
Definition: vpAfma6.cpp:950
unsigned int getHeight() const
Definition: vpImage.h:145
Class that consider the case of the Euler angle using the x-y-z convention, where are respectively ...
Definition: vpRxyzVector.h:152
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height)
vpControlFrameType setRobotFrame(vpRobot::vpControlFrameType newFrame)
Definition: vpRobot.cpp:159
void get_cMe(vpHomogeneousMatrix &cMe)
vpMatrix pseudoInverse(double svThreshold=1e-6) const
Compute the pseudo inverse of the matrix using the SVD.
Definition: vpMatrix.cpp:1810
double _joint_max[6]
Definition: vpAfma6.h:184
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
vpDisplayRobotType displayType
void setJointLimit(vpColVector limitMin, vpColVector limitMax)
void get_fMi(vpHomogeneousMatrix *fMit)
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:157
static const char *const CONST_CCMOP_CAMERA_NAME
Definition: vpAfma6.h:90
void setExternalCameraParameters(const vpCameraParameters cam)
vpMatrix fJe
robot Jacobian expressed in the robot reference frame available
Definition: vpRobot.h:117
Class that consider the case of a translation vector.
Class that consider the case of the parameterization for the rotation.
void set_displayBusy(const bool &status)
vpHomogeneousMatrix camMf2
static bool savePosFile(const char *filename, const vpColVector &q)
double _joint_min[6]
Definition: vpAfma6.h:185
void findHighestPositioningSpeed(vpColVector &q)
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94