MoveTCPToTargetPose.cpp
Go to the documentation of this file.
1 #include "MoveTCPToTargetPose.h"
2 
3 #include "util/TCPControlUtil.h"
4 
5 #include <VirtualRobot/MathTools.h>
6 
7 namespace armarx::skills
8 {
9  namespace
10  {
11  tcp_control::arondto::MoveTCPToTargetPoseAcceptedType GetDefaultParameterization()
12  {
13  tcp_control::arondto::MoveTCPToTargetPoseAcceptedType ret;
14  ret.orientationalAccuracy = 0.01;
15  ret.positionalAccuracy = 25;
16  return ret;
17  }
18  }
19 
20  SkillDescription MoveTCPToTargetPose::Description = skills::SkillDescription{
21  "MoveTCPToTargetPose", "Move the TCP to a target pose",
23  tcp_control::arondto::MoveTCPToTargetPoseAcceptedType::ToAronType(),
24  GetDefaultParameterization().toAron()
25  };
26 
28  TCPControlSkill(mns, arviz, Description.skillName, context),
29  mixin::RobotReadingSkillMixin(mns),
30  PeriodicSimpleSpecializedSkill<tcp_control::arondto::MoveTCPToTargetPoseAcceptedType>(Description, armarx::core::time::Frequency::Hertz(100))
31  {
32  }
33 
34  Skill::InitResult MoveTCPToTargetPose::init(const SpecializedInitInput &in)
35  {
37 
38  tcpControlUnitRequestedOnStart = true;
39  if (!context.tcpControlUnitPrx->isRequested())
40  {
41  tcpControlUnitRequestedOnStart = false;
42  context.tcpControlUnitPrx->request();
43  }
44  context.tcpControlUnitPrx->setCycleTime(100);
45 
46  return {.status = TerminatedSkillStatus::Succeeded};
47  }
48 
49  PeriodicSkill::StepResult MoveTCPToTargetPose::step(const SpecializedMainInput& in)
50  {
51  auto robot = robotReader.getSynchronizedRobot(in.parameters.robotName, armem::Time::Now(), VirtualRobot::RobotIO::RobotDescription::eStructure);
52  if (!robot)
53  {
54  ARMARX_WARNING << "Unable to get robot from robotstatememory. Abort.";
55  return {ActiveOrTerminatedSkillStatus::Failed, nullptr};
56  }
57 
58  std::string handRootNodeName = "Hand L Base";
59  std::string robotAPIHandFileName = "RobotAPI/robots/Armar3/ArmarIII-LeftHand.xml";
60  if (simox::alg::starts_with(in.parameters.kinematicChainName, "Right"))
61  {
62  handRootNodeName = "Hand R Base";
63  robotAPIHandFileName = "RobotAPI/robots/Armar3/ArmarIII-RightHand.xml";
64  }
65 
66  std::string tcpName = robot->getRobotNodeSet(in.parameters.kinematicChainName)->getTCP()->getName();
67 
68  const Eigen::Matrix4f tcpPoseGlobal = robot->getRobotNode(tcpName)->getGlobalPose();
69  const Eigen::Matrix4f handRootPoseGlobal = robot->getRobotNode(handRootNodeName)->getGlobalPose();
70  const Eigen::Matrix4f tcpInHandRoot = handRootPoseGlobal.inverse() * tcpPoseGlobal;
71 
72  // get current pose of tcp
73  auto currentTCPPoseGlobal = robot->getRobotNode(tcpName)->getGlobalPose();
74 
75  const auto tcpTargetPoseGlobal = in.parameters.targetPoseGlobal;
76  const Eigen::Matrix4f handRootTargetPoseGlobal = tcpTargetPoseGlobal * tcpInHandRoot.inverse();
77 
78  const float baseSpeedFactor = 1.5;
79  const float maxTranslationSpeed = baseSpeedFactor * 30;
80  const float minTranslationSpeed = maxTranslationSpeed * 0.1; // 10% of max
81  const float maxRotationSpeed = baseSpeedFactor * 0.7;
82  const float minRotationSpeed = maxRotationSpeed * 0.1; // 10% of max
83 
84  // get translational difference
85  Eigen::Vector3f diffPos = tcpTargetPoseGlobal.block<3, 1>(0, 3) - currentTCPPoseGlobal.block<3, 1>(0, 3);
86  float diffPosError = diffPos.norm();
87 
88  float sig = 1 / (1 + pow(M_E, -diffPos.norm() / 5.f + 1.0)); // sigmoid
89  diffPos = baseSpeedFactor * diffPos * sig;
90 
91  // Dont let it go to slow
92  if (diffPosError < minTranslationSpeed && diffPosError != 0)
93  {
94  diffPos *= (minTranslationSpeed / diffPosError);
95  }
96 
97  else if (diffPosError > maxTranslationSpeed && diffPosError != 0)
98  {
99  diffPos *= (maxTranslationSpeed / diffPosError);
100  }
101 
102  if (diffPosError < in.parameters.positionalAccuracy)
103  {
104  diffPos = Eigen::Vector3f::Zero();
105  }
106 
107  FramedDirection cartesianVelocity(diffPos, GlobalFrame, "");
108 
109  {
110  auto l = arviz.layer(layerName);
111  auto a = armarx::viz::Arrow("cartesianVelocity");
112  a.pose(currentTCPPoseGlobal);
113  a.direction(diffPos);
114  a.length(diffPos.norm()*3);
115  l.add(a);
116 
117  auto b = armarx::viz::Sphere("targetPose");
118  b.pose(tcpTargetPoseGlobal);
119  b.radius(diffPos.norm());
120  l.add(b);
121 
122  auto o = armarx::viz::Robot("grasp");
123  o.file("RobotAPI", robotAPIHandFileName);
124  o.pose(handRootTargetPoseGlobal);
125  l.add(o);
126 
127  arviz.commit(l);
128  }
129 
130  Eigen::Vector3f angles(0, 0, 0);
131  //float axisRot = 0;
132 
133  // get rotational difference
134  Eigen::Matrix3f diffRot = tcpTargetPoseGlobal.block<3, 3>(0, 0) * currentTCPPoseGlobal.block<3, 3>(0, 0).inverse();
135  angles = VirtualRobot::MathTools::eigen3f2rpy(diffRot);
136  float anglesError = angles.norm();
137 
138  ARMARX_IMPORTANT << VAROUT(angles);
139 
140  //rotation diff around one axis
141  //Eigen::Matrix4f diffPose = Eigen::Matrix4f::Identity();
142  //diffPose.block<3, 3>(0, 0) = diffRot;
143  //Eigen::Vector3f axis;
144  //VirtualRobot::MathTools::eigen4f2axisangle(diffPose, axis, axisRot);
145 
146  // Check velocities
147  if (anglesError < minRotationSpeed && anglesError != 0)
148  {
149  angles *= (minRotationSpeed / anglesError);
150  }
151 
152  else if (anglesError > maxRotationSpeed && anglesError != 0)
153  {
154  angles *= (maxRotationSpeed / anglesError);
155  }
156 
157  if (anglesError < in.parameters.orientationalAccuracy)
158  {
159  angles = Eigen::Vector3f::Zero();
160  }
161 
162  FramedDirection angleVelocity(angles, GlobalFrame, "");
163 
164  // // make sure we don't move the hand too low
165  // if (handPoseFromKinematicModelEigen(2,3) < 1000 && cartesianVelocity->z < 0)
166  // {
167  // ARMARX_IMPORTANT << "Hand too low, moving it up a bit";
168  // if (handPoseFromKinematicModelEigen(2,3) < 950) cartesianVelocity->z = 0.1f*maxSpeed;
169  // else cartesianVelocity->z = 0.05f*maxSpeed;
170  // }
171 
172  auto cartesianError = diffPosError;
173  auto angleError = anglesError; // axisRot
174 
175  ARMARX_INFO << deactivateSpam(0.5) << "Distance to target: " << (std::to_string(cartesianError) + " mm, " + std::to_string(angleError * 180 / M_PI) + " deg") << ", \n" <<
176  "Elapsed time: " << (armarx::core::time::DateTime::Now() - started).toMilliSeconds() << "\n" <<
177  "Current Thresholds: " << in.parameters.positionalAccuracy << "mm, " << in.parameters.orientationalAccuracy * 180.0f / M_PI << "deg";
178 
179  ARMARX_INFO << deactivateSpam(0.5) << "Request to move TCP: angle vel: (" << angleVelocity.x << ", " << angleVelocity.y << ", " << angleVelocity.z << ")\n"
180  << "catesian vel: " << cartesianVelocity.x << ", " << cartesianVelocity.y << ", " << cartesianVelocity.z << ")";
181 
182  if (angleError <= in.parameters.orientationalAccuracy && cartesianError <= in.parameters.positionalAccuracy)
183  {
185  }
186 
187  context.tcpControlUnitPrx->setTCPVelocity(in.parameters.kinematicChainName, tcpName, new FramedDirection(cartesianVelocity), new FramedDirection(angleVelocity));
188  return {ActiveOrTerminatedSkillStatus::Running, nullptr};
189  }
190 
191  Skill::ExitResult MoveTCPToTargetPose::exit(const SpecializedExitInput &in)
192  {
193  clearLayer();
194 
195  auto robot = robotReader.getSynchronizedRobot(in.parameters.robotName, armem::Time::Now(), VirtualRobot::RobotIO::RobotDescription::eStructure);
196  if (!robot)
197  {
198  ARMARX_WARNING << "Unable to get robot from robotstatememory. Abort exit method.";
199  return {.status = TerminatedSkillStatus::Failed};
200  }
201 
202  std::string tcpName = robot->getRobotNodeSet(in.parameters.kinematicChainName)->getTCP()->getName();
203 
204  stopTCPMovement(in.parameters.kinematicChainName, tcpName);
205 
206  if (!tcpControlUnitRequestedOnStart)
207  {
208  context.tcpControlUnitPrx->release();
209  }
210 
211  return {.status = TerminatedSkillStatus::Succeeded};
212  }
213 }
armarx::skills::TCPControlSkill::stopTCPMovement
void stopTCPMovement(const std::string &kinematicChainName, const std::string &tcpName)
Definition: TCPControlSkill.h:71
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:80
armarx::skills::TCPControlSkillContext
Definition: TCPControlSkill.h:46
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::skills::TerminatedSkillStatus::Succeeded
@ Succeeded
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::skills::ActiveOrTerminatedSkillStatus::Succeeded
@ Succeeded
armarx::skills::ActiveOrTerminatedSkillStatus::Failed
@ Failed
armarx::skills
This file is part of ArmarX.
Definition: PeriodicUpdateWidget.cpp:11
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
TCPControlUtil.h
armarx::skills::mixin::RobotReadingSkillMixin::robotReader
armem::robot_state::VirtualRobotReader robotReader
Definition: RobotReadingSkillMixin.h:11
armarx::skills::MoveTCPToTargetPose::MoveTCPToTargetPose
MoveTCPToTargetPose(armem::client::MemoryNameSystem &mns, armarx::viz::Client &arviz, TCPControlSkillContext &context)
Definition: MoveTCPToTargetPose.cpp:27
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::core::time::Frequency
Represents a frequency.
Definition: Frequency.h:17
armarx::viz::Arrow
Definition: Elements.h:198
armarx::armem::robot_state::RobotReader::connect
virtual void connect(armem::client::MemoryNameSystem &memoryNameSystem)
Definition: RobotReader.cpp:48
armarx::skills::TCPControlSkillContext::tcpControlUnitPrx
TCPControlUnitInterfacePrx tcpControlUnitPrx
Definition: TCPControlSkill.h:49
armarx::starts_with
bool starts_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:43
armarx::viz::Sphere
Definition: Elements.h:134
armarx::skills::mixin::ArvizSkillMixin::arviz
armarx::viz::Client arviz
Definition: ArvizSkillMixin.h:11
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::armem::human::Robot
@ Robot
Definition: util.h:14
armarx::skills::mixin::ArvizSkillMixin::layerName
std::string layerName
Definition: ArvizSkillMixin.h:12
armarx::skills::TCPControlSkill::context
TCPControlSkillContext & context
Definition: TCPControlSkill.h:82
armarx::armem::robot_state::VirtualRobotReader::getSynchronizedRobot
VirtualRobot::RobotPtr getSynchronizedRobot(const std::string &name, const VirtualRobot::RobotIO::RobotDescription &loadMode=VirtualRobot::RobotIO::RobotDescription::eStructure, bool blocking=true)
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::skills::ActiveOrTerminatedSkillStatus::Running
@ Running
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::skills::TerminatedSkillStatus::Failed
@ Failed
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::VariantType::FramedDirection
const VariantTypeId FramedDirection
Definition: FramedPose.h:38
armarx::skills::mixin::ArvizSkillMixin::clearLayer
void clearLayer()
Definition: ArvizSkillMixin.h:20
armarx::skills::TCPControlSkill
Definition: TCPControlSkill.h:57
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::skills::MoveTCPToTargetPose::Description
static SkillDescription Description
Definition: MoveTCPToTargetPose.h:50
armarx::skills::Skill::InitResult
A result struct for skill initialization.
Definition: Skill.h:36
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:73
armarx::viz::Client
Definition: Client.h:109
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
MoveTCPToTargetPose.h
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:55