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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
|
/*
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"
/** @example dpptuExample.cpp Shows how to control the Directed Perception
* pan-tilt unit using ArDPPTU class directly.
*
* This program lets you use the keyboard to control the DPPTU. It uses the same acceleration and slew rates for the pan and tilt axes.
*
* It is also possible to specify the type of PTU in program configuration
* (in the ARIA robot parameter files or program command-line arguments)
* instead. For an example of that, see cameraPTZExample.cpp instead.
Commands:
_________________
UP,DOWN -- tilt up/down by one positional increment
LEFT,RIGHT -- pan left/right by one positional increment
SPACE -- perform reset calibration
I -- initialize PTU to default settings
<,> -- increase/decrease the posIncrement by 0.5 degree
+,- -- increase/decrease the speed by 1 degree/sec
A -- awaits the completion of last issued positional command
R -- change pan/tilt movements to relative or absolute movements
Z -- move pan and tilt axes to zero
1 -- move to stored position 1 (-90, 0)
2 -- move to stored position 2 (90, 0)
3 -- move to stored position 3 (0, -45)
4 -- move to stored position 4 (0, 30)
M -- Enter or Exit monitor (continuous scan) mode
H -- Halt all motion
S -- print current variable values
ESC -- quit
*/
// If defined, use this computer serial port. If not defined, use first robot
// aux. serial port. Most robots have the DPPTU on COM2 if on Linux and COM4 on
// Windows, if not equipped with other accessories which might require those
// ports (e.g. GPS or Laser).
#define SERIAL_PORT ArUtil::COM4
// Determines type of DPPTU to set internal conversion factors. See enum of
// types in ArDPPTU class for possible values.
//#define PTU_TYPE ArDPPTU::PANTILT_PTUD46
// by how much the < and > keys change the position command increment in this
// example
#define POS_INC_ADJUSTMENT 1
/*
This class is the core of this demo, it adds itself to the robot given
as a user task, and contains key handler callbacks to control the PTU.
*/
class KeyPTU
{
public:
// constructor
KeyPTU(ArRobot *robot);
~KeyPTU(void);
void up(void);
void down(void);
void left(void);
void right(void);
void space(void);
void i(void);
void plus(void);
void minus(void);
void greater(void);
void less(void);
void question(void);
void status(void);
void a(void);
void m(void);
void h(void);
void r(void);
void gotoPos(double p, double t);
// the callback function
void drive(void);
protected:
int myPanValPTU;
int myTiltValPTU;
int myDesiredPanPos;
int myDesiredTiltPos;
int mySlew;
int myPosIncrement;
int mySlewIncrement;
int POS_INCREMENT_ADJUSTMENT;
bool myMonitor;
bool myReset;
bool myInit;
bool myAbsolute;
ArFunctorC<KeyPTU> myUpCB;
ArFunctorC<KeyPTU> myDownCB;
ArFunctorC<KeyPTU> myLeftCB;
ArFunctorC<KeyPTU> myRightCB;
ArFunctorC<KeyPTU> mySpaceCB;
ArFunctorC<KeyPTU> myICB;
ArFunctorC<KeyPTU> myPlusCB;
ArFunctorC<KeyPTU> myMinusCB;
ArFunctorC<KeyPTU> myGreaterCB;
ArFunctorC<KeyPTU> myLessCB;
ArFunctorC<KeyPTU> myQuestionCB;
ArFunctorC<KeyPTU> mySCB;
ArFunctorC<KeyPTU> myACB;
ArFunctorC<KeyPTU> myMCB;
ArFunctorC<KeyPTU> myHCB;
ArFunctorC<KeyPTU> myRCB;
ArFunctor2C<KeyPTU, double, double> myPos0CB;
ArFunctor2C<KeyPTU, double, double> myPos1CB;
ArFunctor2C<KeyPTU, double, double> myPos2CB;
ArFunctor2C<KeyPTU, double, double> myPos3CB;
ArFunctor2C<KeyPTU, double, double> myPos4CB;
// the PTU
ArDPPTU myPTU;
// whether the PTU has been inited or not
bool myPTUInited;
// pointer to the robot
ArRobot *myRobot;
// callback for the drive function
ArFunctorC<KeyPTU> myDriveCB;
ArSerialConnection *mySerialConnection;
};
/*
Constructor, sets the robot pointer, and some initial values, also note the
use of constructor chaining on myPTU and myDriveCB.
*/
KeyPTU::KeyPTU(ArRobot *robot) :
myUpCB(this, &KeyPTU::up),
myDownCB(this, &KeyPTU::down),
myLeftCB(this, &KeyPTU::left),
myRightCB(this, &KeyPTU::right),
mySpaceCB(this, &KeyPTU::space),
myICB(this, &KeyPTU::i),
myPlusCB(this, &KeyPTU::plus),
myMinusCB(this, &KeyPTU::minus),
myGreaterCB(this, &KeyPTU::greater),
myLessCB(this, &KeyPTU::less),
myQuestionCB(this, &KeyPTU::question),
mySCB(this, &KeyPTU::status),
myACB(this, &KeyPTU::a),
myMCB(this, &KeyPTU::m),
myHCB(this, &KeyPTU::h),
myRCB(this, &KeyPTU::r),
myPos0CB(this, &KeyPTU::gotoPos, 0, 0),
myPos1CB(this, &KeyPTU::gotoPos, -90.0, 0.0),
myPos2CB(this, &KeyPTU::gotoPos, 90.0, 0.0),
myPos3CB(this, &KeyPTU::gotoPos, 0.0, -45.0),
myPos4CB(this, &KeyPTU::gotoPos, 0.0, 30.0),
myPTU(robot),
myDriveCB(this, &KeyPTU::drive),
mySerialConnection(NULL)
{
#ifdef SERIAL_PORT
mySerialConnection = new ArSerialConnection;
ArLog::log(ArLog::Normal, "dpptuExample: connecting to DPPTU over computer serial port %s.", SERIAL_PORT);
if(mySerialConnection->open(SERIAL_PORT) != 0)
{
ArLog::log(ArLog::Terse, "dpptuExample: Error: Could not open computer serial port %s for DPPTU!", SERIAL_PORT);
Aria::exit(5);
}
myPTU.setDeviceConnection(mySerialConnection);
#endif
// set the robot pointer and add the KeyPTU as user task
ArKeyHandler *keyHandler;
myRobot = robot;
myRobot->addSensorInterpTask("KeyPTU", 50, &myDriveCB);
if ((keyHandler = Aria::getKeyHandler()) == NULL)
{
keyHandler = new ArKeyHandler;
Aria::setKeyHandler(keyHandler);
myRobot->attachKeyHandler(keyHandler);
}
if (!keyHandler->addKeyHandler(ArKeyHandler::UP, &myUpCB))
ArLog::log(ArLog::Terse, "The key handler already has a key for up, keydrive will not work correctly.");
if (!keyHandler->addKeyHandler(ArKeyHandler::DOWN, &myDownCB))
ArLog::log(ArLog::Terse, "The key handler already has a key for down, keydrive will not work correctly.");
if (!keyHandler->addKeyHandler(ArKeyHandler::LEFT, &myLeftCB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for left, keydrive will not work correctly.");
if (!keyHandler->addKeyHandler(ArKeyHandler::RIGHT, &myRightCB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for right, keydrive will not work correctly.");
if (!keyHandler->addKeyHandler(ArKeyHandler::SPACE, &mySpaceCB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for space, keydrive will not work correctly.");
if (!keyHandler->addKeyHandler('i', &myICB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for 'i', keydrive will not work correctly.");
if (!keyHandler->addKeyHandler('+', &myPlusCB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for '+', keydrive will not work correctly.");
if (!keyHandler->addKeyHandler('-', &myMinusCB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for '-', keydrive will not work correctly.");
if (!keyHandler->addKeyHandler('>', &myGreaterCB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for '>', keydrive will not work correctly.");
if (!keyHandler->addKeyHandler('<', &myLessCB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for '<', keydrive will not work correctly.");
if (!keyHandler->addKeyHandler('?', &myQuestionCB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for '?', keydrive will not work correctly.");
if (!keyHandler->addKeyHandler('s', &mySCB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for 'S', keydrive will not work correctly.");
if (!keyHandler->addKeyHandler('a', &myACB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for 'A', keydrive will not work correctly.");
if (!keyHandler->addKeyHandler('z', &myPos0CB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for 'Z', keydrive will not work correctly.");
if (!keyHandler->addKeyHandler('m', &myMCB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for 'M', keydrive will not work correctly.");
if (!keyHandler->addKeyHandler('h', &myHCB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for 'H', keydrive will not work correctly.");
if (!keyHandler->addKeyHandler('r', &myRCB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for 'R', keydrive will not work correctly.");
if(!keyHandler->addKeyHandler('Z', &myPos0CB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for 'Z', keydrive will not work correctly.");
if(!keyHandler->addKeyHandler('1', &myPos1CB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for '1', keydrive will not work correctly.");
if(!keyHandler->addKeyHandler('2', &myPos2CB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for '2', keydrive will not work correctly.");
if(!keyHandler->addKeyHandler('3', &myPos3CB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for '3', keydrive will not work correctly.");
if(!keyHandler->addKeyHandler('4', &myPos4CB))
ArLog::log(ArLog::Terse,
"The key handler already has a key for '4', keydrive will not work correctly.");
// initialize some variables
myReset = false;
myInit = true;
myDesiredPanPos = 0;
myDesiredTiltPos = 0;
myPosIncrement = 1;
mySlewIncrement = 1;
myPTUInited = false;
myMonitor = false;
}
KeyPTU::~KeyPTU()
{
if(mySerialConnection)
{
myPTU.setDeviceConnection(NULL);
delete mySerialConnection;
}
}
void KeyPTU::left(void)
{
myDesiredPanPos += myPosIncrement;
if (myDesiredPanPos > myPTU.getMaxPosPan())
myDesiredPanPos = myPTU.getMaxPosPan();
}
void KeyPTU::right(void)
{
myDesiredPanPos -= myPosIncrement;
if (myDesiredPanPos < myPTU.getMaxNegPan())
myDesiredPanPos = myPTU.getMaxNegPan();
}
void KeyPTU::up(void)
{
myDesiredTiltPos += myPosIncrement;
if (myDesiredTiltPos > myPTU.getMaxPosTilt())
myDesiredTiltPos = myPTU.getMaxPosTilt();
}
void KeyPTU::down(void)
{
myDesiredTiltPos -= myPosIncrement;
if (myDesiredTiltPos < myPTU.getMaxNegTilt())
myDesiredTiltPos = myPTU.getMaxNegTilt();
}
void KeyPTU::space(void)
{
myReset = true;
}
void KeyPTU::i(void)
{
myInit = true;
}
void KeyPTU::plus(void)
{
mySlew += mySlewIncrement;
if (mySlew > myPTU.getMaxPanSlew())
mySlew = myPTU.getMaxPanSlew();
status();
}
void KeyPTU::minus(void)
{
mySlew -= mySlewIncrement;
if (mySlew < myPTU.getMinPanSlew())
mySlew = myPTU.getMinPanSlew();
status();
}
void KeyPTU::greater(void)
{
myPosIncrement += POS_INCREMENT_ADJUSTMENT;
if (myPosIncrement > myPTU.getMaxPosPan())
myPosIncrement = myPTU.getMaxPosPan();
status();
}
void KeyPTU::less(void)
{
myPosIncrement -= POS_INCREMENT_ADJUSTMENT;
if (myPosIncrement < 0)
myPosIncrement = 0;
status();
}
void KeyPTU::a(void)
{
myPTU.awaitExec();
ArLog::log(ArLog::Normal, "AwaitExecution command sent");
}
void KeyPTU::gotoPos(double p, double t)
{
myDesiredPanPos = p;
myDesiredTiltPos = t;
status();
}
void KeyPTU::question(void)
{
ArLog::log(ArLog::Normal, "\r\nCommands:\r\n_________________\r\n");
ArLog::log(ArLog::Normal, "UP,DOWN -- tilt up/down by one positional increment");
ArLog::log(ArLog::Normal, "LEFT,RIGHT -- pan left/right by one positional increment");
ArLog::log(ArLog::Normal, "SPACE -- perform reset calibration");
ArLog::log(ArLog::Normal, "I -- initialize PTU to default settings");
ArLog::log(ArLog::Normal, "<,> -- increase/decrease the posIncrement by %f degree(s)", POS_INC_ADJUSTMENT);
ArLog::log(ArLog::Normal, "+,- -- increase/decrease the speed by 1 degree/sec");
ArLog::log(ArLog::Normal, "A -- awaits the completion of last issued positional command");
ArLog::log(ArLog::Normal, "R -- change pan/tilt movements to relative or absolute movements");
ArLog::log(ArLog::Normal, "Z -- move pan and tilt axes to zero");
ArLog::log(ArLog::Normal, "1 -- move to stored position 1 (-90, 0)");
ArLog::log(ArLog::Normal, "2 -- move to stored position 2 (90, 0)");
ArLog::log(ArLog::Normal, "3 -- move to stored position 3 (0, -45)");
ArLog::log(ArLog::Normal, "4 -- move to stored position 4 (0, 30)");
ArLog::log(ArLog::Normal, "M -- Enter or Exit monitor (continuous scan) mode");
ArLog::log(ArLog::Normal, "H -- Halt all motion");
ArLog::log(ArLog::Normal, "S -- print current variable values");
ArLog::log(ArLog::Normal, "ESC -- exit program");
ArLog::log(ArLog::Normal, "\r\n");
}
void KeyPTU::status(void)
{
ArLog::log(ArLog::Normal, "\r\nStatus:\r\n_________________\r\n");
ArLog::log(ArLog::Normal, "Last Pan Command = %.1f deg", myPTU.getLastPanRequest());
ArLog::log(ArLog::Normal, "Last Tilt Command = %.1f deg", myPTU.getLastTiltRequest());
ArLog::log(ArLog::Normal, "Current Pan Position = %.1f deg", myPTU.getPan());
ArLog::log(ArLog::Normal, "Current Tilt Position = %.1f deg", myPTU.getTilt());
ArLog::log(ArLog::Normal, "Pan Slew Rate = %d deg/sec", myPTU.getPanSlew());
ArLog::log(ArLog::Normal, "Tilt Slew Rate = %d deg/sec", myPTU.getTiltSlew());
ArLog::log(ArLog::Normal, "Position Increment = %d deg", myPosIncrement);
if (myAbsolute)
ArLog::log(ArLog::Normal, "Positional-movements using absolute commands");
else
ArLog::log(ArLog::Normal, "Positional-movements using relative commands");
ArLog::log(ArLog::Normal, "\r\n");
}
void KeyPTU::m(void)
{
if (!myMonitor)
{
ArLog::log(ArLog::Normal, "Entering Monitor mode - hit 'M' to disable");
myMonitor = true;
myPTU.initMon(-60,60,30,-30);
}
else
{
myPTU.blank(); //Blank packet exits monitor mode
myMonitor = false;
}
}
void KeyPTU::h(void)
{
myPTU.haltAll();
}
void KeyPTU::r(void)
{
if (!myAbsolute)
{
myAbsolute = true;
}
else
{
myAbsolute = false;
}
status();
}
// the important function
void KeyPTU::drive(void)
{
// if the PTU isn't initialized, initialize it here... it has to be
// done here instead of above because it needs to be done when the
// robot is connected
if (!myPTUInited && myRobot->isConnected())
{
ArLog::log(ArLog::Normal, "Initializing ArDPPTU...");
myPTU.init();
ArLog::log(ArLog::Normal, "Resetting PTU and performing self-calibration...");
myPTU.resetCalib();
myPTU.awaitExec(); // DPPTU will wait for self-calibration to end before executing the following commands (though they will still be sent)
mySlew = myPTU.getPanSlew(); //uses only pan slew rate
myPTU.awaitExec();
myPTUInited = true;
myInit = false;
myAbsolute = true;
}
if (myInit == true) // User hit initialization key
{
ArLog::log(ArLog::Normal, "Initializing PTU...");
myPTU.init();
myInit = false;
myDesiredPanPos = myPTU.getPan();
myDesiredTiltPos = myPTU.getTilt();
mySlew = myPTU.getPanSlew(); //uses only pan slew rate
myReset = false;
}
if (myReset == true) // User hit reset key
{
ArLog::log(ArLog::Normal, "Resetting PTU and performing self-calibration...");
myPTU.resetCalib();
myPTU.awaitExec();
myDesiredPanPos = myPTU.getPan();
myDesiredTiltPos = myPTU.getTilt();
myReset = false;
}
else // User did nothing, or hit a key that changed myDesiredPanPos, myDesiredTiltPos, or mySlew (so request PTU to move if those changed since last request)
{
// Some PTUs can determine their current position (with encoders, etc) and return that.
// canGetRealPanTilt() will return true in this case, and getPan() and
// getTilt() will return those received values. Otherwise, getPan() and
// getTilt() return the last commanded values. getLastPanRequest() and
// getLastTiltRequest() will always return the last commanded values sent by
// ArDPPTU (so in the case that canGetRealPanTilt() is false, getPan() and
// getTilt() return the same pair of values as getLastPanRequest() and
// getLastTiltRequest(). ArDPPTU::canGetRealPanTilt() is initialally false,
// but once the first set of pan and tilt positions is read back from the
// PTU device, it becomes true.
if(myPTU.canGetRealPanTilt())
printf("Position (%.1f deg, %.1f deg) [Incr. %d deg] Press ? for help \r", myPTU.getPan(), myPTU.getTilt(), myPosIncrement);
else
printf("Requested (%.1f deg, %.1f deg) [Incr. %d deg] Press ? for help \r", myPTU.getPan(), myPTU.getTilt(), myPosIncrement);
if (myDesiredPanPos != myPTU.getLastPanRequest())
{
if (myAbsolute)
myPTU.pan(myDesiredPanPos);
else
myPTU.panRel(myDesiredPanPos - myPTU.getPan());
}
if (myDesiredTiltPos != myPTU.getLastTiltRequest())
{
if (myAbsolute)
myPTU.tilt(myDesiredTiltPos);
else
myPTU.tiltRel(myDesiredTiltPos - myPTU.getTilt());
}
if (mySlew != myPTU.getPanSlew())
{
myPTU.panSlew(mySlew);
myPTU.tiltSlew(mySlew);
}
}
}
int main(int argc, char **argv)
{
Aria::init();
ArArgumentParser parser(&argc, argv);
parser.loadDefaultArguments();
ArRobot robot;
ArRobotConnector robotConnector(&parser, &robot);
if(!robotConnector.connectRobot())
{
ArLog::log(ArLog::Terse, "dpptuExample: Could not connect to the robot.");
if(parser.checkHelpAndWarnUnparsed())
{
Aria::logOptions();
Aria::exit(1);
}
}
if (!Aria::parseArgs() || !parser.checkHelpAndWarnUnparsed())
{
Aria::logOptions();
Aria::exit(1);
}
ArLog::log(ArLog::Normal, "dpptuExample: Connected to robot.");
robot.runAsync(true);
// an object for keyboard control, class defined above, this also adds itself as a user task
KeyPTU ptu(&robot);
// turn off the sonar
robot.comInt(ArCommands::SONAR, 0);
printf("Press '?' for available commands\r\n");
// run, if we lose connection to the robot, exit
robot.waitForRunExit();
Aria::exit(0);
}
|