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

joydriveActionExample.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 
00036 // the action which will drive the robot
00037 class JoydriveAction : public ArAction
00038 {
00039 public:
00040   // constructor
00041   JoydriveAction(void);
00042   // empty destructor
00043   virtual ~JoydriveAction(void);
00044   //the fire which will actually tell the resolver what to do
00045   virtual ArActionDesired *fire(ArActionDesired currentDesired);
00046   // whether the joystick is initalized or not
00047   bool joystickInited(void);
00048 protected:
00049   // action desired
00050   ArActionDesired myDesired;
00051   // joystick handler
00052   ArJoyHandler myJoyHandler;
00053 };
00054 
00055 /*
00056   Note the use of constructor chaining with ArAction.
00057 */  
00058 JoydriveAction::JoydriveAction(void) :
00059   ArAction("Joydrive Action", "This action reads the joystick and sets the translational and rotational velocity based on this.")
00060 {
00061   // initialize the joystick
00062   myJoyHandler.init();
00063   // set up the speed parameters on the joystick
00064   myJoyHandler.setSpeeds(50, 700);
00065 }
00066 
00067 JoydriveAction::~JoydriveAction(void)
00068 {
00069 }
00070 
00071 // whether the joystick is there or not
00072 bool JoydriveAction::joystickInited(void)
00073 {
00074   return myJoyHandler.haveJoystick();
00075 }
00076 
00077 // the guts of the thing
00078 ArActionDesired *JoydriveAction::fire(ArActionDesired currentDesired)
00079 {
00080   int rot, trans;
00081 
00082   // print out some info about hte robot
00083   printf("\rx %6.1f  y %6.1f  tth  %6.1f vel %7.1f mpacs %3d", myRobot->getX(),
00084      myRobot->getY(), myRobot->getTh(), myRobot->getVel(), 
00085      myRobot->getMotorPacCount());
00086   fflush(stdout);
00087 
00088   // see if one of the buttons is pushed, if so drive
00089   if (myJoyHandler.haveJoystick() && (myJoyHandler.getButton(1) ||
00090                     myJoyHandler.getButton(2)))
00091   {
00092     // get the readings from the joystick
00093     myJoyHandler.getAdjusted(&rot, &trans);
00094     // set what we want to do
00095     myDesired.setVel(trans);
00096     myDesired.setDeltaHeading(-rot);
00097     // return the actionDesired
00098     return &myDesired;
00099   }
00100   else
00101   {
00102     // set what we want to do
00103     myDesired.setVel(0);
00104     myDesired.setDeltaHeading(0);
00105     // return the actionDesired
00106     return &myDesired;
00107   }
00108 }
00109 
00110 
00111 int main(int argc, char **argv)
00112 {
00113   ArRobot robot;
00114   Aria::init();
00115   ArSimpleConnector connector(&argc, argv);
00116   if (!connector.parseArgs() || argc > 1)
00117   {
00118     connector.logOptions();
00119     return 1;
00120   }
00121 
00122   // Instance of the JoydriveAction class defined above
00123   JoydriveAction jdAct;
00124 
00125   // if the joydrive action couldn't find the joystick, then exit.
00126   if (!jdAct.joystickInited())
00127   {
00128     printf("Do not have a joystick, set up the joystick then rerun the program\n\n");
00129     Aria::shutdown();
00130     return 1;
00131   }
00132   
00133   // Connect to the robot
00134   if (!connector.connectRobot(&robot))
00135   {
00136     printf("Could not connect to robot... exiting\n");
00137     return 2;
00138   }
00139 
00140 
00141   // disable sonar, enable motors, disable amigobot sound
00142   robot.comInt(ArCommands::SONAR, 0);
00143   robot.comInt(ArCommands::ENABLE, 1);
00144   robot.comInt(ArCommands::SOUNDTOG, 0);
00145 
00146   // add the action
00147   robot.addAction(&jdAct, 100);
00148   // run the robot, true so it'll exit if we lose connection
00149   robot.run(true);
00150   
00151   // now shutdown and exit
00152   Aria::shutdown();
00153   return 0;
00154 }
00155 

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