MoveTCPOnLine.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2014-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package RobotSkillTemplates::MotionControlGroup
19  * @author Mirko Waechter ( mirko dot waechter at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
27 #include "MoveTCPOnLine.h"
28 
29 using namespace armarx;
30 using namespace MotionControlGroup;
31 
32 // DO NOT EDIT NEXT LINE
33 MoveTCPOnLine::SubClassRegistry MoveTCPOnLine::Registry(MoveTCPOnLine::GetName(), &MoveTCPOnLine::CreateInstance);
34 
35 
36 
38  XMLStateTemplate < MoveTCPOnLine > (stateData), MoveTCPOnLineGeneratedBase < MoveTCPOnLine > (stateData)
39 {
40 }
41 
43 {
44  // put your user code for the enter-point here
45  // execution time should be short (<100ms)
46 
47 }
48 
50 {
51  // put your user code for the execution-phase here
52  // runs in seperate thread, thus can do complex operations
53  // should check constantly whether isRunningTaskStopped() returns true
54  MotionControlGroupStatechartContext* c = getContext<MotionControlGroupStatechartContext>();
55  auto robot = c->getLocalRobot();
56  auto sharedRobot = c->getRobotStateComponent()->getSynchronizedRobot();
57  RemoteRobot::synchronizeLocalClone(robot, sharedRobot);
58  c->getTCPControlUnit()->request();
59  std::string tcpName;
60  if (in.isTCPNameSet())
61  {
62  tcpName = in.getTCPName();
63  }
64  else
65  {
66  tcpName = robot->getRobotNodeSet(in.getKinematicChainName())->getTCP()->getName();
67  }
68  auto tcp = robot->getRobotNode(tcpName);
69  std::string kinematicChainName = in.getKinematicChainName();
70  auto startTime = TimeUtil::GetTime();
71  FramedPosePtr startPose = new FramedPose(tcp->getPoseInRootFrame(), c->getRobot()->getRootNode()->getName(), c->getRobot()->getName());
72  Eigen::Vector3f startPositionInRootFrame = startPose->toEigen().block<3, 1>(0, 3);
73  out.setStartPose(FramedPosePtr::dynamicCast(startPose->clone()));
74 
75  float tcpVelocity = in.getTCPVelocity();
76  Eigen::Vector3f tcpDirectionInRootFrame = in.getMoveDirection()->toRootEigen(robot).normalized();
77  int sleepTimeMilliSec = int(1.0f / in.getControlFrequency() * 1000.0f);
78  bool keepOrientation = in.getKeepStartOrientation();
79  ARMARX_INFO << "Keeping orientation: " << keepOrientation;
80  // uncomment this if you need a continous run function. Make sure to use sleep or use blocking wait to reduce cpu load.
81  while (!isRunningTaskStopped()) // stop run function if returning true
82  {
83  RemoteRobot::synchronizeLocalClone(robot, sharedRobot);
84 
85  FramedDirectionPtr orientationVel, positionVel;
86  if (keepOrientation)
87  {
88  Eigen::Vector3f orientationDelta;
89  FramedPosePtr TCPDeltaToGoal = FramedPosePtr::dynamicCast(startPose->clone());
90  TCPDeltaToGoal->changeFrame(robot, tcpName);
91  VirtualRobot::MathTools::eigen4f2rpy(TCPDeltaToGoal->toEigen(), orientationDelta);
92  orientationVel = new FramedDirection(orientationDelta * 10, tcpName, robot->getName());
93  Eigen::Vector3f axis;
94  float angleError = 0;
95  VirtualRobot::MathTools::eigen4f2axisangle(TCPDeltaToGoal->toEigen(), axis, angleError);
96  ARMARX_INFO << deactivateSpam(1) << VAROUT(angleError);// << VAROUT(startPositionInRootFrame) << VAROUT(tcp->getPositionInRootFrame());
97  }
98  auto timePassed = (TimeUtil::GetTime() - startTime).toSecondsDouble();
99 
100  Eigen::Vector3f currentTargetPosition = startPositionInRootFrame + timePassed * tcpVelocity * tcpDirectionInRootFrame;
101  positionVel = new FramedDirection(currentTargetPosition - tcp->getPositionInRootFrame(), robot->getRootNode()->getName(), robot->getName());
102 
103  float distanceTravelled = (tcp->getPositionInRootFrame() - startPositionInRootFrame).norm();
104  ARMARX_INFO << deactivateSpam(1) << VAROUT(distanceTravelled);// << VAROUT(startPositionInRootFrame) << VAROUT(tcp->getPositionInRootFrame());
105  if (distanceTravelled >= in.getMaxDistance())
106  {
107  emitSuccess();
108  usleep(100000);
109  c->getTCPControlUnit()->release();
110  break;
111  }
112  c->getTCPControlUnit()->setTCPVelocity(kinematicChainName, tcpName, positionVel, orientationVel);
113  TimeUtil::MSSleep(sleepTimeMilliSec);
114  }
115 
116  FramedDirectionPtr zeroVelocity(new FramedDirection(0, 0, 0, tcpName, robot->getName()));
117  c->getTCPControlUnit()->setTCPVelocity(kinematicChainName, tcpName, zeroVelocity, zeroVelocity);
118  TimeUtil::MSSleep(100);
119  c->getTCPControlUnit()->release();
120 
121 
122  {
123  RemoteRobot::synchronizeLocalClone(robot, sharedRobot);
124  float distanceTravelled = (tcp->getPositionInRootFrame() - startPositionInRootFrame).norm();
125  ARMARX_INFO << "Final " << VAROUT(distanceTravelled);
126  out.setDistanceTravelled(distanceTravelled);
127 
128  FramedPosePtr TCPDeltaToGoal = FramedPosePtr::dynamicCast(startPose->clone());
129  TCPDeltaToGoal->changeFrame(robot, tcpName);
130  Eigen::Vector3f axis;
131  float angleError = 0;
132  VirtualRobot::MathTools::eigen4f2axisangle(TCPDeltaToGoal->toEigen(), axis, angleError);
133  ARMARX_INFO << deactivateSpam(1) << "Final angle error: " << angleError;
134  }
135 }
136 
138 {
139  // put your user code for the exit point here
140  // execution time should be short (<100ms)
141  waitForRunningTaskToFinish();
142 }
143 
144 
145 // DO NOT EDIT NEXT FUNCTION
147 {
148  return XMLStateFactoryBasePtr(new MoveTCPOnLine(stateData));
149 }
armarx::MotionControlGroup::MotionControlGroupStatechartContext
Definition: MotionControlGroupStatechartContext.h:66
armarx::RemoteRobot::synchronizeLocalClone
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
Definition: RemoteRobot.cpp:448
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:37
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::XMLStateConstructorParams
Definition: XMLState.h:50
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::MotionControlGroup::MoveTCPOnLine::MoveTCPOnLine
MoveTCPOnLine(const XMLStateConstructorParams &stateData)
Definition: MoveTCPOnLine.cpp:37
armarx::MotionControlGroup::MoveTCPOnLine::run
void run() override
Definition: MoveTCPOnLine.cpp:49
MoveTCPOnLine.h
IceInternal::Handle< FramedPose >
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:137
armarx::MotionControlGroup::MoveTCPOnLine::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: MoveTCPOnLine.cpp:146
armarx::MotionControlGroup::MoveTCPOnLine::Registry
static SubClassRegistry Registry
Definition: MoveTCPOnLine.h:44
MotionControlGroupStatechartContext.h
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::VariantType::FramedDirection
const VariantTypeId FramedDirection
Definition: FramedPose.h:38
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::MotionControlGroup::MoveTCPOnLine::onEnter
void onEnter() override
Definition: MoveTCPOnLine.cpp:42
armarx::MotionControlGroup::MoveTCPOnLine
Definition: MoveTCPOnLine.h:31
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::MotionControlGroup::MoveTCPOnLine::onExit
void onExit() override
Definition: MoveTCPOnLine.cpp:137
norm
double norm(const Point &a)
Definition: point.hpp:94