Visual Servoing Platform  version 3.1.0
vpRobotAfma6.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  * Interface for the Irisa's Afma6 robot.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpConfig.h>
40 
41 #ifdef VISP_HAVE_AFMA6
42 
43 #include <signal.h>
44 #include <stdlib.h>
45 #include <sys/types.h>
46 #include <unistd.h>
47 
48 #include <visp3/core/vpDebug.h>
49 #include <visp3/core/vpExponentialMap.h>
50 #include <visp3/core/vpIoTools.h>
51 #include <visp3/core/vpRotationMatrix.h>
52 #include <visp3/core/vpThetaUVector.h>
53 #include <visp3/core/vpVelocityTwistMatrix.h>
54 #include <visp3/robot/vpRobotAfma6.h>
55 #include <visp3/robot/vpRobotException.h>
56 
57 /* ---------------------------------------------------------------------- */
58 /* --- STATIC ----------------------------------------------------------- */
59 /* ---------------------------------------------------------------------- */
60 
61 bool vpRobotAfma6::robotAlreadyCreated = false;
62 
71 
72 /* ---------------------------------------------------------------------- */
73 /* --- EMERGENCY STOP --------------------------------------------------- */
74 /* ---------------------------------------------------------------------- */
75 
83 void emergencyStopAfma6(int signo)
84 {
85  std::cout << "Stop the Afma6 application by signal (" << signo << "): " << (char)7;
86  switch (signo) {
87  case SIGINT:
88  std::cout << "SIGINT (stop by ^C) " << std::endl;
89  break;
90  case SIGBUS:
91  std::cout << "SIGBUS (stop due to a bus error) " << std::endl;
92  break;
93  case SIGSEGV:
94  std::cout << "SIGSEGV (stop due to a segmentation fault) " << std::endl;
95  break;
96  case SIGKILL:
97  std::cout << "SIGKILL (stop by CTRL \\) " << std::endl;
98  break;
99  case SIGQUIT:
100  std::cout << "SIGQUIT " << std::endl;
101  break;
102  default:
103  std::cout << signo << std::endl;
104  }
105  // std::cout << "Emergency stop called\n";
106  // PrimitiveESTOP_Afma6();
107  PrimitiveSTOP_Afma6();
108  std::cout << "Robot was stopped\n";
109 
110  // Free allocated resources
111  // ShutDownConnection(); // Some times cannot exit here when Ctrl-C
112 
113  fprintf(stdout, "Application ");
114  fflush(stdout);
115  kill(getpid(), SIGKILL);
116  exit(1);
117 }
118 
119 /* ---------------------------------------------------------------------- */
120 /* --- CONSTRUCTOR ------------------------------------------------------ */
121 /* ---------------------------------------------------------------------- */
122 
159 vpRobotAfma6::vpRobotAfma6(bool verbose) : vpAfma6(), vpRobot()
160 {
161 
162  /*
163  #define SIGHUP 1 // hangup
164  #define SIGINT 2 // interrupt (rubout)
165  #define SIGQUIT 3 // quit (ASCII FS)
166  #define SIGILL 4 // illegal instruction (not reset when caught)
167  #define SIGTRAP 5 // trace trap (not reset when caught)
168  #define SIGIOT 6 // IOT instruction
169  #define SIGABRT 6 // used by abort, replace SIGIOT in the future
170  #define SIGEMT 7 // EMT instruction
171  #define SIGFPE 8 // floating point exception
172  #define SIGKILL 9 // kill (cannot be caught or ignored)
173  #define SIGBUS 10 // bus error
174  #define SIGSEGV 11 // segmentation violation
175  #define SIGSYS 12 // bad argument to system call
176  #define SIGPIPE 13 // write on a pipe with no one to read it
177  #define SIGALRM 14 // alarm clock
178  #define SIGTERM 15 // software termination signal from kill
179  */
180 
181  signal(SIGINT, emergencyStopAfma6);
182  signal(SIGBUS, emergencyStopAfma6);
183  signal(SIGSEGV, emergencyStopAfma6);
184  signal(SIGKILL, emergencyStopAfma6);
185  signal(SIGQUIT, emergencyStopAfma6);
186 
187  setVerbose(verbose);
188  if (verbose_)
189  std::cout << "Open communication with MotionBlox.\n";
190  try {
191  this->init();
193  } catch (...) {
194  // vpERROR_TRACE("Error caught") ;
195  throw;
196  }
197  positioningVelocity = defaultPositioningVelocity;
198 
199  vpRobotAfma6::robotAlreadyCreated = true;
200 
201  return;
202 }
203 
204 /* ------------------------------------------------------------------------ */
205 /* --- INITIALISATION ----------------------------------------------------- */
206 /* ------------------------------------------------------------------------ */
207 
228 {
229  InitTry;
230 
231  // Initialise private variables used to compute the measured velocities
232  q_prev_getvel.resize(6);
233  q_prev_getvel = 0;
234  time_prev_getvel = 0;
235  first_time_getvel = true;
236 
237  // Initialise private variables used to compute the measured displacement
238  q_prev_getdis.resize(6);
239  q_prev_getdis = 0;
240  first_time_getdis = true;
241 
242  // Initialize the firewire connection
243  Try(InitializeConnection(verbose_));
244 
245  // Connect to the servoboard using the servo board GUID
246  Try(InitializeNode_Afma6());
247 
248  Try(PrimitiveRESET_Afma6());
249 
250  // Update the eMc matrix in the low level controller
252 
253  // Look if the power is on or off
254  UInt32 HIPowerStatus;
255  UInt32 EStopStatus;
256  Try(PrimitiveSTATUS_Afma6(NULL, NULL, &EStopStatus, NULL, NULL, NULL, &HIPowerStatus));
257  CAL_Wait(0.1);
258 
259  // Print the robot status
260  if (verbose_) {
261  std::cout << "Robot status: ";
262  switch (EStopStatus) {
263  case ESTOP_AUTO:
264  case ESTOP_MANUAL:
265  if (HIPowerStatus == 0)
266  std::cout << "Power is OFF" << std::endl;
267  else
268  std::cout << "Power is ON" << std::endl;
269  break;
270  case ESTOP_ACTIVATED:
271  std::cout << "Emergency stop is activated" << std::endl;
272  break;
273  default:
274  std::cout << "Sorry there is an error on the emergency chain." << std::endl;
275  std::cout << "You have to call Adept for maintenance..." << std::endl;
276  // Free allocated resources
277  }
278  std::cout << std::endl;
279  }
280  // get real joint min/max from the MotionBlox
281  Try(PrimitiveJOINT_MINMAX_Afma6(_joint_min, _joint_max));
282  // for (unsigned int i=0; i < njoint; i++) {
283  // printf("axis %d: joint min %lf, max %lf\n", i, _joint_min[i],
284  // _joint_max[i]);
285  // }
286 
287  // If an error occur in the low level controller, goto here
288  // CatchPrint();
289  Catch();
290 
291  // Test if an error occurs
292  if (TryStt == -20001)
293  printf("No connection detected. Check if the robot is powered on \n"
294  "and if the firewire link exist between the MotionBlox and this "
295  "computer.\n");
296  else if (TryStt == -675)
297  printf(" Timeout enabling power...\n");
298 
299  if (TryStt < 0) {
300  // Power off the robot
301  PrimitivePOWEROFF_Afma6();
302  // Free allocated resources
303  ShutDownConnection();
304 
305  std::cout << "Cannot open connection with the motionblox..." << std::endl;
306  throw vpRobotException(vpRobotException::constructionError, "Cannot open connection with the motionblox");
307  }
308  return;
309 }
310 
348 {
349  InitTry;
350  // Read the robot constants from files
351  // - joint [min,max], coupl_56, long_56
352  // - camera extrinsic parameters relative to eMc
353  vpAfma6::init(tool, projModel);
354 
355  // Set the robot constant (coupl_56, long_56) in the MotionBlox
356  Try(PrimitiveROBOT_CONST_Afma6(_coupl_56, _long_56));
357 
358  // Set the camera constant (eMc pose) in the MotionBlox
359  double eMc_pose[6];
360  for (unsigned int i = 0; i < 3; i++) {
361  eMc_pose[i] = _etc[i]; // translation in meters
362  eMc_pose[i + 3] = _erc[i]; // rotation in rad
363  }
364  // Update the eMc pose in the low level controller
365  Try(PrimitiveCAMERA_CONST_Afma6(eMc_pose));
366 
367  // get real joint min/max from the MotionBlox
368  Try(PrimitiveJOINT_MINMAX_Afma6(_joint_min, _joint_max));
369  // for (unsigned int i=0; i < njoint; i++) {
370  // printf("axis %d: joint min %lf, max %lf\n", i, _joint_min[i],
371  // _joint_max[i]);
372  // }
373 
374  setToolType(tool);
375 
376  CatchPrint();
377  return;
378 }
379 
392 {
393  InitTry;
394  // Set camera extrinsic parameters equal to eMc
395  this->vpAfma6::set_eMc(eMc);
396 
397  // Set the camera constant (eMc pose) in the MotionBlox
398  double eMc_pose[6];
399  for (unsigned int i = 0; i < 3; i++) {
400  eMc_pose[i] = _etc[i]; // translation in meters
401  eMc_pose[i + 3] = _erc[i]; // rotation in rad
402  }
403  // Update the eMc pose in the low level controller
404  Try(PrimitiveCAMERA_CONST_Afma6(eMc_pose));
405 
406  CatchPrint();
407 }
408 
445 {
446  InitTry;
447  // Read the robot constants from files
448  // - joint [min,max], coupl_56, long_56
449  // ans set camera extrinsic parameters equal to eMc
450  vpAfma6::init(tool, eMc);
451 
452  // Set the robot constant (coupl_56, long_56) in the MotionBlox
453  Try(PrimitiveROBOT_CONST_Afma6(_coupl_56, _long_56));
454 
455  // Set the camera constant (eMc pose) in the MotionBlox
456  double eMc_pose[6];
457  for (unsigned int i = 0; i < 3; i++) {
458  eMc_pose[i] = _etc[i]; // translation in meters
459  eMc_pose[i + 3] = _erc[i]; // rotation in rad
460  }
461  // Update the eMc pose in the low level controller
462  Try(PrimitiveCAMERA_CONST_Afma6(eMc_pose));
463 
464  // get real joint min/max from the MotionBlox
465  Try(PrimitiveJOINT_MINMAX_Afma6(_joint_min, _joint_max));
466 
467  setToolType(tool);
468 
469  CatchPrint();
470 }
471 
522 void vpRobotAfma6::init(vpAfma6::vpAfma6ToolType tool, const std::string &filename)
523 {
524  InitTry;
525  // Read the robot constants from files
526  // - joint [min,max], coupl_56, long_56
527  // ans set camera extrinsic parameters from file name
528  vpAfma6::init(tool, filename);
529 
530  // Set the robot constant (coupl_56, long_56) in the MotionBlox
531  Try(PrimitiveROBOT_CONST_Afma6(_coupl_56, _long_56));
532 
533  // Set the camera constant (eMc pose) in the MotionBlox
534  double eMc_pose[6];
535  for (unsigned int i = 0; i < 3; i++) {
536  eMc_pose[i] = _etc[i]; // translation in meters
537  eMc_pose[i + 3] = _erc[i]; // rotation in rad
538  }
539  // Update the eMc pose in the low level controller
540  Try(PrimitiveCAMERA_CONST_Afma6(eMc_pose));
541 
542  // get real joint min/max from the MotionBlox
543  Try(PrimitiveJOINT_MINMAX_Afma6(_joint_min, _joint_max));
544 
545  setToolType(tool);
546 
547  CatchPrint();
548 }
549 
550 /* ------------------------------------------------------------------------ */
551 /* --- DESTRUCTOR --------------------------------------------------------- */
552 /* ------------------------------------------------------------------------ */
553 
561 {
562  InitTry;
563 
565 
566  // Look if the power is on or off
567  UInt32 HIPowerStatus;
568  Try(PrimitiveSTATUS_Afma6(NULL, NULL, NULL, NULL, NULL, NULL, &HIPowerStatus));
569  CAL_Wait(0.1);
570 
571  // if (HIPowerStatus == 1) {
572  // fprintf(stdout, "Power OFF the robot\n");
573  // fflush(stdout);
574 
575  // Try( PrimitivePOWEROFF_Afma6() );
576  // }
577 
578  // Free allocated resources
579  ShutDownConnection();
580 
581  vpRobotAfma6::robotAlreadyCreated = false;
582 
583  CatchPrint();
584  return;
585 }
586 
594 {
595  InitTry;
596 
597  switch (newState) {
598  case vpRobot::STATE_STOP: {
600  Try(PrimitiveSTOP_Afma6());
601  }
602  break;
603  }
606  std::cout << "Change the control mode from velocity to position control.\n";
607  Try(PrimitiveSTOP_Afma6());
608  } else {
609  // std::cout << "Change the control mode from stop to position
610  // control.\n";
611  }
612  this->powerOn();
613  break;
614  }
617  std::cout << "Change the control mode from stop to velocity control.\n";
618  }
619  this->powerOn();
620  break;
621  }
622  default:
623  break;
624  }
625 
626  CatchPrint();
627 
628  return vpRobot::setRobotState(newState);
629 }
630 
631 /* ------------------------------------------------------------------------ */
632 /* --- STOP --------------------------------------------------------------- */
633 /* ------------------------------------------------------------------------ */
634 
643 {
644  InitTry;
645  Try(PrimitiveSTOP_Afma6());
647 
648  CatchPrint();
649  if (TryStt < 0) {
650  vpERROR_TRACE("Cannot stop robot motion");
651  throw vpRobotException(vpRobotException::lowLevelError, "Cannot stop robot motion.");
652  }
653 }
654 
665 {
666  InitTry;
667 
668  // Look if the power is on or off
669  UInt32 HIPowerStatus;
670  UInt32 EStopStatus;
671  bool firsttime = true;
672  unsigned int nitermax = 10;
673 
674  for (unsigned int i = 0; i < nitermax; i++) {
675  Try(PrimitiveSTATUS_Afma6(NULL, NULL, &EStopStatus, NULL, NULL, NULL, &HIPowerStatus));
676  if (EStopStatus == ESTOP_AUTO) {
677  break; // exit for loop
678  } else if (EStopStatus == ESTOP_MANUAL) {
679  break; // exit for loop
680  } else if (EStopStatus == ESTOP_ACTIVATED) {
681  if (firsttime) {
682  std::cout << "Emergency stop is activated! \n"
683  << "Check the emergency stop button and push the yellow "
684  "button before continuing."
685  << std::endl;
686  firsttime = false;
687  }
688  fprintf(stdout, "Remaining time %us \r", nitermax - i);
689  fflush(stdout);
690  CAL_Wait(1);
691  } else {
692  std::cout << "Sorry there is an error on the emergency chain." << std::endl;
693  std::cout << "You have to call Adept for maintenance..." << std::endl;
694  // Free allocated resources
695  ShutDownConnection();
696  exit(0);
697  }
698  }
699 
700  if (EStopStatus == ESTOP_ACTIVATED)
701  std::cout << std::endl;
702 
703  if (EStopStatus == ESTOP_ACTIVATED) {
704  std::cout << "Sorry, cannot power on the robot." << std::endl;
705  throw vpRobotException(vpRobotException::lowLevelError, "Cannot power on the robot.");
706  }
707 
708  if (HIPowerStatus == 0) {
709  fprintf(stdout, "Power ON the Afma6 robot\n");
710  fflush(stdout);
711 
712  Try(PrimitivePOWERON_Afma6());
713  }
714 
715  CatchPrint();
716  if (TryStt < 0) {
717  vpERROR_TRACE("Cannot power on the robot");
718  throw vpRobotException(vpRobotException::lowLevelError, "Cannot power off the robot.");
719  }
720 }
721 
732 {
733  InitTry;
734 
735  // Look if the power is on or off
736  UInt32 HIPowerStatus;
737  Try(PrimitiveSTATUS_Afma6(NULL, NULL, NULL, NULL, NULL, NULL, &HIPowerStatus));
738  CAL_Wait(0.1);
739 
740  if (HIPowerStatus == 1) {
741  fprintf(stdout, "Power OFF the Afma6 robot\n");
742  fflush(stdout);
743 
744  Try(PrimitivePOWEROFF_Afma6());
745  }
746 
747  CatchPrint();
748  if (TryStt < 0) {
749  vpERROR_TRACE("Cannot power off the robot");
750  throw vpRobotException(vpRobotException::lowLevelError, "Cannot power off the robot.");
751  }
752 }
753 
766 {
767  InitTry;
768  bool status = false;
769  // Look if the power is on or off
770  UInt32 HIPowerStatus;
771  Try(PrimitiveSTATUS_Afma6(NULL, NULL, NULL, NULL, NULL, NULL, &HIPowerStatus));
772  CAL_Wait(0.1);
773 
774  if (HIPowerStatus == 1) {
775  status = true;
776  }
777 
778  CatchPrint();
779  if (TryStt < 0) {
780  vpERROR_TRACE("Cannot get the power status");
781  throw vpRobotException(vpRobotException::lowLevelError, "Cannot get the power status.");
782  }
783  return status;
784 }
785 
796 {
798  vpAfma6::get_cMe(cMe);
799 
800  cVe.buildFrom(cMe);
801 }
802 
814 
826 {
827 
828  double position[6];
829  double timestamp;
830 
831  InitTry;
832  Try(PrimitiveACQ_POS_Afma6(position, &timestamp));
833  CatchPrint();
834 
835  vpColVector q(6);
836  for (unsigned int i = 0; i < njoint; i++)
837  q[i] = position[i];
838 
839  try {
840  vpAfma6::get_eJe(q, eJe);
841  } catch (...) {
842  vpERROR_TRACE("catch exception ");
843  throw;
844  }
845 }
870 {
871 
872  double position[6];
873  double timestamp;
874 
875  InitTry;
876  Try(PrimitiveACQ_POS_Afma6(position, &timestamp));
877  CatchPrint();
878 
879  vpColVector q(6);
880  for (unsigned int i = 0; i < njoint; i++)
881  q[i] = position[i];
882 
883  try {
884  vpAfma6::get_fJe(q, fJe);
885  } catch (...) {
886  vpERROR_TRACE("Error caught");
887  throw;
888  }
889 }
890 
919 void vpRobotAfma6::setPositioningVelocity(const double velocity) { positioningVelocity = velocity; }
920 
926 double vpRobotAfma6::getPositioningVelocity(void) { return positioningVelocity; }
927 
1004 
1005 {
1006  vpColVector position(6);
1007  vpRxyzVector rxyz;
1008  vpRotationMatrix R;
1009 
1010  R.buildFrom(pose[3], pose[4], pose[5]); // thetau
1011  rxyz.buildFrom(R);
1012 
1013  for (unsigned int i = 0; i < 3; i++) {
1014  position[i] = pose[i];
1015  position[i + 3] = rxyz[i];
1016  }
1017  if (frame == vpRobot::ARTICULAR_FRAME) {
1018  throw vpRobotException(vpRobotException::lowLevelError, "Positionning error: "
1019  "Joint frame not implemented for pose positionning.");
1020  }
1021  setPosition(frame, position);
1022 }
1107 {
1108 
1110  vpERROR_TRACE("Robot was not in position-based control\n"
1111  "Modification of the robot state");
1113  }
1114 
1115  double _destination[6];
1116  int error = 0;
1117  double timestamp;
1118 
1119  InitTry;
1120  switch (frame) {
1121  case vpRobot::CAMERA_FRAME: {
1122  double _q[njoint];
1123  Try(PrimitiveACQ_POS_Afma6(_q, &timestamp));
1124 
1125  vpColVector q(njoint);
1126  for (unsigned int i = 0; i < njoint; i++)
1127  q[i] = _q[i];
1128 
1129  // Get fMc from the inverse kinematics
1130  vpHomogeneousMatrix fMc;
1131  vpAfma6::get_fMc(q, fMc);
1132 
1133  // Set cMc from the input position
1134  vpTranslationVector txyz;
1135  vpRxyzVector rxyz;
1136  for (unsigned int i = 0; i < 3; i++) {
1137  txyz[i] = position[i];
1138  rxyz[i] = position[i + 3];
1139  }
1140 
1141  // Compute cMc2
1142  vpRotationMatrix cRc2(rxyz);
1143  vpHomogeneousMatrix cMc2(txyz, cRc2);
1144 
1145  // Compute the new position to reach: fMc*cMc2
1146  vpHomogeneousMatrix fMc2 = fMc * cMc2;
1147 
1148  // Compute the corresponding joint position from the inverse kinematics
1149  bool nearest = true;
1150  int solution = this->getInverseKinematics(fMc2, q, nearest);
1151  if (solution) { // Position is reachable
1152  for (unsigned int i = 0; i < njoint; i++) {
1153  _destination[i] = q[i];
1154  }
1155  Try(PrimitiveMOVE_Afma6(_destination, positioningVelocity));
1156  Try(WaitState_Afma6(ETAT_ATTENTE_AFMA6, 1000));
1157  } else {
1158  // Cartesian position is out of range
1159  error = -1;
1160  }
1161 
1162  break;
1163  }
1164  case vpRobot::ARTICULAR_FRAME: {
1165  for (unsigned int i = 0; i < njoint; i++) {
1166  _destination[i] = position[i];
1167  }
1168  Try(PrimitiveMOVE_Afma6(_destination, positioningVelocity));
1169  Try(WaitState_Afma6(ETAT_ATTENTE_AFMA6, 1000));
1170  break;
1171  }
1172  case vpRobot::REFERENCE_FRAME: {
1173  // Set fMc from the input position
1174  vpTranslationVector txyz;
1175  vpRxyzVector rxyz;
1176  for (unsigned int i = 0; i < 3; i++) {
1177  txyz[i] = position[i];
1178  rxyz[i] = position[i + 3];
1179  }
1180  // Compute fMc from the input position
1181  vpRotationMatrix fRc(rxyz);
1182  vpHomogeneousMatrix fMc(txyz, fRc);
1183 
1184  // Compute the corresponding joint position from the inverse kinematics
1185  vpColVector q(6);
1186  bool nearest = true;
1187  int solution = this->getInverseKinematics(fMc, q, nearest);
1188  if (solution) { // Position is reachable
1189  for (unsigned int i = 0; i < njoint; i++) {
1190  _destination[i] = q[i];
1191  }
1192  Try(PrimitiveMOVE_Afma6(_destination, positioningVelocity));
1193  Try(WaitState_Afma6(ETAT_ATTENTE_AFMA6, 1000));
1194  } else {
1195  // Cartesian position is out of range
1196  error = -1;
1197  }
1198 
1199  break;
1200  }
1201  case vpRobot::MIXT_FRAME: {
1202  vpERROR_TRACE("Positionning error. Mixt frame not implemented");
1203  throw vpRobotException(vpRobotException::lowLevelError, "Positionning error: "
1204  "Mixt frame not implemented.");
1205  }
1206  }
1207 
1208  CatchPrint();
1209  if (TryStt == InvalidPosition || TryStt == -1023)
1210  std::cout << " : Position out of range.\n";
1211  else if (TryStt < 0)
1212  std::cout << " : Unknown error (see Fabien).\n";
1213  else if (error == -1)
1214  std::cout << "Position out of range.\n";
1215 
1216  if (TryStt < 0 || error < 0) {
1217  vpERROR_TRACE("Positionning error.");
1218  throw vpRobotException(vpRobotException::positionOutOfRangeError, "Position out of range.");
1219  }
1220 
1221  return;
1222 }
1223 
1291 void vpRobotAfma6::setPosition(const vpRobot::vpControlFrameType frame, const double pos1, const double pos2,
1292  const double pos3, const double pos4, const double pos5, const double pos6)
1293 {
1294  try {
1295  vpColVector position(6);
1296  position[0] = pos1;
1297  position[1] = pos2;
1298  position[2] = pos3;
1299  position[3] = pos4;
1300  position[4] = pos5;
1301  position[5] = pos6;
1302 
1303  setPosition(frame, position);
1304  } catch (...) {
1305  vpERROR_TRACE("Error caught");
1306  throw;
1307  }
1308 }
1309 
1351 void vpRobotAfma6::setPosition(const std::string &filename)
1352 {
1353  vpColVector q;
1354  bool ret;
1355 
1356  ret = this->readPosFile(filename, q);
1357 
1358  if (ret == false) {
1359  vpERROR_TRACE("Bad position in \"%s\"", filename.c_str());
1360  throw vpRobotException(vpRobotException::lowLevelError, "Bad position in filename.");
1361  }
1364 }
1365 
1420 void vpRobotAfma6::getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position, double &timestamp)
1421 {
1422 
1423  InitTry;
1424 
1425  position.resize(6);
1426 
1427  switch (frame) {
1428  case vpRobot::CAMERA_FRAME: {
1429  position = 0;
1430  return;
1431  }
1432  case vpRobot::ARTICULAR_FRAME: {
1433  double _q[njoint];
1434  Try(PrimitiveACQ_POS_Afma6(_q, &timestamp));
1435  for (unsigned int i = 0; i < njoint; i++) {
1436  position[i] = _q[i];
1437  }
1438 
1439  return;
1440  }
1441  case vpRobot::REFERENCE_FRAME: {
1442  double _q[njoint];
1443  Try(PrimitiveACQ_POS_Afma6(_q, &timestamp));
1444 
1445  vpColVector q(njoint);
1446  for (unsigned int i = 0; i < njoint; i++)
1447  q[i] = _q[i];
1448 
1449  // Compute fMc
1450  vpHomogeneousMatrix fMc;
1451  vpAfma6::get_fMc(q, fMc);
1452 
1453  // From fMc extract the pose
1454  vpRotationMatrix fRc;
1455  fMc.extract(fRc);
1456  vpRxyzVector rxyz;
1457  rxyz.buildFrom(fRc);
1458 
1459  for (unsigned int i = 0; i < 3; i++) {
1460  position[i] = fMc[i][3]; // translation x,y,z
1461  position[i + 3] = rxyz[i]; // Euler rotation x,y,z
1462  }
1463  break;
1464  }
1465  case vpRobot::MIXT_FRAME: {
1466  vpERROR_TRACE("Cannot get position in mixt frame: not implemented");
1467  throw vpRobotException(vpRobotException::lowLevelError, "Cannot get position in mixt frame: "
1468  "not implemented");
1469  }
1470  }
1471 
1472  CatchPrint();
1473  if (TryStt < 0) {
1474  vpERROR_TRACE("Cannot get position.");
1475  throw vpRobotException(vpRobotException::lowLevelError, "Cannot get position.");
1476  }
1477 
1478  return;
1479 }
1485 {
1486  double timestamp;
1487  PrimitiveACQ_TIME_Afma6(&timestamp);
1488  return timestamp;
1489 }
1490 
1502 {
1503  double timestamp;
1504  getPosition(frame, position, timestamp);
1505 }
1506 
1516 void vpRobotAfma6::getPosition(const vpRobot::vpControlFrameType frame, vpPoseVector &position, double &timestamp)
1517 {
1518  vpColVector posRxyz;
1519  // recupere position en Rxyz
1520  this->getPosition(frame, posRxyz, timestamp);
1521  vpRxyzVector RxyzVect;
1522  for (unsigned int j = 0; j < 3; j++)
1523  RxyzVect[j] = posRxyz[j + 3];
1524  // recupere le vecteur thetaU correspondant
1525  vpThetaUVector RtuVect(RxyzVect);
1526 
1527  // remplit le vpPoseVector avec translation et rotation ThetaU
1528  for (unsigned int j = 0; j < 3; j++) {
1529  position[j] = posRxyz[j];
1530  position[j + 3] = RtuVect[j];
1531  }
1532 }
1533 
1545 {
1546  double timestamp;
1547  getPosition(frame, position, timestamp);
1548 }
1549 
1614 {
1616  vpERROR_TRACE("Cannot send a velocity to the robot "
1617  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
1619  "Cannot send a velocity to the robot "
1620  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
1621  }
1622 
1623  vpColVector vel_max(6);
1624 
1625  for (unsigned int i = 0; i < 3; i++)
1626  vel_max[i] = getMaxTranslationVelocity();
1627  for (unsigned int i = 3; i < 6; i++)
1628  vel_max[i] = getMaxRotationVelocity();
1629 
1630  vpColVector vel_sat = vpRobot::saturateVelocities(vel, vel_max, true);
1631 
1632  InitTry;
1633 
1634  switch (frame) {
1635  case vpRobot::CAMERA_FRAME: {
1636  Try(PrimitiveMOVESPEED_CART_Afma6(vel_sat.data, REPCAM_AFMA6));
1637  break;
1638  }
1639  case vpRobot::ARTICULAR_FRAME: {
1640  // Try( PrimitiveMOVESPEED_CART(vel_sat.data, REPART_AFMA6) );
1641  Try(PrimitiveMOVESPEED_Afma6(vel_sat.data));
1642  break;
1643  }
1644  case vpRobot::REFERENCE_FRAME: {
1645  Try(PrimitiveMOVESPEED_CART_Afma6(vel_sat.data, REPFIX_AFMA6));
1646  break;
1647  }
1648  case vpRobot::MIXT_FRAME: {
1649  Try(PrimitiveMOVESPEED_CART_Afma6(vel_sat.data, REPMIX_AFMA6));
1650  break;
1651  }
1652  default: {
1653  vpERROR_TRACE("Error in spec of vpRobot. "
1654  "Case not taken in account.");
1655  return;
1656  }
1657  }
1658 
1659  Catch();
1660  if (TryStt < 0) {
1661  if (TryStt == VelStopOnJoint) {
1662  Int32 axisInJoint[njoint];
1663  PrimitiveSTATUS_Afma6(NULL, NULL, NULL, NULL, NULL, axisInJoint, NULL);
1664  for (unsigned int i = 0; i < njoint; i++) {
1665  if (axisInJoint[i])
1666  std::cout << "\nWarning: Velocity control stopped: axis " << i + 1 << " on joint limit!" << std::endl;
1667  }
1668  } else {
1669  printf("\n%s(%d): Error %d", __FUNCTION__, TryLine, TryStt);
1670  if (TryString != NULL) {
1671  // The statement is in TryString, but we need to check the validity
1672  printf(" Error sentence %s\n", TryString); // Print the TryString
1673  } else {
1674  printf("\n");
1675  }
1676  }
1677  }
1678 
1679  return;
1680 }
1681 
1682 /* ------------------------------------------------------------------------ */
1683 /* --- GET ---------------------------------------------------------------- */
1684 /* ------------------------------------------------------------------------ */
1685 
1735 void vpRobotAfma6::getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity, double &timestamp)
1736 {
1737  velocity.resize(6);
1738  velocity = 0;
1739 
1740  double q[6];
1741  vpColVector q_cur(6);
1742  vpHomogeneousMatrix fMc_cur;
1743  vpHomogeneousMatrix cMc; // camera displacement
1744  double time_cur;
1745 
1746  InitTry;
1747 
1748  // Get the current joint position
1749  Try(PrimitiveACQ_POS_Afma6(q, &timestamp));
1750  time_cur = timestamp;
1751  for (unsigned int i = 0; i < njoint; i++) {
1752  q_cur[i] = q[i];
1753  }
1754 
1755  // Get the camera pose from the direct kinematics
1756  vpAfma6::get_fMc(q_cur, fMc_cur);
1757 
1758  if (!first_time_getvel) {
1759 
1760  switch (frame) {
1761  case vpRobot::CAMERA_FRAME: {
1762  // Compute the displacement of the camera since the previous call
1763  cMc = fMc_prev_getvel.inverse() * fMc_cur;
1764 
1765  // Compute the velocity of the camera from this displacement
1766  velocity = vpExponentialMap::inverse(cMc, time_cur - time_prev_getvel);
1767 
1768  break;
1769  }
1770 
1771  case vpRobot::ARTICULAR_FRAME: {
1772  velocity = (q_cur - q_prev_getvel) / (time_cur - time_prev_getvel);
1773  break;
1774  }
1775 
1776  case vpRobot::REFERENCE_FRAME: {
1777  // Compute the displacement of the camera since the previous call
1778  cMc = fMc_prev_getvel.inverse() * fMc_cur;
1779 
1780  // Compute the velocity of the camera from this displacement
1781  vpColVector v;
1782  v = vpExponentialMap::inverse(cMc, time_cur - time_prev_getvel);
1783 
1784  // Express this velocity in the reference frame
1785  vpVelocityTwistMatrix fVc(fMc_cur);
1786  velocity = fVc * v;
1787 
1788  break;
1789  }
1790 
1791  case vpRobot::MIXT_FRAME: {
1792  // Compute the displacement of the camera since the previous call
1793  cMc = fMc_prev_getvel.inverse() * fMc_cur;
1794 
1795  // Compute the ThetaU representation for the rotation
1796  vpRotationMatrix cRc;
1797  cMc.extract(cRc);
1798  vpThetaUVector thetaU;
1799  thetaU.buildFrom(cRc);
1800 
1801  for (unsigned int i = 0; i < 3; i++) {
1802  // Compute the translation displacement in the reference frame
1803  velocity[i] = fMc_prev_getvel[i][3] - fMc_cur[i][3];
1804  // Update the rotation displacement in the camera frame
1805  velocity[i + 3] = thetaU[i];
1806  }
1807 
1808  // Compute the velocity
1809  velocity /= (time_cur - time_prev_getvel);
1810  break;
1811  }
1812  }
1813  } else {
1814  first_time_getvel = false;
1815  }
1816 
1817  // Memorize the camera pose for the next call
1818  fMc_prev_getvel = fMc_cur;
1819 
1820  // Memorize the joint position for the next call
1821  q_prev_getvel = q_cur;
1822 
1823  // Memorize the time associated to the joint position for the next call
1824  time_prev_getvel = time_cur;
1825 
1826  CatchPrint();
1827  if (TryStt < 0) {
1828  vpERROR_TRACE("Cannot get velocity.");
1829  throw vpRobotException(vpRobotException::lowLevelError, "Cannot get velocity.");
1830  }
1831 }
1832 
1842 {
1843  double timestamp;
1844  getVelocity(frame, velocity, timestamp);
1845 }
1846 
1889 {
1890  vpColVector velocity;
1891  getVelocity(frame, velocity, timestamp);
1892 
1893  return velocity;
1894 }
1895 
1905 {
1906  vpColVector velocity;
1907  double timestamp;
1908  getVelocity(frame, velocity, timestamp);
1909 
1910  return velocity;
1911 }
1912 
1962 bool vpRobotAfma6::readPosFile(const std::string &filename, vpColVector &q)
1963 {
1964  std::ifstream fd(filename.c_str(), std::ios::in);
1965 
1966  if (!fd.is_open()) {
1967  return false;
1968  }
1969 
1970  std::string line;
1971  std::string key("R:");
1972  std::string id("#AFMA6 - Position");
1973  bool pos_found = false;
1974  int lineNum = 0;
1975 
1976  q.resize(njoint);
1977 
1978  while (std::getline(fd, line)) {
1979  lineNum++;
1980  if (lineNum == 1) {
1981  if (!(line.compare(0, id.size(), id) == 0)) { // check if Afma6 position file
1982  std::cout << "Error: this position file " << filename << " is not for Afma6 robot" << std::endl;
1983  return false;
1984  }
1985  }
1986  if ((line.compare(0, 1, "#") == 0)) { // skip comment
1987  continue;
1988  }
1989  if ((line.compare(0, key.size(), key) == 0)) { // decode position
1990  // check if there are at least njoint values in the line
1991  std::vector<std::string> chain = vpIoTools::splitChain(line, std::string(" "));
1992  if (chain.size() < njoint + 1) // try to split with tab separator
1993  chain = vpIoTools::splitChain(line, std::string("\t"));
1994  if (chain.size() < njoint + 1)
1995  continue;
1996 
1997  std::istringstream ss(line);
1998  std::string key_;
1999  ss >> key_;
2000  for (unsigned int i = 0; i < njoint; i++)
2001  ss >> q[i];
2002  pos_found = true;
2003  break;
2004  }
2005  }
2006 
2007  // converts rotations from degrees into radians
2008  q[3] = vpMath::rad(q[3]);
2009  q[4] = vpMath::rad(q[4]);
2010  q[5] = vpMath::rad(q[5]);
2011 
2012  fd.close();
2013 
2014  if (!pos_found) {
2015  std::cout << "Error: unable to find a position for Afma6 robot in " << filename << std::endl;
2016  return false;
2017  }
2018 
2019  return true;
2020 }
2021 
2046 bool vpRobotAfma6::savePosFile(const std::string &filename, const vpColVector &q)
2047 {
2048 
2049  FILE *fd;
2050  fd = fopen(filename.c_str(), "w");
2051  if (fd == NULL)
2052  return false;
2053 
2054  fprintf(fd, "\
2055 #AFMA6 - Position - Version 2.01\n\
2056 #\n\
2057 # R: X Y Z A B C\n\
2058 # Joint position: X, Y, Z: translations in meters\n\
2059 # A, B, C: rotations in degrees\n\
2060 #\n\
2061 #\n\n");
2062 
2063  // Save positions in mm and deg
2064  fprintf(fd, "R: %lf %lf %lf %lf %lf %lf\n", q[0], q[1], q[2], vpMath::deg(q[3]), vpMath::deg(q[4]),
2065  vpMath::deg(q[5]));
2066 
2067  fclose(fd);
2068  return (true);
2069 }
2070 
2081 void vpRobotAfma6::move(const std::string &filename)
2082 {
2083  vpColVector q;
2084 
2085  this->readPosFile(filename, q);
2087  this->setPositioningVelocity(10);
2089 }
2090 
2103 void vpRobotAfma6::move(const std::string &filename, const double velocity)
2104 {
2105  vpColVector q;
2106 
2107  this->readPosFile(filename, q);
2109  this->setPositioningVelocity(velocity);
2111 }
2112 
2120 {
2121  InitTry;
2122  Try(PrimitiveGripper_Afma6(1));
2123  std::cout << "Open the gripper..." << std::endl;
2124  CatchPrint();
2125  if (TryStt < 0) {
2126  vpERROR_TRACE("Cannot open the gripper");
2127  throw vpRobotException(vpRobotException::lowLevelError, "Cannot open the gripper.");
2128  }
2129 }
2130 
2139 {
2140  InitTry;
2141  Try(PrimitiveGripper_Afma6(0));
2142  std::cout << "Close the gripper..." << std::endl;
2143  CatchPrint();
2144  if (TryStt < 0) {
2145  vpERROR_TRACE("Cannot close the gripper");
2146  throw vpRobotException(vpRobotException::lowLevelError, "Cannot close the gripper.");
2147  }
2148 }
2149 
2169 {
2170  displacement.resize(6);
2171  displacement = 0;
2172 
2173  double q[6];
2174  vpColVector q_cur(6);
2175  vpHomogeneousMatrix fMc_cur, c_prevMc_cur;
2176  double timestamp;
2177 
2178  InitTry;
2179 
2180  // Get the current joint position
2181  Try(PrimitiveACQ_POS_Afma6(q, &timestamp));
2182  for (unsigned int i = 0; i < njoint; i++) {
2183  q_cur[i] = q[i];
2184  }
2185 
2186  // Compute the camera pose in the reference frame
2187  fMc_cur = get_fMc(q_cur);
2188 
2189  if (!first_time_getdis) {
2190  switch (frame) {
2191  case vpRobot::CAMERA_FRAME: {
2192  // Compute the camera dispacement from the previous pose
2193  c_prevMc_cur = fMc_prev_getdis.inverse() * fMc_cur;
2194 
2196  vpRotationMatrix R;
2197  c_prevMc_cur.extract(t);
2198  c_prevMc_cur.extract(R);
2199 
2200  vpRxyzVector rxyz;
2201  rxyz.buildFrom(R);
2202 
2203  for (unsigned int i = 0; i < 3; i++) {
2204  displacement[i] = t[i];
2205  displacement[i + 3] = rxyz[i];
2206  }
2207  break;
2208  }
2209 
2210  case vpRobot::ARTICULAR_FRAME: {
2211  displacement = q_cur - q_prev_getdis;
2212  break;
2213  }
2214 
2215  case vpRobot::REFERENCE_FRAME: {
2216  std::cout << "getDisplacement() REFERENCE_FRAME not implemented\n";
2217  return;
2218  }
2219 
2220  case vpRobot::MIXT_FRAME: {
2221  std::cout << "getDisplacement() MIXT_FRAME not implemented\n";
2222  return;
2223  }
2224  }
2225  } else {
2226  first_time_getdis = false;
2227  }
2228 
2229  // Memorize the joint position for the next call
2230  q_prev_getdis = q_cur;
2231 
2232  // Memorize the pose for the next call
2233  fMc_prev_getdis = fMc_cur;
2234 
2235  CatchPrint();
2236  if (TryStt < 0) {
2237  vpERROR_TRACE("Cannot get velocity.");
2238  throw vpRobotException(vpRobotException::lowLevelError, "Cannot get velocity.");
2239  }
2240 }
2241 
2253 {
2254  Int32 axisInJoint[njoint];
2255  bool status = true;
2256  jointsStatus.resize(6);
2257  InitTry;
2258 
2259  Try(PrimitiveSTATUS_Afma6(NULL, NULL, NULL, NULL, NULL, axisInJoint, NULL));
2260  for (unsigned int i = 0; i < njoint; i++) {
2261  if (axisInJoint[i]) {
2262  std::cout << "\nWarning: Velocity control stopped: axis " << i + 1 << " on joint limit!" << std::endl;
2263  jointsStatus[i] = axisInJoint[i];
2264  status = false;
2265  } else {
2266  jointsStatus[i] = 0;
2267  }
2268  }
2269 
2270  Catch();
2271  if (TryStt < 0) {
2272  vpERROR_TRACE("Cannot check joint limits.");
2273  throw vpRobotException(vpRobotException::lowLevelError, "Cannot check joint limits.");
2274  }
2275 
2276  return status;
2277 }
2278 
2279 #elif !defined(VISP_BUILD_SHARED_LIBS)
2280 // Work arround to avoid warning: libvisp_robot.a(vpRobotAfma6.cpp.o) has no
2281 // symbols
2282 void dummy_vpRobotAfma6(){};
2283 #endif
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Modelisation of Irisa&#39;s gantry robot named Afma6.
Definition: vpAfma6.h:78
static vpColVector inverse(const vpHomogeneousMatrix &M)
bool getPowerState()
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
vpRxyzVector buildFrom(const vpRotationMatrix &R)
vpRxyzVector _erc
Definition: vpAfma6.h:198
bool checkJointLimits(vpColVector &jointsStatus)
vpTranslationVector _etc
Definition: vpAfma6.h:197
static const unsigned int njoint
Number of joint.
Definition: vpAfma6.h:185
void get_eJe(vpMatrix &_eJe)
Error that can be emited by the vpRobot class and its derivates.
void set_eMc(const vpHomogeneousMatrix &eMc)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void setVerbose(bool verbose)
Definition: vpRobot.h:154
#define vpERROR_TRACE
Definition: vpDebug.h:393
double getTime() const
double getMaxTranslationVelocity(void) const
Definition: vpRobot.cpp:251
double _coupl_56
Definition: vpAfma6.h:192
int getInverseKinematics(const vpHomogeneousMatrix &fMc, vpColVector &q, const bool &nearest=true, const bool &verbose=false) const
Definition: vpAfma6.cpp:532
void setPosition(const vpRobot::vpControlFrameType frame, const vpPoseVector &pose)
Initialize the position controller.
Definition: vpRobot.h:68
vpAfma6ToolType
List of possible tools that can be attached to the robot end-effector.
Definition: vpAfma6.h:118
vpHomogeneousMatrix inverse() const
Class that defines a generic virtual robot.
Definition: vpRobot.h:58
static vpColVector saturateVelocities(const vpColVector &v_in, const vpColVector &v_max, bool verbose=false)
Definition: vpRobot.cpp:163
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
vpControlFrameType
Definition: vpRobot.h:75
void get_cMe(vpHomogeneousMatrix &_cMe) const
vpCameraParameters::vpCameraParametersProjType projModel
Definition: vpAfma6.h:206
double getMaxRotationVelocity(void) const
Definition: vpRobot.cpp:273
void extract(vpRotationMatrix &R) const
double getPositioningVelocity(void)
vpHomogeneousMatrix get_fMc(const vpColVector &q) const
Definition: vpAfma6.cpp:707
vpThetaUVector buildFrom(const vpHomogeneousMatrix &M)
virtual ~vpRobotAfma6(void)
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
void setPositioningVelocity(const double velocity)
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
void setToolType(vpAfma6::vpAfma6ToolType tool)
Set the current tool type.
Definition: vpAfma6.h:185
Implementation of a rotation matrix and operations on such kind of matrices.
void init(void)
static const vpAfma6ToolType defaultTool
Default tool attached to the robot end effector.
Definition: vpAfma6.h:127
void get_cVe(vpVelocityTwistMatrix &_cVe) const
static bool readPosFile(const std::string &filename, vpColVector &q)
static bool savePosFile(const std::string &filename, const vpColVector &q)
Initialize the velocity controller.
Definition: vpRobot.h:67
virtual vpRobotStateType getRobotState(void) const
Definition: vpRobot.h:139
vpRobotStateType
Definition: vpRobot.h:64
void move(const std::string &filename)
bool verbose_
Definition: vpRobot.h:111
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpRotationMatrix buildFrom(const vpHomogeneousMatrix &M)
void get_fJe(vpMatrix &_fJe)
void get_eJe(const vpColVector &q, vpMatrix &eJe) const
Definition: vpAfma6.cpp:864
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
static std::vector< std::string > splitChain(const std::string &chain, const std::string &sep)
Definition: vpIoTools.cpp:1665
void get_fJe(const vpColVector &q, vpMatrix &fJe) const
Definition: vpAfma6.cpp:934
static double rad(double deg)
Definition: vpMath.h:102
virtual void set_eMc(const vpHomogeneousMatrix &eMc)
Definition: vpAfma6.cpp:1119
double _long_56
Definition: vpAfma6.h:193
static double deg(double rad)
Definition: vpMath.h:95
vpMatrix eJe
robot Jacobian expressed in the end-effector frame
Definition: vpRobot.h:99
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:92
void get_cMe(vpHomogeneousMatrix &cMe) const
Definition: vpAfma6.cpp:820
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:156
void init(void)
Definition: vpAfma6.cpp:153
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
double _joint_max[6]
Definition: vpAfma6.h:194
vpMatrix fJe
robot Jacobian expressed in the robot reference frame available
Definition: vpRobot.h:103
Class that consider the case of a translation vector.
Implementation of a rotation vector as axis-angle minimal representation.
double _joint_min[6]
Definition: vpAfma6.h:195
static const double defaultPositioningVelocity
Definition: vpRobotAfma6.h:251
void getDisplacement(vpRobot::vpControlFrameType frame, vpColVector &displacement)
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:241