Component.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 <cstdint>
26#include <experimental/memory>
27#include <memory>
28#include <mutex>
29#include <optional>
30#include <string>
31
32#include <Eigen/Core>
33#include <Eigen/Geometry>
34
35#include <Ice/Current.h>
36
41#include <ArmarXCore/interface/core/PackagePath.h>
42#include <ArmarXCore/interface/observers/ObserverInterface.h>
43#include <ArmarXCore/interface/observers/Timestamp.h>
44
45#include <ArmarXGui/interface/RemoteGuiInterface.h>
46
48#include <RobotAPI/interface/units/LaserScannerUnit.h>
53
58#include <armarx/localization_and_mapping/components/cartographer_mapping_and_localization/ComponentInterface.h>
63
64// forward decl
66{
67 class MapBuilderInterface;
68} // namespace cartographer::mapping
69
70namespace armarx
71{
72 class TransformWriterInterface;
73 class TransformReaderInterface;
75} // namespace armarx
76
78{
79 // forward decl
80 class ArVizDrawer;
82} // namespace armarx::localization_and_mapping::cartographer_adapter
83
85{
86
87
88 /**
89 * @defgroup Component-CartographerMappingAndLocalization CartographerMappingAndLocalization
90 * @ingroup RobotComponents-Components
91 * A description of the component CartographerMappingAndLocalization.
92 *
93 * @class CartographerMappingAndLocalization
94 * @ingroup Component-CartographerMappingAndLocalization
95 * @brief Brief description of class CartographerMappingAndLocalization.
96 *
97 * Detailed description of class CartographerMappingAndLocalization.
98 */
99 class Component :
101 virtual public ArVizComponentPluginUser,
103 virtual public RemoteGuiCallee,
104 virtual public LocalizationRemoteGuiCallee,
106 {
107 public:
108 Component();
109 virtual ~Component();
110
111 /// @see armarx::ManagedIceObject::getDefaultName()
112 std::string getDefaultName() const override;
113 static std::string GetDefaultName();
114
115 // LaserScannerUnitListener interface
116 void reportSensorValues(const ::std::string&,
117 const ::std::string&,
118 const ::armarx::LaserScan&,
119 const ::armarx::TimestampBasePtr&,
120 const ::Ice::Current&) override;
121
122 // PlatformUnitListener interface
123 // TODO: use streaming (RobotUnit)
124 // void reportOdometryPose(const TransformStamped& odometryPose, const Ice::Current&) override;
125 // void reportOdometryVelocity(const TwistStamped& velocity, const Ice::Current&) override;
126
127 // SlamDataCallable interface
128 void onLocalSlamData(const cartographer_adapter::LocalSlamData& slamData) override;
129 // void onGraphOptimized(const OptimizedGraphData& graphData) override;
130 // void onGridMap(const SubMapDataVector& submapData) override;
131 void onLaserSensorData(const cartographer_adapter::LaserScannerData& laserData) override;
132
133 // RemoteGuiCallee
135
136 enum class Mode
137 {
140 };
141
142 void onLocalizationCorrection(const Eigen::Isometry3f& map_T_map_correct) override;
143 void onApplyLocalizationCorrection() override;
144
150
151 protected:
152 /// @see armarx::ManagedIceObject::onInitComponent()
153 void onInitComponent() override;
154
155 /// @see armarx::ManagedIceObject::onConnectComponent()
156 void onConnectComponent() override;
157
158 /// @see armarx::ManagedIceObject::onDisconnectComponent()
159 void onDisconnectComponent() override;
160
162
163 /// @see armarx::ManagedIceObject::onDisconnectComponent()
164 void onExitComponent() override;
165
166 /// @see PropertyUser::createPropertyDefinitions()
168
169 Eigen::Isometry3f worldToMapTransform() const override;
170
171 private:
172 void setupMappingAdapter();
173
174 //! adapter to cartographer
175 std::unique_ptr<cartographer_adapter::CartographerAdapter> mappingAdapter;
177
178 //! ArViz plotter
179 std::unique_ptr<cartographer_adapter::ArVizDrawer> arvizDrawer;
180
181 //! Remote GUI to trigger map creation
182 std::unique_ptr<MappingRemoteGui> mappingRemoteGui;
183 std::unique_ptr<LocalizationRemoteGui> localizationRemoteGui;
184
185 // queues to handle incoming messages
187 laserMessageQueue;
189 odomMessageQueue;
190
191 //! helper flag to prevent creation of multiple maps
192 bool mapCreated{false};
193
194 // Eigen::Isometry3f globalPoseFromLongTermMemory(const std::string& agentName, const auto& robot);
195
196 // FIXME remove
197 // std::atomic<int64_t> lastTimestamp{0};
198
199 // topic publisher
200 RemoteGuiInterfacePrx remoteGui;
201 DebugObserverInterfacePrx debugObserver;
202
203 // others
204 // armem::vision::laser_scans::client::Writer laserScansMemoryWriter;
205 // armem::vision::occupancy_grid::client::Writer occupancyGridMemoryWriter;
206 // armem::robot_state::RobotReader robotReader;
207
208 //! properties
209 struct Properties
210 {
211 armarx::data::PackagePath mapPath{.package = "PriorKnowledgeData",
212 .path = "maps/cartographer"};
213
214 std::string mapStorageSubDir;
215
216 armarx::data::PackagePath mapToLoad{.package = "PriorKnowledgeData",
217 .path = "maps/cartographer/..."};
218
219 armarx::data::PackagePath cartographerConfigPath{.package =
220 "armarx_localization_and_mapping",
221 .path = "cartographer/config/"};
222
223 Mode mode = Mode::Mapping;
224 bool enableVisualization = true;
225 bool enableMappingVisualization = true;
226
228
229 int odomQueryPeriodMs = 20;
230 int occupancyGridMemoryUpdatePeriodMs = 1000;
231
232 bool useNativeOdometryTimestamps = true;
233 bool useNativeLaserscannerTimestamps = true;
234
235 std::string laserScanners;
236
237 struct
238 {
239 float global_localization_min_score = 0.5;
240 } cartographer;
241
242 float obstacleRadius = 500; // [mm]
243
244 float occupiedThreshold = 0.55;
245
246 } properties;
247
248 Throttler throttlerLaserScansMemoryWriter{10.F};
249 Throttler throttlerOccupancyGridMemoryWriter{10.F};
250
251 Throttler throttlerArviz{10.F};
252
253 std::unique_ptr<cartographer_adapter::ArVizDrawerMapBuilder> arVizDrawerMapBuilder;
254
255 std::optional<FrequencyReporter> laserDataReporter;
256 std::optional<FrequencyReporter> odomDataReporter;
257 std::optional<FrequencyReporter> slamResultReporter;
258
259 // std::unique_ptr<components::cartographer_localization_and_mapping::RobotUnitReader> robotUnitReader;
260
261 SimplePeriodicTask<>::pointer_type occupancyGridTask;
262 SimplePeriodicTask<>::pointer_type odometryQueryTask;
263
264 std::experimental::observer_ptr<armem::client::plugins::ReaderWriterPlugin<
265 armem::vision::occupancy_grid::client::Writer>>
266 occupancyGridWriterPlugin;
267
268 std::experimental::observer_ptr<
269 armem::client::plugins::ReaderWriterPlugin<navigation::memory::client::costmap::Writer>>
270 costmapWriterPlugin;
271
272 std::experimental::observer_ptr<
273 armem::client::plugins::ReaderWriterPlugin<armem::robot_state::RobotReader>>
274 robotReaderPlugin;
275
277
278 Eigen::Isometry3f world_T_map = Eigen::Isometry3f::Identity();
279 std::optional<Eigen::Isometry3f> map_T_map_correct = std::nullopt;
280 std::optional<Eigen::Isometry3f> map_T_robot = std::nullopt;
281
282 std::atomic_bool receivingData{false};
283
284
285 std::optional<Eigen::Isometry3f> globalPose() const;
286
287 // armarx::plugins::RobotUnitComponentPlugin* robotUnit;
288
289
290 std::mutex inputMtx;
291
292
293 // used within the odometry task
294 int64_t lastTimestamp = 0;
295 std::atomic_bool robotIsMoving = false;
296
297 plugins::HeartbeatComponentPlugin* heartbeatPlugin = nullptr;
298 };
299} // namespace armarx::localization_and_mapping::components::cartographer_localization_and_mapping
The FrequencyReporter class.
IceUtil::Handle< PeriodicTask< T > > pointer_type
Shared pointer type for convenience.
IceUtil::Handle< RunningTask< T > > pointer_type
Shared pointer type for convenience.
The purpose of the MessageQueue is to provide a convenient way to process incoming messages sequentia...
void onLocalizationCorrection(const Eigen::Isometry3f &map_T_map_correct) override
void reportSensorValues(const ::std::string &, const ::std::string &, const ::armarx::LaserScan &, const ::armarx::TimestampBasePtr &, const ::Ice::Current &) override
void onLocalSlamData(const cartographer_adapter::LocalSlamData &slamData) override
Eigen::Isometry3f worldToMapTransform() const override
static transformation from world to map
void onCreateMapButtonClicked(const RemoteGuiCallee::ButtonClickContext &ctx) override
void onLaserSensorData(const cartographer_adapter::LaserScannerData &laserData) override
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.