This program uses ArActionKeydrive and ArActionJoydrive to allow teleoperation with the keyboard or joystick, and displays gyro data. Additional keys (numbers 0-9 and letters q, w, e, r, t, y, u, i, o, p) activate preset rotation velocities.
00001 00010 #include "Aria.h" 00011 #include "ArAnalogGyro.h" 00012 00013 00014 class GyroTask 00015 { 00016 public: 00017 // the constructor, it must use constructor chaining to intialize its base 00018 // class ArSimpleUserTask 00019 GyroTask(ArRobot *robot); 00020 // empty destructor 00021 ~GyroTask(void) {} 00022 00023 // the task we want to do 00024 void doTask(void); 00025 protected: 00026 //double myHeading; 00027 ArAnalogGyro *myGyro; 00028 ArRobot *myRobot; 00029 ArFunctorC<GyroTask> myTaskCB; 00030 }; 00031 00032 00033 // the constructor, note how it uses chaining to initialize the myTaskCB 00034 GyroTask::GyroTask(ArRobot *robot) : 00035 myTaskCB(this, &GyroTask::doTask) 00036 { 00037 ArKeyHandler *keyHandler; 00038 myRobot = robot; 00039 // just add it to the robot 00040 myRobot->addUserTask("GyroTask", 50, &myTaskCB); 00041 myGyro = new ArAnalogGyro(myRobot); 00042 if ((keyHandler = Aria::getKeyHandler()) == NULL) 00043 { 00044 keyHandler = new ArKeyHandler; 00045 Aria::setKeyHandler(keyHandler); 00046 if (myRobot != NULL) 00047 myRobot->attachKeyHandler(keyHandler); 00048 else 00049 ArLog::log(ArLog::Terse, "GyroTask: No robot to attach a keyHandler to, keyHandling won't work... either make your own keyHandler and drive it yourself, make a keyhandler and attach it to a robot, or give this a robot to attach to."); 00050 } 00051 keyHandler->addKeyHandler('1', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, 10)); 00052 keyHandler->addKeyHandler('2', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, 20)); 00053 keyHandler->addKeyHandler('3', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, 30)); 00054 keyHandler->addKeyHandler('4', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, 40)); 00055 keyHandler->addKeyHandler('5', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, 50)); 00056 keyHandler->addKeyHandler('6', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, 60)); 00057 keyHandler->addKeyHandler('7', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, 70)); 00058 keyHandler->addKeyHandler('8', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, 80)); 00059 keyHandler->addKeyHandler('9', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, 90)); 00060 keyHandler->addKeyHandler('0', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, 100)); 00061 00062 keyHandler->addKeyHandler('q', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, -10)); 00063 keyHandler->addKeyHandler('w', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, -20)); 00064 keyHandler->addKeyHandler('e', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, -30)); 00065 keyHandler->addKeyHandler('r', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, -40)); 00066 keyHandler->addKeyHandler('t', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, -50)); 00067 keyHandler->addKeyHandler('y', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, -60)); 00068 keyHandler->addKeyHandler('u', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, -70)); 00069 keyHandler->addKeyHandler('i', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, -80)); 00070 keyHandler->addKeyHandler('o', new ArFunctor1C<ArRobot, double>(myRobot,&ArRobot::setRotVel, -90)); 00071 keyHandler->addKeyHandler('p', new ArFunctor1C<ArRobot, double>(myRobot, &ArRobot::setRotVel, -100)); 00072 00073 keyHandler->addKeyHandler('a', new ArFunctor1C<ArRobot, double>(myRobot, &ArRobot::setHeading, 0)); 00074 keyHandler->addKeyHandler('s', new ArFunctor1C<ArRobot, double>(myRobot, &ArRobot::setHeading, 90)); 00075 keyHandler->addKeyHandler('d', new ArFunctor1C<ArRobot, double>(myRobot, &ArRobot::setHeading, 180)); 00076 keyHandler->addKeyHandler('f', new ArFunctor1C<ArRobot, double>(myRobot, &ArRobot::setHeading, 270)); 00077 00078 } 00079 00080 void GyroTask::doTask(void) 00081 { 00082 /* 00083 double degrees = -((myRobot->getAnalog() * 5.0 / 255) - 2.509) * 150 / 2.5 * 1.265; 00084 if (fabs(degrees) < 2) 00085 degrees = 0; 00086 myHeading += degrees * .025; 00087 printf("%10f %10f %10f %10f\n", myRobot->getAnalog() * 5.0 / 255, degrees, 00088 myRobot->getRotVel(), myHeading); 00089 fflush(stdout); 00090 */ 00091 printf("gyro th (mode 1 only):%8.4f encoder th:%8.4f ArRobot mixed th:%8.4f temp:%d ave:%g\n", myGyro->getHeading(), myRobot->getRawEncoderPose().getTh(), myRobot->getTh(), myGyro->getTemperature(), myGyro->getAverage()); 00092 } 00093 00094 00095 00096 int main(int argc, char **argv) 00097 { 00098 // robot 00099 ArRobot robot; 00100 00101 // the joydrive action 00102 ArActionJoydrive joydriveAct; 00103 // the keydrive action 00104 ArActionKeydrive keydriveAct; 00105 00106 GyroTask gyro(&robot); 00107 00108 // sonar device, so the limiter will work, this must be added to the robot 00109 ArSonarDevice sonar; 00110 00111 ArSimpleConnector connector(&argc, argv); 00112 if (!connector.parseArgs() || argc > 1) 00113 { 00114 connector.logOptions(); 00115 exit(1); 00116 } 00117 00118 // mandatory init 00119 Aria::init(); 00120 00121 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"); 00122 00123 00124 // if we don't have a joystick, let 'em know 00125 if (!joydriveAct.joystickInited()) 00126 printf("Do not have a joystick, only the arrow keys on the keyboard will work.\n"); 00127 00128 // set the joystick so it won't do anything if the button isn't pressed 00129 joydriveAct.setStopIfNoButtonPressed(false); 00130 00131 // add the sonar to the robot 00132 robot.addRangeDevice(&sonar); 00133 00134 // try to connect, if we fail exit 00135 if (!connector.connectRobot(&robot)) 00136 { 00137 printf("Could not connect to robot... exiting\n"); 00138 Aria::shutdown(); 00139 return 1; 00140 } 00141 00142 robot.comInt(ArCommands::ENABLE, 1); 00143 00144 robot.addAction(&joydriveAct, 50); 00145 robot.addAction(&keydriveAct, 45); 00146 00147 // set the joydrive action so it'll let the keydrive action fire if 00148 // there is no button pressed 00149 joydriveAct.setStopIfNoButtonPressed(false); 00150 00151 00152 // run the robot, true here so that the run will exit if connection lost 00153 robot.run(true); 00154 00155 // now exit 00156 Aria::shutdown(); 00157 return 0; 00158 }
1.4.7