ocra-wbi-plugins
Doxygen documentation for the ocra-wbi-plugins repository
ocra-wbi-plugins/ocra-icub-server/include/ocra-icub-server/Thread.h
Go to the documentation of this file.
00001 
00009 /*
00010  *  This file is part of ocra-icub.
00011  *  Copyright (C) 2016 Institut des Systèmes Intelligents et de Robotique (ISIR)
00012  *
00013  *  This program is free software: you can redistribute it and/or modify
00014  *  it under the terms of the GNU General Public License as published by
00015  *  the Free Software Foundation, either version 3 of the License, or
00016  *  (at your option) any later version.
00017  *
00018  *  This program is distributed in the hope that it will be useful,
00019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  *  GNU General Public License for more details.
00022  *
00023  *  You should have received a copy of the GNU General Public License
00024  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025 */
00026 
00027 #ifndef OCRA_CONTROLLER_SERVER_THREAD_H
00028 #define OCRA_CONTROLLER_SERVER_THREAD_H
00029 
00030 #include <yarpWholeBodyInterface/yarpWholeBodyInterface.h>
00031 #include <wbi/wbi.h>
00032 
00033 #include <ocra-icub-server/IcubControllerServer.h>
00034 
00035 #include <ocra-icub/Utilities.h>
00036 #include <ocra/util/ErrorsHelper.h>
00037 
00038 #include <yarp/os/Bottle.h>
00039 #include <yarp/os/RpcServer.h>
00040 #include <yarp/os/ConnectionReader.h>
00041 #include <yarp/os/Time.h>
00042 
00043 #include <sstream>
00044 #include <string>
00045 
00046 #include <iDynTree/Estimation/SimpleLeggedOdometry.h>
00047 
00048 
00049 class OcraControllerOptions
00050 {
00051 // CLASS_POINTER_TYPEDEFS(OcraControllerOptions)
00052 
00053 public: // Functions
00056     OcraControllerOptions();
00057 
00060     ~OcraControllerOptions();
00061 
00062 public: // Variables
00063     int                     threadPeriod; 
00064     std::string             serverName; 
00065     std::string             robotName; 
00066     std::string             startupTaskSetPath; 
00067     std::string             startupSequence; 
00068     std::string             wbiConfigFilePath; 
00069     std::string             urdfModelPath; 
00070     bool                    runInDebugMode; 
00071     bool                    noOutputMode; 
00072     bool                    isFloatingBase; 
00073     bool                    useOdometry; 
00074     bool                    idleAnkles; 
00075     double                  idleAnkleTime; 
00076     bool                    maintainFinalPosture; 
00077     yarp::os::Property      yarpWbiOptions; 
00078     ocra_recipes::CONTROLLER_TYPE    controllerType; 
00079     ocra_recipes::SOLVER_TYPE    solver; 
00081     double wDdq;
00082     double wTau;
00083     double wFc;
00084 };
00085 
00086 
00093 class Thread: public yarp::os::RateThread
00094 {
00095 // CLASS_POINTER_TYPEDEFS(Thread)
00096 public:
00101     Thread(OcraControllerOptions& controller_options, std::shared_ptr<wbi::wholeBodyInterface> wbi);
00102 
00103     virtual ~Thread();
00104     bool threadInit();
00105     void run();
00106     void threadRelease();
00107 
00108 public:
00112     class ControllerRpcServerCallback : public yarp::os::PortReader
00113     {
00114     CLASS_POINTER_TYPEDEFS(ControllerRpcServerCallback)
00115 
00116     public:
00117 
00121         ControllerRpcServerCallback(Thread& threadRef);
00122 
00128         virtual bool read(yarp::os::ConnectionReader& connection);
00129 
00130     private:
00131 
00132         Thread& thread; 
00133     };
00134 
00135 public:
00139     class DebugRpcServerCallback : public yarp::os::PortReader
00140     {
00141     CLASS_POINTER_TYPEDEFS(DebugRpcServerCallback)
00142 
00143     public:
00144 
00148         DebugRpcServerCallback(Thread& threadRef);
00149 
00155         virtual bool read(yarp::os::ConnectionReader& connection);
00156 
00157     private:
00158 
00159         Thread& thread; 
00160     };
00161 
00162 private:
00163     ocra_icub::OCRA_ICUB_MESSAGE convertStringToOcraIcubMessage(const std::string& s);
00164     void parseIncomingMessage(yarp::os::Bottle& input, yarp::os::Bottle& reply);
00165     void parseDebugMessage(yarp::os::Bottle& input, yarp::os::Bottle& reply);
00166     void writeDebugData();
00167     void putAnklesIntoIdle(double idleTime);
00168     void sendTorqueReferenceToDebugJoint(int idx);
00169     bool setDebugJointToTorqueMode(int idx);
00170 
00171 private:
00172     ocra::Model::Ptr model;
00173     std::shared_ptr<IcubControllerServer> ctrlServer;
00174     static const int ALL_JOINTS = -1; 
00176     //TODO: Need to get the torque mins and maxs at the joint level.
00177     static const int TORQUE_MIN = -24; 
00178     static const int TORQUE_MAX = 24; 
00179     Eigen::ArrayXd minTorques; 
00180     Eigen::ArrayXd maxTorques; 
00183     OcraControllerOptions ctrlOptions; 
00184     std::shared_ptr<wbi::wholeBodyInterface> yarpWbi; 
00185     Eigen::VectorXd torques; 
00186     Eigen::VectorXd initialPosture; 
00189     ocra_icub::OCRA_ICUB_MESSAGE controllerStatus;
00190     ControllerRpcServerCallback::shared_ptr rpcServerCallback; 
00191     yarp::os::RpcServer rpcServerPort; 
00193     // Debugging related
00194     int debugJointIndex;
00195     yarp::os::RpcServer debugRpcPort;
00196     yarp::os::Port debugRefOutPort;
00197     yarp::os::Port debugRealOutPort;
00198     DebugRpcServerCallback::shared_ptr debugRpcCallback; 
00200     Eigen::VectorXd measuredTorques;
00201     bool debuggingAllJoints;
00202     bool userHasSetDebugIndex;
00203 
00204     Eigen::Displacementd l_foot_disp_inverse; 
00206     iDynTree::SimpleLeggedOdometry odometry; 
00207 };
00208 
00209 
00210 #endif // OCRA_CONTROLLER_SERVER_THREAD_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines