teleopActionsExample.cpp

Example using actions for safe teleoperation with keyboard or joystick.

This example program creates several "limiting" actions (stop the robot from hitting detected obstacles), as well as Joydrive and Keydrive actions which request movement based on keys pressed or a joystick attached to the computer. The limiting actions are added at a higher priority than the teleoperation actions, so they prevent those actions from driving the robot if nearby obstacles are detected; otherwise, you can drive the robot using they joystick or keyboard.

00001 #include "Aria.h"
00002 
00015 int main(int argc, char **argv)
00016 {
00017   Aria::init();
00018 
00019   ArRobot robot;
00020   
00021   // limiter for close obstacles
00022   ArActionLimiterForwards limiter("speed limiter near", 300, 600, 250);
00023   // limiter for far away obstacles
00024   ArActionLimiterForwards limiterFar("speed limiter far", 300, 1100, 400);
00025   // limiter that checks IR sensors (like Peoplebot has)
00026   ArActionLimiterTableSensor tableLimiter;
00027   // limiter so we don't bump things backwards
00028   ArActionLimiterBackwards backwardsLimiter;
00029   // the joydrive action
00030   ArActionJoydrive joydriveAct;
00031   // the keydrive action
00032   ArActionKeydrive keydriveAct;
00033   
00034   // sonar device, used by the limiter actions.
00035   ArSonarDevice sonar;
00036 
00037   ArArgumentParser parser(&argc, argv);
00038   parser.loadDefaultArguments();
00039   ArSimpleConnector connector(&parser);
00040   
00041   if (!Aria::parseArgs())
00042   {
00043     Aria::logOptions();
00044     Aria::shutdown();
00045     return 1;
00046   }
00047   
00048 
00049   printf("This program will allow you to use a joystick or keyboard to control the robot.\nYou can use the arrow keys to drive, and the spacebar to stop.\nFor joystick control press the trigger button and then drive.\nPress escape to exit.\n");
00050 
00051   // if we don't have a joystick, let 'em know
00052   if (!joydriveAct.joystickInited())
00053     printf("Do not have a joystick, only the arrow keys on the keyboard will work.\n");
00054   
00055   // add the sonar to the robot
00056   robot.addRangeDevice(&sonar);
00057 
00058   // try to connect, if we fail exit
00059   if (!connector.connectRobot(&robot))
00060   {
00061     printf("Could not connect to robot... exiting\n");
00062     Aria::shutdown();
00063     return 1;
00064   }
00065 
00066   // set the robots maximum velocity (sonar don't work at all well if you're
00067   // going faster)
00068   robot.setAbsoluteMaxTransVel(400);
00069 
00070   // enable the motor
00071   robot.enableMotors();
00072 
00073   // Add the actions, with the limiters as highest priority, then the teleop.
00074   // actions.  This will keep the teleop. actions from being able to drive too 
00075   // fast and hit something
00076   robot.addAction(&tableLimiter, 100);
00077   robot.addAction(&limiter, 95);
00078   robot.addAction(&limiterFar, 90);
00079   robot.addAction(&backwardsLimiter, 85);
00080   robot.addAction(&joydriveAct, 50);
00081   robot.addAction(&keydriveAct, 45);
00082 
00083   // Configure the joydrive action so it will let the lower priority actions
00084   // (i.e. keydriveAct) request motion if the joystick button is
00085   // not pressed.
00086   joydriveAct.setStopIfNoButtonPressed(false);
00087 
00088   
00089   // run the robot, true means that the run will exit if connection lost
00090   robot.run(true);
00091   
00092   Aria::shutdown();
00093   return 0;
00094 }

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