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

joydriveUserTask.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 
00028 /*
00029   This program just drives the robot around with a joystick.
00030 
00031   This example uses a user task which has a joystick handler to drive the
00032   robot.
00033 */
00034 
00035 // the clas which'll have the task to drive the robot
00036 class Joydrive
00037 {
00038 public:
00039   // the constructor
00040   Joydrive(ArRobot *robot);
00041   // the destructor
00042   ~Joydrive(void);
00043 
00044   // the function which'll drive the robot
00045   void drive(void);
00046 
00047 protected:
00048   // the joystick handler
00049   ArJoyHandler myJoyHandler;
00050   // robot pointer
00051   ArRobot *myRobot;
00052   // the callback
00053   ArFunctorC<Joydrive> myDriveCB;
00054 };
00055 
00056 /*
00057   the constructor, note the use of constructor chaining
00058 */
00059 Joydrive::Joydrive(ArRobot *robot) :
00060   myDriveCB(this, &Joydrive::drive)
00061 {
00062   // set the robot, and add joydrive as a task
00063   myRobot = robot;
00064   if (myRobot != NULL)
00065     myRobot->addUserTask("joydrive", 50, &myDriveCB);
00066 
00067   // initialize the joystick handler
00068   myJoyHandler.init();
00069   // set the values we'll get back out of the joystick handler
00070   myJoyHandler.setSpeeds(100, 700);
00071 
00072   // see if we have the joystick, and let the user know
00073   if (myJoyHandler.haveJoystick())
00074   {
00075     printf("Have a joystick\n\n");
00076   }
00077   // we don't have a joystick, exit
00078   else
00079   {
00080     printf("Do not have a joystick, set up the joystick then rerun the program\n\n");
00081     Aria::shutdown();
00082     exit(1);
00083   }
00084 }
00085 
00086 Joydrive::~Joydrive(void)
00087 {
00088   // remove the user task from the robot
00089   if (myRobot != NULL)
00090     myRobot->remUserTask(&myDriveCB);
00091 }
00092 
00093 void Joydrive::drive(void)
00094 {
00095   int trans, rot;
00096 
00097   // print out some data about the robot
00098   printf("\rx %6.1f  y %6.1f  tth  %6.1f vel %7.1f mpacs %3d   ", 
00099      myRobot->getX(), myRobot->getY(), myRobot->getTh(), 
00100      myRobot->getVel(), myRobot->getMotorPacCount());
00101   fflush(stdout);
00102 
00103   // see if a joystick butotn is pushed, if so drive
00104   if (myJoyHandler.haveJoystick() && (myJoyHandler.getButton(1) ||
00105                     myJoyHandler.getButton(2)))
00106   {
00107     // get the values out of the joystick handler
00108     myJoyHandler.getAdjusted(&rot, &trans);
00109     // drive the robot
00110     myRobot->setVel(trans);
00111     myRobot->setRotVel(-rot);
00112   }
00113   // if a button isn't pushed, stop the robot
00114   else
00115   {
00116     myRobot->setVel(0);
00117     myRobot->setRotVel(0);
00118   }
00119 }
00120 
00121 int main(int argc, char **argv) 
00122 {
00123   std::string str;
00124   int ret;
00125   
00126   // connection
00127   ArTcpConnection con;
00128   // robot, this starts it with state reflecting turned off
00129   ArRobot robot(NULL, false);
00130   // make the joydrive instance, which adds its task to the robot
00131   Joydrive joyd(&robot);
00132 
00133   // mandatory aria initialization
00134   Aria::init();
00135   
00136   // open the connection, if it fails, exit
00137   if ((ret = con.open()) != 0)
00138   {
00139     str = con.getOpenMessage(ret);
00140     printf("Open failed: %s\n", str.c_str());
00141     Aria::shutdown();
00142     return 1;
00143   }
00144 
00145   // set the robots connection
00146   robot.setDeviceConnection(&con);
00147   // try to connect, if we fail exit
00148   if (!robot.blockingConnect())
00149   {
00150     printf("Could not connect to robot... exiting\n");
00151     Aria::shutdown();
00152     return 1;
00153   }
00154 
00155   // turn off sonar, turn the motors on, turn off amigobot sound
00156   robot.comInt(ArCommands::SONAR, 0);
00157   robot.comInt(ArCommands::ENABLE, 1);
00158   robot.comInt(ArCommands::SOUNDTOG, 0);
00159 
00160   // run the robot, true so that if connection is lost the run stops
00161   robot.run(true);
00162   // now exit
00163   Aria::shutdown();
00164   return 0;
00165 }
00166 

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