Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
vpSimulatorAfma6.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 
882 {
884  vpERROR_TRACE("Cannot send a velocity to the robot "
885  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
887  "Cannot send a velocity to the robot "
888  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
889  }
890 
891  vpColVector vel_sat(6);
892 
893  double scale_sat = 1;
894  double vel_trans_max = getMaxTranslationVelocity();
895  double vel_rot_max = getMaxRotationVelocity();
896 
897  double vel_abs; // Absolute value
898 
899  // Velocity saturation
900  switch (frame) {
901  // saturation in cartesian space
904  if (vel.getRows() != 6) {
905  vpERROR_TRACE("The velocity vector must have a size of 6 !!!!");
906  throw;
907  }
908 
909  for (unsigned int i = 0; i < 3; ++i) {
910  vel_abs = fabs(vel[i]);
911  if (vel_abs > vel_trans_max && !jointLimit) {
912  vel_trans_max = vel_abs;
913  vpERROR_TRACE("Excess velocity %g m/s in TRANSLATION "
914  "(axis nr. %d).",
915  vel[i], i + 1);
916  }
917 
918  vel_abs = fabs(vel[i + 3]);
919  if (vel_abs > vel_rot_max && !jointLimit) {
920  vel_rot_max = vel_abs;
921  vpERROR_TRACE("Excess velocity %g rad/s in ROTATION "
922  "(axis nr. %d).",
923  vel[i + 3], i + 4);
924  }
925  }
926 
927  double scale_trans_sat = 1;
928  double scale_rot_sat = 1;
929  if (vel_trans_max > getMaxTranslationVelocity())
930  scale_trans_sat = getMaxTranslationVelocity() / vel_trans_max;
931 
932  if (vel_rot_max > getMaxRotationVelocity())
933  scale_rot_sat = getMaxRotationVelocity() / vel_rot_max;
934 
935  if ((scale_trans_sat < 1) || (scale_rot_sat < 1)) {
936  if (scale_trans_sat < scale_rot_sat)
937  scale_sat = scale_trans_sat;
938  else
939  scale_sat = scale_rot_sat;
940  }
941  break;
942  }
943 
944  // saturation in joint space
946  if (vel.getRows() != 6) {
947  vpERROR_TRACE("The velocity vector must have a size of 6 !!!!");
948  throw;
949  }
950  for (unsigned int i = 0; i < 6; ++i) {
951  vel_abs = fabs(vel[i]);
952  if (vel_abs > vel_rot_max && !jointLimit) {
953  vel_rot_max = vel_abs;
954  vpERROR_TRACE("Excess velocity %g rad/s in ROTATION "
955  "(axis nr. %d).",
956  vel[i], i + 1);
957  }
958  }
959  double scale_rot_sat = 1;
960  if (vel_rot_max > getMaxRotationVelocity())
961  scale_rot_sat = getMaxRotationVelocity() / vel_rot_max;
962  if (scale_rot_sat < 1)
963  scale_sat = scale_rot_sat;
964  break;
965  }
966  case vpRobot::MIXT_FRAME: {
967  throw vpRobotException(vpRobotException::wrongStateError, "Cannot set a velocity in MIXT_FRAME frame:"
968  "functionality not implemented");
969  }
971  throw vpRobotException(vpRobotException::wrongStateError, "Cannot set a velocity in END_EFFECTOT_FRAME frame:"
972  "functionality not implemented");
973  }
974  }
975 
976  set_velocity(vel * scale_sat);
977  setRobotFrame(frame);
978  setVelocityCalled = true;
979 }
980 
985 {
987 
988  double vel_rot_max = getMaxRotationVelocity();
989 
990  vpColVector articularCoordinates = get_artCoord();
991  vpColVector velocityframe = get_velocity();
992  vpColVector articularVelocity;
993 
994  switch (frame) {
995  case vpRobot::CAMERA_FRAME: {
996  vpMatrix eJe_;
998  vpAfma6::get_eJe(articularCoordinates, eJe_);
999  eJe_ = eJe_.pseudoInverse();
1001  singularityTest(articularCoordinates, eJe_);
1002  articularVelocity = eJe_ * eVc * velocityframe;
1003  set_artVel(articularVelocity);
1004  break;
1005  }
1006  case vpRobot::REFERENCE_FRAME: {
1007  vpMatrix fJe_;
1008  vpAfma6::get_fJe(articularCoordinates, fJe_);
1009  fJe_ = fJe_.pseudoInverse();
1011  singularityTest(articularCoordinates, fJe_);
1012  articularVelocity = fJe_ * velocityframe;
1013  set_artVel(articularVelocity);
1014  break;
1015  }
1016  case vpRobot::ARTICULAR_FRAME: {
1017  articularVelocity = velocityframe;
1018  set_artVel(articularVelocity);
1019  break;
1020  }
1022  case vpRobot::MIXT_FRAME: {
1023  break;
1024  }
1025  }
1026 
1027  switch (frame) {
1028  case vpRobot::CAMERA_FRAME:
1029  case vpRobot::REFERENCE_FRAME: {
1030  for (unsigned int i = 0; i < 6; ++i) {
1031  double vel_abs = fabs(articularVelocity[i]);
1032  if (vel_abs > vel_rot_max && !jointLimit) {
1033  vel_rot_max = vel_abs;
1034  vpERROR_TRACE("Excess velocity %g rad/s in ROTATION "
1035  "(axis nr. %d).",
1036  articularVelocity[i], i + 1);
1037  }
1038  }
1039  double scale_rot_sat = 1;
1040  double scale_sat = 1;
1041  if (vel_rot_max > getMaxRotationVelocity())
1042  scale_rot_sat = getMaxRotationVelocity() / vel_rot_max;
1043  if (scale_rot_sat < 1)
1044  scale_sat = scale_rot_sat;
1045 
1046  set_artVel(articularVelocity * scale_sat);
1047  break;
1048  }
1051  case vpRobot::MIXT_FRAME: {
1052  break;
1053  }
1054  }
1055 }
1056 
1103 {
1104  vel.resize(6);
1105 
1106  vpColVector articularCoordinates = get_artCoord();
1107  vpColVector articularVelocity = get_artVel();
1108 
1109  switch (frame) {
1110  case vpRobot::CAMERA_FRAME: {
1111  vpMatrix eJe_;
1113  vpAfma6::get_eJe(articularCoordinates, eJe_);
1114  vel = cVe * eJe_ * articularVelocity;
1115  break;
1116  }
1117  case vpRobot::ARTICULAR_FRAME: {
1118  vel = articularVelocity;
1119  break;
1120  }
1121  case vpRobot::REFERENCE_FRAME: {
1122  vpMatrix fJe_;
1123  vpAfma6::get_fJe(articularCoordinates, fJe_);
1124  vel = fJe_ * articularVelocity;
1125  break;
1126  }
1128  case vpRobot::MIXT_FRAME: {
1129  break;
1130  }
1131  default: {
1132  vpERROR_TRACE("Error in spec of vpRobot. "
1133  "Case not taken in account.");
1134  return;
1135  }
1136  }
1137 }
1138 
1156 {
1157  timestamp = vpTime::measureTimeSecond();
1158  getVelocity(frame, vel);
1159 }
1160 
1203 {
1204  vpColVector vel(6);
1205  getVelocity(frame, vel);
1206 
1207  return vel;
1208 }
1209 
1223 {
1224  timestamp = vpTime::measureTimeSecond();
1225  vpColVector vel(6);
1226  getVelocity(frame, vel);
1227 
1228  return vel;
1229 }
1230 
1232 {
1233  double vel_rot_max = getMaxRotationVelocity();
1234  double velmax = fabs(q[0]);
1235  for (unsigned int i = 1; i < 6; i++) {
1236  if (velmax < fabs(q[i]))
1237  velmax = fabs(q[i]);
1238  }
1239 
1240  double alpha = (getPositioningVelocity() * vel_rot_max) / (velmax * 100);
1241  q = q * alpha;
1242 }
1243 
1319 {
1321  vpERROR_TRACE("Robot was not in position-based control\n"
1322  "Modification of the robot state");
1323  // setRobotState(vpRobot::STATE_POSITION_CONTROL) ;
1324  }
1325 
1326  vpColVector articularCoordinates = get_artCoord();
1327 
1328  vpColVector error(6);
1329  double errsqr = 0;
1330  switch (frame) {
1331  case vpRobot::CAMERA_FRAME: {
1332  int nbSol;
1333  vpColVector qdes(6);
1334 
1335  vpTranslationVector txyz;
1336  vpRxyzVector rxyz;
1337  for (unsigned int i = 0; i < 3; i++) {
1338  txyz[i] = q[i];
1339  rxyz[i] = q[i + 3];
1340  }
1341 
1342  vpRotationMatrix cRc2(rxyz);
1343  vpHomogeneousMatrix cMc2(txyz, cRc2);
1344 
1345  vpHomogeneousMatrix fMc_;
1346  vpAfma6::get_fMc(articularCoordinates, fMc_);
1347 
1348  vpHomogeneousMatrix fMc2 = fMc_ * cMc2;
1349 
1350  do {
1351  articularCoordinates = get_artCoord();
1352  qdes = articularCoordinates;
1353  nbSol = getInverseKinematics(fMc2, qdes, true, verbose_);
1354  setVelocityCalled = true;
1355  if (nbSol > 0) {
1356  error = qdes - articularCoordinates;
1357  errsqr = error.sumSquare();
1358  // findHighestPositioningSpeed(error);
1359  set_artVel(error);
1360  if (errsqr < 1e-4) {
1361  set_artCoord(qdes);
1362  error = 0;
1363  set_artVel(error);
1364  set_velocity(error);
1365  break;
1366  }
1367  } else {
1368  vpERROR_TRACE("Positionning error.");
1369  throw vpRobotException(vpRobotException::positionOutOfRangeError, "Position out of range.");
1370  }
1371  } while (errsqr > 1e-8 && nbSol > 0);
1372 
1373  break;
1374  }
1375 
1376  case vpRobot::ARTICULAR_FRAME: {
1377  do {
1378  articularCoordinates = get_artCoord();
1379  error = q - articularCoordinates;
1380  errsqr = error.sumSquare();
1381  // findHighestPositioningSpeed(error);
1382  set_artVel(error);
1383  setVelocityCalled = true;
1384  if (errsqr < 1e-4) {
1385  set_artCoord(q);
1386  error = 0;
1387  set_artVel(error);
1388  set_velocity(error);
1389  break;
1390  }
1391  } while (errsqr > 1e-8);
1392  break;
1393  }
1394 
1395  case vpRobot::REFERENCE_FRAME: {
1396  int nbSol;
1397  vpColVector qdes(6);
1398 
1399  vpTranslationVector txyz;
1400  vpRxyzVector rxyz;
1401  for (unsigned int i = 0; i < 3; i++) {
1402  txyz[i] = q[i];
1403  rxyz[i] = q[i + 3];
1404  }
1405 
1406  vpRotationMatrix fRc(rxyz);
1407  vpHomogeneousMatrix fMc_(txyz, fRc);
1408 
1409  do {
1410  articularCoordinates = get_artCoord();
1411  qdes = articularCoordinates;
1412  nbSol = getInverseKinematics(fMc_, qdes, true, verbose_);
1413  setVelocityCalled = true;
1414  if (nbSol > 0) {
1415  error = qdes - articularCoordinates;
1416  errsqr = error.sumSquare();
1417  // findHighestPositioningSpeed(error);
1418  set_artVel(error);
1419  if (errsqr < 1e-4) {
1420  set_artCoord(qdes);
1421  error = 0;
1422  set_artVel(error);
1423  set_velocity(error);
1424  break;
1425  }
1426  } else
1427  vpERROR_TRACE("Positionning error. Position unreachable");
1428  } while (errsqr > 1e-8 && nbSol > 0);
1429  break;
1430  }
1431  case vpRobot::MIXT_FRAME: {
1432  vpERROR_TRACE("Positionning error. Mixt frame not implemented");
1433  throw vpRobotException(vpRobotException::lowLevelError, "Positionning error: "
1434  "MIXT_FRAME not implemented.");
1435  }
1437  vpERROR_TRACE("Positionning error. Mixt frame not implemented");
1438  throw vpRobotException(vpRobotException::lowLevelError, "Positionning error: "
1439  "END_EFFECTOR_FRAME not implemented.");
1440  }
1441  }
1442 }
1443 
1506 void vpSimulatorAfma6::setPosition(const vpRobot::vpControlFrameType frame, double pos1, double pos2,
1507  double pos3, double pos4, double pos5, double pos6)
1508 {
1509  try {
1510  vpColVector position(6);
1511  position[0] = pos1;
1512  position[1] = pos2;
1513  position[2] = pos3;
1514  position[3] = pos4;
1515  position[4] = pos5;
1516  position[5] = pos6;
1517 
1518  setPosition(frame, position);
1519  } catch (...) {
1520  vpERROR_TRACE("Error caught");
1521  throw;
1522  }
1523 }
1524 
1559 void vpSimulatorAfma6::setPosition(const char *filename)
1560 {
1561  vpColVector q;
1562  bool ret;
1563 
1564  ret = this->readPosFile(filename, q);
1565 
1566  if (ret == false) {
1567  vpERROR_TRACE("Bad position in \"%s\"", filename);
1568  throw vpRobotException(vpRobotException::lowLevelError, "Bad position in filename.");
1569  }
1572 }
1573 
1634 {
1635  q.resize(6);
1636 
1637  switch (frame) {
1638  case vpRobot::CAMERA_FRAME: {
1639  q = 0;
1640  break;
1641  }
1642 
1643  case vpRobot::ARTICULAR_FRAME: {
1644  q = get_artCoord();
1645  break;
1646  }
1647 
1648  case vpRobot::REFERENCE_FRAME: {
1649  vpHomogeneousMatrix fMc_;
1650  vpAfma6::get_fMc(get_artCoord(), fMc_);
1651 
1652  vpRotationMatrix fRc;
1653  fMc_.extract(fRc);
1654  vpRxyzVector rxyz(fRc);
1655 
1656  vpTranslationVector txyz;
1657  fMc_.extract(txyz);
1658 
1659  for (unsigned int i = 0; i < 3; i++) {
1660  q[i] = txyz[i];
1661  q[i + 3] = rxyz[i];
1662  }
1663  break;
1664  }
1665 
1666  case vpRobot::MIXT_FRAME: {
1667  throw vpRobotException(vpRobotException::lowLevelError, "Positionning error: "
1668  "Mixt frame not implemented.");
1669  }
1671  throw vpRobotException(vpRobotException::lowLevelError, "Positionning error: "
1672  "End-effector frame not implemented.");
1673  }
1674  }
1675 }
1676 
1704 {
1705  timestamp = vpTime::measureTimeSecond();
1706  getPosition(frame, q);
1707 }
1708 
1721 {
1722  vpColVector posRxyz;
1723  // recupere position en Rxyz
1724  this->getPosition(frame, posRxyz);
1725 
1726  // recupere le vecteur thetaU correspondant
1727  vpThetaUVector RtuVect(vpRxyzVector(posRxyz[3], posRxyz[4], posRxyz[5]));
1728 
1729  // remplit le vpPoseVector avec translation et rotation ThetaU
1730  for (unsigned int j = 0; j < 3; j++) {
1731  position[j] = posRxyz[j];
1732  position[j + 3] = RtuVect[j];
1733  }
1734 }
1735 
1747 void vpSimulatorAfma6::getPosition(const vpRobot::vpControlFrameType frame, vpPoseVector &position, double &timestamp)
1748 {
1749  timestamp = vpTime::measureTimeSecond();
1750  getPosition(frame, position);
1751 }
1752 
1763 void vpSimulatorAfma6::setJointLimit(const vpColVector &limitMin, const vpColVector &limitMax)
1764 {
1765  if (limitMin.getRows() != 6 || limitMax.getRows() != 6) {
1766  vpTRACE("Joint limit vector has not a size of 6 !");
1767  return;
1768  }
1769 
1770  _joint_min[0] = limitMin[0];
1771  _joint_min[1] = limitMin[1];
1772  _joint_min[2] = limitMin[2];
1773  _joint_min[3] = limitMin[3];
1774  _joint_min[4] = limitMin[4];
1775  _joint_min[5] = limitMin[5];
1776 
1777  _joint_max[0] = limitMax[0];
1778  _joint_max[1] = limitMax[1];
1779  _joint_max[2] = limitMax[2];
1780  _joint_max[3] = limitMax[3];
1781  _joint_max[4] = limitMax[4];
1782  _joint_max[5] = limitMax[5];
1783 }
1784 
1791 {
1792  double q5 = q[4];
1793 
1794  bool cond = fabs(q5 - M_PI / 2) < 1e-1;
1795 
1796  if (cond) {
1797  J[0][3] = 0;
1798  J[0][4] = 0;
1799  J[1][3] = 0;
1800  J[1][4] = 0;
1801  J[3][3] = 0;
1802  J[3][4] = 0;
1803  J[5][3] = 0;
1804  J[5][4] = 0;
1805  return true;
1806  }
1807 
1808  return false;
1809 }
1810 
1815 {
1816  int artNumb = 0;
1817  double diff = 0;
1818  double difft = 0;
1819 
1820  vpColVector articularCoordinates = get_artCoord();
1821 
1822  for (unsigned int i = 0; i < 6; i++) {
1823  if (articularCoordinates[i] <= _joint_min[i]) {
1824  difft = _joint_min[i] - articularCoordinates[i];
1825  if (difft > diff) {
1826  diff = difft;
1827  artNumb = -(int)i - 1;
1828  }
1829  }
1830  }
1831 
1832  for (unsigned int i = 0; i < 6; i++) {
1833  if (articularCoordinates[i] >= _joint_max[i]) {
1834  difft = articularCoordinates[i] - _joint_max[i];
1835  if (difft > diff) {
1836  diff = difft;
1837  artNumb = (int)(i + 1);
1838  }
1839  }
1840  }
1841 
1842  if (artNumb != 0)
1843  std::cout << "\nWarning: Velocity control stopped: axis " << fabs((float)artNumb) << " on joint limit!"
1844  << std::endl;
1845 
1846  return artNumb;
1847 }
1848 
1867 {
1868  displacement.resize(6);
1869  displacement = 0;
1870  vpColVector q_cur(6);
1871 
1872  q_cur = get_artCoord();
1873 
1874  if (!first_time_getdis) {
1875  switch (frame) {
1876  case vpRobot::CAMERA_FRAME: {
1877  std::cout << "getDisplacement() CAMERA_FRAME not implemented\n";
1878  return;
1879  }
1880  case vpRobot::ARTICULAR_FRAME: {
1881  displacement = q_cur - q_prev_getdis;
1882  break;
1883  }
1884  case vpRobot::REFERENCE_FRAME: {
1885  std::cout << "getDisplacement() REFERENCE_FRAME not implemented\n";
1886  return;
1887  }
1888  case vpRobot::MIXT_FRAME: {
1889  std::cout << "getDisplacement() MIXT_FRAME not implemented\n";
1890  return;
1891  }
1893  std::cout << "getDisplacement() END_EFFECTOR_FRAME not implemented\n";
1894  return;
1895  }
1896  }
1897  } else {
1898  first_time_getdis = false;
1899  }
1900 
1901  // Memorize the joint position for the next call
1902  q_prev_getdis = q_cur;
1903 }
1904 
1952 bool vpSimulatorAfma6::readPosFile(const std::string &filename, vpColVector &q)
1953 {
1954  std::ifstream fd(filename.c_str(), std::ios::in);
1955 
1956  if (!fd.is_open()) {
1957  return false;
1958  }
1959 
1960  std::string line;
1961  std::string key("R:");
1962  std::string id("#AFMA6 - Position");
1963  bool pos_found = false;
1964  int lineNum = 0;
1965 
1966  q.resize(njoint);
1967 
1968  while (std::getline(fd, line)) {
1969  lineNum++;
1970  if (lineNum == 1) {
1971  if (!(line.compare(0, id.size(), id) == 0)) { // check if Afma6 position file
1972  std::cout << "Error: this position file " << filename << " is not for Afma6 robot" << std::endl;
1973  return false;
1974  }
1975  }
1976  if ((line.compare(0, 1, "#") == 0)) { // skip comment
1977  continue;
1978  }
1979  if ((line.compare(0, key.size(), key) == 0)) { // decode position
1980  // check if there are at least njoint values in the line
1981  std::vector<std::string> chain = vpIoTools::splitChain(line, std::string(" "));
1982  if (chain.size() < njoint + 1) // try to split with tab separator
1983  chain = vpIoTools::splitChain(line, std::string("\t"));
1984  if (chain.size() < njoint + 1)
1985  continue;
1986 
1987  std::istringstream ss(line);
1988  std::string key_;
1989  ss >> key_;
1990  for (unsigned int i = 0; i < njoint; i++)
1991  ss >> q[i];
1992  pos_found = true;
1993  break;
1994  }
1995  }
1996 
1997  // converts rotations from degrees into radians
1998  q[3] = vpMath::rad(q[3]);
1999  q[4] = vpMath::rad(q[4]);
2000  q[5] = vpMath::rad(q[5]);
2001 
2002  fd.close();
2003 
2004  if (!pos_found) {
2005  std::cout << "Error: unable to find a position for Afma6 robot in " << filename << std::endl;
2006  return false;
2007  }
2008 
2009  return true;
2010 }
2011 
2034 bool vpSimulatorAfma6::savePosFile(const std::string &filename, const vpColVector &q)
2035 {
2036  FILE *fd;
2037  fd = fopen(filename.c_str(), "w");
2038  if (fd == NULL)
2039  return false;
2040 
2041  fprintf(fd, "\
2042 #AFMA6 - Position - Version 2.01\n\
2043 #\n\
2044 # R: X Y Z A B C\n\
2045 # Joint position: X, Y, Z: translations in meters\n\
2046 # A, B, C: rotations in degrees\n\
2047 #\n\
2048 #\n\n");
2049 
2050  // Save positions in mm and deg
2051  fprintf(fd, "R: %lf %lf %lf %lf %lf %lf\n", q[0], q[1], q[2], vpMath::deg(q[3]), vpMath::deg(q[4]),
2052  vpMath::deg(q[5]));
2053 
2054  fclose(fd);
2055  return (true);
2056 }
2057 
2065 void vpSimulatorAfma6::move(const char *filename)
2066 {
2067  vpColVector q;
2068 
2069  try {
2070  this->readPosFile(filename, q);
2073  } catch (...) {
2074  throw;
2075  }
2076 }
2077 
2088 
2097 {
2098  vpHomogeneousMatrix cMe;
2099  vpAfma6::get_cMe(cMe);
2100 
2101  cVe.buildFrom(cMe);
2102 }
2103 
2114 {
2115  try {
2116  vpAfma6::get_eJe(get_artCoord(), eJe_);
2117  } catch (...) {
2118  vpERROR_TRACE("catch exception ");
2119  throw;
2120  }
2121 }
2122 
2134 {
2135  try {
2136  vpColVector articularCoordinates = get_artCoord();
2137  vpAfma6::get_fJe(articularCoordinates, fJe_);
2138  } catch (...) {
2139  vpERROR_TRACE("Error caught");
2140  throw;
2141  }
2142 }
2143 
2148 {
2150  return;
2151 
2152  vpColVector stop(6);
2153  stop = 0;
2154  set_artVel(stop);
2155  set_velocity(stop);
2157 }
2158 
2159 /**********************************************************************************/
2160 /**********************************************************************************/
2161 /**********************************************************************************/
2162 /**********************************************************************************/
2163 
2173 {
2174  // set scene_dir from #define VISP_SCENE_DIR if it exists
2175  // VISP_SCENES_DIR may contain multiple locations separated by ";"
2176  std::string scene_dir_;
2177  std::vector<std::string> scene_dirs = vpIoTools::splitChain(std::string(VISP_SCENES_DIR), std::string(";"));
2178  bool sceneDirExists = false;
2179  for (size_t i = 0; i < scene_dirs.size(); i++)
2180  if (vpIoTools::checkDirectory(scene_dirs[i]) == true) { // directory exists
2181  scene_dir_ = scene_dirs[i];
2182  sceneDirExists = true;
2183  break;
2184  }
2185  if (!sceneDirExists) {
2186  try {
2187  scene_dir_ = vpIoTools::getenv("VISP_SCENES_DIR");
2188  std::cout << "The simulator uses data from VISP_SCENES_DIR=" << scene_dir_ << std::endl;
2189  } catch (...) {
2190  std::cout << "Cannot get VISP_SCENES_DIR environment variable" << std::endl;
2191  }
2192  }
2193 
2194  unsigned int name_length = 30; // the size of this kind of string "/afma6_arm2.bnd"
2195  if (scene_dir_.size() > FILENAME_MAX)
2196  throw vpException(vpException::dimensionError, "Cannot initialize Afma6 simulator");
2197  unsigned int full_length = (unsigned int)scene_dir_.size() + name_length;
2198  if (full_length > FILENAME_MAX)
2199  throw vpException(vpException::dimensionError, "Cannot initialize Afma6 simulator");
2200 
2201  char *name_cam = new char[full_length];
2202 
2203  strcpy(name_cam, scene_dir_.c_str());
2204  strcat(name_cam, "/camera.bnd");
2205  set_scene(name_cam, &camera, cameraFactor);
2206 
2207  if (arm_dir.size() > FILENAME_MAX)
2208  throw vpException(vpException::dimensionError, "Cannot initialize Afma6 simulator");
2209  full_length = (unsigned int)arm_dir.size() + name_length;
2210  if (full_length > FILENAME_MAX)
2211  throw vpException(vpException::dimensionError, "Cannot initialize Afma6 simulator");
2212 
2213  char *name_arm = new char[full_length];
2214  strcpy(name_arm, arm_dir.c_str());
2215  strcat(name_arm, "/afma6_gate.bnd");
2216  std::cout << "name arm: " << name_arm << std::endl;
2217  set_scene(name_arm, robotArms, 1.0);
2218  strcpy(name_arm, arm_dir.c_str());
2219  strcat(name_arm, "/afma6_arm1.bnd");
2220  set_scene(name_arm, robotArms + 1, 1.0);
2221  strcpy(name_arm, arm_dir.c_str());
2222  strcat(name_arm, "/afma6_arm2.bnd");
2223  set_scene(name_arm, robotArms + 2, 1.0);
2224  strcpy(name_arm, arm_dir.c_str());
2225  strcat(name_arm, "/afma6_arm3.bnd");
2226  set_scene(name_arm, robotArms + 3, 1.0);
2227  strcpy(name_arm, arm_dir.c_str());
2228  strcat(name_arm, "/afma6_arm4.bnd");
2229  set_scene(name_arm, robotArms + 4, 1.0);
2230 
2232  tool = getToolType();
2233  strcpy(name_arm, arm_dir.c_str());
2234  switch (tool) {
2235  case vpAfma6::TOOL_CCMOP: {
2236  strcat(name_arm, "/afma6_tool_ccmop.bnd");
2237  break;
2238  }
2239  case vpAfma6::TOOL_GRIPPER: {
2240  strcat(name_arm, "/afma6_tool_gripper.bnd");
2241  break;
2242  }
2243  case vpAfma6::TOOL_VACUUM: {
2244  strcat(name_arm, "/afma6_tool_vacuum.bnd");
2245  break;
2246  }
2247  case vpAfma6::TOOL_CUSTOM: {
2248  std::cout << "The custom tool is not handled in vpSimulatorAfma6.cpp" << std::endl;
2249  break;
2250  }
2252  std::cout << "The generic camera is not handled in vpSimulatorAfma6.cpp" << std::endl;
2253  break;
2254  }
2255  }
2256  set_scene(name_arm, robotArms + 5, 1.0);
2257 
2258  add_rfstack(IS_BACK);
2259 
2260  add_vwstack("start", "depth", 0.0, 100.0);
2261  add_vwstack("start", "window", -0.1, 0.1, -0.1, 0.1);
2262  add_vwstack("start", "type", PERSPECTIVE);
2263  //
2264  // sceneInitialized = true;
2265  // displayObject = true;
2266  displayCamera = true;
2267 
2268  delete[] name_cam;
2269  delete[] name_arm;
2270 }
2271 
2273 {
2274  bool changed = false;
2275  vpHomogeneousMatrix displacement = navigation(I_, changed);
2276 
2277  // if (displacement[2][3] != 0)
2278  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon())
2279  camMf2 = camMf2 * displacement;
2280 
2281  f2Mf = camMf2.inverse() * camMf;
2282 
2283  camMf = camMf2 * displacement * f2Mf;
2284 
2285  double u;
2286  double v;
2287  // if(px_ext != 1 && py_ext != 1)
2288  // we assume px_ext and py_ext > 0
2289  if ((std::fabs(px_ext - 1.) > vpMath::maximum(px_ext, 1.) * std::numeric_limits<double>::epsilon()) &&
2290  (std::fabs(py_ext - 1) > vpMath::maximum(py_ext, 1.) * std::numeric_limits<double>::epsilon())) {
2291  u = (double)I_.getWidth() / (2 * px_ext);
2292  v = (double)I_.getHeight() / (2 * py_ext);
2293  } else {
2294  u = (double)I_.getWidth() / (vpMath::minimum(I_.getWidth(), I_.getHeight()));
2295  v = (double)I_.getHeight() / (vpMath::minimum(I_.getWidth(), I_.getHeight()));
2296  }
2297 
2298  float w44o[4][4], w44cext[4][4], x, y, z;
2299 
2300  vp2jlc_matrix(camMf.inverse(), w44cext);
2301 
2302  add_vwstack("start", "cop", w44cext[3][0], w44cext[3][1], w44cext[3][2]);
2303  x = w44cext[2][0] + w44cext[3][0];
2304  y = w44cext[2][1] + w44cext[3][1];
2305  z = w44cext[2][2] + w44cext[3][2];
2306  add_vwstack("start", "vrp", x, y, z);
2307  add_vwstack("start", "vpn", w44cext[2][0], w44cext[2][1], w44cext[2][2]);
2308  add_vwstack("start", "vup", w44cext[1][0], w44cext[1][1], w44cext[1][2]);
2309  add_vwstack("start", "window", -u, u, -v, v);
2310 
2311  vpHomogeneousMatrix fMit[8];
2312  get_fMi(fMit);
2313 
2314  vp2jlc_matrix(vpHomogeneousMatrix(0, 0, 0, 0, 0, 0), w44o);
2315  display_scene(w44o, robotArms[0], I_, curColor);
2316 
2317  vp2jlc_matrix(fMit[0], w44o);
2318  display_scene(w44o, robotArms[1], I_, curColor);
2319 
2320  vp2jlc_matrix(fMit[2], w44o);
2321  display_scene(w44o, robotArms[2], I_, curColor);
2322 
2323  vp2jlc_matrix(fMit[3], w44o);
2324  display_scene(w44o, robotArms[3], I_, curColor);
2325 
2326  vp2jlc_matrix(fMit[4], w44o);
2327  display_scene(w44o, robotArms[4], I_, curColor);
2328 
2329  vp2jlc_matrix(fMit[5], w44o);
2330  display_scene(w44o, robotArms[5], I_, curColor);
2331 
2332  if (displayCamera) {
2333  vpHomogeneousMatrix cMe;
2334  get_cMe(cMe);
2335  cMe = cMe.inverse();
2336  cMe = fMit[6] * cMe;
2337  vp2jlc_matrix(cMe, w44o);
2338  display_scene(w44o, camera, I_, camColor);
2339  }
2340 
2341  if (displayObject) {
2342  vp2jlc_matrix(fMo, w44o);
2343  display_scene(w44o, scene, I_, curColor);
2344  }
2345 }
2346 
2365 {
2366  vpColVector stop(6);
2367  bool status = true;
2368  stop = 0;
2369  set_artVel(stop);
2370  set_velocity(stop);
2371  vpHomogeneousMatrix fMc_;
2372  fMc_ = fMo * cMo_.inverse();
2373 
2374  vpColVector articularCoordinates = get_artCoord();
2375  int nbSol = getInverseKinematics(fMc_, articularCoordinates, true, verbose_);
2376 
2377  if (nbSol == 0) {
2378  status = false;
2379  vpERROR_TRACE("Positionning error. Position unreachable");
2380  }
2381 
2382  if (verbose_)
2383  std::cout << "Used joint coordinates (rad): " << articularCoordinates.t() << std::endl;
2384 
2385  set_artCoord(articularCoordinates);
2386 
2387  compute_fMi();
2388 
2389  return status;
2390 }
2391 
2406 {
2407  vpColVector stop(6);
2408  stop = 0;
2409  set_artVel(stop);
2410  set_velocity(stop);
2411  vpHomogeneousMatrix fMit[8];
2412  get_fMi(fMit);
2413  fMo = fMit[7] * cMo_;
2414 }
2415 
2427 bool vpSimulatorAfma6::setPosition(const vpHomogeneousMatrix &cdMo_, vpImage<unsigned char> *Iint, const double &errMax)
2428 {
2429  // get rid of max velocity
2430  double vMax = getMaxTranslationVelocity();
2431  double wMax = getMaxRotationVelocity();
2432  setMaxTranslationVelocity(10. * vMax);
2433  setMaxRotationVelocity(10. * wMax);
2434 
2435  vpColVector v(3), w(3), vel(6);
2436  vpHomogeneousMatrix cdMc;
2437  vpTranslationVector cdTc;
2438  vpRotationMatrix cdRc;
2439  vpThetaUVector cdTUc;
2440  vpColVector err(6);
2441  err = 1.;
2442  const double lambda = 5.;
2443 
2445 
2446  unsigned int i, iter = 0;
2447  while ((iter++ < 300) & (err.frobeniusNorm() > errMax)) {
2448  double t = vpTime::measureTimeMs();
2449 
2450  // update image
2451  if (Iint != NULL) {
2452  vpDisplay::display(*Iint);
2453  getInternalView(*Iint);
2454  vpDisplay::flush(*Iint);
2455  }
2456 
2457  // update pose error
2458  cdMc = cdMo_ * get_cMo().inverse();
2459  cdMc.extract(cdRc);
2460  cdMc.extract(cdTc);
2461  cdTUc.buildFrom(cdRc);
2462 
2463  // compute v,w and velocity
2464  v = -lambda * cdRc.t() * cdTc;
2465  w = -lambda * cdTUc;
2466  for (i = 0; i < 3; ++i) {
2467  vel[i] = v[i];
2468  vel[i + 3] = w[i];
2469  err[i] = cdTc[i];
2470  err[i + 3] = cdTUc[i];
2471  }
2472 
2473  // update feat
2475 
2476  // wait for it
2477  vpTime::wait(t, 10);
2478  }
2479  vel = 0.;
2480  set_velocity(vel);
2481  set_artVel(vel);
2483  setMaxRotationVelocity(wMax);
2484 
2485  // std::cout << "setPosition: final error " << err.t() << std::endl;
2486  return (err.frobeniusNorm() <= errMax);
2487 }
2488 
2489 #elif !defined(VISP_BUILD_SHARED_LIBS)
2490 // Work arround to avoid warning: libvisp_robot.a(vpSimulatorAfma6.cpp.o) has
2491 // no symbols
2492 void dummy_vpSimulatorAfma6(){};
2493 #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
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:164
vpMatrix pseudoInverse(double svThreshold=1e-6) const
Definition: vpMatrix.cpp:2206
vpRxyzVector _erc
Definition: vpAfma6.h:198
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:173
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.
double frobeniusNorm() const
vpHomogeneousMatrix getExternalCameraPosition() const
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)
#define vpERROR_TRACE
Definition: vpDebug.h:393
VISP_EXPORT double measureTimeSecond()
Definition: vpTime.cpp:158
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:531
static const vpColor none
Definition: vpColor.h:191
Initialize the position controller.
Definition: vpRobot.h:67
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:289
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:182
vpHomogeneousMatrix fMo
static void flush(const vpImage< unsigned char > &I)
vpHomogeneousMatrix get_fMc(const vpColVector &q) const
Definition: vpAfma6.cpp:706
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:126
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
void setMaxRotationVelocity(double maxVr)
Definition: vpRobot.cpp:260
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:143
Implementation of a rotation matrix and operations on such kind of matrices.
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:422
void getDisplacement(const vpRobot::vpControlFrameType frame, vpColVector &displacement)
Initialize the velocity controller.
Definition: vpRobot.h:66
static bool readPosFile(const std::string &filename, vpColVector &q)
virtual vpRobotStateType getRobotState(void) const
Definition: vpRobot.h:144
vpRobotStateType
Definition: vpRobot.h:64
void initialiseObjectRelativeToCamera(const vpHomogeneousMatrix &cMo)
Initialize the acceleration controller.
Definition: vpRobot.h:68
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:863
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:1767
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:151
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:933
static double rad(double deg)
Definition: vpMath.h:108
void setExternalCameraParameters(const vpCameraParameters &cam)
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
Stops robot motion especially in velocity and acceleration control.
Definition: vpRobot.h:65
This class aims to be a basis used to create all the simulators of robots.
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:310
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:101
unsigned int getHeight() const
Definition: vpImage.h:186
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
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:431
void get_fJe(vpMatrix &fJe)
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:151
void get_cMe(vpHomogeneousMatrix &cMe) const
Definition: vpAfma6.cpp:819
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:433
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:183
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:172
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)
VISP_EXPORT double getMinTimeForUsleepCall()
Definition: vpTime.cpp:86
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:244
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
void setMaxTranslationVelocity(double maxVt)
Definition: vpRobot.cpp:239
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)
static std::string getenv(const std::string &env)
Definition: vpIoTools.cpp:355