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
|
/*
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"
#include <string>
/*
* This is useful as a diagnostic tool, plus it shows all the many accessor
* methods of ArRobot for robot state. It makes the robot wander, using sonar
* to avoid obstacles, and prints out various pieces of robot state information
* each second. Refer to the ARIA ArRobot documentation and to your robot manual
* (section on standard ARCOS SIP packet contents) for details on the data.
*
* If -connectLaser command line argument is given, or is set in robot parameter
* file, it will use th elaser as well as the sonar to wander.
*
* ARIA also contains a class called ArDataLogger which is configurable through
* ArConfig. You can use ArDataLogger in your applications to incorporate
* a similar data logging feature.
*/
/* function to display a byte as a string of 8 '1' and '0' characters. */
std::string byte_as_bitstring(char byte)
{
char tmp[9];
int bit;
int ch;
for(bit = 7, ch = 0; bit >= 0; bit--,ch++)
tmp[ch] = ((byte>>bit)&1) ? '1' : '0';
tmp[8] = 0;
return std::string(tmp);
}
/* function to display a 2-byte int as a string of 16 '1' and '0' characters. */
std::string int_as_bitstring(ArTypes::Byte2 n)
{
char tmp[17];
int bit;
int ch;
for(bit = 15, ch = 0; bit >= 0; bit--, ch++)
tmp[ch] = ((n>>bit)&1) ? '1' : '0';
tmp[16] = 0;
return std::string(tmp);
}
std::string charge_state_string(int state)
{
switch(state)
{
case ArRobot::CHARGING_UNKNOWN:
return "unknown";
case ArRobot::CHARGING_NOT:
return "not";
case ArRobot::CHARGING_BULK:
return "bulk";
case ArRobot::CHARGING_FLOAT:
return "float";
case ArRobot::CHARGING_OVERCHARGE:
return "over";
default:
return "UNREC VAL";
}
}
/* Some events might only be detectable in one robot cycle, not over the
* 1-second period that the main thread sleeps. This cycle callback will detect
* those and save them in some global variables. */
bool wasLeftMotorStalled = false;
bool wasRightMotorStalled = false;
ArTypes::UByte2 cumulativeStallVal = 0;
ArTypes::UByte2 cumulativeRobotFlags = 0;
bool wasLeftIRTriggered = false;
bool wasRightIRTriggered = false;
bool wasEStopTriggered = false;
bool cycleCallback(ArRobot* robot)
{
cumulativeStallVal |= robot->getStallValue();
wasLeftMotorStalled = wasLeftMotorStalled || robot->isLeftMotorStalled();
wasRightMotorStalled = wasRightMotorStalled || robot->isRightMotorStalled();
wasEStopTriggered = wasEStopTriggered || robot->getEstop();
wasLeftIRTriggered = wasLeftIRTriggered || (robot->hasTableSensingIR() && robot->isLeftTableSensingIRTriggered());
wasRightIRTriggered = wasRightIRTriggered || (robot->hasTableSensingIR() && robot->isRightTableSensingIRTriggered());
return true;
}
unsigned int encoderPacketCountPrevSec;
unsigned int encoderPacketCount;
ArTime encoderPacketTimer;
bool packetCallback(ArRobotPacket *packet)
{
if(packet->getID() == 0x90)
{
// encoder packet
++encoderPacketCount;
if(encoderPacketTimer.secSince() >= 1)
{
encoderPacketCountPrevSec = encoderPacketCount;
encoderPacketCount = 0;
encoderPacketTimer.setToNow();
}
//printf("got an encoder packet. Count=%d, CountPrevSec=%d\n", encoderPacketCount, encoderPacketCountPrevSec);
}
return false; // let other packet handlers (e.g. in ArRobot) be called
}
ArActionGroup *ToggleActionGroup = NULL;
bool ToggleActionGroupActive = false;
void toggleaction(int signal)
{
ArLog::log(ArLog::Normal, "%s action group.", ToggleActionGroupActive?"Deactivating":"Activating");
if(ToggleActionGroupActive)
{
ToggleActionGroup->deactivate();
ToggleActionGroupActive = false;
}
else
{
ToggleActionGroup->activate();
ToggleActionGroupActive = true;
}
}
/* main function */
int main(int argc, char **argv)
{
Aria::init();
ArLog::init(ArLog::StdErr, ArLog::Normal);
// robot and devices
ArRobot robot;
ArSonarDevice sonar;
ArBumpers bumpers;
ArIRs ir;
// the actions we'll use to wander and avoid obstacles
ArActionStallRecover recoverAct;
ArActionBumpers bumpAct;
ArActionAvoidFront avoidFrontNearAct("Avoid Front Near", 225, 0);
ArActionAvoidFront avoidFrontFarAct;
ArActionConstantVelocity constantVelocityAct("Constant Velocity", 400);
printf("This program will make the robot wander around, avoiding obstacles, and print some data and events.\nPress Ctrl-C to exit. Send SIGUSR1 signal to toggle wandering.\n");
ArArgumentParser parser(&argc, argv);
parser.loadDefaultArguments();
ArRobotConnector robotConnector(&parser, &robot);
if (!Aria::parseArgs() || !parser.checkHelpAndWarnUnparsed())
{
ArLog::log(ArLog::Terse, "wanderAndLogData: Could not connect to the robot.");
if(parser.checkHelpAndWarnUnparsed())
{
Aria::logOptions();
Aria::exit(1);
return 1;
}
}
if(!robotConnector.connectRobot())
{
ArLog::log(ArLog::Terse, "wanderAndLogData: Could not connect to the robot.");
Aria::exit(2);
return 2;
}
ArLog::log(ArLog::Normal, "wanderAndLogData: Connected.");
ArLaserConnector laserConnector(&parser, &robot, &robotConnector);
if(!laserConnector.connectLasers())
ArLog::log(ArLog::Normal, "Warning: unable to connect to requested lasers, will wander using robot sonar only.");
// add the range devices to the robot
robot.addRangeDevice(&sonar);
robot.addRangeDevice(&bumpers);
robot.addRangeDevice(&ir);
// turn on the motors, turn off amigobot sound effects (for old h8-model amigobots)
robot.enableMotors();
robot.comInt(ArCommands::SOUNDTOG, 0);
// add the actions created above
ArActionGroup wanderGroup(&robot);
wanderGroup.addAction(&recoverAct, 100);
wanderGroup.addAction(&bumpAct, 75);
wanderGroup.addAction(&avoidFrontNearAct, 50);
wanderGroup.addAction(&avoidFrontFarAct, 49);
wanderGroup.addAction(&constantVelocityAct, 25);
ToggleActionGroup = &wanderGroup;
// can use SIGUSR1 to disable wandering (use Linux kill command)
signal(SIGUSR1, toggleaction);
// Cycle callback to check for events
robot.addUserTask("checkevents", 1, new ArGlobalRetFunctor1<bool, ArRobot*>(&cycleCallback, &robot));
// Packet callback to count packets recieved of different types
encoderPacketCount = 0;
encoderPacketCountPrevSec = 0;
encoderPacketTimer.setToNow();
robot.addPacketHandler(new ArGlobalRetFunctor1<bool, ArRobotPacket*>(&packetCallback), ArListPos::FIRST);
// Activate the wander action
if(!parser.checkArgument("nowander"))
{
ArLog::log(ArLog::Normal, "Beginning wandering actions. Send SIGUSR1 to deactivate wander.");
wanderGroup.activate();
ToggleActionGroupActive = true;
}
else
{
ArLog::log(ArLog::Normal, "Not activating wandering actions since -nowander option was given. Send SIGUSR1 to activate wander.");
wanderGroup.deactivate();
ToggleActionGroupActive = false;
}
// start the robot running, true means that if we lose robot connection the
// ArRobot runloop stops
robot.runAsync(true);
// Print data header
#define HEADFORMAT "%-24s %-5s %-16s %-5s %-6s %-6s %-16s %-8s %-8s %-8s %-8s %-8s %-8s %-10s %-10s %-5s %-5s %-5s %8s %s"
#define DATAFORMAT "%-24s %03.02f %-16s %-5s %-6s %-6s %-16s %-8d %-8d %-8g %-8g %-8s %-8s %-10lu %-10lu %-5s %-5s %-5d %6s " // doesn't include bumps details on end
printf("\n" HEADFORMAT "\n\n",
"Time",
"Volts",
"Flags",
"EStop",
"StallL",
"StallR",
"StallVal",
"#SIP/s",
"#Son/s",
"Vel L",
"Vel R",
"DigIns",
"DigOuts",
"Enc L",
"Enc R",
"IR L",
"IR R",
"#Enc/s",
"Chargestate",
"Cur Bumps, (Last Bump Pose)"
);
// Request that we will want encoder data
robot.requestEncoderPackets();
// Print data every second
char timestamp[24];
while(robot.isRunning()) {
robot.lock();
time_t t = time(NULL);
strftime(timestamp, 24, "%Y-%m-%d %H:%M:%S", localtime(&t));
printf( DATAFORMAT,
timestamp,
robot.getRealBatteryVoltage(),
int_as_bitstring(cumulativeRobotFlags).c_str(),
(wasEStopTriggered ? "YES" : " "),
(wasLeftMotorStalled?"YES":" "),
(wasRightMotorStalled?"YES":" "),
int_as_bitstring(cumulativeStallVal).c_str(),
robot.getMotorPacCount(),
robot.getSonarPacCount(),
robot.getLeftVel(),
robot.getRightVel(),
byte_as_bitstring(robot.getDigIn()).c_str(),
byte_as_bitstring(robot.getDigOut()).c_str(),
robot.getLeftEncoder(),
robot.getRightEncoder(),
wasLeftIRTriggered?"YES": " ",
wasRightIRTriggered?"YES":" ",
encoderPacketCountPrevSec,
charge_state_string(robot.getChargeState()).c_str()
);
// list indices of bumpers flaged in stallval
// skip the last bit which is a motor stall flag
ArTypes::UByte2 bumpmask = ArUtil::BIT15;
int bump = 0;
for(int bit = 16; bit > 0; bit--)
{
if(bit == 9) // this is also a motor stall bit
{
bumpmask = bumpmask >> 1;
bit--;
continue;
}
//printf("\n\tComparing stallval=%s to bumpmask=%s... ", int_as_bitstring(stallval).c_str(), int_as_bitstring(bumpmask).c_str());
if(cumulativeStallVal & bumpmask)
printf("%d ", bump);
bumpmask = bumpmask >> 1;
bump++;
}
// print pose of last bump sensor reading
const std::list<ArPoseWithTime*>* bumpsensed = bumpers.getCurrentBuffer();
if(bumpsensed)
{
//printf("%d readings. ", bumpsensed->size());
if(bumpsensed->size() > 0 && bumpsensed->front()) {
printf("(%.0f,%.0f)", bumpsensed->front()->getX(), bumpsensed->front()->getY());
}
}
puts(""); // newline
// clear events to accumulate for the next second
cumulativeRobotFlags = cumulativeStallVal = 0;
wasLeftMotorStalled = wasRightMotorStalled = wasLeftIRTriggered = wasRightIRTriggered = wasEStopTriggered = false;
robot.unlock();
ArUtil::sleep(1000);
}
// robot cycle stopped, probably because of lost robot connection
Aria::exit(0); // exit program
return 0;
}
|