ViSP  2.6.2
vpRobotSimulator.h
1 /****************************************************************************
2  *
3  * $Id: vpRobotSimulator.h 3530 2012-01-03 10:52:12Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Basic class used to make robot simulators.
36  *
37  * Authors:
38  * Nicolas Melchior
39  *
40  *****************************************************************************/
41 
42 #ifndef vpRobotSimulator_HH
43 #define vpRobotSimulator_HH
44 
50 #include <visp/vpConfig.h>
51 
52 
53 
54 #if defined(WIN32) || defined(VISP_HAVE_PTHREAD)
55 #include <cmath> // std::fabs
56 #include <limits> // numeric_limits
57 #if defined(WIN32)
58 # include <windows.h>
59 #elif defined(VISP_HAVE_PTHREAD)
60 # include <pthread.h>
61 #endif
62 
63 #include <visp/vpWireFrameSimulator.h>
64 #include <visp/vpRobot.h>
65 #include <visp/vpDisplayOpenCV.h>
66 #include <visp/vpDisplayX.h>
67 #include <visp/vpDisplayGTK.h>
68 #include <visp/vpDisplayGDI.h>
69 #include <visp/vpDisplayD3D.h>
70 
88 class VISP_EXPORT vpRobotSimulator : protected vpWireFrameSimulator, public vpRobot
89 {
90  public:
92 
93  typedef enum
94  {
96  MODEL_DH
97  } vpDisplayRobotType;
98 
99 
100  protected:
104  double tcur;
106  double tprev;
107 
109  Bound_scene* robotArms;
110 
112  unsigned int size_fMi;
117 
124 
125  #if defined(WIN32)
126  HANDLE hThread;
127  HANDLE mutex_fMi;
128  HANDLE mutex_artVel;
132  #elif defined(VISP_HAVE_PTHREAD)
133  pthread_t thread;
134  pthread_attr_t attr;
135  pthread_mutex_t mutex_fMi;
136  pthread_mutex_t mutex_artVel;
137  pthread_mutex_t mutex_artCoord;
138  pthread_mutex_t mutex_velocity;
139  pthread_mutex_t mutex_display;
140  #endif
141 
143 
145  bool robotStop;
149  unsigned int jointLimitArt;
152 
155 
156  #if defined VISP_HAVE_X11
158  #elif defined VISP_HAVE_GDI
159  vpDisplayGDI display;
160  #elif defined VISP_HAVE_OPENCV
161  vpDisplayOpenCV display;
162  #elif defined VISP_HAVE_D3D9
163  vpDisplayD3D display;
164  #elif defined VISP_HAVE_GTK
165  vpDisplayGTK display;
166  #endif
167 
169 
173 
176 
177  public:
179  vpRobotSimulator(bool display);
180  virtual ~vpRobotSimulator();
181 
182  /* Robot functions */
184  virtual void setVelocity (const vpRobot::vpControlFrameType /*frame*/, const vpColVector & /*velocity*/) = 0;
186  virtual void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &q) = 0;
187 
188  /* vpRobot virtual methods*/
190  virtual void get_eJe(vpMatrix & _eJe) = 0;
192  virtual void get_fJe(vpMatrix &_fJe) = 0;
194  virtual void getCameraDisplacement(vpColVector &v) = 0;
196  virtual void getArticularDisplacement(vpColVector &qdot) = 0;
198  virtual void getDisplacement(const vpRobot::vpControlFrameType frame,vpColVector &q) = 0;
200  void setSingularityManagement (const bool sm) {singularityManagement = sm;}
201 
202 
203  /* Display functions */
204  void initScene(vpSceneObject obj, vpSceneDesiredObject desiredObject);
205  void initScene(const char* obj, const char* desiredObject);
206  void initScene (vpSceneObject obj);
207  void initScene(const char* obj);
208 
215  {
217  }
218 
225  {
227  }
228 
235  {
237  }
238 
244  void setCameraColor(const vpColor col) {camColor = col;}
245 
251  void setCurrentViewColor(const vpColor col) {curColor = col;}
252 
258  void setDesiredViewColor(const vpColor col) {desColor = col;}
259 
266  //if(px_ext != 1 && py_ext != 1)
267  // we assume px_ext and py_ext > 0
268  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
269  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
270  return vpCameraParameters(px_ext,py_ext,I.getWidth()/2,I.getHeight()/2);
271  else
272  {
273  unsigned int size = vpMath::minimum(I.getWidth(),I.getHeight())/2;
274  return vpCameraParameters(size,size,I.getWidth()/2,I.getHeight()/2);
275  }
276  }
277 
283  void set_fMo(const vpHomogeneousMatrix &fMo) {this->fMo = fMo;}
284 
290  vpHomogeneousMatrix get_fMo() const {return fMo;}
291 
297  inline void setDisplayRobotType (const vpDisplayRobotType dispType) {displayType = dispType;}
298 
300 
301  void getInternalView(vpImage<vpRGBa> &I);
302  void getInternalView(vpImage<unsigned char> &I);
303 
304 
313  inline void setConstantSamplingTimeMode(const bool _constantSamplingTimeMode){
314  constantSamplingTimeMode = _constantSamplingTimeMode;
315  }
316 
325  inline void setSamplingTime(const float _samplingTime){
326  if(_samplingTime < static_cast<float>(vpTime::minTimeForUsleepCall)){
327  samplingTime = static_cast<float>(vpTime::minTimeForUsleepCall);
328  }else{
329  samplingTime = _samplingTime;
330  }
331  }
332 
333 
334  protected:
338  #if defined(WIN32)
339  static DWORD WINAPI launcher( LPVOID lpParam )
340  {
341  ((vpRobotSimulator *)lpParam)->updateArticularPosition();
342  return 0;
343  }
344  #elif defined(VISP_HAVE_PTHREAD)
345  static void* launcher(void *arg)
346  {
347  (reinterpret_cast<vpRobotSimulator *>(arg))->updateArticularPosition();
348  // pthread_exit((void*) 0);
349  return NULL;
350  }
351  #endif
352 
353  /* Robot functions */
354  void init() {;}
356  virtual void updateArticularPosition() = 0;
358  virtual int isInJointLimit () = 0;
360  virtual void computeArticularVelocity() = 0;
361 
362  /* Display functions */
363  void initDisplay() {;}
364  virtual void initArms() = 0;
365 
366  #if defined(WIN32)
368  WaitForSingleObject(mutex_artCoord,INFINITE);
369  vpColVector artCoordTmp (6);
370  artCoordTmp = artCoord;
371  ReleaseMutex(mutex_artCoord);
372  return artCoordTmp;}
373  void set_artCoord(const vpColVector &coord) {
374  WaitForSingleObject(mutex_artCoord,INFINITE);
375  artCoord = coord;
376  ReleaseMutex(mutex_artCoord);}
377 
379  WaitForSingleObject(mutex_artVel,INFINITE);
380  vpColVector artVelTmp (artVel);
381  ReleaseMutex(mutex_artVel);
382  return artVelTmp;}
383  void set_artVel(const vpColVector &vel) {
384  WaitForSingleObject(mutex_artVel,INFINITE);
385  artVel = vel;
386  ReleaseMutex(mutex_artVel);}
387 
389  WaitForSingleObject(mutex_velocity,INFINITE);
390  vpColVector velocityTmp = velocity;
391  ReleaseMutex(mutex_velocity);
392  return velocityTmp;}
393  void set_velocity(const vpColVector &vel) {
394  WaitForSingleObject(mutex_velocity,INFINITE);
395  velocity = vel;
396  ReleaseMutex(mutex_velocity);}
397 
398  void set_displayBusy (const bool &status) {
399  WaitForSingleObject(mutex_display,INFINITE);
400  displayBusy = status;
401  ReleaseMutex(mutex_display);}
402  bool get_displayBusy () {
403  WaitForSingleObject(mutex_display,INFINITE);
404  bool status = displayBusy;
405  if (!displayBusy) displayBusy = true;
406  ReleaseMutex(mutex_display);
407  return status;}
408 
409  #elif defined(VISP_HAVE_PTHREAD)
410  vpColVector get_artCoord() {
411  pthread_mutex_lock (&mutex_artCoord);
412  vpColVector artCoordTmp (6);
413  artCoordTmp = artCoord;
414  pthread_mutex_unlock (&mutex_artCoord);
415  return artCoordTmp;}
416  void set_artCoord(const vpColVector &coord) {
417  pthread_mutex_lock (&mutex_artCoord);
418  artCoord = coord;
419  pthread_mutex_unlock (&mutex_artCoord);}
420 
421  vpColVector get_artVel() {
422  pthread_mutex_lock (&mutex_artVel);
423  vpColVector artVelTmp (artVel);
424  pthread_mutex_unlock (&mutex_artVel);
425  return artVelTmp;}
426  void set_artVel(const vpColVector &vel) {
427  pthread_mutex_lock (&mutex_artVel);
428  artVel = vel;
429  pthread_mutex_unlock (&mutex_artVel);}
430 
431  vpColVector get_velocity() {
432  pthread_mutex_lock (&mutex_velocity);
433  vpColVector velocityTmp = velocity;
434  pthread_mutex_unlock (&mutex_velocity);
435  return velocityTmp;}
436  void set_velocity(const vpColVector &vel) {
437  pthread_mutex_lock (&mutex_velocity);
438  velocity = vel;
439  pthread_mutex_unlock (&mutex_velocity);}
440 
441  void set_displayBusy (const bool &status) {
442  pthread_mutex_lock (&mutex_display);
443  displayBusy = status;
444  pthread_mutex_unlock (&mutex_display);}
445  bool get_displayBusy () {
446  pthread_mutex_lock (&mutex_display);
447  bool status = displayBusy;
448  if (!displayBusy) displayBusy = true;
449  pthread_mutex_unlock (&mutex_display);
450  return status;}
451  #endif
452 
454  virtual void get_fMi(vpHomogeneousMatrix *fMit) = 0;
455 };
456 
457 #endif
458 
459 #endif
unsigned int jointLimitArt
vpCameraParameters getExternalCameraParameters() const
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
vpHomogeneousMatrix * fMi
virtual void getDisplacement(const vpRobot::vpControlFrameType frame, vpColVector &q)=0
Get a displacement (frame as to ve specified) between two successive position control.
vpColVector get_velocity()
unsigned int getWidth() const
Definition: vpImage.h:154
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void setSamplingTime(const float _samplingTime)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
Class to define colors available for display functionnalities.
Definition: vpColor.h:123
void set_artVel(const vpColVector &vel)
void setCurrentViewColor(const vpColor col)
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void setCameraColor(const vpColor col)
virtual void get_eJe(vpMatrix &_eJe)=0
Get the robot Jacobian expressed in the end-effector frame.
vpHomogeneousMatrix getExternalCameraPosition() const
void setConstantSamplingTimeMode(const bool _constantSamplingTimeMode)
class that defines a generic virtual robot
Definition: vpRobot.h:60
vpControlFrameType
Definition: vpRobot.h:83
vpHomogeneousMatrix fMo
void set_artCoord(const vpColVector &coord)
static DWORD WINAPI launcher(LPVOID lpParam)
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:137
vpColVector get_artCoord()
vpHomogeneousMatrix get_fMo() const
virtual void getArticularDisplacement(vpColVector &qdot)=0
Get a displacement expressed in the joint space between two successive position control.
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:108
void setDesiredViewColor(const vpColor col)
void setExternalCameraPosition(const vpHomogeneousMatrix camMf)
virtual void getCameraDisplacement(vpColVector &v)=0
Get a displacement expressed in the camera frame between two successive position control.
This class aims to be a basis used to create all the simulators of robots.
void setDesiredCameraPosition(const vpHomogeneousMatrix cdMo)
bool constantSamplingTimeMode
Flag used to force the sampling time in the thread computing the robot's displacement to a constant v...
unsigned int size_fMi
void setSingularityManagement(const bool sm)
Bound_scene * robotArms
The vpDisplayOpenCV allows to display image using the opencv library.
void set_velocity(const vpColVector &vel)
Generic class defining intrinsic camera parameters.
void set_fMo(const vpHomogeneousMatrix &fMo)
virtual void get_fJe(vpMatrix &_fJe)=0
Get the robot Jacobian expressed in the robot reference (or world) frame.
void setExternalCameraPosition(const vpHomogeneousMatrix camMf)
vpImage< vpRGBa > I
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void setDesiredCameraPosition(const vpHomogeneousMatrix cdMo)
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:148
Implementation of a wire frame simulator. Compared to the vpSimulator class, it does not require thir...
vpColVector velocity
vpColVector artCoord
bool setVelocityCalled
Flag used to specify to the thread managing the robot displacements that the setVelocity() method has...
vpColVector get_artVel()
vpCameraParameters cameraParam
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
vpHomogeneousMatrix get_cMo() const
void initScene(vpSceneObject obj, vpSceneDesiredObject desiredObject)
void setDisplayRobotType(const vpDisplayRobotType dispType)
unsigned int getHeight() const
Definition: vpImage.h:145
vpDisplayRobotType displayType
virtual void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)=0
Set the velocity (frame has to be specified) that will be applied to the velocity controller...
static double minTimeForUsleepCall
Definition: vpTime.h:81
vpHomogeneousMatrix getExternalCameraPosition() const
void set_displayBusy(const bool &status)