Visual Servoing Platform  version 3.1.0
vpSimulatorAfma6.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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  * Class which provides a simulator for the robot Afma6.
33  *
34  * Authors:
35  * Nicolas Melchior
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpConfig.h>
40 #if defined(VISP_HAVE_MODULE_GUI) && ((defined(_WIN32) && !defined(WINRT_8_0)) || defined(VISP_HAVE_PTHREAD))
41 #include <cmath> // std::fabs
42 #include <limits> // numeric_limits
43 #include <string>
44 #include <visp3/core/vpImagePoint.h>
45 #include <visp3/core/vpIoTools.h>
46 #include <visp3/core/vpMeterPixelConversion.h>
47 #include <visp3/core/vpPoint.h>
48 #include <visp3/core/vpTime.h>
49 #include <visp3/robot/vpRobotException.h>
50 #include <visp3/robot/vpSimulatorAfma6.h>
51 
52 #include "../wireframe-simulator/vpBound.h"
53 #include "../wireframe-simulator/vpRfstack.h"
54 #include "../wireframe-simulator/vpScene.h"
55 #include "../wireframe-simulator/vpVwstack.h"
56 
58 
63  : vpRobotWireFrameSimulator(), vpAfma6(), q_prev_getdis(), first_time_getdis(true),
64  positioningVelocity(defaultPositioningVelocity), zeroPos(), reposPos(), toolCustom(false), arm_dir()
65 {
66  init();
67  initDisplay();
68 
70 
71 #if defined(_WIN32)
72 #ifdef WINRT_8_1
73  mutex_fMi = CreateMutexEx(NULL, NULL, 0, NULL);
74  mutex_artVel = CreateMutexEx(NULL, NULL, 0, NULL);
75  mutex_artCoord = CreateMutexEx(NULL, NULL, 0, NULL);
76  mutex_velocity = CreateMutexEx(NULL, NULL, 0, NULL);
77  mutex_display = CreateMutexEx(NULL, NULL, 0, NULL);
78 #else
79  mutex_fMi = CreateMutex(NULL, FALSE, NULL);
80  mutex_artVel = CreateMutex(NULL, FALSE, NULL);
81  mutex_artCoord = CreateMutex(NULL, FALSE, NULL);
82  mutex_velocity = CreateMutex(NULL, FALSE, NULL);
83  mutex_display = CreateMutex(NULL, FALSE, NULL);
84 #endif
85 
86  DWORD dwThreadIdArray;
87  hThread = CreateThread(NULL, // default security attributes
88  0, // use default stack size
89  launcher, // thread function name
90  this, // argument to thread function
91  0, // use default creation flags
92  &dwThreadIdArray); // returns the thread identifier
93 #elif defined(VISP_HAVE_PTHREAD)
94  pthread_mutex_init(&mutex_fMi, NULL);
95  pthread_mutex_init(&mutex_artVel, NULL);
96  pthread_mutex_init(&mutex_artCoord, NULL);
97  pthread_mutex_init(&mutex_velocity, NULL);
98  pthread_mutex_init(&mutex_display, NULL);
99 
100  pthread_attr_init(&attr);
101  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
102 
103  pthread_create(&thread, NULL, launcher, (void *)this);
104 #endif
105 
106  compute_fMi();
107 }
108 
116  : vpRobotWireFrameSimulator(do_display), q_prev_getdis(), first_time_getdis(true),
117  positioningVelocity(defaultPositioningVelocity), zeroPos(), reposPos(), toolCustom(false), arm_dir()
118 {
119  init();
120  initDisplay();
121 
123 
124 #if defined(_WIN32)
125 #ifdef WINRT_8_1
126  mutex_fMi = CreateMutexEx(NULL, NULL, 0, NULL);
127  mutex_artVel = CreateMutexEx(NULL, NULL, 0, NULL);
128  mutex_artCoord = CreateMutexEx(NULL, NULL, 0, NULL);
129  mutex_velocity = CreateMutexEx(NULL, NULL, 0, NULL);
130  mutex_display = CreateMutexEx(NULL, NULL, 0, NULL);
131 #else
132  mutex_fMi = CreateMutex(NULL, FALSE, NULL);
133  mutex_artVel = CreateMutex(NULL, FALSE, NULL);
134  mutex_artCoord = CreateMutex(NULL, FALSE, NULL);
135  mutex_velocity = CreateMutex(NULL, FALSE, NULL);
136  mutex_display = CreateMutex(NULL, FALSE, NULL);
137 #endif
138 
139  DWORD dwThreadIdArray;
140  hThread = CreateThread(NULL, // default security attributes
141  0, // use default stack size
142  launcher, // thread function name
143  this, // argument to thread function
144  0, // use default creation flags
145  &dwThreadIdArray); // returns the thread identifier
146 #elif defined(VISP_HAVE_PTHREAD)
147  pthread_mutex_init(&mutex_fMi, NULL);
148  pthread_mutex_init(&mutex_artVel, NULL);
149  pthread_mutex_init(&mutex_artCoord, NULL);
150  pthread_mutex_init(&mutex_velocity, NULL);
151  pthread_mutex_init(&mutex_display, NULL);
152 
153  pthread_attr_init(&attr);
154  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
155 
156  pthread_create(&thread, NULL, launcher, (void *)this);
157 #endif
158 
159  compute_fMi();
160 }
161 
166 {
167  robotStop = true;
168 
169 #if defined(_WIN32)
170 #if defined(WINRT_8_1)
171  WaitForSingleObjectEx(hThread, INFINITE, FALSE);
172 #else // pure win32
173  WaitForSingleObject(hThread, INFINITE);
174 #endif
175  CloseHandle(hThread);
176  CloseHandle(mutex_fMi);
177  CloseHandle(mutex_artVel);
178  CloseHandle(mutex_artCoord);
179  CloseHandle(mutex_velocity);
180  CloseHandle(mutex_display);
181 #elif defined(VISP_HAVE_PTHREAD)
182  pthread_attr_destroy(&attr);
183  pthread_join(thread, NULL);
184  pthread_mutex_destroy(&mutex_fMi);
185  pthread_mutex_destroy(&mutex_artVel);
186  pthread_mutex_destroy(&mutex_artCoord);
187  pthread_mutex_destroy(&mutex_velocity);
188  pthread_mutex_destroy(&mutex_display);
189 #endif
190 
191  if (robotArms != NULL) {
192  for (int i = 0; i < 6; i++)
193  free_Bound_scene(&(robotArms[i]));
194  }
195 
196  delete[] robotArms;
197  delete[] fMi;
198 }
199 
209 {
210  // set arm_dir from #define VISP_ROBOT_ARMS_DIR if it exists
211  // VISP_ROBOT_ARMS_DIR may contain multiple locations separated by ";"
212  std::vector<std::string> arm_dirs = vpIoTools::splitChain(std::string(VISP_ROBOT_ARMS_DIR), std::string(";"));
213  bool armDirExists = false;
214  for (size_t i = 0; i < arm_dirs.size(); i++)
215  if (vpIoTools::checkDirectory(arm_dirs[i]) == true) { // directory exists
216  arm_dir = arm_dirs[i];
217  armDirExists = true;
218  break;
219  }
220  if (!armDirExists) {
221  try {
222  arm_dir = vpIoTools::getenv("VISP_ROBOT_ARMS_DIR");
223  std::cout << "The simulator uses data from VISP_ROBOT_ARMS_DIR=" << arm_dir << std::endl;
224  } catch (...) {
225  std::cout << "Cannot get VISP_ROBOT_ARMS_DIR environment variable" << std::endl;
226  }
227  }
228 
229  this->init(vpAfma6::TOOL_CCMOP);
230  toolCustom = false;
231 
232  size_fMi = 8;
233  fMi = new vpHomogeneousMatrix[8];
236 
237  zeroPos.resize(njoint);
238  zeroPos = 0;
239  reposPos.resize(njoint);
240  reposPos = 0;
241  reposPos[1] = -M_PI / 2;
242  reposPos[2] = M_PI;
243  reposPos[4] = M_PI / 2;
244 
245  artCoord = zeroPos;
246  artVel = 0;
247 
248  q_prev_getdis.resize(njoint);
249  q_prev_getdis = 0;
250  first_time_getdis = true;
251 
252  positioningVelocity = defaultPositioningVelocity;
253 
256 
257  // Software joint limits in radians
258  //_joint_min.resize(njoint);
259  _joint_min[0] = -0.6501;
260  _joint_min[1] = -0.6001;
261  _joint_min[2] = -0.5001;
262  _joint_min[3] = -2.7301;
263  _joint_min[4] = -0.1001;
264  _joint_min[5] = -1.5901;
265  //_joint_max.resize(njoint);
266  _joint_max[0] = 0.7001;
267  _joint_max[1] = 0.5201;
268  _joint_max[2] = 0.4601;
269  _joint_max[3] = 2.7301;
270  _joint_max[4] = 2.4801;
271  _joint_max[5] = 1.5901;
272 }
273 
278 {
279  robotArms = NULL;
280  robotArms = new Bound_scene[6];
281  initArms();
283  vpHomogeneousMatrix(-0.1, 0, 4, vpMath::rad(90), 0, 0));
284  cameraParam.initPersProjWithoutDistortion(558.5309599, 556.055053, 320, 240);
286  vpCameraParameters tmp;
287  getCameraParameters(tmp, 640, 480);
288  px_int = tmp.get_px();
289  py_int = tmp.get_py();
290  sceneInitialized = true;
291 }
292 
309 {
310  this->projModel = proj_model;
311  unsigned int name_length = 30; // the size of this kind of string "/afma6_tool_vacuum.bnd"
312  if (arm_dir.size() > FILENAME_MAX)
313  throw vpException(vpException::dimensionError, "Cannot initialize Afma6 simulator");
314  unsigned int full_length = (unsigned int)arm_dir.size() + name_length;
315  if (full_length > FILENAME_MAX)
316  throw vpException(vpException::dimensionError, "Cannot initialize Afma6 simulator");
317 
318  // Use here default values of the robot constant parameters.
319  switch (tool) {
320  case vpAfma6::TOOL_CCMOP: {
321  _erc[0] = vpMath::rad(164.35); // rx
322  _erc[1] = vpMath::rad(89.64); // ry
323  _erc[2] = vpMath::rad(-73.05); // rz
324  _etc[0] = 0.0117; // tx
325  _etc[1] = 0.0033; // ty
326  _etc[2] = 0.2272; // tz
327 
328  setCameraParameters(vpCameraParameters(1109.5735473989, 1112.1520168160, 320, 240));
329 
330  if (robotArms != NULL) {
331  while (get_displayBusy())
332  vpTime::wait(2);
333  free_Bound_scene(&(robotArms[5]));
334  char *name_arm = new char[full_length];
335  strcpy(name_arm, arm_dir.c_str());
336  strcat(name_arm, "/afma6_tool_ccmop.bnd");
337  set_scene(name_arm, robotArms + 5, 1.0);
338  set_displayBusy(false);
339  delete[] name_arm;
340  }
341  break;
342  }
343  case vpAfma6::TOOL_GRIPPER: {
344  _erc[0] = vpMath::rad(88.33); // rx
345  _erc[1] = vpMath::rad(72.07); // ry
346  _erc[2] = vpMath::rad(2.53); // rz
347  _etc[0] = 0.0783; // tx
348  _etc[1] = 0.1234; // ty
349  _etc[2] = 0.1638; // tz
350 
351  setCameraParameters(vpCameraParameters(852.6583228197, 854.8084224761, 320, 240));
352 
353  if (robotArms != NULL) {
354  while (get_displayBusy())
355  vpTime::wait(2);
356  free_Bound_scene(&(robotArms[5]));
357  char *name_arm = new char[full_length];
358  strcpy(name_arm, arm_dir.c_str());
359  strcat(name_arm, "/afma6_tool_gripper.bnd");
360  set_scene(name_arm, robotArms + 5, 1.0);
361  set_displayBusy(false);
362  delete[] name_arm;
363  }
364  break;
365  }
366  case vpAfma6::TOOL_VACUUM: {
367  _erc[0] = vpMath::rad(90.40); // rx
368  _erc[1] = vpMath::rad(75.11); // ry
369  _erc[2] = vpMath::rad(0.18); // rz
370  _etc[0] = 0.0038; // tx
371  _etc[1] = 0.1281; // ty
372  _etc[2] = 0.1658; // tz
373 
374  setCameraParameters(vpCameraParameters(853.4876600807, 856.0339170706, 320, 240));
375 
376  if (robotArms != NULL) {
377  while (get_displayBusy())
378  vpTime::wait(2);
379  free_Bound_scene(&(robotArms[5]));
380 
381  char *name_arm = new char[full_length];
382 
383  strcpy(name_arm, arm_dir.c_str());
384  strcat(name_arm, "/afma6_tool_vacuum.bnd");
385  set_scene(name_arm, robotArms + 5, 1.0);
386  set_displayBusy(false);
387  delete[] name_arm;
388  }
389  break;
390  }
393  std::cout << "The generic camera is not handled in vpSimulatorAfma6.cpp" << std::endl;
394  }
395  }
396 
397  vpRotationMatrix eRc(_erc);
398  this->_eMc.buildFrom(_etc, eRc);
399 
400  setToolType(tool);
401  return;
402 }
403 
414 void vpSimulatorAfma6::getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width,
415  const unsigned int &image_height)
416 {
417  if (toolCustom) {
418  cam.initPersProjWithoutDistortion(px_int, py_int, image_width / 2, image_height / 2);
419  }
420  // Set default parameters
421  switch (getToolType()) {
422  case vpAfma6::TOOL_CCMOP: {
423  // Set default intrinsic camera parameters for 640x480 images
424  if (image_width == 640 && image_height == 480) {
425  std::cout << "Get default camera parameters for camera \"" << vpAfma6::CONST_CCMOP_CAMERA_NAME << "\""
426  << std::endl;
427  cam.initPersProjWithoutDistortion(1109.5735473989, 1112.1520168160, 320, 240);
428  } else {
429  vpTRACE("Cannot get default intrinsic camera parameters for this image "
430  "resolution");
431  }
432  break;
433  }
434  case vpAfma6::TOOL_GRIPPER: {
435  // Set default intrinsic camera parameters for 640x480 images
436  if (image_width == 640 && image_height == 480) {
437  std::cout << "Get default camera parameters for camera \"" << vpAfma6::CONST_GRIPPER_CAMERA_NAME << "\""
438  << std::endl;
439  cam.initPersProjWithoutDistortion(852.6583228197, 854.8084224761, 320, 240);
440  } else {
441  vpTRACE("Cannot get default intrinsic camera parameters for this image "
442  "resolution");
443  }
444  break;
445  }
448  case vpAfma6::TOOL_VACUUM: {
449  std::cout << "The generic camera is not handled in vpSimulatorAfma6.cpp" << std::endl;
450  break;
451  }
452  default:
453  vpERROR_TRACE("This error should not occur!");
454  break;
455  }
456  return;
457 }
458 
468 {
469  getCameraParameters(cam, I_.getWidth(), I_.getHeight());
470 }
471 
481 {
482  getCameraParameters(cam, I_.getWidth(), I_.getHeight());
483 }
484 
491 {
492  px_int = cam.get_px();
493  py_int = cam.get_py();
494  toolCustom = true;
495 }
496 
502 {
503  double tcur_1 = tcur; // temporary variable used to store the last time
504  // since the last command
505 
506  while (!robotStop) {
507  // Get current time
508  tprev = tcur_1;
510 
512  setVelocityCalled = false;
513 
515 
516  double ellapsedTime = (tcur - tprev) * 1e-3;
517  if (constantSamplingTimeMode) { // if we want a constant velocity, we
518  // force the ellapsed time to the given
519  // samplingTime
520  ellapsedTime = getSamplingTime(); // in second
521  }
522 
523  vpColVector articularCoordinates = get_artCoord();
524  vpColVector articularVelocities = get_artVel();
525 
526  if (jointLimit) {
527  double art = articularCoordinates[jointLimitArt - 1] + ellapsedTime * articularVelocities[jointLimitArt - 1];
528  if (art <= _joint_min[jointLimitArt - 1] || art >= _joint_max[jointLimitArt - 1]) {
529  if (verbose_) {
530  std::cout << "Joint " << jointLimitArt - 1
531  << " reaches a limit: " << vpMath::deg(_joint_min[jointLimitArt - 1]) << " < " << vpMath::deg(art)
532  << " < " << vpMath::deg(_joint_max[jointLimitArt - 1]) << std::endl;
533  }
534 
535  articularVelocities = 0.0;
536  } else
537  jointLimit = false;
538  }
539 
540  articularCoordinates[0] = articularCoordinates[0] + ellapsedTime * articularVelocities[0];
541  articularCoordinates[1] = articularCoordinates[1] + ellapsedTime * articularVelocities[1];
542  articularCoordinates[2] = articularCoordinates[2] + ellapsedTime * articularVelocities[2];
543  articularCoordinates[3] = articularCoordinates[3] + ellapsedTime * articularVelocities[3];
544  articularCoordinates[4] = articularCoordinates[4] + ellapsedTime * articularVelocities[4];
545  articularCoordinates[5] = articularCoordinates[5] + ellapsedTime * articularVelocities[5];
546 
547  int jl = isInJointLimit();
548 
549  if (jl != 0 && jointLimit == false) {
550  if (jl < 0)
551  ellapsedTime = (_joint_min[(unsigned int)(-jl - 1)] - articularCoordinates[(unsigned int)(-jl - 1)]) /
552  (articularVelocities[(unsigned int)(-jl - 1)]);
553  else
554  ellapsedTime = (_joint_max[(unsigned int)(jl - 1)] - articularCoordinates[(unsigned int)(jl - 1)]) /
555  (articularVelocities[(unsigned int)(jl - 1)]);
556 
557  for (unsigned int i = 0; i < 6; i++)
558  articularCoordinates[i] = articularCoordinates[i] + ellapsedTime * articularVelocities[i];
559 
560  jointLimit = true;
561  jointLimitArt = (unsigned int)fabs((double)jl);
562  }
563 
564  set_artCoord(articularCoordinates);
565  set_artVel(articularVelocities);
566 
567  compute_fMi();
568 
569  if (displayAllowed) {
573  }
574 
575  if (displayType == MODEL_3D && displayAllowed) {
576  while (get_displayBusy())
577  vpTime::wait(2);
579  set_displayBusy(false);
580  }
581 
582  if (0 /*displayType == MODEL_DH && displayAllowed*/) {
583  vpHomogeneousMatrix fMit[8];
584  get_fMi(fMit);
585 
586  // vpDisplay::displayFrame(I,getExternalCameraPosition
587  // ()*fMi[6],cameraParam,0.2,vpColor::none);
588 
589  vpImagePoint iP, iP_1;
590  vpPoint pt(0, 0, 0);
591 
594  pt.track(getExternalCameraPosition() * fMit[0]);
597  for (unsigned int k = 1; k < 7; k++) {
598  pt.track(getExternalCameraPosition() * fMit[k - 1]);
600 
601  pt.track(getExternalCameraPosition() * fMit[k]);
603 
605  }
607  thickness_);
608  }
609 
611 
612  vpTime::wait(tcur, 1000 * getSamplingTime());
613  tcur_1 = tcur;
614  } else {
616  }
617  }
618 }
619 
634 {
635  // vpColVector q = get_artCoord();
636  vpColVector q(6); //; = get_artCoord();
637  q = get_artCoord();
638 
639  vpHomogeneousMatrix fMit[8];
640 
641  double q1 = q[0];
642  double q2 = q[1];
643  double q3 = q[2];
644  double q4 = q[3];
645  double q5 = q[4];
646  double q6 = q[5];
647 
648  double c4 = cos(q4);
649  double s4 = sin(q4);
650  double c5 = cos(q5);
651  double s5 = sin(q5);
652  double c6 = cos(q6);
653  double s6 = sin(q6);
654 
655  fMit[0][0][0] = 1;
656  fMit[0][1][0] = 0;
657  fMit[0][2][0] = 0;
658  fMit[0][0][1] = 0;
659  fMit[0][1][1] = 1;
660  fMit[0][2][1] = 0;
661  fMit[0][0][2] = 0;
662  fMit[0][1][2] = 0;
663  fMit[0][2][2] = 1;
664  fMit[0][0][3] = q1;
665  fMit[0][1][3] = 0;
666  fMit[0][2][3] = 0;
667 
668  fMit[1][0][0] = 1;
669  fMit[1][1][0] = 0;
670  fMit[1][2][0] = 0;
671  fMit[1][0][1] = 0;
672  fMit[1][1][1] = 1;
673  fMit[1][2][1] = 0;
674  fMit[1][0][2] = 0;
675  fMit[1][1][2] = 0;
676  fMit[1][2][2] = 1;
677  fMit[1][0][3] = q1;
678  fMit[1][1][3] = q2;
679  fMit[1][2][3] = 0;
680 
681  fMit[2][0][0] = 1;
682  fMit[2][1][0] = 0;
683  fMit[2][2][0] = 0;
684  fMit[2][0][1] = 0;
685  fMit[2][1][1] = 1;
686  fMit[2][2][1] = 0;
687  fMit[2][0][2] = 0;
688  fMit[2][1][2] = 0;
689  fMit[2][2][2] = 1;
690  fMit[2][0][3] = q1;
691  fMit[2][1][3] = q2;
692  fMit[2][2][3] = q3;
693 
694  fMit[3][0][0] = s4;
695  fMit[3][1][0] = -c4;
696  fMit[3][2][0] = 0;
697  fMit[3][0][1] = c4;
698  fMit[3][1][1] = s4;
699  fMit[3][2][1] = 0;
700  fMit[3][0][2] = 0;
701  fMit[3][1][2] = 0;
702  fMit[3][2][2] = 1;
703  fMit[3][0][3] = q1;
704  fMit[3][1][3] = q2;
705  fMit[3][2][3] = q3;
706 
707  fMit[4][0][0] = s4 * s5;
708  fMit[4][1][0] = -c4 * s5;
709  fMit[4][2][0] = c5;
710  fMit[4][0][1] = s4 * c5;
711  fMit[4][1][1] = -c4 * c5;
712  fMit[4][2][1] = -s5;
713  fMit[4][0][2] = c4;
714  fMit[4][1][2] = s4;
715  fMit[4][2][2] = 0;
716  fMit[4][0][3] = c4 * this->_long_56 + q1;
717  fMit[4][1][3] = s4 * this->_long_56 + q2;
718  fMit[4][2][3] = q3;
719 
720  fMit[5][0][0] = s4 * s5 * c6 + c4 * s6;
721  fMit[5][1][0] = -c4 * s5 * c6 + s4 * s6;
722  fMit[5][2][0] = c5 * c6;
723  fMit[5][0][1] = -s4 * s5 * s6 + c4 * c6;
724  fMit[5][1][1] = c4 * s5 * s6 + s4 * c6;
725  fMit[5][2][1] = -c5 * s6;
726  fMit[5][0][2] = -s4 * c5;
727  fMit[5][1][2] = c4 * c5;
728  fMit[5][2][2] = s5;
729  fMit[5][0][3] = c4 * this->_long_56 + q1;
730  fMit[5][1][3] = s4 * this->_long_56 + q2;
731  fMit[5][2][3] = q3;
732 
733  fMit[6][0][0] = fMit[5][0][0];
734  fMit[6][1][0] = fMit[5][1][0];
735  fMit[6][2][0] = fMit[5][2][0];
736  fMit[6][0][1] = fMit[5][0][1];
737  fMit[6][1][1] = fMit[5][1][1];
738  fMit[6][2][1] = fMit[5][2][1];
739  fMit[6][0][2] = fMit[5][0][2];
740  fMit[6][1][2] = fMit[5][1][2];
741  fMit[6][2][2] = fMit[5][2][2];
742  fMit[6][0][3] = fMit[5][0][3];
743  fMit[6][1][3] = fMit[5][1][3];
744  fMit[6][2][3] = fMit[5][2][3];
745 
746  // vpHomogeneousMatrix cMe;
747  // get_cMe(cMe);
748  // cMe = cMe.inverse();
749  // fMit[7] = fMit[6] * cMe;
750  vpAfma6::get_fMc(q, fMit[7]);
751 
752 #if defined(_WIN32)
753 #if defined(WINRT_8_1)
754  WaitForSingleObjectEx(mutex_fMi, INFINITE, FALSE);
755 #else // pure win32
756  WaitForSingleObject(mutex_fMi, INFINITE);
757 #endif
758  for (int i = 0; i < 8; i++)
759  fMi[i] = fMit[i];
760  ReleaseMutex(mutex_fMi);
761 #elif defined(VISP_HAVE_PTHREAD)
762  pthread_mutex_lock(&mutex_fMi);
763  for (int i = 0; i < 8; i++)
764  fMi[i] = fMit[i];
765  pthread_mutex_unlock(&mutex_fMi);
766 #endif
767 }
768 
775 {
776  switch (newState) {
777  case vpRobot::STATE_STOP: {
778  // Start primitive STOP only if the current state is Velocity
780  stopMotion();
781  }
782  break;
783  }
786  std::cout << "Change the control mode from velocity to position control.\n";
787  stopMotion();
788  } else {
789  // std::cout << "Change the control mode from stop to position
790  // control.\n";
791  }
792  break;
793  }
796  std::cout << "Change the control mode from stop to velocity control.\n";
797  }
798  break;
799  }
801  default:
802  break;
803  }
804 
805  return vpRobot::setRobotState(newState);
806 }
807 
883 {
885  vpERROR_TRACE("Cannot send a velocity to the robot "
886  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
888  "Cannot send a velocity to the robot "
889  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
890  }
891 
892  vpColVector vel_sat(6);
893 
894  double scale_sat = 1;
895  double vel_trans_max = getMaxTranslationVelocity();
896  double vel_rot_max = getMaxRotationVelocity();
897 
898  double vel_abs; // Absolute value
899 
900  // Velocity saturation
901  switch (frame) {
902  // saturation in cartesian space
905  if (vel.getRows() != 6) {
906  vpERROR_TRACE("The velocity vector must have a size of 6 !!!!");
907  throw;
908  }
909 
910  for (unsigned int i = 0; i < 3; ++i) {
911  vel_abs = fabs(vel[i]);
912  if (vel_abs > vel_trans_max && !jointLimit) {
913  vel_trans_max = vel_abs;
914  vpERROR_TRACE("Excess velocity %g m/s in TRANSLATION "
915  "(axis nr. %d).",
916  vel[i], i + 1);
917  }
918 
919  vel_abs = fabs(vel[i + 3]);
920  if (vel_abs > vel_rot_max && !jointLimit) {
921  vel_rot_max = vel_abs;
922  vpERROR_TRACE("Excess velocity %g rad/s in ROTATION "
923  "(axis nr. %d).",
924  vel[i + 3], i + 4);
925  }
926  }
927 
928  double scale_trans_sat = 1;
929  double scale_rot_sat = 1;
930  if (vel_trans_max > getMaxTranslationVelocity())
931  scale_trans_sat = getMaxTranslationVelocity() / vel_trans_max;
932 
933  if (vel_rot_max > getMaxRotationVelocity())
934  scale_rot_sat = getMaxRotationVelocity() / vel_rot_max;
935 
936  if ((scale_trans_sat < 1) || (scale_rot_sat < 1)) {
937  if (scale_trans_sat < scale_rot_sat)
938  scale_sat = scale_trans_sat;
939  else
940  scale_sat = scale_rot_sat;
941  }
942  break;
943  }
944 
945  // saturation in joint space
947  if (vel.getRows() != 6) {
948  vpERROR_TRACE("The velocity vector must have a size of 6 !!!!");
949  throw;
950  }
951  for (unsigned int i = 0; i < 6; ++i) {
952  vel_abs = fabs(vel[i]);
953  if (vel_abs > vel_rot_max && !jointLimit) {
954  vel_rot_max = vel_abs;
955  vpERROR_TRACE("Excess velocity %g rad/s in ROTATION "
956  "(axis nr. %d).",
957  vel[i], i + 1);
958  }
959  }
960  double scale_rot_sat = 1;
961  if (vel_rot_max > getMaxRotationVelocity())
962  scale_rot_sat = getMaxRotationVelocity() / vel_rot_max;
963  if (scale_rot_sat < 1)
964  scale_sat = scale_rot_sat;
965  break;
966  }
967  case vpRobot::MIXT_FRAME: {
968  break;
969  }
970  }
971 
972  set_velocity(vel * scale_sat);
973  setRobotFrame(frame);
974  setVelocityCalled = true;
975 }
976 
981 {
983 
984  double vel_rot_max = getMaxRotationVelocity();
985 
986  vpColVector articularCoordinates = get_artCoord();
987  vpColVector velocityframe = get_velocity();
988  vpColVector articularVelocity;
989 
990  switch (frame) {
991  case vpRobot::CAMERA_FRAME: {
992  vpMatrix eJe_;
994  vpAfma6::get_eJe(articularCoordinates, eJe_);
995  eJe_ = eJe_.pseudoInverse();
997  singularityTest(articularCoordinates, eJe_);
998  articularVelocity = eJe_ * eVc * velocityframe;
999  set_artVel(articularVelocity);
1000  break;
1001  }
1002  case vpRobot::REFERENCE_FRAME: {
1003  vpMatrix fJe_;
1004  vpAfma6::get_fJe(articularCoordinates, fJe_);
1005  fJe_ = fJe_.pseudoInverse();
1007  singularityTest(articularCoordinates, fJe_);
1008  articularVelocity = fJe_ * velocityframe;
1009  set_artVel(articularVelocity);
1010  break;
1011  }
1012  case vpRobot::ARTICULAR_FRAME: {
1013  articularVelocity = velocityframe;
1014  set_artVel(articularVelocity);
1015  break;
1016  }
1017  case vpRobot::MIXT_FRAME: {
1018  break;
1019  }
1020  }
1021 
1022  switch (frame) {
1023  case vpRobot::CAMERA_FRAME:
1024  case vpRobot::REFERENCE_FRAME: {
1025  for (unsigned int i = 0; i < 6; ++i) {
1026  double vel_abs = fabs(articularVelocity[i]);
1027  if (vel_abs > vel_rot_max && !jointLimit) {
1028  vel_rot_max = vel_abs;
1029  vpERROR_TRACE("Excess velocity %g rad/s in ROTATION "
1030  "(axis nr. %d).",
1031  articularVelocity[i], i + 1);
1032  }
1033  }
1034  double scale_rot_sat = 1;
1035  double scale_sat = 1;
1036  if (vel_rot_max > getMaxRotationVelocity())
1037  scale_rot_sat = getMaxRotationVelocity() / vel_rot_max;
1038  if (scale_rot_sat < 1)
1039  scale_sat = scale_rot_sat;
1040 
1041  set_artVel(articularVelocity * scale_sat);
1042  break;
1043  }
1045  case vpRobot::MIXT_FRAME: {
1046  break;
1047  }
1048  }
1049 }
1050 
1098 {
1099  vel.resize(6);
1100 
1101  vpColVector articularCoordinates = get_artCoord();
1102  vpColVector articularVelocity = get_artVel();
1103 
1104  switch (frame) {
1105  case vpRobot::CAMERA_FRAME: {
1106  vpMatrix eJe_;
1108  vpAfma6::get_eJe(articularCoordinates, eJe_);
1109  vel = cVe * eJe_ * articularVelocity;
1110  break;
1111  }
1112  case vpRobot::ARTICULAR_FRAME: {
1113  vel = articularVelocity;
1114  break;
1115  }
1116  case vpRobot::REFERENCE_FRAME: {
1117  vpMatrix fJe_;
1118  vpAfma6::get_fJe(articularCoordinates, fJe_);
1119  vel = fJe_ * articularVelocity;
1120  break;
1121  }
1122  case vpRobot::MIXT_FRAME: {
1123  break;
1124  }
1125  default: {
1126  vpERROR_TRACE("Error in spec of vpRobot. "
1127  "Case not taken in account.");
1128  return;
1129  }
1130  }
1131 }
1132 
1150 {
1151  timestamp = vpTime::measureTimeSecond();
1152  getVelocity(frame, vel);
1153 }
1154 
1198 {
1199  vpColVector vel(6);
1200  getVelocity(frame, vel);
1201 
1202  return vel;
1203 }
1204 
1218 {
1219  timestamp = vpTime::measureTimeSecond();
1220  vpColVector vel(6);
1221  getVelocity(frame, vel);
1222 
1223  return vel;
1224 }
1225 
1227 {
1228  double vel_rot_max = getMaxRotationVelocity();
1229  double velmax = fabs(q[0]);
1230  for (unsigned int i = 1; i < 6; i++) {
1231  if (velmax < fabs(q[i]))
1232  velmax = fabs(q[i]);
1233  }
1234 
1235  double alpha = (getPositioningVelocity() * vel_rot_max) / (velmax * 100);
1236  q = q * alpha;
1237 }
1238 
1315 {
1317  vpERROR_TRACE("Robot was not in position-based control\n"
1318  "Modification of the robot state");
1319  // setRobotState(vpRobot::STATE_POSITION_CONTROL) ;
1320  }
1321 
1322  vpColVector articularCoordinates = get_artCoord();
1323 
1324  vpColVector error(6);
1325  double errsqr = 0;
1326  switch (frame) {
1327  case vpRobot::CAMERA_FRAME: {
1328  int nbSol;
1329  vpColVector qdes(6);
1330 
1331  vpTranslationVector txyz;
1332  vpRxyzVector rxyz;
1333  for (unsigned int i = 0; i < 3; i++) {
1334  txyz[i] = q[i];
1335  rxyz[i] = q[i + 3];
1336  }
1337 
1338  vpRotationMatrix cRc2(rxyz);
1339  vpHomogeneousMatrix cMc2(txyz, cRc2);
1340 
1341  vpHomogeneousMatrix fMc_;
1342  vpAfma6::get_fMc(articularCoordinates, fMc_);
1343 
1344  vpHomogeneousMatrix fMc2 = fMc_ * cMc2;
1345 
1346  do {
1347  articularCoordinates = get_artCoord();
1348  qdes = articularCoordinates;
1349  nbSol = getInverseKinematics(fMc2, qdes, true, verbose_);
1350  setVelocityCalled = true;
1351  if (nbSol > 0) {
1352  error = qdes - articularCoordinates;
1353  errsqr = error.sumSquare();
1354  // findHighestPositioningSpeed(error);
1355  set_artVel(error);
1356  if (errsqr < 1e-4) {
1357  set_artCoord(qdes);
1358  error = 0;
1359  set_artVel(error);
1360  set_velocity(error);
1361  break;
1362  }
1363  } else {
1364  vpERROR_TRACE("Positionning error.");
1365  throw vpRobotException(vpRobotException::positionOutOfRangeError, "Position out of range.");
1366  }
1367  } while (errsqr > 1e-8 && nbSol > 0);
1368 
1369  break;
1370  }
1371 
1372  case vpRobot::ARTICULAR_FRAME: {
1373  do {
1374  articularCoordinates = get_artCoord();
1375  error = q - articularCoordinates;
1376  errsqr = error.sumSquare();
1377  // findHighestPositioningSpeed(error);
1378  set_artVel(error);
1379  setVelocityCalled = true;
1380  if (errsqr < 1e-4) {
1381  set_artCoord(q);
1382  error = 0;
1383  set_artVel(error);
1384  set_velocity(error);
1385  break;
1386  }
1387  } while (errsqr > 1e-8);
1388  break;
1389  }
1390 
1391  case vpRobot::REFERENCE_FRAME: {
1392  int nbSol;
1393  vpColVector qdes(6);
1394 
1395  vpTranslationVector txyz;
1396  vpRxyzVector rxyz;
1397  for (unsigned int i = 0; i < 3; i++) {
1398  txyz[i] = q[i];
1399  rxyz[i] = q[i + 3];
1400  }
1401 
1402  vpRotationMatrix fRc(rxyz);
1403  vpHomogeneousMatrix fMc_(txyz, fRc);
1404 
1405  do {
1406  articularCoordinates = get_artCoord();
1407  qdes = articularCoordinates;
1408  nbSol = getInverseKinematics(fMc_, qdes, true, verbose_);
1409  setVelocityCalled = true;
1410  if (nbSol > 0) {
1411  error = qdes - articularCoordinates;
1412  errsqr = error.sumSquare();
1413  // findHighestPositioningSpeed(error);
1414  set_artVel(error);
1415  if (errsqr < 1e-4) {
1416  set_artCoord(qdes);
1417  error = 0;
1418  set_artVel(error);
1419  set_velocity(error);
1420  break;
1421  }
1422  } else
1423  vpERROR_TRACE("Positionning error. Position unreachable");
1424  } while (errsqr > 1e-8 && nbSol > 0);
1425  break;
1426  }
1427  case vpRobot::MIXT_FRAME: {
1428  vpERROR_TRACE("Positionning error. Mixt frame not implemented");
1429  throw vpRobotException(vpRobotException::lowLevelError, "Positionning error: "
1430  "Mixt frame not implemented.");
1431  }
1432  }
1433 }
1434 
1498 void vpSimulatorAfma6::setPosition(const vpRobot::vpControlFrameType frame, const double pos1, const double pos2,
1499  const double pos3, const double pos4, const double pos5, const double pos6)
1500 {
1501  try {
1502  vpColVector position(6);
1503  position[0] = pos1;
1504  position[1] = pos2;
1505  position[2] = pos3;
1506  position[3] = pos4;
1507  position[4] = pos5;
1508  position[5] = pos6;
1509 
1510  setPosition(frame, position);
1511  } catch (...) {
1512  vpERROR_TRACE("Error caught");
1513  throw;
1514  }
1515 }
1516 
1552 void vpSimulatorAfma6::setPosition(const char *filename)
1553 {
1554  vpColVector q;
1555  bool ret;
1556 
1557  ret = this->readPosFile(filename, q);
1558 
1559  if (ret == false) {
1560  vpERROR_TRACE("Bad position in \"%s\"", filename);
1561  throw vpRobotException(vpRobotException::lowLevelError, "Bad position in filename.");
1562  }
1565 }
1566 
1628 {
1629  q.resize(6);
1630 
1631  switch (frame) {
1632  case vpRobot::CAMERA_FRAME: {
1633  q = 0;
1634  break;
1635  }
1636 
1637  case vpRobot::ARTICULAR_FRAME: {
1638  q = get_artCoord();
1639  break;
1640  }
1641 
1642  case vpRobot::REFERENCE_FRAME: {
1643  vpHomogeneousMatrix fMc_;
1644  vpAfma6::get_fMc(get_artCoord(), fMc_);
1645 
1646  vpRotationMatrix fRc;
1647  fMc_.extract(fRc);
1648  vpRxyzVector rxyz(fRc);
1649 
1650  vpTranslationVector txyz;
1651  fMc_.extract(txyz);
1652 
1653  for (unsigned int i = 0; i < 3; i++) {
1654  q[i] = txyz[i];
1655  q[i + 3] = rxyz[i];
1656  }
1657  break;
1658  }
1659 
1660  case vpRobot::MIXT_FRAME: {
1661  vpERROR_TRACE("Positionning error. Mixt frame not implemented");
1662  throw vpRobotException(vpRobotException::lowLevelError, "Positionning error: "
1663  "Mixt frame not implemented.");
1664  }
1665  }
1666 }
1667 
1695 {
1696  timestamp = vpTime::measureTimeSecond();
1697  getPosition(frame, q);
1698 }
1699 
1712 {
1713  vpColVector posRxyz;
1714  // recupere position en Rxyz
1715  this->getPosition(frame, posRxyz);
1716 
1717  // recupere le vecteur thetaU correspondant
1718  vpThetaUVector RtuVect(vpRxyzVector(posRxyz[3], posRxyz[4], posRxyz[5]));
1719 
1720  // remplit le vpPoseVector avec translation et rotation ThetaU
1721  for (unsigned int j = 0; j < 3; j++) {
1722  position[j] = posRxyz[j];
1723  position[j + 3] = RtuVect[j];
1724  }
1725 }
1726 
1738 void vpSimulatorAfma6::getPosition(const vpRobot::vpControlFrameType frame, vpPoseVector &position, double &timestamp)
1739 {
1740  timestamp = vpTime::measureTimeSecond();
1741  getPosition(frame, position);
1742 }
1743 
1754 void vpSimulatorAfma6::setJointLimit(const vpColVector &limitMin, const vpColVector &limitMax)
1755 {
1756  if (limitMin.getRows() != 6 || limitMax.getRows() != 6) {
1757  vpTRACE("Joint limit vector has not a size of 6 !");
1758  return;
1759  }
1760 
1761  _joint_min[0] = limitMin[0];
1762  _joint_min[1] = limitMin[1];
1763  _joint_min[2] = limitMin[2];
1764  _joint_min[3] = limitMin[3];
1765  _joint_min[4] = limitMin[4];
1766  _joint_min[5] = limitMin[5];
1767 
1768  _joint_max[0] = limitMax[0];
1769  _joint_max[1] = limitMax[1];
1770  _joint_max[2] = limitMax[2];
1771  _joint_max[3] = limitMax[3];
1772  _joint_max[4] = limitMax[4];
1773  _joint_max[5] = limitMax[5];
1774 }
1775 
1782 {
1783  double q5 = q[4];
1784 
1785  bool cond = fabs(q5 - M_PI / 2) < 1e-1;
1786 
1787  if (cond) {
1788  J[0][3] = 0;
1789  J[0][4] = 0;
1790  J[1][3] = 0;
1791  J[1][4] = 0;
1792  J[3][3] = 0;
1793  J[3][4] = 0;
1794  J[5][3] = 0;
1795  J[5][4] = 0;
1796  return true;
1797  }
1798 
1799  return false;
1800 }
1801 
1806 {
1807  int artNumb = 0;
1808  double diff = 0;
1809  double difft = 0;
1810 
1811  vpColVector articularCoordinates = get_artCoord();
1812 
1813  for (unsigned int i = 0; i < 6; i++) {
1814  if (articularCoordinates[i] <= _joint_min[i]) {
1815  difft = _joint_min[i] - articularCoordinates[i];
1816  if (difft > diff) {
1817  diff = difft;
1818  artNumb = -(int)i - 1;
1819  }
1820  }
1821  }
1822 
1823  for (unsigned int i = 0; i < 6; i++) {
1824  if (articularCoordinates[i] >= _joint_max[i]) {
1825  difft = articularCoordinates[i] - _joint_max[i];
1826  if (difft > diff) {
1827  diff = difft;
1828  artNumb = (int)(i + 1);
1829  }
1830  }
1831  }
1832 
1833  if (artNumb != 0)
1834  std::cout << "\nWarning: Velocity control stopped: axis " << fabs((float)artNumb) << " on joint limit!"
1835  << std::endl;
1836 
1837  return artNumb;
1838 }
1839 
1858 {
1859  displacement.resize(6);
1860  displacement = 0;
1861  vpColVector q_cur(6);
1862 
1863  q_cur = get_artCoord();
1864 
1865  if (!first_time_getdis) {
1866  switch (frame) {
1867  case vpRobot::CAMERA_FRAME: {
1868  std::cout << "getDisplacement() CAMERA_FRAME not implemented\n";
1869  return;
1870  }
1871 
1872  case vpRobot::ARTICULAR_FRAME: {
1873  displacement = q_cur - q_prev_getdis;
1874  break;
1875  }
1876 
1877  case vpRobot::REFERENCE_FRAME: {
1878  std::cout << "getDisplacement() REFERENCE_FRAME not implemented\n";
1879  return;
1880  }
1881 
1882  case vpRobot::MIXT_FRAME: {
1883  std::cout << "getDisplacement() MIXT_FRAME not implemented\n";
1884  return;
1885  }
1886  }
1887  } else {
1888  first_time_getdis = false;
1889  }
1890 
1891  // Memorize the joint position for the next call
1892  q_prev_getdis = q_cur;
1893 }
1894 
1942 bool vpSimulatorAfma6::readPosFile(const std::string &filename, vpColVector &q)
1943 {
1944  std::ifstream fd(filename.c_str(), std::ios::in);
1945 
1946  if (!fd.is_open()) {
1947  return false;
1948  }
1949 
1950  std::string line;
1951  std::string key("R:");
1952  std::string id("#AFMA6 - Position");
1953  bool pos_found = false;
1954  int lineNum = 0;
1955 
1956  q.resize(njoint);
1957 
1958  while (std::getline(fd, line)) {
1959  lineNum++;
1960  if (lineNum == 1) {
1961  if (!(line.compare(0, id.size(), id) == 0)) { // check if Afma6 position file
1962  std::cout << "Error: this position file " << filename << " is not for Afma6 robot" << std::endl;
1963  return false;
1964  }
1965  }
1966  if ((line.compare(0, 1, "#") == 0)) { // skip comment
1967  continue;
1968  }
1969  if ((line.compare(0, key.size(), key) == 0)) { // decode position
1970  // check if there are at least njoint values in the line
1971  std::vector<std::string> chain = vpIoTools::splitChain(line, std::string(" "));
1972  if (chain.size() < njoint + 1) // try to split with tab separator
1973  chain = vpIoTools::splitChain(line, std::string("\t"));
1974  if (chain.size() < njoint + 1)
1975  continue;
1976 
1977  std::istringstream ss(line);
1978  std::string key_;
1979  ss >> key_;
1980  for (unsigned int i = 0; i < njoint; i++)
1981  ss >> q[i];
1982  pos_found = true;
1983  break;
1984  }
1985  }
1986 
1987  // converts rotations from degrees into radians
1988  q[3] = vpMath::rad(q[3]);
1989  q[4] = vpMath::rad(q[4]);
1990  q[5] = vpMath::rad(q[5]);
1991 
1992  fd.close();
1993 
1994  if (!pos_found) {
1995  std::cout << "Error: unable to find a position for Afma6 robot in " << filename << std::endl;
1996  return false;
1997  }
1998 
1999  return true;
2000 }
2001 
2024 bool vpSimulatorAfma6::savePosFile(const std::string &filename, const vpColVector &q)
2025 {
2026  FILE *fd;
2027  fd = fopen(filename.c_str(), "w");
2028  if (fd == NULL)
2029  return false;
2030 
2031  fprintf(fd, "\
2032 #AFMA6 - Position - Version 2.01\n\
2033 #\n\
2034 # R: X Y Z A B C\n\
2035 # Joint position: X, Y, Z: translations in meters\n\
2036 # A, B, C: rotations in degrees\n\
2037 #\n\
2038 #\n\n");
2039 
2040  // Save positions in mm and deg
2041  fprintf(fd, "R: %lf %lf %lf %lf %lf %lf\n", q[0], q[1], q[2], vpMath::deg(q[3]), vpMath::deg(q[4]),
2042  vpMath::deg(q[5]));
2043 
2044  fclose(fd);
2045  return (true);
2046 }
2047 
2055 void vpSimulatorAfma6::move(const char *filename)
2056 {
2057  vpColVector q;
2058 
2059  try {
2060  this->readPosFile(filename, q);
2063  } catch (...) {
2064  throw;
2065  }
2066 }
2067 
2078 
2087 {
2088  vpHomogeneousMatrix cMe;
2089  vpAfma6::get_cMe(cMe);
2090 
2091  cVe.buildFrom(cMe);
2092 }
2093 
2104 {
2105  try {
2106  vpAfma6::get_eJe(get_artCoord(), eJe_);
2107  } catch (...) {
2108  vpERROR_TRACE("catch exception ");
2109  throw;
2110  }
2111 }
2112 
2124 {
2125  try {
2126  vpColVector articularCoordinates = get_artCoord();
2127  vpAfma6::get_fJe(articularCoordinates, fJe_);
2128  } catch (...) {
2129  vpERROR_TRACE("Error caught");
2130  throw;
2131  }
2132 }
2133 
2138 {
2140  return;
2141 
2142  vpColVector stop(6);
2143  stop = 0;
2144  set_artVel(stop);
2145  set_velocity(stop);
2147 }
2148 
2149 /**********************************************************************************/
2150 /**********************************************************************************/
2151 /**********************************************************************************/
2152 /**********************************************************************************/
2153 
2163 {
2164  // set scene_dir from #define VISP_SCENE_DIR if it exists
2165  // VISP_SCENES_DIR may contain multiple locations separated by ";"
2166  std::string scene_dir_;
2167  std::vector<std::string> scene_dirs = vpIoTools::splitChain(std::string(VISP_SCENES_DIR), std::string(";"));
2168  bool sceneDirExists = false;
2169  for (size_t i = 0; i < scene_dirs.size(); i++)
2170  if (vpIoTools::checkDirectory(scene_dirs[i]) == true) { // directory exists
2171  scene_dir_ = scene_dirs[i];
2172  sceneDirExists = true;
2173  break;
2174  }
2175  if (!sceneDirExists) {
2176  try {
2177  scene_dir_ = vpIoTools::getenv("VISP_SCENES_DIR");
2178  std::cout << "The simulator uses data from VISP_SCENES_DIR=" << scene_dir_ << std::endl;
2179  } catch (...) {
2180  std::cout << "Cannot get VISP_SCENES_DIR environment variable" << std::endl;
2181  }
2182  }
2183 
2184  unsigned int name_length = 30; // the size of this kind of string "/afma6_arm2.bnd"
2185  if (scene_dir_.size() > FILENAME_MAX)
2186  throw vpException(vpException::dimensionError, "Cannot initialize Afma6 simulator");
2187  unsigned int full_length = (unsigned int)scene_dir_.size() + name_length;
2188  if (full_length > FILENAME_MAX)
2189  throw vpException(vpException::dimensionError, "Cannot initialize Afma6 simulator");
2190 
2191  char *name_cam = new char[full_length];
2192 
2193  strcpy(name_cam, scene_dir_.c_str());
2194  strcat(name_cam, "/camera.bnd");
2195  set_scene(name_cam, &camera, cameraFactor);
2196 
2197  if (arm_dir.size() > FILENAME_MAX)
2198  throw vpException(vpException::dimensionError, "Cannot initialize Afma6 simulator");
2199  full_length = (unsigned int)arm_dir.size() + name_length;
2200  if (full_length > FILENAME_MAX)
2201  throw vpException(vpException::dimensionError, "Cannot initialize Afma6 simulator");
2202 
2203  char *name_arm = new char[full_length];
2204  strcpy(name_arm, arm_dir.c_str());
2205  strcat(name_arm, "/afma6_gate.bnd");
2206  std::cout << "name arm: " << name_arm << std::endl;
2207  set_scene(name_arm, robotArms, 1.0);
2208  strcpy(name_arm, arm_dir.c_str());
2209  strcat(name_arm, "/afma6_arm1.bnd");
2210  set_scene(name_arm, robotArms + 1, 1.0);
2211  strcpy(name_arm, arm_dir.c_str());
2212  strcat(name_arm, "/afma6_arm2.bnd");
2213  set_scene(name_arm, robotArms + 2, 1.0);
2214  strcpy(name_arm, arm_dir.c_str());
2215  strcat(name_arm, "/afma6_arm3.bnd");
2216  set_scene(name_arm, robotArms + 3, 1.0);
2217  strcpy(name_arm, arm_dir.c_str());
2218  strcat(name_arm, "/afma6_arm4.bnd");
2219  set_scene(name_arm, robotArms + 4, 1.0);
2220 
2222  tool = getToolType();
2223  strcpy(name_arm, arm_dir.c_str());
2224  switch (tool) {
2225  case vpAfma6::TOOL_CCMOP: {
2226  strcat(name_arm, "/afma6_tool_ccmop.bnd");
2227  break;
2228  }
2229  case vpAfma6::TOOL_GRIPPER: {
2230  strcat(name_arm, "/afma6_tool_gripper.bnd");
2231  break;
2232  }
2233  case vpAfma6::TOOL_VACUUM: {
2234  strcat(name_arm, "/afma6_tool_vacuum.bnd");
2235  break;
2236  }
2237  case vpAfma6::TOOL_CUSTOM: {
2238  std::cout << "The custom tool is not handled in vpSimulatorAfma6.cpp" << std::endl;
2239  break;
2240  }
2242  std::cout << "The generic camera is not handled in vpSimulatorAfma6.cpp" << std::endl;
2243  break;
2244  }
2245  }
2246  set_scene(name_arm, robotArms + 5, 1.0);
2247 
2248  add_rfstack(IS_BACK);
2249 
2250  add_vwstack("start", "depth", 0.0, 100.0);
2251  add_vwstack("start", "window", -0.1, 0.1, -0.1, 0.1);
2252  add_vwstack("start", "type", PERSPECTIVE);
2253  //
2254  // sceneInitialized = true;
2255  // displayObject = true;
2256  displayCamera = true;
2257 
2258  delete[] name_cam;
2259  delete[] name_arm;
2260 }
2261 
2263 {
2264  bool changed = false;
2265  vpHomogeneousMatrix displacement = navigation(I_, changed);
2266 
2267  // if (displacement[2][3] != 0)
2268  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon())
2269  camMf2 = camMf2 * displacement;
2270 
2271  f2Mf = camMf2.inverse() * camMf;
2272 
2273  camMf = camMf2 * displacement * f2Mf;
2274 
2275  double u;
2276  double v;
2277  // if(px_ext != 1 && py_ext != 1)
2278  // we assume px_ext and py_ext > 0
2279  if ((std::fabs(px_ext - 1.) > vpMath::maximum(px_ext, 1.) * std::numeric_limits<double>::epsilon()) &&
2280  (std::fabs(py_ext - 1) > vpMath::maximum(py_ext, 1.) * std::numeric_limits<double>::epsilon())) {
2281  u = (double)I_.getWidth() / (2 * px_ext);
2282  v = (double)I_.getHeight() / (2 * py_ext);
2283  } else {
2284  u = (double)I_.getWidth() / (vpMath::minimum(I_.getWidth(), I_.getHeight()));
2285  v = (double)I_.getHeight() / (vpMath::minimum(I_.getWidth(), I_.getHeight()));
2286  }
2287 
2288  float w44o[4][4], w44cext[4][4], x, y, z;
2289 
2290  vp2jlc_matrix(camMf.inverse(), w44cext);
2291 
2292  add_vwstack("start", "cop", w44cext[3][0], w44cext[3][1], w44cext[3][2]);
2293  x = w44cext[2][0] + w44cext[3][0];
2294  y = w44cext[2][1] + w44cext[3][1];
2295  z = w44cext[2][2] + w44cext[3][2];
2296  add_vwstack("start", "vrp", x, y, z);
2297  add_vwstack("start", "vpn", w44cext[2][0], w44cext[2][1], w44cext[2][2]);
2298  add_vwstack("start", "vup", w44cext[1][0], w44cext[1][1], w44cext[1][2]);
2299  add_vwstack("start", "window", -u, u, -v, v);
2300 
2301  vpHomogeneousMatrix fMit[8];
2302  get_fMi(fMit);
2303 
2304  vp2jlc_matrix(vpHomogeneousMatrix(0, 0, 0, 0, 0, 0), w44o);
2305  display_scene(w44o, robotArms[0], I_, curColor);
2306 
2307  vp2jlc_matrix(fMit[0], w44o);
2308  display_scene(w44o, robotArms[1], I_, curColor);
2309 
2310  vp2jlc_matrix(fMit[2], w44o);
2311  display_scene(w44o, robotArms[2], I_, curColor);
2312 
2313  vp2jlc_matrix(fMit[3], w44o);
2314  display_scene(w44o, robotArms[3], I_, curColor);
2315 
2316  vp2jlc_matrix(fMit[4], w44o);
2317  display_scene(w44o, robotArms[4], I_, curColor);
2318 
2319  vp2jlc_matrix(fMit[5], w44o);
2320  display_scene(w44o, robotArms[5], I_, curColor);
2321 
2322  if (displayCamera) {
2323  vpHomogeneousMatrix cMe;
2324  get_cMe(cMe);
2325  cMe = cMe.inverse();
2326  cMe = fMit[6] * cMe;
2327  vp2jlc_matrix(cMe, w44o);
2328  display_scene(w44o, camera, I_, camColor);
2329  }
2330 
2331  if (displayObject) {
2332  vp2jlc_matrix(fMo, w44o);
2333  display_scene(w44o, scene, I_, curColor);
2334  }
2335 }
2336 
2355 {
2356  vpColVector stop(6);
2357  bool status = true;
2358  stop = 0;
2359  set_artVel(stop);
2360  set_velocity(stop);
2361  vpHomogeneousMatrix fMc_;
2362  fMc_ = fMo * cMo_.inverse();
2363 
2364  vpColVector articularCoordinates = get_artCoord();
2365  int nbSol = getInverseKinematics(fMc_, articularCoordinates, true, verbose_);
2366 
2367  if (nbSol == 0) {
2368  status = false;
2369  vpERROR_TRACE("Positionning error. Position unreachable");
2370  }
2371 
2372  if (verbose_)
2373  std::cout << "Used joint coordinates (rad): " << articularCoordinates.t() << std::endl;
2374 
2375  set_artCoord(articularCoordinates);
2376 
2377  compute_fMi();
2378 
2379  return status;
2380 }
2381 
2396 {
2397  vpColVector stop(6);
2398  stop = 0;
2399  set_artVel(stop);
2400  set_velocity(stop);
2401  vpHomogeneousMatrix fMit[8];
2402  get_fMi(fMit);
2403  fMo = fMit[7] * cMo_;
2404 }
2405 
2417 bool vpSimulatorAfma6::setPosition(const vpHomogeneousMatrix &cdMo_, vpImage<unsigned char> *Iint, const double &errMax)
2418 {
2419  // get rid of max velocity
2420  double vMax = getMaxTranslationVelocity();
2421  double wMax = getMaxRotationVelocity();
2422  setMaxTranslationVelocity(10. * vMax);
2423  setMaxRotationVelocity(10. * wMax);
2424 
2425  vpColVector v(3), w(3), vel(6);
2426  vpHomogeneousMatrix cdMc;
2427  vpTranslationVector cdTc;
2428  vpRotationMatrix cdRc;
2429  vpThetaUVector cdTUc;
2430  vpColVector err(6);
2431  err = 1.;
2432  const double lambda = 5.;
2433 
2435 
2436  unsigned int i, iter = 0;
2437  while ((iter++ < 300) & (err.euclideanNorm() > errMax)) {
2438  double t = vpTime::measureTimeMs();
2439 
2440  // update image
2441  if (Iint != NULL) {
2442  vpDisplay::display(*Iint);
2443  getInternalView(*Iint);
2444  vpDisplay::flush(*Iint);
2445  }
2446 
2447  // update pose error
2448  cdMc = cdMo_ * get_cMo().inverse();
2449  cdMc.extract(cdRc);
2450  cdMc.extract(cdTc);
2451  cdTUc.buildFrom(cdRc);
2452 
2453  // compute v,w and velocity
2454  v = -lambda * cdRc.t() * cdTc;
2455  w = -lambda * cdTUc;
2456  for (i = 0; i < 3; ++i) {
2457  vel[i] = v[i];
2458  vel[i + 3] = w[i];
2459  err[i] = cdTc[i];
2460  err[i + 3] = cdTUc[i];
2461  }
2462 
2463  // update feat
2465 
2466  // wait for it
2467  vpTime::wait(t, 10);
2468  }
2469  vel = 0.;
2470  set_velocity(vel);
2471  set_artVel(vel);
2473  setMaxRotationVelocity(wMax);
2474 
2475  // std::cout << "setPosition: final error " << err.t() << std::endl;
2476  return (err.euclideanNorm() <= errMax);
2477 }
2478 
2479 #elif !defined(VISP_BUILD_SHARED_LIBS)
2480 // Work arround to avoid warning: libvisp_robot.a(vpSimulatorAfma6.cpp.o) has
2481 // no symbols
2482 void dummy_vpSimulatorAfma6(){};
2483 #endif
static void displayCamera(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness)
Modelisation of Irisa&#39;s gantry robot named Afma6.
Definition: vpAfma6.h:78
double euclideanNorm() const
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
vpMatrix pseudoInverse(double svThreshold=1e-6) const
Definition: vpMatrix.cpp:1931
vpRxyzVector _erc
Definition: vpAfma6.h:198
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
vpTranslationVector _etc
Definition: vpAfma6.h:197
static const unsigned int njoint
Number of joint.
Definition: vpAfma6.h:185
Error that can be emited by the vpRobot class and its derivates.
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:367
vpHomogeneousMatrix getExternalCameraPosition() const
void setMaxTranslationVelocity(const double maxVt)
Definition: vpRobot.cpp:239
void get_eJe(vpMatrix &eJe)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void get_cVe(vpVelocityTwistMatrix &cVe)
void move(const char *filename)
static void * launcher(void *arg)
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 ...
#define vpERROR_TRACE
Definition: vpDebug.h:393
VISP_EXPORT double measureTimeSecond()
Definition: vpTime.cpp:250
double getMaxTranslationVelocity(void) const
Definition: vpRobot.cpp:251
int getInverseKinematics(const vpHomogeneousMatrix &fMc, vpColVector &q, const bool &nearest=true, const bool &verbose=false) const
Definition: vpAfma6.cpp:532
static std::string getenv(const char *env)
Definition: vpIoTools.cpp:262
static const vpColor none
Definition: vpColor.h:192
Initialize the position controller.
Definition: vpRobot.h:68
error that can be emited by ViSP classes.
Definition: vpException.h:71
vpAfma6ToolType
List of possible tools that can be attached to the robot end-effector.
Definition: vpAfma6.h:118
void setJointLimit(const vpColVector &limitMin, const vpColVector &limitMax)
unsigned int getRows() const
Definition: vpArray2D.h:156
void track(const vpHomogeneousMatrix &cMo)
vpHomogeneousMatrix inverse() const
static const double defaultPositioningVelocity
vpRowVector t() const
vpControlFrameType
Definition: vpRobot.h:75
static bool savePosFile(const std::string &filename, const vpColVector &q)
void getExternalImage(vpImage< vpRGBa > &I)
vpCameraParameters::vpCameraParametersProjType projModel
Definition: vpAfma6.h:206
vpAfma6ToolType getToolType() const
Get the current tool type.
Definition: vpAfma6.h:160
double getMaxRotationVelocity(void) const
Definition: vpRobot.cpp:273
void extract(vpRotationMatrix &R) const
static const vpColor green
Definition: vpColor.h:183
vpHomogeneousMatrix fMo
static void flush(const vpImage< unsigned char > &I)
vpHomogeneousMatrix get_fMc(const vpColVector &q) const
Definition: vpAfma6.cpp:707
vpThetaUVector buildFrom(const vpHomogeneousMatrix &M)
vpRotationMatrix t() const
void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &q)
vpRobot::vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
Class that defines what is a point.
Definition: vpPoint.h:58
void setToolType(vpAfma6::vpAfma6ToolType tool)
Set the current tool type.
Definition: vpAfma6.h:185
bool initialiseCameraRelativeToObject(const vpHomogeneousMatrix &cMo)
static const char *const CONST_GRIPPER_CAMERA_NAME
Definition: vpAfma6.h:105
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:137
Implementation of a rotation matrix and operations on such kind of matrices.
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
void getDisplacement(const vpRobot::vpControlFrameType frame, vpColVector &displacement)
Initialize the velocity controller.
Definition: vpRobot.h:67
static bool readPosFile(const std::string &filename, vpColVector &q)
virtual vpRobotStateType getRobotState(void) const
Definition: vpRobot.h:139
vpRobotStateType
Definition: vpRobot.h:64
void initialiseObjectRelativeToCamera(const vpHomogeneousMatrix &cMo)
Initialize the acceleration controller.
Definition: vpRobot.h:69
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
#define vpTRACE
Definition: vpDebug.h:416
vpHomogeneousMatrix f2Mf
void set_displayBusy(const bool &status)
static void display(const vpImage< unsigned char > &I)
void setExternalCameraPosition(const vpHomogeneousMatrix &camMf_)
void getInternalView(vpImage< vpRGBa > &I)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
void get_eJe(const vpColVector &q, vpMatrix &eJe) const
Definition: vpAfma6.cpp:864
Generic class defining intrinsic camera parameters.
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &q)
bool singularityTest(const vpColVector &q, vpMatrix &J)
static std::vector< std::string > splitChain(const std::string &chain, const std::string &sep)
Definition: vpIoTools.cpp:1665
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:145
void setCameraParameters(const vpCameraParameters &cam)
double getSamplingTime() const
void set_velocity(const vpColVector &vel)
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &q)
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
void get_fJe(const vpColVector &q, vpMatrix &fJe) const
Definition: vpAfma6.cpp:934
static double rad(double deg)
Definition: vpMath.h:102
void setExternalCameraParameters(const vpCameraParameters &cam)
This class aims to be a basis used to create all the simulators of robots.
void setMaxRotationVelocity(const double maxVr)
Definition: vpRobot.cpp:260
void display_scene(Matrix mat, Bound_scene &sc, const vpImage< vpRGBa > &I, const vpColor &color)
double getPositioningVelocity(void)
double _long_56
Definition: vpAfma6.h:193
vpHomogeneousMatrix camMf
vpHomogeneousMatrix navigation(const vpImage< vpRGBa > &I, bool &changed)
static double deg(double rad)
Definition: vpMath.h:95
unsigned int getHeight() const
Definition: vpImage.h:178
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0))
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:429
void get_fJe(vpMatrix &fJe)
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:92
void get_cMe(vpHomogeneousMatrix &cMe) const
Definition: vpAfma6.cpp:820
vpHomogeneousMatrix _eMc
Definition: vpAfma6.h:200
double sumSquare() const
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:431
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:156
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height)
vpControlFrameType setRobotFrame(vpRobot::vpControlFrameType newFrame)
Definition: vpRobot.cpp:207
vpControlFrameType getRobotFrame(void) const
Definition: vpRobot.h:167
void get_cMe(vpHomogeneousMatrix &cMe)
double _joint_max[6]
Definition: vpAfma6.h:194
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void set_artCoord(const vpColVector &coord)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
VISP_EXPORT double getMinTimeForUsleepCall()
Definition: vpTime.cpp:80
void set_artVel(const vpColVector &vel)
void get_fMi(vpHomogeneousMatrix *fMit)
static const char *const CONST_CCMOP_CAMERA_NAME
Definition: vpAfma6.h:100
unsigned int getWidth() const
Definition: vpImage.h:229
Class that consider the case of a translation vector.
Implementation of a rotation vector as axis-angle minimal representation.
vpHomogeneousMatrix camMf2
double _joint_min[6]
Definition: vpAfma6.h:195
void findHighestPositioningSpeed(vpColVector &q)
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:241