PrimaryMotorCortexService.C

Go to the documentation of this file.
00001 /*!@file NeovisionII/PrimaryMotorCortexService.C */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the 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
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/NeovisionII/PrimaryMotorCortexService.C $
00035 // $Id: PrimaryMotorCortexService.C 12962 2010-03-06 02:13:53Z irock $
00036 //
00037 
00038 #include "NeovisionII/PrimaryMotorCortexService.H"
00039 #include "Util/sformat.H"
00040 #include "Image/DrawOps.H"
00041 
00042 using namespace std;
00043 using namespace ImageIceMod;
00044 
00045 PrimaryMotorCortexI::PrimaryMotorCortexI(OptionManager& mgr,
00046     const std::string& descrName,
00047     const std::string& tagName) :
00048   ModelComponent(mgr, descrName, tagName)
00049 {
00050 
00051   //Subscribe to the various topics
00052   IceStorm::TopicPrx topicPrx;
00053   itsTopicsSubscriptions.push_back(TopicInfo("PrefrontalCortexTopic", topicPrx));
00054   itsTopicsSubscriptions.push_back(TopicInfo("HomeInterfaceTopic", topicPrx));
00055 
00056   itsScorbot = nub::soft_ref<Scorbot>(new Scorbot(mgr));
00057   addSubComponent(itsScorbot);
00058 
00059   itsHomePosition.base = 10413;
00060   itsHomePosition.sholder = -9056;
00061   itsHomePosition.elbow = 11867;
00062   itsHomePosition.wristRoll = -6707;
00063   itsHomePosition.wristPitch = 4513;
00064   itsHomePosition.gripper = -1434;
00065   itsHomePosition.ex1 = 500;
00066 }
00067 
00068 
00069 
00070 PrimaryMotorCortexI::~PrimaryMotorCortexI()
00071 {
00072   unsubscribeSimEvents();
00073 }
00074 
00075 void PrimaryMotorCortexI::start2()
00076 {
00077 }
00078 
00079 void PrimaryMotorCortexI::stop1()
00080 {
00081   //Set all of the motors to stay where they are, and turn on the control box
00082   itsScorbot->stopControl();
00083   itsScorbot->motorsOff();
00084   LINFO("Done");
00085 }
00086 
00087 void PrimaryMotorCortexI::init()
00088 {
00089   //Set all of the motors to stay where they are, and turn on the control box
00090   itsScorbot->motorsOn();
00091   itsScorbot->startControl();
00092   Scorbot::ArmPos currArmPos = itsScorbot->getArmPos();
00093   itsScorbot->setArmPos(currArmPos);
00094 
00095   goHome();
00096   LINFO("!At Home.");
00097 
00098 }
00099 
00100 
00101 void PrimaryMotorCortexI::initSimEvents(Ice::CommunicatorPtr icPtr, Ice::ObjectPrx objectPrx)
00102 {
00103   //Get the IceStorm object
00104   Ice::ObjectPrx obj = icPtr->stringToProxy("SimEvents/TopicManager:tcp -p 11111");
00105   IceStorm::TopicManagerPrx topicManager =
00106     IceStorm::TopicManagerPrx::checkedCast(obj);
00107 
00108   //Create a Segmenter Topic
00109   IceStorm::TopicPrx topic;
00110   try {
00111     topic = topicManager->retrieve("PrimaryMotorCortexTopic"); //check if the Retina topic exists
00112   } catch (const IceStorm::NoSuchTopic&) {
00113     topic = topicManager->create("PrimaryMotorCortexTopic"); //The retina topic does not exists, create
00114   }
00115   //Make a one way visualCortex message publisher for efficency
00116   Ice::ObjectPrx pub = topic->getPublisher()->ice_oneway();
00117   itsEventsPub = SimEvents::EventsPrx::uncheckedCast(pub);
00118 
00119   //Subscribe the SimulationViewer to theTopics
00120   itsObjectPrx = objectPrx;
00121   //subscribe  to the topics
00122   for(uint i=0; i<itsTopicsSubscriptions.size();  i++)
00123   {
00124     try {
00125       IceStorm::QoS qos;
00126       itsTopicsSubscriptions[i].topicPrx =
00127         topicManager->retrieve(itsTopicsSubscriptions[i].name.c_str()); //Get the
00128       itsTopicsSubscriptions[i].topicPrx->subscribeAndGetPublisher(qos, itsObjectPrx); //Subscribe to the retina topic
00129     } catch (const IceStorm::NoSuchTopic&) {
00130       LFATAL("Error! No %s topic found!", itsTopicsSubscriptions[i].name.c_str());
00131     } catch (const char* msg) {
00132       LINFO("Error %s", msg);
00133     } catch (const Ice::Exception& e) {
00134       cerr << e << endl;
00135     }
00136   }
00137 }
00138 
00139 void PrimaryMotorCortexI::unsubscribeSimEvents()
00140 {
00141   //Unsubscribe from all the topics we are registerd to
00142   for(uint i=0; i<itsTopicsSubscriptions.size();  i++)
00143   {
00144     itsTopicsSubscriptions[i].topicPrx->unsubscribe(itsObjectPrx);
00145   }
00146 }
00147 
00148 
00149 void PrimaryMotorCortexI::moveObject(Point3D<float> P1)
00150 {
00151   std::vector<std::string> PositionNames;
00152 
00153   std::vector<Scorbot::ArmPos> armPositions;
00154   Scorbot::ArmPos tmpArmPos;
00155 
00156   //Move the arm right above the object
00157   Point3D<float> aboveObjectPoint = P1;
00158   aboveObjectPoint.z = aboveObjectPoint.z + 100;
00159   tmpArmPos = itsScorbot->getIKArmPos(aboveObjectPoint);
00160   armPositions.push_back(tmpArmPos);
00161   PositionNames.push_back("Above Object");
00162 
00163   //Open the gripper
00164   tmpArmPos.gripper = 0;
00165   armPositions.push_back(tmpArmPos);
00166   PositionNames.push_back("Open Gripper");
00167 
00168   //Move to the actual object
00169   tmpArmPos = itsScorbot->getIKArmPos(P1);
00170   armPositions.push_back(tmpArmPos);
00171   PositionNames.push_back("Object");
00172 
00173   //Slide the object back
00174   Point3D<float> slidePoint = P1;
00175   slidePoint.y = slidePoint.y - 1000;
00176   tmpArmPos = itsScorbot->getIKArmPos(slidePoint);
00177   armPositions.push_back(tmpArmPos);
00178   PositionNames.push_back("Slide Object");
00179 
00180   //Raise the arm again
00181   slidePoint.z = slidePoint.z + 100;
00182   tmpArmPos = itsScorbot->getIKArmPos(slidePoint);
00183   armPositions.push_back(tmpArmPos);
00184   PositionNames.push_back("Above Slid Object");
00185 
00186   //Move the arm back on the slide
00187   tmpArmPos.ex1 = 1000;
00188   armPositions.push_back(tmpArmPos);
00189   PositionNames.push_back("Sliding Back");
00190 
00191 
00192   for(uint i=0; i<armPositions.size(); i++)
00193   {
00194     LINFO("Going To %s\n", PositionNames.at(i).c_str());
00195     itsScorbot->setArmPos(armPositions[i]);
00196     sleep(1);
00197     int timer = 0;
00198     while(!itsScorbot->moveDone() && timer < 300)
00199     {
00200       usleep(10000);
00201       timer++;
00202     }
00203     if(PositionNames.at(i) == "Object")
00204       sleep(5);
00205 //    LINFO("Pos %i reached in timer %i", i, timer);
00206   }
00207 
00208 
00209   LINFO("Finished Moving Object - Going Home");
00210   goHome();
00211 }
00212 
00213 void PrimaryMotorCortexI::goHome()
00214 {
00215 
00216   //Move Arm Home
00217   Scorbot::ArmPos itsHomePosition;
00218   itsHomePosition.base = 10413;
00219   itsHomePosition.sholder = -4500;
00220   itsHomePosition.elbow = 7567;
00221   itsHomePosition.wristRoll = 8371;
00222   itsHomePosition.wristPitch = 4811;
00223   itsHomePosition.gripper = -1434;
00224   itsHomePosition.ex1 = 500;
00225 
00226   std::vector<Scorbot::ArmPos> armPositions;
00227   Scorbot::ArmPos tmpArmPos = itsScorbot->getArmPos();
00228 
00229   LINFO("Add positions");
00230   {
00231     //Open the Gripper
00232     tmpArmPos.gripper = 0;
00233     armPositions.push_back(tmpArmPos);
00234 
00235     //Move the Shoulder and Elbow up
00236     tmpArmPos.sholder = itsHomePosition.sholder;
00237     tmpArmPos.elbow   = itsHomePosition.elbow;
00238     tmpArmPos.wristRoll   = itsHomePosition.wristRoll;
00239     tmpArmPos.wristPitch   = itsHomePosition.wristPitch;
00240     armPositions.push_back(tmpArmPos);
00241 
00242     //Move the slide back
00243     tmpArmPos.ex1 = itsHomePosition.ex1;
00244     armPositions.push_back(tmpArmPos);
00245 
00246     //Rotate the base
00247     tmpArmPos.base = itsHomePosition.base;
00248     armPositions.push_back(tmpArmPos);
00249   }
00250 
00251   LINFO("Move to positions");
00252 
00253   for(uint i=0; i<armPositions.size(); i++)
00254   {
00255     LINFO("Going to Pos %i...", i);
00256     itsScorbot->setArmPos(armPositions[i]);
00257     sleep(1);
00258     int timer = 0;
00259     while(!itsScorbot->moveDone() && timer < 300)
00260     {
00261       usleep(10000);
00262       timer++;
00263     }
00264     LINFO("Pos %i reached in timer %i", i, timer);
00265   }
00266 }
00267 
00268 
00269 void PrimaryMotorCortexI::evolve(const SimEvents::EventMessagePtr& eMsg,
00270     const Ice::Current&)
00271 {
00272   LINFO("Got message");
00273   if (eMsg->ice_isA("::SimEvents::PrimaryMotorCortexBiasMessage"))
00274   {
00275     SimEvents::PrimaryMotorCortexBiasMessagePtr msg = SimEvents::PrimaryMotorCortexBiasMessagePtr::dynamicCast(eMsg);
00276 
00277     if (msg->moveArm)
00278     {
00279       LINFO("Move Object");
00280       float x = msg->armPos.x;
00281       float y = msg->armPos.y;
00282       float z = msg->armPos.z;
00283 
00284       moveObject(Point3D<float>(x,y,z));
00285 
00286     }
00287   }
00288 
00289 }
00290 
00291 /////////////////////////// The VC Service to init the retina and start as a deamon ///////////////
00292 class PrimaryMotorCortexService : public Ice::Service {
00293   protected:
00294     virtual bool start(int, char* argv[]);
00295     virtual bool stop() {
00296       if (itsMgr)
00297         delete itsMgr;
00298       return true;
00299     }
00300 
00301   private:
00302     Ice::ObjectAdapterPtr itsAdapter;
00303     ModelManager *itsMgr;
00304 };
00305 
00306 bool PrimaryMotorCortexService::start(int argc, char* argv[])
00307 {
00308 
00309   itsMgr = new ModelManager("PrimaryMotorCortexService");
00310 
00311   nub::ref<PrimaryMotorCortexI> pmc(new PrimaryMotorCortexI(*itsMgr));
00312   itsMgr->addSubComponent(pmc);
00313 
00314   itsMgr->parseCommandLine((const int)argc, (const char**)argv, "", 0, 0);
00315 
00316   char adapterStr[255];
00317   sprintf(adapterStr, "default -p %i", BrainObjects::PrimaryMotorCortexPort);
00318   itsAdapter = communicator()->createObjectAdapterWithEndpoints("PMCAdapter",
00319       adapterStr);
00320 
00321   Ice::ObjectPtr object = pmc.get();
00322   Ice::ObjectPrx objectPrx = itsAdapter->add(object, communicator()->stringToIdentity("PMC"));
00323   pmc->initSimEvents(communicator(), objectPrx);
00324   itsAdapter->activate();
00325 
00326   itsMgr->start();
00327   pmc->init();
00328 
00329   return true;
00330 }
00331 
00332 // ######################################################################
00333 int main(int argc, char** argv) {
00334 
00335   PrimaryMotorCortexService svc;
00336   return svc.main(argc, argv);
00337 }
Generated on Sun May 8 08:41:02 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3