app-ScorbotLabel.C

00001 #include <Ice/Ice.h>
00002 #include "Raster/Raster.H"
00003 #include "Component/ModelManager.H"
00004 #include "Media/FrameSeries.H"
00005 #include "Raster/GenericFrame.H"
00006 #include "Transport/FrameInfo.H"
00007 #include "Image/Image.H"
00008 #include "Image/DrawOps.H"
00009 #include "GUI/PrefsWindow.H"
00010 #include "Devices/DeviceOpts.H"
00011 #include "Devices/Serial.H"
00012 #include "Image/DrawOps.H"
00013 #include <vector>
00014 #include "GUI/XWinManaged.H"
00015 #include "GUI/ImageDisplayStream.H"
00016 #include "GUI/PrefsWindow.H"
00017 #include "GUI/DebugWin.H"
00018 #include <pthread.h>
00019 #include "GUI/PrefsWindow.H"
00020 #include <iomanip>
00021 #include <signal.h>
00022 
00023 
00024 std::string filePrefix;
00025 std::vector<Image<PixRGB<byte> > > imageStore;
00026 nub::soft_ref<OutputFrameSeries> ofs;
00027 nub::soft_ref<InputFrameSeries> ifs;
00028 
00029 
00030 
00031 // ######################################################################
00032 void terminate(int s)
00033 {
00034         std::cout <<
00035                 std::endl << "#################### INTERRUPT - WRITING IMAGES ####################" << std::endl;
00036 
00037         for(size_t i=0; i<imageStore.size(); ++i)
00038         {
00039                 std::cout << i+1 << "/" << imageStore.size() << std::endl;
00040     std::ostringstream ss;
00041     ss << filePrefix << "/" << std::setfill('0') << std::setw(5) << i << ".pnm";
00042     Raster::WriteRGB(imageStore[i], ss.str());
00043         }
00044         exit(0);
00045 }
00046 
00047 
00048 class ScorbotReallySimple: public ModelComponent
00049 {
00050   public:
00051     ScorbotReallySimple(OptionManager& mgr, 
00052         const std::string& descrName = "",
00053         const std::string& tagName = "") :
00054       ModelComponent(mgr, descrName, tagName),
00055       itsSerial(new Serial(mgr)) 
00056     {
00057       addSubComponent(itsSerial);
00058       itsSerial->configure("/dev/ttyUSB0", 115200, "8N1", false, false, 10000);
00059       //itsSerial->setBlocking(true);
00060     }
00061 
00062     bool getState()
00063     {
00064 
00065       int ret;
00066       char retVal = '?';
00067       char cmd = 'S';
00068       itsSerial->write(&cmd, 1);
00069       usleep(5000);
00070 
00071       ret = itsSerial->read(&retVal, 1);
00072       if (ret != 1 || retVal != '0' || retVal != '1')
00073       {
00074         LINFO("%i: RetVal %c", ret, retVal);
00075       }
00076       return (retVal == '1');
00077     }
00078     
00079     void setPos(int pos)
00080     {
00081       LINFO("Moving to %i", pos);
00082       char cmd[2];
00083       cmd[0] = 'M';
00084       cmd[1] = pos;
00085       itsSerial->write(&cmd, 2);
00086 
00087       sleep(5);
00088 
00089       //LINFO("Wait for 0");
00090       //while(getState()) usleep(100000);
00091       LINFO("Wait for 1");
00092       while(!getState()) usleep(10000);
00093       LINFO("Move Done");
00094     }
00095 
00096   private:
00097     nub::ref<Serial> itsSerial;
00098 };
00099 
00100 // ######################################################################
00101 int getKey()
00102 {
00103   const nub::soft_ref<ImageDisplayStream> ids =
00104     ofs->findFrameDestType<ImageDisplayStream>();
00105 
00106   const rutz::shared_ptr<XWinManaged> uiwin =
00107     ids.is_valid()
00108     ? ids->getWindow("Output")
00109     : rutz::shared_ptr<XWinManaged>();
00110   return uiwin->getLastKeyPress();
00111 }
00112 
00113 // ######################################################################
00114 int main(int argc, char* argv[])
00115 {
00116         signal(SIGHUP, terminate);  signal(SIGINT, terminate);
00117         signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00118         signal(SIGALRM, terminate);
00119 
00120   LINFO("#############################################STARTING##############################################");
00121   //srand(unsigned(time(NULL)));
00122 
00123   ModelManager mgr("App Scorbot Label");
00124 
00125   nub::ref<ScorbotReallySimple> scorbot(new ScorbotReallySimple(mgr));
00126   mgr.addSubComponent(scorbot);
00127 
00128   ifs.reset(new InputFrameSeries(mgr));
00129   mgr.addSubComponent(ifs);
00130 
00131   ofs.reset(new OutputFrameSeries(mgr));
00132   mgr.addSubComponent(ofs);
00133 
00134   if(mgr.parseCommandLine(argc, argv, "FilePrefix", 1, 1) == false) return -1;
00135   mgr.start();
00136   
00137         filePrefix = mgr.getExtraArg(0);
00138   
00139         PrefsWindow pWin("Camera Control", SimpleFont::FIXED(8));
00140         pWin.setValueNumChars(16);
00141         pWin.addPrefsForComponent(ifs.get(), true);
00142         pWin.update();
00143         
00144 
00145 
00146   // state 0 -> 1 means arm just started moving toward home
00147   // state 1 means on the way to home position or waiting at the home postion
00148   // state 1 -> 0 means the path started
00149   
00150         bool isStartDetected      = false;
00151         bool isFirstEdgeDetected  = false;
00152         bool isSecondEdgeDetected = false;
00153 
00154   
00155   ifs->startStream();
00156   while(true)
00157   {
00158                 pWin.update();
00159     if(ifs->updateNext() == FRAME_COMPLETE) break;
00160     GenericFrame input = ifs->readFrame();
00161     
00162     //Display the new frame
00163     Image<PixRGB<byte> > img = input.asRgb();
00164     ofs->writeRGB(img, "Output", FrameInfo("output", SRC_POS));
00165     ofs->updateNext();
00166 
00167                 if(isStartDetected)
00168                         imageStore.push_back(img);
00169 
00170     if (!isStartDetected)
00171     {
00172       if (!isFirstEdgeDetected)
00173       {
00174         LINFO("####### Restart the arm program...");
00175         if (!scorbot->getState()) continue;
00176         isFirstEdgeDetected = true;
00177         continue;
00178       }
00179       if (!isSecondEdgeDetected)
00180       {
00181         LINFO("####### Waiting for the arm to home...");
00182         if (scorbot->getState()) continue;
00183         isSecondEdgeDetected = true;
00184         isStartDetected = true;
00185         LINFO("####### Start recording...");
00186         continue;
00187       }
00188     }
00189   }  while(true) 
00190   
00191   return 0;
00192 } // main
00193 
Generated on Sun May 8 08:41:32 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3