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

ArSonarDevice.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
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 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "ArExport.h"
00027 #include "ariaOSDef.h"
00028 #include "ArSonarDevice.h"
00029 #include "ArSensorReading.h"
00030 #include "ArRobot.h"
00031 
00032 AREXPORT ArSonarDevice::ArSonarDevice(size_t currentBufferSize,
00033                  size_t cumulativeBufferSize, const char *name) :
00034   ArRangeDevice(currentBufferSize, cumulativeBufferSize, name, 5000), 
00035   myProcessCB(this, &ArSonarDevice::processReadings)
00036 {
00037   setMaxDistToKeepCumulative(3000); 
00038   myFilterNearDist = 50;    // 50 mm between cumulative readings, at least
00039   myFilterFarDist = 3000;       // throw out cumulative readings this far
00040                                 // from robot
00041   setMaxSecondsToKeepCurrent(5);
00042   setMaxSecondsToKeepCumulative(15);
00043   setCurrentDrawingData(new ArDrawingData("polyArrows", 
00044                                           ArColor(0x33, 0xCC, 0xFF), 
00045                                           200,  // mm length of arrow
00046                                           70),  // first sensor layer
00047                         true);
00048 }
00049 
00050 AREXPORT ArSonarDevice::~ArSonarDevice()
00051 {
00052   if (myRobot != NULL)
00053   {
00054     myRobot->remSensorInterpTask(&myProcessCB);
00055     myRobot->remRangeDevice(this);
00056   }
00057 }
00058 
00059 AREXPORT void ArSonarDevice::setRobot(ArRobot *robot)
00060 {
00061   myRobot = robot;
00062   if (myRobot != NULL)
00063     myRobot->addSensorInterpTask(myName.c_str(), 10, &myProcessCB);
00064   ArRangeDevice::setRobot(robot);
00065 }
00066 
00067 AREXPORT void ArSonarDevice::processReadings(void)
00068 {
00069   int i;
00070   ArSensorReading *reading;
00071   lockDevice();
00072 
00073   for (i = 0; i < myRobot->getNumSonar(); i++)
00074   {
00075     reading = myRobot->getSonarReading(i);
00076     if (reading == NULL || !reading->isNew(myRobot->getCounter()))
00077       continue;
00078     addReading(reading->getX(), reading->getY());
00079   }
00080 
00081   // delete too-far readings
00082   std::list<ArPoseWithTime *> *readingList;
00083   std::list<ArPoseWithTime *>::iterator it;
00084   double dx, dy, rx, ry;
00085     
00086   myCumulativeBuffer.beginInvalidationSweep();
00087   readingList = myCumulativeBuffer.getBuffer();
00088   rx = myRobot->getX();
00089   ry = myRobot->getY();
00090   // walk through the list and see if this makes any old readings bad
00091   if (readingList != NULL)
00092     {
00093       for (it = readingList->begin(); it != readingList->end(); ++it)
00094     {
00095       dx = (*it)->getX() - rx;
00096       dy = (*it)->getY() - ry;
00097       if ((dx*dx + dy*dy) > (myFilterFarDist * myFilterFarDist)) 
00098         myCumulativeBuffer.invalidateReading(it);
00099     }
00100     }
00101   myCumulativeBuffer.endInvalidationSweep();
00102   // leave this unlock here or the world WILL end
00103   unlockDevice();
00104 }
00105 
00115 AREXPORT void ArSonarDevice::addReading(double x, double y)
00116 {
00117   double rx = myRobot->getX();
00118   double ry = myRobot->getY();
00119   double dx = x - rx;       
00120   double dy = y - ry;
00121   double dist2 = dx*dx + dy*dy;
00122   
00123   if (dist2 < myMaxRange*myMaxRange)
00124     myCurrentBuffer.addReading(x,y);
00125   
00126   if (dist2 < myMaxDistToKeepCumulative * myMaxDistToKeepCumulative)
00127     {
00128       std::list<ArPoseWithTime *> *readingList;
00129       std::list<ArPoseWithTime *>::iterator it;
00130 
00131       myCumulativeBuffer.beginInvalidationSweep();
00132 
00133       readingList = myCumulativeBuffer.getBuffer();
00134       // walk through the list and see if this makes any old readings bad
00135       if (readingList != NULL)
00136     {
00137       for (it = readingList->begin(); it != readingList->end(); ++it)
00138         {
00139           dx = (*it)->getX() - x;
00140           dy = (*it)->getY() - y;
00141           if ((dx*dx + dy*dy) < (myFilterNearDist * myFilterNearDist)) 
00142         myCumulativeBuffer.invalidateReading(it);
00143         }
00144     }
00145       myCumulativeBuffer.endInvalidationSweep();
00146 
00147       myCumulativeBuffer.addReading(x,y);
00148     }
00149 }

Generated on Tue Feb 20 10:51:41 2007 for Aria by  doxygen 1.4.0