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
33static const std::string layerName{"AStarPathPlannerTestLayer"};
34
35void
37{
38 usingProxy(getProperty<std::string>("WorkingMemoryName").getValue());
39 offeringTopic(getProperty<std::string>("DebugDrawerName").getValue());
40 usingProxy(getProperty<std::string>("AStarPathPlannerName").getValue());
41}
42
43void
45{
47 getProperty<std::string>("WorkingMemoryName").getValue());
49 getProperty<std::string>("DebugDrawerName").getValue());
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
172void
174{
175 debugDrawerPrx->clearLayer(layerName);
176 debugDrawerPrx->clearDebugLayer();
177}
Property< PropertyType > getProperty(const std::string &name)
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
The Pose class.
Definition Pose.h:243
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_INFO_S
Definition Logging.h:202
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187