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

actionGroupExample.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 "Aria.h"
00027 
00048 ArActionGroup *teleop;
00049 ArActionGroup *wander;
00050 
00051 // Activate the teleop action group. activateExlcusive() causes
00052 // all other active action groups to be deactivated.
00053 void teleopMode(void)
00054 {
00055   teleop->activateExclusive();
00056   printf("\n== Teleoperation Mode ==\n");
00057   printf("   Use the arrow keys to drive, and the spacebar to stop.\n    For joystick control hold the trigger button.\n    Press 'w' to switch to wander mode.\n    Press escape to exit.\n");
00058 }
00059 
00060 // Activate the wander action group. activateExlcusive() causes
00061 // all other active action groups to be deactivated.
00062 void wanderMode(void)
00063 {
00064   wander->activateExclusive();
00065   printf("\n== Wander Mode ==\n");
00066   printf("    The robot will now just wander around avoiding things.\n    Press 't' to switch to  teleop mode.\n    Press escape to exit.\n");
00067 }
00068 
00069 
00070 int main(int argc, char** argv)
00071 {
00072   Aria::init();
00073   ArArgumentParser argParser(&argc, argv);
00074   ArSimpleConnector con(&argParser);
00075   ArRobot robot;
00076   ArSonarDevice sonar;
00077 
00078   argParser.loadDefaultArguments();
00079   if(!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
00080   {
00081     Aria::logOptions();
00082     return 1;
00083   }
00084 
00085   /* - the action group for teleoperation actions: */
00086   teleop = new ArActionGroup(&robot);
00087 
00088   // don't hit any tables (if the robot has IR table sensors)
00089   teleop->addAction(new ArActionLimiterTableSensor, 100);
00090 
00091   // limiter for close obstacles
00092   teleop->addAction(new ArActionLimiterForwards("speed limiter near", 
00093                         300, 600, 250), 95);
00094 
00095   // limiter for far away obstacles
00096   teleop->addAction(new ArActionLimiterForwards("speed limiter far", 
00097                            300, 1100, 400), 90);
00098 
00099   // limiter so we don't bump things backwards
00100   teleop->addAction(new ArActionLimiterBackwards, 85);
00101 
00102   // the joydrive action (drive from joystick)
00103   ArActionJoydrive joydriveAct("joydrive", 400, 15);
00104   teleop->addAction(&joydriveAct, 50);
00105 
00106   // the keydrive action (drive from keyboard)
00107   teleop->addAction(new ArActionKeydrive, 45);
00108   
00109 
00110 
00111   /* - the action group for wander actions: */
00112   wander = new ArActionGroup(&robot);
00113 
00114   // if we're stalled we want to back up and recover
00115   wander->addAction(new ArActionStallRecover, 100);
00116 
00117   // react to bumpers
00118   wander->addAction(new ArActionBumpers, 75);
00119 
00120   // turn to avoid things closer to us
00121   wander->addAction(new ArActionAvoidFront("Avoid Front Near", 225, 0), 50);
00122 
00123   // turn avoid things further away
00124   wander->addAction(new ArActionAvoidFront, 45);
00125 
00126   // keep moving
00127   wander->addAction(new ArActionConstantVelocity("Constant Velocity", 400), 25);
00128 
00129   
00130 
00131   /* - use key commands to switch modes, and use keyboard
00132    *   and joystick as inputs for teleoperation actions. */
00133 
00134   // create key handler if Aria does not already have one
00135   ArKeyHandler *keyHandler = Aria::getKeyHandler();
00136   if (keyHandler == NULL)
00137   {
00138     keyHandler = new ArKeyHandler;
00139     Aria::setKeyHandler(keyHandler);
00140     robot.attachKeyHandler(keyHandler);
00141   }
00142 
00143   // set the callbacks
00144   ArGlobalFunctor teleopCB(&teleopMode);
00145   ArGlobalFunctor wanderCB(&wanderMode);
00146   keyHandler->addKeyHandler('w', &wanderCB);
00147   keyHandler->addKeyHandler('W', &wanderCB);
00148   keyHandler->addKeyHandler('t', &teleopCB);
00149   keyHandler->addKeyHandler('T', &teleopCB);
00150 
00151   // if we don't have a joystick, let 'em know
00152   if (!joydriveAct.joystickInited())
00153     printf("Note: Do not have a joystick, only the arrow keys on the keyboard will work.\n");
00154   
00155   // set the joystick so it won't do anything if the button isn't pressed
00156   joydriveAct.setStopIfNoButtonPressed(false);
00157 
00158 
00159   /* - connect to the robot, then enter teleoperation mode.  */
00160 
00161   robot.addRangeDevice(&sonar);
00162   if(!con.connectRobot(&robot))
00163   { 
00164     ArLog::log(ArLog::Terse, "actionGroupExample: Could not connect to the robot.");
00165     Aria::shutdown();
00166     return 1;
00167   }
00168 
00169   robot.enableMotors();
00170   teleopMode();
00171   robot.run(true);
00172 
00173   Aria::shutdown();
00174   return 0;
00175 }

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