joydriveActionExample.cpp

Example ArAction object that uses ArJoyHandler to get joystick input.

This program just drives the robot around with a joystick, by way of an ArAction class which reads data fram an ArJoyHandler. Its a practical example of actions.

00001 #include "Aria.h"
00002 
00011 // the action which will drive the robot
00012 class JoydriveAction : public ArAction
00013 {
00014 public:
00015   // constructor
00016   JoydriveAction(void);
00017   // empty destructor
00018   virtual ~JoydriveAction(void);
00019   //the fire which will actually tell the resolver what to do
00020   virtual ArActionDesired *fire(ArActionDesired currentDesired);
00021   // whether the joystick is initalized or not
00022   bool joystickInited(void);
00023 protected:
00024   // action desired
00025   ArActionDesired myDesired;
00026   // joystick handler
00027   ArJoyHandler myJoyHandler;
00028 };
00029 
00030 /*
00031   Note the use of constructor chaining with ArAction.
00032 */  
00033 JoydriveAction::JoydriveAction(void) :
00034   ArAction("Joydrive Action", "This action reads the joystick and sets the translational and rotational velocity based on this.")
00035 {
00036   // initialize the joystick
00037   myJoyHandler.init();
00038   // set up the speed parameters on the joystick
00039   myJoyHandler.setSpeeds(50, 700);
00040 }
00041 
00042 JoydriveAction::~JoydriveAction(void)
00043 {
00044 }
00045 
00046 // whether the joystick is there or not
00047 bool JoydriveAction::joystickInited(void)
00048 {
00049   return myJoyHandler.haveJoystick();
00050 }
00051 
00052 // the guts of the thing
00053 ArActionDesired *JoydriveAction::fire(ArActionDesired currentDesired)
00054 {
00055   int rot, trans;
00056 
00057   // print out some info about hte robot
00058   printf("\rx %6.1f  y %6.1f  tth  %6.1f vel %7.1f mpacs %3d", myRobot->getX(),
00059      myRobot->getY(), myRobot->getTh(), myRobot->getVel(), 
00060      myRobot->getMotorPacCount());
00061   fflush(stdout);
00062 
00063   // see if one of the buttons is pushed, if so drive
00064   if (myJoyHandler.haveJoystick() && (myJoyHandler.getButton(1) ||
00065                     myJoyHandler.getButton(2)))
00066   {
00067     // get the readings from the joystick
00068     myJoyHandler.getAdjusted(&rot, &trans);
00069     // set what we want to do
00070     myDesired.setVel(trans);
00071     myDesired.setDeltaHeading(-rot);
00072     // return the actionDesired
00073     return &myDesired;
00074   }
00075   else
00076   {
00077     // set what we want to do
00078     myDesired.setVel(0);
00079     myDesired.setDeltaHeading(0);
00080     // return the actionDesired
00081     return &myDesired;
00082   }
00083 }
00084 
00085 
00086 int main(int argc, char **argv)
00087 {
00088   ArRobot robot;
00089   Aria::init();
00090   ArSimpleConnector connector(&argc, argv);
00091   if (!connector.parseArgs() || argc > 1)
00092   {
00093     connector.logOptions();
00094     return 1;
00095   }
00096 
00097   // Instance of the JoydriveAction class defined above
00098   JoydriveAction jdAct;
00099 
00100   // if the joydrive action couldn't find the joystick, then exit.
00101   if (!jdAct.joystickInited())
00102   {
00103     printf("Do not have a joystick, set up the joystick then rerun the program\n\n");
00104     Aria::shutdown();
00105     return 1;
00106   }
00107   
00108   // Connect to the robot
00109   if (!connector.connectRobot(&robot))
00110   {
00111     printf("Could not connect to robot... exiting\n");
00112     return 2;
00113   }
00114 
00115 
00116   // disable sonar, enable motors, disable amigobot sound
00117   robot.comInt(ArCommands::SONAR, 0);
00118   robot.comInt(ArCommands::ENABLE, 1);
00119   robot.comInt(ArCommands::SOUNDTOG, 0);
00120 
00121   // add the action
00122   robot.addAction(&jdAct, 100);
00123   // run the robot, true so it'll exit if we lose connection
00124   robot.run(true);
00125   
00126   // now shutdown and exit
00127   Aria::shutdown();
00128   return 0;
00129 }
00130 

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