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
27using namespace armarx;
28using namespace GraspingPipelineGroup;
29
30// DO NOT EDIT NEXT LINE
31CalculateTrajectory::SubClassRegistry
32 CalculateTrajectory::Registry(CalculateTrajectory::GetName(),
34
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
73void
75{
76 // put your user code for the enter-point here
77 // execution time should be short (<100ms)
78
79 graspingManager = getGraspingManager();
80}
81
82void
84{
85 // put your user code for the execution-phase here
86 // runs in seperate thread, thus can do complex operations
87 // should check constantly whether isRunningTaskStopped() returns true
88 memoryx::WorkingMemoryInterfacePrx workingMemory = getWorkingMemory();
89 memoryx::ObjectInstanceBasePtr objectInstance;
90 std::string objectName;
91 ARMARX_INFO << "Run() of CalculateTrajectory started";
92 if (in.isObjectInstanceChannelSet())
93 {
94 objectName = in.getObjectInstanceChannel()->getDataField("className")->getString();
95 auto id = in.getObjectInstanceChannel()->getDataField("id")->getString();
96 objectInstance = workingMemory->getObjectInstancesSegment()->getObjectInstanceById(id);
97 }
98 else if (in.isObjectNameSet())
99 {
100 objectName = in.getObjectName();
101 objectInstance =
102 workingMemory->getObjectInstancesSegment()->getObjectInstanceByName(objectName);
103 }
104 else
105 {
106 ARMARX_ERROR << "Either ObjectName or ObjectInstanceChannel must bet set!";
107 emitFailure();
108 return;
109 }
110 if (!objectInstance)
111 {
112 ARMARX_WARNING << "Could not find ObjectInstance with name " + objectName;
113 emitFailure();
114 return;
115 }
116
117
118 GraspingTrajectory gt;
119 int maxTries = 5;
120
121 for (int i = 0; i < maxTries; i++)
122 {
123 ARMARX_INFO << "Try " << i << " of " << maxTries;
124 try
125 {
126 gt = graspingManager->generateGraspingTrajectory(objectInstance->getId());
127 }
128 catch (armarx::LocalException&)
129 {
131 }
132
133 if (gt.poseTrajectory && gt.configTrajectory && !gt.rnsToUse.empty())
134 {
135 // gt.configTrajectory = balanceTimestamps(TrajectoryPtr::dynamicCast(gt.configTrajectory));
136 out.setKinematicChainName(gt.rnsToUse);
137 out.setHandName(gt.endeffector);
138 // gt.poseTrajectory = balanceTimestamps(TrajectoryPtr::dynamicCast(gt.poseTrajectory));
139
140 JSONObjectPtr json = new JSONObject();
141 json->setVariant("posetrajectory", new Variant(gt.poseTrajectory));
142 ARMARX_INFO << "Json: " << json->asString(true);
143
144 out.setPlatformTrajectory(gt.poseTrajectory);
145 out.setJointTrajectory(gt.configTrajectory);
146 out.setGraspPose(gt.grasp.framedPose);
147 out.setPreGraspPose(gt.grasp.framedPrePose);
148 std::vector<Vector3Ptr> platformPointList;
149 TrajectoryPtr platformTraj = TrajectoryPtr ::dynamicCast(gt.poseTrajectory);
150 for (const Trajectory::TrajData& point : *platformTraj)
151 {
152 platformPointList.push_back(
153 new Vector3(point.getPosition(0), point.getPosition(1), point.getPosition(2)));
154 }
155
156 out.setPlatformPointList(platformPointList);
157 emitSuccess();
158 return;
159 }
160 else
161 {
162
164 << "GraspingManger could not generate a graspingTrajectory. Trying again....";
165 }
166 }
167 ARMARX_WARNING << "Stopped trying to find a solution after " << maxTries << " trys.";
168 emitFailure();
169}
170
171//void CalculateTrajectory::onBreak()
172//{
173// // put your user code for the breaking point here
174// // execution time should be short (<100ms)
175//}
176
177void
179{
180 // put your user code for the exit point here
181 // execution time should be short (<100ms)
182}
183
184// DO NOT EDIT NEXT FUNCTION
#define VAROUT(x)
CalculateTrajectory(const XMLStateConstructorParams &stateData)
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition JSONObject.h:44
The Trajectory class represents n-dimensional sampled trajectories.
Definition Trajectory.h:77
The Variant class is described here: Variants.
Definition Variant.h:224
The Vector3 class.
Definition Pose.h:113
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
This file offers overloads of toIce() and fromIce() functions for STL container types.
void handleExceptions()
IceInternal::Handle< Trajectory > TrajectoryPtr
Definition Trajectory.h:52
IceInternal::Handle< JSONObject > JSONObjectPtr
Definition JSONObject.h:34
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64