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

ArRatioInputKeydrive.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 "ArRatioInputKeydrive.h"
00030 #include "ArRobot.h"
00031 #include "ariaInternal.h"
00032 #include "ArKeyHandler.h"
00033 
00034 ArRatioInputKeydrive::ArRatioInputKeydrive(ArRobot *robot, 
00035                                                     ArActionRatioInput *input,
00036                                                     int priority,
00037                                                     double velIncrement) :
00038   myUpCB(this, &ArRatioInputKeydrive::up),
00039   myDownCB(this, &ArRatioInputKeydrive::down),
00040   myLeftCB(this, &ArRatioInputKeydrive::left),
00041   myRightCB(this, &ArRatioInputKeydrive::right),
00042   mySpaceCB(this, &ArRatioInputKeydrive::space),
00043   myFireCB(this, &ArRatioInputKeydrive::fireCallback),
00044   myActivateCB(this, &ArRatioInputKeydrive::activate),
00045   myDeactivateCB(this, &ArRatioInputKeydrive::deactivate)
00046 {
00047   myRobot = robot;
00048   myInput = input;
00049   myInput->addFireCallback(priority, &myFireCB);
00050   myInput->addActivateCallback(&myActivateCB);
00051   myInput->addDeactivateCallback(&myDeactivateCB);
00052   myFireCB.setName("Keydrive");
00053   myVelIncrement = velIncrement;
00054   myHaveKeys = false;
00055   myTransRatio = 0;
00056   myRotRatio = 0;
00057   myThrottle = 100;
00058   myPrinting = false;
00059 }
00060 
00061 ArRatioInputKeydrive::~ArRatioInputKeydrive()
00062 {
00063   myInput->remFireCallback(&myFireCB);
00064   myInput->remActivateCallback(&myActivateCB);
00065 }
00066 
00067 
00068 void ArRatioInputKeydrive::takeKeys(void)
00069 {
00070   myHaveKeys = true;
00071   ArKeyHandler *keyHandler;
00072   if ((keyHandler = Aria::getKeyHandler()) == NULL)
00073   {
00074     ArLog::log(ArLog::Terse, 
00075                "ArRatioInputKeydrive::takeKeys: There is no key handler, keydrive will not work.");
00076   }
00077   // now that we have one, add our keys as callbacks, print out big
00078   // warning messages if they fail
00079   if (!keyHandler->addKeyHandler(ArKeyHandler::UP, &myUpCB))
00080     ArLog::log(ArLog::Terse, "The key handler already has a key for up, keydrive will not work correctly.");
00081   if (!keyHandler->addKeyHandler(ArKeyHandler::DOWN, &myDownCB))
00082     ArLog::log(ArLog::Terse, "The key handler already has a key for down, keydrive will not work correctly.");
00083   if (!keyHandler->addKeyHandler(ArKeyHandler::LEFT, &myLeftCB))
00084     ArLog::log(ArLog::Terse,  
00085                "The key handler already has a key for left, keydrive will not work correctly.");
00086   if (!keyHandler->addKeyHandler(ArKeyHandler::RIGHT, &myRightCB))
00087     ArLog::log(ArLog::Terse,  
00088                "The key handler already has a key for right, keydrive will not work correctly.");
00089   if (!keyHandler->addKeyHandler(ArKeyHandler::SPACE, &mySpaceCB))
00090     ArLog::log(ArLog::Terse,  
00091                "The key handler already has a key for space, keydrive will not work correctly.");
00092 }
00093 
00094 void ArRatioInputKeydrive::giveUpKeys(void)
00095 {
00096   ArKeyHandler *keyHandler;
00097   myHaveKeys = false;
00098   if ((keyHandler = Aria::getKeyHandler()) == NULL)
00099   {
00100     ArLog::log(ArLog::Terse, 
00101                "ArRatioInputKeydrive::giveUpKeys: There is no key handler, something is probably horribly wrong .");
00102   }
00103   // now that we have one, add our keys as callbacks, print out big
00104   // warning messages if they fail
00105   if (!keyHandler->remKeyHandler(&myUpCB))
00106     ArLog::log(ArLog::Terse, "ArRatioInputKeydrive: The key handler already didn't have a key for up, something is wrong.");
00107   if (!keyHandler->remKeyHandler(&myDownCB))
00108     ArLog::log(ArLog::Terse, "ArRatioInputKeydrive: The key handler already didn't have a key for down, something is wrong.");
00109   if (!keyHandler->remKeyHandler(&myLeftCB))
00110     ArLog::log(ArLog::Terse,  
00111                "ArRatioInputKeydrive: The key handler already didn't have a key for left, something is wrong.");
00112   if (!keyHandler->remKeyHandler(&myRightCB))
00113     ArLog::log(ArLog::Terse,  
00114                "ArRatioInputKeydrive: The key handler already didn't have a key for right, something is wrong.");
00115   if (!keyHandler->remKeyHandler(&mySpaceCB))
00116     ArLog::log(ArLog::Terse,  
00117                "ArRatioInputKeydrive: The key handler didn't have a key for space, something is wrong.");
00118 }
00119 
00120 
00121 void ArRatioInputKeydrive::up(void)
00122 {
00123   if (myPrinting)
00124     printf("up\n");
00125   myTransRatio += myVelIncrement;
00126   if (myTransRatio > 100)
00127     myTransRatio = 100;
00128 }
00129 
00130 void ArRatioInputKeydrive::down(void)
00131 {
00132   if (myPrinting)
00133     printf("down\n");
00134   myTransRatio -= myVelIncrement;
00135   if (myTransRatio < -100)
00136     myTransRatio = -100;
00137 }
00138 
00139 void ArRatioInputKeydrive::left(void)
00140 {
00141   if (myPrinting)
00142     printf("left\n");
00143   myRotRatio = 100;
00144 }
00145 
00146 void ArRatioInputKeydrive::right(void)
00147 {
00148   if (myPrinting)
00149     printf("right\n");
00150   myRotRatio = -100;
00151 }
00152 
00153 void ArRatioInputKeydrive::space(void)
00154 {
00155   if (myPrinting)
00156     printf("stop\n");
00157   myTransRatio = 0;
00158   myRotRatio = 0;
00159 }
00160 
00161 void ArRatioInputKeydrive::activate(void)
00162 {
00163   // set things so we'll stop
00164   myTransRatio = 0;
00165   myRotRatio = 0;
00166   if (myHaveKeys)
00167     takeKeys();
00168 }
00169 
00170 void ArRatioInputKeydrive::deactivate(void)
00171 {
00172   if (myHaveKeys)
00173     giveUpKeys();
00174 }
00175 
00176 void ArRatioInputKeydrive::fireCallback(void)
00177 {
00178   // set what we want to do
00179   myInput->setTransRatio(myTransRatio);
00180   myInput->setRotRatio(myRotRatio);
00181   myInput->setThrottleRatio(myThrottle);
00182 
00183   // reset us to going straight (if they're holding the key we'll keep turning)
00184   myRotRatio = 0;
00185 
00186   if (myHaveKeys)
00187     return;
00188   ArKeyHandler *keyHandler;
00189    
00190   // see if there is already a keyhandler, if not make one for ourselves
00191   if ((keyHandler = Aria::getKeyHandler()) == NULL)
00192   {
00193     keyHandler = new ArKeyHandler;
00194     Aria::setKeyHandler(keyHandler);
00195     myRobot->attachKeyHandler(keyHandler);
00196   }
00197   takeKeys();
00198 }

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