Aria  2.8.0
directMotionExample.cpp

Sends a sequence of motion commands directly to the robot using ArRobot's direct motion command methods.This demo starts up the robot in its own thread, then calls a series of the motion commands methods in ArRobot to simply send commands directly to the robot (rather than using e.g. ArAction objects.)

Note that if you want to stop direct motion commands and let ArActions take over, you must call ArRobot::clearDirectMotion() (otherwise ArRobot may continue sending motion commands that conflict with the requests of the action resolver). See the section on motion commands and actions for details.

/*
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 is a connection handler class, to demonstrate how to run code in
response to events such as the program connecting an disconnecting
from the robot.
*/
class ConnHandler
{
public:
// Constructor
ConnHandler(ArRobot *robot);
// Destructor, its just empty
~ConnHandler(void) {}
// to be called if the connection was made
void connected(void);
// to call if the connection failed
void connFail(void);
// to be called if the connection was lost
void disconnected(void);
protected:
// robot pointer
ArRobot *myRobot;
// the functor callbacks
ArFunctorC<ConnHandler> myConnectedCB;
ArFunctorC<ConnHandler> myDisconnectedCB;
};
ConnHandler::ConnHandler(ArRobot *robot) :
myConnectedCB(this, &ConnHandler::connected),
myConnFailCB(this, &ConnHandler::connFail),
myDisconnectedCB(this, &ConnHandler::disconnected)
{
myRobot = 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::connFail(void)
{
printf("directMotionDemo connection handler: Failed to connect.\n");
myRobot->stopRunning();
return;
}
// turn on motors, and off sonar, and off amigobot sounds, when connected
void ConnHandler::connected(void)
{
printf("directMotionDemo connection handler: Connected\n");
myRobot->comInt(ArCommands::SONAR, 0);
myRobot->comInt(ArCommands::ENABLE, 1);
myRobot->comInt(ArCommands::SOUNDTOG, 0);
}
// lost connection, so just exit
void ConnHandler::disconnected(void)
{
printf("directMotionDemo connection handler: Lost connection, exiting program.\n");
}
int main(int argc, char **argv)
{
ArArgumentParser argParser(&argc, argv);
argParser.loadDefaultArguments();
ArRobot robot;
ArRobotConnector con(&argParser, &robot);
// the connection handler from above
ConnHandler ch(&robot);
{
return 1;
}
if(!con.connectRobot())
{
ArLog::log(ArLog::Normal, "directMotionExample: Could not connect to the robot. Exiting.");
if(argParser.checkHelpAndWarnUnparsed())
{
}
return 1;
}
ArLog::log(ArLog::Normal, "directMotionExample: Connected.");
if(!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
{
}
// Run the robot processing cycle in its own thread. Note that after starting this
// thread, we must lock and unlock the ArRobot object before and after
// accessing it.
robot.runAsync(false);
// Send the robot a series of motion commands directly, sleeping for a
// few seconds afterwards to give the robot time to execute them.
printf("directMotionExample: Setting rot velocity to 100 deg/sec then sleeping 3 seconds\n");
robot.lock();
robot.setRotVel(100);
robot.unlock();
ArUtil::sleep(3*1000);
printf("Stopping\n");
robot.lock();
robot.setRotVel(0);
robot.unlock();
printf("directMotionExample: Telling the robot to go 300 mm on left wheel and 100 mm on right wheel for 5 seconds\n");
robot.lock();
robot.setVel2(300, 100);
robot.unlock();
ArTime start;
start.setToNow();
while (1)
{
robot.lock();
if (start.mSecSince() > 5000)
{
robot.unlock();
break;
}
robot.unlock();
}
printf("directMotionExample: Telling the robot to move forwards one meter, then sleeping 5 seconds\n");
robot.lock();
robot.move(1000);
robot.unlock();
start.setToNow();
while (1)
{
robot.lock();
if (robot.isMoveDone())
{
printf("directMotionExample: Finished distance\n");
robot.unlock();
break;
}
if (start.mSecSince() > 5000)
{
printf("directMotionExample: Distance timed out\n");
robot.unlock();
break;
}
robot.unlock();
}
printf("directMotionExample: Telling the robot to move backwards one meter, then sleeping 5 seconds\n");
robot.lock();
robot.move(-1000);
robot.unlock();
start.setToNow();
while (1)
{
robot.lock();
if (robot.isMoveDone())
{
printf("directMotionExample: Finished distance\n");
robot.unlock();
break;
}
if (start.mSecSince() > 10000)
{
printf("directMotionExample: Distance timed out\n");
robot.unlock();
break;
}
robot.unlock();
}
printf("directMotionExample: Telling the robot to turn to 180, then sleeping 4 seconds\n");
robot.lock();
robot.setHeading(180);
robot.unlock();
start.setToNow();
while (1)
{
robot.lock();
if (robot.isHeadingDone(5))
{
printf("directMotionExample: Finished turn\n");
robot.unlock();
break;
}
if (start.mSecSince() > 5000)
{
printf("directMotionExample: Turn timed out\n");
robot.unlock();
break;
}
robot.unlock();
}
printf("directMotionExample: Telling the robot to turn to 90, then sleeping 2 seconds\n");
robot.lock();
robot.setHeading(90);
robot.unlock();
start.setToNow();
while (1)
{
robot.lock();
if (robot.isHeadingDone(5))
{
printf("directMotionExample: Finished turn\n");
robot.unlock();
break;
}
if (start.mSecSince() > 5000)
{
printf("directMotionExample: turn timed out\n");
robot.unlock();
break;
}
robot.unlock();
}
printf("directMotionExample: Setting vel2 to 200 mm/sec on both wheels, then sleeping 3 seconds\n");
robot.lock();
robot.setVel2(200, 200);
robot.unlock();
printf("directMotionExample: Stopping the robot, then sleeping for 2 seconds\n");
robot.lock();
robot.stop();
robot.unlock();
printf("directMotionExample: Setting velocity to 200 mm/sec then sleeping 3 seconds\n");
robot.lock();
robot.setVel(200);
robot.unlock();
printf("directMotionExample: Stopping the robot, then sleeping for 2 seconds\n");
robot.lock();
robot.stop();
robot.unlock();
printf("directMotionExample: Setting vel2 with 0 on left wheel, 200 mm/sec on right, then sleeping 5 seconds\n");
robot.lock();
robot.setVel2(0, 200);
robot.unlock();
printf("directMotionExample: Telling the robot to rotate at 50 deg/sec then sleeping 5 seconds\n");
robot.lock();
robot.setRotVel(50);
robot.unlock();
printf("directMotionExample: Telling the robot to rotate at -50 deg/sec then sleeping 5 seconds\n");
robot.lock();
robot.setRotVel(-50);
robot.unlock();
printf("directMotionExample: Setting vel2 with 0 on both wheels, then sleeping 3 seconds\n");
robot.lock();
robot.setVel2(0, 0);
robot.unlock();
printf("directMotionExample: Now having the robot change heading by -125 degrees, then sleeping for 6 seconds\n");
robot.lock();
robot.setDeltaHeading(-125);
robot.unlock();
printf("directMotionExample: Now having the robot change heading by 45 degrees, then sleeping for 6 seconds\n");
robot.lock();
robot.setDeltaHeading(45);
robot.unlock();
printf("directMotionExample: Setting vel2 with 200 on left wheel, 0 on right wheel, then sleeping 5 seconds\n");
robot.lock();
robot.setVel2(200, 0);
robot.unlock();
printf("directMotionExample: Done, exiting.\n");
return 0;
}
ArRobot::stop
void stop(void)
Stops the robot.
Definition: ArRobot.cpp:1935
ArRobot::setDeltaHeading
void setDeltaHeading(double deltaHeading)
Sets the delta heading.
Definition: ArRobot.cpp:2118
ArCommands::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
ArRobot::setRotVel
void setRotVel(double velocity)
Sets the rotational velocity.
Definition: ArRobot.cpp:2099
ArRobot::setHeading
void setHeading(double heading)
Sets the heading.
Definition: ArRobot.cpp:2080
ArRobotConnector
Connect to robot or simulator based on program command line parameters.
Definition: ArRobotConnector.h:80
ArRobot::isHeadingDone
bool isHeadingDone(double delta=0.0) const
Sees if the robot is done changing to the previously given setHeading.
Definition: ArRobot.cpp:2061
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
ArRobot::setVel2
void setVel2(double leftVelocity, double rightVelocity)
Sets the velocity of the wheels independently.
Definition: ArRobot.cpp:1993
ArRobot::move
void move(double distance)
Move the given distance forward/backwards.
Definition: ArRobot.cpp:2013
ArTime::setToNow
void setToNow(void)
Resets the time.
Definition: ariaUtil.cpp:1086
ArTime::mSecSince
long mSecSince(ArTime since) const
Gets the number of milliseconds since the given timestamp to this one.
Definition: ariaUtil.h:1029
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::setVel
void setVel(double velocity)
Sets the velocity.
Definition: ArRobot.cpp:1956
ArRobot::runAsync
void runAsync(bool stopRunIfNotConnected, bool runNonThreadedPacketReader=false)
Starts the instance to do processing in its own new thread.
Definition: ArRobot.cpp:301
ArRobot::unlock
int unlock()
Unlock the robot instance.
Definition: ArRobot.h:1272
ArCommands::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
Use normal logging.
Definition: ArLog.h:62
ArTime
A class for time readings and measuring durations.
Definition: ariaUtil.h:1001
ArRobot::isMoveDone
bool isMoveDone(double delta=0.0)
Sees if the robot is done moving the previously given move.
Definition: ArRobot.cpp:2036
ArListPos::FIRST
place item first in the list
Definition: ariaTypedefs.h:70
ArRobot::lock
int lock()
Lock the robot instance.
Definition: ArRobot.h:1268
ArCommands::ENABLE
int, enable (1) or disable (0) motors
Definition: ArCommands.h:43