Skip to content
Snippets Groups Projects
MMMPlayer.cpp 23.6 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
 */

#include "MMMPlayer.h"
#include <MMM/Motion/MotionReaderXML.h>

#include <ArmarXCore/core/exceptions/local/ExpressionException.h>
#include <ArmarXCore/core/system/ArmarXDataPath.h>
#include <ArmarXCore/observers/variant/Variant.h>
#include <ArmarXCore/interface/observers/VariantBase.h>
#include <ArmarXCore/core/system/cmake/CMakePackageFinder.h>
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed

using namespace armarx;
using namespace MMM;


void MMMPlayer::onInitComponent()
{
    usingProxy(getProperty<std::string>("KinematicUnitName").getValue());
    usingProxy("DebugObserver");

    motionFPS = 1.0f;
    desiredFPS = 1.0f;
Patrick Grube's avatar
Patrick Grube committed
    paused = true;

Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
    usePIDController = getProperty<bool>("UsePIDController").getValue();
    loopPlayback = getProperty<bool>("LoopPlayback").getValue();
Patrick Grube's avatar
Patrick Grube committed
    desiredFPS = getProperty<float>("FPS").getValue();
    maxVel = getProperty<float>("absMaximumVelocity").getValue();

    usingTopic(getProperty<std::string>("KinematicTopicName").getValue());
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
}


void MMMPlayer::onConnectComponent()
{
    kinematicUnit = getProxy<KinematicUnitInterfacePrx>(getProperty<std::string>("KinematicUnitName").getValue());

    debugObserver = getProxy<DebugObserverInterfacePrx>("DebugObserver");
    std::string armarxProjects = getProperty<std::string>("ArmarXProjects").getValue();
    if (!armarxProjects.empty())
    {
        std::vector<std::string> projects;
        boost::split(projects, armarxProjects, boost::is_any_of(",;"), boost::token_compress_on);

        for (std::string & p : projects)
            //            ARMARX_INFO << "Adding to datapaths of " << p;
            armarx::CMakePackageFinder finder(p);
            if (!finder.packageFound())
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
            {
                ARMARX_WARNING << "ArmarX Package " << p << " has not been found!";
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
            {
                ARMARX_INFO << "Adding " << p << " to datapaths: " << finder.getDataDir();
                armarx::ArmarXDataPath::addDataPaths(finder.getDataDir());
    std::string motionDefault = getProperty<std::string>("MMMFile").getValue();
    if (!motionDefault.empty())
    {
        load(motionDefault, "Armar4");
    }
bool MMMPlayer::start()
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
{
Patrick Grube's avatar
Patrick Grube committed
    ScopedRecursiveLock lock(mmmMutex);

    if (!motion)
    {
    motionStartTime = IceUtil::Time::now();
Patrick Grube's avatar
Patrick Grube committed
    currentFrame = 0;
    frameOffset = 0;
        task = new PeriodicTask<MMMPlayer>(this, &MMMPlayer::run, 10, false, "MMMPlayerTask");
        task->start();
Patrick Grube's avatar
Patrick Grube committed
        motionStartTime = IceUtil::Time::now();
        paused = false;
        return task->isRunning();
    }
    catch (...)
    {
        ARMARX_WARNING << "Failed to start MMMPLayer task";
    }
bool MMMPlayer::startMMMPlayer(const Ice::Current&)
Patrick Grube's avatar
Patrick Grube committed
    ScopedRecursiveLock lock(mmmMutex);
    bool started = MMMPlayer::start();
        ARMARX_INFO << "started MMMPlayer from GUI";
bool MMMPlayer::pauseMMMPlayer(const Ice::Current&)
Patrick Grube's avatar
Patrick Grube committed
    ScopedRecursiveLock lock(mmmMutex);

Patrick Grube's avatar
Patrick Grube committed
        return false;
Patrick Grube's avatar
Patrick Grube committed
    if (!paused)
Patrick Grube's avatar
Patrick Grube committed
        paused = true;
        kinematicUnit->setJointVelocities(nullVelocities);
Patrick Grube's avatar
Patrick Grube committed

Patrick Grube's avatar
Patrick Grube committed
        if (direction < 0)
        {
            frameOffset = motion->getNumFrames() - currentFrame;
Patrick Grube's avatar
Patrick Grube committed
            frameOffset = currentFrame;
Patrick Grube's avatar
Patrick Grube committed
        }
Patrick Grube's avatar
Patrick Grube committed
        ARMARX_INFO << "MMMPlayer paused from GUI at frame " << currentFrame;
Patrick Grube's avatar
Patrick Grube committed
    }
Patrick Grube's avatar
Patrick Grube committed
    else
Patrick Grube's avatar
Patrick Grube committed
    {
Patrick Grube's avatar
Patrick Grube committed
        motionStartTime = IceUtil::Time::now();
        paused = false;
        ARMARX_INFO << "MMMPlayer resumed from GUI";
Patrick Grube's avatar
Patrick Grube committed
    }
Patrick Grube's avatar
Patrick Grube committed
    return paused;
bool MMMPlayer::stopMMMPlayer(const Ice::Current&)
Patrick Grube's avatar
Patrick Grube committed
    ScopedRecursiveLock lock(mmmMutex);

Patrick Grube's avatar
Patrick Grube committed
    paused = true;

        if (task->isRunning())
        {
            ARMARX_WARNING << "Failed to stop MMMPlayer";
            ARMARX_INFO << "stopped MMMPlayer task from GUI";
Patrick Grube's avatar
Patrick Grube committed

        kinematicUnit->setJointVelocities(nullVelocities);
Patrick Grube's avatar
Patrick Grube committed

        return !(task->isRunning());
    }
    catch (...)
        ARMARX_WARNING << "Failed to stop MMMPlayer";
void MMMPlayer::load(const std::string& MMMFile, const std::string& projects)
Patrick Grube's avatar
Patrick Grube committed
    ScopedRecursiveLock lock(mmmMutex);
    currentFrame = 0;
    direction = 1;

    //    start = 0.3;
    //    end = 1.26;
    //    jointOffets["Right Arm Shoulder2"] = 20.0/180.0*M_PI;
    //    jointOffets["Left Arm Shoulder2"] = 20.0/180.0*M_PI;
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
    {
        std::vector<std::string> proj;
        boost::split(proj, projects, boost::is_any_of(",;"), boost::token_compress_on);

        for (std::string & p : proj)
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        {
            ARMARX_INFO << "Adding to datapaths of " << p;
            armarx::CMakePackageFinder finder(p);
            if (!finder.packageFound())
            {
                ARMARX_WARNING << "ArmarX Package " << p << " has not been found!";
            {
                ARMARX_INFO << "Adding to datapaths: " << finder.getDataDir();
                armarx::ArmarXDataPath::addDataPaths(finder.getDataDir());
            }
    MotionReaderXML reader;
    ArmarXDataPath::getAbsolutePath(MMMFile, motionPath);
    ARMARX_VERBOSE << "Set motionPath to " + motionPath ;
    motion = reader.loadMotion(motionPath);

    if (!motion)
    if (motion->getNumFrames() > 1)
    {
        float frameDuration = motion->getMotionFrame(1)->timestep - motion->getMotionFrame(0)->timestep;
        ARMARX_CHECK_EXPRESSION(frameDuration != 0);
        ARMARX_CHECK_EXPRESSION(frameDuration > 0)
        motionFPS = 1.0f / frameDuration;
        ARMARX_VERBOSE << VAROUT(motionFPS);
    }

    jointNames = motion->getJointNames();
    /* Debug: Clamp to joint limits
        unsigned int left_hip1_index = 0;
        unsigned int left_hip2_index = 0;
        unsigned int left_hip3_index = 0;
        unsigned int left_knee_index = 0;*/
    unsigned int left_ankle1_index = 0;
    unsigned int left_ankle2_index = 0;

    /*unsigned int right_hip1_index = 0;
    unsigned int right_hip2_index = 0;
    unsigned int right_hip3_index = 0;
    unsigned int right_knee_index = 0;*/
    unsigned int right_ankle1_index = 0;
    unsigned int right_ankle2_index = 0;



    for (unsigned int i = 0; i < jointNames.size(); i++)
    {
        /*if (jointNames[i] == "LegL_Hip1_joint")
        {
            left_hip1_index = i;
        }

        if (jointNames[i] == "LegL_Hip2_joint")
        {
            left_hip2_index = i;
        }

        if (jointNames[i] == "LegL_Hip3_joint")
        {
            left_hip3_index = i;
        }

        if (jointNames[i] == "LegL_Knee_joint")
        {
            left_knee_index = i;
        }
        */
        if (jointNames[i] == "LegL_Ank1_joint")
        {
            left_ankle1_index = i;
        }

        if (jointNames[i] == "LegL_Ank2_joint")
        {
            left_ankle2_index = i;
        }

        /*
                if (jointNames[i] == "LegR_Hip1_joint")
                {
                    right_hip1_index = i;
                }

                if (jointNames[i] == "LegR_Hip2_joint")
                {
                    right_hip2_index = i;
                }

                if (jointNames[i] == "LegR_Hip3_joint")
                {
                    right_hip3_index = i;
                }

                if (jointNames[i] == "LegR_Knee_joint")
                {
                    right_knee_index = i;
                }
        */
        if (jointNames[i] == "LegR_Ank1_joint")
        {
            right_ankle1_index = i;
        }

        if (jointNames[i] == "LegR_Ank2_joint")
        {
            right_ankle2_index = i;
        }



    }

    for (unsigned int i = 0; i < motion->getNumFrames(); i++)
    {
        //ARMARX_INFO << "Trajectory clamped";
        /*motion->getMotionFrame(i)->joint[left_hip1_index] = std::max(-0.44f, motion->getMotionFrame(i)->joint[left_hip1_index]);
        motion->getMotionFrame(i)->joint[left_hip1_index] = std::min(1.53f, motion->getMotionFrame(i)->joint[left_hip1_index]);
        motion->getMotionFrame(i)->joint[left_hip2_index] = std::max(-0.13f, motion->getMotionFrame(i)->joint[left_hip2_index]);
        motion->getMotionFrame(i)->joint[left_hip2_index] = std::min(0.60f, motion->getMotionFrame(i)->joint[left_hip2_index]);
        motion->getMotionFrame(i)->joint[left_hip3_index] = std::max(-0.60f, motion->getMotionFrame(i)->joint[left_hip3_index]);
        motion->getMotionFrame(i)->joint[left_hip3_index] = std::min(0.39f, motion->getMotionFrame(i)->joint[left_hip3_index]);
        motion->getMotionFrame(i)->joint[left_knee_index] = std::max(0.1f, motion->getMotionFrame(i)->joint[left_knee_index]);
        motion->getMotionFrame(i)->joint[left_knee_index] = std::min(1.61f, motion->getMotionFrame(i)->joint[left_knee_index]);*/
        motion->getMotionFrame(i)->joint[left_ankle1_index] = std::max(-0.45f, motion->getMotionFrame(i)->joint[left_ankle1_index]);
        motion->getMotionFrame(i)->joint[left_ankle1_index] = std::min(0.45f, motion->getMotionFrame(i)->joint[left_ankle1_index]);
        motion->getMotionFrame(i)->joint[left_ankle2_index] = std::max(-0.20f, motion->getMotionFrame(i)->joint[left_ankle2_index]);
        motion->getMotionFrame(i)->joint[left_ankle2_index] = std::min(0.20f, motion->getMotionFrame(i)->joint[left_ankle2_index]);


        /* motion->getMotionFrame(i)->joint[right_hip1_index] = std::max(-0.47f, motion->getMotionFrame(i)->joint[right_hip1_index]);
         motion->getMotionFrame(i)->joint[right_hip1_index] = std::min(1.56f, motion->getMotionFrame(i)->joint[right_hip1_index]);
         motion->getMotionFrame(i)->joint[right_hip2_index] = std::max(-0.16f, motion->getMotionFrame(i)->joint[right_hip2_index]);
         motion->getMotionFrame(i)->joint[right_hip2_index] = std::min(0.63f, motion->getMotionFrame(i)->joint[right_hip2_index]);
         motion->getMotionFrame(i)->joint[right_hip3_index] = std::max(-0.63f, motion->getMotionFrame(i)->joint[right_hip3_index]);
         motion->getMotionFrame(i)->joint[right_hip3_index] = std::min(0.39f, motion->getMotionFrame(i)->joint[right_hip3_index]);
         motion->getMotionFrame(i)->joint[right_knee_index] = std::max(0.1f, motion->getMotionFrame(i)->joint[right_knee_index]);
         motion->getMotionFrame(i)->joint[right_knee_index] = std::min(1.61f, motion->getMotionFrame(i)->joint[right_knee_index]);*/
        motion->getMotionFrame(i)->joint[right_ankle1_index] = std::max(-0.45f, motion->getMotionFrame(i)->joint[right_ankle1_index]);
        motion->getMotionFrame(i)->joint[right_ankle1_index] = std::min(0.45f, motion->getMotionFrame(i)->joint[right_ankle1_index]);
        motion->getMotionFrame(i)->joint[right_ankle2_index] = std::max(-0.20f, motion->getMotionFrame(i)->joint[right_ankle2_index]);
        motion->getMotionFrame(i)->joint[right_ankle2_index] = std::min(0.20f, motion->getMotionFrame(i)->joint[right_ankle2_index]);

    }

    NameControlModeMap modes;
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
    for (size_t i = 0; i < jointNames.size(); ++i)
    {
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        const auto& jointName = jointNames.at(i);

        if (usePIDController)
            modes[jointName] = eVelocityControl;
            modes[jointName] = ePositionControl;
Patrick Grube's avatar
Patrick Grube committed
        nullVelocities[jointName] = 0.0;
        jointNamesUsed[jointName] = true;
    kinematicUnit->switchControlMode(modes);
    kinematicUnit->setJointVelocities(nullVelocities);
bool MMMPlayer::loadTrajectory(const std::string& MMMFile, const std::string& projects, const Ice::Current&)
Patrick Grube's avatar
Patrick Grube committed
    ARMARX_VERBOSE << "loaded trajectory " << MMMFile;
    load(MMMFile, projects);
Patrick Grube's avatar
Patrick Grube committed
        return false;
Patrick Grube's avatar
Patrick Grube committed
        return true;
Patrick Grube's avatar
Patrick Grube committed
}

int MMMPlayer::getNumberOfFrames(const Ice::Current&)
Patrick Grube's avatar
Patrick Grube committed
{
Patrick Grube's avatar
Patrick Grube committed
    return motion->getNumFrames();
int MMMPlayer::getCurrentFrame(const Ice::Current&)
bool MMMPlayer::setLoopPlayback(bool loop, const Ice::Current&)
    loopPlayback = loop;
    return loopPlayback;
}
std::string MMMPlayer::getMotionPath(const Ice::Current&)
StringList MMMPlayer::getJointNames(const Ice::Current&)
int MMMPlayer::setUsePID(bool usePID, const Ice::Current&)

    ScopedRecursiveLock lock(mmmMutex);

    usePIDController = usePID;
Patrick Grube's avatar
Patrick Grube committed
    NameControlModeMap modes;
Patrick Grube's avatar
Patrick Grube committed
    for (size_t i = 0; i < jointNames.size(); ++i)
    {
        const auto& jointName = jointNames.at(i);

        if (usePIDController)
Patrick Grube's avatar
Patrick Grube committed
        {
            modes[jointName] = eVelocityControl;
        }
        else
        {
            modes[jointName] = ePositionControl;
        }
    }
Patrick Grube's avatar
Patrick Grube committed
    try
    {
        kinematicUnit->switchControlMode(modes);
Patrick Grube's avatar
Patrick Grube committed
    }
    catch (...) { }

void MMMPlayer::setFPS(float FPS, const Ice::Current&)
Patrick Grube's avatar
Patrick Grube committed
    ARMARX_VERBOSE << "set FPS to " << FPS;
bool MMMPlayer::setJointsInUse(const std::string& jointName, bool inUse, const Ice::Current&)
Patrick Grube's avatar
Patrick Grube committed
    jointNamesUsed[jointName] = inUse;
Patrick Grube's avatar
Patrick Grube committed
    return jointNamesUsed[jointName];
void MMMPlayer::setCurrentFrame(int frameNumber, const Ice::Current&)

    ScopedRecursiveLock lock(mmmMutex);

Patrick Grube's avatar
Patrick Grube committed
    currentFrame = frameNumber;

    if (direction < 0)
    {
        frameOffset = motion->getNumFrames() - frameNumber;
        frameOffset = frameNumber;

    auto frame = motion->getMotionFrame(frameNumber);

    targetPositionValues.clear();
    StringVariantBaseMap debugTargetValues;
    ARMARX_CHECK_EXPRESSION((int)jointNames.size() == frame->joint.rows());
    for (size_t i = 0; i < jointNames.size(); ++i)
Patrick Grube's avatar
Patrick Grube committed
    {
        const auto& jointName = jointNames.at(i);

        if (jointNamesUsed[jointName])
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        {
            auto& targetPosValue = targetPositionValues[jointName] = frame->joint[i];
            auto it = jointOffets.find(jointName);

            if (it != jointOffets.end())
            {
                targetPosValue += it->second;
            assert(targetPosValue == targetPositionValues[jointName]);
            debugTargetValues[jointName] = new Variant(targetPosValue);
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        }
Patrick Grube's avatar
Patrick Grube committed
    }
    debugObserver->setDebugChannel("targetJointAngles", debugTargetValues);
    kinematicUnit->setJointVelocities(nullVelocities);
    kinematicUnit->setJointAngles(targetPositionValues);
    ARMARX_INFO << "MMMPlayer: Current frame set to " << frameNumber;
}
void MMMPlayer::run()
{
    NameValueMap targetVelocities;
Patrick Grube's avatar
Patrick Grube committed
    if (paused)
Patrick Grube's avatar
Patrick Grube committed
        sleep(1);
        //        ARMARX_INFO << deatargetVel  = std::min<double>(maxVel / 180.0 * M_PI, targetVel);
        //deactivateSpam(3) << " pausing MMMPlayer at frame " << currentFrame;
Patrick Grube's avatar
Patrick Grube committed
        motionStartTime = IceUtil::Time::now();
Patrick Grube's avatar
Patrick Grube committed
        currentMotionTime = ((IceUtil::Time::now() - motionStartTime).toSecondsDouble() * 100 * desiredFPS / motionFPS) + frameOffset;
Patrick Grube's avatar
Patrick Grube committed
        if (direction > 0)
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        {
Patrick Grube's avatar
Patrick Grube committed
            currentFrame = currentMotionTime;
Patrick Grube's avatar
Patrick Grube committed
        else
Patrick Grube's avatar
Patrick Grube committed
            currentFrame = motion->getNumFrames() - (int)currentMotionTime;
Patrick Grube's avatar
Patrick Grube committed
        if (currentFrame >= (int)motion->getNumFrames() || currentFrame < 0)
Patrick Grube's avatar
Patrick Grube committed
            ARMARX_VERBOSE << "current frame " << currentFrame;
            kinematicUnit->setJointVelocities(nullVelocities);
Patrick Grube's avatar
Patrick Grube committed

Patrick Grube's avatar
Patrick Grube committed
            if (loopPlayback)
Patrick Grube's avatar
Patrick Grube committed
                direction *= -1;
                ARMARX_VERBOSE << "direction " << direction;
                motionStartTime = IceUtil::Time::now();
                frameOffset = 1;
                return;
Patrick Grube's avatar
Patrick Grube committed
            else if (task)
Patrick Grube's avatar
Patrick Grube committed
                task->stop();
                sleep(1);
                //                ARMARX_VERBOSE << "after stop task running:" << task->isRunning();
                return;
Patrick Grube's avatar
Patrick Grube committed
        }
Patrick Grube's avatar
Patrick Grube committed
        double interpolationValue = currentMotionTime - (int)currentMotionTime;
        auto frame = motion->getMotionFrame(currentFrame);
        MMM::MotionFramePtr nextFrame;
Patrick Grube's avatar
Patrick Grube committed
        if (currentFrame + 1 < (int)motion->getNumFrames())
        {
            nextFrame = motion->getMotionFrame(currentFrame + 1);
        }
        NameValueMap tmpTargetValues;
Patrick Grube's avatar
Patrick Grube committed
        StringVariantBaseMap debugTargetValues;
        {
            ScopedRecursiveLock lock(mmmMutex);
            targetPositionValues.clear();
            ARMARX_INFO << deactivateSpam(1) << "frame: " << currentFrame << " " << VAROUT(interpolationValue);
            ARMARX_CHECK_EXPRESSION((int)jointNames.size() == frame->joint.rows());
Patrick Grube's avatar
Patrick Grube committed

            for (size_t i = 0; i < jointNames.size(); ++i)
                const auto& jointName = jointNames.at(i);
Patrick Grube's avatar
Patrick Grube committed

                if (jointNamesUsed[jointName])
                    auto& targetPosValue = targetPositionValues[jointName] = frame->joint[i];
                    /*if (nextFrame)
                    {
                        targetPosValue += interpolationValue * (nextFrame->joint[i] - frame->joint[i]);
                    }*/
                    auto it = jointOffets.find(jointName);
                    if (it != jointOffets.end())
Patrick Grube's avatar
Patrick Grube committed
                    {
                        targetPosValue += it->second;
Patrick Grube's avatar
Patrick Grube committed
                    }
                    //                    assert(targetPosValue == targetPositionValues[jointName]);
                    debugTargetValues[jointName] = new Variant(targetPosValue);
                    float& targetVel = targetVelocities[jointName] =  0.0;
                    auto vel = frame->joint_vel[i];
Patrick Grube's avatar
Patrick Grube committed

                    if (vel != 0.0f)
                    {
                        targetVel = vel;
                    }
                    else if (nextFrame)
                    {
                        if (nextFrame->timestep - frame->timestep == 0)
                        {
                            ARMARX_INFO << deactivateSpam() << "timestep delta is zero at frame " << currentFrame << " - cannot calculate velocity";
                        }
Patrick Grube's avatar
Patrick Grube committed

                        targetVel = targetVelocities[jointName] = (nextFrame->joint[i] - frame->joint[i]) / (nextFrame->timestep - frame->timestep);
                    }
                    targetVel *= 0.4;
                    MMM::MotionFramePtr nextNextFrame;
Patrick Grube's avatar
Patrick Grube committed

                    if (currentFrame + 2 < (int)motion->getNumFrames())
Patrick Grube's avatar
Patrick Grube committed
                    {
                        nextNextFrame = motion->getMotionFrame(currentFrame + 2);
Patrick Grube's avatar
Patrick Grube committed
                    }

                    targetVel *= desiredFPS / motionFPS * direction;

Patrick Grube's avatar
Patrick Grube committed
                    //            debugObserver->setDebugDatafield(jointName, "p", new Variant(pid->second->previousError * pid->second->Kp));
                    //            debugObserver->setDebugDatafield(jointName, "i", new Variant(pid->second->integral* pid->second->Ki));
                    //            debugObserver->setDebugDatafield(jointName, "d", new Variant(pid->second->derivative * pid->second->Kd));
                    //            debugObserver->setDebugDatafield(jointName, "velocity", new Variant(targetVelocities[jointName]));
                    //        targetVelocities[jointName] =  0.0;
                    auto pid = PIDs.find(jointName);

                    if (usePIDController)
                        if (pid != PIDs.end())
                        {
                            auto cv = pid->second->getControlValue();
                            targetVel += cv;
                            ARMARX_INFO << deactivateSpam(0.5, jointName) << jointName << " ControlValue   " << cv;
                            if (cv > 20)
                            {
                                ARMARX_INFO << "" << (jointName) << ": v:" << (targetVel) << " pos: " << targetPosValue;
                            }
                            //                    debugObserver->setDebugDatafield(jointName, "p", new Variant(pid->second->previousError * pid->second->Kp));
                            //                    debugObserver->setDebugDatafield(jointName, "i", new Variant(pid->second->integral * pid->second->Ki));
                            //                    debugObserver->setDebugDatafield(jointName, "d", new Variant(pid->second->derivative * pid->second->Kd));
                            //                    debugObserver->setDebugDatafield(jointName, "velocity", new Variant(targetVelocities[jointName]));
                        }
                    }

                    targetVel  = std::min<double>(maxVel / 180.0 * M_PI, targetVel);
                    targetVel  = std::max<double>(-1 * maxVel / 180.0 * M_PI, targetVel);
                }
                else
                {
                    targetVelocities[jointName] = 0.0;
                }
Patrick Grube's avatar
Patrick Grube committed
            }
            tmpTargetValues = targetPositionValues;
Patrick Grube's avatar
Patrick Grube committed

Patrick Grube's avatar
Patrick Grube committed
        {
            ScopedLock lock(jointAnglesMutex);
            updatePIDController(latestJointAngles);
Patrick Grube's avatar
Patrick Grube committed
        }
Patrick Grube's avatar
Patrick Grube committed
        debugObserver->setDebugChannel("targetJointAngles", debugTargetValues);
        kinematicUnit->setJointVelocities(targetVelocities);
Patrick Grube's avatar
Patrick Grube committed
        if (!usePIDController)
        {
            kinematicUnit->setJointAngles(tmpTargetValues);
Patrick Grube's avatar
Patrick Grube committed
        }
Patrick Grube's avatar
Patrick Grube committed
        ARMARX_VERBOSE << deactivateSpam(1) << "Playing frame " << currentFrame;
void MMMPlayer::reportJointAngles(const NameValueMap& angles, bool, const Ice::Current&)
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
{
    ScopedLock lock(jointAnglesMutex);
    latestJointAngles = angles;
}
void MMMPlayer::updatePIDController(const NameValueMap& angles)
{
    if (!usePIDController)
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
    {
        ARMARX_INFO << deactivateSpam() << "jointangles reporting DISABLED";
        return;
    }

Patrick Grube's avatar
Patrick Grube committed
    for (const auto & joint : angles)
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
    {
        const std::string& name = joint.first;

        if (targetPositionValues.find(name) == targetPositionValues.end())
        {
            continue;
        }

Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        auto it = PIDs.find(name);

        if (it == PIDs.end())
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        {
            PIDs[name] = PIDControllerPtr(new PIDController(getProperty<float>("Kp").getValue(),
                                          getProperty<float>("Ki").getValue(),
                                          getProperty<float>("Kd").getValue()));
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
            it = PIDs.find(name);
        }
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        PIDControllerPtr pid = it->second;
        pid->update(joint.second, targetPositionValues[name]);
    }
}

Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
void MMMPlayer::onDisconnectComponent()
{
    ScopedRecursiveLock lock(mmmMutex);

Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
        task->stop();
    kinematicUnit->stop();
Nikolaus Vahrenkamp's avatar
Nikolaus Vahrenkamp committed
}


void MMMPlayer::onExitComponent()
{

}

PropertyDefinitionsPtr MMMPlayer::createPropertyDefinitions()
{
    return PropertyDefinitionsPtr(new MMMPlayerPropertyDefinitions(
                                      getConfigIdentifier()));
}