Skip to content
Snippets Groups Projects
DMPComponent.h 8.24 KiB
/*
 * This file is part of ArmarX.
 *
 * ArmarX is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * ArmarX is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    RobotAPI::ArmarXObjects::DMPComponent
 * @author     Mirko Waechter ( mirko dot waechter at kit dot edu )
 * @date       2015
 * @copyright  http://www.gnu.org/licenses/gpl.txt
 *             GNU General Public License
 */

#ifndef _ARMARX_COMPONENT_RobotAPI_DMPComponent_H
#define _ARMARX_COMPONENT_RobotAPI_DMPComponent_H



#include <RobotComponents/interface/components/DMPComponentBase.h>
#include <MemoryX/interface/component/WorkingMemoryInterface.h>
#include <MemoryX/interface/component/LongtermMemoryInterface.h>
#include <MemoryX/libraries/memorytypes/entity/DMPEntity.h>

#include <MemoryX/interface/memorytypes/MemoryEntities.h>
#include <MemoryX/interface/memorytypes/MemorySegments.h>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>


#include <boost/variant/variant.hpp>
#include <boost/variant/get.hpp>

#include <dmp/representation/dmp/dmpregistration.h>

#include <Core/core/Component.h>
#include "dmp/representation/dmp/dmpinterface.h"
#include "dmp/representation/dmp/basicdmp.h"
#include "dmp/representation/dmp/dmp3rdorder.h"
#include "dmp/representation/dmp/quaterniondmp.h"
#include "dmp/representation/dmp/endvelodmp.h"
#include "dmp/representation/dmp/dmp3rdorderforcefield.h"
#include "dmp/representation/dmp/forcefielddmp.h"
#include "dmp/representation/dmp/adaptive3rdorderdmp.h"
#include "dmp/representation/dmp/simpleendvelodmp.h"
//#include "dmp/representation/dmp/endveloforcefielddmp.h"
//#include "dmp/representation/dmp/endveloforcefieldwithobjrepulsiondmp.h"
//#include "dmp/representation/dmp/periodictransientdmp.h"

namespace armarx
{
    /**
     * @class DMPComponentPropertyDefinitions
     * @brief
     */
    #define ARMARX_DMPTYPE_BASICDMP 10
    #define ARMARX_DMPTYPE_ENDVELODMP 11
    #define ARMARX_DMPTYPE_SIMPLEENDVELODMP 12
    #define ARMARX_DMPTYPE_FORCEFIELDDMP 13
    #define ARMARX_DMPTYPE_ENDVELFORCEFILELDDMP 14
    #define ARMARX_DMPTYPE_PERIODICTRANSIENTDMP 15
    #define ARMARX_DMPTYPE_DMP3RDORDER 20
    #define ARMARX_DMPTYPE_DMP3RDORDERFORCEFIELD 21
    #define ARMARX_DMPTYPE_ADAPTIVEGOAL3RDORDERDMP 22
    #define ARMARX_DMPTYPE_QUATERNIONDMP 30



    typedef boost::variant<double, DMP::DVec, Eigen::Quaternionf> paraType;
    typedef std::map<int, paraType > configMap;
    typedef std::pair<int, paraType > configPair;

    class DMPComponentPropertyDefinitions:
        public armarx::ComponentPropertyDefinitions
    {
    public:
        DMPComponentPropertyDefinitions(std::string prefix):
            armarx::ComponentPropertyDefinitions(prefix)
        {
//            defineRequiredProperty<std::string>("LongtermMemoryName", "Description");
            defineOptionalProperty<std::string>("LongtermMemoryName", "LongtermMemory", "Name of the LongtermMemory component");
        }
    };

    /**
     * @class DMPComponent
     * @ingroup Components
     * @ingroup RobotAPI-Components
     * @brief A brief description
     *
     *
     * Detailed Description
     */


    class DMPComponent :
        virtual public Component,
        virtual public DMPComponentBase
    {
    public:
        typedef boost::shared_ptr<DMP::DMPInterface<DMP::DMPState> > BasicDMPInterfacePtr;
        typedef boost::shared_ptr<DMP::DMPInterface<DMP::_3rdOrderDMP> > DMP3rdOrderInterfacePtr;

        DMPComponent():
            basicdmp(new DMP::BasicDMP()),
            dmp3rdorder(new DMP::DMP3rdOrder()),
            dmpType(ARMARX_DMPTYPE_BASICDMP),
            tCurrent(0)
        {}

        /**
         * @see armarx::ManagedIceObject::getDefaultName()
         */
        virtual std::string getDefaultName() const
        {
            return "DMPComponent";
        }

        /**
         * @see PropertyUser::createPropertyDefinitions()
         */
        virtual armarx::PropertyDefinitionsPtr createPropertyDefinitions()
        {
            return armarx::PropertyDefinitionsPtr
                {
                    new DMPComponentPropertyDefinitions{getConfigIdentifier()}
                };
        }


        virtual void calculateWholeTrajectory();

        virtual DMP::Vec<DMP::DMPState> calculateNextState(DMP::Vec<DMP::DMPState> &currentStates, double t, double tInit, DMP::DVec &canonicalValues);
        virtual DMP::Vec<DMP::_3rdOrderDMP> calculateNextState(DMP::Vec<DMP::_3rdOrderDMP> &currentStates, double t, double tInit, DMP::DVec &canonicalValues);


        //transmit data from client to server (using ice)
        virtual void instantiateDMP(int DMPType, const ::Ice::Current& = ::Ice::Current());
        virtual void usingBasicDMP(const ::Ice::Current& = ::Ice::Current()){is3rdOrder = false;}
        virtual void using3rdOrderDMP(const ::Ice::Current& = ::Ice::Current()){is3rdOrder = true;}
        virtual void setDMPState(const ::armarx::cStateVec &state, const ::Ice::Current& = ::Ice::Current());
        virtual void setParameter(const int paraID, double value, const ::Ice::Current& = ::Ice::Current());
        virtual void setGoal(const DVector &value, const Ice::Current & = ::Ice::Current());
        virtual void setStartPosition(const DVector &value, const Ice::Current & = ::Ice::Current());
        virtual void setTimeStamps(const DVector &value, const Ice::Current & = ::Ice::Current());
        virtual void setCanonicalValues(const DVector &value, const Ice::Current & = ::Ice::Current());
        virtual void getDMPFromDatabase(const std::string &dmpName, const Ice::Current & = ::Ice::Current());
        virtual void readTrajectoryFromFile(const std::string &file, const Ice::Current & = ::Ice::Current());
        virtual void trainDMP(const ::Ice::Current& = ::Ice::Current());
        virtual void storeDMPInDatabase(const std::string &name, const ::Ice::Current& = ::Ice::Current());
        virtual void removeDMPFromDatabase(const std::string &name, const ::Ice::Current& = ::Ice::Current());



        //transmit data from server to client (using ice)
        virtual ::armarx::cStateVec getNextState(const ::Ice::Current& = ::Ice::Current());



        virtual int getBasicDMPType(){
            int basictype = (dmpType/10) * 10;
            if(basictype == 2){
                is3rdOrder = true;
            }
            return basictype;
        }

        virtual std::string getDMPTypeName();

        virtual void setConfigurationMap(configMap conf){configs = conf;setDMPConfiguration();}

        virtual void setDMPConfiguration();

        configMap constructConfigMap(DMP::Vec<int> paraIDs , DMP::Vec<paraType> paraVals);
        configMap configs;
        DMP::DVec timestamps;
        DMP::DVec canonicalValues;
int tCurrent;
    protected:
        BasicDMPInterfacePtr basicdmp;
        DMP3rdOrderInterfacePtr dmp3rdorder;
        DMP::Vec<DMP::SampledTrajectoryV2> trajs;

        int dmpType;

        DMP::Vec<DMP::DMPState> currentDMPState;
        DMP::Vec<DMP::_3rdOrderDMP> currentDMP3rdOrder;


        memoryx::LongtermMemoryInterfacePrx longtermMemoryPrx;
        memoryx::PersistentDMPDataSegmentBasePrx dmpDataMemoryPrx;

        /**
         * @see armarx::ManagedIceObject::onInitComponent()
         */
        virtual void onInitComponent();

        /**
         * @see armarx::ManagedIceObject::onConnectComponent()
         */
        virtual void onConnectComponent();

        /**
         * @see armarx::ManagedIceObject::onDisconnectComponent()
         */
        virtual void onDisconnectComponent();

        /**
         * @see armarx::ManagedIceObject::onExitComponent()
         */
        virtual void onExitComponent();

        /**
         * @see PropertyUser::createPropertyDefinitions()
         */
//        virtual armarx::PropertyDefinitionsPtr createPropertyDefinitions();
    };
    typedef ::IceInternal::Handle< ::armarx::DMPComponent> DMPComponentPtr;

}



#endif