ResolveLandmark.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::PlatformGroup
17 * @author Fabian PK ( fabian dot peller-konrad at kit dot edu )
18 * @date 2020
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#include "ResolveLandmark.h"
24
25#include "PlatformContext.h"
26
27//#include <ArmarXCore/core/time/TimeUtil.h>
28//#include <ArmarXCore/observers/variant/DatafieldRef.h>
29
30#include <VirtualRobot/MathTools.h>
31
36#include <RobotAPI/libraries/armem_locations/aron/Location.aron.generated.h>
38
41
43{
44 // DO NOT EDIT NEXT LINE
45 ResolveLandmark::SubClassRegistry ResolveLandmark::Registry(ResolveLandmark::GetName(),
47
48 void
53
55 {
56 PlatformContext* context = getContext<PlatformContext>();
57
58 ARMARX_IMPORTANT << "ON ENTER!";
59 auto poseMatToLandmarkPose =
60 [this](Eigen::Matrix4f poseMat, const std::string& landmarkName)
61 {
62 Eigen::Vector3f rpy;
63 VirtualRobot::MathTools::eigen4f2rpy(poseMat, rpy);
64 const float goalYaw = rpy[2];
65
66 Vector3Ptr goalPose(new Vector3(poseMat(0, 3), poseMat(1, 3), goalYaw));
67 ARMARX_INFO << "Landmark: '" << landmarkName << "' => position: ["
68 << goalPose->toEigen().transpose() << "]";
69 return goalPose;
70 };
71
72
73 auto lookupLandmarkNamesWithNavigationMemory =
74 [this, poseMatToLandmarkPose, context](const std::string& sceneName,
75 const std::vector<std::string>& landmarkNames,
76 std::map<std::string, Vector3Ptr>& outPoseMap,
77 std::vector<Vector3Ptr>& outPoseList) -> bool
78 {
79 ARMARX_INFO << "Getting the MNS";
82 mns.setComponent(getContext<armarx::PlatformContext>());
83 locReader.connect(mns);
84
85 const auto locations = locReader.getAllLocationsInGlobalFrame();
86 ARMARX_INFO << "Got " << locations.size() << " locations";
87
88 std::vector<std::string> locationNames;
89 for (const auto& [n, l] : locations)
90 {
91 ARMARX_CHECK(l.framedPose.header.frame == armarx::GlobalFrame);
92 locationNames.push_back(n);
93 }
94
95 for (const std::string& landmarkName : landmarkNames)
96 {
97 const std::string landmarkMemoryName = sceneName + "/" + landmarkName;
98 if (locations.count(landmarkMemoryName) == 0)
99 {
100 std::stringstream ss;
101 ss << "Could not find location with name '" << landmarkMemoryName
102 << "'. Available are: ";
103 for (const auto& location : locations)
104 {
105 ss << "'" << location.first << "', \n";
106 }
107 ARMARX_ERROR << ss.str();
108 return false;
109 }
110 auto location = locations.at(landmarkMemoryName);
111 Vector3Ptr landmarkPose =
112 poseMatToLandmarkPose(location.framedPose.pose, landmarkName);
113 outPoseMap[landmarkName] = landmarkPose;
114 outPoseList.push_back(landmarkPose);
115 }
116
117 return true;
118 };
119
120
121 const std::string sceneName = in.getSceneName();
122 ARMARX_IMPORTANT << "Got scene name: '" << sceneName << "'";
123
124 std::vector<std::string> landmarkNames;
125 if (in.isLandmarkNamesSet())
126 {
127 landmarkNames = in.getLandmarkNames();
128 }
129 else
130 {
131 landmarkNames = {in.getLandmarkName()};
132 }
133
134 ARMARX_INFO << "landmark names: " << landmarkNames;
135
136 std::map<std::string, Vector3Ptr> poseMap;
137 std::vector<Vector3Ptr> poseList;
138 bool success = false;
139
140 if (true)
141 {
142 ARMARX_INFO << "Calling function lookupLandmarkNamesWithNavigationMemory()";
143 success = lookupLandmarkNamesWithNavigationMemory(
144 sceneName, landmarkNames, poseMap, poseList);
145 ARMARX_INFO << "Calling function lookupLandmarkNamesWithNavigationMemory() Success? "
146 << success;
147 }
148 else
149 {
150 // TODO: Only get old memory stuff if necessary. (Should be removed in the future)
151 auto priorKnowledge = context->priorKnowledgePrx;
152 auto graphSegment = priorKnowledge->getGraphSegment();
153
154 auto resolveLandmark = [this, sceneName, poseMatToLandmarkPose](
155 ::memoryx::GraphMemorySegmentBasePrx graphSegment,
156 const std::string& landmarkName) -> Vector3Ptr
157 {
158 if (!graphSegment->hasNodeWithName(sceneName, landmarkName))
159 {
160 ARMARX_WARNING << "Target landmark '" << landmarkName
161 << "' doesn't exist in graph " << sceneName;
162 auto nodes = graphSegment->getNodesByScene(sceneName);
163 for (auto& node : nodes)
164 {
165 ARMARX_INFO << "node: " << node->getName();
166 }
167 return nullptr;
168 }
169
170 auto node = graphSegment->getNodeFromSceneByName(sceneName, landmarkName);
172 ARMARX_CHECK_NOT_NULL(node->getPose());
173
174 PosePtr pose = PosePtr::dynamicCast(node->getPose());
176 ARMARX_CHECK_NOT_NULL(pose->position);
177
178 Eigen::Matrix4f poseMat = pose->toEigen();
179 Vector3Ptr goalPose = poseMatToLandmarkPose(poseMat, landmarkName);
180
181 return goalPose;
182 };
183
184
185 for (const std::string& landmarkName : landmarkNames)
186 {
187 Vector3Ptr pose = resolveLandmark(graphSegment, landmarkName);
188 if (pose)
189 {
190 poseMap[landmarkName] = pose;
191 poseList.push_back(pose);
192 }
193 else
194 {
195 success = true;
196 }
197 }
198 }
199
200 if (success)
201 {
202 if (in.isLandmarkNamesSet())
203 {
204 ARMARX_INFO << "Setting output maps";
205 // Set output in any case so user can still use a failure.
206 out.setLandmarkPoseMap(poseMap);
207 out.setLandmarkPoseList(poseList);
208 }
209 else
210 {
211 ARMARX_INFO << "Setting output value";
212 ARMARX_CHECK_EQUAL(poseList.size(), 1);
213 out.setlandmarkPosition(poseList.front());
214 }
215 emitSuccess();
216 }
217 else
218 {
219 emitFailure();
220 }
221 }
222
223 //void ResolveLandmark::onBreak()
224 //{
225 // // put your user code for the breaking point here
226 // // execution time should be short (<100ms)
227 //}
228
229 void
231 {
232 // put your user code for the exit point here
233 // execution time should be short (<100ms)
234 }
235
236 // DO NOT EDIT NEXT FUNCTION
242} // namespace armarx::PlatformGroup
memoryx::PriorKnowledgeInterfacePrx priorKnowledgePrx
armarx::armem::mns::MemoryNameSystemInterfacePrx memoryNameSystemPrx
ResolveLandmark(const XMLStateConstructorParams &stateData)
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
The Vector3 class.
Definition Pose.h:113
The memory name system (MNS) client.
std::map< std::string, armarx::navigation::location::arondto::Location > getAllLocationsInGlobalFrame()
Definition Reader.cpp:73
void connect(armem::client::MemoryNameSystem &memoryNameSystem)
Definition Reader.cpp:26
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#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
std::string const GlobalFrame
Variable of the global coordinate system.
Definition FramedPose.h:65
IceInternal::Handle< Vector3 > Vector3Ptr
Definition Pose.h:165
IceInternal::Handle< Pose > PosePtr
Definition Pose.h:306
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition XMLState.h:64