Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

ArJoyHandler_WIN.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "ArExport.h"
00027 #include "ariaOSDef.h"
00028 #include "ArJoyHandler.h"
00029 
00030 AREXPORT bool ArJoyHandler::init(void)
00031 {
00032 
00033   myPhysMax = 1;
00034   myLastZ = 0;
00035 
00036   // first see if we can talk to the first joystick
00037   if (joyGetDevCaps(JOYSTICKID1,&myJoyCaps,sizeof(myJoyCaps)) == 
00038       JOYERR_NOERROR &&
00039       joyGetPos(JOYSTICKID1,&myJoyInfo) != JOYERR_UNPLUGGED) 
00040   {
00041     myJoyID = JOYSTICKID1;
00042 
00043     // So far, it seems that the x range is the same as that of y and
00044     // z, so only one is used
00045     myPhysMax  = myJoyCaps.wXmax - myJoyCaps.wXmin;
00046 
00047     myInitialized = true;
00048     startCal();
00049     endCal();
00050     return true;
00051   } 
00052   // we couldn't talk to the first one so try the second one
00053   else if (joyGetDevCaps(JOYSTICKID2,&myJoyCaps,sizeof(myJoyCaps)) == 
00054       JOYERR_NOERROR &&
00055       joyGetPos(JOYSTICKID2,&myJoyInfo) != JOYERR_UNPLUGGED) 
00056   {
00057     myJoyID = JOYSTICKID2;
00058 
00059     // So far, it seems that the x range is the same as that of y and
00060     // z, so only one is used
00061     myPhysMax = myJoyCaps.wXmax - myJoyCaps.wXmin;
00062 
00063     myInitialized = true;
00064     startCal();
00065     endCal();
00066     return true;
00067   } 
00068   // we couldn't talk to either one
00069   else
00070   {
00071     myInitialized = false;
00072     return false;
00073   }
00074 
00075   // Just to prevent any possible divide-by-zeros...
00076   if (myPhysMax == 0) {
00077     myPhysMax = 1;
00078   }
00079 
00080   getData();
00081 }
00082 
00083 void ArJoyHandler::getData(void)
00084 {
00085   int x, y, z;
00086   if (!myFirstData && myLastDataGathered.mSecSince() < 5)
00087     return;
00088 
00089   myFirstData = false;
00090   myLastDataGathered.setToNow();
00091   MMRESULT joyResult = joyGetPos(myJoyID,&myJoyInfo);
00092 
00093   if (joyResult == JOYERR_NOERROR) 
00094   {
00095     // KMC: I don't understand this logic... The spec says that 
00096     // getAxis returns a number between -1 and 1; the getAxis method
00097     // multiplies the contents of myAxes by 128.  The logic below
00098     // however seems to double everything... 
00099 
00100     x = (int)(myJoyInfo.wXpos*256.0/myPhysMax)-128;
00101     y = (int)-((myJoyInfo.wYpos*256.0/myPhysMax)-128);
00102     z = (int)-((myJoyInfo.wZpos*256.0/myPhysMax)-128);
00103 
00104 /***/
00105 /***
00106     x = (int) 128 * ((2.0 * (double) myJoyInfo.wXpos / (double) myPhysMax) - 1);
00107     y = (int)-128 * ((2.0 * (double) myJoyInfo.wYpos / (double) myPhysMax) - 1);
00108     z = (int)-128 * ((2.0 * (double) myJoyInfo.wZpos / (double) myPhysMax) - 1);
00109 **/
00110 
00111     if (myLastZ != z)
00112       myHaveZ = true;
00113     if (x > myMaxX)
00114       myMaxX = x;
00115     if (x < myMinX)
00116       myMinX = x;
00117     if (y > myMaxY)
00118       myMaxY = y;
00119     if (y < myMinY)
00120       myMinY = y;
00121 
00122     myAxes[1] = x;
00123     myAxes[2] = y;
00124     myAxes[3] = z;
00125     
00126     myLastZ = z;
00127     
00128     myButtons[1] = (myJoyInfo.wButtons & 1);
00129     myButtons[2] = (myJoyInfo.wButtons & 2);
00130     myButtons[3] = (myJoyInfo.wButtons & 4);
00131     myButtons[4] = (myJoyInfo.wButtons & 8);
00132   }
00133   else //(joyResult == JOYERR_UNPLUGGED) 
00134   {
00135     myAxes[1] = 0;
00136     myAxes[2] = 0;
00137     myAxes[3] = 0;
00138     myButtons[1] = 0;
00139     myButtons[2] = 0;
00140     myButtons[3] = 0;
00141     myButtons[4] = 0;
00142     
00143     // Reset the initialized flag so that the joystick button in the GUI
00144     // will be disabled.
00145     myInitialized = false;
00146   } 
00147 }
00148 
00149 

Generated on Tue Feb 20 10:51:40 2007 for Aria by  doxygen 1.4.0