gripperExample.cpp

Example program demonstrating use of the Pioneer gripper.

Shows how to control the Pioneer 2DOF gripper accessory. In addition to the arrow keys to teleoperate the robot, Use the following keyboard keys to control it: u Lift the gripper up d Lift the gripper down o Open the gripper c Close the gripper s Stop gripper movement.

See also:
ArModeGripper (the gripper control mode used in the "demo" program)
00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004,2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006 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 
00043 #include "Aria.h"
00044 
00045 // Adds robot callback to print gripper status.
00046 class PrintGripStatus
00047 {
00048   ArGripper* myGripper;
00049   ArFunctorC<PrintGripStatus> myPrintCB;
00050 public:
00051   PrintGripStatus(ArGripper* gripper) : 
00052     myGripper(gripper),
00053     myPrintCB(this, &PrintGripStatus::printStatus)
00054   {
00055   }
00056 
00057   void addRobotTask(ArRobot* robot)
00058   {
00059     robot->addUserTask("PrintGripStatus", 10, &myPrintCB);
00060   }
00061 
00062   void printStatus()
00063   {
00064     myGripper->logState();
00065   }
00066 };
00067 
00068 // Adds key handler callbacks for controlling the gripper
00069 class GripperControlHandler
00070 {
00071   ArGripper* myGripper;
00072   ArFunctorC<GripperControlHandler> myUpCB;
00073   ArFunctorC<GripperControlHandler> myDownCB;
00074   ArFunctorC<GripperControlHandler> myOpenCB;
00075   ArFunctorC<GripperControlHandler> myCloseCB;
00076   ArFunctorC<GripperControlHandler> myStopCB;
00077 public:
00078   GripperControlHandler(ArGripper* gripper) : 
00079     myGripper(gripper),
00080     myUpCB(this, &GripperControlHandler::liftUp),
00081     myDownCB(this, &GripperControlHandler::liftDown),
00082     myOpenCB(this, &GripperControlHandler::open),
00083     myCloseCB(this, &GripperControlHandler::close),
00084     myStopCB(this, &GripperControlHandler::stop)
00085   {
00086   }
00087 
00088   void addKeyHandlers(ArRobot *robot)
00089   {
00090     ArKeyHandler *keyHandler = Aria::getKeyHandler();
00091     if(keyHandler == NULL)
00092     {
00093       keyHandler = new ArKeyHandler();
00094       Aria::setKeyHandler(keyHandler);
00095       robot->attachKeyHandler(keyHandler);
00096     }
00097     keyHandler->addKeyHandler(ArKeyHandler::PAGEUP, &myUpCB);
00098     keyHandler->addKeyHandler('u', &myUpCB);
00099     keyHandler->addKeyHandler(ArKeyHandler::PAGEDOWN, &myDownCB);
00100     keyHandler->addKeyHandler('d', &myDownCB);
00101     keyHandler->addKeyHandler('o', &myOpenCB);
00102     keyHandler->addKeyHandler('c', &myCloseCB);
00103     keyHandler->addKeyHandler('s', &myStopCB);
00104   }
00105 
00106   void liftUp()
00107   {
00108     ArLog::log(ArLog::Normal, "Moving gripper lift up...");
00109     myGripper->liftUp();
00110   }
00111 
00112   void liftDown()
00113   {
00114     ArLog::log(ArLog::Normal, "Moving gripper lift down...");
00115     myGripper->liftDown();
00116   }
00117 
00118   void stop()
00119   {
00120     ArLog::log(ArLog::Normal, "Stopping gripper...");
00121     myGripper->gripperHalt(); // stops both lift an grip
00122     //myGripper->liftStop(); // stops just the lift
00123     //myGripper->gripStop(); // stops just the gripper
00124   }
00125 
00126   void close()
00127   {
00128     ArLog::log(ArLog::Normal, "Closing gripper...");
00129     myGripper->gripClose();
00130   }
00131 
00132   void open()
00133   {
00134     ArLog::log(ArLog::Normal, "Opening gripper...");
00135     myGripper->gripOpen();
00136   }
00137 
00138 };
00139 
00140 int main(int argc, char **argv) 
00141 {
00142 
00143   Aria::init();
00144   ArRobot robot;
00145   ArArgumentParser argParser(&argc, argv);
00146   ArSimpleConnector connector(&argParser);
00147   ArGripper gripper(&robot);
00148   ArSonarDevice sonar;
00149   robot.addRangeDevice(&sonar);
00150 
00151   argParser.loadDefaultArguments();
00152 
00153   if (!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
00154   {
00155     Aria::logOptions();
00156     Aria::shutdown();
00157     return 1;
00158   }
00159   
00160   if (!connector.connectRobot(&robot))
00161   {
00162     ArLog::log(ArLog::Terse, "gripperExample: Could not connect to robot... exiting");
00163     Aria::shutdown();
00164     return 2;
00165   }
00166   ArLog::log(ArLog::Normal, "gripperExample: Connected to robot.");
00167 
00168   ArLog::log(ArLog::Normal, "gripperExample: GripperType=%d", gripper.getType());
00169   gripper.logState();
00170   if(gripper.getType() == ArGripper::NOGRIPPER)
00171   {
00172     ArLog::log(ArLog::Terse, "gripperExample: Error: Robot does not have a gripper. Exiting.");
00173     Aria::shutdown();
00174     return -1;
00175   }
00176 
00177   // Teleoperation actions with obstacle-collision avoidance
00178   ArActionLimiterTableSensor tableLimit;
00179   robot.addAction(&tableLimit, 110);
00180   ArActionLimiterForwards limitNearAction("near", 300, 600, 250);
00181   robot.addAction(&limitNearAction, 100);
00182   ArActionLimiterForwards limitFarAction("far", 300, 1100, 400);
00183   robot.addAction(&limitFarAction, 90);
00184   ArActionLimiterBackwards limitBackAction;
00185   robot.addAction(&limitBackAction, 50);
00186   ArActionJoydrive joydriveAction("joydrive", 400, 15);
00187   robot.addAction(&joydriveAction, 40);
00188   joydriveAction.setStopIfNoButtonPressed(false);
00189   ArActionKeydrive keydriveAction;
00190   robot.addAction(&keydriveAction, 30);
00191   
00192 
00193   // Handlers to control the gripper and print out info (classes defined above)
00194   PrintGripStatus printStatus(&gripper);
00195   GripperControlHandler gripControl(&gripper);
00196   printStatus.addRobotTask(&robot);
00197   gripControl.addKeyHandlers(&robot);
00198 
00199   // enable motors and run (if we lose connection to the robot, exit)
00200   ArLog::log(ArLog::Normal, "You may now operate the robot with arrow keys or joystick. Operate the gripper with the u, d, o, c, and page up/page down keys.");
00201   robot.enableMotors();
00202   robot.run(true);
00203   
00204   Aria::shutdown();
00205   return 0;
00206 }
00207 
00208 

Generated on Fri Dec 1 10:55:10 2006 for Aria by  doxygen 1.4.7