app-ScorbotServer.C

00001 #include <Ice/Ice.h>
00002 
00003 #include "Component/ModelComponent.H"
00004 #include "Component/ModelManager.H"
00005 #include "Robots/Scorbot/Scorbot.ice.H"
00006 #include "Robots/Scorbot/ScorbotInterface.H"
00007 
00008 #include <signal.h>
00009 
00010 
00011 // ######################################################################
00012 const ModelOptionCateg MOC_ScorbotServer = 
00013 { MOC_SORTPRI_3, "Scorbot Server Related Options" };
00014 
00015 const ModelOptionDef OPT_Port = 
00016 { MODOPT_ARG(std::string), "Port", &MOC_ScorbotServer, OPTEXP_CORE,
00017         "Scorbot Server Port Number",
00018         "scorbot-port", '\0', "", "10000" };
00019 
00020 
00021 
00022 class ScorbotI: public ScorbotIce::Scorbot, public ModelComponent
00023 {
00024         public:
00025                 ScorbotI(OptionManager& mgr,
00026                                 const std::string& descrName = "ScorbotServer",
00027                                 const std::string& tagName   = "ScorbotServer") :
00028                         ModelComponent(mgr, descrName, tagName),
00029                         itsScorbotInterface(new ScorbotInterface(mgr)), 
00030                         itsPort(&OPT_Port, this, 0)
00031                 { 
00032                         addSubComponent(itsScorbotInterface);
00033 
00034                         JointType2Joint_t[ScorbotIce::Base]     = ScorbotInterface::Base;
00035                         JointType2Joint_t[ScorbotIce::Shoulder] = ScorbotInterface::Shoulder;
00036                         JointType2Joint_t[ScorbotIce::Elbow]    = ScorbotInterface::Elbow;
00037                         JointType2Joint_t[ScorbotIce::Wrist1]   = ScorbotInterface::Wrist1;
00038                         JointType2Joint_t[ScorbotIce::Wrist2]   = ScorbotInterface::Wrist2;
00039                         JointType2Joint_t[ScorbotIce::Gripper]  = ScorbotInterface::Gripper;
00040                         JointType2Joint_t[ScorbotIce::Slider]   = ScorbotInterface::Slider;
00041 
00042                         Joint_t2JointType[ScorbotInterface::Base]     = ScorbotIce::Base;
00043                         Joint_t2JointType[ScorbotInterface::Shoulder] = ScorbotIce::Shoulder;
00044                         Joint_t2JointType[ScorbotInterface::Elbow]    = ScorbotIce::Elbow;
00045                         Joint_t2JointType[ScorbotInterface::Wrist1]   = ScorbotIce::Wrist1;
00046                         Joint_t2JointType[ScorbotInterface::Wrist2]   = ScorbotIce::Wrist2;
00047                         Joint_t2JointType[ScorbotInterface::Gripper]  = ScorbotIce::Gripper;
00048                         Joint_t2JointType[ScorbotInterface::Slider]   = ScorbotIce::Slider;
00049                 }
00050 
00051                 // ######################################################################
00052                 ScorbotInterface::encoderVals_t encoderValsType2encoderVals_t(ScorbotIce::encoderValsType encodersT)
00053                 {
00054                         ScorbotInterface::encoderVals_t encoders_t;
00055                         ScorbotIce::encoderValsType::iterator encIt;
00056                         for(encIt = encodersT.begin(); encIt != encodersT.end(); ++encIt)
00057                                 encoders_t[JointType2Joint_t[encIt->first]] = encIt->second;
00058                         return encoders_t;
00059                 }
00060 
00061                 // ######################################################################
00062                 ScorbotIce::encoderValsType encoderVals_t2encoderValsType(ScorbotInterface::encoderVals_t encoders_t)
00063                 {
00064                         ScorbotIce::encoderValsType encodersT;
00065                         ScorbotInterface::encoderVals_t::iterator encIt;
00066                         for(encIt = encoders_t.begin(); encIt != encoders_t.end(); ++encIt)
00067                                 encodersT[Joint_t2JointType[encIt->first]] = encIt->second;
00068                         return encodersT;
00069                 }
00070 
00071                 // ######################################################################
00072                 ScorbotIce::pwmValsType pwmVals_t2pwmValsType(ScorbotInterface::pwmVals_t pwms_t)
00073                 {
00074                         ScorbotIce::pwmValsType pwmsT;
00075                         ScorbotInterface::pwmVals_t::iterator encIt;
00076                         for(encIt = pwms_t.begin(); encIt != pwms_t.end(); ++encIt)
00077                                 pwmsT[Joint_t2JointType[encIt->first]] = encIt->second;
00078                         return pwmsT;
00079                 }
00080 
00081                 // ######################################################################
00082                 void init()
00083                 {
00084                 }
00085 
00086                 // ######################################################################
00087                 //! Set the joint to move to the given encoder position in the given amount of time
00088                 void setJoint(ScorbotIce::JointType joint, int encoderPos, int timeMS, const Ice::Current&)
00089                 {       itsScorbotInterface->setJoint(JointType2Joint_t[joint], encoderPos, timeMS); }
00090 
00091                 // ######################################################################
00092                 //! A convenience function to set multiple joint positions
00093                 /*! This function just loops over all positions in pos and sets them using setJointPos() !*/
00094                 void setJoints(const ScorbotIce::encoderValsType &pos, int timeMS, const Ice::Current&)
00095                 { itsScorbotInterface->setJoints(encoderValsType2encoderVals_t(pos), timeMS);   }
00096 
00097                 // ######################################################################
00098                 //! Get the position of a joint
00099                 int getEncoder(ScorbotIce::JointType joint, const Ice::Current&)
00100                 { return itsScorbotInterface->getEncoder(JointType2Joint_t[joint]);     }
00101 
00102                 // ######################################################################
00103                 //! Get the position of all joints
00104                 ScorbotIce::encoderValsType getEncoders(const Ice::Current&)
00105                 { return encoderVals_t2encoderValsType(itsScorbotInterface->getEncoders());     }
00106 
00107                 // ######################################################################
00108                 //! Turn on/off the motors at the Scorpower box
00109                 void setEnabled(bool enabled, const Ice::Current&)
00110                 {       itsScorbotInterface->setEnabled(enabled);       }
00111 
00112                 // ######################################################################
00113                 //! Reset all encoders to 0, and set the desired position of all joints to 0
00114                 void resetEncoders(const Ice::Current&)
00115                 { itsScorbotInterface->resetEncoders(); }
00116 
00117                 // ######################################################################
00118                 //! Get the current PWM value of a joint
00119                 float getPWM(ScorbotIce::JointType joint, const Ice::Current&)
00120                 {       return itsScorbotInterface->getPWM(JointType2Joint_t[joint]);   }
00121 
00122                 // ######################################################################
00123                 //! Get the current PWM values of all joints
00124                 ScorbotIce::pwmValsType getPWMs(const Ice::Current&)
00125                 {       return pwmVals_t2pwmValsType(itsScorbotInterface->getPWMs());   }
00126 
00127                 // ######################################################################
00128                 //! Set the PID control params for a joint
00129                 void setControlParams(ScorbotIce::JointType joint, 
00130                                 float pGain, float iGain, float dGain, float maxI, float maxPWM, float pwmOffset, const Ice::Current&)
00131                 {       itsScorbotInterface->setControlParams(JointType2Joint_t[joint], pGain, iGain, dGain, maxI, maxPWM, pwmOffset); }
00132 
00133                 // ######################################################################
00134                 //! Get the PID values for a joint from the microcontroller
00135                 void getPIDVals(ScorbotIce::JointType joint, float &pGain, float &iGain, float &dGain, float &maxI, float &maxPWM, float &pwmOffset, const Ice::Current&)
00136                 {       itsScorbotInterface->getPIDVals(JointType2Joint_t[joint], pGain, iGain, dGain, maxI, maxPWM, pwmOffset); }
00137 
00138                 // ######################################################################
00139                 //! Get the current target position and velocity
00140                 void getTuningVals(ScorbotIce::JointType joint,
00141                                 int &targetPos, int &targetVel, float &gravityCompensation, const Ice::Current&)
00142                 { itsScorbotInterface->getTuningVals(JointType2Joint_t[joint], targetPos, targetVel, gravityCompensation);      }
00143 
00144                 // ######################################################################
00145                 //! Set the gravity compensation parameters
00146                 void setGravityParameters(int upperArmMass, int foreArmMass, float compensationScale, const Ice::Current&)
00147                 {       itsScorbotInterface->setGravityParameters(upperArmMass, foreArmMass, compensationScale);        }
00148 
00149                 // ######################################################################
00150                 //! Get the current gravity compensation parameters
00151                 void getGravityParameters(int &upperArmMass, int &foreArmMass, float &compensationScale, const Ice::Current&)
00152                 {       itsScorbotInterface->getGravityParameters(upperArmMass, foreArmMass, compensationScale);        }
00153 
00154                 // ######################################################################
00155                 //! Get the requested port number
00156                 std::string getPort() { return itsPort.getVal(); }
00157 
00158                 void prepareForExit()
00159                 {
00160                         std::cerr << "#################### PREPARING FOR EXIT ####################" << std::endl;
00161 
00162                         sleep(1);
00163                 }
00164 
00165         private:
00166                 std::map<ScorbotIce::JointType, ScorbotInterface::Joint_t> JointType2Joint_t;
00167                 std::map<ScorbotInterface::Joint_t, ScorbotIce::JointType> Joint_t2JointType;
00168 
00169                 nub::soft_ref<ScorbotInterface> itsScorbotInterface;
00170                 OModelParam<std::string> itsPort;
00171 };
00172 nub::soft_ref<ScorbotI> scorbotServer;
00173 
00174 // ######################################################################
00175 void terminate(int s)
00176 {
00177         std::cerr <<
00178                 std::endl << "#################### INTERRUPT - SHUTTING DOWN SCORBOT ####################" << std::endl;
00179 
00180         scorbotServer->prepareForExit();
00181         exit(0);
00182 }
00183 
00184 // ######################################################################
00185 int main(int argc, char** argv)
00186 {
00187         signal(SIGHUP, terminate);  signal(SIGINT, terminate);
00188         signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00189         signal(SIGALRM, terminate);
00190 
00191         ModelManager manager("ScorbotServerManager");
00192         scorbotServer.reset(new ScorbotI(manager));
00193         manager.addSubComponent(scorbotServer);
00194         manager.parseCommandLine(argc, argv, "", 0, 0);
00195         manager.start();
00196 
00197         std::string connString("default -p ");
00198         connString += scorbotServer->getPort();
00199 
00200         int status = 0;
00201         Ice::CommunicatorPtr ic;
00202 
00203         try {
00204                 ic = Ice::initialize(argc, argv);
00205                 Ice::ObjectAdapterPtr adapter = 
00206                         ic->createObjectAdapterWithEndpoints(
00207                                         "ScorbotServerAdapter", connString.c_str());
00208                 Ice::ObjectPtr object = scorbotServer.get();
00209                 adapter->add(object, ic->stringToIdentity("ScorbotServer"));
00210                 adapter->activate();
00211                 scorbotServer->init();
00212                 ic->waitForShutdown();
00213         } catch(const Ice::Exception& e) {
00214                 std::cerr << e << std::endl;
00215                 status = 1;
00216         } catch(const char* msg)  {
00217                 std::cerr << msg << std::endl;
00218                 status = 1;
00219         }
00220         if(ic) {
00221                 try {
00222                         ic->destroy(); 
00223                 } catch(const Ice::Exception& e) {
00224                         std::cerr << e << std::endl;
00225                         status = 1;
00226                 }
00227         }
00228 
00229         scorbotServer->prepareForExit();
00230         return status;
00231 }
00232 
Generated on Sun May 8 08:04:10 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3