00001 /*!@file Robots/IRobot/IRobotSimService.C Ice interface to the IRobot Simulator */ 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 <elazary@usc.edu> 00034 // $HeadURL $ 00035 // $Id $ 00036 // 00037 00038 #include "Component/ModelManager.H" 00039 #include "Raster/GenericFrame.H" 00040 #include "Image/Layout.H" 00041 #include "Media/FrameSeries.H" 00042 #include "Transport/FrameInfo.H" 00043 #include "Image/MatrixOps.H" 00044 #include "GUI/ImageDisplayStream.H" 00045 #include "GUI/XWinManaged.H" 00046 #include "Robots/IRobot/IRobotSim.H" 00047 #include "Ice/IceImageUtils.H" 00048 #include "Ice/ImageIce.ice.H" 00049 #include <Ice/Ice.h> 00050 #include <Ice/Service.h> 00051 #include "Ice/IRobot.ice.H" 00052 00053 #include <stdio.h> 00054 #include <stdlib.h> 00055 00056 #define KEY_UP 98 00057 #define KEY_DOWN 104 00058 #define KEY_LEFT 100 00059 #define KEY_RIGHT 102 00060 00061 class IRobotI : public ModelComponent, public Robots::IRobot, public IceUtil::Thread 00062 { 00063 public: 00064 IRobotI(ModelManager& mgr, 00065 nub::soft_ref<OutputFrameSeries> ofs, 00066 const std::string& descrName = "IRobotSimService", 00067 const std::string& tagName = "IRobotSimService") : 00068 ModelComponent(mgr, descrName, tagName), 00069 itsOfs(ofs), 00070 itsWorldView(true), 00071 itsCurrentSpeed(0), 00072 itsCurrentSteering(0) 00073 { 00074 itsIRobotSim = nub::soft_ref<IRobotSim>(new IRobotSim(mgr)); 00075 addSubComponent(itsIRobotSim); 00076 } 00077 00078 virtual void run() 00079 { 00080 itsIRobotSim->initViewport(); 00081 while(true) 00082 { 00083 itsIRobotSim->simLoop(); 00084 00085 //Image<PixRGB<byte> > camImage; 00086 00087 float rightWheel = (itsCurrentSpeed*10) + (itsCurrentSteering*10); 00088 float leftWheel = (itsCurrentSpeed*10) - (itsCurrentSteering*10); 00089 00090 itsIRobotSim->setMotors(rightWheel, leftWheel); 00091 00092 00093 // if (itsWorldView) 00094 // camImage = flipVertic(itsIRobotSim->getFrame(-1)); 00095 // else 00096 // camImage = flipVertic(itsIRobotSim->getFrame(1)); 00097 itsCurrentFrame = flipVertic(itsIRobotSim->getFrame(-1)); 00098 00099 // itsOfs->writeRGB(camImage, "IRobotSim", FrameInfo("IRobotSim", SRC_POS)); 00100 } 00101 00102 } 00103 00104 00105 virtual float getSpeed(const Ice::Current&) { return itsCurrentSpeed; } 00106 virtual short setSpeed(const float speed, const Ice::Current&) { itsCurrentSpeed = speed; return 0; } 00107 virtual float getSteering(const Ice::Current&) { return itsCurrentSteering; } 00108 virtual short setSteering(const float steeringPos, const Ice::Current&) { itsCurrentSteering = steeringPos; return 0;} 00109 virtual ImageIceMod::ImageIce getImageSensor(const short i, bool color, const Ice::Current&) 00110 { 00111 return Image2Ice(itsCurrentFrame); 00112 } 00113 virtual ImageIceMod::DimsIce getImageSensorDims(const short i, const Ice::Current&) 00114 { 00115 ImageIceMod::DimsIce dims; 00116 dims.w = 0; 00117 dims.h = 0; 00118 return dims; 00119 } 00120 virtual float getSensorValue(const short i, const Ice::Current&){ 00121 switch (i) 00122 { 00123 case 0: return itsIRobotSim->getXPos(); 00124 case 1: return itsIRobotSim->getYPos(); 00125 case 2: return itsIRobotSim->getOri(); 00126 } 00127 00128 return 0; 00129 } 00130 00131 virtual bool getSensors(float &xPos, float &yPos, float &ori, const Ice::Current&) 00132 { 00133 itsIRobotSim->getSensors(xPos, yPos, ori); 00134 return true; 00135 } 00136 00137 virtual bool getDistanceAngle(float &dist, float &ang, const Ice::Current&) 00138 { 00139 float xPos, yPos, ori; 00140 itsIRobotSim->getSensors(xPos, yPos, ori); 00141 00142 //TODO get dist and ang from pos 00143 // 00144 return false; 00145 00146 } 00147 00148 00149 virtual void motorsOff(const short i, const Ice::Current&){ 00150 itsCurrentSpeed = 0; 00151 itsCurrentSteering = 0; 00152 itsIRobotSim->setMotors(0, 0); 00153 } 00154 virtual void setMotor(const short i, const float val, const Ice::Current&){ } 00155 virtual short sendRawCmd(const std::string& data, const Ice::Current&) { return 0; } 00156 virtual void playSong(const short song, const Ice::Current&) { } 00157 virtual void shutdown(const Ice::Current&) { } 00158 virtual void sendStart(const Ice::Current&) { } 00159 virtual void setMode(const Robots::IRobotModes demo, const Ice::Current&) { } 00160 virtual void setDemo(const short demo, const Ice::Current&) { } 00161 virtual void setLED(const short led, const short color, const short intensity, const Ice::Current&) { } 00162 00163 00164 private: 00165 nub::soft_ref<OutputFrameSeries> itsOfs; 00166 nub::soft_ref<IRobotSim> itsIRobotSim; 00167 Image<PixRGB<byte> > itsCurrentFrame; 00168 bool itsWorldView; 00169 float itsCurrentSpeed; 00170 float itsCurrentSteering; 00171 00172 }; 00173 00174 00175 // ###################################################################### 00176 class IRobotSimService : public Ice::Service { 00177 protected: 00178 virtual bool start(int, char*[]); 00179 private: 00180 Ice::ObjectAdapterPtr itsAdapter; 00181 ModelManager *itsMgr; 00182 }; 00183 00184 //bool IRobotSimService::stop() 00185 //{ 00186 // if (itsMgr) 00187 // delete itsMgr; 00188 // return true; 00189 //} 00190 00191 00192 bool IRobotSimService::start(int argc, char* argv[]) 00193 { 00194 // Instantiate a ModelManager: 00195 itsMgr = new ModelManager("IRobot Simulator Service"); 00196 00197 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*itsMgr)); 00198 itsMgr->addSubComponent(ofs); 00199 00200 // Instantiate our various ModelComponents: 00201 nub::ref<IRobotI> iRobotI(new IRobotI(*itsMgr, ofs)); 00202 itsMgr->addSubComponent(iRobotI); 00203 00204 // Parse command-line: 00205 if (itsMgr->parseCommandLine(argc, argv, "", 0, 0) == false) 00206 return(1); 00207 00208 char endpointBuff[64]; 00209 sprintf(endpointBuff, "default -p %d", 10000); 00210 itsAdapter = communicator()->createObjectAdapterWithEndpoints("IRobotSimAdapter", endpointBuff); 00211 00212 Ice::ObjectPtr object = iRobotI.get(); 00213 Ice::ObjectPrx objectPrx = itsAdapter->add(object, communicator()->stringToIdentity("IRobotService")); 00214 itsAdapter->activate(); 00215 00216 itsMgr->start(); 00217 00218 //Start the retina evolve thread 00219 IceUtil::ThreadPtr iRobotIThread = iRobotI.get(); 00220 iRobotIThread->start(); 00221 LINFO("Started"); 00222 00223 return true; 00224 00225 } 00226 00227 // ###################################################################### 00228 int main(int argc, char** argv) { 00229 IRobotSimService svc; 00230 return svc.main(argc, argv); 00231 00232 } 00233