00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "ArExport.h"
00028 #include "ariaOSDef.h"
00029 #include "ArRangeDevice.h"
00030 #include "ArRobot.h"
00031
00055 ArRangeDevice::ArRangeDevice(size_t currentBufferSize,
00056 size_t cumulativeBufferSize,
00057 const char *name,
00058 unsigned int maxRange,
00059 int maxSecondsToKeepCurrent,
00060 int maxSecondsToKeepCumulative,
00061 double maxDistToKeepCumulative) :
00062 myCurrentBuffer(currentBufferSize),
00063 myCumulativeBuffer(cumulativeBufferSize),
00064 myFilterCB(this, &ArRangeDevice::filterCallback)
00065 {
00066 myRobot = NULL;
00067 myName = name;
00068 myMaxRange = maxRange;
00069 myRawReadings = NULL;
00070 setMaxSecondsToKeepCurrent(maxSecondsToKeepCurrent);
00071 setMaxSecondsToKeepCumulative(maxSecondsToKeepCumulative);
00072 setMaxDistToKeepCumulative(maxDistToKeepCumulative);
00073 myCurrentDrawingData = NULL;
00074 myOwnCurrentDrawingData = false;
00075 myCumulativeDrawingData = NULL;
00076 myOwnCumulativeDrawingData = false;
00077 }
00078
00079 ArRangeDevice::~ArRangeDevice()
00080 {
00081 if (myRobot != NULL)
00082 myRobot->remSensorInterpTask(&myFilterCB);
00083 if (myCurrentDrawingData != NULL && myOwnCurrentDrawingData)
00084 {
00085 delete myCurrentDrawingData;
00086 myCurrentDrawingData = NULL;
00087 myOwnCurrentDrawingData = false;
00088 }
00089 if (myCumulativeDrawingData != NULL && myOwnCumulativeDrawingData)
00090 {
00091 delete myCumulativeDrawingData;
00092 myCumulativeDrawingData = NULL;
00093 myOwnCumulativeDrawingData = false;
00094 }
00095 }
00096
00097
00098 const char * ArRangeDevice::getName(void) const
00099 {
00100 return myName.c_str();
00101 }
00102
00103 void ArRangeDevice::setRobot(ArRobot *robot)
00104 {
00105 char buf[512];
00106 sprintf(buf, "filter %s", getName());
00107
00108 if (myRobot != NULL)
00109 myRobot->remSensorInterpTask(&myFilterCB);
00110
00111 myRobot = robot;
00112
00113 if (myRobot != NULL)
00114 myRobot->addSensorInterpTask(buf, 100, &myFilterCB);
00115 }
00116
00117 ArRobot *ArRangeDevice::getRobot(void)
00118 {
00119 return myRobot;
00120 }
00121
00122 void ArRangeDevice::filterCallback(void)
00123 {
00124 std::list<ArPoseWithTime *>::iterator it;
00125
00126 lockDevice();
00127
00128 if (myMaxSecondsToKeepCurrent > 0 &&
00129 myCurrentBuffer.getSize() > 0)
00130 {
00131
00132 myCurrentBuffer.beginInvalidationSweep();
00133 for (it = getCurrentBuffer()->begin();
00134 it != getCurrentBuffer()->end();
00135 ++it)
00136 {
00137 if ((*it)->getTime().secSince() >= myMaxSecondsToKeepCurrent)
00138 myCurrentBuffer.invalidateReading(it);
00139 }
00140 myCurrentBuffer.endInvalidationSweep();
00141 }
00142
00143
00144 bool doingDist = true;
00145 bool doingAge = true;
00146
00147 if (myCumulativeBuffer.getSize() == 0)
00148 {
00149 unlockDevice();
00150 return;
00151 }
00152
00153 double squaredFarDist = (myMaxDistToKeepCumulative *
00154 myMaxDistToKeepCumulative);
00155
00156 if (squaredFarDist < 1)
00157 doingDist = false;
00158 if (myMaxSecondsToKeepCumulative <= 0)
00159 doingAge = false;
00160
00161 if (!doingDist && !doingAge)
00162 {
00163 unlockDevice();
00164 return;
00165 }
00166
00167
00168 myCumulativeBuffer.beginInvalidationSweep();
00169 for (it = getCumulativeBuffer()->begin();
00170 it != getCumulativeBuffer()->end();
00171 ++it)
00172 {
00173
00174 if (doingDist &&
00175 myRobot->getPose().squaredFindDistanceTo(*(*it)) > squaredFarDist)
00176 myCumulativeBuffer.invalidateReading(it);
00177 else if (doingAge &&
00178 (*it)->getTime().secSince() >= myMaxSecondsToKeepCumulative)
00179 myCumulativeBuffer.invalidateReading(it);
00180 }
00181 myCumulativeBuffer.endInvalidationSweep();
00182 unlockDevice();
00183 }
00184
00191 void ArRangeDevice::setCurrentBufferSize(size_t size)
00192 {
00193 myCurrentBuffer.setSize(size);
00194 }
00195
00202 void ArRangeDevice::setCumulativeBufferSize(size_t size)
00203 {
00204 myCumulativeBuffer.setSize(size);
00205 }
00206
00207 void ArRangeDevice::addReading(double x, double y)
00208 {
00209 myCurrentBuffer.addReading(x, y);
00210 myCumulativeBuffer.addReading(x, y);
00211 }
00212
00230 double ArRangeDevice::currentReadingPolar(double startAngle,
00231 double endAngle,
00232 double *angle) const
00233 {
00234 ArPose pose;
00235 if (myRobot != NULL)
00236 pose = myRobot->getPose();
00237 else
00238 {
00239 ArLog::log(ArLog::Normal, "ArRangeDevice %s: NULL robot, won't get polar reading correctly", getName());
00240 pose.setPose(0, 0);
00241 }
00242 return myCurrentBuffer.getClosestPolar(startAngle, endAngle,
00243 pose,
00244 myMaxRange,
00245 angle);
00246 }
00247
00265 double ArRangeDevice::cumulativeReadingPolar(double startAngle,
00266 double endAngle,
00267 double *angle) const
00268 {
00269 ArPose pose;
00270 if (myRobot != NULL)
00271 pose = myRobot->getPose();
00272 else
00273 {
00274 ArLog::log(ArLog::Normal, "ArRangeDevice %s: NULL robot, won't get polar reading correctly", getName());
00275 pose.setPose(0, 0);
00276 }
00277 return myCumulativeBuffer.getClosestPolar(startAngle, endAngle,
00278 pose,
00279 myMaxRange,
00280 angle);
00281 }
00282
00296 double ArRangeDevice::currentReadingBox(double x1, double y1,
00297 double x2, double y2,
00298 ArPose *pose) const
00299 {
00300 ArPose robotPose;
00301 if (myRobot != NULL)
00302 robotPose = myRobot->getPose();
00303 else
00304 {
00305 ArLog::log(ArLog::Normal, "ArRangeDevice %s: NULL robot, won't get reading box correctly", getName());
00306 robotPose.setPose(0, 0);
00307 }
00308 return myCurrentBuffer.getClosestBox(x1, y1, x2, y2, robotPose,
00309 myMaxRange, pose);
00310 }
00311
00325 double ArRangeDevice::cumulativeReadingBox(double x1, double y1,
00326 double x2, double y2,
00327 ArPose *pose) const
00328 {
00329 ArPose robotPose;
00330 if (myRobot != NULL)
00331 robotPose = myRobot->getPose();
00332 else
00333 {
00334 ArLog::log(ArLog::Normal, "ArRangeDevice %s: NULL robot, won't get reading box correctly", getName());
00335 robotPose.setPose(0, 0);
00336 }
00337 return myCumulativeBuffer.getClosestBox(x1, y1, x2, y2,
00338 robotPose,
00339 myMaxRange, pose);
00340 }
00341
00348 void ArRangeDevice::applyTransform(ArTransform trans,
00349 bool doCumulative)
00350 {
00351 myCurrentBuffer.applyTransform(trans);
00352 if (doCumulative)
00353 myCumulativeBuffer.applyTransform(trans);
00354 }
00355
00356 std::vector<ArSensorReading> *ArRangeDevice::getRawReadingsAsVector(void)
00357 {
00358
00359 std::list<ArSensorReading *>::const_iterator it;
00360 myRawReadingsVector.clear();
00361
00362 if (myRawReadings == NULL)
00363 return &myRawReadingsVector;
00364 myRawReadingsVector.reserve(myRawReadings->size());
00365 for (it = myRawReadings->begin(); it != myRawReadings->end(); it++)
00366 myRawReadingsVector.insert(myRawReadingsVector.begin(), *(*it));
00367 return &myRawReadingsVector;
00368 }
00369
00370
00371 void ArRangeDevice::setCurrentDrawingData(ArDrawingData *data,
00372 bool takeOwnershipOfData)
00373 {
00374 if (myCurrentDrawingData != NULL && myOwnCurrentDrawingData)
00375 {
00376 delete myCurrentDrawingData;
00377 myCurrentDrawingData = NULL;
00378 myOwnCurrentDrawingData = false;
00379 }
00380 myCurrentDrawingData = data;
00381 myOwnCurrentDrawingData = takeOwnershipOfData;
00382 }
00383
00384 void ArRangeDevice::setCumulativeDrawingData(ArDrawingData *data,
00385 bool takeOwnershipOfData)
00386 {
00387 if (myCumulativeDrawingData != NULL && myOwnCumulativeDrawingData)
00388 {
00389 delete myCumulativeDrawingData;
00390 myCumulativeDrawingData = NULL;
00391 myOwnCumulativeDrawingData = false;
00392 }
00393 myCumulativeDrawingData = data;
00394 myOwnCumulativeDrawingData = takeOwnershipOfData;
00395 }