iCub-main
joystickCheck.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 RobotCub Consortium, European Commission FP6 Project IST-004370
3  * Author: Marco Randazzo
4  * email: marco.randazzo@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17 */
18 
59 #include <iostream>
60 #include <SDL.h>
61 #include <yarp/os/Time.h>
62 #include <yarp/os/Log.h>
63 #include <math.h>
64 
65 int main( int argc, char **argv )
66 {
67  // start SDL subsystem
68  SDL_JoystickEventState ( SDL_QUERY );
69  if ( SDL_InitSubSystem ( SDL_INIT_JOYSTICK) < 0 )
70  {
71  yError ("JoystickCheck: Unable to initialize joystick system: %s\n", SDL_GetError() );
72  return 0;
73  }
74 
75  // get the list of available joysticks
76  fprintf ( stderr, "\n");
77  int joystick_num = SDL_NumJoysticks ();
78 
79  // if not joysticks are found, quit immediately
80  if (joystick_num <= 0)
81  {
82  yError ( "JoystickCheck: no joysticks found.\n");
83  return 0;
84  }
85 
86  // open the first joystick
87  fprintf ( stderr, "JoystickCheck: (%d) joystick(s) found.\n",joystick_num);
88  int joy_id = 0;
89  SDL_Joystick* joy1 = SDL_JoystickOpen ( joy_id );
90  if ( joy1 == NULL )
91  {
92  yError ( "Could not open default joystick.\n" );
93  return 0;
94  }
95  int numAxes = SDL_JoystickNumAxes ( joy1 );
96  int numBalls = SDL_JoystickNumBalls ( joy1 );
97  int numHats = SDL_JoystickNumHats ( joy1 );
98  int numButtons = SDL_JoystickNumButtons ( joy1 );
99  bool first =true;
100  bool active=false;
101  double* firstAxes = new double [numAxes];
102  double* firstBalls = new double [numBalls];
103  double* firstHats = new double [numHats];
104  double* firstButtons = new double [numButtons];
105  double* valAxes = new double [numAxes];
106  double* valBalls = new double [numBalls];
107  double* valHats = new double [numHats];
108  double* valButtons = new double [numButtons];
109 
110  //check for user activity on the opened joystick
111  for (int trial=0; trial < 1000; trial++ )
112  {
113  yarp::os::Time::delay(0.010);
114  SDL_JoystickUpdate();
115 
116  if (first==true)
117  {
118  for (int i=0; i < numAxes; i++)
119  firstAxes[i] = (double)SDL_JoystickGetAxis(joy1, i);
120  //for (int i=0; i < numBalls; i++)
121  // firstBalls[i] = (double)SDL_JoystickGetBall(joy1, i, dx, dy);
122  for (int i=0; i < numHats; i++)
123  firstHats[i] = (double)SDL_JoystickGetHat(joy1, i);
124  for (int i=0; i < numButtons; i++)
125  firstButtons[i] = (double)SDL_JoystickGetButton(joy1, i);
126  }
127  else
128  {
129  for (int i=0; i < numAxes; i++)
130  {
131  valAxes[i] = (double)SDL_JoystickGetAxis(joy1, i);
132  if (fabs(valAxes[i]-firstAxes[i])>25000)
133  {
134  active=true;
135  break;
136  }
137  }
138  //for (int i=0; i < numBalls; i++)
139  // valAxes[i] = (double)SDL_JoystickGetBall(joy1, i, dx, dy);
140  for (int i=0; i < numHats; i++)
141  {
142  valHats[i] = (double)SDL_JoystickGetHat(joy1, i);
143  if (fabs(valHats[i]-firstHats[i])>100)
144  {
145  active=true;
146  break;
147  }
148  }
149  for (int i=0; i < numButtons; i++)
150  {
151  valButtons[i] = (double)SDL_JoystickGetButton(joy1, i);
152  if (fabs(valButtons[i]-firstButtons[i])>0.5)
153  {
154  active=true;
155  break;
156  }
157  }
158  }
159  first = false;
160  if (active==true)
161  {
162  yInfo ( "joysticks activity detected.\n" );
163  return 101;
164  }
165  }
166 
167  yInfo ( "no joysticks are currently used.\n" );
168  return 0;
169 }
int main(int argc, char **argv)
fprintf(fid,'\n')