Visual Servoing Platform  version 3.0.0
vpRobotWireFrameSimulator.h
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  * Basic class used to make robot simulators.
32  *
33  * Authors:
34  * Nicolas Melchior
35  *
36  *****************************************************************************/
37 
38 #ifndef vpRobotWireFrameSimulator_HH
39 #define vpRobotWireFrameSimulator_HH
40 
46 #include <visp3/core/vpConfig.h>
47 
48 #if defined(VISP_HAVE_MODULE_GUI) && (defined(_WIN32) || defined(VISP_HAVE_PTHREAD))
49 
50 #include <cmath> // std::fabs
51 #include <limits> // numeric_limits
52 #if defined(_WIN32)
53 # include <windows.h>
54 #elif defined(VISP_HAVE_PTHREAD)
55 # include <pthread.h>
56 #endif
57 
58 #include <visp3/robot/vpWireFrameSimulator.h>
59 #include <visp3/robot/vpRobot.h>
60 #include <visp3/robot/vpRobotSimulator.h>
61 #include <visp3/gui/vpDisplayOpenCV.h>
62 #include <visp3/gui/vpDisplayX.h>
63 #include <visp3/gui/vpDisplayGTK.h>
64 #include <visp3/gui/vpDisplayGDI.h>
65 #include <visp3/gui/vpDisplayD3D.h>
66 
85 {
86  public:
88 
89  typedef enum
90  {
92  MODEL_DH
93  } vpDisplayRobotType;
94 
95 
96  protected:
98  double tcur;
100  double tprev;
101 
103  Bound_scene* robotArms;
104 
106  unsigned int size_fMi;
111 
118 
119 #if defined(_WIN32)
120  HANDLE hThread;
121  HANDLE mutex_fMi;
122  HANDLE mutex_artVel;
123  HANDLE mutex_artCoord;
124  HANDLE mutex_velocity;
125  HANDLE mutex_display;
126 #elif defined(VISP_HAVE_PTHREAD)
127  pthread_t thread;
128  pthread_attr_t attr;
129  pthread_mutex_t mutex_fMi;
130  pthread_mutex_t mutex_artVel;
131  pthread_mutex_t mutex_artCoord;
132  pthread_mutex_t mutex_velocity;
133  pthread_mutex_t mutex_display;
134 #endif
135 
137 
139  bool robotStop;
143  unsigned int jointLimitArt;
146 
149 
150  #if defined VISP_HAVE_X11
152  #elif defined VISP_HAVE_GDI
153  vpDisplayGDI display;
154  #elif defined VISP_HAVE_OPENCV
155  vpDisplayOpenCV display;
156  #elif defined VISP_HAVE_D3D9
157  vpDisplayD3D display;
158  #elif defined VISP_HAVE_GTK
159  vpDisplayGTK display;
160  #endif
161 
163 
167 
170 
171  bool verbose_;
172 
173 //private:
174 //#ifndef DOXYGEN_SHOULD_SKIP_THIS
175 // vpRobotWireFrameSimulator(const vpRobotWireFrameSimulator &)
176 // : vpWireFrameSimulator(), vpRobotSimulator(),
177 // I(), tcur(0), tprev(0), robotArms(NULL), size_fMi(8), fMi(NULL), artCoord(), artVel(), velocity(),
178 // #if defined(_WIN32)
179 // #elif defined(VISP_HAVE_PTHREAD)
180 // thread(), attr(),
181 // #endif
182 // mutex_fMi(), mutex_artVel(), mutex_artCoord(), mutex_velocity(), mutex_display(),
183 // displayBusy(false), robotStop(false), jointLimit(false), jointLimitArt(false), singularityManagement(true),
184 // cameraParam(),
185 // #if defined(VISP_HAVE_DISPLAY)
186 // display(),
187 // #endif
188 // displayType(MODEL_3D), displayAllowed(true), constantSamplingTimeMode(false),
189 // setVelocityCalled(false), verbose_(false)
190 // {
191 // throw vpException(vpException::functionNotImplementedError, "Not implemented!");
192 // }
193 // vpRobotWireFrameSimulator &operator=(const vpRobotWireFrameSimulator &){
194 // throw vpException(vpException::functionNotImplementedError, "Not implemented!");
195 // return *this;
196 // }
197 //#endif
198 
199  public:
201  vpRobotWireFrameSimulator(bool display);
202  virtual ~vpRobotWireFrameSimulator();
203 
210  //if(px_ext != 1 && py_ext != 1)
211  // we assume px_ext and py_ext > 0
212  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
213  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
214  return vpCameraParameters(px_ext,py_ext,I.getWidth()/2,I.getHeight()/2);
215  else
216  {
217  unsigned int size = vpMath::minimum(I.getWidth(),I.getHeight())/2;
218  return vpCameraParameters(size,size,I.getWidth()/2,I.getHeight()/2);
219  }
220  }
227  {
229  }
230 
231  void getInternalView(vpImage<vpRGBa> &I);
232  void getInternalView(vpImage<unsigned char> &I);
233 
240  vpHomogeneousMatrix get_fMo() const {return fMo;}
241 
242  /* Display functions */
243  void initScene(const vpSceneObject &obj, const vpSceneDesiredObject &desiredObject);
244  void initScene(const char* obj, const char* desiredObject);
245  void initScene (const vpSceneObject &obj);
246  void initScene(const char* obj);
247 
253  void setCameraColor(const vpColor col) {camColor = col;}
254 
263  inline void setConstantSamplingTimeMode(const bool _constantSamplingTimeMode){
264  constantSamplingTimeMode = _constantSamplingTimeMode;
265  }
266 
272  void setCurrentViewColor(const vpColor col) {curColor = col;}
273 
279  void setDesiredViewColor(const vpColor col) {desColor = col;}
280 
287  {
289  }
290 
296  inline void setDisplayRobotType (const vpDisplayRobotType dispType) {displayType = dispType;}
303  {
305  }
309  void setGraphicsThickness(unsigned int thickness)
310  {
311  this->thickness_ = thickness;
312  }
313 
323  inline void setSamplingTime(const double &delta_t)
324  {
325  if(delta_t < static_cast<float>(vpTime::getMinTimeForUsleepCall() * 1e-3)){
326  this->delta_t_ = static_cast<float>(vpTime::getMinTimeForUsleepCall() * 1e-3);
327  } else {
328  this->delta_t_ = delta_t;
329  }
330  }
332  void setSingularityManagement (const bool sm) {singularityManagement = sm;}
333 
337  void setVerbose(bool verbose)
338  {
339  this->verbose_ = verbose;
340  }
341 
347  void set_fMo(const vpHomogeneousMatrix &fMo_) {this->fMo = fMo_;}
348 
349  protected:
353  #if defined(_WIN32)
354  static DWORD WINAPI launcher( LPVOID lpParam )
355  {
356  ((vpRobotWireFrameSimulator *)lpParam)->updateArticularPosition();
357  return 0;
358  }
359  #elif defined(VISP_HAVE_PTHREAD)
360  static void* launcher(void *arg)
361  {
362  (reinterpret_cast<vpRobotWireFrameSimulator *>(arg))->updateArticularPosition();
363  // pthread_exit((void*) 0);
364  return NULL;
365  }
366  #endif
367 
368  /* Robot functions */
369  void init() {;}
371  virtual void updateArticularPosition() = 0;
373  virtual int isInJointLimit () = 0;
375  virtual void computeArticularVelocity() = 0;
376 
377  /* Display functions */
378  void initDisplay() {;}
379  virtual void initArms() = 0;
380 
381  #if defined(_WIN32)
382  vpColVector get_artCoord() const {
383  WaitForSingleObject(mutex_artCoord,INFINITE);
384  vpColVector artCoordTmp (6);
385  artCoordTmp = artCoord;
386  ReleaseMutex(mutex_artCoord);
387  return artCoordTmp;}
388  void set_artCoord(const vpColVector &coord) {
389  WaitForSingleObject(mutex_artCoord,INFINITE);
390  artCoord = coord;
391  ReleaseMutex(mutex_artCoord);}
392 
393  vpColVector get_artVel() const {
394  WaitForSingleObject(mutex_artVel,INFINITE);
395  vpColVector artVelTmp (artVel);
396  ReleaseMutex(mutex_artVel);
397  return artVelTmp;}
398  void set_artVel(const vpColVector &vel) {
399  WaitForSingleObject(mutex_artVel,INFINITE);
400  artVel = vel;
401  ReleaseMutex(mutex_artVel);}
402 
403  vpColVector get_velocity() {
404  WaitForSingleObject(mutex_velocity,INFINITE);
405  vpColVector velocityTmp = velocity;
406  ReleaseMutex(mutex_velocity);
407  return velocityTmp;}
408  void set_velocity(const vpColVector &vel) {
409  WaitForSingleObject(mutex_velocity,INFINITE);
410  velocity = vel;
411  ReleaseMutex(mutex_velocity);}
412 
413  void set_displayBusy (const bool &status) {
414  WaitForSingleObject(mutex_display,INFINITE);
415  displayBusy = status;
416  ReleaseMutex(mutex_display);}
417  bool get_displayBusy () {
418  WaitForSingleObject(mutex_display,INFINITE);
419  bool status = displayBusy;
420  if (!displayBusy) displayBusy = true;
421  ReleaseMutex(mutex_display);
422  return status;}
423 
424  #elif defined(VISP_HAVE_PTHREAD)
426  pthread_mutex_lock (&mutex_artCoord);
427  vpColVector artCoordTmp (6);
428  artCoordTmp = artCoord;
429  pthread_mutex_unlock (&mutex_artCoord);
430  return artCoordTmp;}
431  void set_artCoord(const vpColVector &coord) {
432  pthread_mutex_lock (&mutex_artCoord);
433  artCoord = coord;
434  pthread_mutex_unlock (&mutex_artCoord);}
435 
437  pthread_mutex_lock (&mutex_artVel);
438  vpColVector artVelTmp (artVel);
439  pthread_mutex_unlock (&mutex_artVel);
440  return artVelTmp;}
441  void set_artVel(const vpColVector &vel) {
442  pthread_mutex_lock (&mutex_artVel);
443  artVel = vel;
444  pthread_mutex_unlock (&mutex_artVel);}
445 
447  pthread_mutex_lock (&mutex_velocity);
448  vpColVector velocityTmp = velocity;
449  pthread_mutex_unlock (&mutex_velocity);
450  return velocityTmp;}
451  void set_velocity(const vpColVector &vel) {
452  pthread_mutex_lock (&mutex_velocity);
453  velocity = vel;
454  pthread_mutex_unlock (&mutex_velocity);}
455 
456  void set_displayBusy (const bool &status) {
457  pthread_mutex_lock (&mutex_display);
458  displayBusy = status;
459  pthread_mutex_unlock (&mutex_display);}
460  bool get_displayBusy () {
461  pthread_mutex_lock (&mutex_display);
462  bool status = displayBusy;
463  if (!displayBusy) displayBusy = true;
464  pthread_mutex_unlock (&mutex_display);
465  return status;}
466  #endif
467 
469  virtual void get_fMi(vpHomogeneousMatrix *fMit) = 0;
470 };
471 
472 #endif
473 #endif
void setDisplayRobotType(const vpDisplayRobotType dispType)
void setSingularityManagement(const bool sm)
unsigned int getWidth() const
Definition: vpImage.h:161
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void * launcher(void *arg)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
void set_fMo(const vpHomogeneousMatrix &fMo_)
bool constantSamplingTimeMode
Flag used to force the sampling time in the thread computing the robot's displacement to a constant v...
Define the X11 console to display images.
Definition: vpDisplayX.h:148
void setGraphicsThickness(unsigned int thickness)
void setExternalCameraPosition(const vpHomogeneousMatrix &cam_Mf)
void setDesiredCameraPosition(const vpHomogeneousMatrix &cdMo_)
vpHomogeneousMatrix fMo
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:141
vpHomogeneousMatrix get_fMo() const
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:105
This class aims to be a basis used to create all the robot simulators.
void setExternalCameraPosition(const vpHomogeneousMatrix camMf_)
void set_displayBusy(const bool &status)
The vpDisplayOpenCV allows to display image using the opencv library.
void setSamplingTime(const double &delta_t)
Generic class defining intrinsic camera parameters.
void setCameraColor(const vpColor col)
vpHomogeneousMatrix getExternalCameraPosition() const
void initScene(const vpSceneObject &obj, const vpSceneDesiredObject &desiredObject)
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:152
vpCameraParameters getExternalCameraParameters() const
void set_velocity(const vpColVector &vel)
Implementation of a wire frame simulator. Compared to the vpSimulator class, it does not require thir...
void setCurrentViewColor(const vpColor col)
This class aims to be a basis used to create all the simulators of robots.
bool setVelocityCalled
Flag used to specify to the thread managing the robot displacements that the setVelocity() method has...
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void setConstantSamplingTimeMode(const bool _constantSamplingTimeMode)
vpHomogeneousMatrix get_cMo() const
void setDesiredViewColor(const vpColor col)
void setDesiredCameraPosition(const vpHomogeneousMatrix cdMo_)
unsigned int getHeight() const
Definition: vpImage.h:152
void set_artCoord(const vpColVector &coord)
VISP_EXPORT double getMinTimeForUsleepCall()
Definition: vpTime.cpp:82
void set_artVel(const vpColVector &vel)
vpHomogeneousMatrix getExternalCameraPosition() const