Main MRPT website > C++ reference
MRPT logo

CJoystick.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  CJOYSTICK_H
00029 #define  CJOYSTICK_H
00030 
00031 #include <mrpt/config.h>
00032 #include <mrpt/utils/utils_defs.h>
00033 #include <mrpt/hwdrivers/link_pragmas.h>
00034 
00035 /*---------------------------------------------------------------
00036         Class
00037   ---------------------------------------------------------------*/
00038 namespace mrpt
00039 {
00040         namespace hwdrivers
00041         {
00042                 /** Access to joysticks and gamepads (read buttons and position), and request number of joysticks in the system.
00043                   *  Since MRPT 0.6.0 this class works in Windows and Linux.
00044                   */
00045                 class HWDRIVERS_IMPEXP CJoystick
00046                 {
00047                 private:
00048                         /** The axis limits:
00049                          */
00050                         int     m_x_min,m_x_max,m_y_min,m_y_max,m_z_min,m_z_max;
00051 
00052 
00053         #if defined(MRPT_OS_LINUX)
00054                         int                     m_joy_fd;               //!< File FD for the joystick, or -1 if not open (Linux only)
00055                         int                     m_joy_index;    //!< The index of the joystick open in m_joy_fd (Linux only)
00056                         /** Using an event system we only have deltas, need to keep the whole joystick state (Linux only) */
00057                         vector_bool     m_joystate_btns;
00058                         /** Using an event system we only have deltas, need to keep the whole joystick state (Linux only) */
00059                         vector_int      m_joystate_axes;
00060         #endif
00061 
00062                 public:
00063                         /** Constructor
00064                          */
00065                         CJoystick();
00066 
00067                         /** Destructor
00068                          */
00069                         virtual ~CJoystick();
00070 
00071                         /** Returns the number of Joysticks in the computer.
00072                          */
00073                         static int  getJoysticksCount();
00074 
00075                         /** Gets joystick information.
00076                          *
00077                          *   This method will try first to open the joystick, so you can safely call it while the joystick is plugged and removed arbitrarly.
00078                          *
00079                          * \param nJoy The index of the joystick to query: The first one is 0, the second 1, etc... See CJoystick::getJoysticksCount to discover the number of joysticks in the system.
00080                          * \param x The x axis position, range [-1,1]
00081                          * \param y The y axis position, range [-1,1]
00082                          * \param z The z axis position, range [-1,1]
00083                          * \param buttons Each element will hold true if buttons are pressed. The size of the vector will be set automatically to the number of buttons.
00084                          * \param raw_x_pos If it is desired the raw integer measurement from JoyStick, set this pointer to a desired placeholder.
00085                          * \param raw_y_pos If it is desired the raw integer measurement from JoyStick, set this pointer to a desired placeholder.
00086                          * \param raw_z_pos If it is desired the raw integer measurement from JoyStick, set this pointer to a desired placeholder.
00087                          *
00088                          * \return Returns true if successfull, false on error, for example, if joystick is not present.
00089                          *
00090                          * \sa setLimits
00091                          */
00092                         bool  getJoystickPosition(
00093                                 int     nJoy,
00094                                 float   &x,
00095                                 float   &y,
00096                                 float   &z,
00097                                 std::vector<bool>       &buttons,
00098                                 int *raw_x_pos=NULL,
00099                                 int     *raw_y_pos=NULL,
00100                                 int *raw_z_pos=NULL );
00101 
00102                         /** Set the axis limit values, for computing a [-1,1] position index easily (Only required to calibrate analog joystick).
00103                          *   It seems that these values must been calibrated for each joystick model.
00104                          *
00105                          * \sa getJoystickPosition
00106                          */
00107         #ifdef MRPT_OS_WINDOWS
00108                         void  setLimits( int x_min = 0,int x_max = 0xFFFF, int y_min=0,int y_max = 0xFFFF,int z_min=0,int z_max = 0xFFFF );
00109         #else
00110                         void  setLimits( int x_min = -32767,int x_max = 32767, int y_min=-32767,int y_max = 32767,int z_min=-32767,int z_max = 32767);
00111         #endif
00112                 }; // End of class def.
00113 
00114         } // End of namespace
00115 } // End of namespace
00116 
00117 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011