RobotBrainStimulator.C

Go to the documentation of this file.
00001 /*!@file Robots/RobotBrain/RobotBrainStimulator.C Stimulator the Robot with recorded data */
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 "Component/ModelComponent.H"
00040 #include "Component/ModelParam.H"
00041 #include "Component/ModelOptionDef.H"
00042 #include "Raster/GenericFrame.H"
00043 #include "Image/Layout.H"
00044 #include "Media/FrameSeries.H"
00045 #include "Transport/FrameInfo.H"
00046 #include "Image/MatrixOps.H"
00047 #include "GUI/ImageDisplayStream.H"
00048 #include "GUI/XWinManaged.H"
00049 #include "Robots/IRobot/IRobotSim.H"
00050 #include "Ice/IceImageUtils.H"
00051 #include "Ice/ImageIce.ice.H"
00052 #include <Ice/Ice.h>
00053 #include <Ice/Service.h>
00054 #include "Ice/IRobot.ice.H"
00055 
00056 #include <stdio.h>
00057 #include <stdlib.h>
00058 #include <sys/types.h>
00059 #include <sys/stat.h>
00060 #include <fcntl.h>
00061 
00062 #define KEY_UP 98
00063 #define KEY_DOWN 104
00064 #define KEY_LEFT 100
00065 #define KEY_RIGHT 102
00066 
00067 const ModelOptionCateg MOC_StimulatorData = {
00068     MOC_SORTPRI_3, "Stimulator Data related options" };
00069 
00070 const ModelOptionDef OPT_StimulatorFilename =
00071 { MODOPT_ARG(std::string), "StimulatorFile", &MOC_StimulatorData, OPTEXP_CORE,
00072     "Path to the stimulator data input file",
00073     "stimulator-file", '\0', "", "" };
00074 
00075 
00076 class RobotBrainStimulator : public ModelComponent, public Robots::IRobot, public IceUtil::Thread
00077 {
00078   public:
00079     RobotBrainStimulator(ModelManager& mgr,
00080         nub::soft_ref<OutputFrameSeries> ofs,
00081         nub::soft_ref<InputFrameSeries> ifs,
00082         const std::string& descrName = "RobotBrainStimulator",
00083         const std::string& tagName = "RobotBrainStimulator") :
00084       ModelComponent(mgr, descrName, tagName),
00085       itsOfs(ofs),
00086       itsIfs(ifs),
00087       itsFilename(&OPT_StimulatorFilename, this, 0),
00088       itsCurrentSpeed(0),
00089       itsCurrentSteering(0)
00090     {
00091 
00092     }
00093 
00094     virtual void start1()
00095     {
00096       if((itsStimulatorFd = fopen(itsFilename.getVal().c_str(), "r")) == NULL)
00097         LFATAL("Can not open stimulator file %s", itsFilename.getVal().c_str());
00098     }
00099 
00100     virtual void run()
00101     {
00102       while(true)
00103       {
00104 
00105       }
00106 
00107     }
00108 
00109 
00110     virtual float getSpeed(const Ice::Current&) { return itsCurrentSpeed; }
00111     virtual short setSpeed(const float speed, const Ice::Current&) { itsCurrentSpeed = speed;  return 0; }
00112     virtual float getSteering(const Ice::Current&) { return itsCurrentSteering; }
00113     virtual short setSteering(const float steeringPos, const Ice::Current&) { itsCurrentSteering = steeringPos; return 0;}
00114 
00115     virtual ImageIceMod::ImageIce getImageSensor(const short i, const bool color, const Ice::Current&)
00116     {
00117 
00118       ////grab the images
00119       GenericFrame input = itsIfs->readFrame();
00120       Image<PixRGB<byte> > img = input.asRgb();
00121       if (img.initialized())
00122         itsCurrentFrame = img;
00123 
00124       return Image2Ice(itsCurrentFrame);
00125     }
00126     virtual ImageIceMod::DimsIce getImageSensorDims(const short i, const Ice::Current&)
00127     {
00128       ImageIceMod::DimsIce dims;
00129       dims.w = 0;
00130       dims.h = 0;
00131       return dims;
00132     }
00133     virtual float getSensorValue(const short i, const Ice::Current&){
00134       return 0;
00135     }
00136 
00137     virtual bool getSensors(float &xPos, float &yPos, float &ori, const Ice::Current&)
00138     {
00139       return false; //not implimanted
00140     }
00141 
00142     virtual bool getDistanceAngle(float &dist, float &ang, const Ice::Current&)
00143     {
00144       //Read the sensors from a file and update the image
00145 
00146       char* linePtr=NULL;
00147       size_t len=0;
00148 
00149       ssize_t ret = getline(&linePtr, &len, itsStimulatorFd);
00150 
00151       if (ret == -1)
00152         LFATAL("End of input reached");
00153 
00154       std::string line(linePtr);
00155 
00156       vector<string> fields;
00157       fields.clear();
00158       if(line.length() > 1) {
00159         string::size_type start = 0;
00160         string::size_type end = line.find_first_of(" ");
00161         while(end != string::npos) {
00162           fields.push_back(line.substr(start, end-start));
00163           start = line.find_first_not_of(" ", end+1);
00164           end = line.find_first_of(" ", start);
00165         }
00166         fields.push_back(line.substr(start, line.length()-start));
00167       }
00168 
00169       if (fields.size() != 6)
00170         LFATAL("Bad data '%s'", line.c_str());
00171 
00172       dist = atof(fields[2].c_str());
00173       ang = atof(fields[3].c_str());
00174 
00175 
00176       int frame = atoi(fields[5].c_str());
00177 
00178       if (itsIfs->frame() != frame)
00179         LFATAL("Frame out of sync");
00180 
00181       itsIfs->updateNext(); //Update the frame here so we are synced with the image
00182 
00183       //sleep(2);
00184       usleep(200000);
00185      // getchar();
00186 
00187       return true;
00188 
00189     }
00190 
00191 
00192     virtual void motorsOff(const short i, const Ice::Current&){
00193       itsCurrentSpeed = 0;
00194       itsCurrentSteering = 0;
00195     }
00196     virtual void setMotor(const short i, const float val, const Ice::Current&){ }
00197     virtual short sendRawCmd(const std::string& data, const Ice::Current&) { return 0; }
00198     virtual void playSong(const short song, const Ice::Current&) { }
00199     virtual void shutdown(const Ice::Current&) { }
00200     virtual void sendStart(const Ice::Current&) { }
00201     virtual void setMode(const Robots::IRobotModes demo, const Ice::Current&) { }
00202     virtual void setDemo(const short demo, const Ice::Current&) { }
00203     virtual void setLED(const short led, const short color, const short intensity, const Ice::Current&) { }
00204 
00205 
00206   private:
00207     nub::soft_ref<OutputFrameSeries> itsOfs;
00208     nub::soft_ref<InputFrameSeries> itsIfs;
00209     OModelParam<std::string>        itsFilename;
00210     Image<PixRGB<byte> > itsCurrentFrame;
00211     float itsCurrentSpeed;
00212     float itsCurrentSteering;
00213     FILE*  itsStimulatorFd;
00214 
00215 };
00216 
00217 
00218 // ######################################################################
00219 class RobotBrainStimulatorService : public Ice::Service {
00220         protected:
00221                 virtual bool start(int, char*[]);
00222         private:
00223                 Ice::ObjectAdapterPtr itsAdapter;
00224     ModelManager *itsMgr;
00225 };
00226 
00227 bool RobotBrainStimulatorService::start(int argc, char* argv[])
00228 {
00229   // Instantiate a ModelManager:
00230         itsMgr = new ModelManager("Robot Brain Stimulator Service");
00231 
00232   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*itsMgr));
00233   itsMgr->addSubComponent(ofs);
00234 
00235   nub::ref<InputFrameSeries> ifs(new InputFrameSeries(*itsMgr));
00236   itsMgr->addSubComponent(ifs);
00237 
00238   // Instantiate our various ModelComponents:
00239   nub::ref<RobotBrainStimulator> robotBrainStimulator(new RobotBrainStimulator(*itsMgr, ofs, ifs));
00240   itsMgr->addSubComponent(robotBrainStimulator);
00241 
00242   // Parse command-line:
00243   if (itsMgr->parseCommandLine(argc, argv, "", 0, 0) == false)
00244     return(1);
00245 
00246   char endpointBuff[64];
00247   sprintf(endpointBuff, "default -p %d", 10000);
00248         itsAdapter = communicator()->createObjectAdapterWithEndpoints("RobotBrainStimulatorAdaptor", endpointBuff);
00249 
00250         Ice::ObjectPtr object = robotBrainStimulator.get();
00251   Ice::ObjectPrx objectPrx = itsAdapter->add(object, communicator()->stringToIdentity("IRobotService"));
00252         itsAdapter->activate();
00253 
00254   itsMgr->start();
00255 
00256   //IceUtil::ThreadPtr iRobotIThread = iRobotI.get();
00257   //iRobotIThread->start();
00258   LINFO("Started");
00259 
00260         return true;
00261 
00262 }
00263 
00264 // ######################################################################
00265 int main(int argc, char** argv) {
00266   RobotBrainStimulatorService svc;
00267   return svc.main(argc, argv);
00268 
00269 }
00270 
Generated on Sun May 8 08:41:32 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3