Aria  2.8.0
imuExample.cpp

Example of gathering IMU data from SeekurSample program that demonstrates how to get IMU data from an IMU on
a Seekur-class robot (from SeekurOS). Only some Seekurs have IMUs.

[Some Pioneers also have gyros that are integrated into the robot and normally automatically correct data or which can optionally return the data seperately (using different packets and different ARIA interface in ArAnalogGyro).]

The data comes back in a custom packet with ID 0x9A. It's requested with client command 26.

Press escape to exit the program.

/*
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 <time.h>
bool imuPacketHandler(ArRobotPacket* packet) {
if (packet->getID() != 0x9a) {
//ArLog::log(ArLog::Normal, "ignoring packet with id: %d", packet->getID());
return false;
}
int timeSince = packet->bufToByte();
int numPerAxis = packet->bufToByte();
ArLog::log(ArLog::Normal, "got IMUpac, %dms since readings took place, %d readings per axis", timeSince, numPerAxis);
int i;
int range;
double x = 0.0;
double y = 0.0;
double z = 0.0;
double multiplier = 0.0;
double offset = 0.0;
int numGyroAxes = packet->bufToByte();
for (i=1; i <= numPerAxis; i++) {
x = 0.0;
y = 0.0;
z = 0.0;
range = packet->bufToByte();
if (range == 3) {
multiplier = 0.07326;
offset = 0.0;
}
else if (range == 2) {
multiplier = 0.03663;
offset = 0.0;
}
else if (range == 1) {
multiplier = 0.01832;
offset = 0.0;
}
if (numGyroAxes >= 1) {
x = (packet->bufToByte2() * multiplier) + offset;
}
if (numGyroAxes >= 2) {
y = (packet->bufToByte2() * multiplier) + offset;
}
if (numGyroAxes >= 3) {
z = (packet->bufToByte2() * multiplier) + offset;
}
ArLog::log(ArLog::Normal, "gyro: reading %d of %d, x: %10.4f deg/s, y: %10.4f deg/s, z: %10.4f deg/s",
i, numPerAxis, x, y, z);
}
int numAccelAxes = packet->bufToByte();
for (i=1; i <= numPerAxis; i++) {
x = 0.0;
y = 0.0;
z = 0.0;
multiplier = 0.002522 * 9806.65;
offset = 0.0;
if (numAccelAxes >= 1) {
x = (packet->bufToByte2() * multiplier) + offset;
}
if (numAccelAxes >= 2) {
y = (packet->bufToByte2() * multiplier) + offset;
}
if (numAccelAxes >= 3) {
z = (packet->bufToByte2() * multiplier) + offset;
}
ArLog::log(ArLog::Normal, "accel: reading %d of %d, x: %10.4f mg, y: %10.4f mg, z: %10.4f mg",
i, numPerAxis, x, y, z);
}
int numTemperatureAxes = packet->bufToByte();
for (i=1; i <= numPerAxis; i++) {
x = 0.0;
y = 0.0;
z = 0.0;
multiplier = 0.1453;
offset = 25.0;
if (numTemperatureAxes >= 1) {
x = (packet->bufToByte2() * multiplier) + offset;
}
if (numTemperatureAxes >= 2) {
y = (packet->bufToByte2() * multiplier) + offset;
}
if (numTemperatureAxes >= 3) {
z = (packet->bufToByte2() * multiplier) + offset;
}
ArLog::log(ArLog::Normal, "temperature: reading %d of %d, x: %10.4f degC, y: %10.4f degC, z: %10.4f degC",
i, numPerAxis, x, y, z);
}
return true;
}
int main(int argc, char **argv) {
ArArgumentParser parser(&argc, argv);
ArRobot *robot = new ArRobot;
ArSimpleConnector simpleConnector(&parser);
ArKeyHandler keyHandler;
Aria::setKeyHandler(&keyHandler);
robot->attachKeyHandler(&keyHandler);
parser.loadDefaultArguments();
if (!simpleConnector.parseArgs() || !parser.checkHelpAndWarnUnparsed(1)) {
simpleConnector.logOptions();
keyHandler.restore();
}
if (!simpleConnector.connectRobot(robot)) {
ArLog::log(ArLog::Terse, "Error connecting to robot.");
keyHandler.restore();
}
ArLog::log(ArLog::Normal, "Connected to robot.");
ArGlobalRetFunctor1<bool, ArRobotPacket *> myImuPacketHandler(&imuPacketHandler);
robot->addPacketHandler(&myImuPacketHandler, ArListPos::FIRST);
robot->runAsync(true);
robot->lock();
robot->comInt(26, 2); // request IMU packets continuously
ArLog::log(ArLog::Normal, "Requested IMU packets, waiting...");
robot->unlock();
robot->waitForRunExit();
exit(0);
}
Aria::shutdown
static void shutdown()
Shutdown all Aria processes/threads.
Definition: Aria.cpp:332
ArKeyHandler
Perform actions when keyboard keys are pressed.
Definition: ArKeyHandler.h:65
ArLog::Terse
Use terse logging.
Definition: ArLog.h:61
ArRobotPacket::getID
ArTypes::UByte getID(void)
returns the ID of the packet
Definition: ArRobotPacket.cpp:81
ArSimpleConnector
Legacy connector for robot and laser.
Definition: ArSimpleConnector.h:51
ArRobot::comInt
bool comInt(unsigned char command, short int argument)
Sends a command to the robot with an int for argument.
Definition: ArRobot.cpp:5634
ArRobot::addPacketHandler
void addPacketHandler(ArRetFunctor1< bool, ArRobotPacket * > *functor, ArListPos::Pos position=ArListPos::LAST)
Adds a packet handler to the list of packet handlers.
Definition: ArRobot.cpp:2613
Aria::setKeyHandler
static void setKeyHandler(ArKeyHandler *keyHandler)
Sets the key handler, so that other classes can find it using getKeyHandler()
Definition: Aria.cpp:624
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
ArBasePacket::bufToByte2
virtual ArTypes::Byte2 bufToByte2(void)
Gets a ArTypes::Byte2 from the buffer.
Definition: ArBasePacket.cpp:456
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
ArLog::StdOut
Use stdout for logging.
Definition: ArLog.h:54
ArRobot
Central class for communicating with and operating the robot.
Definition: ArRobot.h:82
ArBasePacket::bufToByte
virtual ArTypes::Byte bufToByte(void)
Gets a ArTypes::Byte from the buffer.
Definition: ArBasePacket.cpp:443
ArArgumentParser
Parse and store program command-line arguments for use by other ARIA classes.
Definition: ArArgumentParser.h:64
ArLog::init
static bool init(LogType type, LogLevel level, const char *fileName="", bool logTime=false, bool alsoPrint=false, bool printThisCall=true)
Initialize the logging utility with options.
Definition: ArLog.cpp:437
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
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
ArListPos::FIRST
place item first in the list
Definition: ariaTypedefs.h:70
ArRobot::lock
int lock()
Lock the robot instance.
Definition: ArRobot.h:1268
ArGlobalRetFunctor1
Functor for a global function with 1 parameter and return value.
Definition: ArFunctor.h:1270
ArRobot::attachKeyHandler
void attachKeyHandler(ArKeyHandler *keyHandler, bool exitOnEscape=true, bool useExitNotShutdown=true)
Attachs a key handler.
Definition: ArRobot.cpp:6641
ArRobotPacket
Represents the packets sent to the robot as well as those received from it.
Definition: ArRobotPacket.h:41
ArKeyHandler::restore
void restore(void)
Sets stdin back to its original settings, if its been restored it won't read anymore.
Definition: ArKeyHandler.cpp:117
ArRobot::waitForRunExit
WaitState waitForRunExit(unsigned int msecs=0)
Suspend calling thread until the ArRobot run loop has exited.
Definition: ArRobot.cpp:2923