AStarPathPlannerTestComponent.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 RobotComponents::applications::AStarPathPlannerTestApp
17 * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18 * @date 2015 Humanoids Group, H2T, KIT
19 * @license http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
24 
25 #include <chrono>
26 
32 
33 static const std::string layerName{"AStarPathPlannerTestLayer"};
34 
35 void
37 {
38  usingProxy(getProperty<std::string>("WorkingMemoryName").getValue());
39  offeringTopic(getProperty<std::string>("DebugDrawerName").getValue());
40  usingProxy(getProperty<std::string>("AStarPathPlannerName").getValue());
41 }
42 
43 void
45 {
46  workingMemoryPrx = getProxy<memoryx::WorkingMemoryInterfacePrx>(
47  getProperty<std::string>("WorkingMemoryName").getValue());
48  debugDrawerPrx = getTopic<armarx::DebugDrawerInterfacePrx>(
49  getProperty<std::string>("DebugDrawerName").getValue());
50  aStarPathPlannerPrx = getProxy<armarx::AStarPathPlannerBasePrx>(
51  getProperty<std::string>("AStarPathPlannerName").getValue());
52 
53  //pass all objects from the scene to the planner
54  auto objInstPrx = workingMemoryPrx->getObjectInstancesSegment();
55  const auto objIds = objInstPrx->getAllEntityIds();
56 
57  armarx::ObjectPositionBaseList objects{};
58 
59  for (const auto& id : objIds)
60  {
61  const memoryx::EntityBasePtr entityBase = objInstPrx->getEntityById(id);
62  const memoryx::ObjectInstanceBasePtr object =
63  memoryx::ObjectInstancePtr::dynamicCast(entityBase);
64 
65  assert(object);
66 
67  const std::string className = object->getMostProbableClass();
68 
69  memoryx::ObjectClassList classes = workingMemoryPrx->getPriorKnowledge()
70  ->getObjectClassesSegment()
71  ->getClassWithSubclasses(className);
72 
73  if (!classes.size())
74  {
75  ARMARX_INFO << "No classes for most probable class '" << className << "' of object '"
76  << object->getName() << "' with id " << id;
77  continue;
78  }
79 
80  armarx::ObjectPositionBase obj;
81 
82  obj.objectClassBase = classes.at(0);
83 
84  obj.objectPose = ::armarx::PoseBasePtr{
85  new Pose{object->getPositionBase(), object->getOrientationBase()}};
86 
87  objects.push_back(obj);
88 
89  ARMARX_VERBOSE << "Added class '" << className << "' of object '" << object->getName()
90  << "' with id " << id;
91  }
92 
93  ARMARX_INFO << "using " << objects.size() << "objects";
94 
95  aStarPathPlannerPrx->setCollisionObjects(objects);
96 
97  memoryx::AgentInstancesSegmentBasePrx agSegPrx = workingMemoryPrx->getAgentInstancesSegment();
98 
99  //set agent
100  const auto agentIds = agSegPrx->getAllEntityIds();
101 
102  if (!agentIds.size())
103  {
104  ARMARX_ERROR << "No agent found in memory";
105  return;
106  }
107 
108  const auto agent0Id = agentIds.at(0);
109 
110  ARMARX_INFO << "using agent " << agent0Id;
111 
112  memoryx::AgentInstanceBasePtr agent = agSegPrx->getAgentInstanceById(agent0Id);
113 
114  std::string agentCollisionModelName{"Platform"};
115 
116  aStarPathPlannerPrx->setAgent(agent, agentCollisionModelName);
117  aStarPathPlannerPrx->setSafetyMargin(0);
118 
119 
120  //plan the path
121  armarx::Vector3BasePtr from{new armarx::Vector3{1900.f, 2300.f, 0.f}};
122  //armarx::Vector3BasePtr to {new armarx::Vector3{2500.f, 7000.f, 1.f}};
123  armarx::Vector3BasePtr to{new armarx::Vector3{2600.f, 9500.f, 1.f}};
124 
125 
126  ARMARX_INFO << "Starting path planning";
127  Vector3BaseList path;
128  std::size_t iterations = 25;
129  std::chrono::high_resolution_clock::duration duration{0};
130 
131  for (std::size_t i = 1; i <= iterations; ++i)
132  {
133  ARMARX_INFO_S << "iteration " << i;
134  const auto tStart = std::chrono::high_resolution_clock::now();
135  path = aStarPathPlannerPrx->getPath(from, to);
136  const auto tEnd = std::chrono::high_resolution_clock::now();
137  duration += (tEnd - tStart);
138  }
140  << "Path planning done (avg). T[ms] = "
141  << std::chrono::duration_cast<std::chrono::milliseconds>(duration / iterations).count();
142 
143  if (!path.size())
144  {
145  ARMARX_INFO << "Found no path!";
146  return;
147  }
148 
149  //draw the path
150 
151  std::stringstream lineName{};
152  DrawColor color{1.f, 0.f, 0.f, 1.f};
153 
154  for (std::size_t i = 0; i < path.size() - 1; ++i)
155  {
156  lineName.str(layerName);
157  lineName << "from_" << i << "_to_" << i + 1;
158  debugDrawerPrx->setLineVisu(
159  layerName, lineName.str(), path.at(i), path.at(i + 1), 5.f, color);
160 
161  //change color to make the first edge distinct
162  if (!i)
163  {
164  color.r = 0.f;
165  color.g = 1.f;
166  }
167  }
168 
169  ARMARX_INFO << "Found path whith " << path.size() - 1 << " edges";
170 }
171 
172 void
174 {
175  debugDrawerPrx->clearLayer(layerName);
176  debugDrawerPrx->clearDebugLayer();
177 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
AStarPathPlannerTestComponent.h
armarx::AStarPathPlannerTestComponent::onConnectComponent
void onConnectComponent() override
Definition: AStarPathPlannerTestComponent.cpp:44
ObjectClass.h
armarx::AStarPathPlannerTestComponent::onInitComponent
void onInitComponent() override
Definition: AStarPathPlannerTestComponent.cpp:36
armarx::AStarPathPlannerTestComponent::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: AStarPathPlannerTestComponent.cpp:173
armarx::Vector3
The Vector3 class.
Definition: Pose.h:112
MemoryXCoreObjectFactories.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::Pose
The Pose class.
Definition: Pose.h:242
ObjectInstance.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:300
MemoryXTypesObjectFactories.h
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:202
MemoryXUpdaterObjectFactories.h
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:154