Visual Servoing Platform  version 3.1.0
vpSimulatorViper850.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 Viper850.
33  *
34  * Authors:
35  * Nicolas Melchior
36  *
37  *****************************************************************************/
38 
39 #include <visp3/robot/vpSimulatorViper850.h>
40 
41 #if defined(VISP_HAVE_MODULE_GUI) && ((defined(_WIN32) && !defined(WINRT_8_0)) || defined(VISP_HAVE_PTHREAD))
42 
43 #include <cmath> // std::fabs
44 #include <limits> // numeric_limits
45 #include <string>
46 #include <visp3/core/vpImagePoint.h>
47 #include <visp3/core/vpIoTools.h>
48 #include <visp3/core/vpMeterPixelConversion.h>
49 #include <visp3/core/vpPoint.h>
50 #include <visp3/core/vpTime.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(), vpViper850(), 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  // free_Bound_scene (&(camera));
193  for (int i = 0; i < 6; i++)
194  free_Bound_scene(&(robotArms[i]));
195  }
196 
197  delete[] robotArms;
198  delete[] fMi;
199 }
200 
210 {
211  // set arm_dir from #define VISP_ROBOT_ARMS_DIR if it exists
212  // VISP_ROBOT_ARMS_DIR may contain multiple locations separated by ";"
213  std::vector<std::string> arm_dirs = vpIoTools::splitChain(std::string(VISP_ROBOT_ARMS_DIR), std::string(";"));
214  bool armDirExists = false;
215  for (size_t i = 0; i < arm_dirs.size(); i++)
216  if (vpIoTools::checkDirectory(arm_dirs[i]) == true) { // directory exists
217  arm_dir = arm_dirs[i];
218  armDirExists = true;
219  break;
220  }
221  if (!armDirExists) {
222  try {
223  arm_dir = vpIoTools::getenv("VISP_ROBOT_ARMS_DIR");
224  std::cout << "The simulator uses data from VISP_ROBOT_ARMS_DIR=" << arm_dir << std::endl;
225  } catch (...) {
226  std::cout << "Cannot get VISP_ROBOT_ARMS_DIR environment variable" << std::endl;
227  }
228  }
229 
231  toolCustom = false;
232 
233  size_fMi = 8;
234  fMi = new vpHomogeneousMatrix[8];
237 
238  zeroPos.resize(njoint);
239  zeroPos = 0;
240  zeroPos[1] = -M_PI / 2;
241  zeroPos[2] = M_PI;
242  reposPos.resize(njoint);
243  reposPos = 0;
244  reposPos[1] = -M_PI / 2;
245  reposPos[2] = M_PI;
246  reposPos[4] = M_PI / 2;
247 
248  artCoord = reposPos;
249  artVel = 0;
250 
251  q_prev_getdis.resize(njoint);
252  q_prev_getdis = 0;
253  first_time_getdis = true;
254 
255  positioningVelocity = defaultPositioningVelocity;
256 
259 
260  // Software joint limits in radians
261  // joint_min.resize(njoint);
262  joint_min[0] = vpMath::rad(-50);
263  joint_min[1] = vpMath::rad(-135);
264  joint_min[2] = vpMath::rad(-40);
265  joint_min[3] = vpMath::rad(-190);
266  joint_min[4] = vpMath::rad(-110);
267  joint_min[5] = vpMath::rad(-184);
268  // joint_max.resize(njoint);
269  joint_max[0] = vpMath::rad(50);
270  joint_max[1] = vpMath::rad(-40);
271  joint_max[2] = vpMath::rad(215);
272  joint_max[3] = vpMath::rad(190);
273  joint_max[4] = vpMath::rad(110);
274  joint_max[5] = vpMath::rad(184);
275 }
276 
281 {
282  robotArms = NULL;
283  robotArms = new Bound_scene[6];
284  initArms();
285  setExternalCameraPosition(vpHomogeneousMatrix(0.0, 0.5, 1.5, vpMath::rad(90), 0, 0));
286  cameraParam.initPersProjWithoutDistortion(558.5309599, 556.055053, 320, 240);
288  vpCameraParameters tmp;
289  getCameraParameters(tmp, 640, 480);
290  px_int = tmp.get_px();
291  py_int = tmp.get_py();
292  sceneInitialized = true;
293 }
294 
311 {
312  this->projModel = proj_model;
313 
314  // Use here default values of the robot constant parameters.
315  switch (tool) {
317  erc[0] = vpMath::rad(0.07); // rx
318  erc[1] = vpMath::rad(2.76); // ry
319  erc[2] = vpMath::rad(-91.50); // rz
320  etc[0] = -0.0453; // tx
321  etc[1] = 0.0005; // ty
322  etc[2] = 0.0728; // tz
323 
324  setCameraParameters(vpCameraParameters(1232.0, 1233.0, 320, 240));
325  break;
326  }
328  erc[0] = vpMath::rad(0.1527764261); // rx
329  erc[1] = vpMath::rad(1.285092455); // ry
330  erc[2] = vpMath::rad(-90.75777848); // rz
331  etc[0] = -0.04558630174; // tx
332  etc[1] = -0.00134326752; // ty
333  etc[2] = 0.001000828017; // tz
334 
335  setCameraParameters(vpCameraParameters(868.0, 869.0, 320, 240));
336  break;
337  }
341  std::cout << "This tool is not handled in vpSimulatorViper850.cpp" << std::endl;
342  break;
343  }
344  }
345 
346  vpRotationMatrix eRc(erc);
347  this->eMc.buildFrom(etc, eRc);
348 
349  setToolType(tool);
350  return;
351 }
352 
363 void vpSimulatorViper850::getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width,
364  const unsigned int &image_height)
365 {
366  if (toolCustom) {
367  cam.initPersProjWithoutDistortion(px_int, py_int, image_width / 2, image_height / 2);
368  }
369  // Set default parameters
370  switch (getToolType()) {
372  // Set default intrinsic camera parameters for 640x480 images
373  if (image_width == 640 && image_height == 480) {
374  std::cout << "Get default camera parameters for camera \"" << vpViper850::CONST_MARLIN_F033C_CAMERA_NAME << "\""
375  << std::endl;
376  cam.initPersProjWithoutDistortion(1232.0, 1233.0, 320, 240);
377  } else {
378  vpTRACE("Cannot get default intrinsic camera parameters for this image "
379  "resolution");
380  }
381  break;
382  }
384  // Set default intrinsic camera parameters for 640x480 images
385  if (image_width == 640 && image_height == 480) {
386  std::cout << "Get default camera parameters for camera \"" << vpViper850::CONST_PTGREY_FLEA2_CAMERA_NAME << "\""
387  << std::endl;
388  cam.initPersProjWithoutDistortion(868.0, 869.0, 320, 240);
389  } else {
390  vpTRACE("Cannot get default intrinsic camera parameters for this image "
391  "resolution");
392  }
393  break;
394  }
398  std::cout << "This tool is not handled in vpSimulatorViper850.cpp" << std::endl;
399  break;
400  }
401  }
402  return;
403 }
404 
414 {
415  getCameraParameters(cam, I_.getWidth(), I_.getHeight());
416 }
417 
427 {
428  getCameraParameters(cam, I_.getWidth(), I_.getHeight());
429 }
430 
437 {
438  px_int = cam.get_px();
439  py_int = cam.get_py();
440  toolCustom = true;
441 }
442 
448 {
449  double tcur_1 = tcur; // temporary variable used to store the last time
450  // since the last command
451 
452  while (!robotStop) {
453  // Get current time
454  tprev = tcur_1;
456 
458  setVelocityCalled = false;
460 
461  double ellapsedTime = (tcur - tprev) * 1e-3;
462  if (constantSamplingTimeMode) { // if we want a constant velocity, we
463  // force the ellapsed time to the given
464  // samplingTime
465  ellapsedTime = getSamplingTime(); // in second
466  }
467 
468  vpColVector articularCoordinates = get_artCoord();
469  vpColVector articularVelocities = get_artVel();
470 
471  if (jointLimit) {
472  double art = articularCoordinates[jointLimitArt - 1] + ellapsedTime * articularVelocities[jointLimitArt - 1];
473  if (art <= joint_min[jointLimitArt - 1] || art >= joint_max[jointLimitArt - 1]) {
474  if (verbose_) {
475  std::cout << "Joint " << jointLimitArt - 1
476  << " reaches a limit: " << vpMath::deg(joint_min[jointLimitArt - 1]) << " < " << vpMath::deg(art)
477  << " < " << vpMath::deg(joint_max[jointLimitArt - 1]) << std::endl;
478  }
479  articularVelocities = 0.0;
480  } else
481  jointLimit = false;
482  }
483 
484  articularCoordinates[0] = articularCoordinates[0] + ellapsedTime * articularVelocities[0];
485  articularCoordinates[1] = articularCoordinates[1] + ellapsedTime * articularVelocities[1];
486  articularCoordinates[2] = articularCoordinates[2] + ellapsedTime * articularVelocities[2];
487  articularCoordinates[3] = articularCoordinates[3] + ellapsedTime * articularVelocities[3];
488  articularCoordinates[4] = articularCoordinates[4] + ellapsedTime * articularVelocities[4];
489  articularCoordinates[5] = articularCoordinates[5] + ellapsedTime * articularVelocities[5];
490 
491  int jl = isInJointLimit();
492 
493  if (jl != 0 && jointLimit == false) {
494  if (jl < 0)
495  ellapsedTime = (joint_min[(unsigned int)(-jl - 1)] - articularCoordinates[(unsigned int)(-jl - 1)]) /
496  (articularVelocities[(unsigned int)(-jl - 1)]);
497  else
498  ellapsedTime = (joint_max[(unsigned int)(jl - 1)] - articularCoordinates[(unsigned int)(jl - 1)]) /
499  (articularVelocities[(unsigned int)(jl - 1)]);
500 
501  for (unsigned int i = 0; i < 6; i++)
502  articularCoordinates[i] = articularCoordinates[i] + ellapsedTime * articularVelocities[i];
503 
504  jointLimit = true;
505  jointLimitArt = (unsigned int)fabs((double)jl);
506  }
507 
508  set_artCoord(articularCoordinates);
509  set_artVel(articularVelocities);
510 
511  compute_fMi();
512 
513  if (displayAllowed) {
517  }
518 
519  if (displayType == MODEL_3D && displayAllowed) {
520  while (get_displayBusy())
521  vpTime::wait(2);
523  set_displayBusy(false);
524  }
525 
526  if (displayType == MODEL_DH && displayAllowed) {
527  vpHomogeneousMatrix fMit[8];
528  get_fMi(fMit);
529 
530  // vpDisplay::displayFrame(I,getExternalCameraPosition
531  // ()*fMi[6],cameraParam,0.2,vpColor::none);
532 
533  vpImagePoint iP, iP_1;
534  vpPoint pt(0, 0, 0);
535 
538  pt.track(getExternalCameraPosition() * fMit[0]);
541  for (int k = 1; k < 7; k++) {
542  pt.track(getExternalCameraPosition() * fMit[k - 1]);
544 
545  pt.track(getExternalCameraPosition() * fMit[k]);
547 
549  }
551  thickness_);
552  }
553 
555 
556  vpTime::wait(tcur, 1000 * getSamplingTime());
557  tcur_1 = tcur;
558  } else {
560  }
561  }
562 }
563 
577 {
578  // vpColVector q = get_artCoord();
579  vpColVector q(6); //; = get_artCoord();
580  q = get_artCoord();
581 
582  vpHomogeneousMatrix fMit[8];
583 
584  double q1 = q[0];
585  double q2 = q[1];
586  double q3 = q[2];
587  double q4 = q[3];
588  double q5 = q[4];
589  double q6 = q[5];
590 
591  double c1 = cos(q1);
592  double s1 = sin(q1);
593  double c2 = cos(q2);
594  double s2 = sin(q2);
595  double c23 = cos(q2 + q3);
596  double s23 = sin(q2 + q3);
597  double c4 = cos(q4);
598  double s4 = sin(q4);
599  double c5 = cos(q5);
600  double s5 = sin(q5);
601  double c6 = cos(q6);
602  double s6 = sin(q6);
603 
604  fMit[0][0][0] = c1;
605  fMit[0][1][0] = s1;
606  fMit[0][2][0] = 0;
607  fMit[0][0][1] = 0;
608  fMit[0][1][1] = 0;
609  fMit[0][2][1] = -1;
610  fMit[0][0][2] = -s1;
611  fMit[0][1][2] = c1;
612  fMit[0][2][2] = 0;
613  fMit[0][0][3] = a1 * c1;
614  fMit[0][1][3] = a1 * s1;
615  fMit[0][2][3] = d1;
616 
617  fMit[1][0][0] = c1 * c2;
618  fMit[1][1][0] = s1 * c2;
619  fMit[1][2][0] = -s2;
620  fMit[1][0][1] = -c1 * s2;
621  fMit[1][1][1] = -s1 * s2;
622  fMit[1][2][1] = -c2;
623  fMit[1][0][2] = -s1;
624  fMit[1][1][2] = c1;
625  fMit[1][2][2] = 0;
626  fMit[1][0][3] = c1 * (a2 * c2 + a1);
627  fMit[1][1][3] = s1 * (a2 * c2 + a1);
628  fMit[1][2][3] = d1 - a2 * s2;
629 
630  double quickcomp1 = a3 * c23 - a2 * c2 - a1;
631 
632  fMit[2][0][0] = -c1 * c23;
633  fMit[2][1][0] = -s1 * c23;
634  fMit[2][2][0] = s23;
635  fMit[2][0][1] = s1;
636  fMit[2][1][1] = -c1;
637  fMit[2][2][1] = 0;
638  fMit[2][0][2] = c1 * s23;
639  fMit[2][1][2] = s1 * s23;
640  fMit[2][2][2] = c23;
641  fMit[2][0][3] = -c1 * quickcomp1;
642  fMit[2][1][3] = -s1 * quickcomp1;
643  fMit[2][2][3] = a3 * s23 - a2 * s2 + d1;
644 
645  double quickcomp2 = c1 * (s23 * d4 - quickcomp1);
646  double quickcomp3 = s1 * (s23 * d4 - quickcomp1);
647 
648  fMit[3][0][0] = -c1 * c23 * c4 + s1 * s4;
649  fMit[3][1][0] = -s1 * c23 * c4 - c1 * s4;
650  fMit[3][2][0] = s23 * c4;
651  fMit[3][0][1] = c1 * s23;
652  fMit[3][1][1] = s1 * s23;
653  fMit[3][2][1] = c23;
654  fMit[3][0][2] = -c1 * c23 * s4 - s1 * c4;
655  fMit[3][1][2] = -s1 * c23 * s4 + c1 * c4;
656  fMit[3][2][2] = s23 * s4;
657  fMit[3][0][3] = quickcomp2;
658  fMit[3][1][3] = quickcomp3;
659  fMit[3][2][3] = c23 * d4 + a3 * s23 - a2 * s2 + d1;
660 
661  fMit[4][0][0] = c1 * (s23 * s5 - c5 * c23 * c4) + s1 * c5 * s4;
662  fMit[4][1][0] = s1 * (s23 * s5 - c5 * c23 * c4) - c1 * c5 * s4;
663  fMit[4][2][0] = s23 * c4 * c5 + c23 * s5;
664  fMit[4][0][1] = c1 * c23 * s4 + s1 * c4;
665  fMit[4][1][1] = s1 * c23 * s4 - c1 * c4;
666  fMit[4][2][1] = -s23 * s4;
667  fMit[4][0][2] = c1 * (s23 * c5 + s5 * c23 * c4) - s1 * s5 * s4;
668  fMit[4][1][2] = s1 * (s23 * c5 + s5 * c23 * c4) + c1 * s5 * s4;
669  fMit[4][2][2] = -s23 * c4 * s5 + c23 * c5;
670  fMit[4][0][3] = quickcomp2;
671  fMit[4][1][3] = quickcomp3;
672  fMit[4][2][3] = c23 * d4 + a3 * s23 - a2 * s2 + d1;
673 
674  fMit[5][0][0] = c1 * (c23 * (c4 * c5 * c6 - s4 * s6) - s23 * s5 * c6) - s1 * (s4 * c5 * c6 + c4 * s6);
675  fMit[5][1][0] = -s1 * (c23 * (-c4 * c5 * c6 + s4 * s6) + s23 * s5 * c6) + c1 * (s4 * c5 * c6 + c4 * s6);
676  fMit[5][2][0] = s23 * (s4 * s6 - c4 * c5 * c6) - c23 * s5 * c6;
677  fMit[5][0][1] = -c1 * (c23 * (c4 * c5 * s6 + s4 * c6) - s23 * s5 * s6) + s1 * (s4 * c5 * s6 - c4 * c6);
678  fMit[5][1][1] = -s1 * (c23 * (c4 * c5 * s6 + s4 * c6) - s23 * s5 * s6) - c1 * (s4 * c5 * s6 - c4 * c6);
679  fMit[5][2][1] = s23 * (c4 * c5 * s6 + s4 * c6) + c23 * s5 * s6;
680  fMit[5][0][2] = c1 * (c23 * c4 * s5 + s23 * c5) - s1 * s4 * s5;
681  fMit[5][1][2] = s1 * (c23 * c4 * s5 + s23 * c5) + c1 * s4 * s5;
682  fMit[5][2][2] = -s23 * c4 * s5 + c23 * c5;
683  fMit[5][0][3] = quickcomp2;
684  fMit[5][1][3] = quickcomp3;
685  fMit[5][2][3] = s23 * a3 + c23 * d4 - a2 * s2 + d1;
686 
687  fMit[6][0][0] = c1 * (c23 * (c4 * c5 * c6 - s4 * s6) - s23 * s5 * c6) - s1 * (s4 * c5 * c6 + c4 * s6);
688  fMit[6][1][0] = -s1 * (c23 * (-c4 * c5 * c6 + s4 * s6) + s23 * s5 * c6) + c1 * (s4 * c5 * c6 + c4 * s6);
689  fMit[6][2][0] = s23 * (s4 * s6 - c4 * c5 * c6) - c23 * s5 * c6;
690  fMit[6][0][1] = -c1 * (c23 * (c4 * c5 * s6 + s4 * c6) - s23 * s5 * s6) + s1 * (s4 * c5 * s6 - c4 * c6);
691  fMit[6][1][1] = -s1 * (c23 * (c4 * c5 * s6 + s4 * c6) - s23 * s5 * s6) - c1 * (s4 * c5 * s6 - c4 * c6);
692  fMit[6][2][1] = s23 * (c4 * c5 * s6 + s4 * c6) + c23 * s5 * s6;
693  fMit[6][0][2] = c1 * (c23 * c4 * s5 + s23 * c5) - s1 * s4 * s5;
694  fMit[6][1][2] = s1 * (c23 * c4 * s5 + s23 * c5) + c1 * s4 * s5;
695  fMit[6][2][2] = -s23 * c4 * s5 + c23 * c5;
696  fMit[6][0][3] = c1 * (c23 * (c4 * s5 * d6 - a3) + s23 * (c5 * d6 + d4) + a1 + a2 * c2) - s1 * s4 * s5 * d6;
697  fMit[6][1][3] = s1 * (c23 * (c4 * s5 * d6 - a3) + s23 * (c5 * d6 + d4) + a1 + a2 * c2) + c1 * s4 * s5 * d6;
698  fMit[6][2][3] = s23 * (a3 - c4 * s5 * d6) + c23 * (c5 * d6 + d4) - a2 * s2 + d1;
699 
701  get_cMe(cMe);
702  cMe = cMe.inverse();
703  // fMit[7] = fMit[6] * cMe;
704  vpViper::get_fMc(q, fMit[7]);
705 
706 #if defined(_WIN32)
707 #if defined(WINRT_8_1)
708  WaitForSingleObjectEx(mutex_fMi, INFINITE, FALSE);
709 #else // pure win32
710  WaitForSingleObject(mutex_fMi, INFINITE);
711 #endif
712  for (int i = 0; i < 8; i++)
713  fMi[i] = fMit[i];
714  ReleaseMutex(mutex_fMi);
715 #elif defined(VISP_HAVE_PTHREAD)
716  pthread_mutex_lock(&mutex_fMi);
717  for (int i = 0; i < 8; i++)
718  fMi[i] = fMit[i];
719  pthread_mutex_unlock(&mutex_fMi);
720 #endif
721 }
722 
729 {
730  switch (newState) {
731  case vpRobot::STATE_STOP: {
732  // Start primitive STOP only if the current state is Velocity
734  stopMotion();
735  }
736  break;
737  }
740  std::cout << "Change the control mode from velocity to position control.\n";
741  stopMotion();
742  } else {
743  // std::cout << "Change the control mode from stop to position
744  // control.\n";
745  }
746  break;
747  }
750  std::cout << "Change the control mode from stop to velocity control.\n";
751  }
752  break;
753  }
755  default:
756  break;
757  }
758 
759  return vpRobot::setRobotState(newState);
760 }
761 
837 {
839  vpERROR_TRACE("Cannot send a velocity to the robot "
840  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
842  "Cannot send a velocity to the robot "
843  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
844  }
845 
846  vpColVector vel_sat(6);
847 
848  double scale_sat = 1;
849  double vel_trans_max = getMaxTranslationVelocity();
850  double vel_rot_max = getMaxRotationVelocity();
851 
852  double vel_abs; // Absolute value
853 
854  // Velocity saturation
855  switch (frame) {
856  // saturation in cartesian space
859  if (vel.getRows() != 6) {
860  vpERROR_TRACE("The velocity vector must have a size of 6 !!!!");
861  throw;
862  }
863 
864  for (unsigned int i = 0; i < 3; ++i) {
865  vel_abs = fabs(vel[i]);
866  if (vel_abs > vel_trans_max && !jointLimit) {
867  vel_trans_max = vel_abs;
868  vpERROR_TRACE("Excess velocity %g m/s in TRANSLATION "
869  "(axis nr. %d).",
870  vel[i], i + 1);
871  }
872 
873  vel_abs = fabs(vel[i + 3]);
874  if (vel_abs > vel_rot_max && !jointLimit) {
875  vel_rot_max = vel_abs;
876  vpERROR_TRACE("Excess velocity %g rad/s in ROTATION "
877  "(axis nr. %d).",
878  vel[i + 3], i + 4);
879  }
880  }
881 
882  double scale_trans_sat = 1;
883  double scale_rot_sat = 1;
884  if (vel_trans_max > getMaxTranslationVelocity())
885  scale_trans_sat = getMaxTranslationVelocity() / vel_trans_max;
886 
887  if (vel_rot_max > getMaxRotationVelocity())
888  scale_rot_sat = getMaxRotationVelocity() / vel_rot_max;
889 
890  if ((scale_trans_sat < 1) || (scale_rot_sat < 1)) {
891  if (scale_trans_sat < scale_rot_sat)
892  scale_sat = scale_trans_sat;
893  else
894  scale_sat = scale_rot_sat;
895  }
896  break;
897  }
898 
899  // saturation in joint space
901  if (vel.getRows() != 6) {
902  vpERROR_TRACE("The velocity vector must have a size of 6 !!!!");
903  throw;
904  }
905  for (unsigned int i = 0; i < 6; ++i) {
906  vel_abs = fabs(vel[i]);
907  if (vel_abs > vel_rot_max && !jointLimit) {
908  vel_rot_max = vel_abs;
909  vpERROR_TRACE("Excess velocity %g rad/s in ROTATION "
910  "(axis nr. %d).",
911  vel[i], i + 1);
912  }
913  }
914  double scale_rot_sat = 1;
915  if (vel_rot_max > getMaxRotationVelocity())
916  scale_rot_sat = getMaxRotationVelocity() / vel_rot_max;
917  if (scale_rot_sat < 1)
918  scale_sat = scale_rot_sat;
919  break;
920  }
921  case vpRobot::MIXT_FRAME: {
922  break;
923  }
924  }
925 
926  set_velocity(vel * scale_sat);
927  setRobotFrame(frame);
928  setVelocityCalled = true;
929 }
930 
935 {
937 
938  double vel_rot_max = getMaxRotationVelocity();
939 
940  vpColVector articularCoordinates = get_artCoord();
941  vpColVector velocityframe = get_velocity();
942  vpColVector articularVelocity;
943 
944  switch (frame) {
945  case vpRobot::CAMERA_FRAME: {
946  vpMatrix eJe_;
948  vpViper850::get_eJe(articularCoordinates, eJe_);
949  eJe_ = eJe_.pseudoInverse();
951  singularityTest(articularCoordinates, eJe_);
952  articularVelocity = eJe_ * eVc * velocityframe;
953  set_artVel(articularVelocity);
954  break;
955  }
957  vpMatrix fJe_;
958  vpViper850::get_fJe(articularCoordinates, fJe_);
959  fJe_ = fJe_.pseudoInverse();
961  singularityTest(articularCoordinates, fJe_);
962  articularVelocity = fJe_ * velocityframe;
963  set_artVel(articularVelocity);
964  break;
965  }
967  articularVelocity = velocityframe;
968  set_artVel(articularVelocity);
969  break;
970  }
971  case vpRobot::MIXT_FRAME: {
972  break;
973  }
974  }
975 
976  switch (frame) {
979  for (unsigned int i = 0; i < 6; ++i) {
980  double vel_abs = fabs(articularVelocity[i]);
981  if (vel_abs > vel_rot_max && !jointLimit) {
982  vel_rot_max = vel_abs;
983  vpERROR_TRACE("Excess velocity %g rad/s in ROTATION "
984  "(axis nr. %d).",
985  articularVelocity[i], i + 1);
986  }
987  }
988  double scale_rot_sat = 1;
989  double scale_sat = 1;
990 
991  if (vel_rot_max > getMaxRotationVelocity())
992  scale_rot_sat = getMaxRotationVelocity() / vel_rot_max;
993  if (scale_rot_sat < 1)
994  scale_sat = scale_rot_sat;
995 
996  set_artVel(articularVelocity * scale_sat);
997  break;
998  }
1000  case vpRobot::MIXT_FRAME: {
1001  break;
1002  }
1003  }
1004 }
1005 
1053 {
1054  vel.resize(6);
1055 
1056  vpColVector articularCoordinates = get_artCoord();
1057  vpColVector articularVelocity = get_artVel();
1058 
1059  switch (frame) {
1060  case vpRobot::CAMERA_FRAME: {
1061  vpMatrix eJe_;
1063  vpViper850::get_eJe(articularCoordinates, eJe_);
1064  vel = cVe * eJe_ * articularVelocity;
1065  break;
1066  }
1067  case vpRobot::ARTICULAR_FRAME: {
1068  vel = articularVelocity;
1069  break;
1070  }
1071  case vpRobot::REFERENCE_FRAME: {
1072  vpMatrix fJe_;
1073  vpViper850::get_fJe(articularCoordinates, fJe_);
1074  vel = fJe_ * articularVelocity;
1075  break;
1076  }
1077  case vpRobot::MIXT_FRAME: {
1078  break;
1079  }
1080  default: {
1081  vpERROR_TRACE("Error in spec of vpRobot. "
1082  "Case not taken in account.");
1083  return;
1084  }
1085  }
1086 }
1087 
1105 {
1106  timestamp = vpTime::measureTimeSecond();
1107  getVelocity(frame, vel);
1108 }
1109 
1153 {
1154  vpColVector vel(6);
1155  getVelocity(frame, vel);
1156 
1157  return vel;
1158 }
1159 
1173 {
1174  timestamp = vpTime::measureTimeSecond();
1175  vpColVector vel(6);
1176  getVelocity(frame, vel);
1177 
1178  return vel;
1179 }
1180 
1182 {
1183  double vel_rot_max = getMaxRotationVelocity();
1184  double velmax = fabs(q[0]);
1185  for (unsigned int i = 1; i < 6; i++) {
1186  if (velmax < fabs(q[i]))
1187  velmax = fabs(q[i]);
1188  }
1189 
1190  double alpha = (getPositioningVelocity() * vel_rot_max) / (velmax * 100);
1191  q = q * alpha;
1192 }
1193 
1270 {
1272  vpERROR_TRACE("Robot was not in position-based control\n"
1273  "Modification of the robot state");
1274  // setRobotState(vpRobot::STATE_POSITION_CONTROL) ;
1275  }
1276 
1277  vpColVector articularCoordinates = get_artCoord();
1278 
1279  vpColVector error(6);
1280  double errsqr = 0;
1281  switch (frame) {
1282  case vpRobot::CAMERA_FRAME: {
1283  unsigned int nbSol;
1284  vpColVector qdes(6);
1285 
1286  vpTranslationVector txyz;
1287  vpRxyzVector rxyz;
1288  for (unsigned int i = 0; i < 3; i++) {
1289  txyz[i] = q[i];
1290  rxyz[i] = q[i + 3];
1291  }
1292 
1293  vpRotationMatrix cRc2(rxyz);
1294  vpHomogeneousMatrix cMc2(txyz, cRc2);
1295 
1296  vpHomogeneousMatrix fMc_;
1297  vpViper850::get_fMc(articularCoordinates, fMc_);
1298 
1299  vpHomogeneousMatrix fMc2 = fMc_ * cMc2;
1300 
1301  do {
1302  articularCoordinates = get_artCoord();
1303  qdes = articularCoordinates;
1304  nbSol = getInverseKinematics(fMc2, qdes, verbose_);
1305  setVelocityCalled = true;
1306  if (nbSol > 0) {
1307  error = qdes - articularCoordinates;
1308  errsqr = error.sumSquare();
1309  // findHighestPositioningSpeed(error);
1310  set_artVel(error);
1311  if (errsqr < 1e-4) {
1312  set_artCoord(qdes);
1313  error = 0;
1314  set_artVel(error);
1315  set_velocity(error);
1316  break;
1317  }
1318  } else {
1319  vpERROR_TRACE("Positionning error.");
1320  throw vpRobotException(vpRobotException::positionOutOfRangeError, "Position out of range.");
1321  }
1322  } while (errsqr > 1e-8 && nbSol > 0);
1323 
1324  break;
1325  }
1326 
1327  case vpRobot::ARTICULAR_FRAME: {
1328  do {
1329  articularCoordinates = get_artCoord();
1330  error = q - articularCoordinates;
1331  errsqr = error.sumSquare();
1332  // findHighestPositioningSpeed(error);
1333  set_artVel(error);
1334  setVelocityCalled = true;
1335  if (errsqr < 1e-4) {
1336  set_artCoord(q);
1337  error = 0;
1338  set_artVel(error);
1339  set_velocity(error);
1340  break;
1341  }
1342  } while (errsqr > 1e-8);
1343  break;
1344  }
1345 
1346  case vpRobot::REFERENCE_FRAME: {
1347  unsigned int nbSol;
1348  vpColVector qdes(6);
1349 
1350  vpTranslationVector txyz;
1351  vpRxyzVector rxyz;
1352  for (unsigned int i = 0; i < 3; i++) {
1353  txyz[i] = q[i];
1354  rxyz[i] = q[i + 3];
1355  }
1356 
1357  vpRotationMatrix fRc(rxyz);
1358  vpHomogeneousMatrix fMc_(txyz, fRc);
1359 
1360  do {
1361  articularCoordinates = get_artCoord();
1362  qdes = articularCoordinates;
1363  nbSol = getInverseKinematics(fMc_, qdes, verbose_);
1364  if (nbSol > 0) {
1365  error = qdes - articularCoordinates;
1366  errsqr = error.sumSquare();
1367  // findHighestPositioningSpeed(error);
1368  set_artVel(error);
1369  setVelocityCalled = true;
1370  if (errsqr < 1e-4) {
1371  set_artCoord(qdes);
1372  error = 0;
1373  set_artVel(error);
1374  set_velocity(error);
1375  break;
1376  }
1377  } else
1378  vpERROR_TRACE("Positionning error. Position unreachable");
1379  } while (errsqr > 1e-8 && nbSol > 0);
1380  break;
1381  }
1382  case vpRobot::MIXT_FRAME: {
1383  vpERROR_TRACE("Positionning error. Mixt frame not implemented");
1384  throw vpRobotException(vpRobotException::lowLevelError, "Positionning error: "
1385  "Mixt frame not implemented.");
1386  }
1387  }
1388 }
1389 
1453 void vpSimulatorViper850::setPosition(const vpRobot::vpControlFrameType frame, const double pos1, const double pos2,
1454  const double pos3, const double pos4, const double pos5, const double pos6)
1455 {
1456  try {
1457  vpColVector position(6);
1458  position[0] = pos1;
1459  position[1] = pos2;
1460  position[2] = pos3;
1461  position[3] = pos4;
1462  position[4] = pos5;
1463  position[5] = pos6;
1464 
1465  setPosition(frame, position);
1466  } catch (...) {
1467  vpERROR_TRACE("Error caught");
1468  throw;
1469  }
1470 }
1471 
1507 void vpSimulatorViper850::setPosition(const char *filename)
1508 {
1509  vpColVector q;
1510  bool ret;
1511 
1512  ret = this->readPosFile(filename, q);
1513 
1514  if (ret == false) {
1515  vpERROR_TRACE("Bad position in \"%s\"", filename);
1516  throw vpRobotException(vpRobotException::lowLevelError, "Bad position in filename.");
1517  }
1520 }
1521 
1583 {
1584  q.resize(6);
1585 
1586  switch (frame) {
1587  case vpRobot::CAMERA_FRAME: {
1588  q = 0;
1589  break;
1590  }
1591 
1592  case vpRobot::ARTICULAR_FRAME: {
1593  q = get_artCoord();
1594  break;
1595  }
1596 
1597  case vpRobot::REFERENCE_FRAME: {
1598  vpHomogeneousMatrix fMc_;
1599  vpViper::get_fMc(get_artCoord(), fMc_);
1600 
1601  vpRotationMatrix fRc;
1602  fMc_.extract(fRc);
1603  vpRxyzVector rxyz(fRc);
1604 
1605  vpTranslationVector txyz;
1606  fMc_.extract(txyz);
1607 
1608  for (unsigned int i = 0; i < 3; i++) {
1609  q[i] = txyz[i];
1610  q[i + 3] = rxyz[i];
1611  }
1612  break;
1613  }
1614 
1615  case vpRobot::MIXT_FRAME: {
1616  vpERROR_TRACE("Positionning error. Mixt frame not implemented");
1617  throw vpRobotException(vpRobotException::lowLevelError, "Positionning error: "
1618  "Mixt frame not implemented.");
1619  }
1620  }
1621 }
1622 
1650 {
1651  timestamp = vpTime::measureTimeSecond();
1652  getPosition(frame, q);
1653 }
1654 
1667 {
1668  vpColVector posRxyz;
1669  // recupere position en Rxyz
1670  this->getPosition(frame, posRxyz);
1671 
1672  // recupere le vecteur thetaU correspondant
1673  vpThetaUVector RtuVect(vpRxyzVector(posRxyz[3], posRxyz[4], posRxyz[5]));
1674 
1675  // remplit le vpPoseVector avec translation et rotation ThetaU
1676  for (unsigned int j = 0; j < 3; j++) {
1677  position[j] = posRxyz[j];
1678  position[j + 3] = RtuVect[j];
1679  }
1680 }
1681 
1694  double &timestamp)
1695 {
1696  timestamp = vpTime::measureTimeSecond();
1697  getPosition(frame, position);
1698 }
1699 
1708 void vpSimulatorViper850::setJointLimit(const vpColVector &limitMin, const vpColVector &limitMax)
1709 {
1710  if (limitMin.getRows() != 6 || limitMax.getRows() != 6) {
1711  vpTRACE("Joint limit vector has not a size of 6 !");
1712  return;
1713  }
1714 
1715  joint_min[0] = limitMin[0];
1716  joint_min[1] = limitMin[1];
1717  joint_min[2] = limitMin[2];
1718  joint_min[3] = limitMin[3];
1719  joint_min[4] = limitMin[4];
1720  joint_min[5] = limitMin[5];
1721 
1722  joint_max[0] = limitMax[0];
1723  joint_max[1] = limitMax[1];
1724  joint_max[2] = limitMax[2];
1725  joint_max[3] = limitMax[3];
1726  joint_max[4] = limitMax[4];
1727  joint_max[5] = limitMax[5];
1728 }
1729 
1736 {
1737  double q2 = q[1];
1738  double q3 = q[2];
1739  double q5 = q[4];
1740 
1741  double c2 = cos(q2);
1742  double c3 = cos(q3);
1743  double s3 = sin(q3);
1744  double c23 = cos(q2 + q3);
1745  double s23 = sin(q2 + q3);
1746  double s5 = sin(q5);
1747 
1748  bool cond1 = fabs(s5) < 1e-1;
1749  bool cond2 = fabs(a3 * s3 + c3 * d4) < 1e-1;
1750  bool cond3 = fabs(a2 * c2 - a3 * c23 + s23 * d4 + a1) < 1e-1;
1751 
1752  if (cond1) {
1753  J[3][0] = 0;
1754  J[5][0] = 0;
1755  J[3][1] = 0;
1756  J[5][1] = 0;
1757  J[3][2] = 0;
1758  J[5][2] = 0;
1759  J[3][3] = 0;
1760  J[5][3] = 0;
1761  J[3][4] = 0;
1762  J[5][4] = 0;
1763  J[3][5] = 0;
1764  J[5][5] = 0;
1765  return true;
1766  }
1767 
1768  if (cond2) {
1769  J[1][0] = 0;
1770  J[2][0] = 0;
1771  J[3][0] = 0;
1772  J[4][0] = 0;
1773  J[5][0] = 0;
1774  J[1][1] = 0;
1775  J[2][1] = 0;
1776  J[3][1] = 0;
1777  J[4][1] = 0;
1778  J[5][1] = 0;
1779  J[1][2] = 0;
1780  J[2][2] = 0;
1781  J[3][2] = 0;
1782  J[4][2] = 0;
1783  J[5][2] = 0;
1784  return true;
1785  }
1786 
1787  if (cond3) {
1788  J[0][0] = 0;
1789  J[3][0] = 0;
1790  J[4][0] = 0;
1791  J[5][0] = 0;
1792  J[0][1] = 0;
1793  J[3][1] = 0;
1794  J[4][1] = 0;
1795  J[5][1] = 0;
1796  }
1797 
1798  return false;
1799 }
1800 
1805 {
1806  int artNumb = 0;
1807  double diff = 0;
1808  double difft = 0;
1809 
1810  vpColVector articularCoordinates = get_artCoord();
1811 
1812  for (unsigned int i = 0; i < 6; i++) {
1813  if (articularCoordinates[i] <= joint_min[i]) {
1814  difft = joint_min[i] - articularCoordinates[i];
1815  if (difft > diff) {
1816  diff = difft;
1817  artNumb = -(int)i - 1;
1818  }
1819  }
1820  }
1821 
1822  for (unsigned int i = 0; i < 6; i++) {
1823  if (articularCoordinates[i] >= joint_max[i]) {
1824  difft = articularCoordinates[i] - joint_max[i];
1825  if (difft > diff) {
1826  diff = difft;
1827  artNumb = (int)(i + 1);
1828  }
1829  }
1830  }
1831 
1832  if (artNumb != 0)
1833  std::cout << "\nWarning: Velocity control stopped: axis " << fabs((float)artNumb) << " on joint limit!"
1834  << std::endl;
1835 
1836  return artNumb;
1837 }
1838 
1857 {
1858  displacement.resize(6);
1859  displacement = 0;
1860  vpColVector q_cur(6);
1861 
1862  q_cur = get_artCoord();
1863 
1864  if (!first_time_getdis) {
1865  switch (frame) {
1866  case vpRobot::CAMERA_FRAME: {
1867  std::cout << "getDisplacement() CAMERA_FRAME not implemented\n";
1868  return;
1869  }
1870 
1871  case vpRobot::ARTICULAR_FRAME: {
1872  displacement = q_cur - q_prev_getdis;
1873  break;
1874  }
1875 
1876  case vpRobot::REFERENCE_FRAME: {
1877  std::cout << "getDisplacement() REFERENCE_FRAME not implemented\n";
1878  return;
1879  }
1880 
1881  case vpRobot::MIXT_FRAME: {
1882  std::cout << "getDisplacement() MIXT_FRAME not implemented\n";
1883  return;
1884  }
1885  }
1886  } else {
1887  first_time_getdis = false;
1888  }
1889 
1890  // Memorize the joint position for the next call
1891  q_prev_getdis = q_cur;
1892 }
1893 
1956 bool vpSimulatorViper850::readPosFile(const std::string &filename, vpColVector &q)
1957 {
1958  std::ifstream fd(filename.c_str(), std::ios::in);
1959 
1960  if (!fd.is_open()) {
1961  return false;
1962  }
1963 
1964  std::string line;
1965  std::string key("R:");
1966  std::string id("#Viper850 - Position");
1967  bool pos_found = false;
1968  int lineNum = 0;
1969 
1970  q.resize(njoint);
1971 
1972  while (std::getline(fd, line)) {
1973  lineNum++;
1974  if (lineNum == 1) {
1975  if (!(line.compare(0, id.size(), id) == 0)) { // check if Viper850 position file
1976  std::cout << "Error: this position file " << filename << " is not for Viper850 robot" << std::endl;
1977  return false;
1978  }
1979  }
1980  if ((line.compare(0, 1, "#") == 0)) { // skip comment
1981  continue;
1982  }
1983  if ((line.compare(0, key.size(), key) == 0)) { // decode position
1984  // check if there are at least njoint values in the line
1985  std::vector<std::string> chain = vpIoTools::splitChain(line, std::string(" "));
1986  if (chain.size() < njoint + 1) // try to split with tab separator
1987  chain = vpIoTools::splitChain(line, std::string("\t"));
1988  if (chain.size() < njoint + 1)
1989  continue;
1990 
1991  std::istringstream ss(line);
1992  std::string key_;
1993  ss >> key_;
1994  for (unsigned int i = 0; i < njoint; i++)
1995  ss >> q[i];
1996  pos_found = true;
1997  break;
1998  }
1999  }
2000 
2001  // converts rotations from degrees into radians
2002  q.deg2rad();
2003 
2004  fd.close();
2005 
2006  if (!pos_found) {
2007  std::cout << "Error: unable to find a position for Viper850 robot in " << filename << std::endl;
2008  return false;
2009  }
2010 
2011  return true;
2012 }
2013 
2035 bool vpSimulatorViper850::savePosFile(const std::string &filename, const vpColVector &q)
2036 {
2037 
2038  FILE *fd;
2039  fd = fopen(filename.c_str(), "w");
2040  if (fd == NULL)
2041  return false;
2042 
2043  fprintf(fd, "\
2044 #Viper - Position - Version 1.0\n\
2045 #\n\
2046 # R: A B C D E F\n\
2047 # Joint position in degrees\n\
2048 #\n\
2049 #\n\n");
2050 
2051  // Save positions in mm and deg
2052  fprintf(fd, "R: %lf %lf %lf %lf %lf %lf\n", vpMath::deg(q[0]), vpMath::deg(q[1]), vpMath::deg(q[2]),
2053  vpMath::deg(q[3]), vpMath::deg(q[4]), vpMath::deg(q[5]));
2054 
2055  fclose(fd);
2056  return (true);
2057 }
2058 
2066 void vpSimulatorViper850::move(const char *filename)
2067 {
2068  vpColVector q;
2069 
2070  try {
2071  this->readPosFile(filename, q);
2074  } catch (...) {
2075  throw;
2076  }
2077 }
2078 
2089 
2098 {
2099  vpHomogeneousMatrix cMe;
2100  vpViper850::get_cMe(cMe);
2101 
2102  cVe.buildFrom(cMe);
2103 }
2104 
2115 {
2116  try {
2118  } catch (...) {
2119  vpERROR_TRACE("catch exception ");
2120  throw;
2121  }
2122 }
2123 
2135 {
2136  try {
2137  vpColVector articularCoordinates = get_artCoord();
2138  vpViper850::get_fJe(articularCoordinates, fJe_);
2139  } catch (...) {
2140  vpERROR_TRACE("Error caught");
2141  throw;
2142  }
2143 }
2144 
2149 {
2151  return;
2152 
2153  vpColVector stop(6);
2154  stop = 0;
2155  set_artVel(stop);
2156  set_velocity(stop);
2158 }
2159 
2160 /**********************************************************************************/
2161 /**********************************************************************************/
2162 /**********************************************************************************/
2163 /**********************************************************************************/
2164 
2174 {
2175  // set scene_dir from #define VISP_SCENE_DIR if it exists
2176  // VISP_SCENES_DIR may contain multiple locations separated by ";"
2177  std::string scene_dir_;
2178  std::vector<std::string> scene_dirs = vpIoTools::splitChain(std::string(VISP_SCENES_DIR), std::string(";"));
2179  bool sceneDirExists = false;
2180  for (size_t i = 0; i < scene_dirs.size(); i++)
2181  if (vpIoTools::checkDirectory(scene_dirs[i]) == true) { // directory exists
2182  scene_dir_ = scene_dirs[i];
2183  sceneDirExists = true;
2184  break;
2185  }
2186  if (!sceneDirExists) {
2187  try {
2188  scene_dir_ = vpIoTools::getenv("VISP_SCENES_DIR");
2189  std::cout << "The simulator uses data from VISP_SCENES_DIR=" << scene_dir_ << std::endl;
2190  } catch (...) {
2191  std::cout << "Cannot get VISP_SCENES_DIR environment variable" << std::endl;
2192  }
2193  }
2194 
2195  unsigned int name_length = 30; // the size of this kind of string "/viper850_arm2.bnd"
2196  if (scene_dir_.size() > FILENAME_MAX)
2197  throw vpException(vpException::dimensionError, "Cannot initialize Viper850 simulator");
2198 
2199  unsigned int full_length = (unsigned int)scene_dir_.size() + name_length;
2200  if (full_length > FILENAME_MAX)
2201  throw vpException(vpException::dimensionError, "Cannot initialize Viper850 simulator");
2202 
2203  char *name_cam = new char[full_length];
2204 
2205  strcpy(name_cam, scene_dir_.c_str());
2206  strcat(name_cam, "/camera.bnd");
2207  set_scene(name_cam, &camera, cameraFactor);
2208 
2209  if (arm_dir.size() > FILENAME_MAX)
2210  throw vpException(vpException::dimensionError, "Cannot initialize Viper850 simulator");
2211  full_length = (unsigned int)arm_dir.size() + name_length;
2212  if (full_length > FILENAME_MAX)
2213  throw vpException(vpException::dimensionError, "Cannot initialize Viper850 simulator");
2214 
2215  char *name_arm = new char[full_length];
2216  strcpy(name_arm, arm_dir.c_str());
2217  strcat(name_arm, "/viper850_arm1.bnd");
2218  set_scene(name_arm, robotArms, 1.0);
2219  strcpy(name_arm, arm_dir.c_str());
2220  strcat(name_arm, "/viper850_arm2.bnd");
2221  set_scene(name_arm, robotArms + 1, 1.0);
2222  strcpy(name_arm, arm_dir.c_str());
2223  strcat(name_arm, "/viper850_arm3.bnd");
2224  set_scene(name_arm, robotArms + 2, 1.0);
2225  strcpy(name_arm, arm_dir.c_str());
2226  strcat(name_arm, "/viper850_arm4.bnd");
2227  set_scene(name_arm, robotArms + 3, 1.0);
2228  strcpy(name_arm, arm_dir.c_str());
2229  strcat(name_arm, "/viper850_arm5.bnd");
2230  set_scene(name_arm, robotArms + 4, 1.0);
2231  strcpy(name_arm, arm_dir.c_str());
2232  strcat(name_arm, "/viper850_arm6.bnd");
2233  set_scene(name_arm, robotArms + 5, 1.0);
2234 
2235  // set_scene("./arm2.bnd", robotArms+1, 1.0);
2236  // set_scene("./arm3.bnd", robotArms+2, 1.0);
2237  // set_scene("./arm4.bnd", robotArms+3, 1.0);
2238  // set_scene("./arm5.bnd", robotArms+4, 1.0);
2239  // set_scene("./arm6.bnd", robotArms+5, 1.0);
2240 
2241  add_rfstack(IS_BACK);
2242 
2243  add_vwstack("start", "depth", 0.0, 100.0);
2244  add_vwstack("start", "window", -0.1, 0.1, -0.1, 0.1);
2245  add_vwstack("start", "type", PERSPECTIVE);
2246  //
2247  // sceneInitialized = true;
2248  // displayObject = true;
2249  displayCamera = true;
2250 
2251  delete[] name_cam;
2252  delete[] name_arm;
2253 }
2254 
2256 {
2257  bool changed = false;
2258  vpHomogeneousMatrix displacement = navigation(I_, changed);
2259 
2260  // if (displacement[2][3] != 0)
2261  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon())
2262  camMf2 = camMf2 * displacement;
2263 
2264  f2Mf = camMf2.inverse() * camMf;
2265 
2266  camMf = camMf2 * displacement * f2Mf;
2267 
2268  double u;
2269  double v;
2270  // if(px_ext != 1 && py_ext != 1)
2271  // we assume px_ext and py_ext > 0
2272  if ((std::fabs(px_ext - 1.) > vpMath::maximum(px_ext, 1.) * std::numeric_limits<double>::epsilon()) &&
2273  (std::fabs(py_ext - 1) > vpMath::maximum(py_ext, 1.) * std::numeric_limits<double>::epsilon())) {
2274  u = (double)I_.getWidth() / (2 * px_ext);
2275  v = (double)I_.getHeight() / (2 * py_ext);
2276  } else {
2277  u = (double)I_.getWidth() / (vpMath::minimum(I_.getWidth(), I_.getHeight()));
2278  v = (double)I_.getHeight() / (vpMath::minimum(I_.getWidth(), I_.getHeight()));
2279  }
2280 
2281  float w44o[4][4], w44cext[4][4], x, y, z;
2282 
2283  vp2jlc_matrix(camMf.inverse(), w44cext);
2284 
2285  add_vwstack("start", "cop", w44cext[3][0], w44cext[3][1], w44cext[3][2]);
2286  x = w44cext[2][0] + w44cext[3][0];
2287  y = w44cext[2][1] + w44cext[3][1];
2288  z = w44cext[2][2] + w44cext[3][2];
2289  add_vwstack("start", "vrp", x, y, z);
2290  add_vwstack("start", "vpn", w44cext[2][0], w44cext[2][1], w44cext[2][2]);
2291  add_vwstack("start", "vup", w44cext[1][0], w44cext[1][1], w44cext[1][2]);
2292  add_vwstack("start", "window", -u, u, -v, v);
2293 
2294  vpHomogeneousMatrix fMit[8];
2295  get_fMi(fMit);
2296 
2297  vp2jlc_matrix(vpHomogeneousMatrix(0, 0, 0, 0, 0, 0), w44o);
2298  display_scene(w44o, robotArms[0], I_, curColor);
2299 
2300  vp2jlc_matrix(fMit[0], w44o);
2301  display_scene(w44o, robotArms[1], I_, curColor);
2302 
2303  vp2jlc_matrix(fMit[1], w44o);
2304  display_scene(w44o, robotArms[2], I_, curColor);
2305 
2306  vp2jlc_matrix(fMit[2], w44o);
2307  display_scene(w44o, robotArms[3], I_, curColor);
2308 
2309  vp2jlc_matrix(fMit[3], w44o);
2310  display_scene(w44o, robotArms[4], I_, curColor);
2311 
2312  vp2jlc_matrix(fMit[6], w44o);
2313  display_scene(w44o, robotArms[5], I_, curColor);
2314 
2315  if (displayCamera) {
2316  vpHomogeneousMatrix cMe;
2317  get_cMe(cMe);
2318  cMe = cMe.inverse();
2319  cMe = fMit[6] * cMe;
2320  vp2jlc_matrix(cMe, w44o);
2321  display_scene(w44o, camera, I_, camColor);
2322  }
2323 
2324  if (displayObject) {
2325  vp2jlc_matrix(fMo, w44o);
2326  display_scene(w44o, scene, I_, curColor);
2327  }
2328 }
2329 
2347 {
2348  vpColVector stop(6);
2349  bool status = true;
2350  stop = 0;
2351  set_artVel(stop);
2352  set_velocity(stop);
2353  vpHomogeneousMatrix fMc_;
2354  fMc_ = fMo * cMo_.inverse();
2355 
2356  vpColVector articularCoordinates = get_artCoord();
2357  unsigned int nbSol = getInverseKinematics(fMc_, articularCoordinates, verbose_);
2358 
2359  if (nbSol == 0) {
2360  status = false;
2361  vpERROR_TRACE("Positionning error. Position unreachable");
2362  }
2363 
2364  if (verbose_)
2365  std::cout << "Used joint coordinates (rad): " << articularCoordinates.t() << std::endl;
2366 
2367  set_artCoord(articularCoordinates);
2368 
2369  compute_fMi();
2370 
2371  return status;
2372 }
2373 
2388 {
2389  vpColVector stop(6);
2390  stop = 0;
2391  set_artVel(stop);
2392  set_velocity(stop);
2393  vpHomogeneousMatrix fMit[8];
2394  get_fMi(fMit);
2395  fMo = fMit[7] * cMo_;
2396 }
2397 
2398 #elif !defined(VISP_BUILD_SHARED_LIBS)
2399 // Work arround to avoid warning: libvisp_robot.a(vpSimulatorViper850.cpp.o)
2400 // has no symbols
2401 void dummy_vpSimulatorViper850(){};
2402 #endif
static void displayCamera(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &q)
vpMatrix pseudoInverse(double svThreshold=1e-6) const
Definition: vpMatrix.cpp:1931
void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &q)
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
double a3
for joint 3
Definition: vpViper.h:165
vpHomogeneousMatrix eMc
End effector to camera transformation.
Definition: vpViper.h:157
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 get_fMi(vpHomogeneousMatrix *fMit)
void get_fJe(const vpColVector &q, vpMatrix &fJe) const
Definition: vpViper.cpp:1159
static const vpToolType defaultTool
Default tool attached to the robot end effector.
Definition: vpViper850.h:137
void get_cMe(vpHomogeneousMatrix &cMe)
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void * launcher(void *arg)
bool singularityTest(const vpColVector &q, vpMatrix &J)
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
void getExternalImage(vpImage< vpRGBa > &I)
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
unsigned int getRows() const
Definition: vpArray2D.h:156
void track(const vpHomogeneousMatrix &cMo)
vpHomogeneousMatrix inverse() const
vpRowVector t() const
void get_fJe(vpMatrix &fJe)
vpControlFrameType
Definition: vpRobot.h:75
vpRxyzVector erc
Definition: vpViper.h:160
static const char *const CONST_MARLIN_F033C_CAMERA_NAME
Definition: vpViper850.h:122
double getMaxRotationVelocity(void) const
Definition: vpRobot.cpp:273
void extract(vpRotationMatrix &R) const
static const vpColor green
Definition: vpColor.h:183
vpHomogeneousMatrix fMo
double a2
for joint 2
Definition: vpViper.h:164
static void flush(const vpImage< unsigned char > &I)
static bool savePosFile(const std::string &filename, const vpColVector &q)
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 deg2rad()
Definition: vpColVector.h:134
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.
vpHomogeneousMatrix get_fMc(const vpColVector &q) const
Definition: vpViper.cpp:600
double d1
for joint 1
Definition: vpViper.h:163
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
double a1
Definition: vpViper.h:163
void move(const char *filename)
Initialize the velocity controller.
Definition: vpRobot.h:67
virtual vpRobotStateType getRobotState(void) const
Definition: vpRobot.h:139
vpRobotStateType
Definition: vpRobot.h:64
void setJointLimit(const vpColVector &limitMin, const vpColVector &limitMax)
void setToolType(vpViper850::vpToolType tool)
Set the current tool type.
Definition: vpViper850.h:170
vpTranslationVector etc
Definition: vpViper.h:159
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)
void initialiseObjectRelativeToCamera(const vpHomogeneousMatrix &cMo)
static void display(const vpImage< unsigned char > &I)
void setExternalCameraPosition(const vpHomogeneousMatrix &camMf_)
vpColVector joint_max
Definition: vpViper.h:172
vpToolType
List of possible tools that can be attached to the robot end-effector.
Definition: vpViper850.h:128
Generic class defining intrinsic camera parameters.
void getDisplacement(const vpRobot::vpControlFrameType frame, vpColVector &displacement)
Modelisation of the ADEPT Viper 850 robot.
Definition: vpViper850.h:103
unsigned int getInverseKinematics(const vpHomogeneousMatrix &fMc, vpColVector &q, const bool &verbose=false) const
Definition: vpViper.cpp:563
static std::vector< std::string > splitChain(const std::string &chain, const std::string &sep)
Definition: vpIoTools.cpp:1665
void get_cMe(vpHomogeneousMatrix &cMe) const
Definition: vpViper.cpp:922
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:145
static bool readPosFile(const std::string &filename, vpColVector &q)
double getSamplingTime() const
void set_velocity(const vpColVector &vel)
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double rad(double deg)
Definition: vpMath.h:102
void setExternalCameraParameters(const vpCameraParameters &cam)
vpToolType getToolType() const
Get the current tool type.
Definition: vpViper850.h:161
This class aims to be a basis used to create all the simulators of robots.
vpRobot::vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
void display_scene(Matrix mat, Bound_scene &sc, const vpImage< vpRGBa > &I, const vpColor &color)
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 setCameraParameters(const vpCameraParameters &cam)
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:92
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
vpControlFrameType setRobotFrame(vpRobot::vpControlFrameType newFrame)
Definition: vpRobot.cpp:207
vpControlFrameType getRobotFrame(void) const
Definition: vpRobot.h:167
double d4
for joint 4
Definition: vpViper.h:166
void findHighestPositioningSpeed(vpColVector &q)
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)
void get_cVe(vpVelocityTwistMatrix &cVe)
vpCameraParameters::vpCameraParametersProjType projModel
Definition: vpViper850.h:177
VISP_EXPORT double getMinTimeForUsleepCall()
Definition: vpTime.cpp:80
void set_artVel(const vpColVector &vel)
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height)
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &q)
unsigned int getWidth() const
Definition: vpImage.h:229
static const double defaultPositioningVelocity
Class that consider the case of a translation vector.
Implementation of a rotation vector as axis-angle minimal representation.
double getPositioningVelocity(void)
static const unsigned int njoint
Number of joint.
Definition: vpViper.h:154
vpHomogeneousMatrix camMf2
void get_eJe(const vpColVector &q, vpMatrix &eJe) const
Definition: vpViper.cpp:970
bool initialiseCameraRelativeToObject(const vpHomogeneousMatrix &cMo)
static const char *const CONST_PTGREY_FLEA2_CAMERA_NAME
Definition: vpViper850.h:123
void get_eJe(vpMatrix &eJe)
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:241
double d6
for joint 6
Definition: vpViper.h:167
vpColVector joint_min
Definition: vpViper.h:173