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 
25 #include "MoveTCPOnLine.h"
26 #include <VirtualRobot/MathTools.h>
27 #include <VirtualRobot/RobotNodeSet.h>
28 
30 
32 
33 using namespace armarx;
34 using namespace MotionControlGroup;
35 
36 // DO NOT EDIT NEXT LINE
37 MoveTCPOnLine::SubClassRegistry MoveTCPOnLine::Registry(MoveTCPOnLine::GetName(),
39 
41  XMLStateTemplate<MoveTCPOnLine>(stateData), MoveTCPOnLineGeneratedBase<MoveTCPOnLine>(stateData)
42 {
43 }
44 
45 void
47 {
48  // put your user code for the enter-point here
49  // execution time should be short (<100ms)
50 }
51 
52 void
54 {
55  // put your user code for the execution-phase here
56  // runs in seperate thread, thus can do complex operations
57  // should check constantly whether isRunningTaskStopped() returns true
58  MotionControlGroupStatechartContext* c = getContext<MotionControlGroupStatechartContext>();
59  auto robot = c->getLocalRobot();
60  auto sharedRobot = c->getRobotStateComponent()->getSynchronizedRobot();
61  RemoteRobot::synchronizeLocalClone(robot, sharedRobot);
62  c->getTCPControlUnit()->request();
63  std::string tcpName;
64  if (in.isTCPNameSet())
65  {
66  tcpName = in.getTCPName();
67  }
68  else
69  {
70  tcpName = robot->getRobotNodeSet(in.getKinematicChainName())->getTCP()->getName();
71  }
72  auto tcp = robot->getRobotNode(tcpName);
73  std::string kinematicChainName = in.getKinematicChainName();
74  auto startTime = TimeUtil::GetTime();
75  FramedPosePtr startPose = new FramedPose(tcp->getPoseInRootFrame(),
76  c->getRobot()->getRootNode()->getName(),
77  c->getRobot()->getName());
78  Eigen::Vector3f startPositionInRootFrame = startPose->toEigen().block<3, 1>(0, 3);
79  out.setStartPose(FramedPosePtr::dynamicCast(startPose->clone()));
80 
81  float tcpVelocity = in.getTCPVelocity();
82  Eigen::Vector3f tcpDirectionInRootFrame =
83  in.getMoveDirection()->toRootEigen(robot).normalized();
84  int sleepTimeMilliSec = int(1.0f / in.getControlFrequency() * 1000.0f);
85  bool keepOrientation = in.getKeepStartOrientation();
86  ARMARX_INFO << "Keeping orientation: " << keepOrientation;
87  // uncomment this if you need a continous run function. Make sure to use sleep or use blocking wait to reduce cpu load.
88  while (!isRunningTaskStopped()) // stop run function if returning true
89  {
90  RemoteRobot::synchronizeLocalClone(robot, sharedRobot);
91 
92  FramedDirectionPtr orientationVel, positionVel;
93  if (keepOrientation)
94  {
95  Eigen::Vector3f orientationDelta;
96  FramedPosePtr TCPDeltaToGoal = FramedPosePtr::dynamicCast(startPose->clone());
97  TCPDeltaToGoal->changeFrame(robot, tcpName);
98  VirtualRobot::MathTools::eigen4f2rpy(TCPDeltaToGoal->toEigen(), orientationDelta);
99  orientationVel = new FramedDirection(orientationDelta * 10, tcpName, robot->getName());
100  Eigen::Vector3f axis;
101  float angleError = 0;
102  VirtualRobot::MathTools::eigen4f2axisangle(TCPDeltaToGoal->toEigen(), axis, angleError);
104  << deactivateSpam(1)
105  << VAROUT(
106  angleError); // << VAROUT(startPositionInRootFrame) << VAROUT(tcp->getPositionInRootFrame());
107  }
108  auto timePassed = (TimeUtil::GetTime() - startTime).toSecondsDouble();
109 
110  Eigen::Vector3f currentTargetPosition =
111  startPositionInRootFrame + timePassed * tcpVelocity * tcpDirectionInRootFrame;
112  positionVel = new FramedDirection(currentTargetPosition - tcp->getPositionInRootFrame(),
113  robot->getRootNode()->getName(),
114  robot->getName());
115 
116  float distanceTravelled = (tcp->getPositionInRootFrame() - startPositionInRootFrame).norm();
118  << deactivateSpam(1)
119  << VAROUT(
120  distanceTravelled); // << VAROUT(startPositionInRootFrame) << VAROUT(tcp->getPositionInRootFrame());
121  if (distanceTravelled >= in.getMaxDistance())
122  {
123  emitSuccess();
124  usleep(100000);
125  c->getTCPControlUnit()->release();
126  break;
127  }
128  c->getTCPControlUnit()->setTCPVelocity(
129  kinematicChainName, tcpName, positionVel, orientationVel);
130  TimeUtil::MSSleep(sleepTimeMilliSec);
131  }
132 
133  FramedDirectionPtr zeroVelocity(new FramedDirection(0, 0, 0, tcpName, robot->getName()));
134  c->getTCPControlUnit()->setTCPVelocity(kinematicChainName, tcpName, zeroVelocity, zeroVelocity);
135  TimeUtil::MSSleep(100);
136  c->getTCPControlUnit()->release();
137 
138 
139  {
140  RemoteRobot::synchronizeLocalClone(robot, sharedRobot);
141  float distanceTravelled = (tcp->getPositionInRootFrame() - startPositionInRootFrame).norm();
142  ARMARX_INFO << "Final " << VAROUT(distanceTravelled);
143  out.setDistanceTravelled(distanceTravelled);
144 
145  FramedPosePtr TCPDeltaToGoal = FramedPosePtr::dynamicCast(startPose->clone());
146  TCPDeltaToGoal->changeFrame(robot, tcpName);
147  Eigen::Vector3f axis;
148  float angleError = 0;
149  VirtualRobot::MathTools::eigen4f2axisangle(TCPDeltaToGoal->toEigen(), axis, angleError);
150  ARMARX_INFO << deactivateSpam(1) << "Final angle error: " << angleError;
151  }
152 }
153 
154 void
156 {
157  // put your user code for the exit point here
158  // execution time should be short (<100ms)
159  waitForRunningTaskToFinish();
160 }
161 
162 // DO NOT EDIT NEXT FUNCTION
165 {
166  return XMLStateFactoryBasePtr(new MoveTCPOnLine(stateData));
167 }
armarx::MotionControlGroup::MotionControlGroupStatechartContext
Definition: MotionControlGroupStatechartContext.h:75
armarx::RemoteRobot::synchronizeLocalClone
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
Definition: RemoteRobot.cpp:522
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:36
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::XMLStateConstructorParams
Definition: XMLState.h:49
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::MotionControlGroup::MoveTCPOnLine::MoveTCPOnLine
MoveTCPOnLine(const XMLStateConstructorParams &stateData)
Definition: MoveTCPOnLine.cpp:40
armarx::MotionControlGroup::MoveTCPOnLine::run
void run() override
Definition: MoveTCPOnLine.cpp:53
MoveTCPOnLine.h
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:64
IceInternal::Handle< FramedPose >
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
armarx::XMLStateTemplate
Class for legacy to stay compatible with old statecharts.
Definition: XMLState.h:146
armarx::MotionControlGroup::MoveTCPOnLine::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: MoveTCPOnLine.cpp:164
armarx::MotionControlGroup::MoveTCPOnLine::Registry
static SubClassRegistry Registry
Definition: MoveTCPOnLine.h:43
MotionControlGroupStatechartContext.h
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::VariantType::FramedDirection
const VariantTypeId FramedDirection
Definition: FramedPose.h:37
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:198
armarx::MotionControlGroup::MoveTCPOnLine::onEnter
void onEnter() override
Definition: MoveTCPOnLine.cpp:46
armarx::MotionControlGroup::MoveTCPOnLine
Definition: MoveTCPOnLine.h:31
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::MotionControlGroup::MoveTCPOnLine::onExit
void onExit() override
Definition: MoveTCPOnLine.cpp:155
norm
double norm(const Point &a)
Definition: point.hpp:102