Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ArIrrfDevice.cpp

00001 /*
00002 ActivMedia Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004,2005 ActivMedia Robotics, LLC
00004 
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 ActivMedia Robotics for information about a commercial version of ARIA at 
00022 robots@activmedia.com or 
00023 ActivMedia Robotics, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 
00025 */
00026 
00027 #include "ArExport.h"
00028 
00029 #include "ariaOSDef.h"
00030 #include "ArIrrfDevice.h"
00031 #include "ArCommands.h"
00032 
00033 ArIrrfDevice::ArIrrfDevice(size_t currentBufferSize,
00034                              size_t cumulativeBufferSize, const char *name) :
00035   ArRangeDevice(currentBufferSize, cumulativeBufferSize, name, 5000),
00036   myPacketHandler(this, &ArIrrfDevice::packetHandler)
00037 {
00038   int i;
00039   myRobot = NULL;
00040   // These numbers haven't been proven, yet.  It will take experimentation to maximize the best results
00041   myCumulativeMaxRange = 10000;
00042   myMaxRange = 5000;
00043   myFilterFarDist = 7000;
00044   myFilterNearDist = 50;
00045 
00046   // The 91 readings start at -81 degrees, and move in 1.8 degree steps
00047   myRawReadings = new std::list<ArSensorReading *>;
00048   for(i=0;i<91;i++)
00049     myRawReadings->push_back(new ArSensorReading(0, 0, (1.8*i - 81)));
00050 }
00051 
00052 ArIrrfDevice::~ArIrrfDevice()
00053 {
00054   if (myRobot != NULL)
00055   {
00056     myRobot->remPacketHandler(&myPacketHandler);
00057     myRobot->remRangeDevice(this);
00058   }
00059 }
00060 
00061 void ArIrrfDevice::setRobot(ArRobot *robot)
00062 {
00063   myRobot = robot;
00064   if (myRobot != NULL)
00065     myRobot->addPacketHandler(&myPacketHandler, ArListPos::LAST);
00066 }
00067 
00068 void ArIrrfDevice::processReadings(void)
00069 {
00070   int i;
00071   double rx, ry, nx, ny, dx, dy, dist;
00072   ArSensorReading *reading;
00073   std::list<ArSensorReading *>::iterator rawIt;
00074   std::list<ArPoseWithTime *> *readingList;
00075   std::list<ArPoseWithTime *>::iterator readIt;
00076   lockDevice();
00077 
00078   rx = myRobot->getX();
00079   ry = myRobot->getY();
00080 
00081   i=0;
00082   for (rawIt = myRawReadings->begin();rawIt != myRawReadings->end();rawIt++)
00083   {
00084     reading = (*rawIt);
00085     nx = reading->getX();
00086     ny = reading->getY();
00087     dx = nx - rx;
00088     dy = nx - ry;
00089     dist = (dx*dx) + (dy*dy);
00090     if (!reading->isNew(myRobot->getCounter()))
00091       continue;
00092 
00093     if (dist < (myMaxRange * myMaxRange))
00094       myCurrentBuffer.addReading(nx, ny);
00095 
00096     if (dist < (myCumulativeMaxRange * myCumulativeMaxRange))
00097     {
00098       myCumulativeBuffer.beginInvalidationSweep();
00099       readingList = myCumulativeBuffer.getBuffer();
00100 
00101       if (readingList != NULL)
00102       {
00103         for (readIt = readingList->begin(); 
00104              readIt != readingList->end();
00105              readIt++)
00106         {
00107           dx = (*readIt)->getX() - nx;
00108           dy = (*readIt)->getY() - ny;
00109           if ((dx*dx + dy*dy) < (myFilterNearDist * myFilterNearDist))
00110             myCumulativeBuffer.invalidateReading(readIt);
00111         }
00112       }
00113       myCumulativeBuffer.endInvalidationSweep();
00114       myCumulativeBuffer.addReading(nx, ny);
00115     }
00116   }
00117  
00118   readingList = myCumulativeBuffer.getBuffer();
00119 
00120   rx = myRobot->getX();
00121   ry = myRobot->getY();
00122 
00123   myCumulativeBuffer.beginInvalidationSweep();
00124   if (readingList != NULL)
00125   {
00126     for (readIt = readingList->begin(); readIt != readingList->end();readIt++)
00127     {
00128       dx = (*readIt)->getX() - rx;
00129       dy = (*readIt)->getY() - ry;
00130       if ((dx*dx + dy*dy) > (myFilterFarDist * myFilterFarDist))
00131         myCumulativeBuffer.invalidateReading(readIt);
00132     }
00133   }
00134   myCumulativeBuffer.endInvalidationSweep();
00135 
00136   unlockDevice();
00137 }
00138 
00145 bool ArIrrfDevice::packetHandler(ArRobotPacket *packet)
00146 {
00147   int portNum, i, dist, packetCounter;
00148   double conv;
00149   ArTransform packetTrans;
00150   std::list<ArSensorReading *>::iterator it;
00151   ArSensorReading *reading;
00152   ArPose pose;
00153   ArTransform encoderTrans;
00154   ArPose encoderPose;
00155 
00156   pose = myRobot->getPose();
00157   conv = 2.88;
00158 
00159   packetTrans.setTransform(pose);
00160   packetCounter = myRobot->getCounter();
00161 
00162   if (packet->getID() != 0x10)
00163     return false;
00164 
00165   // Which Aux port the IRRF is connected to
00166   portNum = packet->bufToByte2();
00167   encoderTrans = myRobot->getEncoderTransform();
00168   encoderPose = encoderTrans.doInvTransform(pose);
00169 
00170   i = 0;
00171   for (i=0, it = myRawReadings->begin();it != myRawReadings->end();it++, i++)
00172   {
00173     reading = (*it);
00174     dist = (int) ((packet->bufToUByte2()) / conv);
00175     reading->newData(dist, pose, encoderPose, packetTrans, packetCounter, packet->getTimeReceived());
00176   }
00177 
00178   myLastReading.setToNow();
00179 
00180   processReadings();
00181 
00182   return true;
00183 }

Generated on Wed Oct 19 12:56:35 2005 for Aria by  doxygen 1.4.0