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 
00018 #include "Aria.h"
00019 
00020 // Adds robot callback to print gripper status.
00021 class PrintGripStatus
00022 {
00023   ArGripper* myGripper;
00024   ArFunctorC<PrintGripStatus> myPrintCB;
00025 public:
00026   PrintGripStatus(ArGripper* gripper) : 
00027     myGripper(gripper),
00028     myPrintCB(this, &PrintGripStatus::printStatus)
00029   {
00030   }
00031 
00032   void addRobotTask(ArRobot* robot)
00033   {
00034     robot->addUserTask("PrintGripStatus", 10, &myPrintCB);
00035   }
00036 
00037   void printStatus()
00038   {
00039     myGripper->logState();
00040   }
00041 };
00042 
00043 // Adds key handler callbacks for controlling the gripper
00044 class GripperControlHandler
00045 {
00046   ArGripper* myGripper;
00047   ArFunctorC<GripperControlHandler> myUpCB;
00048   ArFunctorC<GripperControlHandler> myDownCB;
00049   ArFunctorC<GripperControlHandler> myOpenCB;
00050   ArFunctorC<GripperControlHandler> myCloseCB;
00051   ArFunctorC<GripperControlHandler> myStopCB;
00052 public:
00053   GripperControlHandler(ArGripper* gripper) : 
00054     myGripper(gripper),
00055     myUpCB(this, &GripperControlHandler::liftUp),
00056     myDownCB(this, &GripperControlHandler::liftDown),
00057     myOpenCB(this, &GripperControlHandler::open),
00058     myCloseCB(this, &GripperControlHandler::close),
00059     myStopCB(this, &GripperControlHandler::stop)
00060   {
00061   }
00062 
00063   void addKeyHandlers(ArRobot *robot)
00064   {
00065     ArKeyHandler *keyHandler = Aria::getKeyHandler();
00066     if(keyHandler == NULL)
00067     {
00068       keyHandler = new ArKeyHandler();
00069       Aria::setKeyHandler(keyHandler);
00070       robot->attachKeyHandler(keyHandler);
00071     }
00072     keyHandler->addKeyHandler(ArKeyHandler::PAGEUP, &myUpCB);
00073     keyHandler->addKeyHandler('u', &myUpCB);
00074     keyHandler->addKeyHandler(ArKeyHandler::PAGEDOWN, &myDownCB);
00075     keyHandler->addKeyHandler('d', &myDownCB);
00076     keyHandler->addKeyHandler('o', &myOpenCB);
00077     keyHandler->addKeyHandler('c', &myCloseCB);
00078     keyHandler->addKeyHandler('s', &myStopCB);
00079   }
00080 
00081   void liftUp()
00082   {
00083     ArLog::log(ArLog::Normal, "Moving gripper lift up...");
00084     myGripper->liftUp();
00085   }
00086 
00087   void liftDown()
00088   {
00089     ArLog::log(ArLog::Normal, "Moving gripper lift down...");
00090     myGripper->liftDown();
00091   }
00092 
00093   void stop()
00094   {
00095     ArLog::log(ArLog::Normal, "Stopping gripper...");
00096     myGripper->gripperHalt(); // stops both lift an grip
00097     //myGripper->liftStop(); // stops just the lift
00098     //myGripper->gripStop(); // stops just the gripper
00099   }
00100 
00101   void close()
00102   {
00103     ArLog::log(ArLog::Normal, "Closing gripper...");
00104     myGripper->gripClose();
00105   }
00106 
00107   void open()
00108   {
00109     ArLog::log(ArLog::Normal, "Opening gripper...");
00110     myGripper->gripOpen();
00111   }
00112 
00113 };
00114 
00115 int main(int argc, char **argv) 
00116 {
00117 
00118   Aria::init();
00119   ArRobot robot;
00120   ArArgumentParser argParser(&argc, argv);
00121   ArSimpleConnector connector(&argParser);
00122   ArGripper gripper(&robot);
00123   ArSonarDevice sonar;
00124   robot.addRangeDevice(&sonar);
00125 
00126   argParser.loadDefaultArguments();
00127 
00128   if (!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
00129   {
00130     Aria::logOptions();
00131     Aria::shutdown();
00132     return 1;
00133   }
00134   
00135   if (!connector.connectRobot(&robot))
00136   {
00137     ArLog::log(ArLog::Terse, "gripperExample: Could not connect to robot... exiting");
00138     Aria::shutdown();
00139     return 2;
00140   }
00141   ArLog::log(ArLog::Normal, "gripperExample: Connected to robot.");
00142 
00143   ArLog::log(ArLog::Normal, "gripperExample: GripperType=%d", gripper.getType());
00144   gripper.logState();
00145   if(gripper.getType() == ArGripper::NOGRIPPER)
00146   {
00147     ArLog::log(ArLog::Terse, "gripperExample: Error: Robot does not have a gripper. Exiting.");
00148     Aria::shutdown();
00149     return -1;
00150   }
00151 
00152   // Teleoperation actions with obstacle-collision avoidance
00153   ArActionLimiterTableSensor tableLimit;
00154   robot.addAction(&tableLimit, 110);
00155   ArActionLimiterForwards limitNearAction("near", 300, 600, 250);
00156   robot.addAction(&limitNearAction, 100);
00157   ArActionLimiterForwards limitFarAction("far", 300, 1100, 400);
00158   robot.addAction(&limitFarAction, 90);
00159   ArActionLimiterBackwards limitBackAction;
00160   robot.addAction(&limitBackAction, 50);
00161   ArActionJoydrive joydriveAction("joydrive", 400, 15);
00162   robot.addAction(&joydriveAction, 40);
00163   joydriveAction.setStopIfNoButtonPressed(false);
00164   ArActionKeydrive keydriveAction;
00165   robot.addAction(&keydriveAction, 30);
00166   
00167 
00168   // Handlers to control the gripper and print out info (classes defined above)
00169   PrintGripStatus printStatus(&gripper);
00170   GripperControlHandler gripControl(&gripper);
00171   printStatus.addRobotTask(&robot);
00172   gripControl.addKeyHandlers(&robot);
00173 
00174   // enable motors and run (if we lose connection to the robot, exit)
00175   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.");
00176   robot.enableMotors();
00177   robot.run(true);
00178   
00179   Aria::shutdown();
00180   return 0;
00181 }
00182 
00183 

Generated on Fri Jul 31 12:36:37 2009 for Aria by  doxygen 1.4.7