1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*
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 program just drives the robot around with a joystick.
This example shows an example of a program making a thread for its own use
(reading the joystick and driving the robot), having the robot run in its
own thread, and then keeping its main thread to itself. Demonstrates the
thread locking that must be done for threads to work safely. If you don't
know or understand threading, or you don't need threading, you probably
shouldn't do it this way, as it is more complicated.
*/
/*
This class creates its own thread, and then runs in the thread, controlling
the robot with the joystick.
*/
class Joydrive : public ArASyncTask
{
public:
// constructor
Joydrive(ArRobot *robot);
// empty destructor
~Joydrive(void) {}
// the function to run in the new thread, this just is called once, so
// only return when you want th ethread to exit
virtual void * runThread(void *arg);
protected:
// joystick handler
ArJoyHandler myJoyHandler;
// robot pointer
ArRobot *myRobot;
};
// a nice simple constructor
Joydrive::Joydrive(ArRobot *robot)
{
setThreadName("Joydrive");
// set the robot pointer
myRobot = robot;
// initialize the joystick
myJoyHandler.init();
// set up the joystick so we'll get the speeds out we want
myJoyHandler.setSpeeds(40, 700);
// see if we have a joystick, and let the users know
if (myJoyHandler.haveJoystick())
{
printf("Have a joystick\n\n");
}
// if we don't have a joystick, then print error message and exit
else
{
printf("Do not have a joystick, set up the joystick then rerun the program\n\n");
Aria::exit(1); // exit program with error code 1
}
// this is what creates are own thread, its from the ArASyncTask
create();
}
// this is the function called in the new thread
void *Joydrive::runThread(void *arg)
{
threadStarted();
int trans, rot;
// only run while running, ie play nice and pay attention to the thread
//being shutdown
while (myRunning)
{
// lock the robot before touching it
myRobot->lock();
if (!myRobot->isConnected())
{
myRobot->unlock();
break;
}
// print out some information about the robot
printf("\rx %6.1f y %6.1f tth %6.1f vel %7.1f mpacs %3d ",
myRobot->getX(), myRobot->getY(), myRobot->getTh(),
myRobot->getVel(), myRobot->getMotorPacCount());
fflush(stdout);
// if one of the joystick buttons is pushed, drive the robot
if (myJoyHandler.haveJoystick() && (myJoyHandler.getButton(1) ||
myJoyHandler.getButton(2)))
{
// get out the values from the joystick
myJoyHandler.getAdjusted(&rot, &trans);
// drive the robot
myRobot->setVel(trans);
myRobot->setRotVel(-rot);
}
// if no buttons are pushed stop the robot
else
{
myRobot->setVel(0);
myRobot->setRotVel(0);
}
// unlock the robot, so everything else can run
myRobot->unlock();
// now take a little nap
ArUtil::sleep(50);
}
// return out here, means the thread is done
return NULL;
}
/*
This is a connection handler, fairly simple, but quite useful, esp when
the robot is running in another thread.
*/
class ConnHandler
{
public:
// constructor
ConnHandler(ArRobot *robot, Joydrive *jd);
// 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;
// pointer to joydrive
Joydrive *myJoydrive;
// the functor callbacks
ArFunctorC<ConnHandler> *myConnectedCB;
ArFunctorC<ConnHandler> *myConnFailCB;
ArFunctorC<ConnHandler> *myDisconnectedCB;
};
// the mythical constructor
ConnHandler::ConnHandler(ArRobot *robot, Joydrive *jd)
{
// set the pointers
myRobot = robot;
myJoydrive = jd;
// now create the functor callbacks, then set them on the robot
myConnectedCB = new ArFunctorC<ConnHandler>(this, &ConnHandler::connected);
myRobot->addConnectCB(myConnectedCB, ArListPos::FIRST);
myConnFailCB = new ArFunctorC<ConnHandler>(this, &ConnHandler::connFail);
myRobot->addFailedConnectCB(myConnFailCB, ArListPos::FIRST);
myDisconnectedCB = new ArFunctorC<ConnHandler>(this,
&ConnHandler::disconnected);
myRobot->addDisconnectNormallyCB(myDisconnectedCB, ArListPos::FIRST);
myRobot->addDisconnectOnErrorCB(myDisconnectedCB, ArListPos::FIRST);
}
// when we connect turn off the sonar, turn on the motors, and disable amigobot
// sound
void ConnHandler::connected(void)
{
myRobot->comInt(ArCommands::SONAR, 0);
myRobot->comInt(ArCommands::ENABLE, 1);
myRobot->comInt(ArCommands::SOUNDTOG, 0);
}
// just exit if we failed to connect
void ConnHandler::connFail(void)
{
printf("Failed to connect.\n");
myRobot->stopRunning();
myJoydrive->stopRunning();
Aria::exit(2); // exit program with error code 2
}
// if we lost connection then exit
void ConnHandler::disconnected(void)
{
printf("Lost connection\n");
myRobot->stopRunning();
myJoydrive->stopRunning();
Aria::exit(3); // exit program with error code 3
}
int main(int argc, char **argv)
{
std::string str;
int ret;
// connection to the robot
ArTcpConnection con;
// the robot
ArRobot robot;
// ake the joydrive object, which also creates its own thread
Joydrive joyd(&robot);
// the connection handler
ConnHandler ch(&robot, &joyd);
// init aria, which will make a dedicated signal handling thread
Aria::init(Aria::SIGHANDLE_THREAD);
// open the connection with default args, exit if it fails
if ((ret = con.open()) != 0)
{
str = con.getOpenMessage(ret);
printf("Open failed: %s\n", str.c_str());
Aria::shutdown();
return 1;
}
// set the connection on the robot
robot.setDeviceConnection(&con);
// run the robot in its own thread
robot.runAsync(false);
// have the robot connect asyncronously (so its loop is still running)
// if this fails it means that the robot isn't running in its own thread
if (!robot.asyncConnect())
{
printf(
"asyncConnect failed because robot is not running in its own thread.\n");
Aria::shutdown();
return 1;
}
// now we just wait for the robot to be done running
printf("Waiting for the robot's run to exit.\n");
robot.waitForRunExit();
// then we exit
printf("exiting main\n");
Aria::exit(0); // exit program
return 0;
}
|