Skip to content
Snippets Groups Projects
MMMPlayer.h 8.01 KiB
Newer Older
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
/*
 * 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.
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
 *
 * 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.
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
 *
 * 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    RobotComponents::ArmarXObjects::MMMPlayer
 * @author     Mirko Waechter ( mirko dot waechter at kit dot edu )
 * @date       2014
 * @copyright  http://www.gnu.org/licenses/gpl-2.0.txt
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
 *             GNU General Public License
 */

#ifndef _ARMARX_COMPONENT_RobotComponents_MMMPlayer_H
#define _ARMARX_COMPONENT_RobotComponents_MMMPlayer_H

#include <MMM/Motion/Motion.h>

#include <ArmarXCore/core/Component.h>
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed

#include <RobotAPI/interface/units/KinematicUnitInterface.h>

#include <ArmarXCore/core/services/tasks/PeriodicTask.h>
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
#include <RobotAPI/libraries/core/PIDController.h>

#include <ArmarXCore/interface/observers/ObserverInterface.h>
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
#include <RobotComponents/interface/components/MMMPlayerInterface.h>


Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
namespace armarx
{
    /**
     * \class MMMPlayerPropertyDefinitions
     * \brief
     */
    class MMMPlayerPropertyDefinitions:
        public ComponentPropertyDefinitions
    {
    public:
        MMMPlayerPropertyDefinitions(std::string prefix):
            ComponentPropertyDefinitions(prefix)
        {
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
            defineRequiredProperty<std::string>("KinematicUnitName", "Name of the KinematicUnit to which the joint values should be sent");
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
            defineRequiredProperty<std::string>("KinematicTopicName", "Name of the KinematicUnit reporting topic");
            defineOptionalProperty<std::string>("ArmarXProjects", "", "Comma-separated list with names of ArmarXProjects (e.g. 'Armar3,Armar4'). The MMM XML File can be specified relatively to a data path of one of these projects.");
            defineOptionalProperty<std::string>("MMMFile", "", "Path to MMM XML File");
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
            defineOptionalProperty<float>("FPS", 100.0f, "FPS with which the recording should be executed. Velocities will be adapted.");
Patrick Grube's avatar
Patrick Grube committed
            defineOptionalProperty<bool>("LoopPlayback", false, "Specify whether motion should be repeated after execution")
            .setCaseInsensitive(true)
            .map("true",    true)
            .map("yes",     true)
            .map("1",       true)
            .map("false",   false)
            .map("no",      false)
            .map("0",       false);
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
            defineOptionalProperty<bool>("UsePIDController", true, "Specify whether the PID Controller should be used")
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
            .setCaseInsensitive(true)
            .map("true",    true)
            .map("yes",     true)
            .map("1",       true)
            .map("false",   false)
            .map("no",      false)
            .map("0",       false);
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
            defineOptionalProperty<float>("Kp", 1.f, "Proportional gain value of PID Controller");
            defineOptionalProperty<float>("Ki", 0.00001f, "Integral gain value of PID Controller");
            defineOptionalProperty<float>("Kd", 1.0f, "Differential gain value of PID Controller");
            defineOptionalProperty<float>("absMaximumVelocity", 120.0f, "The PID will never set a velocity above this threshold in degree");
            defineOptionalProperty<std::string>("RobotNodeSetName", "RobotState", "The name of the robot node set to be used (only need for PIDController mode)");

        }
    };

    /**
Mirko Wächter's avatar
Mirko Wächter committed
     * \defgroup Component-MMMPlayer MMMPlayer
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
     * \ingroup RobotComponents-Components
     * \brief Replays an MMM trajectory from a file.
     *
     * MMMPlayer reads an MMM trajectory from an MMM XML file (component property) and replays the motion using the KinematicUnit and its currently loaded robot.
     * The trajectory can be replayed using position control or velocity control.
     * In the latter case, the control parameters (P, I, D) can be configured via component properties.
     */
Mirko Wächter's avatar
Mirko Wächter committed

    /**
     * @ingroup Component-MMMPlayer
     * @brief The MMMPlayer class
     */
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
    class MMMPlayer :
        virtual public armarx::Component,
Patrick Grube's avatar
Patrick Grube committed
    public armarx::MMMPlayerInterface
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
    {
    public:
        /**
         * @see armarx::ManagedIceObject::getDefaultName()
         */
        virtual std::string getDefaultName() const
        {
            return "MMMPlayer";
        }

    protected:
        /**
         * @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 PropertyDefinitionsPtr createPropertyDefinitions();

        void run();
        void setJointAngles(int frameNumber);
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        MMM::MotionPtr motion;
        KinematicUnitInterfacePrx kinematicUnit;
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        DebugObserverInterfacePrx debugObserver;
        PeriodicTask<MMMPlayer>::pointer_type task;
        StringList jointNames;
Patrick Grube's avatar
Patrick Grube committed
        NameValueMap jointNamesUsed;
        std::string motionPath;
        bool usePIDController;
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        int currentFrame;
        double currentMotionTime, frameOffset;
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        float motionFPS;
        float desiredFPS;
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        std::map<std::string, PIDControllerPtr> PIDs;
        NameValueMap targetPositionValues;
Patrick Grube's avatar
Patrick Grube committed
        NameValueMap nullVelocities;
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        IceUtil::Time motionStartTime;
        //        float start, end;
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        int direction;
        NameValueMap jointOffets;
        float maxVel;
    private:
        bool start();
        bool pause();
        bool stop();
        void load(const std::string& filename, const std::string& projects);
        void updatePIDController(const NameValueMap& angles);
Patrick Grube's avatar
Patrick Grube committed
        RecursiveMutex mmmMutex;
        NameValueMap latestJointAngles;
        Mutex jointAnglesMutex;
Patrick Grube's avatar
Patrick Grube committed
        bool paused;
Patrick Grube's avatar
Patrick Grube committed

    public:
        // MMMPlayerInterface
        virtual bool startMMMPlayer(const Ice::Current&);
        virtual bool pauseMMMPlayer(const Ice::Current&);
        virtual bool stopMMMPlayer(const Ice::Current&);
        virtual bool loadTrajectory(const std::string& filename, const std::string& projects, const Ice::Current&);
        virtual int getNumberOfFrames(const Ice::Current&);
        virtual int getCurrentFrame(const Ice::Current&);
        virtual void setCurrentFrame(const int frame, const Ice::Current&);
        virtual bool setLoopPlayback(bool loop, const Ice::Current&);
        virtual std::string getMotionPath(const Ice::Current&);
        virtual StringList getJointNames(const Ice::Current&);
        virtual int setUsePID(bool usePID, const Ice::Current&);
        virtual void setFPS(float FPS, const Ice::Current&);
        virtual bool setJointsInUse(const std::string& jointName, bool inUse, const Ice::Current&);
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed

        // KinematicUnitListener interface
    public:
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        void reportControlModeChanged(const NameControlModeMap&, bool, const Ice::Current&) {}
        void reportJointAngles(const NameValueMap& angles, bool, const Ice::Current&);
        void reportJointVelocities(const NameValueMap&, bool, const Ice::Current&) {}
        void reportJointTorques(const NameValueMap&, bool, const Ice::Current&) {}
        void reportJointAccelerations(const NameValueMap&, bool, const Ice::Current&) {}
        void reportJointCurrents(const NameValueMap&, bool, const Ice::Current&) {}
        void reportJointMotorTemperatures(const NameValueMap&, bool, const Ice::Current&) {}
        void reportJointStatuses(const NameStatusMap&, bool, const Ice::Current&) {}