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