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
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
00041 myCumulativeMaxRange = 10000;
00042 myMaxRange = 5000;
00043 myFilterFarDist = 7000;
00044 myFilterNearDist = 50;
00045
00046
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
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 }