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

ArRatioInputJoydrive.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 "ArRatioInputJoydrive.h"
00029 #include "ArRobot.h"
00030 #include "ariaInternal.h"
00031 
00054 AREXPORT ArRatioInputJoydrive::ArRatioInputJoydrive(
00055     ArRobot *robot,
00056     ArActionRatioInput *input,
00057     int priority,
00058     bool stopIfNoButtonPressed,
00059     bool useOSCalForJoystick) :
00060   myFireCB(this, &ArRatioInputJoydrive::fireCallback)
00061 {
00062   myRobot = robot;
00063   myInput = input;
00064   myInput->addFireCallback(priority, &myFireCB);
00065   myFireCB.setName("Joydrive");
00066   if ((myJoyHandler = Aria::getJoyHandler()) == NULL)
00067   {
00068     myJoyHandler = new ArJoyHandler;
00069     myJoyHandler->init();
00070     Aria::setJoyHandler(myJoyHandler);
00071   }
00072 
00073   myUseOSCal = useOSCalForJoystick;
00074   myPreviousUseOSCal = myUseOSCal;
00075   myStopIfNoButtonPressed = stopIfNoButtonPressed;
00076   myFiredLast = false;
00077   myPrinting = false;
00078 }
00079 
00080 AREXPORT ArRatioInputJoydrive::~ArRatioInputJoydrive()
00081 {
00082   myInput->remFireCallback(&myFireCB);
00083 }
00084 
00085 AREXPORT void ArRatioInputJoydrive::setStopIfNoButtonPressed(
00086     bool stopIfNoButtonPressed)
00087 {
00088   myStopIfNoButtonPressed = stopIfNoButtonPressed;
00089 }
00090 
00091 AREXPORT bool ArRatioInputJoydrive::getStopIfNoButtonPressed(void)
00092 {
00093   return myStopIfNoButtonPressed;
00094 }
00095 
00096 AREXPORT bool ArRatioInputJoydrive::joystickInited(void)
00097 {
00098   return myJoyHandler->haveJoystick();
00099 }
00100 
00104 AREXPORT void ArRatioInputJoydrive::setUseOSCal(bool useOSCal)
00105 {
00106   myUseOSCal = useOSCal;
00107   myPreviousUseOSCal = useOSCal;
00108   myJoyHandler->setUseOSCal(useOSCal);
00109 }
00110 
00114 AREXPORT bool ArRatioInputJoydrive::getUseOSCal(void)
00115 {
00116   return myUseOSCal;
00117 }
00118 
00119 
00120 void ArRatioInputJoydrive::fireCallback(void)
00121 {
00122   double rot, trans, throttle;
00123 
00124   if (myPreviousUseOSCal != myUseOSCal)
00125   {
00126     myJoyHandler->setUseOSCal(myUseOSCal);
00127     myPreviousUseOSCal = myUseOSCal;
00128   }
00129 
00130   if (myJoyHandler->haveJoystick() && myJoyHandler->getButton(1))
00131   {
00132     // get the readings from the joystick
00133     myJoyHandler->getDoubles(&rot, &trans);
00134     
00135     if (!myJoyHandler->haveZAxis()) 
00136     {
00137       throttle = 1;
00138     }
00139     // if we are using the throttle, interpolate its position between
00140     // low and high throttle values
00141     else
00142     {
00143       throttle = myJoyHandler->getAxis(3);
00144       throttle += 1.0;
00145       throttle /= 2.0;
00146     }
00147     myInput->setRatios(trans * 100, -rot * 100, throttle * 100);
00148     myFiredLast = true;
00149     if (myPrinting)
00150       printf("joy %g %g %g\n", trans * 100, -rot * 100, throttle * 100);
00151   }
00152   else if (myJoyHandler->haveJoystick() && (myStopIfNoButtonPressed ||
00153                         myFiredLast))
00154   {
00155     if (myPrinting)
00156       printf("joy nothing\n");
00157     myFiredLast = false;
00158     myInput->setRatios(0, 0, myInput->getThrottleRatio());
00159   }
00160   
00161 }

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