Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

robotSyncTaskExample.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 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 #include "Aria.h"
00027 
00039 class PrintingTask
00040 {
00041 public:
00042   // Constructor. Adds our 'user task' to the given robot object.
00043   PrintingTask(ArRobot *robot);
00044 
00045   // Destructor. Does nothing.
00046   ~PrintingTask(void) {}
00047   
00048   // This method will be called by the callback functor
00049   void doTask(void);
00050 protected:
00051   ArRobot *myRobot;
00052 
00053   // The functor to add to the robot for our 'user task'.
00054   ArFunctorC<PrintingTask> myTaskCB;
00055 };
00056 
00057 
00058 // the constructor (note how it uses chaining to initialize myTaskCB)
00059 PrintingTask::PrintingTask(ArRobot *robot) :
00060   myTaskCB(this, &PrintingTask::doTask)
00061 {
00062   myRobot = robot;
00063   // just add it to the robot
00064   myRobot->addSensorInterpTask("PrintingTask", 50, &myTaskCB);
00065 }
00066 
00067 void PrintingTask::doTask(void)
00068 {
00069   // print out some info about the robot
00070   printf("\rx %6.1f  y %6.1f  th  %6.1f vel %7.1f mpacs %3d", myRobot->getX(),
00071      myRobot->getY(), myRobot->getTh(), myRobot->getVel(), 
00072      myRobot->getMotorPacCount());
00073   fflush(stdout);
00074 
00075   // Need sensor readings? Try myRobot->getRangeDevices() to get all 
00076   // range devices, then for each device in the list, call lockDevice(), 
00077   // getCurrentBuffer() to get a list of recent sensor reading positions, then
00078   // unlockDevice().
00079 }
00080 
00081 int main(int argc, char** argv)
00082 {
00083   // the connection
00084   ArSimpleConnector con(&argc, argv);
00085   if(!con.parseArgs())
00086   {
00087     con.logOptions();
00088     return 1;
00089   }
00090 
00091   // robot
00092   ArRobot robot;
00093 
00094   // sonar array range device
00095   ArSonarDevice sonar;
00096 
00097   // This object encapsulates the task we want to do every cycle. 
00098   // Upon creation, it puts a callback functor in the ArRobot object
00099   // as a 'user task'.
00100   PrintingTask pt(&robot);
00101 
00102   // the actions we will use to wander
00103   ArActionStallRecover recover;
00104   ArActionAvoidFront avoidFront;
00105   ArActionConstantVelocity constantVelocity("Constant Velocity", 400);
00106 
00107   // initialize aria
00108   Aria::init();
00109 
00110   // add the sonar object to the robot
00111   robot.addRangeDevice(&sonar);
00112 
00113   // open the connection to the robot; if this fails exit
00114   if(!con.connectRobot(&robot))
00115   {
00116     printf("Could not connect to the robot.\n");
00117     return 2;
00118   }
00119   printf("Connected to the robot. (Press Ctrl-C to exit)\n");
00120   
00121   
00122   // turn on the motors, turn off amigobot sounds
00123   robot.comInt(ArCommands::ENABLE, 1);
00124   robot.comInt(ArCommands::SOUNDTOG, 0);
00125 
00126   // add the wander actions
00127   robot.addAction(&recover, 100);
00128   robot.addAction(&avoidFront, 50);
00129   robot.addAction(&constantVelocity, 25);
00130   
00131   // Start the robot process cycle running. Each cycle, it calls the robot's
00132   // tasks. When the PrintingTask was created above, it added a new
00133   // task to the robot. 'true' means that if the robot connection
00134   // is lost, then ArRobot's processing cycle ends and this call returns.
00135   robot.run(true);
00136 
00137   printf("Disconnected. Goodbye.\n");
00138   
00139   return 0;
00140 }

Generated on Tue Feb 20 10:51:42 2007 for Aria by  doxygen 1.4.0