SelfLocalization.h
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  * @author Fabian Reister ( fabian dot reister at kit dot edu )
17  * @date 2021
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
22 #pragma once
23 
24 #include <atomic>
25 #include <optional>
26 
27 #include <Eigen/Core>
28 #include <Eigen/Geometry>
29 
34 
38 #include <RobotAPI/interface/core/RobotLocalization.h>
39 #include <RobotAPI/interface/units/LocalizationUnitInterface.h>
40 #include <RobotAPI/interface/units/PlatformUnitInterface.h>
43 
44 #include <RobotComponents/interface/components/LaserScannerSelfLocalisation.h>
45 
46 #include <MemoryX/interface/components/LongtermMemoryInterface.h>
47 #include <MemoryX/interface/components/WorkingMemoryInterface.h>
49 
51 {
52  class TransformWriterInterface;
53  class TransformReaderInterface;
54 } // namespace armarx::armem::client::robot_state::localization
55 
56 namespace armarx
57 {
58 
59  struct PoseStamped
60  {
61  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
62 
63  Eigen::Affine3f pose = Eigen::Affine3f::Identity(); // [mm]
65  };
66 
67  /**
68  * @brief The SelfLocalization class.
69  *
70  * This class serves as a base class for all self-localization implementations.
71  * The global pose is sent to a multitude of topics, written to ArMem and to MemoryX short and longterm memory,
72  * which is all done by this class.
73  *
74  * All you have to do, is to
75  * - implement worldToMapTransform()
76  * - call publishSelfLocalization(...) whenever needed.
77  *
78  *
79  * Hint:
80  * If your component which is derived from SelfLocalization implements its own interface,
81  * make sure that it also extends the MemoryListenerInterface
82  * (RobotAPI/interface/armem/client/MemoryListenerInterface.ice).
83  */
84  class
85  [[deprecated("This class was moved to armarx::localization_and_mapping. It is kept for ArmarXSimulation but should not be removed after CMake modernization of ArmarXSimulation.")]]
87  virtual public armarx::Component,
89  {
90 
91  public:
93  virtual ~SelfLocalization();
94 
95  PropertyDefinitionsPtr createPropertyDefinitions() override;
96 
97  void publishSelfLocalization(const PoseStamped& map_T_robot);
98 
99  [[nodiscard]] std::optional<Eigen::Affine3f> globalPoseFromLongTermMemory() const;
100 
101  /// @see armarx::ManagedIceObject::onInitComponent()
102  void onInitComponent() override;
103 
104  /// @see armarx::ManagedIceObject::onConnectComponent()
105  void onConnectComponent() override;
106 
107  void onReconnectComponent();
108 
109  /// @see armarx::ManagedIceObject::onDisconnectComponent()
110  void onDisconnectComponent() override;
111 
112  /// @see armarx::ManagedIceObject::onExitComponent()
113  void
114  onExitComponent() override
115  {
116  }
117 
118 
119  protected:
120  /// static transformation from world to map
121  virtual Eigen::Affine3f worldToMapTransform() const = 0;
122 
123  const std::string& getAgentName() const noexcept;
124  const std::string& getRobotRootFrame() const noexcept;
125  const std::shared_ptr<VirtualRobot::Robot>& getRobot() const noexcept;
126 
127  armem::client::robot_state::localization::TransformWriterInterface&
128  getTransformWriter() const;
129  const armem::client::robot_state::localization::TransformReaderInterface&
130  getTransformReader() const;
131 
132  private:
133  void publishSelfLocalizationOnTopic(const PoseStamped& map_T_robot);
134 
135  void publishSelfLocalizationToWorkingMemory(const PoseStamped& map_T_robot);
136  void publishSelfLocalizationToLongtermMemory(const PoseStamped& map_T_robot);
137  void publishSelfLocalizationToArMem(const PoseStamped& map_T_robot);
138 
139  // setup
140  void setupArMem();
141  void setupWorkingMemory();
142  void setupLongTermMemory();
143 
144  // helpers
145  TransformStamped toTransformStamped(const PoseStamped& poseStamped);
146  armem::robot_state::Transform toTransform(const PoseStamped& poseStamped);
147 
148  // publisher
149  LaserScannerSelfLocalisationListenerPrx reportTopic;
150  GlobalRobotPoseLocalizationListenerPrx globalRobotPoseTopic;
151  GlobalRobotPoseLocalizationCorrectionListenerPrx gobalRobotPoseCorrectionTopic;
152 
153  // long-term memory
154  memoryx::LongtermMemoryInterfacePrx longtermMemory;
155  memoryx::PersistentEntitySegmentBasePrx ltmSelfLocalisationSegment;
156  std::optional<std::string> ltmPoseEntityId;
157 
158  // working memory
159  memoryx::WorkingMemoryInterfacePrx workingMemory;
160  memoryx::AgentInstancesSegmentBasePrx wmAgentsMemory;
161  memoryx::AgentInstancePtr wmAgentInstance;
162 
163  // proxies
164  RobotStateComponentInterfacePrx robotStateComponent;
165  LocalizationUnitInterfacePrx localizationUnitPrx;
166 
167  // utils
168  IceReportSkipper skipper{0.};
169 
170  struct Properties
171  {
172  // old working memory
173  bool updateWorkingMemory = true;
174  float workingMemoryUpdateFrequency = 50.F;
175 
176  // old long term memory
177  std::string longtermMemoryName = "LongtermMemory";
178  bool updateLongTermMemory = true;
179  float longtermMemoryUpdateFrequency = 1.F;
180  bool initialPoseFromLTM = true;
181 
182  // armem
183  bool updateArMem = true;
184  float armMemUpdateFrequency = 1000.F;
185 
186  // general
187  bool useLocalizationUnit = true;
188  std::string localizationUnitName = ""; // will be set in c'tor
189  } properties;
190 
191  std::string agentName;
192  std::string robotRootFrame;
193 
194  std::shared_ptr<VirtualRobot::Robot> robot;
195 
196  armem::client::plugins::ReaderWriterPlugin<armem::client::robot_state::localization::TransformWriter>* transformWriterPlugin = nullptr;
197  armem::client::plugins::ReaderWriterPlugin<armem::client::robot_state::localization::TransformReader>* transformReaderPlugin = nullptr;
198 
199  Eigen::Affine3f global_T_map;
200 
201  std::atomic_bool connected{false};
202 
203  bool onConnectCalled{false};
204  };
205 
206 } // namespace armarx
memoryx::AgentInstancePtr
IceInternal::Handle< AgentInstance > AgentInstancePtr
Typedef of AgentEntityPtr as IceInternal::Handle<AgentEntity> for convenience.
Definition: AgentInstance.h:38
armarx::PoseStamped::pose
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Eigen::Affine3f pose
Definition: SelfLocalization.h:63
TransformReader.h
VirtualRobot
Definition: FramedPose.h:43
armarx::SelfLocalization
The SelfLocalization class.
Definition: SelfLocalization.h:84
armarx::armem::client::robot_state::localization
Definition: interfaces.h:33
DateTime.h
armarx::armem
Definition: LegacyRobotStateMemoryAdapter.cpp:31
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
ReaderWriterPlugin.h
armarx::SelfLocalization::onExitComponent
void onExitComponent() override
Definition: SelfLocalization.h:114
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::armem::human::Robot
@ Robot
Definition: util.h:14
armarx::PoseStamped
Definition: SelfLocalization.h:59
IceReportSkipper.h
types.h
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
PropertyDefinitionContainer.h
std
Definition: Application.h:66
IceUtil::Handle< class PropertyDefinitionContainer >
IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface >
AgentInstance.h
TransformWriter.h
armarx::IceReportSkipper
Definition: IceReportSkipper.h:36
armarx::PoseStamped::timestamp
armarx::DateTime timestamp
Definition: SelfLocalization.h:64
armarx::armem::client::plugins::ListeningPluginUser
A memory name system client which listens to the memory updates topic (MemoryListenerInterface).
Definition: ListeningPluginUser.h:23
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
ListeningPluginUser.h