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 
38 using namespace armarx;
39 using namespace MotionControlGroup;
40 
41 // DO NOT EDIT NEXT LINE
42 MoveJointTrajectory::SubClassRegistry
43  MoveJointTrajectory::Registry(MoveJointTrajectory::GetName(),
45 
46 void
48 {
49 }
50 
51 void
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 
157 void
159 {
160 }
161 
162 void
164 {
165 }
166 
167 // DO NOT EDIT NEXT FUNCTION
170 {
171  return XMLStateFactoryBasePtr(new MoveJointTrajectory(stateData));
172 }
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getLocalStructureRobot
const VirtualRobot::RobotPtr getLocalStructureRobot()
Definition: MotionControlGroupStatechartContext.h:108
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:75
armarx::RemoteRobot::synchronizeLocalClone
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
Definition: RemoteRobot.cpp:522
armarx::MotionControlGroup::MoveJointTrajectory::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: MoveJointTrajectory.cpp:169
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:100
armarx::MotionControlGroup::MoveJointTrajectory::onBreak
void onBreak() override
Definition: MoveJointTrajectory.cpp:163
MoveJointTrajectory.h
armarx::XMLStateConstructorParams
Definition: XMLState.h:49
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:158
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
armarx::TrajectoryController::getCurrentTimestamp
double getCurrentTimestamp() const
Definition: TrajectoryController.cpp:129
IceInternal::Handle
Definition: forward_declarations.h:8
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
armarx::MotionControlGroup::MoveJointTrajectory::MoveJointTrajectory
MoveJointTrajectory(const XMLStateConstructorParams &stateData)
Definition: MoveJointTrajectory.h:40
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getRobotStateComponent
RobotStateComponentInterfacePrx getRobotStateComponent()
Definition: MotionControlGroupStatechartContext.h:114
armarx::MotionControlGroup::MoveJointTrajectory::onEnter
void onEnter() override
Definition: MoveJointTrajectory.cpp:47
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:67
CycleUtil.h
armarx::control::njoint_controller::platform::platform_follower_controller::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformFollowerController.h:88
armarx::MotionControlGroup::MoveJointTrajectory::run
void run() override
Definition: MoveJointTrajectory.cpp:52
armarx::MotionControlGroup::MotionControlGroupStatechartContext::getKinematicUnit
KinematicUnitInterfacePrx getKinematicUnit()
Definition: MotionControlGroupStatechartContext.h:120
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:198
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:407
armarx::MotionControlGroup::MoveJointTrajectory::Registry
static SubClassRegistry Registry
Definition: MoveJointTrajectory.h:54
TrajectoryController.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
RobotStatechartContext.h