app-ScorbotServer.C

00001 #include <Ice/Ice.h>
00002 #include "Ice/Scorbot.ice.H"
00003 #include "Component/ModelComponent.H"
00004 #include "Component/ModelManager.H"
00005 #include "Devices/Scorbot.H"
00006 #include <signal.h>
00007 
00008 // ######################################################################
00009 const ModelOptionCateg MOC_ScorbotServer = 
00010 { MOC_SORTPRI_3, "Scorbot Server Related Options" };
00011 
00012 const ModelOptionDef OPT_Port = 
00013 { MODOPT_ARG(std::string), "Port", &MOC_ScorbotServer, OPTEXP_CORE,
00014   "Scorbot Server Port Number",
00015   "scorbot-port", '\0', "", "10000" };
00016 
00017 class ScorbotI: public Robots::ScorbotIce, public ModelComponent
00018 {
00019   public:
00020     ScorbotI(OptionManager& mgr,
00021         const std::string& descrName = "ScorbotServer",
00022         const std::string& tagName = "ScorbotServer") :
00023       ModelComponent(mgr, descrName, tagName),
00024       itsScorbot(new Scorbot(mgr)), 
00025       itsPort(&OPT_Port, this, 0)
00026   { 
00027     addSubComponent(itsScorbot);
00028   }
00029 
00030     void init()
00031     {
00032             //Initial arm position will be 0-0
00033             Scorbot::ArmPos armPos;
00034             armPos.base = 0;
00035             armPos.sholder = 0;
00036             armPos.elbow = 0;
00037             armPos.wristRoll = 0;
00038             armPos.wristPitch = 0;
00039             armPos.ex1 =0;
00040             armPos.ex2 =0;
00041             itsScorbot->resetEncoders();
00042             itsScorbot->setArmPos(armPos);
00043             itsScorbot->motorsOn();
00044 
00045     }
00046 
00047     // ######################################################################
00048     //!Get the inverse kinematics
00049     Robots::ArmPos getIK(float x, float y, float z, const Ice::Current&)
00050     {
00051       Point3D<float> pos(x, y, z);
00052 
00053       Scorbot::ArmPos ikarmPos = itsScorbot->getIKArmPos(pos);
00054 
00055       Robots::ArmPos ikPosIce;
00056       ikPosIce.base = ikarmPos.base;
00057       ikPosIce.shoulder = ikarmPos.sholder;
00058       ikPosIce.elbow = ikarmPos.elbow;
00059       ikPosIce.gripper = ikarmPos.gripper;
00060       ikPosIce.wristroll = ikarmPos.wristRoll;
00061       ikPosIce.wristpitch = ikarmPos.wristPitch;
00062       ikPosIce.wrist1 = ikarmPos.wrist1;
00063       ikPosIce.wrist2 = ikarmPos.wrist2;
00064       ikPosIce.ex1 = ikarmPos.ex1;
00065       ikPosIce.ex2 = ikarmPos.ex2;
00066       return ikPosIce;
00067     }
00068 
00069     // ######################################################################
00070     //!Set the end effector position in x,y,z
00071     bool getEFpos(float &x, float &y, float &z, const Ice::Current&)
00072     {
00073       std::cout << __FUNCTION__ << std::endl;
00074       itsScorbot->getEF_pos(x, y, z);
00075       return true;
00076     }
00077 
00078     // ######################################################################
00079     //!get the end effector position in x,y,z
00080     bool setEFPos(float x,float y,float z, const Ice::Current&)
00081     {
00082       std::cout << __FUNCTION__ << std::endl;
00083       Scorbot::ArmPos IKpos = itsScorbot->getIKArmPos(Point3D<float>(x,y,z));
00084       itsScorbot->setArmPos(IKpos);
00085       return true;
00086     }
00087 
00088     // ######################################################################
00089     //! Set the motor pwm 
00090     bool setMotor(Robots::JOINTS joint, int pwm, const Ice::Current&)
00091     {
00092       std::cout << __FUNCTION__ << std::endl;
00093       return itsScorbot->setMotor(RobotArm::MOTOR(joint), pwm);
00094     }
00095 
00096     // ######################################################################
00097     //! Get the current pwm value
00098     int getPWM(Robots::JOINTS j, const Ice::Current&)
00099     {
00100       std::cout << __FUNCTION__ << std::endl;
00101       return itsScorbot->getPWM(RobotArm::MOTOR(j));
00102     }
00103 
00104     // ######################################################################
00105     //! Set the joint to a given position
00106     bool setJointPos(Robots::JOINTS joint, int pos, const Ice::Current&)
00107     {
00108       std::cout << __FUNCTION__ << std::endl;
00109       return itsScorbot->setJointPos(RobotArm::MOTOR(joint), pos);
00110     }
00111 
00112     // ######################################################################
00113     //! Get the joint position
00114     int getJointPos(Robots::JOINTS joint, const Ice::Current&)
00115     {
00116       std::cout << __FUNCTION__ << std::endl;
00117       return itsScorbot->getJointPos(RobotArm::MOTOR(joint));
00118     }
00119 
00120     // ######################################################################
00121     //! Get the anguler joint position
00122     float getEncoderAng(Robots::JOINTS joint, const Ice::Current&)
00123     {
00124       std::cout << __FUNCTION__ << std::endl;
00125       return itsScorbot->getEncoderAng(RobotArm::MOTOR(joint));
00126     }
00127 
00128     // ######################################################################
00129     //! Reset all encoders to 0
00130     void resetEncoders(const Ice::Current&)
00131     {
00132       std::cout << __FUNCTION__ << std::endl;
00133       itsScorbot->resetEncoders();
00134     }
00135 
00136     // ######################################################################
00137     //! Stop eveything
00138     void stopAllMotors(const Ice::Current&)
00139     {
00140       std::cout << __FUNCTION__ << std::endl;
00141       itsScorbot->stopAllMotors();
00142     }
00143 
00144     // ######################################################################
00145     void setSafety(bool val, const Ice::Current&)
00146     {
00147       std::cout << __FUNCTION__ << std::endl;
00148       itsScorbot->setSafety(val);
00149     }
00150 
00151     // ######################################################################
00152     //! Home Joint
00153     void homeMotor(Robots::JOINTS joint, int LimitSeekSpeed, int MSJumpSpeed,
00154         float MSJumpDelay, int MSSeekSpeed, bool MSStopCondition,
00155         bool checkMS, const Ice::Current&)
00156     {
00157       std::cout << __FUNCTION__ << std::endl;
00158       itsScorbot->homeMotor(RobotArm::MOTOR(joint), LimitSeekSpeed, MSJumpSpeed, 
00159           MSJumpSpeed, MSSeekSpeed, MSSeekSpeed, checkMS);
00160     }
00161 
00162     // ######################################################################
00163     //! Home All Joints
00164     void homeMotors(const Ice::Current&)
00165     {
00166       std::cout << __FUNCTION__ << std::endl;
00167       itsScorbot->homeMotors();
00168     }
00169 
00170     // ######################################################################
00171     //! Get the microSwitch states
00172     int getMicroSwitch(const Ice::Current&)
00173     {
00174       std::cout << __FUNCTION__ << std::endl;
00175       return itsScorbot->getMicroSwitch();
00176     }
00177 
00178     // ######################################################################
00179     //! Get the microswitch to a spacific joint
00180     int getMicroSwitchMotor(Robots::JOINTS m, const Ice::Current&)
00181     {
00182       std::cout << __FUNCTION__ << std::endl;
00183       return itsScorbot->getMicroSwitchMotor(RobotArm::MOTOR(m));
00184     }
00185 
00186     // ######################################################################
00187     //! Shutdown
00188     void shutdown(const Ice::Current&)
00189     {
00190       std::cout << __FUNCTION__ << std::endl;
00191       prepareForExit();
00192       exit(0);
00193     }
00194 
00195     void motorsOn(const Ice::Current&)
00196     {
00197       itsScorbot->motorsOn();
00198     }
00199 
00200     void motorsOff(const Ice::Current&)
00201     {
00202       itsScorbot->motorsOff();
00203     }
00204 
00205 
00206     // ######################################################################
00207     //! Convert encoder tics to angle
00208     double enc2ang(int encoderTicks, const Ice::Current&)
00209     {
00210       return itsScorbot->enc2ang(encoderTicks);
00211     }
00212 
00213     // ######################################################################
00214     //! Convert angle to encoder ticks
00215     int ang2enc(double degrees, const Ice::Current&)
00216     {
00217       return itsScorbot->ang2enc(degrees);
00218     }
00219 
00220     // ######################################################################
00221     //! Convert encoder ticks to mm
00222     double enc2mm(int encoderTicks, const Ice::Current&)
00223     {
00224       return itsScorbot->enc2mm(encoderTicks);
00225     }
00226 
00227     // ######################################################################
00228     //! Convert mm to encoder tics
00229     int mm2enc(double mm, const Ice::Current&)
00230     {
00231       return itsScorbot->mm2enc(mm);
00232     }
00233 
00234     bool setArmPos(const Robots::ArmPos& pos, const Ice::Current&)
00235     {
00236       Scorbot::ArmPos armPos;
00237       armPos.base = pos.base;
00238       armPos.sholder = pos.shoulder;
00239       armPos.elbow = pos.elbow;
00240       armPos.gripper = pos.gripper;
00241       armPos.wristRoll = pos.wristroll;
00242       armPos.wristPitch = pos.wristpitch;
00243       armPos.wrist1 = pos.wrist1;
00244       armPos.wrist2 = pos.wrist2;
00245       armPos.ex1 = pos.ex1;
00246       armPos.ex2 = pos.ex2;
00247       armPos.duration = pos.duration;
00248       itsScorbot->setArmPos(armPos);
00249       return true;
00250     }
00251 
00252     Robots::ArmPos getArmPos(const Ice::Current&)
00253     {
00254       Scorbot::ArmPos armPos = itsScorbot->getArmPos();
00255       Robots::ArmPos posIce;
00256       posIce.base = armPos.base;
00257       posIce.shoulder = armPos.sholder;
00258       posIce.elbow = armPos.elbow;
00259       posIce.gripper = armPos.gripper;
00260       posIce.wristroll = armPos.wristRoll;
00261       posIce.wristpitch = armPos.wristPitch;
00262       posIce.wrist1 = armPos.wrist1;
00263       posIce.wrist2 = armPos.wrist2;
00264       posIce.ex1 = armPos.ex1;
00265       posIce.ex2 = armPos.ex2;
00266       return posIce;
00267     }
00268     
00269 
00270     std::string getPort() { return itsPort.getVal(); }
00271 
00272     void prepareForExit()
00273     {
00274       itsScorbot->stopAllMotors();
00275       itsScorbot->motorsOff();
00276       sleep(1);
00277     }
00278 
00279   private:
00280     nub::soft_ref<Scorbot> itsScorbot;
00281     OModelParam<std::string> itsPort;
00282 };
00283 nub::soft_ref<ScorbotI> scorbotServer;
00284 
00285 // ######################################################################
00286 void terminate(int s)
00287 {
00288   std::cerr <<
00289     std::endl << "*** INTERRUPT - SHUTTING DOWN SCORBOT ***" << std::endl;
00290 
00291   scorbotServer->prepareForExit();
00292   exit(0);
00293 }
00294 
00295 // ######################################################################
00296 int main(int argc, char** argv)
00297 {
00298         signal(SIGHUP, terminate);  signal(SIGINT, terminate);
00299         signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00300         signal(SIGALRM, terminate);
00301 
00302   ModelManager manager("ScorbotServerManager");
00303   scorbotServer.reset(new ScorbotI(manager));
00304   manager.addSubComponent(scorbotServer);
00305   manager.parseCommandLine(argc, argv, "", 0, 0);
00306   manager.start();
00307 
00308   std::string connString("default -p ");
00309   connString += scorbotServer->getPort();
00310 
00311   int status = 0;
00312   Ice::CommunicatorPtr ic;
00313 
00314   try {
00315     ic = Ice::initialize(argc, argv);
00316     Ice::ObjectAdapterPtr adapter = 
00317       ic->createObjectAdapterWithEndpoints(
00318           "ScorbotServerAdapter", connString.c_str());
00319     Ice::ObjectPtr object = scorbotServer.get();
00320     adapter->add(object, ic->stringToIdentity("ScorbotServer"));
00321     adapter->activate();
00322     scorbotServer->init();
00323     ic->waitForShutdown();
00324   } catch(const Ice::Exception& e) {
00325     std::cerr << e << std::endl;
00326     status = 1;
00327   } catch(const char* msg)  {
00328     std::cerr << msg << std::endl;
00329     status = 1;
00330   }
00331   if(ic) {
00332     try {
00333       ic->destroy(); 
00334     } catch(const Ice::Exception& e) {
00335       std::cerr << e << std::endl;
00336       status = 1;
00337     }
00338   }
00339 
00340   scorbotServer->prepareForExit();
00341   return status;
00342 }
Generated on Sun May 8 08:04:10 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3