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 <chrono>
23 #include <thread>
24 
25 #include "MoveJointTrajectory.h"
26 
29 
30 #include <RobotAPI/interface/units/KinematicUnitInterface.h>
31 
32 #include <RobotAPI/interface/components/TrajectoryPlayerInterface.h>
35 #include <VirtualRobot/RobotConfig.h>
36 
37 using namespace armarx;
38 using namespace MotionControlGroup;
39 
40 // DO NOT EDIT NEXT LINE
41 MoveJointTrajectory::SubClassRegistry MoveJointTrajectory::Registry(MoveJointTrajectory::GetName(), &MoveJointTrajectory::CreateInstance);
42 
44 {
45 }
46 
48 {
49  MotionControlGroupStatechartContext* context = getContext<MotionControlGroupStatechartContext>();
50  auto robot = context->getLocalStructureRobot();
51 
52  auto combinedTraj = in.getTrajectory();
53  ARMARX_INFO << VAROUT(combinedTraj->output());
54  if (in.getUseTrajectoryPlayerComponent())
55  {
56  TrajectoryPlayerInterfacePrx trajPlayer = context->getProxy<TrajectoryPlayerInterfacePrx>(in.getTrajectoryPlayerName());
57  trajPlayer->resetTrajectoryPlayer(false);
58  trajPlayer->considerConstraints(false);
59  trajPlayer->setLoopPlayback(false);
60  trajPlayer->loadJointTraj(combinedTraj);
61  trajPlayer->startTrajectoryPlayer();
62  auto endTime = trajPlayer->getEndTime();
63  double currentTime = 0;
64  do
65  {
66  ARMARX_INFO << deactivateSpam(1) << "CurrentTime: " << currentTime << " endTime: " << endTime;
67  usleep(10000);
68  currentTime = trajPlayer->getCurrentTime();
69  }
70  while (currentTime < endTime && !isRunningTaskStopped());
71  if (!isRunningTaskStopped() && currentTime >= endTime)
72  {
73  if (in.getWaitTimeAfterExecution() > 0)
74  {
75  ARMARX_INFO << "Waiting for " << in.getWaitTimeAfterExecution();
76  TimeUtil::MSSleep(in.getWaitTimeAfterExecution());
77  }
78  trajPlayer->pauseTrajectoryPlayer();
79  emitSuccess();
80  }
81  else if (isRunningTaskStopped())
82  {
83  trajPlayer->pauseTrajectoryPlayer();
84  }
85  }
86  else
87  {
88  TrajectoryController ctrl(combinedTraj, in.getKp());
89  auto duration = combinedTraj->getTimeLength();
90  auto jointNames = combinedTraj->getDimensionNames();
91  auto rns = VirtualRobot::RobotNodeSet::createRobotNodeSet(robot, "tempset", jointNames, robot->getRootNode()->getName());
92 
93 
94 
95 
96  auto timestep = in.getTimeStep();
97  CycleUtil cycle(timestep * 1000);
98  NameControlModeMap modes;
99  NameValueMap targetVelocities;
100 
101  for (auto& jointName : jointNames)
102  {
103  modes[jointName] = eVelocityControl;
104  }
105  context->getKinematicUnit()->switchControlMode(modes);
106  // uncomment this if you need a continous run function. Make sure to use sleep or use blocking wait to reduce cpu load.
107  while (!isRunningTaskStopped()) // stop run function if returning true
108  {
109  // do your calculations
111 
112  Eigen::VectorXf velocityTargets = ctrl.update(timestep, rns->getJointValuesEigen());
113  int i = 0;
114  for (auto& jointName : jointNames)
115  {
116  targetVelocities[jointName] = velocityTargets(i);
117  i++;
118  }
119  context->getKinematicUnit()->setJointVelocities(targetVelocities);
120  // context->getDebugDrawerTopicProxy()->setSphereVisu("trajectory", "timestep" + std::to_string(), new )
121  ARMARX_INFO << deactivateSpam(2) << "timestep: " << ctrl.getCurrentTimestamp() << " Duration: " << duration;
122  if (ctrl.getCurrentTimestamp() >= duration)
123  {
124  ARMARX_INFO << "Reached Goal!";
125  emitSuccess();
126  break;
127  }
128  cycle.waitForCycleDuration();
129  }
130  for (auto& jointName : jointNames)
131  {
132  targetVelocities[jointName] = 0;
133  }
134  context->getKinematicUnit()->setJointVelocities(targetVelocities);
135  }
136  // RemoteRobot::synchronizeLocalClone(robot, context->getRobotStateComponent());
137 
138  // for (auto goalConfigMapPtr : in.getGoalConfig())
139  // {
140  // auto goalConfigMap = goalConfigMapPtr->toStdMap<float>();
141  // NameValueMap errors;
142  // for (auto& pair : goalConfigMap)
143  // {
144  // errors[pair.first] = robot->getRobotNode(pair.first)->getJointValue();
145  // }
146  // ARMARX_INFO << VAROUT(errors) << VAROUT(goalConfigMap);
147  // }
148 
149 }
150 
152 {
153 
154 }
156 {
157 
158 }
159 
160 
161 
162 // DO NOT EDIT NEXT FUNCTION
164 {
165  return XMLStateFactoryBasePtr(new MoveJointTrajectory(stateData));
166 }
167 
168 
169 
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getLocalStructureRobot
const VirtualRobot::RobotPtr getLocalStructureRobot()
Definition: MotionControlGroupStatechartContext.h:91
armarx::TrajectoryController
Subcontroller which handles all program interaction with the modle, communicates with other controlle...
Definition: TrajectoryController.h:32
armarx::MotionControlGroup::MotionControlGroupStatechartContext
Definition: MotionControlGroupStatechartContext.h:66
armarx::RemoteRobot::synchronizeLocalClone
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
Definition: RemoteRobot.cpp:448
armarx::MotionControlGroup::MoveJointTrajectory::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: MoveJointTrajectory.cpp:163
armarx::TimeUtil::MSSleep
static void MSSleep(int durationMS)
lock the calling thread for a given duration (like usleep(...) but using Timeserver time)
Definition: TimeUtil.cpp:94
armarx::MotionControlGroup::MoveJointTrajectory::onBreak
void onBreak() override
Definition: MoveJointTrajectory.cpp:155
MoveJointTrajectory.h
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::CycleUtil
This util class helps with keeping a cycle time during a control cycle.
Definition: CycleUtil.h:40
armarx::MotionControlGroup::MoveJointTrajectory::onExit
void onExit() override
Definition: MoveJointTrajectory.cpp:151
armarx::TrajectoryController::getCurrentTimestamp
double getCurrentTimestamp() const
Definition: TrajectoryController.cpp:115
IceInternal::Handle
Definition: forward_declarations.h:8
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::MotionControlGroup::MoveJointTrajectory::MoveJointTrajectory
MoveJointTrajectory(const XMLStateConstructorParams &stateData)
Definition: MoveJointTrajectory.h:43
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getRobotStateComponent
RobotStateComponentInterfacePrx getRobotStateComponent()
Definition: MotionControlGroupStatechartContext.h:96
armarx::MotionControlGroup::MoveJointTrajectory::onEnter
void onEnter() override
Definition: MoveJointTrajectory.cpp:43
armarx::CycleUtil::waitForCycleDuration
IceUtil::Time waitForCycleDuration()
This function will wait (virtual or system time) until the cycle time is reached.
Definition: CycleUtil.cpp:53
armarx::TrajectoryController::update
const Eigen::VectorXf & update(double deltaT, const Eigen::VectorXf &currentPosition)
Definition: TrajectoryController.cpp:54
CycleUtil.h
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
armarx::control::njoint_controller::platform::platform_follower_controller::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformFollowerController.h:91
armarx::MotionControlGroup::MoveJointTrajectory::run
void run() override
Definition: MoveJointTrajectory.cpp:47
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getKinematicUnit
KinematicUnitInterfacePrx getKinematicUnit()
Definition: MotionControlGroupStatechartContext.h:100
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::ManagedIceObject::getProxy
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
Definition: ManagedIceObject.cpp:393
armarx::MotionControlGroup::MoveJointTrajectory::Registry
static SubClassRegistry Registry
Definition: MoveJointTrajectory.h:56
TrajectoryController.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
RobotStatechartContext.h