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

ArRatioInputJoydrive.cpp

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

Generated on Wed Oct 19 12:56:36 2005 for Aria by  doxygen 1.4.0