iSpike
2.1
Spike conversion library for robotics
|
00001 //iSpike includes 00002 #include <iSpike/Reader/YarpVisualReader.hpp> 00003 #include <iSpike/Bitmap.hpp> 00004 #include <iSpike/YarpConnection.hpp> 00005 #include <iSpike/Log/Log.hpp> 00006 #include <iSpike/ISpikeException.hpp> 00007 using namespace ispike; 00008 00009 //Other includes 00010 #include <iostream> 00011 using namespace std; 00012 00013 //Property names 00014 #define PORT_NAME_PROP "Port Name" 00015 #define SLEEP_DURATION_PROP "Sleep Duration ms" 00016 00018 YarpVisualReader::YarpVisualReader(string nameserverIP, unsigned nameserverPort) { 00019 // Connect to YARP and get list of ports 00020 yarpConnection = NULL;//Initialize to null so that it is deleted correctly if an exception is thrown 00021 yarpConnection = new YarpConnection(nameserverIP, nameserverPort); 00022 00023 //Store port names as properties of this reader 00024 vector<string> yarpPortNames; 00025 map<string, YarpPortDetails>& portMap = yarpConnection->getPortMap(); 00026 for (map<string, YarpPortDetails>::iterator iter = portMap.begin(); iter != portMap.end(); ++iter){ 00027 yarpPortNames.push_back(iter->first); 00028 } 00029 if(yarpPortNames.empty()) { 00030 addProperty(Property("undefined", yarpPortNames, PORT_NAME_PROP, "The Yarp Port name", true)); 00031 } else { 00032 addProperty(Property(yarpPortNames[0], yarpPortNames, PORT_NAME_PROP, "The Yarp Port name", true)); 00033 } 00034 00035 addProperty(Property(Property::Integer, 50, SLEEP_DURATION_PROP, "Amount to sleep in milliseconds in between reads.", false)); 00036 00037 //Create the description 00038 readerDescription = Description("Yarp Visual Reader", "This is a Yarp visual reader", "Visual Reader"); 00039 00040 //Initialize variables 00041 portName = "Undefined"; 00042 } 00043 00044 00046 YarpVisualReader::~YarpVisualReader() { 00047 if(isRunning()){ 00048 requestStop(); 00049 getThreadPointer()->join(); 00050 } 00051 if(isInitialized()) { 00052 delete bitmap1; 00053 delete bitmap2; 00054 } 00055 if(yarpConnection != NULL) 00056 delete yarpConnection; 00057 } 00058 00059 00060 /*--------------------------------------------------------------------*/ 00061 /*--------- PUBLIC METHODS -------*/ 00062 /*--------------------------------------------------------------------*/ 00063 00064 //Inherited from VisualReader 00065 Bitmap& YarpVisualReader::getBitmap(){ 00066 boost::mutex::scoped_lock lock(threadMutex); 00067 if(returnBitmap1) 00068 return *bitmap1; 00069 return *bitmap2; 00070 } 00071 00072 00073 //Inherited from Reader 00074 void YarpVisualReader::initialize(map<string, Property>& properties){ 00075 updateProperties(properties); 00076 00077 bitmap1 = new Bitmap(0,0,0); 00078 bitmap2 = new Bitmap(0,0,0); 00079 returnBitmap1 = true; 00080 00081 setInitialized(true); 00082 } 00083 00084 00085 //Inherited from PropertyHolder 00086 void YarpVisualReader::setProperties(map<string, Property> &properties){ 00087 updateProperties(properties); 00088 } 00089 00090 00091 //Inherited from iSpikeThread 00092 void YarpVisualReader::start() { 00093 if(!isInitialized()) 00094 throw ISpikeException("YarpVisualReader cannot be started because it has not been initialized."); 00095 if(isRunning()) 00096 throw ISpikeException("Cannot start YarpVisualReader - it is already running."); 00097 00098 this->setThreadPointer(boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&YarpVisualReader::workerFunction, this)))); 00099 } 00100 00101 /*--------------------------------------------------------------------*/ 00102 /*--------- PRIVATE METHODS -------*/ 00103 /*--------------------------------------------------------------------*/ 00104 00106 void YarpVisualReader::swapBitmap(){ 00107 boost::mutex::scoped_lock lock(threadMutex); 00108 if(returnBitmap1) 00109 returnBitmap1 = false; 00110 else 00111 returnBitmap1 = true; 00112 00113 //Image has changed so increment the image ID 00114 ++imageID; 00115 } 00116 00117 00119 void YarpVisualReader::updateProperties(map<string, Property>& properties){ 00120 if(!isInitialized()) 00121 portName = updateComboProperty(properties[PORT_NAME_PROP]); 00122 00123 sleepDuration_ms = updateIntegerProperty(properties[SLEEP_DURATION_PROP]); 00124 } 00125 00126 00127 //Inherited from iSpikeThread 00128 void YarpVisualReader::workerFunction(){ 00129 setRunning(true); 00130 clearError(); 00131 00132 try{ 00133 //Connect to port 00134 map<string, YarpPortDetails>& portMap = yarpConnection->getPortMap(); 00135 map<string, YarpPortDetails>::iterator iter = portMap.find(portName); 00136 if (iter == portMap.end() ) 00137 throw ISpikeException("YarpVisualReader: Yarp IP/Port map is empty!"); 00138 00139 string ip = iter->second.getIp(); 00140 unsigned port = iter->second.getPort(); 00141 LOG(LOG_INFO) << "YarpVisualReader: Yarp Port IP: " << ip << " Port: " << port; 00142 00143 //Main run loop 00144 unsigned loopCtr = 0; 00145 while(!isStopRequested()) { 00146 if(loopCtr % 20 == 0){ 00147 LOG(LOG_DEBUG)<<"YarpVisualReader: Reading image."; 00148 } 00149 00150 yarpConnection->connect_to_port(ip, port); 00151 yarpConnection->prepare_to_read_binary(); 00152 00153 //Load new version of image into new buffer 00154 if(returnBitmap1){ 00155 yarpConnection->read_image(*bitmap2); 00156 swapBitmap(); 00157 } 00158 else{ 00159 yarpConnection->read_image(*bitmap1); 00160 swapBitmap(); 00161 } 00162 00163 //Sleep for the specified amount 00164 if(sleepDuration_ms > 0) 00165 boost::this_thread::sleep(boost::posix_time::milliseconds(sleepDuration_ms)); 00166 00167 ++loopCtr; 00168 } 00169 } 00170 catch(ISpikeException& ex){ 00171 setError(ex.msg()); 00172 } 00173 catch(...){ 00174 setError("An unknown exception occurred in the YarpVisualReader"); 00175 } 00176 00177 setRunning(false); 00178 } 00179