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

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

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