Visual Servoing Platform  version 3.0.0
vpRobotPtu46.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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 
50 /* ---------------------------------------------------------------------- */
51 /* --- STATIC ------------------------------------------------------------ */
52 /* ------------------------------------------------------------------------ */
53 
54 bool vpRobotPtu46::robotAlreadyCreated = false;
56 
57 /* ----------------------------------------------------------------------- */
58 /* --- CONSTRUCTOR ------------------------------------------------------ */
59 /* ---------------------------------------------------------------------- */
60 
61 
71 vpRobotPtu46::vpRobotPtu46 (const char *device)
72  :
73  vpRobot ()
74 {
75  this->device = new char [FILENAME_MAX];
76 
77  sprintf(this->device, "%s", device);
78 
79  vpDEBUG_TRACE (12, "Open communication with Ptu-46.");
80  try
81  {
82  init();
83  }
84  catch(...)
85  {
86  delete [] this->device;
87  vpERROR_TRACE("Error caught") ;
88  throw ;
89  }
90 
91  try
92  {
94  }
95  catch(...)
96  {
97  delete [] this->device;
98  vpERROR_TRACE("Error caught") ;
99  throw ;
100  }
101  positioningVelocity = defaultPositioningVelocity ;
102  return ;
103 }
104 
105 /* ------------------------------------------------------------------------ */
106 /* --- DESTRUCTOR -------------------------------------------------------- */
107 /* ------------------------------------------------------------------------ */
108 
116 {
117 
119 
120  if (0 != ptu.close())
121  {
122  vpERROR_TRACE ("Error while closing communications with the robot ptu-46.");
123  }
124 
125  vpRobotPtu46::robotAlreadyCreated = false;
126 
127  delete [] device;
128 
129  return;
130 }
131 
132 
133 /* -------------------------------------------------------------------------- */
134 /* --- INITIALISATION ------------------------------------------------------- */
135 /* -------------------------------------------------------------------------- */
136 
146 void
148 {
149 
150  vpDEBUG_TRACE (12, "Open connection Ptu-46.");
151  if (0 != ptu.init(device) )
152  {
153  vpERROR_TRACE ("Cannot open connexion with ptu-46.");
155  "Cannot open connexion with ptu-46");
156  }
157 
158  return ;
159 }
160 
161 
170 {
171  switch (newState)
172  {
173  case vpRobot::STATE_STOP:
174  {
176  {
177  ptu.stop();
178  }
179  break;
180  }
182  {
184  {
185  vpDEBUG_TRACE (12, "Passage vitesse -> position.");
186  ptu.stop();
187  }
188  else
189  {
190  vpDEBUG_TRACE (1, "Passage arret -> position.");
191  }
192  break;
193  }
195  {
197  {
198  vpDEBUG_TRACE (10, "Arret du robot...");
199  ptu.stop();
200  }
201  break;
202  }
203  default:
204  break ;
205  }
206 
207  return vpRobot::setRobotState (newState);
208 }
209 
215 void
217 {
218  ptu.stop();
220 }
221 
222 
233 void
235 {
236  vpHomogeneousMatrix cMe ;
237  vpPtu46::get_cMe(cMe) ;
238 
239  cVe.buildFrom(cMe) ;
240 }
241 
251 void
253 {
254  vpPtu46::get_cMe(cMe) ;
255 }
256 
267 void
269 {
270  vpColVector q(2) ;
272 
273  try
274  {
275  vpPtu46::get_eJe(q,eJe) ;
276  }
277  catch(...)
278  {
279  vpERROR_TRACE("catch exception ") ;
280  throw ;
281  }
282 }
283 
291 void
293 {
294  vpColVector q(2) ;
296 
297  try
298  {
299  vpPtu46::get_fJe(q,fJe) ;
300  }
301  catch(...)
302  {
303  vpERROR_TRACE("Error caught");
304  throw ;
305  }
306 
307 }
308 
309 
316 void
318 {
319  positioningVelocity = velocity;
320 }
327 double
329 {
330  return positioningVelocity;
331 }
332 
333 
350 void
352  const vpColVector & q )
353 {
354 
356  {
357  vpERROR_TRACE ("Robot was not in position-based control\n"
358  "Modification of the robot state");
360  }
361 
362  switch(frame)
363  {
365  vpERROR_TRACE ("Cannot move the robot in camera frame: "
366  "not implemented");
368  "Cannot move the robot in camera frame: "
369  "not implemented");
370  break;
372  vpERROR_TRACE ("Cannot move the robot in reference frame: "
373  "not implemented");
375  "Cannot move the robot in reference frame: "
376  "not implemented");
377  break;
378  case vpRobot::MIXT_FRAME:
379  vpERROR_TRACE ("Cannot move the robot in mixt frame: "
380  "not implemented");
382  "Cannot move the robot in mixt frame: "
383  "not implemented");
384  break;
386  break ;
387  }
388 
389  // Interface for the controller
390  double artpos[2];
391 
392  artpos[0] = q[0];
393  artpos[1] = q[1];
394 
395  if (0 != ptu.move(artpos, positioningVelocity, PTU_ABSOLUTE_MODE) )
396  {
397  vpERROR_TRACE ("Positionning error.");
399  "Positionning error.");
400  }
401 
402  return ;
403 }
404 
405 
422 void
424  const double &q1, const double &q2)
425 {
426  try{
427  vpColVector q(2) ;
428  q[0] = q1 ;
429  q[1] = q2 ;
430 
431  setPosition(frame,q) ;
432  }
433  catch(...)
434  {
435  vpERROR_TRACE("Error caught");
436  throw ;
437  }
438 }
439 
453 void
454 vpRobotPtu46::setPosition(const char *filename)
455 {
456  vpColVector q ;
457  if (readPositionFile(filename, q) == false) {
458  vpERROR_TRACE ("Cannot get ptu-46 position from file");
460  "Cannot get ptu-46 position from file");
461  }
463 }
464 
478 void
480  vpColVector & q)
481 {
482  vpDEBUG_TRACE (9, "# Entree.");
483 
484  switch(frame)
485  {
486  case vpRobot::CAMERA_FRAME :
487  vpERROR_TRACE ("Cannot get position in camera frame: not implemented");
489  "Cannot get position in camera frame: "
490  "not implemented");
491  break;
493  vpERROR_TRACE ("Cannot get position in reference frame: "
494  "not implemented");
496  "Cannot get position in reference frame: "
497  "not implemented");
498  break;
499  case vpRobot::MIXT_FRAME:
500  vpERROR_TRACE ("Cannot get position in mixt frame: "
501  "not implemented");
503  "Cannot get position in mixt frame: "
504  "not implemented");
505  break;
507  break ;
508  }
509 
510  double artpos[2];
511 
512  if (0 != ptu.getCurrentPosition( artpos ) )
513  {
514  vpERROR_TRACE ("Error when calling recup_posit_Afma4.");
516  "Error when calling recup_posit_Afma4.");
517  }
518 
519  q.resize (vpPtu46::ndof);
520 
521  q[0] = artpos[0];
522  q[1] = artpos[1];
523 }
524 
525 
555 void
557  const vpColVector & v)
558 {
559  TPtuFrame ptuFrameInterface;
560 
562  {
563  vpERROR_TRACE ("Cannot send a velocity to the robot "
564  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
566  "Cannot send a velocity to the robot "
567  "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
568  }
569 
570  switch(frame)
571  {
572  case vpRobot::CAMERA_FRAME :
573  {
574  ptuFrameInterface = PTU_CAMERA_FRAME;
575  if ( v.getRows() != 2) {
576  vpERROR_TRACE ("Bad dimension fo speed vector in camera frame");
578  "Bad dimension for speed vector "
579  "in camera frame");
580  }
581  break ;
582  }
584  {
585  ptuFrameInterface = PTU_ARTICULAR_FRAME;
586  if ( v.getRows() != 2) {
587  vpERROR_TRACE ("Bad dimension fo speed vector in articular frame");
589  "Bad dimension for speed vector "
590  "in articular frame");
591  }
592  break ;
593  }
595  {
596  vpERROR_TRACE ("Cannot send a velocity to the robot "
597  "in the reference frame: "
598  "functionality not implemented");
600  "Cannot send a velocity to the robot "
601  "in the reference frame:"
602  "functionality not implemented");
603  break ;
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  break ;
615  }
616  default:
617  {
618  vpERROR_TRACE ("Error in spec of vpRobot. "
619  "Case not taken in account.");
621  "Cannot send a velocity to the robot ");
622  }
623  }
624 
625  vpDEBUG_TRACE (12, "Velocity limitation.");
626  bool norm = false; // Flag to indicate when velocities need to be nomalized
627  double ptuSpeedInterface[2];
628 
629  switch(frame) {
631  case vpRobot::CAMERA_FRAME : {
632  double max = this ->maxRotationVelocity;
633  for (unsigned int i = 0 ; i < 2; ++ i) // rx and ry of the camera
634  {
635  if (fabs (v[i]) > max)
636  {
637  norm = true;
638  max = fabs (v[i]);
639  vpERROR_TRACE ("Excess velocity: ROTATION "
640  "(axe nr.%d).", i);
641  }
642  }
643  // Rotations velocities normalisation
644  if (norm == true) {
645  max = this ->maxRotationVelocity / max;
646  for (unsigned int i = 0 ; i < 2; ++ i)
647  ptuSpeedInterface [i] = v[i]*max;
648  }
649  break;
650  }
651  default:
652  // Should never occur
653  break;
654 
655  }
656 
657  vpCDEBUG(12) << "v: " << ptuSpeedInterface[0]
658  << " " << ptuSpeedInterface[1] << std::endl;
659  ptu.move(ptuSpeedInterface, ptuFrameInterface);
660  return;
661 }
662 
663 
664 /* ------------------------------------------------------------------------- */
665 /* --- GET ----------------------------------------------------------------- */
666 /* ------------------------------------------------------------------------- */
667 
668 
680 void
682  vpColVector & q_dot)
683 {
684 
685  TPtuFrame ptuFrameInterface = PTU_ARTICULAR_FRAME;
686 
687  switch (frame)
688  {
690  {
691  vpERROR_TRACE ("Cannot get a velocity in the camera frame: "
692  "functionality not implemented");
694  "Cannot get a velocity in the camera frame:"
695  "functionality not implemented");
696  break ;
697  }
699  {
700  ptuFrameInterface = PTU_ARTICULAR_FRAME;
701  break ;
702  }
704  {
705  vpERROR_TRACE ("Cannot get a velocity in the reference frame: "
706  "functionality not implemented");
708  "Cannot get a velocity in the reference frame:"
709  "functionality not implemented");
710  break ;
711  }
712  case vpRobot::MIXT_FRAME:
713  {
714 
715  vpERROR_TRACE ("Cannot get a velocity in the mixt frame: "
716  "functionality not implemented");
718  "Cannot get a velocity in the mixt frame:"
719  "functionality not implemented");
720  break ;
721  }
722  }
723 
724  q_dot.resize(vpPtu46::ndof);
725  double ptuSpeedInterface[2];
726 
727  ptu.getCurrentSpeed(ptuSpeedInterface, ptuFrameInterface);
728 
729  q_dot[0] = ptuSpeedInterface[0];
730  q_dot[1] = ptuSpeedInterface[1];
731 
732 }
733 
734 
748 {
749  vpColVector q_dot;
750  getVelocity (frame, q_dot);
751 
752  return q_dot;
753 }
754 
774 bool
776 {
777  FILE * pt_f ;
778  pt_f = fopen(filename,"r") ;
779 
780  if (pt_f == NULL) {
781  vpERROR_TRACE ("Can not open ptu-46 position file %s", filename);
782  return false;
783  }
784 
785  char line[FILENAME_MAX];
786  char head[] = "R:";
787  bool end = false;
788 
789  do {
790  // skip lines begining with # for comments
791  if (fgets (line, 100, pt_f) != NULL) {
792  if ( strncmp (line, "#", 1) != 0) {
793  // this line is not a comment
794  if ( fscanf (pt_f, "%s", line) != EOF) {
795  if ( strcmp (line, head) == 0)
796  end = true; // robot position was found
797  }
798  else
799  return (false); // end of file without position
800  }
801  }
802  else {
803  return (false);// end of file
804  }
805 
806  }
807  while ( end != true );
808 
809  double q1,q2;
810  // Read positions
811  fscanf(pt_f, "%lf %lf", &q1, &q2);
812  q.resize(vpPtu46::ndof) ;
813 
814  q[0] = vpMath::rad(q1) ; // Rot tourelle
815  q[1] = vpMath::rad(q2) ;
816 
817  fclose(pt_f) ;
818  return (true);
819 }
820 
833 void
834 vpRobotPtu46::getCameraDisplacement(vpColVector &v)
835 {
837 
838 }
850 void vpRobotPtu46::getArticularDisplacement(vpColVector &d)
851 {
853 }
854 
876 void
878  vpColVector &d)
879 {
880  double d_[6];
881 
882  switch (frame)
883  {
885  {
886  d.resize (6);
887  ptu.measureDpl(d_, PTU_CAMERA_FRAME);
888  d[0]=d_[0];
889  d[1]=d_[1];
890  d[2]=d_[2];
891  d[3]=d_[3];
892  d[4]=d_[4];
893  d[5]=d_[5];
894  break ;
895  }
897  {
898  ptu.measureDpl(d_, PTU_ARTICULAR_FRAME);
899  d.resize (vpPtu46::ndof);
900  d[0]=d_[0]; // pan
901  d[1]=d_[1]; // tilt
902  break ;
903  }
905  {
906  vpERROR_TRACE ("Cannot get a displacement in the reference frame: "
907  "functionality not implemented");
909  "Cannot get a displacement in the reference frame:"
910  "functionality not implemented");
911  break ;
912  }
913  case vpRobot::MIXT_FRAME:
914  {
915  vpERROR_TRACE ("Cannot get a displacement in the mixt frame: "
916  "functionality not implemented");
918  "Cannot get a displacement in the reference frame:"
919  "functionality not implemented");
920 
921  break ;
922  }
923  }
924 }
925 
926 #elif !defined(VISP_BUILD_SHARED_LIBS)
927 // Work arround to avoid warning: libvisp_robot.a(vpRobotPtu46.cpp.o) has no symbols
928 void dummy_vpRobotPtu46() {};
929 #endif
930 
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
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 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)
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)
static double rad(double deg)
Definition: vpMath.h:104
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:138
#define vpDEBUG_TRACE
Definition: vpDebug.h:478
bool readPositionFile(const char *filename, vpColVector &q)
void setPositioningVelocity(const double velocity)
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:217