Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpRobotWireFrameSimulator.h
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  * 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(WINRT_8_0)) || 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 
212  //if(px_ext != 1 && py_ext != 1)
213  // we assume px_ext and py_ext > 0
214  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
215  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
216  return vpCameraParameters(px_ext,py_ext,I.getWidth()/2,I.getHeight()/2);
217  else
218  {
219  unsigned int size = vpMath::minimum(I.getWidth(),I.getHeight())/2;
220  return vpCameraParameters(size,size,I.getWidth()/2,I.getHeight()/2);
221  }
222  }
229  {
231  }
232 
233  void getInternalView(vpImage<vpRGBa> &I);
234  void getInternalView(vpImage<unsigned char> &I);
235 
242  vpHomogeneousMatrix get_fMo() const {return fMo;}
243 
244  /* Display functions */
245  void initScene(const vpSceneObject &obj, const vpSceneDesiredObject &desiredObject);
246  void initScene(const char* obj, const char* desiredObject);
247  void initScene (const vpSceneObject &obj);
248  void initScene(const char* obj);
249 
255  void setCameraColor(const vpColor col) {camColor = col;}
256 
265  inline void setConstantSamplingTimeMode(const bool _constantSamplingTimeMode){
266  constantSamplingTimeMode = _constantSamplingTimeMode;
267  }
268 
274  void setCurrentViewColor(const vpColor col) {curColor = col;}
275 
281  void setDesiredViewColor(const vpColor col) {desColor = col;}
282 
289  {
291  }
292 
298  inline void setDisplayRobotType (const vpDisplayRobotType dispType) {displayType = dispType;}
305  {
307  }
311  void setGraphicsThickness(unsigned int thickness)
312  {
313  this->thickness_ = thickness;
314  }
315 
325  inline void setSamplingTime(const double &delta_t)
326  {
327  if(delta_t < static_cast<float>(vpTime::getMinTimeForUsleepCall() * 1e-3)){
328  this->delta_t_ = static_cast<float>(vpTime::getMinTimeForUsleepCall() * 1e-3);
329  } else {
330  this->delta_t_ = delta_t;
331  }
332  }
334  void setSingularityManagement (const bool sm) {singularityManagement = sm;}
335 
339  void setVerbose(bool verbose)
340  {
341  this->verbose_ = verbose;
342  }
343 
349  void set_fMo(const vpHomogeneousMatrix &fMo_) {this->fMo = fMo_;}
351 
352  protected:
358 #if defined(_WIN32)
359  static DWORD WINAPI launcher( LPVOID lpParam )
360  {
361  ((vpRobotWireFrameSimulator *)lpParam)->updateArticularPosition();
362  return 0;
363  }
364 #elif defined(VISP_HAVE_PTHREAD)
365  static void* launcher(void *arg)
366  {
367  (reinterpret_cast<vpRobotWireFrameSimulator *>(arg))->updateArticularPosition();
368  // pthread_exit((void*) 0);
369  return NULL;
370  }
371 #endif
372 
373  /* Robot functions */
374  void init() {;}
376  virtual void updateArticularPosition() = 0;
378  virtual int isInJointLimit () = 0;
380  virtual void computeArticularVelocity() = 0;
381 
382  /* Display functions */
383  void initDisplay() {;}
384  virtual void initArms() = 0;
385 
386  #if defined(_WIN32)
387  vpColVector get_artCoord() const {
388 # if defined(WINRT_8_1)
389  WaitForSingleObjectEx(mutex_artCoord, INFINITE, FALSE);
390 # else // pure win32
391  WaitForSingleObject(mutex_artCoord,INFINITE);
392 # endif
393  vpColVector artCoordTmp (6);
394  artCoordTmp = artCoord;
395  ReleaseMutex(mutex_artCoord);
396  return artCoordTmp;}
397  void set_artCoord(const vpColVector &coord) {
398 # if defined(WINRT_8_1)
399  WaitForSingleObjectEx(mutex_artCoord, INFINITE, FALSE);
400 # else // pure win32
401  WaitForSingleObject(mutex_artCoord, INFINITE);
402 # endif
403  artCoord = coord;
404  ReleaseMutex(mutex_artCoord);}
405 
406  vpColVector get_artVel() const {
407 # if defined(WINRT_8_1)
408  WaitForSingleObjectEx(mutex_artVel, INFINITE, FALSE);
409 # else // pure win32
410  WaitForSingleObject(mutex_artVel, INFINITE);
411 # endif
412  vpColVector artVelTmp (artVel);
413  ReleaseMutex(mutex_artVel);
414  return artVelTmp;}
415  void set_artVel(const vpColVector &vel) {
416 # if defined(WINRT_8_1)
417  WaitForSingleObjectEx(mutex_artVel, INFINITE, FALSE);
418 # else // pure win32
419  WaitForSingleObject(mutex_artVel, INFINITE);
420 # endif
421  artVel = vel;
422  ReleaseMutex(mutex_artVel);}
423 
424  vpColVector get_velocity() {
425 # if defined(WINRT_8_1)
426  WaitForSingleObjectEx(mutex_velocity, INFINITE, FALSE);
427 # else // pure win32
428  WaitForSingleObject(mutex_velocity, INFINITE);
429 # endif
430  vpColVector velocityTmp = velocity;
431  ReleaseMutex(mutex_velocity);
432  return velocityTmp;}
433  void set_velocity(const vpColVector &vel) {
434 # if defined(WINRT_8_1)
435  WaitForSingleObjectEx(mutex_velocity, INFINITE, FALSE);
436 # else // pure win32
437  WaitForSingleObject(mutex_velocity, INFINITE);
438 # endif
439  velocity = vel;
440  ReleaseMutex(mutex_velocity);}
441 
442  void set_displayBusy (const bool &status) {
443 # if defined(WINRT_8_1)
444  WaitForSingleObjectEx(mutex_display, INFINITE, FALSE);
445 # else // pure win32
446  WaitForSingleObject(mutex_display, INFINITE);
447 # endif
448  displayBusy = status;
449  ReleaseMutex(mutex_display);}
450  bool get_displayBusy () {
451 # if defined(WINRT_8_1)
452  WaitForSingleObjectEx(mutex_display, INFINITE, FALSE);
453 # else // pure win32
454  WaitForSingleObject(mutex_display, INFINITE);
455 # endif
456  bool status = displayBusy;
457  if (!displayBusy) displayBusy = true;
458  ReleaseMutex(mutex_display);
459  return status;}
460 
461  #elif defined(VISP_HAVE_PTHREAD)
463  pthread_mutex_lock (&mutex_artCoord);
464  vpColVector artCoordTmp (6);
465  artCoordTmp = artCoord;
466  pthread_mutex_unlock (&mutex_artCoord);
467  return artCoordTmp;}
468  void set_artCoord(const vpColVector &coord) {
469  pthread_mutex_lock (&mutex_artCoord);
470  artCoord = coord;
471  pthread_mutex_unlock (&mutex_artCoord);}
472 
474  pthread_mutex_lock (&mutex_artVel);
475  vpColVector artVelTmp (artVel);
476  pthread_mutex_unlock (&mutex_artVel);
477  return artVelTmp;}
478  void set_artVel(const vpColVector &vel) {
479  pthread_mutex_lock (&mutex_artVel);
480  artVel = vel;
481  pthread_mutex_unlock (&mutex_artVel);}
482 
484  pthread_mutex_lock (&mutex_velocity);
485  vpColVector velocityTmp = velocity;
486  pthread_mutex_unlock (&mutex_velocity);
487  return velocityTmp;}
488  void set_velocity(const vpColVector &vel) {
489  pthread_mutex_lock (&mutex_velocity);
490  velocity = vel;
491  pthread_mutex_unlock (&mutex_velocity);}
492 
493  void set_displayBusy (const bool &status) {
494  pthread_mutex_lock (&mutex_display);
495  displayBusy = status;
496  pthread_mutex_unlock (&mutex_display);}
497  bool get_displayBusy () {
498  pthread_mutex_lock (&mutex_display);
499  bool status = displayBusy;
500  if (!displayBusy) displayBusy = true;
501  pthread_mutex_unlock (&mutex_display);
502  return status;}
503  #endif
504 
506  virtual void get_fMi(vpHomogeneousMatrix *fMit) = 0;
508 };
509 
510 #endif
511 #endif
void setDisplayRobotType(const vpDisplayRobotType dispType)
void setSingularityManagement(const bool sm)
unsigned int getWidth() const
Definition: vpImage.h:226
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...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
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:140
vpHomogeneousMatrix get_fMo() const
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed...
Definition: vpDisplayD3D.h:107
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. Thus to enable this class OpenC...
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 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:151
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:175
void set_artCoord(const vpColVector &coord)
VISP_EXPORT double getMinTimeForUsleepCall()
Definition: vpTime.cpp:82
void set_artVel(const vpColVector &vel)
vpHomogeneousMatrix getExternalCameraPosition() const