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 "ArRobot.h"
00030 #include "ArBumpers.h"
00031
00040 ArBumpers::ArBumpers(size_t currentBufferSize, size_t cumulativeBufferSize,
00041 const char *name, int maxSecondsToKeepCurrent, double angleRange) :
00042 ArRangeDevice(currentBufferSize, cumulativeBufferSize, name, 5000, maxSecondsToKeepCurrent),
00043 myProcessCB(this, &ArBumpers::processReadings)
00044 {
00045 myBumpMask = (ArUtil::BIT1 | ArUtil::BIT2 | ArUtil::BIT3 | ArUtil::BIT4 |
00046 ArUtil::BIT5 | ArUtil::BIT6 | ArUtil::BIT7 | ArUtil::BIT8);
00047
00048 myAngleRange = angleRange;
00049
00050 setCurrentDrawingData(new ArDrawingData("polyDots", ArColor(0, 0, 0),
00051 120,
00052 83),
00053 true);
00054 }
00055
00056 ArBumpers::~ArBumpers()
00057 {
00058 if (myRobot != NULL)
00059 {
00060 myRobot->remSensorInterpTask(&myProcessCB);
00061 myRobot->remRangeDevice(this);
00062 }
00063 }
00064
00065 void ArBumpers::setRobot(ArRobot *robot)
00066 {
00067 myRobot = robot;
00068 if (myRobot != NULL)
00069 myRobot->addSensorInterpTask(myName.c_str(), 10, &myProcessCB);
00070 ArRangeDevice::setRobot(robot);
00071 }
00072
00076 void ArBumpers::processReadings(void)
00077 {
00078 int frontBump;
00079 int rearBump;
00080 int whichBumper;
00081
00082 if (myRobot->hasFrontBumpers())
00083 frontBump = ((myRobot->getStallValue() & 0xff00) >> 8) & myBumpMask;
00084 else
00085 {
00086 frontBump = 0;
00087 }
00088 if (myRobot->hasRearBumpers())
00089 rearBump = (myRobot->getStallValue() & 0xff) & myBumpMask;
00090 else
00091 {
00092 rearBump = 0;
00093 }
00094
00095 if(frontBump!= 0)
00096 {
00097 whichBumper = 1;
00098 addBumpToBuffer(frontBump, whichBumper);
00099 }
00100
00101 if(rearBump != 0)
00102 {
00103 whichBumper = 2;
00104 addBumpToBuffer(rearBump, whichBumper);
00105 }
00106
00107 }
00108
00113 void ArBumpers::addBumpToBuffer(int bumpValue, int whichBumper)
00114 {
00115 int numBumpers;
00116 double x;
00117 double y;
00118 double degree;
00119 double radius;
00120
00121 const ArRobotParams *params;
00122 params = myRobot->getRobotParams();
00123
00124 radius = params->getRobotRadius();
00125
00126 if(whichBumper == 1) numBumpers = myRobot->getNumFrontBumpers();
00127 else numBumpers = myRobot->getNumRearBumpers();
00128
00129 for (int i = 0; i < numBumpers; i++)
00130 {
00131 if((i == 0 && (bumpValue & ArUtil::BIT1)) ||
00132 (i == 1 && (bumpValue & ArUtil::BIT2)) ||
00133 (i == 2 && (bumpValue & ArUtil::BIT3)) ||
00134 (i == 3 && (bumpValue & ArUtil::BIT4)) ||
00135 (i == 4 && (bumpValue & ArUtil::BIT5)) ||
00136 (i == 5 && (bumpValue & ArUtil::BIT6)) ||
00137 (i == 6 && (bumpValue & ArUtil::BIT7)) ||
00138 (i == 7 && (bumpValue & ArUtil::BIT8)))
00139 {
00140 degree = -1 * (i * (myAngleRange / (double)numBumpers) +
00141 ((myAngleRange / (double)numBumpers) / 2) - (myAngleRange / 2));
00142
00143 if(whichBumper == 2) degree = degree + 180;
00144
00145 x = radius * ArMath::cos(degree);
00146 y = radius * ArMath::sin(degree);
00147
00148 ArPose pose;
00149 pose.setX(x);
00150 pose.setY(y);
00151
00152 ArTransform global = myRobot->getToGlobalTransform();
00153 pose = global.doTransform(pose);
00154
00155 myCurrentBuffer.addReading(pose.getX(), pose.getY());
00156 }
00157 }
00158 }