00001 #include <vector> 00002 #include "Image/Point2D.H" 00003 #include "LocalizationParticle.h" 00004 #include "LocalizationMap.h" 00005 #include "Ice/RobotSimEvents.ice.H" 00006 //#include "LocalizationSensorReaderMessage.h" 00007 #include <iostream> 00008 00009 using namespace std; 00010 00011 #ifndef LOCALIZATIONSENSORREADER_H_ 00012 #define LOCALIZATIONSENSORREADER_H_ 00013 00014 namespace VirtualSensorMessage 00015 { 00016 class VirtualSensorMessage 00017 { 00018 public: 00019 00020 }; 00021 00022 class IMUDataServerMessage : public VirtualSensorMessage 00023 { 00024 public: 00025 vector<float> orientation; 00026 IMUDataServerMessage() 00027 { 00028 // 00029 } 00030 }; 00031 00032 class PipeColorSegmentMessage : public VirtualSensorMessage 00033 { 00034 public: 00035 bool pipeIsVisible; 00036 PipeColorSegmentMessage() 00037 { 00038 // 00039 } 00040 }; 00041 00042 class VisionRectangleMessage : public VirtualSensorMessage 00043 { 00044 public: 00045 bool rectangleIsVisible; 00046 int rectangleRelOrientation; 00047 VisionRectangleMessage() 00048 { 00049 // 00050 } 00051 }; 00052 00053 class BuoyColorSegmentMessage : public VirtualSensorMessage 00054 { 00055 public: 00056 BuoyColorSegmentMessage() 00057 { 00058 // 00059 } 00060 }; 00061 } 00062 00063 //used to convert sensor data into particle weights 00064 class LocalizationSensorReader 00065 { 00066 public: 00067 00068 struct SensorType 00069 { 00070 const static int compass = 0; 00071 const static int hydrophone = 1; 00072 const static int detector_buoy = 2; 00073 const static int detector_bin = 3; 00074 const static int detector_pipe = 4; 00075 00076 int mId; 00077 }; 00078 00079 struct Flags 00080 { 00081 const static float voteFailed = -1; 00082 }; 00083 00084 LocalizationSensorReader() 00085 { 00086 inited = false; 00087 } 00088 00089 ~LocalizationSensorReader() 00090 { 00091 delete mVirtualMsg; 00092 } 00093 00094 void takeReading(const RobotSimEvents::EventMessagePtr& realMsg) 00095 { 00096 mRealMsg = realMsg; 00097 calculateInitialOffset(); 00098 inited = true; 00099 } 00100 00101 void takeVirtualReading(VirtualSensorMessage::VirtualSensorMessage * virtualMsg) 00102 { 00103 mVirtualMsg = virtualMsg; 00104 } 00105 00106 virtual void calculateInitialOffset() = 0; 00107 00108 virtual float getScaledVote() = 0; 00109 00110 static VirtualSensorMessage::VirtualSensorMessage * generatetVirtualSensorMessage(LocalizationParticle & part, LocalizationMap & map); 00111 00112 float mScale; 00113 RobotSimEvents::EventMessagePtr mRealMsg; 00114 VirtualSensorMessage::VirtualSensorMessage * mVirtualMsg; 00115 float mVote; 00116 float mScaledVote; //value calculated from last reading 00117 int mSensorType; 00118 00119 bool inited; 00120 }; 00121 00122 class BuoySensor: public LocalizationSensorReader 00123 { 00124 public: 00125 BuoySensor() 00126 { 00127 mSensorType = LocalizationSensorReader::SensorType::detector_buoy; 00128 } 00129 00130 BuoySensor(float scale) 00131 { 00132 mSensorType = LocalizationSensorReader::SensorType::detector_buoy; 00133 mScale = scale; 00134 } 00135 00136 virtual void calculateInitialOffset() 00137 { 00138 // 00139 } 00140 00141 virtual float getScaledVote() 00142 { 00143 if (mRealMsg->ice_isA("::RobotSimEvents::PipeColorSegmentMessage")) 00144 { 00145 RobotSimEvents::BuoyColorSegmentMessagePtr realMsg = RobotSimEvents::BuoyColorSegmentMessagePtr::dynamicCast(mRealMsg); 00146 VirtualSensorMessage::BuoyColorSegmentMessage * virtualMsg = static_cast<VirtualSensorMessage::BuoyColorSegmentMessage*>(mVirtualMsg); 00147 mVote = 1.0f; 00148 mScaledVote = mVote * mScale; 00149 return mScaledVote; 00150 } 00151 return Flags::voteFailed; 00152 } 00153 00154 static VirtualSensorMessage::VirtualSensorMessage * generatetVirtualSensorMessage(LocalizationParticle & part, LocalizationMap & map) 00155 { 00156 VirtualSensorMessage::BuoyColorSegmentMessage * msg = new VirtualSensorMessage::BuoyColorSegmentMessage; 00157 return msg; 00158 } 00159 }; 00160 00161 class CompassSensor: public LocalizationSensorReader 00162 { 00163 public: 00164 float mInitialOffset; 00165 CompassSensor() 00166 { 00167 mSensorType = LocalizationSensorReader::SensorType::compass; 00168 mInitialOffset = 0; 00169 } 00170 00171 CompassSensor(float scale) 00172 { 00173 mSensorType = LocalizationSensorReader::SensorType::compass; 00174 mInitialOffset = 0; 00175 mScale = scale; 00176 } 00177 00178 virtual void calculateInitialOffset() 00179 { 00180 RobotSimEvents::IMUDataServerMessagePtr realMsg = RobotSimEvents::IMUDataServerMessagePtr::dynamicCast(mRealMsg); 00181 mInitialOffset = realMsg->orientation[2]; 00182 } 00183 00184 virtual float getScaledVote() 00185 { 00186 if (mRealMsg->ice_isA("::RobotSimEvents::IMUDataServerMessage")) 00187 { 00188 float realOrientation = 0.0f; 00189 float virtualOrientation = 0.0f; 00190 RobotSimEvents::IMUDataServerMessagePtr realMsg = RobotSimEvents::IMUDataServerMessagePtr::dynamicCast(mRealMsg); 00191 VirtualSensorMessage::IMUDataServerMessage * virtualMsg = static_cast<VirtualSensorMessage::IMUDataServerMessage*>(mVirtualMsg); 00192 realOrientation = realMsg->orientation[2] - mInitialOffset; 00193 virtualOrientation = virtualMsg->orientation[2]; 00194 mVote = 1.0f - LocalizationUtil::linearAngleDiffRatio(realOrientation, virtualOrientation); 00195 mScaledVote = mVote * mScale; 00196 return mScaledVote; 00197 } 00198 return Flags::voteFailed; 00199 } 00200 00201 static VirtualSensorMessage::VirtualSensorMessage * generatetVirtualSensorMessage(LocalizationParticle & part, LocalizationMap & map) 00202 { 00203 VirtualSensorMessage::IMUDataServerMessage * msg = new VirtualSensorMessage::IMUDataServerMessage; 00204 msg->orientation.resize(3); 00205 msg->orientation[2] = LocalizationUtil::polarToEuler(part.mState.mAngle); // conventional -> euler 00206 return msg; 00207 } 00208 }; 00209 00210 class PipeSensor: public LocalizationSensorReader 00211 { 00212 public: 00213 PipeSensor() 00214 { 00215 mSensorType = LocalizationSensorReader::SensorType::detector_pipe; 00216 } 00217 00218 PipeSensor(float scale) 00219 { 00220 mSensorType = LocalizationSensorReader::SensorType::detector_pipe; 00221 mScale = scale; 00222 } 00223 00224 virtual void calculateInitialOffset() 00225 { 00226 // 00227 } 00228 00229 virtual float getScaledVote() 00230 { 00231 if (mRealMsg->ice_isA("::RobotSimEvents::PipeColorSegmentMessage")) 00232 { 00233 mVote = 0.0f; 00234 RobotSimEvents::PipeColorSegmentMessagePtr realMsg = RobotSimEvents::PipeColorSegmentMessagePtr::dynamicCast(mRealMsg); 00235 VirtualSensorMessage::PipeColorSegmentMessage * virtualMsg = static_cast<VirtualSensorMessage::PipeColorSegmentMessage*>(mVirtualMsg); 00236 if(virtualMsg->pipeIsVisible && realMsg->size > 1500) //make sure we see a decent size orange blob 00237 { 00238 mVote += realMsg->size / (2.0f * 8000.0f); 00239 if(mVote > 0.5f) 00240 mVote = 0.5f; 00241 } 00242 mVote += 0.5f; 00243 mScaledVote = mVote * mScale; 00244 return mScaledVote; 00245 } 00246 return Flags::voteFailed; 00247 } 00248 00249 static VirtualSensorMessage::VirtualSensorMessage * generatetVirtualSensorMessage(LocalizationParticle & part, LocalizationMap & map) 00250 { 00251 VirtualSensorMessage::PipeColorSegmentMessage * msg = new VirtualSensorMessage::PipeColorSegmentMessage; 00252 msg->pipeIsVisible = false; 00253 for(unsigned int i = 0; i < map.mMapEntities.size(); i ++) 00254 { 00255 if(map.mMapEntities[i].mObjectType == LocalizationMapEntity::ObjectType::pipe) 00256 { 00257 float distToPipe = sqrt(pow(part.mState.mPoint.i - map.mMapEntities[i].mCenter.i, 2) + pow(part.mState.mPoint.j - map.mMapEntities[i].mCenter.j, 2)); 00258 float largerLength = map.mMapEntities[i].mDim.i; 00259 if(map.mMapEntities[i].mDim.j > largerLength) 00260 { 00261 largerLength = map.mMapEntities[i].mDim.j; 00262 } 00263 if(distToPipe <= largerLength / 2) 00264 { 00265 msg->pipeIsVisible = true; 00266 } 00267 } 00268 } 00269 return msg; 00270 } 00271 }; 00272 00273 class RectangleSensor: public LocalizationSensorReader 00274 { 00275 public: 00276 RectangleSensor() 00277 { 00278 mSensorType = LocalizationSensorReader::SensorType::detector_pipe; 00279 } 00280 00281 RectangleSensor(float scale) 00282 { 00283 mSensorType = LocalizationSensorReader::SensorType::detector_pipe; 00284 mScale = scale; 00285 } 00286 00287 virtual void calculateInitialOffset() 00288 { 00289 // 00290 } 00291 00292 virtual float getScaledVote() 00293 { 00294 if (mRealMsg->ice_isA("::RobotSimEvents::VisionRectangleMessage")) 00295 { 00296 RobotSimEvents::VisionRectangleMessagePtr realMsg = RobotSimEvents::VisionRectangleMessagePtr::dynamicCast(mRealMsg); 00297 VirtualSensorMessage::VisionRectangleMessage * virtualMsg = static_cast<VirtualSensorMessage::VisionRectangleMessage*>(mVirtualMsg); 00298 int realOrientation = 0; //fuck it I'm just gonna average all of them 00299 for(int i = 0; i < realMsg->quads.size(); i ++) 00300 { 00301 realOrientation += realMsg->quads[i].angle; 00302 } 00303 realOrientation /= realMsg->quads.size(); 00304 if(virtualMsg->rectangleIsVisible) 00305 { 00306 mVote = 1 - 0.5 * LocalizationUtil::linearAngleDiffRatio(realOrientation, virtualMsg->rectangleRelOrientation); 00307 } 00308 else 00309 { 00310 mVote = 0.25f; 00311 } 00312 mScaledVote = mVote * mScale; 00313 return mScaledVote; 00314 } 00315 return Flags::voteFailed; 00316 } 00317 00318 static VirtualSensorMessage::VirtualSensorMessage * generatetVirtualSensorMessage(LocalizationParticle & part, LocalizationMap & map) 00319 { 00320 VirtualSensorMessage::VisionRectangleMessage * msg = new VirtualSensorMessage::VisionRectangleMessage; 00321 msg->rectangleIsVisible = false; 00322 int closestRectIndex = -1; 00323 float closestRectDist = -1; 00324 for(unsigned int i = 0; i < map.mMapEntities.size(); i ++) 00325 { 00326 if((map.mMapEntities[i].mShapeType == LocalizationMapEntity::ShapeType::rect || map.mMapEntities[i].mShapeType == LocalizationMapEntity::ShapeType::square) && map.mMapEntities[i].mInteractionType != LocalizationMapEntity::InteractionType::external) 00327 { 00328 float distToRect = sqrt(pow(part.mState.mPoint.i - map.mMapEntities[i].mCenter.i, 2) + pow(part.mState.mPoint.j - map.mMapEntities[i].mCenter.j, 2)); 00329 float largerLength = map.mMapEntities[i].mDim.i; 00330 if(map.mMapEntities[i].mDim.j > largerLength) 00331 { 00332 largerLength = map.mMapEntities[i].mDim.j; 00333 } 00334 if(distToRect <= largerLength / 2) 00335 { 00336 msg->rectangleIsVisible = true; 00337 } 00338 if(distToRect >= closestRectDist) 00339 { 00340 closestRectDist = distToRect; 00341 closestRectIndex = i; 00342 } 00343 } 00344 } 00345 msg->rectangleRelOrientation = map.mMapEntities[closestRectIndex].mOrientation; 00346 return msg; 00347 } 00348 }; 00349 00350 #endif /* LOCALIZATIONSENSORREADER_H_ */