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

ArActionJoydrive.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 "ArActionJoydrive.h"
00029 #include "ArRobot.h"
00030 #include "ariaInternal.h"
00031 
00062 AREXPORT ArActionJoydrive::ArActionJoydrive(const char *name, 
00063                         double transVelMax, 
00064                         double turnAmountMax,
00065                         bool stopIfNoButtonPressed,
00066                         bool useOSCalForJoystick) :
00067   ArAction(name, "This action reads the joystick and sets the translational and rotational velocities based on this.")
00068 {
00069   if ((myJoyHandler = Aria::getJoyHandler()) == NULL)
00070   {
00071     myJoyHandler = new ArJoyHandler;
00072     myJoyHandler->init();
00073     Aria::setJoyHandler(myJoyHandler);
00074   }
00075 
00076   setSpeeds(transVelMax, turnAmountMax);
00077 
00078   setNextArgument(ArArg("trans vel max", &myTransVelMax, "The full speed to go when the joystick is maxed forwards/backwards (mm/sec)"));
00079   
00080   setNextArgument(ArArg("turn amount max", &myTurnAmountMax, "The full amount to turn if the joystick is pushed all the way right/left (deg)"));
00081 
00082   setNextArgument(ArArg("stop if no button pressed", &myStopIfNoButtonPressed, "If this is true, then joydrive will stop the robot if there is no button pressed, otherwise it will just let whatever lower priority things go."));
00083   myStopIfNoButtonPressed = stopIfNoButtonPressed;
00084 
00085   setNextArgument(ArArg("use os calibration for joystick", &myUseOSCal, "If this is true then the os calibration for the joystick will be used, otherwise autocalibration will be used."));
00086   myUseOSCal = useOSCalForJoystick;
00087   myPreviousUseOSCal = myUseOSCal;
00088 
00089   myUseThrottle = false;
00090 }
00091 
00092 AREXPORT ArActionJoydrive::~ArActionJoydrive()
00093 {
00094 
00095 }
00096 
00097 AREXPORT void ArActionJoydrive::setStopIfNoButtonPressed(
00098     bool stopIfNoButtonPressed)
00099 {
00100   myStopIfNoButtonPressed = stopIfNoButtonPressed;
00101 }
00102 
00103 AREXPORT bool ArActionJoydrive::getStopIfNoButtonPressed(void)
00104 {
00105   return myStopIfNoButtonPressed;
00106 }
00107 
00108 AREXPORT bool ArActionJoydrive::joystickInited(void)
00109 {
00110   return myJoyHandler->haveJoystick();
00111 }
00112 
00116 AREXPORT void ArActionJoydrive::setUseOSCal(bool useOSCal)
00117 {
00118   myUseOSCal = useOSCal;
00119   myPreviousUseOSCal = useOSCal;
00120   myJoyHandler->setUseOSCal(useOSCal);
00121 }
00122 
00126 AREXPORT bool ArActionJoydrive::getUseOSCal(void)
00127 {
00128   return myUseOSCal;
00129 }
00130 
00131 AREXPORT void ArActionJoydrive::setSpeeds(double transVelMax, 
00132                       double turnAmountMax)
00133 {
00134   myTransVelMax = transVelMax;
00135   myTurnAmountMax = turnAmountMax;
00136 }
00137 
00138 AREXPORT void ArActionJoydrive::setThrottleParams(double lowSpeed, double highSpeed)
00139 {
00140   myUseThrottle = true;
00141   myLowThrottle = lowSpeed;
00142   myHighThrottle = highSpeed;
00143 }
00144 
00145 AREXPORT ArActionDesired *ArActionJoydrive::fire(ArActionDesired currentDesired)
00146 {
00147   double rot, trans, throttle;
00148 
00149   myDesired.reset();
00150   if (myPreviousUseOSCal != myUseOSCal)
00151   {
00152     myJoyHandler->setUseOSCal(myUseOSCal);
00153     myPreviousUseOSCal = myUseOSCal;
00154   }
00155 
00156   if (myJoyHandler->haveJoystick() && myJoyHandler->getButton(1))
00157   {
00158     // get the readings from the joystick
00159     myJoyHandler->getDoubles(&rot, &trans);
00160     rot *= myTurnAmountMax;
00161     // if we're not using the throttle just mult simply, or if we
00162     // don't know if we have a throttle
00163     if (!myUseThrottle || !myJoyHandler->haveZAxis()) 
00164     {
00165       trans *= myTransVelMax;
00166     }
00167     // if we are using the throttle, interpolate its position between
00168     // low and high throttle values
00169     else
00170     {
00171       throttle = myJoyHandler->getAxis(3);
00172       throttle += 1.0;
00173       throttle /= 2.0;
00174       trans = trans * (myLowThrottle + 
00175                (throttle * (myHighThrottle - myLowThrottle)));
00176     }
00177     // set what we want to do
00178     myDesired.setVel(trans);
00179     myDesired.setDeltaHeading(-rot);
00180     // return the actionDesired
00181     return &myDesired;
00182   }
00183   else if (myJoyHandler->haveJoystick() && myStopIfNoButtonPressed)
00184   {
00185     myDesired.setVel(0);
00186     myDesired.setDeltaHeading(0);
00187     return &myDesired;
00188   }
00189   
00190   return NULL;
00191 }

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