Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpRobotPtu46.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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Interface for the ptu-46 robot.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
38 #include <signal.h>
39 #include <string.h>
40 
41 #include <visp3/core/vpConfig.h>
42 #ifdef VISP_HAVE_PTU46
43 
44 /* Headers des fonctions implementees. */
45 #include <visp3/robot/vpPtu46.h>
46 #include <visp3/robot/vpRobotPtu46.h>
47 #include <visp3/robot/vpRobotException.h>
48 #include <visp3/core/vpDebug.h>
49 #include <visp3/core/vpIoTools.h>
50 
51 /* ---------------------------------------------------------------------- */
52 /* --- STATIC ------------------------------------------------------------ */
53 /* ------------------------------------------------------------------------ */
54 
55 bool vpRobotPtu46::robotAlreadyCreated = false;
57 
58 /* ----------------------------------------------------------------------- */
59 /* --- CONSTRUCTOR ------------------------------------------------------ */
60 /* ---------------------------------------------------------------------- */
61 
62 
72 vpRobotPtu46::vpRobotPtu46 (const char *device)
73  :
74  vpRobot ()
75 {
76  this->device = new char [FILENAME_MAX];
77 
78  sprintf(this->device, "%s", device);
79 
80  vpDEBUG_TRACE (12, "Open communication with Ptu-46.");
81  try
82  {
83  init();
84  }
85  catch(...)
86  {
87  delete [] this->device;
88  vpERROR_TRACE("Error caught") ;
89  throw ;
90  }
91 
92  try
93  {
95  }
96  catch(...)
97  {
98  delete [] this->device;
99  vpERROR_TRACE("Error caught") ;
100  throw ;
101  }
102  positioningVelocity = defaultPositioningVelocity ;
103  return ;
104 }
105 
106 /* ------------------------------------------------------------------------ */
107 /* --- DESTRUCTOR -------------------------------------------------------- */
108 /* ------------------------------------------------------------------------ */
109 
117 {
118 
120 
121  if (0 != ptu.close())
122  {
123  vpERROR_TRACE ("Error while closing communications with the robot ptu-46.");
124  }
125 
126  vpRobotPtu46::robotAlreadyCreated = false;
127 
128  delete [] device;
129 
130  return;
131 }
132 
133 
134 /* -------------------------------------------------------------------------- */
135 /* --- INITIALISATION ------------------------------------------------------- */
136 /* -------------------------------------------------------------------------- */
137 
147 void
149 {
150 
151  vpDEBUG_TRACE (12, "Open connection Ptu-46.");
152  if (0 != ptu.init(device) )
153  {
154  vpERROR_TRACE ("Cannot open connection with ptu-46.");
156  "Cannot open connection with ptu-46");
157  }
158 
159  return ;
160 }
161 
162 
171 {
172  switch (newState)
173  {
174  case vpRobot::STATE_STOP:
175  {
177  {
178  ptu.stop();
179  }
180  break;
181  }
183  {
185  {
186  vpDEBUG_TRACE (12, "Passage vitesse -> position.");
187  ptu.stop();
188  }
189  else
190  {
191  vpDEBUG_TRACE (1, "Passage arret -> position.");
192  }
193  break;
194  }
196  {
198  {
199  vpDEBUG_TRACE (10, "Arret du robot...");
200  ptu.stop();
201  }
202  break;
203  }
204  default:
205  break ;
206  }
207 
208  return vpRobot::setRobotState (newState);
209 }
210 
216 void
218 {
219  ptu.stop();
221 }
222 
223 
234 void
236 {
237  vpHomogeneousMatrix cMe ;
238  vpPtu46::get_cMe(cMe) ;
239 
240  cVe.buildFrom(cMe) ;
241 }
242 
252 void
254 {
255  vpPtu46::get_cMe(cMe) ;
256 }
257 
268 void
270 {
271  vpColVector q(2) ;
273 
274  try
275  {
276  vpPtu46::get_eJe(q,eJe) ;
277  }
278  catch(...)
279  {
280  vpERROR_TRACE("catch exception ") ;
281  throw ;
282  }
283 }
284 
292 void
294 {
295  vpColVector q(2) ;
297 
298  try
299  {
300  vpPtu46::get_fJe(q,fJe) ;
301  }
302  catch(...)
303  {
304  vpERROR_TRACE("Error caught");
305  throw ;
306  }
307 
308 }
309 
310 
317 void
319 {
320  positioningVelocity = velocity;
321 }
328 double
330 {
331  return positioningVelocity;
332 }
333 
334 
351 void
353  const vpColVector & q )
354 {
355 
357  {
358  vpERROR_TRACE ("Robot was not in position-based control\n"
359  "Modification of the robot state");
361  }
362 
363  switch(frame)
364  {
366  vpERROR_TRACE ("Cannot move the robot in camera frame: "
367  "not implemented");
369  "Cannot move the robot in camera frame: "
370  "not implemented");
371  break;
373  vpERROR_TRACE ("Cannot move the robot in reference frame: "
374  "not implemented");
376  "Cannot move the robot in reference frame: "
377  "not implemented");
378  break;
379  case vpRobot::MIXT_FRAME:
380  vpERROR_TRACE ("Cannot move the robot in mixt frame: "
381  "not implemented");
383  "Cannot move the robot in mixt frame: "
384  "not implemented");
385  break;
387  break ;
388  }
389 
390  // Interface for the controller
391  double artpos[2];
392 
393  artpos[0] = q[0];
394  artpos[1] = q[1];
395 
396  if (0 != ptu.move(artpos, positioningVelocity, PTU_ABSOLUTE_MODE) )
397  {
398  vpERROR_TRACE ("Positionning error.");
400  "Positionning error.");
401  }
402 
403  return ;
404 }
405 
406 
423 void
425  const double &q1, const double &q2)
426 {
427  try{
428  vpColVector q(2) ;
429  q[0] = q1 ;
430  q[1] = q2 ;
431 
432  setPosition(frame,q) ;
433  }
434  catch(...)
435  {
436  vpERROR_TRACE("Error caught");
437  throw ;
438  }
439 }
440 
454 void
455 vpRobotPtu46::setPosition(const char *filename)
456 {
457  vpColVector q ;
458  if (readPositionFile(filename, q) == false) {
459  vpERROR_TRACE ("Cannot get ptu-46 position from file");
461  "Cannot get ptu-46 position from file");
462  }
464 }
465 
479 void
481  vpColVector & q)
482 {
483  vpDEBUG_TRACE (9, "# Entree.");
484 
485  switch(frame)
486  {
487  case vpRobot::CAMERA_FRAME :
488  vpERROR_TRACE ("Cannot get position in camera frame: not implemented");
490  "Cannot get position in camera frame: "
491  "not implemented");
492  break;
494  vpERROR_TRACE ("Cannot get position in reference frame: "
495  "not implemented");
497  "Cannot get position in reference frame: "
498  "not implemented");
499  break;
500  case vpRobot::MIXT_FRAME:
501  vpERROR_TRACE ("Cannot get position in mixt frame: "
502  "not implemented");
504  "Cannot get position in mixt frame: "
505  "not implemented");
506  break;
508  break ;
509  }
510 
511  double artpos[2];
512 
513  if (0 != ptu.getCurrentPosition( artpos ) )
514  {
515  vpERROR_TRACE ("Error when calling recup_posit_Afma4.");
517  "Error when calling recup_posit_Afma4.");
518  }
519 
520  q.resize (vpPtu46::ndof);
521 
522  q[0] = artpos[0];
523  q[1] = artpos[1];
524 }
525 
526 
556 void
558  const vpColVector & v)
559 {
560  TPtuFrame ptuFrameInterface;
561 
563  {
564  vpERROR_TRACE ("Cannot send a velocity to the robot "
565  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
567  "Cannot send a velocity to the robot "
568  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
569  }
570 
571  switch(frame)
572  {
573  case vpRobot::CAMERA_FRAME :
574  {
575  ptuFrameInterface = PTU_CAMERA_FRAME;
576  if ( v.getRows() != 2) {
577  vpERROR_TRACE ("Bad dimension fo speed vector in camera frame");
579  "Bad dimension for speed vector "
580  "in camera frame");
581  }
582  break ;
583  }
585  {
586  ptuFrameInterface = PTU_ARTICULAR_FRAME;
587  if ( v.getRows() != 2) {
588  vpERROR_TRACE ("Bad dimension fo speed vector in articular frame");
590  "Bad dimension for speed vector "
591  "in articular frame");
592  }
593  break ;
594  }
596  {
597  vpERROR_TRACE ("Cannot send a velocity to the robot "
598  "in the reference frame: "
599  "functionality not implemented");
601  "Cannot send a velocity to the robot "
602  "in the reference frame:"
603  "functionality not implemented");
604  }
605  case vpRobot::MIXT_FRAME :
606  {
607  vpERROR_TRACE ("Cannot send a velocity to the robot "
608  "in the mixt frame: "
609  "functionality not implemented");
611  "Cannot send a velocity to the robot "
612  "in the mixt frame:"
613  "functionality not implemented");
614  }
615  default:
616  {
617  vpERROR_TRACE ("Error in spec of vpRobot. "
618  "Case not taken in account.");
620  "Cannot send a velocity to the robot ");
621  }
622  }
623 
624  vpDEBUG_TRACE (12, "Velocity limitation.");
625  double ptuSpeedInterface[2];
626 
627  switch(frame) {
629  case vpRobot::CAMERA_FRAME : {
630  double max = this ->maxRotationVelocity;
631  bool norm = false; // Flag to indicate when velocities need to be nomalized
632  for (unsigned int i = 0 ; i < 2; ++ i) // rx and ry of the camera
633  {
634  if (fabs (v[i]) > max)
635  {
636  norm = true;
637  max = fabs (v[i]);
638  vpERROR_TRACE ("Excess velocity: ROTATION "
639  "(axe nr.%d).", i);
640  }
641  }
642  // Rotations velocities normalisation
643  if (norm == true) {
644  max = this ->maxRotationVelocity / max;
645  for (unsigned int i = 0 ; i < 2; ++ i)
646  ptuSpeedInterface [i] = v[i]*max;
647  }
648  break;
649  }
650  default:
651  // Should never occur
652  break;
653 
654  }
655 
656  vpCDEBUG(12) << "v: " << ptuSpeedInterface[0]
657  << " " << ptuSpeedInterface[1] << std::endl;
658  ptu.move(ptuSpeedInterface, ptuFrameInterface);
659  return;
660 }
661 
662 
663 /* ------------------------------------------------------------------------- */
664 /* --- GET ----------------------------------------------------------------- */
665 /* ------------------------------------------------------------------------- */
666 
667 
679 void
681  vpColVector & q_dot)
682 {
683 
684  TPtuFrame ptuFrameInterface = PTU_ARTICULAR_FRAME;
685 
686  switch (frame)
687  {
689  {
690  vpERROR_TRACE ("Cannot get a velocity in the camera frame: "
691  "functionality not implemented");
693  "Cannot get a velocity in the camera frame:"
694  "functionality not implemented");
695  }
697  {
698  ptuFrameInterface = PTU_ARTICULAR_FRAME;
699  break ;
700  }
702  {
703  vpERROR_TRACE ("Cannot get a velocity in the reference frame: "
704  "functionality not implemented");
706  "Cannot get a velocity in the reference frame:"
707  "functionality not implemented");
708  }
709  case vpRobot::MIXT_FRAME:
710  {
711 
712  vpERROR_TRACE ("Cannot get a velocity in the mixt frame: "
713  "functionality not implemented");
715  "Cannot get a velocity in the mixt frame:"
716  "functionality not implemented");
717  }
718  }
719 
720  q_dot.resize(vpPtu46::ndof);
721  double ptuSpeedInterface[2];
722 
723  ptu.getCurrentSpeed(ptuSpeedInterface, ptuFrameInterface);
724 
725  q_dot[0] = ptuSpeedInterface[0];
726  q_dot[1] = ptuSpeedInterface[1];
727 
728 }
729 
730 
744 {
745  vpColVector q_dot;
746  getVelocity (frame, q_dot);
747 
748  return q_dot;
749 }
750 
770 bool
771 vpRobotPtu46::readPositionFile(const std::string &filename, vpColVector &q)
772 {
773  std::ifstream fd(filename.c_str(), std::ios::in);
774 
775  if(! fd.is_open()) {
776  return false;
777  }
778 
779  std::string line;
780  std::string key("R:");
781  std::string id("#PTU-EVI - Position");
782  bool pos_found = false;
783  int lineNum = 0;
784 
786 
787  while(std::getline(fd, line)) {
788  lineNum ++;
789  if (lineNum == 1) {
790  if(! (line.compare(0, id.size(), id) == 0)) { // check if Ptu-46 position file
791  std::cout << "Error: this position file " << filename << " is not for Ptu-46 robot" << std::endl;
792  return false;
793  }
794  }
795  if((line.compare(0, 1, "#") == 0)) { // skip comment
796  continue;
797  }
798  if((line.compare(0, key.size(), key) == 0)) { // decode position
799  // check if there are at least njoint values in the line
800  std::vector<std::string> chain = vpIoTools::splitChain(line, std::string(" "));
801  if (chain.size() < vpPtu46::ndof+1) // try to split with tab separator
802  chain = vpIoTools::splitChain(line, std::string("\t"));
803  if(chain.size() < vpPtu46::ndof+1)
804  continue;
805 
806  std::istringstream ss(line);
807  std::string key_;
808  ss >> key_;
809  for (unsigned int i=0; i< vpPtu46::ndof; i++)
810  ss >> q[i];
811  pos_found = true;
812  break;
813  }
814  }
815 
816  // converts rotations from degrees into radians
817  q.deg2rad();
818 
819  fd.close();
820 
821  if (!pos_found) {
822  std::cout << "Error: unable to find a position for Ptu-46 robot in " << filename << std::endl;
823  return false;
824  }
825 
826  return true;
827 }
828 
841 void
842 vpRobotPtu46::getCameraDisplacement(vpColVector &v)
843 {
845 
846 }
858 void vpRobotPtu46::getArticularDisplacement(vpColVector &d)
859 {
861 }
862 
884 void
886  vpColVector &d)
887 {
888  double d_[6];
889 
890  switch (frame)
891  {
893  {
894  d.resize (6);
895  ptu.measureDpl(d_, PTU_CAMERA_FRAME);
896  d[0]=d_[0];
897  d[1]=d_[1];
898  d[2]=d_[2];
899  d[3]=d_[3];
900  d[4]=d_[4];
901  d[5]=d_[5];
902  break ;
903  }
905  {
906  ptu.measureDpl(d_, PTU_ARTICULAR_FRAME);
907  d.resize (vpPtu46::ndof);
908  d[0]=d_[0]; // pan
909  d[1]=d_[1]; // tilt
910  break ;
911  }
913  {
914  vpERROR_TRACE ("Cannot get a displacement in the reference frame: "
915  "functionality not implemented");
917  "Cannot get a displacement in the reference frame:"
918  "functionality not implemented");
919  }
920  case vpRobot::MIXT_FRAME:
921  {
922  vpERROR_TRACE ("Cannot get a displacement in the mixt frame: "
923  "functionality not implemented");
925  "Cannot get a displacement in the reference frame:"
926  "functionality not implemented");
927  }
928  }
929 }
930 
931 #elif !defined(VISP_BUILD_SHARED_LIBS)
932 // Work arround to avoid warning: libvisp_robot.a(vpRobotPtu46.cpp.o) has no symbols
933 void dummy_vpRobotPtu46() {};
934 #endif
935 
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
void get_fJe(const vpColVector &q, vpMatrix &fJe) const
Definition: vpPtu46.cpp:303
Error that can be emited by the vpRobot class and its derivates.
void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &q)
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &q_dot)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void get_cMe(vpHomogeneousMatrix &_cMe) const
#define vpERROR_TRACE
Definition: vpDebug.h:391
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &q_dot)
static const double defaultPositioningVelocity
Definition: vpRobotPtu46.h:105
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &q)
Initialize the position controller.
Definition: vpRobot.h:69
void getDisplacement(vpRobot::vpControlFrameType frame, vpColVector &q)
Class that defines a generic virtual robot.
Definition: vpRobot.h:58
vpControlFrameType
Definition: vpRobot.h:76
void get_fJe(vpMatrix &_fJe)
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void deg2rad()
Definition: vpColVector.h:127
void get_cMe(vpHomogeneousMatrix &_cMe) const
Definition: vpPtu46.cpp:232
Initialize the velocity controller.
Definition: vpRobot.h:68
static const unsigned int ndof
Definition: vpPtu46.h:82
vpRobotStateType
Definition: vpRobot.h:64
virtual ~vpRobotPtu46(void)
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
#define vpCDEBUG(level)
Definition: vpDebug.h:502
void init(void)
static std::vector< std::string > splitChain(const std::string &chain, const std::string &sep)
Definition: vpIoTools.cpp:1596
Implementation of a velocity twist matrix and operations on such kind of matrices.
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
void get_eJe(vpMatrix &_eJe)
void get_eJe(const vpColVector &q, vpMatrix &eJe) const
Definition: vpPtu46.cpp:272
void get_cVe(vpVelocityTwistMatrix &_cVe) const
double getPositioningVelocity(void)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
virtual vpRobotStateType getRobotState(void) const
Definition: vpRobot.h:141
#define vpDEBUG_TRACE
Definition: vpDebug.h:478
bool readPositionFile(const std::string &filename, vpColVector &q)
void setPositioningVelocity(const double velocity)
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:225