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

ArJoyHandler.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 #include "ariaUtil.h"
00030 
00039 AREXPORT ArJoyHandler::ArJoyHandler(bool useOSCal, bool useOld)
00040 {
00041   myInitialized = false;
00042   myUseOSCal = useOSCal;
00043   myUseOld = useOld;
00044   myHaveZ = false;
00045   myFirstData = true;
00046 }
00047 
00048 AREXPORT ArJoyHandler::~ArJoyHandler()
00049 {
00050 }
00051 
00052 
00059 AREXPORT void ArJoyHandler::setUseOSCal(bool useOSCal)
00060 {
00061   myUseOSCal = useOSCal;
00062 }
00063 
00070 AREXPORT bool ArJoyHandler::getUseOSCal(void)
00071 {
00072   return myUseOSCal;
00073 }
00074 
00079 AREXPORT void ArJoyHandler::startCal(void)
00080 {
00081   int x, y;
00082   getUnfiltered(&x, &y);
00083   myMaxX = x;
00084   myMinX = x;
00085   myMaxY = y;
00086   myMinY = y;
00087   myCenX = x;
00088   myCenY = y;
00089 }
00090 
00098 AREXPORT void ArJoyHandler::endCal(void)
00099 {
00100   int x, y;
00101   
00102   getUnfiltered(&x, &y);
00103   myCenX = x;
00104   myCenY = y;
00105 }
00106 
00107 AREXPORT void ArJoyHandler::getStats(int *maxX, int *minX, int *maxY, 
00108                      int *minY, int *cenX, int *cenY)
00109 {
00110   *maxX = myMaxX;
00111   *minX = myMinX;
00112   *maxY = myMaxY;
00113   *minY = myMinY;
00114   *cenX = myCenX;
00115   *cenY = myCenY;
00116 }
00117 
00118 AREXPORT void ArJoyHandler::setStats(int maxX, int minX, int maxY, int minY, 
00119                int cenX, int cenY)
00120 {
00121   myMaxX = maxX;
00122   myMinX = minX;
00123   myMaxY = maxY;
00124   myMinY = minY;
00125   myCenX = cenX;
00126   myCenY = cenY;
00127 }
00128 
00129 AREXPORT void ArJoyHandler::getSpeeds(int *x, int *y, int *z)
00130 {
00131   *x = myTopX;
00132   *y = myTopY;
00133   if (z != NULL)
00134     *z = myTopZ;
00135 }
00136 
00152 AREXPORT void ArJoyHandler::getAdjusted(int *x, int *y, int *z)
00153 {
00154   int curX, curY, curZ;
00155 
00156   getUnfiltered(&curX, &curY, &curZ);
00157   if (myUseOSCal)
00158   {
00159     *x = ArMath::roundInt(((double)curX) / 128.0 * ((double)myTopX));
00160     *y = ArMath::roundInt(((double)curY) / 128.0 * ((double)myTopY));
00161     if (z != NULL)
00162       *z = ArMath::roundInt(((double)curZ) / 128.0 * ((double)myTopZ));
00163     return;
00164   }
00165 
00166   if (curX > myCenX && myMaxX - myCenX != 0 ) {
00167     *x = (int)((double)(curX - myCenX)/(double)(myMaxX - myCenX)*
00168            (double)myTopX);
00169   } else if (curX <= myCenX && myCenX - myMinX != 0) {
00170     *x = (int)((double)(myCenX - curX)/(double)(myCenX - myMinX)*
00171            (double)-myTopX);
00172   } else
00173     *x = 0;
00174   if (curY > myCenY && myMaxY - myCenY != 0) {
00175     *y = (int)((double)(curY - myCenY)/(double)(myMaxY - myCenY)*
00176            (double)myTopY);
00177   } else if (curY <= myCenY && myCenY - myMinY != 0) {
00178     *y = (int)((double)(myCenY - curY)/(double)(myCenY - myMinY)*
00179            (double)-myTopY);
00180   } else 
00181     *y = 0;
00182   if (z != NULL)
00183     *z = ArMath::roundInt(((double)curZ) / 128.0 * ((double)myTopZ));
00184   
00185 }
00186 
00202 AREXPORT void ArJoyHandler::getDoubles(double *x, double *y, double *z)
00203 {
00204   int curX, curY, curZ;
00205 
00206   getUnfiltered(&curX, &curY, &curZ);
00207   if (myUseOSCal)
00208   {
00209     *x = ((double)curX) / 128.0;
00210     *y = ((double)curY) / 128.0;
00211     if (z != NULL)
00212       *z = ((double)curZ) / 128.0;
00213     return;
00214   }
00215 
00216   if (curX > myCenX && myMaxX - myCenX != 0 ) {
00217     *x = (int)((double)(curX - myCenX)/(double)(myMaxX - myCenX));
00218   } else if (curX <= myCenX && myCenX - myMinX != 0) {
00219     *x = (int)((double)(myCenX - curX)/(double)(myCenX - myMinX));
00220   } else
00221     *x = 0;
00222   if (curY > myCenY && myMaxY - myCenY != 0) {
00223     *y = (int)((double)(curY - myCenY)/(double)(myMaxY - myCenY));
00224   } else if (curY <= myCenY && myCenY - myMinY != 0) {
00225     *y = (int)((double)(myCenY - curY)/(double)(myCenY - myMinY));
00226   } else 
00227     *y = 0;
00228   if (z != NULL)
00229     *z = curZ;
00230 }
00231 
00245 AREXPORT void ArJoyHandler::getUnfiltered(int *x, int* y, int *z)
00246 {
00247   getData();
00248   *x = myAxes[1];
00249   *y = myAxes[2];
00250   if (z != NULL)
00251     *z = myAxes[3];
00252 }
00253 
00258 AREXPORT double ArJoyHandler::getAxis(unsigned int axis)
00259 {
00260   // make sure we have that axis
00261   if (axis < 1 || axis > myAxes.size())
00262     return 0;
00263 
00264   std::map<unsigned int, int>::iterator iter = myAxes.find(axis);
00265   if (iter != myAxes.end()) {
00266     return (iter->second) / 128.0;
00267   }
00268   else {
00269     return 0;
00270   }
00277 }
00278 
00286 AREXPORT bool ArJoyHandler::getButton(unsigned int button)
00287 {
00288   getData();
00289   // make sure we have that axis
00290   if (button < 1 || button > myButtons.size())
00291     return 0;
00292 
00293   // now make sure its in there
00294   if (myButtons.find(button) == myButtons.end())
00295     return 0;
00296   
00297   return myButtons[button];
00298 }
00299 
00304 AREXPORT unsigned int ArJoyHandler::getNumAxes(void)
00305 {
00306   return myAxes.size();
00307 }
00308 
00313 AREXPORT unsigned int ArJoyHandler::getNumButtons(void)
00314 {
00315   return myButtons.size();
00316 }
00317 

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