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 
42 namespace armarx::PlatformGroup
43 {
44  // DO NOT EDIT NEXT LINE
45  ResolveLandmark::SubClassRegistry ResolveLandmark::Registry(ResolveLandmark::GetName(),
47 
48  void
50  {
51  PlatformContext* context = getContext<PlatformContext>();
52 
53  ARMARX_IMPORTANT << "ON ENTER!";
54  auto poseMatToLandmarkPose =
55  [this](Eigen::Matrix4f poseMat, const std::string& landmarkName)
56  {
57  Eigen::Vector3f rpy;
58  VirtualRobot::MathTools::eigen4f2rpy(poseMat, rpy);
59  const float goalYaw = rpy[2];
60 
61  Vector3Ptr goalPose(new Vector3(poseMat(0, 3), poseMat(1, 3), goalYaw));
62  ARMARX_INFO << "Landmark: '" << landmarkName << "' => position: ["
63  << goalPose->toEigen().transpose() << "]";
64  return goalPose;
65  };
66 
67 
68  auto lookupLandmarkNamesWithNavigationMemory =
69  [this, poseMatToLandmarkPose, context](const std::string& sceneName,
70  const std::vector<std::string>& landmarkNames,
71  std::map<std::string, Vector3Ptr>& outPoseMap,
72  std::vector<Vector3Ptr>& outPoseList) -> bool
73  {
74  ARMARX_INFO << "Getting the MNS";
77  mns.setComponent(getContext<armarx::PlatformContext>());
78  locReader.connect(mns);
79 
80  const auto locations = locReader.getAllLocationsInGlobalFrame();
81  ARMARX_INFO << "Got " << locations.size() << " locations";
82 
83  std::vector<std::string> locationNames;
84  for (const auto& [n, l] : locations)
85  {
86  ARMARX_CHECK(l.framedPose.header.frame == armarx::GlobalFrame);
87  locationNames.push_back(n);
88  }
89 
90  for (const std::string& landmarkName : landmarkNames)
91  {
92  const std::string landmarkMemoryName = sceneName + "/" + landmarkName;
93  if (locations.count(landmarkMemoryName) == 0)
94  {
95  std::stringstream ss;
96  ss << "Could not find location with name '" << landmarkMemoryName
97  << "'. Available are: ";
98  for (const auto& location : locations)
99  {
100  ss << "'" << location.first << "', \n";
101  }
102  ARMARX_ERROR << ss.str();
103  return false;
104  }
105  auto location = locations.at(landmarkMemoryName);
106  Vector3Ptr landmarkPose =
107  poseMatToLandmarkPose(location.framedPose.pose, landmarkName);
108  outPoseMap[landmarkName] = landmarkPose;
109  outPoseList.push_back(landmarkPose);
110  }
111 
112  return true;
113  };
114 
115 
116  const std::string sceneName = in.getSceneName();
117  ARMARX_IMPORTANT << "Got scene name: '" << sceneName << "'";
118 
119  std::vector<std::string> landmarkNames;
120  if (in.isLandmarkNamesSet())
121  {
122  landmarkNames = in.getLandmarkNames();
123  }
124  else
125  {
126  landmarkNames = {in.getLandmarkName()};
127  }
128 
129  ARMARX_INFO << "landmark names: " << landmarkNames;
130 
131  std::map<std::string, Vector3Ptr> poseMap;
132  std::vector<Vector3Ptr> poseList;
133  bool success = false;
134 
135  if (true)
136  {
137  ARMARX_INFO << "Calling function lookupLandmarkNamesWithNavigationMemory()";
138  success = lookupLandmarkNamesWithNavigationMemory(
139  sceneName, landmarkNames, poseMap, poseList);
140  ARMARX_INFO << "Calling function lookupLandmarkNamesWithNavigationMemory() Success? "
141  << success;
142  }
143  else
144  {
145  // TODO: Only get old memory stuff if necessary. (Should be removed in the future)
146  auto priorKnowledge = context->priorKnowledgePrx;
147  auto graphSegment = priorKnowledge->getGraphSegment();
148 
149  auto resolveLandmark = [this, sceneName, poseMatToLandmarkPose](
150  ::memoryx::GraphMemorySegmentBasePrx graphSegment,
151  const std::string& landmarkName) -> Vector3Ptr
152  {
153  if (!graphSegment->hasNodeWithName(sceneName, landmarkName))
154  {
155  ARMARX_WARNING << "Target landmark '" << landmarkName
156  << "' doesn't exist in graph " << sceneName;
157  auto nodes = graphSegment->getNodesByScene(sceneName);
158  for (auto& node : nodes)
159  {
160  ARMARX_INFO << "node: " << node->getName();
161  }
162  return nullptr;
163  }
164 
165  auto node = graphSegment->getNodeFromSceneByName(sceneName, landmarkName);
166  ARMARX_CHECK_NOT_NULL(node);
167  ARMARX_CHECK_NOT_NULL(node->getPose());
168 
169  PosePtr pose = PosePtr::dynamicCast(node->getPose());
170  ARMARX_CHECK_NOT_NULL(pose);
171  ARMARX_CHECK_NOT_NULL(pose->position);
172 
173  Eigen::Matrix4f poseMat = pose->toEigen();
174  Vector3Ptr goalPose = poseMatToLandmarkPose(poseMat, landmarkName);
175 
176  return goalPose;
177  };
178 
179 
180  for (const std::string& landmarkName : landmarkNames)
181  {
182  Vector3Ptr pose = resolveLandmark(graphSegment, landmarkName);
183  if (pose)
184  {
185  poseMap[landmarkName] = pose;
186  poseList.push_back(pose);
187  }
188  else
189  {
190  success = true;
191  }
192  }
193  }
194 
195  if (success)
196  {
197  if (in.isLandmarkNamesSet())
198  {
199  ARMARX_INFO << "Setting output maps";
200  // Set output in any case so user can still use a failure.
201  out.setLandmarkPoseMap(poseMap);
202  out.setLandmarkPoseList(poseList);
203  }
204  else
205  {
206  ARMARX_INFO << "Setting output value";
207  ARMARX_CHECK_EQUAL(poseList.size(), 1);
208  out.setlandmarkPosition(poseList.front());
209  }
210  emitSuccess();
211  }
212  else
213  {
214  emitFailure();
215  }
216  }
217 
218  //void ResolveLandmark::run()
219  //{
220  // // put your user code for the execution-phase here
221  // // runs in seperate thread, thus can do complex operations
222  // // should check constantly whether isRunningTaskStopped() returns true
223  //
224  // // get a private kinematic instance for this state of the robot (tick "Robot State Component" proxy checkbox in statechart group)
225  // VirtualRobot::RobotPtr robot = getLocalRobot();
226  //
227  //// uncomment this if you need a continous run function. Make sure to use sleep or use blocking wait to reduce cpu load.
228  // while (!isRunningTaskStopped()) // stop run function if returning true
229  // {
230  // // do your calculations
231  // // synchronize robot clone to most recent state
232  // RemoteRobot::synchronizeLocalClone(robot, getRobotStateComponent());
233  // }
234  //}
235 
236  //void ResolveLandmark::onBreak()
237  //{
238  // // put your user code for the breaking point here
239  // // execution time should be short (<100ms)
240  //}
241 
242  void
244  {
245  // put your user code for the exit point here
246  // execution time should be short (<100ms)
247  }
248 
249  // DO NOT EDIT NEXT FUNCTION
252  {
253  return XMLStateFactoryBasePtr(new ResolveLandmark(stateData));
254  }
255 } // namespace armarx::PlatformGroup
armarx::PlatformGroup::ResolveLandmark::onEnter
void onEnter() override
Definition: ResolveLandmark.cpp:49
armarx::PlatformGroup::ResolveLandmark::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: ResolveLandmark.cpp:251
Reader.h
armarx::PlatformGroup::ResolveLandmark::onExit
void onExit() override
Definition: ResolveLandmark.cpp:243
armarx::armem::locations::client::Reader::getAllLocationsInGlobalFrame
std::map< std::string, armarx::navigation::location::arondto::Location > getAllLocationsInGlobalFrame()
Definition: Reader.cpp:71
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
query.h
ARMARX_CHECK_NOT_NULL
#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...
Definition: ExpressionException.h:206
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
ResolveLandmark.h
armarx::PlatformGroup::ResolveLandmark::ResolveLandmark
ResolveLandmark(const XMLStateConstructorParams &stateData)
Definition: ResolveLandmark.h:32
armarx::PlatformGroup
Definition: MoveInSteps.h:28
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
IceInternal::Handle< Vector3 >
armarx::armem::locations::client::Reader
Definition: Reader.h:18
MemoryXCoreObjectFactories.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::armem::locations::client::Reader::connect
void connect(armem::client::MemoryNameSystem &memoryNameSystem)
Definition: Reader.cpp:24
armarx::PlatformGroup::ResolveLandmark::Registry
static SubClassRegistry Registry
Definition: ResolveLandmark.h:45
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
PlatformContext.h
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::armem::client::MemoryNameSystem::setComponent
void setComponent(ManagedIceObject *component)
Definition: MemoryNameSystem.cpp:440
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
memoryx::KBM::Vector3
Eigen::Vector3d Vector3
Definition: kbm.h:41
Builder.h
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
MemoryXTypesObjectFactories.h
armarx::PlatformContext::priorKnowledgePrx
memoryx::PriorKnowledgeInterfacePrx priorKnowledgePrx
Definition: PlatformContext.h:131
Reader.h
armarx::PlatformContext
Definition: PlatformContext.h:64
MemoryNameSystem.h
ARMARX_CHECK_EQUAL
#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...
Definition: ExpressionException.h:130
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::PlatformContext::memoryNameSystemPrx
armarx::armem::mns::MemoryNameSystemInterfacePrx memoryNameSystemPrx
Definition: PlatformContext.h:128
armarx::status::success
@ success