Go to the documentation of this file.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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "jaus/extras/rangesensor/rangesubscriber.h"
00041 #include <cxutils/math/cxmath.h>
00042 using namespace JAUS;
00043
00044 const std::string RangeSubscriber::Name = "urn:jaus:jss:jpp:extras:RangeSubscriber";
00045
00046
00052 RangeSubscriber::RangeSubscriber() : Management::Child(Service::ID(RangeSubscriber::Name),
00053 Service::ID(Management::Name))
00054 {
00055
00056 }
00057
00058
00064 RangeSubscriber::~RangeSubscriber()
00065 {
00066 }
00067
00068
00074 void RangeSubscriber::Shutdown()
00075 {
00076 }
00077
00078
00090 bool RangeSubscriber::CreateRangeSubscription(const Address& id,
00091 const Byte deviceID,
00092 const unsigned int waitTimeMs)
00093 {
00094 QueryRangeSensorConfiguration queryConfig(id, GetComponentID());
00095 Send(&queryConfig);
00096
00097 QueryLocalRangeScan queryEvent;
00098 queryEvent.SetSensorID(deviceID);
00099 return EventsService()->RequestEveryChangeEvent(id, &queryEvent, waitTimeMs);
00100 }
00101
00102
00115 bool RangeSubscriber::GetRangeSensorInfo(const Address& id,
00116 RangeSensorConfig::List& list,
00117 const unsigned int waitTimeMs) const
00118 {
00119 QueryRangeSensorConfiguration query(id, GetComponentID());
00120 ReportRangeSensorConfiguration report;
00121 if(Send(&query, &report, waitTimeMs))
00122 {
00123 list = *report.GetConfiguration();
00124 return list.size() > 0 ? true : false;
00125 }
00126 return false;
00127 }
00128
00129
00140 bool RangeSubscriber::HaveRangeSubscription(const Address& id,
00141 const int deviceID) const
00142 {
00143 bool result = false;
00144 Events::Subscription::List list = GetComponent()->EventsService()->GetSubscriptions(id, REPORT_LOCAL_RANGE_SCAN);
00145 Events::Subscription::List::iterator s;
00146 for(s = list.begin();
00147 s != list.end();
00148 s++)
00149 {
00150 if(deviceID < 0)
00151 {
00152 result = true;
00153 break;
00154 }
00155 else
00156 {
00157 QueryLocalRangeScan* query = dynamic_cast<QueryLocalRangeScan *>(s->mpQueryMessage);
00158 if(query && query->GetSensorID() == (Byte)deviceID)
00159 {
00160 result = true;
00161 break;
00162 }
00163 }
00164 }
00165
00166 return result;
00167 }
00168
00169
00180 bool RangeSubscriber::CancelRangeSubscription(const Address& id,
00181 const int deviceID)
00182 {
00183 bool result = false;
00184
00185 Events::Subscription::List list = GetComponent()->EventsService()->GetSubscriptions(id, REPORT_LOCAL_RANGE_SCAN);
00186 Events::Subscription::List::iterator s;
00187 for(s = list.begin();
00188 s != list.end();
00189 s++)
00190 {
00191 if(deviceID < 0)
00192 {
00193 if(GetComponent()->EventsService()->CancelSubscription(s->mProducer,
00194 REPORT_LOCAL_RANGE_SCAN,
00195 s->mID,
00196 Service::DefaultWaitMs))
00197 {
00198 result = true;
00199 }
00200 }
00201 else
00202 {
00203 QueryLocalRangeScan* query = dynamic_cast<QueryLocalRangeScan *>(s->mpQueryMessage);
00204 if(query && query->GetSensorID() == (Byte)deviceID)
00205 {
00206 if(GetComponent()->EventsService()->CancelSubscription(s->mProducer,
00207 REPORT_LOCAL_RANGE_SCAN,
00208 s->mID,
00209 Service::DefaultWaitMs))
00210 {
00211 result = true;
00212 }
00213 break;
00214 }
00215 }
00216 }
00217
00218 return result;
00219 }
00220
00221
00230 void RangeSubscriber::RegisterCallback(Callback* callback, const bool add)
00231 {
00232 Mutex::ScopedLock lock(&mRangeCallbacksMutex);
00233 if(add)
00234 {
00235 mRangeCallbacks.insert(callback);
00236 }
00237 else
00238 {
00239 Callback::Set::iterator cb;
00240
00241 cb = mRangeCallbacks.find(callback);
00242 if(cb != mRangeCallbacks.end())
00243 {
00244 mRangeCallbacks.erase(cb);
00245 }
00246 }
00247 }
00248
00249
00260 void RangeSubscriber::Receive(const Message* message)
00261 {
00262 switch(message->GetMessageCode())
00263 {
00264 case REPORT_LOCAL_RANGE_SCAN:
00265 {
00266 const ReportLocalRangeScan* report =
00267 dynamic_cast<const ReportLocalRangeScan*>(message);
00268 if(report)
00269 {
00270 Point3D::List scan;
00271 Point3D::List scanPolar;
00272 mRangeSensorMutex.Lock();
00273
00274 std::map<Address, RangeSensorConfig::Map>::iterator comp;
00275 comp = mRangeSensors.find(report->GetSourceID());
00276 if(comp != mRangeSensors.end())
00277 {
00278 RangeSensorConfig::Map::iterator config = comp->second.find(report->GetSensorID());
00279 if(config != comp->second.end())
00280 {
00281
00282 ReportLocalRangeScan::Scan::const_iterator v;
00283 const ReportLocalRangeScan::Scan* ptr = report->GetScan();
00284 double angle = config->second.mScanAngle/-2.0;
00285 double incr = config->second.mAngleIncrement;
00286 double divider = 1000.0;
00287 if(config->second.mUnitType == RangeSensorConfig::CM)
00288 {
00289 divider = 100.0;
00290 }
00291
00292 CxUtils::Quaternion rotX, rotY, rotZ, final;
00293 rotX.SetRotationX(report->GetSensorOrientation().mX);
00294 rotY.SetRotationY(report->GetSensorOrientation().mY);
00295 rotZ.SetRotationZ(report->GetSensorOrientation().mZ);
00296
00297 final = rotX*rotY*rotZ;
00298
00299 bool first = true;
00300 double pitch = 0.0;
00301
00302 for(v = ptr->begin();
00303 v != ptr->end();
00304 v++)
00305 {
00306 Point3D point((*v)/divider);
00307 Point3D polar(point.mX);
00308
00309
00310
00311 CxUtils::Quaternion rotation;
00312 rotation.SetRotationZ(angle);
00313 point = rotation.Rotate(point);
00314
00315
00316
00317 point = final.Rotate(point);
00318 point += report->GetSensorLocation();
00319
00320
00321 scan.push_back(point);
00322
00323 if(first)
00324 {
00325 pitch = atan2(point.mZ, 1.0);
00326 first = false;
00327 }
00328 polar.mY = pitch;
00329 polar.mZ = angle;
00330 scanPolar.push_back(polar);
00331
00332 angle += incr;
00333 }
00334 }
00335 }
00336
00337 mRangeSensorMutex.Unlock();
00338
00339 ProcessLocalRangeScan(scan,
00340 report->GetSourceID(),
00341 report->GetSensorID(),
00342 report->GetTimeStamp());
00343
00344 mRangeCallbacksMutex.Lock();
00345
00346 Callback::Set::iterator cb;
00347 for(cb = mRangeCallbacks.begin();
00348 cb != mRangeCallbacks.end();
00349 cb++)
00350 {
00351 (*cb)->ProcessLocalRangeScan(scan,
00352 report->GetSourceID(),
00353 report->GetSensorID(),
00354 report->GetTimeStamp());
00355 (*cb)->ProcessLocalRangeScanPolar(scanPolar,
00356 report->GetSourceID(),
00357 report->GetSensorID(),
00358 report->GetTimeStamp());
00359 }
00360
00361 mRangeCallbacksMutex.Unlock();
00362
00363 }
00364 }
00365 break;
00366 case REPORT_RANGE_SENSOR_CONFIGURATION:
00367 {
00368 const ReportRangeSensorConfiguration* report =
00369 dynamic_cast<const ReportRangeSensorConfiguration*>(message);
00370 if(report)
00371 {
00372 Mutex::ScopedLock lock(&mRangeSensorMutex);
00373 RangeSensorConfig::List::const_iterator c;
00374 for(c = report->GetConfiguration()->begin();
00375 c != report->GetConfiguration()->end();
00376 c++)
00377 {
00378 mRangeSensors[report->GetSourceID()][c->mID] = *c;
00379 }
00380 }
00381 }
00382 break;
00383 default:
00384 break;
00385 }
00386 }
00387
00388
00400 Message* RangeSubscriber::CreateMessage(const UShort messageCode) const
00401 {
00402 Message* message = NULL;
00403 switch(messageCode)
00404 {
00405 case QUERY_LOCAL_RANGE_SCAN:
00406 message = new QueryLocalRangeScan();
00407 break;
00408 case QUERY_RANGE_SENSOR_CONFIGURATION:
00409 message = new QueryRangeSensorConfiguration();
00410 break;
00411 case REPORT_RANGE_SENSOR_CONFIGURATION:
00412 message = new ReportRangeSensorConfiguration();
00413 break;
00414 case REPORT_LOCAL_RANGE_SCAN:
00415 message = new ReportLocalRangeScan();
00416 break;
00417 default:
00418 message = NULL;
00419 break;
00420 };
00421 return message;
00422 }
00423
00424
00425