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

sonyPTZDemo.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 
00038 /*
00039   This class is the core of this demo, it adds itself to the robot given
00040   as a user task, then drives the robot and camera from the joystick
00041 */
00042 class Joydrive
00043 {
00044 public:
00045   // constructor
00046   Joydrive(ArRobot *robot, int LRAmount = 15, int UDAmount = 10, 
00047        int zoomAmount = 50);
00048   // destructor, its just empty
00049   ~Joydrive(void) {}
00050 
00051   // the callback function
00052   void drive(void);
00053 
00054   // callbacks for key presses
00055   void up(void);
00056   void down(void);
00057   void left(void);
00058   void right(void);
00059   void in(void);
00060   void out(void);
00061   void center(void);
00062 protected:
00063   // the rotational max for the robot
00064   int myRotValRobot;
00065   // the translational max for the robot
00066   int myTransValRobot;
00067   // the pan max for the camera
00068   int myPanValCamera;
00069   // the tilt max for the camera
00070   int myTiltValCamera;
00071   // the zoom max for the camera
00072   int myZoomValCamera;
00073   
00074   // joystick handler
00075   ArJoyHandler myJoyHandler;
00076 
00077   // the camera
00078   ArSonyPTZ myCam;
00079   // the robot pointer
00080   ArRobot *myRobot;
00081   // callback for the drive function
00082   ArFunctorC<Joydrive> myDriveCB;
00083   ArFunctorC<Joydrive> myUpCB;
00084   ArFunctorC<Joydrive> myDownCB;
00085   ArFunctorC<Joydrive> myLeftCB;
00086   ArFunctorC<Joydrive> myRightCB;
00087   ArFunctorC<Joydrive> myInCB;
00088   ArFunctorC<Joydrive> myOutCB;
00089   ArFunctorC<Joydrive> myCenterCB;
00090   int myLRAmount;
00091   int myUDAmount;
00092   int myZoomAmount;
00093 };
00094 
00095 /*
00096   Constructor, sets the robot pointer, and some initial values, also note the
00097   use of constructor chaining on myCam and myDriveCB.
00098 */
00099 Joydrive::Joydrive(ArRobot *robot, int LRAmount, int UDAmount, int zoomAmount) :
00100   myCam(robot),
00101   myDriveCB(this, &Joydrive::drive),
00102   myUpCB(this, &Joydrive::up),
00103   myDownCB(this, &Joydrive::down),
00104   myLeftCB(this, &Joydrive::left),
00105   myRightCB(this, &Joydrive::right),
00106   myInCB(this, &Joydrive::in),
00107   myOutCB(this, &Joydrive::out),
00108   myCenterCB(this, &Joydrive::center)
00109     
00110 {
00111   // set the robot pointer and add the joydrive as a user task
00112   myRobot = robot;
00113   myRobot->addUserTask("joydrive", 50, &myDriveCB);
00114   
00115 
00116 
00117 
00118   // initalize some variables
00119   myRotValRobot = 100;
00120   myTransValRobot = 700;
00121   myPanValCamera = 8;
00122   myTiltValCamera = 3;
00123   myZoomValCamera = 50;
00124   myLRAmount = LRAmount;
00125   myUDAmount = UDAmount;
00126   myZoomAmount = zoomAmount;
00127 
00128   ArKeyHandler *keyHandler;
00129   myRobot = robot;
00130   // see if there is already a keyhandler, if not make one for ourselves
00131   if ((keyHandler = Aria::getKeyHandler()) == NULL)
00132   {
00133     keyHandler = new ArKeyHandler;
00134     Aria::setKeyHandler(keyHandler);
00135     myRobot->attachKeyHandler(keyHandler);
00136   }
00137   // now that we have one, add our keys as callbacks, print out big
00138   // warning messages if they fail
00139   if (!keyHandler->addKeyHandler(ArKeyHandler::UP, &myUpCB))
00140     ArLog::log(ArLog::Terse, "The key handler already has a key for up, keydrive will not work correctly.");
00141   if (!keyHandler->addKeyHandler(ArKeyHandler::DOWN, &myDownCB))
00142     ArLog::log(ArLog::Terse, "The key handler already has a key for down, keydrive will not work correctly.");
00143   if (!keyHandler->addKeyHandler(ArKeyHandler::LEFT, &myLeftCB))
00144     ArLog::log(ArLog::Terse,  
00145 "The key handler already has a key for left, keydrive will not work correctly.");
00146   if (!keyHandler->addKeyHandler(ArKeyHandler::RIGHT, &myRightCB))
00147     ArLog::log(ArLog::Terse,  
00148 "The key handler already has a key for right, keydrive will not work correctly.");
00149   if (!keyHandler->addKeyHandler('z', &myInCB))
00150     ArLog::log(ArLog::Terse,  
00151 "The key handler already has a key for 'z', keydrive will not work correctly.");
00152   if (!keyHandler->addKeyHandler('Z', &myInCB))
00153     ArLog::log(ArLog::Terse,  
00154 "The key handler already has a key for 'Z', keydrive will not work correctly.");
00155   if (!keyHandler->addKeyHandler('x', &myOutCB))
00156     ArLog::log(ArLog::Terse,  
00157 "The key handler already has a key for 'x', keydrive will not work correctly.");
00158   if (!keyHandler->addKeyHandler('X', &myOutCB))
00159     ArLog::log(ArLog::Terse,  
00160 "The key handler already has a key for 'X', keydrive will not work correctly.");
00161   if (!keyHandler->addKeyHandler('c', &myCenterCB))
00162     ArLog::log(ArLog::Terse,  
00163 "The key handler already has a key for 'c', keydrive will not work correctly.");
00164   if (!keyHandler->addKeyHandler('C', &myCenterCB))
00165     ArLog::log(ArLog::Terse,  
00166 "The key handler already has a key for 'C', keydrive will not work correctly.");
00167 
00168 
00169   // initialize the joystick
00170   myJoyHandler.init();
00171   // see if we have a joystick, and let the usre know the results
00172   if (myJoyHandler.haveJoystick())
00173   {
00174     printf("Have a joystick\n\n");
00175   }
00176   // we don't have a joystic, so get out
00177   else
00178   {
00179     printf("Do not have a joystick, only the keyboard will work.\n");
00180   }
00181 }
00182 
00183 void Joydrive::left(void)
00184 {
00185   myCam.panTiltRel(-myLRAmount, 0);
00186 }
00187 
00188 void Joydrive::right(void)
00189 {
00190   myCam.panTiltRel(myLRAmount, 0);
00191 }
00192 
00193 void Joydrive::up(void)
00194 {
00195   myCam.panTiltRel(0, myUDAmount);
00196 }
00197 
00198 void Joydrive::down(void)
00199 {
00200   myCam.panTiltRel(0, -myUDAmount);
00201 }
00202 
00203 void Joydrive::in(void)
00204 {
00205   myCam.zoomRel(myZoomAmount);
00206 }
00207 
00208 void Joydrive::out(void)
00209 {
00210   myCam.zoomRel(-myZoomAmount);
00211 }
00212 
00213 void Joydrive::center(void)
00214 {
00215   myCam.panTilt(0,0);
00216   myCam.zoom(0);
00217 }
00218 
00219 void Joydrive::drive(void)
00220 {
00221   int trans, rot;
00222   int pan, tilt;
00223   int zoom, nothing;
00224 
00225   // if both buttons are pushed, zoom the joystick
00226   if (myJoyHandler.haveJoystick() && myJoyHandler.getButton(1) && 
00227       myJoyHandler.getButton(2))
00228   {
00229     // set its speed so we get desired value range, we only care about y
00230     myJoyHandler.setSpeeds(0, myZoomValCamera);
00231     // get the values
00232     myJoyHandler.getAdjusted(&nothing, &zoom);
00233     // zoom the camera
00234     myCam.zoomRel(zoom);
00235   }
00236   // if both buttons aren't pushed, see if one button is pushed
00237   else 
00238   {
00239     // if button one is pushed, drive the robot
00240     if (myJoyHandler.haveJoystick() && myJoyHandler.getButton(1))
00241     {
00242       // set the speed on the joystick so we get the values we want
00243       myJoyHandler.setSpeeds(myRotValRobot, myTransValRobot);
00244       // get the values
00245       myJoyHandler.getAdjusted(&rot, &trans);
00246       // set the robots speed
00247       myRobot->setVel(trans);
00248       myRobot->setRotVel(-rot);
00249     }
00250     // if button one isn't pushed, stop the robot
00251     else
00252     {
00253       myRobot->setVel(0);
00254       myRobot->setRotVel(0);
00255     }
00256     // if button two is pushed, move the camera
00257     if (myJoyHandler.haveJoystick() && myJoyHandler.getButton(2))
00258     {
00259       // set the speeds on the joystick so we get desired value range
00260       myJoyHandler.setSpeeds(myPanValCamera, myTiltValCamera);
00261       // get the values
00262       myJoyHandler.getAdjusted(&pan, &tilt);
00263       // drive the camera
00264       myCam.panTiltRel(pan, tilt);
00265     } 
00266   }
00267 
00268 }
00269 
00270 int main(int argc, char **argv) 
00271 {
00272   // just some stuff for returns
00273   std::string str;
00274   int ret;
00275   
00276   // robots connection
00277   ArSerialConnection con;
00278   // the robot, this turns state reflection off
00279   ArRobot robot(NULL, false);
00280   // the joydrive as defined above, this also adds itself as a user task
00281   Joydrive joyd(&robot);
00282 
00283   // mandatory init
00284   Aria::init();
00285 
00286   // open the connection, if it fails, exit
00287   if ((ret = con.open()) != 0)
00288   {
00289     str = con.getOpenMessage(ret);
00290     printf("Open failed: %s\n", str.c_str());
00291     Aria::shutdown();
00292     return 1;
00293   }
00294 
00295   // set the connection o nthe robot
00296   robot.setDeviceConnection(&con);
00297   // connect, if we fail, exit
00298   if (!robot.blockingConnect())
00299   {
00300     printf("Could not connect to robot... exiting\n");
00301     Aria::shutdown();
00302     return 1;
00303   }
00304 
00305   // turn off the sonar, enable the motors, turn off amigobot sounds
00306   robot.comInt(ArCommands::SONAR, 0);
00307   robot.comInt(ArCommands::ENABLE, 1);
00308   robot.comInt(ArCommands::SOUNDTOG, 0);
00309 
00310   // run, if we lose connection to the robot, exit
00311   robot.run(true);
00312   
00313   // shutdown and go away
00314   Aria::shutdown();
00315   return 0;
00316 }
00317 

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