Aria  2.8.0
robotConnectionCallbacks.cpp

Demonstrates how callbacks can be registered with ArRobot to be called when a connection succeeds or fails.

/*
Adept MobileRobots Robotics Interface for Applications (ARIA)
Copyright (C) 2004, 2005 ActivMedia Robotics LLC
Copyright (C) 2006, 2007, 2008, 2009, 2010 MobileRobots Inc.
Copyright (C) 2011, 2012, 2013 Adept Technology
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
If you wish to redistribute ARIA under different terms, contact
Adept MobileRobots for information about a commercial version of ARIA at
robots@mobilerobots.com or
Adept MobileRobots, 10 Columbia Drive, Amherst, NH 03031; +1-603-881-7960
*/
#include "Aria.h"
/*
This class contains the methods called by the connection callback functors.
*/
class ConnHandler
{
public:
// Constructor
ConnHandler(ArRobot *robot);
// Destructor, its just empty
~ConnHandler(void) {}
// called if the connection was sucessfully made
void connected(void);
// called if the connection failed. stop the robot processing thread.
void connectionFailed(void);
// called when the connection is closed
void disconnected(void);
// called if the connection is lost due to an error
void connectionLost(void);
protected:
// keep a robot pointer
ArRobot *myRobot;
// the functor callbacks
ArFunctorC<ConnHandler> myConnectedCB;
ArFunctorC<ConnHandler> myDisconnectedCB;
};
/* ConnHandler constructor. Initialize functor objects, then
* add them as connection handler callbacks with the robot object.
*/
ConnHandler::ConnHandler(ArRobot *robot) :
myConnectedCB(this, &ConnHandler::connected),
myConnFailCB(this, &ConnHandler::connectionFailed),
myDisconnectedCB(this, &ConnHandler::disconnected),
myConnLostCB(this, &ConnHandler::connectionLost)
{
// keep a robot pointer for use by the handler callback methods
myRobot = robot;
// add the callbacks to the robot
myRobot->addConnectCB(&myConnectedCB, ArListPos::FIRST);
myRobot->addFailedConnectCB(&myConnFailCB, ArListPos::FIRST);
myRobot->addDisconnectNormallyCB(&myDisconnectedCB, ArListPos::FIRST);
myRobot->addDisconnectOnErrorCB(&myDisconnectedCB, ArListPos::FIRST);
}
// just exit if the connection failed
void ConnHandler::connectionFailed(void)
{
ArLog::log(ArLog::Normal, "ConnHandler: Connection failed. Exiting the program.");
}
void ConnHandler::connected(void)
{
ArLog::log(ArLog::Normal, "ConnHandler: Connected. Turning off sonar,");
// turn off sonar, turn off amigobot sounds
myRobot->comInt(ArCommands::SONAR, 0);
myRobot->comInt(ArCommands::SOUNDTOG, 0);
}
// disconnected
void ConnHandler::disconnected(void)
{
ArLog::log(ArLog::Normal, "ConnHandler: Connection closed. Exiting the program.");
}
// lost connection due to errror, exit
void ConnHandler::connectionLost(void)
{
ArLog::log(ArLog::Normal, "ConnHandler: Lost connection due to an error! Exiting the program!");
}
int main(int argc, char **argv)
{
ArRobot robot;
ArArgumentParser argParser(&argc, argv);
ArSimpleConnector con(&argParser);
if(!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
{
return 1;
}
// Create a connection handler object, defined above, then try to connect to the
// robot.
ConnHandler ch(&robot);
con.connectRobot(&robot);
robot.runAsync(true);
// Sleep for 10 seconds, then request that ArRobot stop its thread.
ArLog::log(ArLog::Normal, "Sleeping for 10 seconds...");
ArUtil::sleep(10000);
ArLog::log(ArLog::Normal, "...requesting that the robot thread exit, then shutting down ARIA and exiting.");
robot.stopRunning();
return 0;
}
ArRobot::stopRunning
void stopRunning(bool doDisconnect=true)
Stops the robot from doing any more processing.
Definition: ArRobot.cpp:340
ArCommands::SONAR
@ SONAR
int, enable (1) or disable (0) sonar
Definition: ArCommands.h:63
ArFunctorC< ConnHandler >
ArRobot::addConnectCB
void addConnectCB(ArFunctor *functor, ArListPos::Pos position=ArListPos::LAST)
Adds a connect callback.
Definition: ArRobot.cpp:2646
ArSimpleConnector
Legacy connector for robot and laser.
Definition: ArSimpleConnector.h:51
Aria::exit
static void exit(int exitCode=0)
Shutdown all Aria processes/threads, call exit callbacks, and exit the program.
Definition: Aria.cpp:367
ArLog::log
static void log(LogLevel level, const char *str,...)
Log a message, with formatting and variable number of arguments.
Definition: ArLog.cpp:93
Aria::init
static void init(SigHandleMethod method=SIGHANDLE_THREAD, bool initSockets=true, bool sigHandleExitNotShutdown=true)
Initialize Aria global data struture and perform OS-specific initialization, including adding OS sign...
Definition: Aria.cpp:128
ArRobot
Central class for communicating with and operating the robot.
Definition: ArRobot.h:82
ArArgumentParser
Parse and store program command-line arguments for use by other ARIA classes.
Definition: ArArgumentParser.h:64
Aria::logOptions
static void logOptions(void)
Logs all the options for the program (Calls all the callbacks added with addLogOptionsCB())
Definition: Aria.cpp:794
ArRobot::runAsync
void runAsync(bool stopRunIfNotConnected, bool runNonThreadedPacketReader=false)
Starts the instance to do processing in its own new thread.
Definition: ArRobot.cpp:301
ArCommands::SOUNDTOG
@ SOUNDTOG
int, AmigoBot (old H8 model) specific, enable(1) or diable(0) sound
Definition: ArCommands.h:125
Aria::parseArgs
static bool parseArgs(void)
Parses the arguments for the program (calls all the callbacks added with addParseArgsCB())
Definition: Aria.cpp:759
ArUtil::sleep
static void sleep(unsigned int ms)
Sleep for the given number of milliseconds.
Definition: ariaUtil.cpp:151
ArLog::Normal
@ Normal
Use normal logging.
Definition: ArLog.h:62
ArListPos::FIRST
@ FIRST
place item first in the list
Definition: ariaTypedefs.h:70