00001 /* 00002 * ObstacleProviderController.cpp 00003 * 00004 * Created on: Dec 14, 2011 00005 * Author: mriedel 00006 */ 00007 00008 #include <obs_detection/ObstacleProviderController.hpp> 00009 00010 #include <telekyb_base/ROS/ROSModule.hpp> 00011 00012 #include <telekyb_msgs/StampedPointArray.h> 00013 00014 namespace TELEKYB_NAMESPACE { 00015 00016 ObstacleProviderController::ObstacleProviderController() 00017 : nodeHandle(ROSModule::Instance().getMainNodeHandle()),recvFirstTKState(false) 00018 { 00019 tStateSub = nodeHandle.subscribe(options.tTKStateTopicName->getValue(),1,&ObstacleProviderController::tkStateCB, this); 00020 tObsPointsPub = nodeHandle.advertise<telekyb_msgs::StampedPointArray>(options.tObsPubTopicName->getValue(), 10); 00021 00022 loadObstacleProviders(); 00023 00024 Time timeOut(options.tInitialStateTimeout->getValue()); 00025 Time rate(0.1); 00026 while (!recvFirstTKState && timeOut.toDSec() > 0.0) { 00027 // wait 00028 timeOut -= rate; 00029 rate.sleep(); 00030 } 00031 00032 } 00033 00034 ObstacleProviderController::~ObstacleProviderController() 00035 { 00036 // has to shutdown before it runs out of scope! 00037 tStateSub.shutdown(); 00038 } 00039 00040 void ObstacleProviderController::loadObstacleProviders() 00041 { 00042 std::vector< std::string > obstacleProviderStrings = options.tObstacleProviders->getValue(); 00043 for (unsigned int i = 0; i < obstacleProviderStrings.size(); ++i) { 00044 obsContainer.loadObstacleProvider(obstacleProviderStrings[i]); 00045 } 00046 } 00047 00048 void ObstacleProviderController::obstacleProviderStep() 00049 { 00050 allObstaclePositions.clear(); 00051 std::set<ObstacleProvider*>::const_iterator it; 00052 for (it = obsContainer.getLoadedObstacleProviders().begin(); it != obsContainer.getLoadedObstacleProviders().end(); it++) { 00053 std::vector<Position3D> providerObstaclePoints; 00054 (*it)->getObstaclePoints(lastState, providerObstaclePoints); 00055 std::copy(providerObstaclePoints.begin(), providerObstaclePoints.end(), std::back_inserter(allObstaclePositions)); 00056 } 00057 00058 telekyb_msgs::StampedPointArray msg; 00059 msg.points.resize(allObstaclePositions.size()); 00060 for (unsigned int i = 0; i < allObstaclePositions.size(); ++i) { 00061 msg.points[i].x = allObstaclePositions[i](0); 00062 msg.points[i].y = allObstaclePositions[i](1); 00063 msg.points[i].z = allObstaclePositions[i](2); 00064 } 00065 00066 // send out ObstaclePoints 00067 tObsPointsPub.publish(msg); 00068 } 00069 00070 void ObstacleProviderController::spin() 00071 { 00072 if (!recvFirstTKState) { 00073 ROS_ERROR("Did not receive a TKState Msg within Timeout. Quitting..."); 00074 return; 00075 } 00076 00077 if (obsContainer.getLoadedObstacleProviders().empty()) { 00078 ROS_ERROR("No Obstacle Providers loaded. Quitting..."); 00079 return; 00080 } 00081 00082 Time sleepTime(1.0 / options.tObsSpinrate->getValue()); 00083 while(ros::ok()) { 00084 00085 obstacleProviderStep(); 00086 00087 sleepTime.sleep(); 00088 } 00089 } 00090 00091 void ObstacleProviderController::tkStateCB(const telekyb_msgs::TKState::ConstPtr& tkStateMsg) 00092 { 00093 boost::mutex::scoped_lock lastStateLock(lastStateMutex); 00094 lastState = *tkStateMsg; 00095 recvFirstTKState = true; 00096 } 00097 00098 } /* namespace telekyb */