CalculateTrajectory.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::GraspingPipelineGroup
17  * @author Stefan Reither ( stef dot reither at web dot de )
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "CalculateTrajectory.h"
24 
26 
27 using namespace armarx;
28 using namespace GraspingPipelineGroup;
29 
30 // DO NOT EDIT NEXT LINE
31 CalculateTrajectory::SubClassRegistry CalculateTrajectory::Registry(CalculateTrajectory::GetName(), &CalculateTrajectory::CreateInstance);
32 
33 
34 
35 
37 {
38  ARMARX_INFO << t->output();
39  double length = t->getLength(0);
40  if (length == 0.0f)
41  {
42  return t;
43  }
44  ARMARX_INFO << VAROUT(length);
45  double timelength = t->getTimeLength();
46  ARMARX_INFO << VAROUT(timelength);
47  auto timestamps = t->getTimestamps();
48  ARMARX_INFO << VAROUT(timestamps);
49  Ice::DoubleSeq newTimestamps;
50  newTimestamps.push_back(0);
51  for (size_t var = 0; var < timestamps.size() - 1; ++var)
52  {
53  double tBefore = timestamps.at(var);
54  double tAfter = (timestamps.at(var + 1));
55  ARMARX_INFO << VAROUT(tBefore) << VAROUT(tAfter);
56  double partLength = t->getLength(0, tBefore, tAfter);
57  double lengthPortion = partLength / length;
58  ARMARX_INFO << VAROUT(partLength) << VAROUT(lengthPortion);
59  newTimestamps.push_back(*newTimestamps.rbegin() + timelength * lengthPortion);
60  }
61  ARMARX_INFO << VAROUT(newTimestamps);
62  TrajectoryPtr newTraj = new Trajectory();
63  for (size_t d = 0; d < t->dim(); ++d)
64  {
65  newTraj->addDimension(t->getDimensionData(d), newTimestamps, t->getDimensionName(d));
66  }
67  newTraj->setLimitless(t->getLimitless());
68  ARMARX_INFO << newTraj->output();
69 
70  return newTraj;
71 }
72 
74 {
75  // put your user code for the enter-point here
76  // execution time should be short (<100ms)
77 
78  graspingManager = getGraspingManager();
79 }
80 
82 {
83  // put your user code for the execution-phase here
84  // runs in seperate thread, thus can do complex operations
85  // should check constantly whether isRunningTaskStopped() returns true
86  memoryx::WorkingMemoryInterfacePrx workingMemory = getWorkingMemory();
87  memoryx::ObjectInstanceBasePtr objectInstance;
88  std::string objectName;
89  ARMARX_INFO << "Run() of CalculateTrajectory started";
90  if (in.isObjectInstanceChannelSet())
91  {
92  objectName = in.getObjectInstanceChannel()->getDataField("className")->getString();
93  auto id = in.getObjectInstanceChannel()->getDataField("id")->getString();
94  objectInstance = workingMemory->getObjectInstancesSegment()->getObjectInstanceById(id);
95 
96  }
97  else if (in.isObjectNameSet())
98  {
99  objectName = in.getObjectName();
100  objectInstance = workingMemory->getObjectInstancesSegment()->getObjectInstanceByName(objectName);
101  }
102  else
103  {
104  ARMARX_ERROR << "Either ObjectName or ObjectInstanceChannel must bet set!";
105  emitFailure();
106  return;
107  }
108  if (!objectInstance)
109  {
110  ARMARX_WARNING << "Could not find ObjectInstance with name " + objectName;
111  emitFailure();
112  return;
113  }
114 
115 
116  GraspingTrajectory gt;
117  int maxTries = 5;
118 
119  for (int i = 0; i < maxTries; i++)
120  {
121  ARMARX_INFO << "Try " << i << " of " << maxTries;
122  try
123  {
124  gt = graspingManager->generateGraspingTrajectory(objectInstance->getId());
125  }
126  catch (armarx::LocalException&)
127  {
129  }
130 
131  if (gt.poseTrajectory && gt.configTrajectory && !gt.rnsToUse.empty())
132  {
133  // gt.configTrajectory = balanceTimestamps(TrajectoryPtr::dynamicCast(gt.configTrajectory));
134  out.setKinematicChainName(gt.rnsToUse);
135  out.setHandName(gt.endeffector);
136  // gt.poseTrajectory = balanceTimestamps(TrajectoryPtr::dynamicCast(gt.poseTrajectory));
137 
138  JSONObjectPtr json = new JSONObject();
139  json->setVariant("posetrajectory", new Variant(gt.poseTrajectory));
140  ARMARX_INFO << "Json: " << json->asString(true);
141 
142  out.setPlatformTrajectory(gt.poseTrajectory);
143  out.setJointTrajectory(gt.configTrajectory);
144  out.setGraspPose(gt.grasp.framedPose);
145  out.setPreGraspPose(gt.grasp.framedPrePose);
146  std::vector<Vector3Ptr> platformPointList;
147  TrajectoryPtr platformTraj = TrajectoryPtr ::dynamicCast(gt.poseTrajectory);
148  for (const Trajectory::TrajData& point : *platformTraj)
149  {
150  platformPointList.push_back(new Vector3(point.getPosition(0),
151  point.getPosition(1),
152  point.getPosition(2)));
153  }
154 
155  out.setPlatformPointList(platformPointList);
156  emitSuccess();
157  return;
158  }
159  else
160  {
161 
162  ARMARX_INFO << "GraspingManger could not generate a graspingTrajectory. Trying again....";
163  }
164  }
165  ARMARX_WARNING << "Stopped trying to find a solution after " << maxTries << " trys.";
166  emitFailure();
167 
168 }
169 
170 //void CalculateTrajectory::onBreak()
171 //{
172 // // put your user code for the breaking point here
173 // // execution time should be short (<100ms)
174 //}
175 
177 {
178  // put your user code for the exit point here
179  // execution time should be short (<100ms)
180 }
181 
182 
183 // DO NOT EDIT NEXT FUNCTION
185 {
186  return XMLStateFactoryBasePtr(new CalculateTrajectory(stateData));
187 }
188 
armarx::GraspingPipelineGroup::CalculateTrajectory::onExit
void onExit() override
Definition: CalculateTrajectory.cpp:176
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::GraspingPipelineGroup::CalculateTrajectory::onEnter
void onEnter() override
Definition: CalculateTrajectory.cpp:73
armarx::GraspingPipelineGroup::CalculateTrajectory::balanceTimestamps
TrajectoryPtr balanceTimestamps(TrajectoryPtr t)
Definition: CalculateTrajectory.cpp:36
armarx::GraspingPipelineGroup::CalculateTrajectory::Registry
static SubClassRegistry Registry
Definition: CalculateTrajectory.h:48
JSONObject.h
armarx::GraspingPipelineGroup::CalculateTrajectory::graspingManager
GraspingManagerInterfacePrx graspingManager
Definition: CalculateTrajectory.h:54
armarx::JSONObject
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition: JSONObject.h:43
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
armarx::Trajectory::TrajData
Definition: Trajectory.h:85
armarx::GraspingPipelineGroup::CalculateTrajectory::CalculateTrajectory
CalculateTrajectory(const XMLStateConstructorParams &stateData)
Definition: CalculateTrajectory.h:33
IceInternal::Handle< Trajectory >
armarx::GraspingPipelineGroup::CalculateTrajectory::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: CalculateTrajectory.cpp:184
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
CalculateTrajectory.h
armarx::VariantType::Trajectory
const VariantTypeId Trajectory
Definition: Trajectory.h:44
armarx::GraspingPipelineGroup::CalculateTrajectory::run
void run() override
Definition: CalculateTrajectory.cpp:81
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
memoryx::KBM::Vector3
Eigen::Vector3d Vector3
Definition: kbm.h:41
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:141
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28