00001 /*!@file test-IRobot.C a test the irobot service */ 00002 00003 //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Lior Elazary <lelazary@yahoo.com> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/IRobot/test-IRobot.C $ 00035 // $Id: test-IRobot.C 12962 2010-03-06 02:13:53Z irock $ 00036 // 00037 00038 #include <Ice/Ice.h> 00039 #include "Ice/IRobot.ice.H" 00040 #include "Ice/ImageIce.ice.H" 00041 #include "Ice/IceImageUtils.H" 00042 00043 #include "Component/ModelManager.H" 00044 #include "Media/FrameSeries.H" 00045 #include "Transport/FrameInfo.H" 00046 #include "Raster/GenericFrame.H" 00047 #include "Image/Image.H" 00048 #include "Image/DrawOps.H" 00049 #include "Image/ColorOps.H" 00050 #include "GUI/XWinManaged.H" 00051 #include "GUI/ImageDisplayStream.H" 00052 #include "Util/Timer.H" 00053 #include "Robots/RobotBrain/RobotCommon.H" 00054 00055 using namespace std; 00056 using namespace Robots; 00057 00058 #define KEY_UP 98 00059 #define KEY_DOWN 104 00060 #define KEY_LEFT 100 00061 #define KEY_RIGHT 102 00062 00063 int getKey(nub::ref<OutputFrameSeries> &ofs) 00064 { 00065 const nub::soft_ref<ImageDisplayStream> ids = 00066 ofs->findFrameDestType<ImageDisplayStream>(); 00067 00068 const rutz::shared_ptr<XWinManaged> uiwin = 00069 ids.is_valid() 00070 ? ids->getWindow("Output") 00071 : rutz::shared_ptr<XWinManaged>(); 00072 if (uiwin.is_valid()) 00073 return uiwin->getLastKeyPress(); 00074 00075 return -1; 00076 } 00077 00078 ////////////////////////////////////////////////////////////////////// 00079 int main(int argc, char** argv) 00080 { 00081 00082 MYLOGVERB = LOG_INFO; 00083 ModelManager manager("test-IRobot"); 00084 00085 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager)); 00086 manager.addSubComponent(ofs); 00087 00088 Timer localTimer; 00089 00090 int status = 0; 00091 Ice::CommunicatorPtr ic; 00092 try { 00093 ic = Ice::initialize(argc, argv); 00094 Ice::ObjectPrx base = ic->stringToProxy( 00095 "IRobotService:default -p 10000 -h " ROBOT_IP); 00096 IRobotPrx iRobot = IRobotPrx::checkedCast(base); 00097 if(!iRobot) 00098 throw "Invalid proxy"; 00099 00100 manager.exportOptions(MC_RECURSE); 00101 00102 if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 0) == false) 00103 return 1; 00104 manager.start(); 00105 00106 00107 iRobot->sendStart(); 00108 iRobot->setMode(Robots::SafeMode); 00109 localTimer.reset(); 00110 00111 00112 float speed = 0, steering = 0; 00113 Timer localTimer; 00114 00115 int id = 0; 00116 id=id; 00117 00118 Image<PixRGB<byte> > iRobotImg(256,256, ZEROS); 00119 ofs->writeRGB(iRobotImg, "Output", FrameInfo("Output", SRC_POS)); 00120 00121 while(true) 00122 { 00123 localTimer.reset(); 00124 // LINFO("Get %i\n", id++); 00125 ImageIceMod::ImageIce imgIce = iRobot->getImageSensor(320*4 + id, false); 00126 // LINFO("Rate: %f fps", 1.0F/localTimer.getSecs()); 00127 00128 if (imgIce.pixSize == 1) 00129 iRobotImg = toRGB(Ice2Image<byte>(imgIce)); 00130 else 00131 iRobotImg = Ice2Image<PixRGB<byte> >(imgIce); 00132 00133 ////Show the image from the robot camera 00134 00135 00136 //Get other sensors 00137 float dist, ang; 00138 iRobot->getDistanceAngle(dist, ang); 00139 printf("%f %f %f %f %f %i\n", speed, steering, dist, ang, localTimer.get()/1000.0F, 00140 ofs->frame()); 00141 fflush(stdout); 00142 00143 ofs->writeRGB(iRobotImg, "Output", FrameInfo("Output", SRC_POS)); 00144 00145 00146 // drawLine(iRobotImg, Point2D<int>(iRobotImg.getWidth()/2, 0), 00147 // Point2D<int>(iRobotImg.getWidth()/2, iRobotImg.getHeight()), 00148 // PixRGB<byte>(255,0,0)); 00149 00150 //ofs->writeRGB(iRobotImg, "Cross", FrameInfo("Cross", SRC_POS)); 00151 ofs->updateNext(); 00152 00153 ofs->writeRGB(iRobotImg, "Output", FrameInfo("Output", SRC_POS)); 00154 usleep(100000); 00155 int key = getKey(ofs); 00156 00157 00158 if (key != -1) 00159 { 00160 //LINFO("Got Key:%d", key); 00161 switch(key) 00162 { 00163 case KEY_UP: 00164 speed = 0.05; 00165 steering = 0; 00166 iRobot->setSteering(steering); 00167 iRobot->setSpeed(speed); 00168 break; 00169 case KEY_DOWN: 00170 speed = -0.05; 00171 steering = 0; 00172 iRobot->setSteering(steering); 00173 iRobot->setSpeed(speed); 00174 break; 00175 case KEY_LEFT: 00176 steering = 0.05; 00177 iRobot->setSteering(steering); 00178 iRobot->setSpeed(speed); 00179 break; 00180 case KEY_RIGHT: 00181 steering = -0.05; 00182 iRobot->setSteering(steering); 00183 iRobot->setSpeed(speed); 00184 break; 00185 case 65: //space 00186 speed = 0; 00187 steering = 0; 00188 iRobot->motorsOff(0); 00189 break; 00190 case 33: //p for playing the song 00191 iRobot->setMode(Robots::SafeMode); 00192 //LINFO("Play song"); 00193 iRobot->playSong(0); 00194 break; 00195 case 40: //d for dock with base station 00196 //LINFO("Docking"); 00197 iRobot->setMode(Robots::CoverAndDockMode); 00198 break; 00199 00200 case 39: //s for status 00201 { 00202 float chargeState = iRobot->getSensorValue(21); 00203 float voltage = iRobot->getSensorValue(22); 00204 float current = iRobot->getSensorValue(23); 00205 float batteryTemp = iRobot->getSensorValue(24); 00206 float batteryCharge = iRobot->getSensorValue(25); 00207 float batteryCapacity = iRobot->getSensorValue(26); 00208 00209 LINFO("ChargeState %i", (int)chargeState); 00210 LINFO("Voltage %i mV", (unsigned int)voltage); 00211 LINFO("Current %i mA", (int)current); 00212 LINFO("Battery Temperature %iC", (int)batteryTemp); 00213 LINFO("Battery Charge %i mAh", (int)batteryCharge); 00214 LINFO("Battery Capacity %i mAh", (int)batteryCapacity); 00215 } 00216 break; 00217 case 27: //r 00218 iRobot->sendStart(); 00219 iRobot->setMode(Robots::SafeMode); 00220 break; 00221 00222 00223 default: 00224 LINFO("Unknown key %i\n", key); 00225 break; 00226 } 00227 localTimer.reset(); 00228 // iRobot->setSteering(steering); 00229 // iRobot->setSpeed(speed); 00230 // usleep(100000); 00231 // LINFO("Rate2: %f fps", 1.0F/localTimer.getSecs()); 00232 } 00233 } 00234 00235 } 00236 catch (const Ice::Exception& ex) { 00237 cerr << ex << endl; 00238 status = 1; 00239 } 00240 catch(const char* msg) { 00241 cerr << msg << endl; 00242 status = 1; 00243 } 00244 if (ic) 00245 ic->destroy(); 00246 return status; 00247 }