MoveJointTrajectory.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotSkillTemplates::MotionControlGroup
17 * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18 * @date 2016
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22#include "MoveJointTrajectory.h"
23
24#include <chrono>
25#include <thread>
26
27#include <VirtualRobot/RobotConfig.h>
28#include <VirtualRobot/RobotNodeSet.h>
29
32
33#include <RobotAPI/interface/components/TrajectoryPlayerInterface.h>
34#include <RobotAPI/interface/units/KinematicUnitInterface.h>
37
38using namespace armarx;
39using namespace MotionControlGroup;
40
41// DO NOT EDIT NEXT LINE
42MoveJointTrajectory::SubClassRegistry
43 MoveJointTrajectory::Registry(MoveJointTrajectory::GetName(),
45
46void
50
51void
53{
55 getContext<MotionControlGroupStatechartContext>();
56 auto robot = context->getLocalStructureRobot();
57
58 auto combinedTraj = in.getTrajectory();
59 ARMARX_INFO << VAROUT(combinedTraj->output());
60 if (in.getUseTrajectoryPlayerComponent())
61 {
62 TrajectoryPlayerInterfacePrx trajPlayer =
63 context->getProxy<TrajectoryPlayerInterfacePrx>(in.getTrajectoryPlayerName());
64 trajPlayer->resetTrajectoryPlayer(false);
65 trajPlayer->considerConstraints(false);
66 trajPlayer->setLoopPlayback(false);
67 trajPlayer->loadJointTraj(combinedTraj);
68 trajPlayer->startTrajectoryPlayer();
69 auto endTime = trajPlayer->getEndTime();
70 double currentTime = 0;
71 do
72 {
73 ARMARX_INFO << deactivateSpam(1) << "CurrentTime: " << currentTime
74 << " endTime: " << endTime;
75 usleep(10000);
76 currentTime = trajPlayer->getCurrentTime();
77 } while (currentTime < endTime && !isRunningTaskStopped());
78 if (!isRunningTaskStopped() && currentTime >= endTime)
79 {
80 if (in.getWaitTimeAfterExecution() > 0)
81 {
82 ARMARX_INFO << "Waiting for " << in.getWaitTimeAfterExecution();
83 TimeUtil::MSSleep(in.getWaitTimeAfterExecution());
84 }
85 trajPlayer->pauseTrajectoryPlayer();
86 emitSuccess();
87 }
88 else if (isRunningTaskStopped())
89 {
90 trajPlayer->pauseTrajectoryPlayer();
91 }
92 }
93 else
94 {
95 TrajectoryController ctrl(combinedTraj, in.getKp());
96 auto duration = combinedTraj->getTimeLength();
97 auto jointNames = combinedTraj->getDimensionNames();
98 auto rns = VirtualRobot::RobotNodeSet::createRobotNodeSet(
99 robot, "tempset", jointNames, robot->getRootNode()->getName());
100
101
102 auto timestep = in.getTimeStep();
103 CycleUtil cycle(timestep * 1000);
104 NameControlModeMap modes;
105 NameValueMap targetVelocities;
106
107 for (auto& jointName : jointNames)
108 {
109 modes[jointName] = eVelocityControl;
110 }
111 context->getKinematicUnit()->switchControlMode(modes);
112 // uncomment this if you need a continous run function. Make sure to use sleep or use blocking wait to reduce cpu load.
113 while (!isRunningTaskStopped()) // stop run function if returning true
114 {
115 // do your calculations
117
118 Eigen::VectorXf velocityTargets = ctrl.update(timestep, rns->getJointValuesEigen());
119 int i = 0;
120 for (auto& jointName : jointNames)
121 {
122 targetVelocities[jointName] = velocityTargets(i);
123 i++;
124 }
125 context->getKinematicUnit()->setJointVelocities(targetVelocities);
126 // context->getDebugDrawerTopicProxy()->setSphereVisu("trajectory", "timestep" + std::to_string(), new )
127 ARMARX_INFO << deactivateSpam(2) << "timestep: " << ctrl.getCurrentTimestamp()
128 << " Duration: " << duration;
129 if (ctrl.getCurrentTimestamp() >= duration)
130 {
131 ARMARX_INFO << "Reached Goal!";
132 emitSuccess();
133 break;
134 }
135 cycle.waitForCycleDuration();
136 }
137 for (auto& jointName : jointNames)
138 {
139 targetVelocities[jointName] = 0;
140 }
141 context->getKinematicUnit()->setJointVelocities(targetVelocities);
142 }
143 // RemoteRobot::synchronizeLocalClone(robot, context->getRobotStateComponent());
144
145 // for (auto goalConfigMapPtr : in.getGoalConfig())
146 // {
147 // auto goalConfigMap = goalConfigMapPtr->toStdMap<float>();
148 // NameValueMap errors;
149 // for (auto& pair : goalConfigMap)
150 // {
151 // errors[pair.first] = robot->getRobotNode(pair.first)->getJointValue();
152 // }
153 // ARMARX_INFO << VAROUT(errors) << VAROUT(goalConfigMap);
154 // }
155}
156
157void
161
162void
166
167// DO NOT EDIT NEXT FUNCTION
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
#define VAROUT(x)
This util class helps with keeping a cycle time during a control cycle.
Definition CycleUtil.h:41
IceUtil::Time waitForCycleDuration()
This function will wait (virtual or system time) until the cycle time is reached.
Definition CycleUtil.cpp:53
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
custom implementation of the StatechartContext for a statechart
MoveJointTrajectory(const XMLStateConstructorParams &stateData)
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
static void MSSleep(int durationMS)
lock the calling thread for a given duration (like usleep(...) but using Timeserver time)
Definition TimeUtil.cpp:100
Subcontroller which handles all program interaction with the modle, communicates with other controlle...
const Eigen::VectorXf & update(double deltaT, const Eigen::VectorXf &currentPosition)
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64