00001 // File: HawkScanner.C 00002 // Author: Josh Villbrandt <josh.villbrandt@usc.edu> 00003 // Date: April 2010 00004 00005 #include "Robots/BeoHawk/computer/HawkScanner.H" 00006 00007 // ###################################################################### 00008 HawkScanner::HawkScanner(std::string myName, int argc, char* argv[]) 00009 : HawkAgent(myName, argc, argv) { 00010 // Help section 00011 helpTitle = "HawkScanner"; 00012 helpDescription = "Interface between the URG-04LX-UG01 laser scanner and ICE."; 00013 helpOptions.push_back("\t--refreshPeriod\t(100000) Microseconds between each scanner message."); 00014 00015 // Parameters 00016 refreshPeriod = loadIntParameter("refreshPeriod", 100000); 00017 00018 if(!helpParameter()) { 00019 // Setup to laser scanner 00020 if(!urg.connect(SCANNER_ADDRESS)) { 00021 printf("UrgCtrl::connect: %s\n", urg.what()); 00022 exit(1); 00023 } 00024 00025 // Print useful parameters 00026 std::cout << "frontIndex: " << urg.rad2index(0.0) << std::endl; 00027 std::cout << "halfRangeRadians: " << urg.index2rad(0) << std::endl; 00028 std::cout << "maxScanLines: " << urg.maxScanLines() << std::endl; 00029 00030 // Setup timer 00031 timer = Timer(1000000); 00032 } 00033 } 00034 00035 // ###################################################################### 00036 void HawkScanner::registerTopics() { 00037 registerPublisher("SensorDataMessage"); 00038 //registerSubscription(""); 00039 } 00040 00041 // ###################################################################### 00042 bool HawkScanner::scheduler() { 00043 // Send ICE message 00044 sendScannerMessage(); 00045 00046 // Ensure consistent output 00047 int sleepTime = refreshPeriod - timer.getSecs()*1000000; 00048 if(sleepTime > 0) usleep(sleepTime); 00049 //std::cout << timer.getSecs()*1000000 << " " << sleepTime << std::endl; 00050 timer.reset(); 00051 00052 // Immediatly rerun scheduler 00053 return true; 00054 } 00055 00056 // ###################################################################### 00057 void HawkScanner::catchMessage(const HawkMessages::MessagePtr& hawkMessage, const Ice::Current&) { 00058 00059 } 00060 00061 // ###################################################################### 00062 void HawkScanner::sendScannerMessage() { 00063 // Get laser data 00064 long timestamp = 0; 00065 std::vector<long> data; 00066 urg.capture(data, ×tamp); 00067 00068 // Convert to LongSeq (and trim bad edge) 00069 int trimEdgeOffset = 2 * urg.rad2index(0.0) - urg.maxScanLines(); 00070 HawkMessages::LongSeq scannerData; 00071 for(int i = 0; i < (int)data.size(); i++) { 00072 if(i >= trimEdgeOffset) scannerData.push_back(data.at(i)); 00073 } 00074 00075 // Create fake attemptedMove 00076 HawkMessages::Pose attemptedMove; 00077 attemptedMove.x = 0; 00078 attemptedMove.y = 0; 00079 attemptedMove.z = 0; 00080 attemptedMove.theta = 0; 00081 00082 // Create message 00083 HawkMessages::SensorDataMessagePtr msg = new HawkMessages::SensorDataMessage; 00084 msg->angularResolution = SCANNER_RESOLUTION; 00085 msg->scannerData = scannerData; 00086 msg->attemptedMove = attemptedMove; 00087 00088 // Send message 00089 publish("SensorDataMessage", msg); 00090 } 00091 00092 // ###################################################################### 00093 int main (int argc, char* argv[]) { 00094 std::cout << "HawkScanner: starting..." << std::endl; 00095 00096 HawkScanner agent("HawkScanner", argc, argv); 00097 agent.start(); 00098 00099 std::cout << "HawkScanner: all done!" << std::endl; 00100 }